This is a sample application that is written in Java. It queries entities in Mirage, centralizes endpoints, and migrates a CVD operating system.

//This is the Java sample code of Mirage API, which includes three parts.
//1. Query entities in Mirage, such as policy, volume, pending device, CVD, base layer and app layer
//2. Centralize endpoints.
//3. Migrate CVD Operating System.
//4. CVD assign base layer.
//5. CVD update app layer.
package com.vmware.mirage.mit.sample;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import org.apache.axis2.AxisFault;
import org.apache.axis2.java.security.SSLProtocolSocketFactory;
import org.apache.axis2.java.security.TrustAllTrustManager;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.log4j.Logger;

import com.vmware.mirage.mit.MitServiceStub;
import com.vmware.mirage.mit.MitServiceStub.*;

public class MirageAPISample {

    private static Logger logger = Logger.getLogger(MirageAPISample.class);

    private MitServiceStub client = null;

    private static String ENDPOINT = "https://<Web Management Server IP>:7443/mirageapi/mitservice.svc";

    private static String USERNAME = "<username>";

    private static String PASSWORD = "<password>";

    private static String DOMAIN = "<domain name>";

    public static void main(final String... args) throws Exception {
        final MirageAPISample sample = new MirageAPISample();
        sample.login();
        try {
            sample.policyQuery();
            sample.volumeQuery();
            sample.cvdQuery();
            sample.pendingDeviceQuery();
            sample.baselayerQuery();
            sample.applayerQuery();
            sample.ceFlow();
            sample.migrationFlow();
            sample.provisionFlow();
            sample.cvdAssignBaseLayerFlow();
            sample.cvdUpdateAppLayerFlow();
        } finally {
            try {
                sample.logout();
            } catch (final Exception e) {
                logger.error("Failed to logout.", e);
            }
        }
    }

    // Create and configure API client with ENDPOINT
    public MirageAPISample() throws AxisFault, KeyManagementException, NoSuchAlgorithmException {
        client = new MitServiceStub(ENDPOINT);
        configureClient();
    }

    // Login Mirage API server with username and password
    public void login() throws Exception {

        final Login login = new Login();
        login.setUsername(USERNAME);
        login.setPassword(PASSWORD);
        client.login(login);
    }

    // Logout Mirage API server
    public void logout() throws Exception {
        final Logout logout = new Logout();
        client.logout(logout);
    }

    // Query policy without filter, only the first page returns.
    public void policyQuery() throws Exception {
        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setPage(1);

        final Policy_Query policy_Query = new Policy_Query();
        policy_Query.setQueryDefinition(queryDefinition);
        final Policy_QueryResponse policies = client.policy_Query(policy_Query);
        final QueryResult result = policies.getPolicy_QueryResult();

        if (result.getElements().getAnyType() != null) {
            for (final Object element : result.getElements().getAnyType()) {
                final PolicyDetails policyDetails = (PolicyDetails) element;
                System.out.println(String.format("policyDetails %s %s", policyDetails.getName(),
                        policyDetails.getImageId()));
            }
        }
    }

    // Query volumes without filter, only the first page returns.
    public void volumeQuery() throws Exception {
        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setPage(1);
        queryDefinition.setFilter(null);

        final Volume_Query volume_Query = new Volume_Query();
        volume_Query.setQueryDefinition(queryDefinition);
        final Volume_QueryResponse volumes = client.volume_Query(volume_Query);
        final QueryResult result = volumes.getVolume_QueryResult();

        if (result.getElements().getAnyType() != null) {
            for (final Object element : result.getElements().getAnyType()) {
                final VolumeDetails volumeDetails = (VolumeDetails) element;
                System.out.println(String.format("volumeDetails %s %s", volumeDetails.getName(),
                        volumeDetails.getPath()));
            }
        }
    }

