/**
  * Prepare the ViPR BlockSnapshotSession instance for the pass BlockSnapshot instance.
  *
  * @param snapshot A reference to the snapshot.
  * @return A reference to the newly created snapshot session.
  */
 private BlockSnapshotSession prepareSnapshotSession(BlockSnapshot snapshot) {
   s_logger.info("Prepare BlockSnapshotSession for snapshot {}", snapshot.getId());
   BlockSnapshotSession snapshotSession = new BlockSnapshotSession();
   URI snapSessionURI = URIUtil.createId(BlockSnapshotSession.class);
   snapshotSession.setId(snapSessionURI);
   snapshotSession.setSessionLabel(getSessionLabelFromSettingsInstance(snapshot));
   URI cgURI = snapshot.getConsistencyGroup();
   if (NullColumnValueGetter.isNullURI(cgURI)) {
     snapshotSession.setParent(snapshot.getParent());
     snapshotSession.setLabel(snapshot.getLabel());
   } else {
     snapshotSession.setConsistencyGroup(cgURI);
     snapshotSession.setLabel(snapshot.getSnapsetLabel());
     Volume parent = getDbClient().queryObject(Volume.class, snapshot.getParent());
     if (parent != null) {
       snapshotSession.setReplicationGroupInstance(parent.getReplicationGroupInstance());
       snapshotSession.setSessionSetName(parent.getReplicationGroupInstance());
     }
   }
   snapshotSession.setProject(snapshot.getProject());
   snapshotSession.setStorageController(snapshot.getStorageController());
   snapshotSession.setSessionInstance(snapshot.getSettingsInstance());
   StringSet linkedTargets = new StringSet();
   linkedTargets.add(snapshot.getId().toString());
   snapshotSession.setLinkedTargets(linkedTargets);
   return snapshotSession;
 }
 /**
  * Gets the session label from settings instance.
  *
  * @param snapshot the snapshot
  * @return the session label from settings instance
  */
 private String getSessionLabelFromSettingsInstance(BlockSnapshot snapshot) {
   String sessionLabel = null;
   String settingsInstance = snapshot.getSettingsInstance();
   if (settingsInstance != null && !settingsInstance.isEmpty()) {
     String[] instanceArray = settingsInstance.split(SMIS80_DELIMITER_REGEX);
     sessionLabel = instanceArray[3];
   }
   return sessionLabel;
 }
  /** {@inheritDoc} */
  @Override
  public void process() throws MigrationCallbackException {
    s_logger.info("Executing BlockSnapshotSession migration callback.");
    try {
      DbClient dbClient = getDbClient();
      List<BlockSnapshotSession> snapshotSessions = new ArrayList<BlockSnapshotSession>();
      Map<URI, Map<String, BlockSnapshotSession>> groupSessionMap = new HashMap<>();
      List<URI> snapshotURIs = dbClient.queryByType(BlockSnapshot.class, true);
      Iterator<BlockSnapshot> snapshotsIter =
          dbClient.queryIterativeObjects(BlockSnapshot.class, snapshotURIs, true);
      while (snapshotsIter.hasNext()) {
        BlockSnapshot snapshot = snapshotsIter.next();
        if (isSnapshotSessionSupported(snapshot)) {
          // Check if this is a group snapshot.
          URI cgURI = snapshot.getConsistencyGroup();
          if (NullColumnValueGetter.isNullURI(cgURI)) {
            // The storage system for the single volume snapshot supports
            // snapshot sessions, then we need to prepare and create a
            // snapshot session for that snapshot and add the snapshot as
            // a linked target for the session.
            BlockSnapshotSession snapshotSession = prepareSnapshotSession(snapshot);
            snapshotSessions.add(snapshotSession);
          } else {
            // Create the group session if necessary and add the snapshot as a
            // linked target for that group session.
            String settingsInstance = snapshot.getSettingsInstance();
            Map<String, BlockSnapshotSession> grpSnapshotSessions = groupSessionMap.get(cgURI);
            if (grpSnapshotSessions != null) {
              BlockSnapshotSession snapshotSession = grpSnapshotSessions.get(settingsInstance);
              if (snapshotSession == null) {
                snapshotSession = prepareSnapshotSession(snapshot);
                grpSnapshotSessions.put(settingsInstance, snapshotSession);
                snapshotSessions.add(snapshotSession);
              } else {
                StringSet linkedTargets = snapshotSession.getLinkedTargets();
                linkedTargets.add(snapshot.getId().toString());
              }
            } else {
              grpSnapshotSessions = new HashMap<String, BlockSnapshotSession>();
              groupSessionMap.put(cgURI, grpSnapshotSessions);
              BlockSnapshotSession snapshotSession = prepareSnapshotSession(snapshot);
              grpSnapshotSessions.put(settingsInstance, snapshotSession);
              snapshotSessions.add(snapshotSession);
            }
          }
        }
      }

      if (!snapshotSessions.isEmpty()) {
        dbClient.createObject(snapshotSessions);
      }
    } catch (Exception e) {
      s_logger.error("Caught exception during BlockSnapshotSession migration", e);
    }
  }