@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);
        }

        // Get permission from the Cluster Controller to remove the content nodes.
        for (StorageNode storageNode : applicationApi.getStorageNodesInGroupInClusterOrder()) {
            // Consider changing the semantics of setting storage node state to DOWN in cluster controller, to avoid 2 calls.
            storageNode.setStorageNodeState(context.createSubcontextForSingleAppOp(true), ClusterControllerNodeState.DOWN);
            storageNode.forceDistributorState(context, ClusterControllerNodeState.DOWN);
        }

        // 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);
        }
    }
@Test
    public void testAcquirePermissionToRemoveConfigServer() throws OrchestrationException {
        final HostedVespaClusterPolicy clusterPolicy = mock(HostedVespaClusterPolicy.class);
        final HostedVespaPolicy policy = new HostedVespaPolicy(clusterPolicy, clientFactory, applicationApiFactory, flagSource);
        final ApplicationApi applicationApi = mock(ApplicationApi.class);
        when(applicationApi.applicationId()).thenReturn(ApplicationId.fromSerializedForm("tenant:app:default"));

        ClusterApi clusterApi1 = mock(ClusterApi.class);
        ClusterApi clusterApi2 = mock(ClusterApi.class);
        ClusterApi clusterApi3 = mock(ClusterApi.class);
        List<ClusterApi> clusterApis = List.of(clusterApi1, clusterApi2, clusterApi3);
        when(applicationApi.getClusters()).thenReturn(clusterApis);

        StorageNode storageNode1 = mock(StorageNode.class);
        HostName hostName1 = new HostName("storage-1");
        when(storageNode1.hostName()).thenReturn(hostName1);

        HostName hostName2 = new HostName("host-2");

        StorageNode storageNode3 = mock(StorageNode.class);
        HostName hostName3 = new HostName("storage-3");
        when(storageNode1.hostName()).thenReturn(hostName3);

        List<StorageNode> upStorageNodes = List.of(storageNode1, storageNode3);
        when(applicationApi.getStorageNodesInGroupInClusterOrder()).thenReturn(upStorageNodes);

        List<HostName> noRemarksHostNames = List.of(hostName1, hostName2, hostName3);
        when(applicationApi.getNodesInGroupWith(any())).thenReturn(noRemarksHostNames);

        InOrder order = inOrder(applicationApi, clusterPolicy, storageNode1, storageNode3);

        OrchestratorContext context = mock(OrchestratorContext.class);
        OrchestratorContext probeContext = mock(OrchestratorContext.class);
        when(context.createSubcontextForSingleAppOp(true)).thenReturn(probeContext);
        policy.acquirePermissionToRemove(context, applicationApi);

        order.verify(applicationApi).getClusters();
        order.verify(clusterPolicy).verifyGroupGoingDownPermanentlyIsFine(clusterApi1);
        order.verify(clusterPolicy).verifyGroupGoingDownPermanentlyIsFine(clusterApi2);
        order.verify(clusterPolicy).verifyGroupGoingDownPermanentlyIsFine(clusterApi3);

        order.verify(applicationApi).getStorageNodesInGroupInClusterOrder();
        order.verify(storageNode1).setStorageNodeState(probeContext, ClusterControllerNodeState.DOWN);
        order.verify(storageNode3).setStorageNodeState(probeContext, ClusterControllerNodeState.DOWN);

        order.verify(applicationApi).getNodesInGroupWith(any());
        order.verify(applicationApi).setHostState(context, hostName1, HostStatus.PERMANENTLY_DOWN);
        order.verify(applicationApi).setHostState(context, hostName2, HostStatus.PERMANENTLY_DOWN);
        order.verify(applicationApi).setHostState(context, hostName3, HostStatus.PERMANENTLY_DOWN);

        order.verifyNoMoreInteractions();
    }