    // Query Cvds whose name starts with "VMware" and progress equals to 100, only the first page returns.
    public void cvdQuery() throws Exception {

        /* name starts with "VMware" */
        final QueryFilterBeginsWith beginFilter = new QueryFilterBeginsWith();
        beginFilter.setField(FilterField.CVD_NAME);
        beginFilter.setValue("VMware");

        /* progress is 100 */
        final QueryFilterEquals equalFilter = new QueryFilterEquals();
        equalFilter.setField(FilterField.CVD_PROGRESS);
        equalFilter.setValue((long) 100); // must use long for progress

        /* Create and filter */
        final QueryFilterAnd andFilter = new QueryFilterAnd();
        final ArrayOfQueryFilter filterArr = new ArrayOfQueryFilter();
        filterArr.addQueryFilter(beginFilter);
        filterArr.addQueryFilter(equalFilter);
        andFilter.setFilters(filterArr);

        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setPage(1);
        queryDefinition.setFilter(andFilter);
        final Cvd_Query cvdQuery = new Cvd_Query();
        cvdQuery.setQueryDefinition(queryDefinition);
        final Cvd_QueryResponse cvds = client.cvd_Query(cvdQuery);
        final QueryResult result = cvds.getCvd_QueryResult();
        if (result.getElements().getAnyType() != null) {
            for (final Object element : result.getElements().getAnyType()) {
                final CvdDetails cvdDetails = (CvdDetails) element;
                System.out.println(String.format("cvdDetails %s %s %s", cvdDetails.getId(), cvdDetails.getName(),
                        cvdDetails.getOperationProgress()));
            }
        }
    }

    // Query pending device whose state is disconnect, only the first page returns.
    public void pendingDeviceQuery() throws Exception {

        /* Disconnected */
        final QueryFilterEquals equalFilter = new QueryFilterEquals();
        equalFilter.setField(FilterField.DEVICE_CONNECTION_STATE);
        equalFilter.setValue(false);

        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setFilter(equalFilter);
        queryDefinition.setPage(1);

        final PendingDevice_Query pendingDeviceQuery = new PendingDevice_Query();
        pendingDeviceQuery.setQueryDefinition(queryDefinition);

        final PendingDevice_QueryResponse pendingDevices = client.pendingDevice_Query(pendingDeviceQuery);
        final QueryResult result = pendingDevices.getPendingDevice_QueryResult();
        if (result.getElements().getAnyType() != null) {
            for (final Object element : result.getElements().getAnyType()) {
                final DeviceDetails deviceDetails = (DeviceDetails) element;
                System.out.println(String.format("deviceDetails %s %s", deviceDetails.getName(),
                        deviceDetails.getConnected()));
            }
        }
    }

    // Query base layers without filter, only the first page returns.
    public void baselayerQuery() throws Exception {
        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setPage(1);

        final BaseLayer_Query baseLayerQuery = new BaseLayer_Query();
        baseLayerQuery.setQueryDefinition(queryDefinition);

        final BaseLayer_QueryResponse baseLayers = client.baseLayer_Query(baseLayerQuery);
        final QueryResult result = baseLayers.getBaseLayer_QueryResult();
        if (result.getElements().getAnyType() != null) {
            for (final Object element : result.getElements().getAnyType()) {
                final LayerDetails baseLayerDetails = (LayerDetails) element;
                System.out.println(String.format("baseLayer %s %s", baseLayerDetails.getName(),
                        baseLayerDetails.getImageId()));
            }
        }
    }

    // Query app layers without filter, only the first page returns.
    public void applayerQuery() throws Exception {
        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setPage(1);

        final AppLayer_Query baseLayerQuery = new AppLayer_Query();
        baseLayerQuery.setQueryDefinition(queryDefinition);

        final AppLayer_QueryResponse baseLayers = client.appLayer_Query(baseLayerQuery);
        final QueryResult result = baseLayers.getAppLayer_QueryResult();
        if (result.getElements().getAnyType() != null) {
            for (final Object element : result.getElements().getAnyType()) {
                final LayerDetails baseLayerDetails = (LayerDetails) element;
                System.out.println(String.format("appLayer %s %s", baseLayerDetails.getName(),
                        baseLayerDetails.getImageId()));
            }
        }
    }

