/**
   * Gets image entity and sends patch to update total datastore and total image datastore field.
   *
   * @param current
   */
  protected void updateTotalImageDatastore(final State current) {
    try {
      // build the image entity update patch
      ImageService.State imageServiceState = new ImageService.State();
      imageServiceState.totalImageDatastore = getZookeeperHostMonitor().getImageDatastores().size();
      imageServiceState.totalDatastore = getZookeeperHostMonitor().getAllDatastores().size();
      Operation imagePatch =
          getCloudStoreHelper()
              .createPatch(ImageServiceFactory.SELF_LINK + "/" + current.image)
              .setBody(imageServiceState);

      // create operation sequence
      OperationSequence sequence =
          OperationSequence.create(imagePatch)
              .setCompletion(
                  (Map<Long, Operation> ops, Map<Long, Throwable> failures) -> {
                    if (failures != null && failures.size() > 0) {
                      failTask(failures.values().iterator().next());
                      return;
                    }
                  });

      if (!current.isSelfProgressionDisabled) {
        // move to next stage
        Operation progress =
            this.buildSelfPatchOperation(
                this.buildPatch(
                    TaskState.TaskStage.STARTED, TaskState.SubStage.TRIGGER_COPIES, null));

        sequence.next(progress);
      }

      sequence.sendWith(this);
    } catch (Exception e) {
      failTask(e);
    }
  }
  private static void commonVmAndImageSetup(
      VmDcpBackend vmDcpBackend, NetworkDcpBackend networkDcpBackend) throws Throwable {
    AttachedDiskCreateSpec disk1 =
        new AttachedDiskCreateSpecBuilder().name("disk1").flavor("core-100").bootDisk(true).build();
    AttachedDiskCreateSpec disk2 =
        new AttachedDiskCreateSpecBuilder().name("disk2").flavor("core-200").capacityGb(10).build();

    List<LocalitySpec> affinities = new ArrayList<>();
    affinities.add(new LocalitySpec("disk-id1", "disk"));
    affinities.add(new LocalitySpec("disk-id2", "disk"));

    ImageService.State imageServiceState = new ImageService.State();
    imageServiceState.name = "image-1";
    imageServiceState.state = ImageState.READY;
    imageServiceState.size = 1024L * 1024L;
    imageServiceState.replicationType = ImageReplicationType.EAGER;
    imageServiceState.imageSettings = new ArrayList<>();
    ImageService.State.ImageSetting imageSetting = new ImageService.State.ImageSetting();
    imageSetting.name = "n1";
    imageSetting.defaultValue = "v1";
    imageServiceState.imageSettings.add(imageSetting);
    imageSetting = new ImageService.State.ImageSetting();
    imageSetting.name = "n2";
    imageSetting.defaultValue = "v2";
    imageServiceState.imageSettings.add(imageSetting);

    Operation result = dcpClient.post(ImageServiceFactory.SELF_LINK, imageServiceState);

    createdImageState = result.getBody(ImageService.State.class);

    imageId = ServiceUtils.getIDFromDocumentSelfLink(createdImageState.documentSelfLink);

    vmCreateSpec = new VmCreateSpec();
    vmCreateSpec.setName("test-vm");
    vmCreateSpec.setFlavor("core-100");
    vmCreateSpec.setSourceImageId(imageId);
    vmCreateSpec.setAttachedDisks(ImmutableList.of(disk1, disk2));
    vmCreateSpec.setAffinities(affinities);
    vmCreateSpec.setTags(ImmutableSet.of("value1", "value2"));

    List<String> networks = new ArrayList<>();

    List<String> portGroups = new ArrayList<>();
    portGroups.add("p1");
    NetworkCreateSpec networkCreateSpec = new NetworkCreateSpec();
    networkCreateSpec.setName("n1");
    networkCreateSpec.setPortGroups(portGroups);
    TaskEntity networkTask = networkDcpBackend.createNetwork(networkCreateSpec);
    networks.add(networkTask.getEntityId());

    portGroups = new ArrayList<>();
    portGroups.add("p2");
    networkCreateSpec.setName("n2");
    networkCreateSpec.setPortGroups(portGroups);
    networkTask = networkDcpBackend.createNetwork(networkCreateSpec);
    networks.add(networkTask.getEntityId());

    vmCreateSpec.setNetworks(networks);

    createdVmTaskEntity = vmDcpBackend.prepareVmCreate(projectId, vmCreateSpec);
  }