class HostedVespaPolicy implements Policy {

    @Override
    public void acquirePermissionToRemove(OrchestratorContext context, ApplicationApi applicationApi)
            throws HostStateChangeDeniedException {
        ApplicationInstanceStatus applicationStatus = applicationApi.getApplicationStatus();
        if (applicationStatus == ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN) {
            throw new HostStateChangeDeniedException(
                    applicationApi.getNodeGroup(),
                    HostedVespaPolicy.APPLICATION_SUSPENDED_CONSTRAINT,
                    "Unable to test availability constraints as the application " +
                            applicationApi.applicationId() + " is allowed to be down");
        }

        // Apply per-cluster policy
        for (ClusterApi cluster : applicationApi.getClusters()) {
            clusterPolicy.verifyGroupGoingDownPermanentlyIsFine(cluster);
        }

        // Ask Cluster Controller to set storage nodes to DOWN.
        // These storage nodes are guaranteed to be NO_REMARKS
        for (StorageNode storageNode : applicationApi.getStorageNodesInGroupInClusterOrder()) {
            storageNode.setNodeState(context, ClusterControllerNodeState.DOWN);
        }

        if (context.usePermanentlyDownStatus()) {
            // Ensure all nodes in the group are marked as permanently down
            for (HostName hostName : applicationApi.getNodesInGroupWith(status -> status != HostStatus.PERMANENTLY_DOWN)) {
                applicationApi.setHostState(context, hostName, HostStatus.PERMANENTLY_DOWN);
            }
        } else {
            for (HostName hostName : applicationApi.getNodesInGroupWith(status -> status != HostStatus.ALLOWED_TO_BE_DOWN)) {
                applicationApi.setHostState(context, hostName, HostStatus.ALLOWED_TO_BE_DOWN);
            }
        }
    }

    public  HostedVespaPolicy(HostedVespaClusterPolicy clusterPolicy,
                             ClusterControllerClientFactory clusterControllerClientFactory,
                             ApplicationApiFactory applicationApiFactory);

}