    // Centralization endpoint flow.
    public void ceFlow() throws Exception {
        /* Get policy */
        final QueryFilterContains policyFilter = new QueryFilterContains();
        policyFilter.setField(FilterField.POLICY_NAME);
        policyFilter.setValue("default");
        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setPage(1);
        queryDefinition.setFilter(policyFilter);
        final Policy_Query policyQuery = new Policy_Query();
        policyQuery.setQueryDefinition(queryDefinition);
        final Policy_QueryResponse policyResponse = client.policy_Query(policyQuery);
        final Object[] policyArr = policyResponse.getPolicy_QueryResult().getElements().getAnyType();
        if (policyArr.length == 0) {
            return;
        }
        final PolicyDetails policy = (PolicyDetails) policyArr[0];

        /* Get volume */
        final QueryFilterEquals volumeFilter = new QueryFilterEquals();
        policyFilter.setField(FilterField.VOLUME_NAME);
        policyFilter.setValue("DefaultVolume");
        queryDefinition.setFilter(volumeFilter);
        final Volume_Query volumeQuery = new Volume_Query();
        volumeQuery.setQueryDefinition(queryDefinition);
        final Volume_QueryResponse volumeResponse = client.volume_Query(volumeQuery);
        final Object[] volumeArr = volumeResponse.getVolume_QueryResult().getElements().getAnyType();
        if (volumeArr.length == 0) {
            return;
        }
        final VolumeDetails volume = (VolumeDetails) volumeArr[0];

        /* Get device */
        final PendingDevice_Query pendingDeviceQuery = new PendingDevice_Query();
        pendingDeviceQuery.setQueryDefinition(queryDefinition);
        final PendingDevice_QueryResponse pendingDeviceResponse = client.pendingDevice_Query(pendingDeviceQuery);
        final Object[] deviceArr = pendingDeviceResponse.getPendingDevice_QueryResult().getElements().getAnyType();
        if (deviceArr.length == 0) {
            return;
        }
        final DeviceDetails device = (DeviceDetails) deviceArr[0];

        /* Create new Cvd */
        final PendingDevice_CreateNewCvd pendingDeviceCreateNewCvd = new PendingDevice_CreateNewCvd();
        pendingDeviceCreateNewCvd.setPolicyId(policy.getImageId());
        pendingDeviceCreateNewCvd.setVolumeId(volume.getId());
        final ArrayOfId ids = new ArrayOfId();
        ids.addId(device.getId());
        pendingDeviceCreateNewCvd.setDeviceIds(ids);
        final PendingDevice_CreateNewCvdResponse batchResultResponse = client
                .pendingDevice_CreateNewCvd(pendingDeviceCreateNewCvd);
        final BatchResult batchResult = batchResultResponse.getPendingDevice_CreateNewCvdResult();

        /* Get the Id of new Cvd from BatchResult */
        final OperationResult[] operationResultArr = batchResult.getResults().getOperationResult();
        if (operationResultArr.length == 0) {
            throw new Exception("There is no result for PendingDevice_CreateNewCvd.");
        }
        final OperationResult result = operationResultArr[0];
        final long idValue = (long) result.getResult();
        final Id cvdId = new Id();
        cvdId.setIdValue(idValue);

        /* Validate the completion of creating Cvd */
        final QueryFilterEquals equalFilter = new QueryFilterEquals();
        equalFilter.setField(FilterField.CVD_ID);
        equalFilter.setValue(cvdId);
        queryDefinition.setFilter(equalFilter);
        queryDefinition.setPage(1);
        final Cvd_Query cvdQuery = new Cvd_Query();
        cvdQuery.setQueryDefinition(queryDefinition);
        while (true) {
            final Cvd_QueryResponse cvds = client.cvd_Query(cvdQuery);
            final QueryResult cvdResult = cvds.getCvd_QueryResult();
            final Object[] cvdResultArr = cvdResult.getElements().getAnyType();
            if (cvdResultArr.length > 0) {
                final CvdDetails cvdDetails = (CvdDetails) cvdResultArr[0];
                final ImageVersion machineVersion = cvdDetails.getMachineVersion();
                /* The complete condition of CE flow is that the machine version of CVD is not 1.0 */
                if (machineVersion.getMajor() != 1 || machineVersion.getMinor() != 0) {
                    break;
                }
                TimeUnit.MINUTES.sleep(2);
            }
        }
        System.out.println("OK to create new cvd.");
    }

