private List<CifsShareACL> queryDBShareACLs() {

    try {
      ContainmentConstraint containmentConstraint = null;

      if (this.fs != null) {
        _log.info(
            "Querying DB for Share ACLs of share {} of filesystemId {} ",
            this.shareName,
            fs.getId());
        containmentConstraint =
            ContainmentConstraint.Factory.getFileCifsShareAclsConstraint(this.fs.getId());
      } else {
        // Snapshot
        _log.info(
            "Querying DB for Share ACLs of share {} of snapshotId {} ",
            this.shareName,
            this.snapshot.getId());
        containmentConstraint =
            ContainmentConstraint.Factory.getSnapshotCifsShareAclsConstraint(this.snapshot.getId());
      }

      List<CifsShareACL> shareAclList =
          CustomQueryUtility.queryActiveResourcesByConstraint(
              this.dbClient, CifsShareACL.class, containmentConstraint);

      return shareAclList;

    } catch (Exception e) {
      _log.error("Error while querying DB for ACL of a share {}", e);
    }

    return null;
  }
 /**
  * If any endpoint is used in an active File Export, throws an exception
  *
  * @param endpoints endpoints being added
  * @param varrays endpoints belong to
  *     <p>Assumes endpoint formats have been validated.
  */
 public static void checkNotUsedByActiveFileExport(String endpoint, DbClient dbClient) {
   Network network = NetworkUtil.getEndpointNetwork(endpoint, dbClient);
   if (network != null) {
     Set<String> netVArrayIds = network.getConnectedVirtualArrays();
     if ((netVArrayIds != null) && (!netVArrayIds.isEmpty())) {
       Iterator<String> netVArrayIdsIter = netVArrayIds.iterator();
       while (netVArrayIdsIter.hasNext()) {
         String varrayId = netVArrayIdsIter.next();
         List<FileShare> fileShares =
             CustomQueryUtility.queryActiveResourcesByConstraint(
                 dbClient,
                 FileShare.class,
                 AlternateIdConstraint.Factory.getConstraint(FileShare.class, "varray", varrayId));
         for (FileShare fileShare : fileShares) {
           FSExportMap fsExports = fileShare.getFsExports();
           if (fsExports != null) {
             Iterator<FileExport> it = fsExports.values().iterator();
             while (it.hasNext()) {
               FileExport fileExport = it.next();
               if (fileExport.getClients().contains(endpoint)
                   || fileExport.getStoragePort().contains(endpoint)) {
                 throw APIException.badRequests.endpointsCannotBeUpdatedActiveExport(endpoint);
               }
             }
           }
         }
       }
     }
   }
 }
  /**
   * Cleans up instances of {@link BlockSnapshot} that failed to be created. Also, any stale entries
   * in {@link BlockSnapshotSession#getLinkedTargets()} will be cleaned up.
   *
   * @param volume Volume URI to process.
   * @param itemsToUpdate Items to be updated.
   * @param itemsToDelete Items to be deleted.
   */
  @Override
  public void process(
      URI volume, Collection<DataObject> itemsToUpdate, Collection<DataObject> itemsToDelete) {
    List<BlockSnapshot> snapshots =
        CustomQueryUtility.queryActiveResourcesByConstraint(
            getDbClient(),
            BlockSnapshot.class,
            ContainmentConstraint.Factory.getVolumeSnapshotConstraint(volume));
    List<BlockSnapshot> failedSnapshots = new ArrayList<>();
    List<BlockSnapshotSession> updateSessions = new ArrayList<>();

    failedSnapshots.addAll(
        Collections2.filter(
            snapshots,
            new Predicate<BlockSnapshot>() {
              @Override
              public boolean apply(BlockSnapshot snapshot) {
                return Strings.isNullOrEmpty(snapshot.getNativeId());
              }
            }));

    // Removed failed snapshots from any existing sessions
    for (BlockSnapshot failedSnapshot : failedSnapshots) {
      log.info("Removing failed snapshot: {}", failedSnapshot.getLabel());
      List<BlockSnapshotSession> sessions =
          CustomQueryUtility.queryActiveResourcesByConstraint(
              getDbClient(),
              BlockSnapshotSession.class,
              ContainmentConstraint.Factory.getLinkedTargetSnapshotSessionConstraint(
                  failedSnapshot.getId()));

      for (BlockSnapshotSession session : sessions) {
        log.info("Updating existing session: {}", session.getSessionLabel());
        StringSet linkedTargets = session.getLinkedTargets();
        linkedTargets.remove(failedSnapshot.getId().toString());
        updateSessions.add(session);
      }
    }

    itemsToUpdate.addAll(updateSessions);
    itemsToDelete.addAll(failedSnapshots);
  }
  /**
   * Checks if an port in use by an export masks
   *
   * @param portId the port URI being checked
   * @param dbClient
   * @return true if the port in use by export masks
   */
  public static boolean isBlockStoragePortInUse(URI portId, DbClient dbClient) {

    if (portId != null) {
      List<ExportMask> exportMasks =
          CustomQueryUtility.queryActiveResourcesByConstraint(
              dbClient,
              ExportMask.class,
              AlternateIdConstraint.Factory.getConstraint(
                  ExportMask.class, "storagePorts", portId.toString()));
      return (exportMasks != null && !exportMasks.isEmpty());
    }
    return false;
  }
  /**
   * Checks if an initiator in use by an export groups
   *
   * @param initId the initiator URI being checked
   * @param dbClient
   * @return true if the initiator in use by export groups
   */
  public static boolean isInitiatorInUse(URI initId, DbClient dbClient) {

    if (initId != null) {

      List<ExportGroup> exportGroups =
          CustomQueryUtility.queryActiveResourcesByConstraint(
              dbClient,
              ExportGroup.class,
              AlternateIdConstraint.Factory.getConstraint(
                  ExportGroup.class, "initiators", initId.toString()));
      return (exportGroups != null && !exportGroups.isEmpty());
    }
    return false;
  }
  private List<QuotaDirectory> queryDBQuotaDirectories(FileShare fs) {
    _log.info("Querying all quota directories Using FsId {}", fs.getId());
    try {
      ContainmentConstraint containmentConstraint =
          ContainmentConstraint.Factory.getQuotaDirectoryConstraint(fs.getId());
      List<QuotaDirectory> fsQuotaDirs =
          CustomQueryUtility.queryActiveResourcesByConstraint(
              _dbClient, QuotaDirectory.class, containmentConstraint);
      return fsQuotaDirs;
    } catch (Exception e) {
      _log.error("Error while querying {}", e);
    }

    return null;
  }
  /**
   * get the volume to be updated after application add and remove operations could be the volume
   * passed in if it's a simple block volume or the vplex virtual volume if it's a backing volume
   *
   * @param voluri uri of volume operated on during add or remove volume from application operation
   * @param dbClient
   * @return the volume to update
   */
  private Volume getVolume(URI voluri, DbClient dbClient) {
    // if this is a vplex volume, update the parent virtual volume
    List<Volume> vplexVolumes =
        CustomQueryUtility.queryActiveResourcesByConstraint(
            dbClient, Volume.class, getVolumesByAssociatedId(voluri.toString()));

    Volume volume = null;

    for (Volume vplexVolume : vplexVolumes) {
      URI storageURI = vplexVolume.getStorageController();
      StorageSystem storage = dbClient.queryObject(StorageSystem.class, storageURI);
      if (DiscoveredDataObject.Type.vplex.name().equals(storage.getSystemType())) {
        volume = vplexVolume;
      }
    }

    if (volume == null) {
      volume = dbClient.queryObject(Volume.class, voluri);
    }
    return volume;
  }