@Override
  public void exportGroupRemoveVolumes(
      URI storageURI, URI exportGroupURI, List<URI> volumeURIs, String token) throws Exception {
    /*
     * foreach volume in list
     * foreach initiator in ExportGroup
     * if volume not used in another ExportGroup with same initiator
     * scli unmap --volume volid --sdc initiator.sdcid
     */
    ExportOrchestrationTask taskCompleter = new ExportOrchestrationTask(exportGroupURI, token);
    try {
      ExportGroup exportGroup = _dbClient.queryObject(ExportGroup.class, exportGroupURI);
      StorageSystem storage = _dbClient.queryObject(StorageSystem.class, storageURI);

      List<ExportMask> masks = ExportMaskUtils.getExportMasks(_dbClient, exportGroup, storageURI);
      if (masks != null && !masks.isEmpty()) {
        // Set up workflow steps.
        Workflow workflow =
            _workflowService.getNewWorkflow(
                MaskingWorkflowEntryPoints.getInstance(), "exportGroupRemoveVolumes", true, token);

        // Generate a list of Initiators
        List<URI> initiatorURIs = StringSetUtil.stringSetToUriList(exportGroup.getInitiators());
        Map<URI, List<URI>> exportToRemoveVolumesList = new HashMap<>();
        // Generate a mapping of volume URIs to the # of
        // ExportGroups that it is associated with
        Map<URI, Map<URI, Integer>> exportMaskToVolumeCount =
            ExportMaskUtils.mapExportMaskToVolumeShareCount(_dbClient, volumeURIs, initiatorURIs);

        // Generate a mapping of the ExportMask URI to a list volumes to
        // remove from that ExportMask
        for (ExportMask exportMask : masks) {
          Map<URI, Integer> volumeToCountMap = exportMaskToVolumeCount.get(exportMask.getId());
          if (volumeToCountMap == null) {
            continue;
          }
          for (Map.Entry<URI, Integer> it : volumeToCountMap.entrySet()) {
            URI volumeURI = it.getKey();
            Integer numberOfExportGroupsVolumesIsIn = it.getValue();
            if (numberOfExportGroupsVolumesIsIn == 1) {
              List<URI> volumesToRemove = exportToRemoveVolumesList.get(exportMask.getId());
              if (volumesToRemove == null) {
                volumesToRemove = new ArrayList<>();
                exportToRemoveVolumesList.put(exportMask.getId(), volumesToRemove);
              }
              volumesToRemove.add(volumeURI);
            }
          }
        }

        // With the mapping of ExportMask to list of volume URIs,
        // generate a step to remove the volumes from the ExportMask
        for (Map.Entry<URI, List<URI>> entry : exportToRemoveVolumesList.entrySet()) {
          ExportMask exportMask = _dbClient.queryObject(ExportMask.class, entry.getKey());
          log.info(
              String.format(
                  "Adding step to remove volumes %s from ExportMask %s",
                  Joiner.on(',').join(entry.getValue()), exportMask.getMaskName()));
          generateExportMaskRemoveVolumesWorkflow(
              workflow, null, storage, exportGroup, exportMask, entry.getValue(), null);
        }

        String successMessage =
            String.format(
                "ExportGroup remove volumes successfully applied for StorageArray %s",
                storage.getLabel());
        workflow.executePlan(taskCompleter, successMessage);
      } else {
        taskCompleter.ready(_dbClient);
      }
    } catch (DeviceControllerException dex) {
      taskCompleter.error(
          _dbClient,
          DeviceControllerErrors.scaleio.encounteredAnExceptionFromScaleIOOperation(
              "exportGroupRemoveVolumes", dex.getMessage()));
    } catch (Exception ex) {
      _log.error("ExportGroup Orchestration failed.", ex);
      taskCompleter.error(
          _dbClient,
          DeviceControllerErrors.scaleio.encounteredAnExceptionFromScaleIOOperation(
              "exportGroupRemoveVolumes", ex.getMessage()));
    }
  }
  @Override
  public void exportGroupDelete(URI storageURI, URI exportGroupURI, String token) throws Exception {
    ExportOrchestrationTask taskCompleter = new ExportOrchestrationTask(exportGroupURI, token);
    try {
      ExportGroup exportGroup = _dbClient.queryObject(ExportGroup.class, exportGroupURI);
      StorageSystem storage = _dbClient.queryObject(StorageSystem.class, storageURI);

      List<ExportMask> masks = ExportMaskUtils.getExportMasks(_dbClient, exportGroup, storageURI);
      if (masks != null && !masks.isEmpty()) {
        Workflow workflow =
            _workflowService.getNewWorkflow(
                MaskingWorkflowEntryPoints.getInstance(), "exportGroupDelete", true, token);

        Map<URI, Integer> volumeMap =
            ExportUtils.getExportGroupVolumeMap(_dbClient, storage, exportGroup);
        List<URI> volumeURIs = new ArrayList<>(volumeMap.keySet());
        List<URI> initiatorURIs = StringSetUtil.stringSetToUriList(exportGroup.getInitiators());
        Map<URI, Map<URI, Integer>> exportMaskToVolumeCount =
            ExportMaskUtils.mapExportMaskToVolumeShareCount(_dbClient, volumeURIs, initiatorURIs);

        for (ExportMask exportMask : masks) {
          List<URI> exportGroupURIs = new ArrayList<>();
          if (!ExportUtils.isExportMaskShared(_dbClient, exportMask.getId(), exportGroupURIs)) {
            log.info(
                String.format("Adding step to delete ExportMask %s", exportMask.getMaskName()));
            generateExportMaskDeleteWorkflow(
                workflow, null, storage, exportGroup, exportMask, null);
          } else {
            Map<URI, Integer> volumeToExportGroupCount =
                exportMaskToVolumeCount.get(exportMask.getId());
            List<URI> volumesToRemove = new ArrayList<>();
            for (URI uri : volumeMap.keySet()) {
              if (volumeToExportGroupCount == null) {
                continue;
              }
              // Remove the volume only if it is not shared with
              // more than 1 ExportGroup
              Integer numExportGroupsVolumeIsIn = volumeToExportGroupCount.get(uri);
              if (numExportGroupsVolumeIsIn != null && numExportGroupsVolumeIsIn == 1) {
                volumesToRemove.add(uri);
              }
            }
            if (!volumesToRemove.isEmpty()) {
              log.info(
                  String.format(
                      "Adding step to remove volumes %s from ExportMask %s",
                      Joiner.on(',').join(volumesToRemove), exportMask.getMaskName()));
              generateExportMaskRemoveVolumesWorkflow(
                  workflow, null, storage, exportGroup, exportMask, volumesToRemove, null);
            }
          }
        }

        String successMessage =
            String.format(
                "ExportGroup delete successfully completed for StorageArray %s",
                storage.getLabel());
        workflow.executePlan(taskCompleter, successMessage);
      } else {
        taskCompleter.ready(_dbClient);
      }
    } catch (DeviceControllerException dex) {
      taskCompleter.error(
          _dbClient,
          DeviceControllerErrors.scaleio.encounteredAnExceptionFromScaleIOOperation(
              "exportGroupDelete", dex.getMessage()));
    } catch (Exception ex) {
      _log.error("ExportGroup Orchestration failed.", ex);
      taskCompleter.error(
          _dbClient,
          DeviceControllerErrors.scaleio.encounteredAnExceptionFromScaleIOOperation(
              "exportGroupDelete", ex.getMessage()));
    }
  }
  @Override
  public void deleteOrRemoveVolumesFromExportMask(
      URI arrayURI,
      URI exportGroupURI,
      URI exportMaskURI,
      List<URI> volumes,
      List<URI> initiatorURIs,
      TaskCompleter completer,
      String stepId) {
    try {
      StorageSystem array = _dbClient.queryObject(StorageSystem.class, arrayURI);
      BlockStorageDevice device = _blockController.getDevice(array.getSystemType());
      ExportMask exportMask = _dbClient.queryObject(ExportMask.class, exportMaskURI);
      WorkflowStepCompleter.stepExecuting(stepId);

      // If the exportMask isn't found, or has been deleted, nothing to do.
      if (exportMask == null || exportMask.getInactive()) {
        _log.info(String.format("ExportMask %s inactive, returning success", exportMaskURI));
        WorkflowStepCompleter.stepSucceded(stepId);
        return;
      }

      // Protect concurrent operations by locking {host, array} dupple.
      // Lock will be released when workflow step completes.
      List<String> lockKeys =
          ControllerLockingUtil.getHostStorageLockKeys(
              _dbClient,
              ExportGroupType.Host,
              StringSetUtil.stringSetToUriList(exportMask.getInitiators()),
              arrayURI);
      getWorkflowService()
          .acquireWorkflowStepLocks(
              stepId, lockKeys, LockTimeoutValue.get(LockType.VPLEX_BACKEND_EXPORT));

      // Make sure the completer will complete the workflow. This happens on rollback case.
      if (!completer.getOpId().equals(stepId)) {
        completer.setOpId(stepId);
      }

      // Refresh the ExportMask
      exportMask = refreshExportMask(array, device, exportMask);

      // Determine if we're deleting the last volume in the mask.
      StringMap maskVolumesMap = exportMask.getVolumes();
      Set<String> remainingVolumes = new HashSet<String>();
      List<URI> passedVolumesInMask = new ArrayList<>(volumes);
      if (maskVolumesMap != null) {
        remainingVolumes.addAll(maskVolumesMap.keySet());
      }
      for (URI volume : volumes) {
        remainingVolumes.remove(volume.toString());

        // Remove any volumes from the volume list that are no longer
        // in the export mask. When a failure occurs removing a backend
        // volume from a mask, the rollback method will try and remove it
        // again. However, in the case of a distributed volume, one side
        // may have succeeded, so we will try and remove it again. Previously,
        // this was not a problem. However, new validation exists at the
        // block level that checks to make sure the volume to remove is
        // actually in the mask, which now causes a failure when you remove
        // it a second time. So, we check here and remove any volumes that
        // are not in the mask to handle this condition.
        if ((maskVolumesMap != null) && (!maskVolumesMap.keySet().contains(volume.toString()))) {
          passedVolumesInMask.remove(volume);
        }
      }

      // None of the volumes is in the export mask, so we are done.
      if (passedVolumesInMask.isEmpty()) {
        _log.info(
            "None of these volumes {} are in export mask {}", volumes, exportMask.forDisplay());
        WorkflowStepCompleter.stepSucceded(stepId);
        return;
      }

      // If it is last volume and there are no existing volumes, delete the ExportMask.
      if (remainingVolumes.isEmpty() && !exportMask.hasAnyExistingVolumes()) {
        device.doExportDelete(array, exportMask, passedVolumesInMask, initiatorURIs, completer);
      } else {
        List<Initiator> initiators = null;
        if (initiatorURIs != null && !initiatorURIs.isEmpty()) {
          initiators = _dbClient.queryObject(Initiator.class, initiatorURIs);
        }
        device.doExportRemoveVolumes(array, exportMask, passedVolumesInMask, initiators, completer);
      }
    } catch (Exception ex) {
      _log.error("Failed to delete or remove volumes to export mask for vnx: ", ex);
      VPlexApiException vplexex =
          DeviceControllerExceptions.vplex.addStepsForCreateVolumesFailed(ex);
      WorkflowStepCompleter.stepFailed(stepId, vplexex);
    }
  }
  @Override
  public void exportGroupAddVolumes(
      URI storageURI, URI exportGroupURI, Map<URI, Integer> volumeMap, String token)
      throws Exception {
    /*
     * foreach volume in list
     * foreach initiator in ExportGroup
     * scli map --volume volid --sdc initiator.sdcid
     */
    ExportOrchestrationTask taskCompleter = new ExportOrchestrationTask(exportGroupURI, token);
    try {
      ExportGroup exportGroup = _dbClient.queryObject(ExportGroup.class, exportGroupURI);
      StorageSystem storage = _dbClient.queryObject(StorageSystem.class, storageURI);

      List<URI> initiatorURIs = StringSetUtil.stringSetToUriList(exportGroup.getInitiators());
      if (initiatorURIs != null && !initiatorURIs.isEmpty()) {
        // Set up workflow steps.
        Workflow workflow =
            _workflowService.getNewWorkflow(
                MaskingWorkflowEntryPoints.getInstance(), "exportGroupAddVolumes", true, token);

        // Create a mapping of ExportMasks to Add Volumes to or
        // add to a list of new Exports to create
        Map<URI, Map<URI, Integer>> exportMaskToVolumesToAdd = new HashMap<>();
        List<URI> initiatorsToPlace = new ArrayList<>(initiatorURIs);

        // Need to figure out which ExportMasks to add volumes to.
        for (ExportMask exportMask :
            ExportMaskUtils.getExportMasks(_dbClient, exportGroup, storageURI)) {
          if (exportMask.hasAnyInitiators()) {
            exportMaskToVolumesToAdd.put(exportMask.getId(), volumeMap);
            for (String uriString : exportMask.getInitiators()) {
              URI initiatorURI = URI.create(uriString);
              initiatorsToPlace.remove(initiatorURI);
            }
          }
        }

        Map<String, List<URI>> computeResourceToInitiators =
            mapInitiatorsToComputeResource(exportGroup, initiatorsToPlace);
        log.info(
            String.format(
                "Need to create ExportMasks for these compute resources %s",
                Joiner.on(',').join(computeResourceToInitiators.entrySet())));
        // ExportMask that need to be newly create because we just added
        // volumes from 'storage' StorageSystem to this ExportGroup
        for (Map.Entry<String, List<URI>> toCreate : computeResourceToInitiators.entrySet()) {
          generateExportMaskCreateWorkflow(
              workflow, null, storage, exportGroup, toCreate.getValue(), volumeMap, token);
        }

        log.info(
            String.format(
                "Need to add volumes for these ExportMasks %s",
                exportMaskToVolumesToAdd.entrySet()));
        // We already know about the ExportMask, so we just add volumes to it
        for (Map.Entry<URI, Map<URI, Integer>> toAddVolumes : exportMaskToVolumesToAdd.entrySet()) {
          ExportMask exportMask = _dbClient.queryObject(ExportMask.class, toAddVolumes.getKey());
          generateExportMaskAddVolumesWorkflow(
              workflow, null, storage, exportGroup, exportMask, toAddVolumes.getValue());
        }

        String successMessage =
            String.format(
                "ExportGroup add volumes successfully applied for StorageArray %s",
                storage.getLabel());
        workflow.executePlan(taskCompleter, successMessage);
      } else {
        taskCompleter.ready(_dbClient);
      }
    } catch (DeviceControllerException dex) {
      taskCompleter.error(
          _dbClient,
          DeviceControllerErrors.scaleio.encounteredAnExceptionFromScaleIOOperation(
              "exportGroupAddVolumes", dex.getMessage()));
    } catch (Exception ex) {
      _log.error("ExportGroup Orchestration failed.", ex);
      taskCompleter.error(
          _dbClient,
          DeviceControllerErrors.scaleio.encounteredAnExceptionFromScaleIOOperation(
              "exportGroupAddVolumes", ex.getMessage()));
    }
  }
  @Override
  public void createOrAddVolumesToExportMask(
      URI arrayURI,
      URI exportGroupURI,
      URI exportMaskURI,
      Map<URI, Integer> volumeMap,
      List<URI> initiatorURIs2,
      TaskCompleter completer,
      String stepId) {
    try {
      StorageSystem array = _dbClient.queryObject(StorageSystem.class, arrayURI);
      ExportMask exportMask = _dbClient.queryObject(ExportMask.class, exportMaskURI);
      WorkflowStepCompleter.stepExecuting(stepId);

      // If the exportMask isn't found, or has been deleted, fail, ask user to retry.
      if (exportMask == null || exportMask.getInactive()) {
        _log.info(String.format("ExportMask %s deleted or inactive, failing", exportMaskURI));
        ServiceError svcerr =
            VPlexApiException.errors.createBackendExportMaskDeleted(
                exportMaskURI.toString(), arrayURI.toString());
        WorkflowStepCompleter.stepFailed(stepId, svcerr);
        return;
      }

      // Protect concurrent operations by locking {host, array} dupple.
      // Lock will be released when workflow step completes.
      List<String> lockKeys =
          ControllerLockingUtil.getHostStorageLockKeys(
              _dbClient,
              ExportGroupType.Host,
              StringSetUtil.stringSetToUriList(exportMask.getInitiators()),
              arrayURI);
      getWorkflowService()
          .acquireWorkflowStepLocks(
              stepId, lockKeys, LockTimeoutValue.get(LockType.VPLEX_BACKEND_EXPORT));

      // Fetch the Initiators
      List<URI> initiatorURIs = new ArrayList<URI>();
      List<Initiator> initiators = new ArrayList<Initiator>();
      for (String initiatorId : exportMask.getInitiators()) {
        Initiator initiator = _dbClient.queryObject(Initiator.class, URI.create(initiatorId));
        if (initiator != null) {
          initiators.add(initiator);
          initiatorURIs.add(initiator.getId());
        }
      }

      // We do not refresh here, as the VNXExportOperations code will throw an exception
      // if the StorageGroup was not found.
      BlockStorageDevice device = _blockController.getDevice(array.getSystemType());
      if (!exportMask.hasAnyVolumes() && exportMask.getCreatedBySystem()) {
        // We are creating this ExportMask on the hardware! (Maybe not the first time though...)

        // Fetch the targets
        List<URI> targets = new ArrayList<URI>();
        for (String targetId : exportMask.getStoragePorts()) {
          targets.add(URI.create(targetId));
        }

        // Clear the export_mask nativeId; otherwise the VnxExportOps will attempt to look it
        // up and fail. An empty String will suffice as having no nativeId.
        if (exportMask.getNativeId() != null) {
          exportMask.setNativeId("");
          _dbClient.updateAndReindexObject(exportMask);
        }
        // The default completer passed in is for add volume, create correct one
        completer =
            new ExportMaskCreateCompleter(
                exportGroupURI, exportMaskURI, initiatorURIs, volumeMap, stepId);
        device.doExportCreate(array, exportMask, volumeMap, initiators, targets, completer);
      } else {
        device.doExportAddVolumes(array, exportMask, initiators, volumeMap, completer);
      }
    } catch (Exception ex) {
      _log.error("Failed to create or add volumes to export mask for vnx: ", ex);
      VPlexApiException vplexex =
          DeviceControllerExceptions.vplex.addStepsForCreateVolumesFailed(ex);
      WorkflowStepCompleter.stepFailed(stepId, vplexex);
    }
  }
  @Override
  public void deleteOrRemoveVolumesFromExportMask(
      URI arrayURI,
      URI exportGroupURI,
      URI exportMaskURI,
      List<URI> volumes,
      TaskCompleter completer,
      String stepId) {
    try {
      WorkflowStepCompleter.stepExecuting(stepId);
      StorageSystem array = _dbClient.queryObject(StorageSystem.class, arrayURI);
      BlockStorageDevice device = _blockController.getDevice(array.getSystemType());
      ExportMask exportMask = _dbClient.queryObject(ExportMask.class, exportMaskURI);

      // If the exportMask isn't found, or has been deleted, nothing to do.
      if (exportMask == null || exportMask.getInactive()) {
        _log.info(String.format("ExportMask %s inactive, returning success", exportMaskURI));
        WorkflowStepCompleter.stepSucceded(stepId);
        return;
      }

      // Protect concurrent operations by locking {host, array} dupple.
      // Lock will be released when work flow step completes.
      List<String> lockKeys =
          ControllerLockingUtil.getHostStorageLockKeys(
              _dbClient,
              ExportGroupType.Host,
              StringSetUtil.stringSetToUriList(exportMask.getInitiators()),
              arrayURI);
      getWorkflowService()
          .acquireWorkflowStepLocks(
              stepId, lockKeys, LockTimeoutValue.get(LockType.VPLEX_BACKEND_EXPORT));

      // Make sure the completer will complete the work flow. This happens on roll back case.
      if (!completer.getOpId().equals(stepId)) {
        completer.setOpId(stepId);
      }

      // Refresh the ExportMask
      exportMask = refreshExportMask(array, device, exportMask);

      // Determine if we're deleting the last volume.
      Set<String> remainingVolumes = new HashSet<String>();
      if (exportMask.getVolumes() != null) {
        remainingVolumes.addAll(exportMask.getVolumes().keySet());
      }

      for (URI volume : volumes) {
        remainingVolumes.remove(volume.toString());
      }

      // If it is last volume, delete the ExportMask.
      if (remainingVolumes.isEmpty()
          && (exportMask.getExistingVolumes() == null
              || exportMask.getExistingVolumes().isEmpty())) {
        _log.debug(
            String.format(
                "Calling doExportGroupDelete on the device %s", array.getId().toString()));
        device.doExportGroupDelete(array, exportMask, completer);
      } else {
        _log.debug(
            String.format(
                "Calling doExportRemoveVolumes on the device %s", array.getId().toString()));
        device.doExportRemoveVolumes(array, exportMask, volumes, completer);
      }
    } catch (Exception ex) {
      _log.error("Failed to delete or remove volumes to export mask for cinder: ", ex);
      VPlexApiException vplexex =
          DeviceControllerExceptions.vplex.addStepsForDeleteVolumesFailed(ex);
      WorkflowStepCompleter.stepFailed(stepId, vplexex);
    }
  }
  @Override
  public void createOrAddVolumesToExportMask(
      URI arrayURI,
      URI exportGroupURI,
      URI exportMaskURI,
      Map<URI, Integer> volumeMap,
      TaskCompleter completer,
      String stepId) {
    _log.debug("START - createOrAddVolumesToExportMask");
    try {
      WorkflowStepCompleter.stepExecuting(stepId);
      StorageSystem array = _dbClient.queryObject(StorageSystem.class, arrayURI);
      ExportMask exportMask = _dbClient.queryObject(ExportMask.class, exportMaskURI);

      // If the exportMask isn't found, or has been deleted, fail, ask user to retry.
      if (exportMask == null || exportMask.getInactive()) {
        _log.info(String.format("ExportMask %s deleted or inactive, failing", exportMaskURI));
        ServiceError svcerr =
            VPlexApiException.errors.createBackendExportMaskDeleted(
                exportMaskURI.toString(), arrayURI.toString());
        WorkflowStepCompleter.stepFailed(stepId, svcerr);
        return;
      }

      // Protect concurrent operations by locking {host, array} dupple.
      // Lock will be released when work flow step completes.
      List<String> lockKeys =
          ControllerLockingUtil.getHostStorageLockKeys(
              _dbClient,
              ExportGroupType.Host,
              StringSetUtil.stringSetToUriList(exportMask.getInitiators()),
              arrayURI);
      getWorkflowService()
          .acquireWorkflowStepLocks(
              stepId, lockKeys, LockTimeoutValue.get(LockType.VPLEX_BACKEND_EXPORT));

      // Refresh the ExportMask
      BlockStorageDevice device = _blockController.getDevice(array.getSystemType());

      if (!exportMask.hasAnyVolumes()) {
        /*
         * We are creating this ExportMask on the hardware! (Maybe not
         * the first time though...)
         *
         * Fetch the Initiators
         */
        List<Initiator> initiators = new ArrayList<Initiator>();
        for (String initiatorId : exportMask.getInitiators()) {
          Initiator initiator = _dbClient.queryObject(Initiator.class, URI.create(initiatorId));
          if (initiator != null) {
            initiators.add(initiator);
          }
        }

        // Fetch the targets
        List<URI> targets = new ArrayList<URI>();
        for (String targetId : exportMask.getStoragePorts()) {
          targets.add(URI.create(targetId));
        }

        if (volumeMap != null) {
          for (URI volume : volumeMap.keySet()) {
            exportMask.addVolume(volume, volumeMap.get(volume));
          }
        }

        _dbClient.persistObject(exportMask);

        _log.debug(
            String.format(
                "Calling doExportGroupCreate on the device %s", array.getId().toString()));
        device.doExportGroupCreate(array, exportMask, volumeMap, initiators, targets, completer);
      } else {
        _log.debug(
            String.format("Calling doExportAddVolumes on the device %s", array.getId().toString()));
        device.doExportAddVolumes(array, exportMask, volumeMap, completer);
      }

    } catch (Exception ex) {
      _log.error("Failed to create or add volumes to export mask for cinder: ", ex);
      VPlexApiException vplexex =
          DeviceControllerExceptions.vplex.addStepsForCreateVolumesFailed(ex);
      WorkflowStepCompleter.stepFailed(stepId, vplexex);
    }

    _log.debug("END - createOrAddVolumesToExportMask");
  }
  @Override
  public void exportGroupAddVolumes(
      URI storageURI, URI exportGroupURI, Map<URI, Integer> volumeMap, String token)
      throws Exception {
    ExportOrchestrationTask taskCompleter = null;
    try {
      BlockStorageDevice device = getDevice();
      taskCompleter = new ExportOrchestrationTask(exportGroupURI, token);
      StorageSystem storage = _dbClient.queryObject(StorageSystem.class, storageURI);
      ExportGroup exportGroup = _dbClient.queryObject(ExportGroup.class, exportGroupURI);
      boolean anyVolumesAdded = false;
      boolean createdNewMask = false;
      if (exportGroup.getExportMasks() != null) {
        // Set up workflow steps.
        Workflow workflow =
            _workflowService.getNewWorkflow(
                MaskingWorkflowEntryPoints.getInstance(), "exportGroupAddVolumes", true, token);
        List<ExportMask> exportMasksToZoneAddVolumes = new ArrayList<ExportMask>();
        List<URI> volumesToZoneAddVolumes = new ArrayList<URI>();
        List<URI> exportMasksToZoneCreate = new ArrayList<URI>();
        Map<URI, Integer> volumesToZoneCreate = new HashMap<URI, Integer>();
        // Add the volume to all the ExportMasks that are contained in the
        // ExportGroup. The volumes should be added only if they don't
        // already exist for the ExportMask.
        Collection<URI> initiatorURIs =
            Collections2.transform(
                exportGroup.getInitiators(), CommonTransformerFunctions.FCTN_STRING_TO_URI);
        List<URI> hostURIs = new ArrayList<URI>();
        Map<String, URI> portNameToInitiatorURI = new HashMap<String, URI>();
        List<String> portNames = new ArrayList<String>();
        processInitiators(exportGroup, initiatorURIs, portNames, portNameToInitiatorURI, hostURIs);
        // We always want to have the full list of initiators for the hosts involved in
        // this export. This will allow the export operation to always find any
        // existing exports for a given host.
        queryHostInitiatorsAndAddToList(portNames, portNameToInitiatorURI, initiatorURIs, hostURIs);
        Map<String, Set<URI>> foundMatches = device.findExportMasks(storage, portNames, false);
        Set<String> checkMasks = mergeWithExportGroupMaskURIs(exportGroup, foundMatches.values());
        for (String maskURIStr : checkMasks) {
          ExportMask exportMask = _dbClient.queryObject(ExportMask.class, URI.create(maskURIStr));
          _log.info(String.format("Checking mask %s", exportMask.getMaskName()));
          if (!exportMask.getInactive() && exportMask.getStorageDevice().equals(storageURI)) {
            exportMask = device.refreshExportMask(storage, exportMask);
            // BlockStorageDevice level, so that it has up-to-date
            // info from the array
            Map<URI, Integer> volumesToAdd =
                getVolumesToAdd(volumeMap, exportMask, exportGroup, token);
            // Not able to get VolumesToAdd due to error condition so, return
            if (null == volumesToAdd) {
              return;
            }
            _log.info(
                String.format(
                    "Mask %s, adding volumes %s",
                    exportMask.getMaskName(), Joiner.on(',').join(volumesToAdd.entrySet())));
            if (volumesToAdd.size() > 0) {
              exportMasksToZoneAddVolumes.add(exportMask);
              volumesToZoneAddVolumes.addAll(volumesToAdd.keySet());

              // Make sure the zoning map is getting updated for user-created masks
              updateZoningMap(exportGroup, exportMask, true);
              generateExportMaskAddVolumesWorkflow(
                  workflow,
                  EXPORT_GROUP_ZONING_TASK,
                  storage,
                  exportGroup,
                  exportMask,
                  volumesToAdd);
              anyVolumesAdded = true;
              // Need to check if the mask is not already associated with
              // ExportGroup. This is case when we are adding volume to
              // the export and there is an existing export on the array.
              // We have to reuse that existing export, but we need also
              // associated it with the ExportGroup.
              if (!exportGroup.hasMask(exportMask.getId())) {
                exportGroup.addExportMask(exportMask.getId());
                _dbClient.updateAndReindexObject(exportGroup);
              }
            }
          }
        }
        if (!anyVolumesAdded) {
          // This is the case where we were requested to add volumes to the
          // export for this storage array, but none were scheduled to be
          // added. This could be either because there are existing masks,
          // but the volumes are already in the export mask or there are no
          // masks for the storage array. We are checking if there are any
          // masks and if there are initiators for the export.
          if (!ExportMaskUtils.hasExportMaskForStorage(_dbClient, exportGroup, storageURI)
              && exportGroup.hasInitiators()) {
            _log.info(
                "No existing masks to which the requested volumes can be added. Creating a new mask");
            List<URI> initiators = StringSetUtil.stringSetToUriList(exportGroup.getInitiators());

            Map<String, List<URI>> hostInitiatorMap =
                mapInitiatorsToComputeResource(exportGroup, initiators);

            if (!hostInitiatorMap.isEmpty()) {
              for (Map.Entry<String, List<URI>> resourceEntry : hostInitiatorMap.entrySet()) {
                String computeKey = resourceEntry.getKey();
                List<URI> computeInitiatorURIs = resourceEntry.getValue();
                _log.info(String.format("New export masks for %s", computeKey));
                GenExportMaskCreateWorkflowResult result =
                    generateExportMaskCreateWorkflow(
                        workflow,
                        EXPORT_GROUP_ZONING_TASK,
                        storage,
                        exportGroup,
                        computeInitiatorURIs,
                        volumeMap,
                        token);
                exportMasksToZoneCreate.add(result.getMaskURI());
                volumesToZoneCreate.putAll(volumeMap);
              }
              createdNewMask = true;
            }
          }
        }

        if (!exportMasksToZoneAddVolumes.isEmpty()) {
          generateZoningAddVolumesWorkflow(
              workflow, null, exportGroup, exportMasksToZoneAddVolumes, volumesToZoneAddVolumes);
        }

        if (!exportMasksToZoneCreate.isEmpty()) {
          generateZoningCreateWorkflow(
              workflow, null, exportGroup, exportMasksToZoneCreate, volumesToZoneCreate);
        }

        String successMessage =
            String.format(
                "Successfully added volumes to export on StorageArray %s", storage.getLabel());
        workflow.executePlan(taskCompleter, successMessage);
      } else {
        if (exportGroup.hasInitiators()) {
          _log.info("There are no masks for this export. Need to create anew.");
          List<URI> initiatorURIs = new ArrayList<URI>();
          for (String initiatorURIStr : exportGroup.getInitiators()) {
            initiatorURIs.add(URI.create(initiatorURIStr));
          }
          // Invoke the export group create operation,
          // which should in turn create a workflow operations to
          // create the export for the newly added volume(s).
          exportGroupCreate(storageURI, exportGroupURI, initiatorURIs, volumeMap, token);
          anyVolumesAdded = true;
        } else {
          _log.warn("There are no initiator for export group: " + exportGroup.getLabel());
        }
      }
      if (!anyVolumesAdded && !createdNewMask) {
        taskCompleter.ready(_dbClient);
        _log.info(
            "No volumes pushed to array because either they already exist "
                + "or there were no initiators added to the export yet.");
      }
    } catch (Exception ex) {
      _log.error("ExportGroup Orchestration failed.", ex);
      // TODO add service code here
      if (taskCompleter != null) {
        ServiceError serviceError =
            DeviceControllerException.errors.jobFailedMsg(ex.getMessage(), ex);
        taskCompleter.error(_dbClient, serviceError);
      }
    }
  }
  /**
   * Updates the initiator to target list map in the export mask
   *
   * @throws Exception
   */
  private void updateTargetsInExportMask(
      StorageSystem storage,
      Volume volume,
      Map<Volume, Map<String, List<String>>> volumeToInitiatorTargetMapFromAttachResponse,
      List<Initiator> fcInitiatorList,
      ExportMask exportMask)
      throws Exception {
    log.debug("START - updateTargetsInExportMask");
    // ITLS for initiator URIs vs Target port URIs - This will be the final
    // filtered list to send for the zoning map update
    Map<URI, List<URI>> mapFilteredInitiatorURIVsTargetURIList = new HashMap<URI, List<URI>>();

    // From the initiators list, construct the map of initiator WWNs to
    // their URIs
    Map<String, URI> initiatorsWWNVsURI = getWWNvsURIFCInitiatorsMap(fcInitiatorList);

    URI varrayURI = volume.getVirtualArray();

    /*
     * Get the list of storage ports from the storage system which are
     * associated with the varray This will be map of storage port WWNs with
     * their URIs
     */
    Map<String, URI> mapVarrayTaggedPortWWNVsURI =
        getVarrayTaggedStoragePortWWNs(storage, varrayURI);

    // List of WWN entries, used below for filtering the target port list
    // from attach response
    Set<String> varrayTaggedPortWWNs = mapVarrayTaggedPortWWNVsURI.keySet();

    URI vpoolURI = volume.getVirtualPool();
    VirtualPool vpool = dbClient.queryObject(VirtualPool.class, vpoolURI);
    int pathsPerInitiator = vpool.getPathsPerInitiator();

    // Process the attach response output
    Set<Volume> volumeKeysSet = volumeToInitiatorTargetMapFromAttachResponse.keySet();
    for (Volume volumeRes : volumeKeysSet) {
      log.info(
          String.format(
              "Processing attach response for the volume with URI %s and name %s",
              volumeRes.getId(), volumeRes.getLabel()));
      Map<String, List<String>> initiatorTargetMap =
          volumeToInitiatorTargetMapFromAttachResponse.get(volumeRes);
      Set<String> initiatorKeysSet = initiatorTargetMap.keySet();
      for (String initiatorKey : initiatorKeysSet) {
        // The list of filtered target ports ( which are varray tagged )
        // from the attach response
        List<String> filteredTargetList =
            filterTargetsFromResponse(varrayTaggedPortWWNs, initiatorTargetMap, initiatorKey);
        log.info(
            String.format(
                "For initiator %s accessible storage ports are %s ",
                initiatorKey, filteredTargetList.toString()));

        List<String> tmpTargetList = null;
        if (!isVplex(volumeRes)) {
          // For VPLEX - no path validations
          // Path validations are required only for the Host Exports
          tmpTargetList = checkPathsPerInitiator(pathsPerInitiator, filteredTargetList);

          if (null == tmpTargetList) {
            // Rollback case - throw the exception
            throw new Exception(
                String.format(
                    "Paths per initiator criteria is not met for the initiator : %s "
                        + " Target counts is: %s Expected paths per initiator is: %s",
                    initiatorKey,
                    String.valueOf(filteredTargetList.size()),
                    String.valueOf(pathsPerInitiator)));
          }

        } else {
          tmpTargetList = filteredTargetList;
        }

        // Now populate URIs for the map to be returned - convert WWNs
        // to URIs
        populateInitiatorTargetURIMap(
            mapFilteredInitiatorURIVsTargetURIList,
            initiatorsWWNVsURI,
            mapVarrayTaggedPortWWNVsURI,
            initiatorKey,
            tmpTargetList);
      } // End initiator iteration
    } // End volume iteration

    // Clean all existing targets in the export mask and add new targets
    List<URI> storagePortListFromMask =
        StringSetUtil.stringSetToUriList(exportMask.getStoragePorts());
    for (URI removeUri : storagePortListFromMask) {
      exportMask.removeTarget(removeUri);
    }
    exportMask.setStoragePorts(null);

    // Now add new target ports and populate the zoning map
    Set<URI> initiatorURIKeys = mapFilteredInitiatorURIVsTargetURIList.keySet();
    for (URI initiatorURI : initiatorURIKeys) {
      List<URI> storagePortURIList = mapFilteredInitiatorURIVsTargetURIList.get(initiatorURI);
      for (URI portURI : storagePortURIList) {
        exportMask.addTarget(portURI);
      }
    }

    log.debug("END - updateTargetsInExportMask");
  }