    public void provisionFlow() throws Exception {
        /* Get policy */
        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setPage(1);
        final Policy_Query policyQuery = new Policy_Query();
        policyQuery.setQueryDefinition(queryDefinition);
        final Policy_QueryResponse policyResponse = client.policy_Query(policyQuery);
        final Object[] policyArr = policyResponse.getPolicy_QueryResult().getElements().getAnyType();
        if (policyArr.length == 0) {
            return;
        }
        final PolicyDetails policy = (PolicyDetails) policyArr[0];

        /* Get volume */
        final Volume_Query volumeQuery = new Volume_Query();
        volumeQuery.setQueryDefinition(queryDefinition);
        final Volume_QueryResponse volumeResponse = client.volume_Query(volumeQuery);
        final Object[] volumeArr = volumeResponse.getVolume_QueryResult().getElements().getAnyType();
        if (volumeArr.length == 0) {
            return;
        }
        final VolumeDetails volume = (VolumeDetails) volumeArr[0];

        /* Get device */
        final PendingDevice_Query pendingDeviceQuery = new PendingDevice_Query();
        pendingDeviceQuery.setQueryDefinition(queryDefinition);
        final PendingDevice_QueryResponse pendingDeviceResponse = client.pendingDevice_Query(pendingDeviceQuery);
        final Object[] deviceArr = pendingDeviceResponse.getPendingDevice_QueryResult().getElements().getAnyType();
        if (deviceArr.length == 0) {
            return;
        }
        final DeviceDetails device = (DeviceDetails) deviceArr[0];

        /* Get base layer */
        final BaseLayer_Query baseLayerQuery = new BaseLayer_Query();
        baseLayerQuery.setQueryDefinition(queryDefinition);
        final BaseLayer_QueryResponse baseLayerResponse = client.baseLayer_Query(baseLayerQuery);
        final Object[] baseLayerArr = baseLayerResponse.getBaseLayer_QueryResult().getElements().getAnyType();
        if (baseLayerArr.length == 0) {
            return;
        }
        final LayerDetails baseLayerDetails = (LayerDetails) baseLayerArr[0];

        /* Start device provision */
        final MachineIdentityInfo machineIdentityInfo = new MachineIdentityInfo();
        machineIdentityInfo.setDomainMember(true);
        machineIdentityInfo.setDomainOrWorkgroupName(DOMAIN);
        machineIdentityInfo.setUser(USERNAME);
        machineIdentityInfo.setPassword(PASSWORD);

        final PendingDevice_Provision pendingDeviceProvision = new PendingDevice_Provision();
        pendingDeviceProvision.setPolicyImageId(policy.getImageId());
        pendingDeviceProvision.setVolumeId(volume.getId());
        pendingDeviceProvision.setBaseLayerImageId(baseLayerDetails.getImageId());
        pendingDeviceProvision.setIdentityInfo(machineIdentityInfo);
        pendingDeviceProvision.setIgnoreWarnings(true);

        final ArrayOfId ids = new ArrayOfId();
        ids.addId(device.getId());
        pendingDeviceProvision.setPendingDevices(ids);
        final PendingDevice_ProvisionResponse provisionResponse = client
                .pendingDevice_Provision(pendingDeviceProvision);
        final BatchResult batchResult = provisionResponse.getPendingDevice_ProvisionResult();
        final OperationResult[] provisionResultArr = batchResult.getResults().getOperationResult();
        if (provisionResultArr.length == 0) {
            throw new Exception("There is no result for pendingDevice_Provision.");
        }
        final OperationResult result = provisionResultArr[0];
        if (result.getSuccess() == false) {
            final String message = result.getFault() == null ? "" : result.getFault().getMessage();
            throw new Exception("Failed to provision device. " + message);
        }

        /* Wait for provision complete */
        final long idValue = (long) result.getResult();
        final Id cvdId = new Id();
        cvdId.setIdValue(idValue);

        waitForAssignmentComplete("DeviceProvisioning", cvdId);
    }

    // OS migration flow.
    public void migrationFlow() throws Exception {
        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setPage(1);

        final Cvd_Query cvdQuery = new Cvd_Query();
        cvdQuery.setQueryDefinition(queryDefinition);

        final Cvd_QueryResponse cvdResponse = client.cvd_Query(cvdQuery);
        final QueryResult cvdResult = cvdResponse.getCvd_QueryResult();

        final Object[] cvdArr = cvdResult.getElements().getAnyType();
        if (cvdArr.length == 0) {
            throw new Exception("There is no result for Cvd_Query");
        }

        final CvdDetails cvd = (CvdDetails) cvdArr[0];

        final BaseLayer_Query baseLayerQuery = new BaseLayer_Query();
        baseLayerQuery.setQueryDefinition(queryDefinition);
        final BaseLayer_QueryResponse baseLayerResponse = client.baseLayer_Query(baseLayerQuery);
        final QueryResult baseLayerResult = baseLayerResponse.getBaseLayer_QueryResult();

        final Object[] baseLayerArr = baseLayerResult.getElements().getAnyType();
        if (baseLayerArr.length == 0) {
            throw new Exception("There is no result for baseLayer_Query");
        }
        final LayerDetails baseLayer = (LayerDetails) baseLayerArr[0];

        final MachineIdentityInfo machineIdentityInfo = new MachineIdentityInfo();
        machineIdentityInfo.setDomainMember(true);
        machineIdentityInfo.setDomainOrWorkgroupName(DOMAIN);
        machineIdentityInfo.setUser(USERNAME);
        machineIdentityInfo.setPassword(PASSWORD);

        final MigrationTarget migrationTarget = new MigrationTarget();
        migrationTarget.setIdentityInfo(machineIdentityInfo);
        migrationTarget.setCvdId(cvd.getId());

        final ArrayOfMigrationTarget migrationTargets = new ArrayOfMigrationTarget();
        migrationTargets.addMigrationTarget(migrationTarget);

        final OsMigration_Begin osMigration_Begin = new OsMigration_Begin();
        osMigration_Begin.setMigrationTargets(migrationTargets);
        osMigration_Begin.setBaseLayerId(baseLayer.getImageId());
        osMigration_Begin.setAppLayerIds(null);
        osMigration_Begin.setIgnoreWarnings(true);

        final OsMigration_BeginResponse migrationResponse = client.osMigration_Begin(osMigration_Begin);
        final BatchResult migrationResult = migrationResponse.getOsMigration_BeginResult();

        final OperationResult[] results = migrationResult.getResults().getOperationResult();
        if (results.length == 0) {
            throw new Exception("There is no result for osMigration_Begin");
        }

        final OperationResult result = results[0];

        if (result.getSuccess() == true) {
            System.out.println(String.format("Migration begin successfully for Cvd(Id=%d)", cvd.getId().getIdValue()));
        } else {
            System.out.println(String.format("Migration begin failed for Cvd(Id=%d), fault is: %s", cvd.getId()
                    .getIdValue(), result.getFault().getMessage()));
        }

        /* Wait for migration complete */
        waitForAssignmentComplete("Migration", cvd.getId());
    }

    //Cvd assign base layer flow
    public void cvdAssignBaseLayerFlow() throws Exception {
        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setPage(1);

        final Cvd_Query cvdQuery = new Cvd_Query();
        cvdQuery.setQueryDefinition(queryDefinition);

        final Cvd_QueryResponse cvdResponse = client.cvd_Query(cvdQuery);
        final QueryResult cvdResult = cvdResponse.getCvd_QueryResult();

        final Object[] cvdArr = cvdResult.getElements().getAnyType();
        if (cvdArr.length == 0) {
            throw new Exception("There is no result for Cvd_Query");
        }

        final CvdDetails cvd = (CvdDetails) cvdArr[0];

        final BaseLayer_Query baseLayerQuery = new BaseLayer_Query();
        baseLayerQuery.setQueryDefinition(queryDefinition);
        final BaseLayer_QueryResponse baseLayerResponse = client.baseLayer_Query(baseLayerQuery);
        final QueryResult baseLayerResult = baseLayerResponse.getBaseLayer_QueryResult();

        final Object[] baseLayerArr = baseLayerResult.getElements().getAnyType();
        if (baseLayerArr.length == 0) {
            throw new Exception("There is no result for BaseLayer_Query");
        }
        final LayerDetails baseLayer = (LayerDetails) baseLayerArr[0];

        /* Assign base layer to the CVD */
        final Cvd_AssignBaseLayer cvdAssignBaseLayer = new Cvd_AssignBaseLayer();
        final ArrayOfId ids = new ArrayOfId();
        ids.addId(cvd.getId());
        cvdAssignBaseLayer.setCvdIds(ids);
        cvdAssignBaseLayer.setBaseLayerImageId(baseLayer.getImageId());
        cvdAssignBaseLayer.setIgnoreWarnings(true);

        final Cvd_AssignBaseLayerResponse assignResponse = client.cvd_AssignBaseLayer(cvdAssignBaseLayer);
        final BatchResult assignResult = assignResponse.getCvd_AssignBaseLayerResult();

        final OperationResult[] results = assignResult.getResults().getOperationResult();
        if (results.length == 0) {
            throw new Exception("There is no result for cvd_AssignBaseLayer");
        }

        final OperationResult result = results[0];

        if (result.getSuccess() == true) {
            System.out.println(String.format("Cvd assign baselayer begin successfully for Cvd(Id=%d)", cvd.getId()
                    .getIdValue()));
        } else {
            System.out.println(String.format("Cvd assign baselayer begin failed for Cvd(Id=%d), fault is: %s", cvd
                    .getId().getIdValue(), result.getFault().getMessage()));
        }

        /* Wait for base layer assign complete */
        waitForAssignmentComplete("AssignBaseImage", cvd.getId());
    }

    //Cvd update app layer flow
    public void cvdUpdateAppLayerFlow() throws Exception {
        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setPage(1);

        final Cvd_Query cvdQuery = new Cvd_Query();
        cvdQuery.setQueryDefinition(queryDefinition);

        final Cvd_QueryResponse cvdResponse = client.cvd_Query(cvdQuery);
        final QueryResult cvdResult = cvdResponse.getCvd_QueryResult();

        final Object[] cvdArr = cvdResult.getElements().getAnyType();
        if (cvdArr.length == 0) {
            throw new Exception("There is no result for Cvd_Query");
        }

        final CvdDetails cvd = (CvdDetails) cvdArr[0];

        final AppLayer_Query appLayerQuery = new AppLayer_Query();
        appLayerQuery.setQueryDefinition(queryDefinition);
        final AppLayer_QueryResponse appLayerResponse = client.appLayer_Query(appLayerQuery);
        final QueryResult appLayerResult = appLayerResponse.getAppLayer_QueryResult();

        final Object[] appLayerArr = appLayerResult.getElements().getAnyType();
        if (appLayerArr.length == 0) {
            throw new Exception("There is no result for AppLayer_Query");
        }
        final LayerDetails appLayer = (LayerDetails) appLayerArr[0];

        /*Update Cvd app layer*/
        final Cvd_UpdateAppLayer cvdUpdateAppLayer = new Cvd_UpdateAppLayer();
        final ArrayOfId ids = new ArrayOfId();
        ids.addId(cvd.getId());
        cvdUpdateAppLayer.setCvdIds(ids);
        final ArrayOfImageId appLayerIds = new ArrayOfImageId();
        appLayerIds.addImageId(appLayer.getImageId());
        cvdUpdateAppLayer.setAddAppLayerImageIds(appLayerIds);
        cvdUpdateAppLayer.setRemoveAppLayerImageIds(null);
        cvdUpdateAppLayer.setIgnoreWarnings(true);

        final Cvd_UpdateAppLayerResponse assignResponse = client.cvd_UpdateAppLayer(cvdUpdateAppLayer);
        final BatchResult assignResult = assignResponse.getCvd_UpdateAppLayerResult();

        final OperationResult[] results = assignResult.getResults().getOperationResult();
        if (results.length == 0) {
            throw new Exception("There is no result for cvd_UpdateAppLayer");
        }

        final OperationResult result = results[0];

        if (result.getSuccess() == true) {
            System.out.println(String.format("Cvd update applayer begin successfully for Cvd(Id=%d)", cvd.getId()
                    .getIdValue()));
        } else {
            System.out.println(String.format("Cvd update applayer begin failed for Cvd(Id=%d), fault is: %s", cvd
                    .getId().getIdValue(), result.getFault().getMessage()));
        }
        /*Wait for app layer update complete*/
        waitForAssignmentComplete("AssignAppLayer", cvd.getId());
    }

    private void waitForAssignmentComplete(final String taskName, final Id cvdId) throws Exception {
        final QueryFilterEquals taskNameFilter = new QueryFilterEquals();
        taskNameFilter.setField(FilterField.ASSIGNMENT_TASK_NAME);
        taskNameFilter.setValue(taskName);

        final QueryFilterEquals cvdIdFilter = new QueryFilterEquals();
        cvdIdFilter.setField(FilterField.ASSIGNMENT_CVD_ID);
        cvdIdFilter.setValue(cvdId);

        final QueryFilterAnd andFilter = new QueryFilterAnd();
        final ArrayOfQueryFilter filterArr = new ArrayOfQueryFilter();
        filterArr.addQueryFilter(taskNameFilter);
        filterArr.addQueryFilter(cvdIdFilter);
        andFilter.setFilters(filterArr);

        final QueryDefinition queryDefinition = new QueryDefinition();
        queryDefinition.setFilter(andFilter);
        queryDefinition.setPage(1);

        final Assignment_Query assignmentQuery = new Assignment_Query();
        assignmentQuery.setQueryDefinition(queryDefinition);

        while (true) {
            final Assignment_QueryResponse assignmentResponse = client.assignment_Query(assignmentQuery);
            final QueryResult assignmentResult = assignmentResponse.getAssignment_QueryResult();
            final Object[] assignmentArr = assignmentResult.getElements().getAnyType();
            if (IsAssignmentDone(assignmentArr)) {
                break;
            }
            TimeUnit.MINUTES.sleep(2);
        }
    }

    private boolean IsAssignmentDone(final Object[] assignments) {
        if (assignments.length == 0) {
            return false;
        }
        final HashSet<String> statusFailed = new HashSet<String>();
        statusFailed.add("Cancelled");
        statusFailed.add("Rejected");
        statusFailed.add("Blocked");
        final String statusDone = "Done";
        boolean assignmentDone = true;
        for (final Object assignment : assignments) {
            final AssignmentDetails assignmentDetails = (AssignmentDetails) assignment;
            if (statusFailed.contains(assignmentDetails.getStatus())) {
                System.out.println(String.format("Assignment failed for Cvd(Id=%d), operation failed.",
                        assignmentDetails.getCvdId().getIdValue()));
                return true;
            }
            assignmentDone &= assignmentDetails.getStatus().equals(statusDone);
        }
        if (assignmentDone) {
            System.out.println("Assignment done, operation succeed.");
        }
        return assignmentDone;
    }

    // Configure client to skip certificate validation and support Http session
    private void configureClient() throws NoSuchAlgorithmException, KeyManagementException {
        final SSLContext sslCtx = SSLContext.getInstance("TLS");
        sslCtx.init(null, new TrustManager[] { new TrustAllTrustManager() }, null);
        client._getServiceClient()
                .getOptions()
                .setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER,
                        new Protocol("https", (ProtocolSocketFactory) new SSLProtocolSocketFactory(sslCtx), 7443));
        final HttpState httpState = new HttpState();
        client._getServiceClient().getOptions()
                .setProperty(org.apache.axis2.transport.http.HTTPConstants.CACHED_HTTP_STATE, httpState);
    }
}