/**
   * Returns a Map of networkURI => [set of endpoints connected].
   *
   * @param dbClient
   * @param initiators
   * @return
   */
  public static Map<URI, Set<String>> getNetworkToInitiators(
      DbClient dbClient, List<Initiator> initiators) {
    Map<URI, Set<String>> networkToEndPoints = new HashMap<URI, Set<String>>();
    for (Initiator initiator : initiators) {
      Set<NetworkLite> networkLites =
          getEndpointAllNetworksLite(initiator.getInitiatorPort(), dbClient);
      if (null == networkLites || networkLites.isEmpty()) {
        _log.info(
            String.format(
                "getNetworkToInitiators(%s) -- Initiator is not associated with any network",
                initiator.getInitiatorPort()));
      } else {
        for (NetworkLite networkLite : networkLites) {
          URI networkUri = networkLite.getId();
          _log.info(
              String.format(
                  "Adding initiator, network (%s, %s) to map",
                  initiator.getInitiatorPort(), networkLite.getLabel()));
          Set<String> endPoints = networkToEndPoints.get(networkUri);
          if (null == endPoints) {
            endPoints = new HashSet<String>();
          }
          endPoints.add(initiator.getInitiatorPort());
          networkToEndPoints.put(networkUri, endPoints);
        }
      }
    }

    return networkToEndPoints;
  }
  /**
   * Get the network that has the endpoint
   *
   * @param endpoint the formatted and validated endpoint
   * @param dbClient an instance if DbClient
   * @param excludedNetworks do not get network with nativeId in the list
   * @return a reference to the network that contains the endpoint. Null if the network is not
   *     found.
   */
  public static NetworkLite getEndpointNetworkLite(
      String endpoint, DbClient dbClient, Set<String> excludedNetworks) {
    _log.debug("Finding networklite for endpoint {}", endpoint);
    URIQueryResultList networkList = new URIQueryResultList();
    Iterator<URI> iterator;
    URI networkUri = null;
    NetworkLite network;
    dbClient.queryByConstraint(
        AlternateIdConstraint.Factory.getEndpointNetworkConstraint(endpoint), networkList);
    iterator = networkList.iterator();
    while (iterator.hasNext()) {
      networkUri = iterator.next();
      network = getNetworkLite(networkUri, dbClient);

      // vsan id is in the excluded list, skip it
      if (excludedNetworks != null && excludedNetworks.contains(network.getNativeId())) {
        continue;
      }

      if (network != null && network.getInactive() == false) {
        _log.info(
            String.format(
                "endpoint %s in network %s (%s)", endpoint, network.getLabel(), network.getId()));
        return network;
      } else {
        _log.info(
            "networklite {} for endpoint {} was deleted or is inactive", networkUri, endpoint);
      }
    }
    _log.info("networklite could not be found for endpoint {}", endpoint);
    return null;
  }
 /**
  * Returns an instance of NetworkLite for every network that is routed to the request network.
  *
  * @param networkLite the networklite for which the routed networks are requested
  * @param dbClient an instance of dbClient
  * @return an instance of NetworkLite for every network that is routed to the request network
  */
 public static Set<NetworkLite> getNetworkLiteRoutedNetworks(
     NetworkLite networkLite, DbClient dbClient) {
   if (networkLite != null
       && networkLite.getRoutedNetworks() != null
       && !networkLite.getRoutedNetworks().isEmpty()) {
     return getNetworkLites(networkLite.getRoutedNetworks(), dbClient);
   }
   return new HashSet<NetworkLite>();
 }
 /**
  * Returns the ports in the given network in a port-wwn-to-port map.
  *
  * @param networkLite the network
  * @param ports the ports
  * @return a map of port-wwn-to-port
  */
 public static Map<String, StoragePort> getPortsInNetworkMap(
     NetworkLite networkLite, Collection<StoragePort> ports) {
   Map<String, StoragePort> map = new HashMap<String, StoragePort>();
   if (networkLite != null) {
     for (StoragePort port : ports) {
       if (port.getNetwork() != null && port.getNetwork().equals(networkLite.getId())) {
         map.put(port.getPortNetworkId(), port);
       }
     }
     if (map.isEmpty() && networkLite.getRoutedNetworks() != null) {
       for (StoragePort port : ports) {
         if (port.getNetwork() != null
             && networkLite.getRoutedNetworks().contains(port.getNetwork().toString())) {
           map.put(port.getPortNetworkId(), port);
         }
       }
     }
   }
   return map;
 }
 /**
  * Given the URI for a Network, obtain it's NetworkLite structure. This is done without
  * instantiating the endpoint data in the Network by calling DbClient.queryObjectFields, which
  * retrieves only certain fields from the database.
  *
  * @param networkURI
  * @param client
  * @return NetworkLite
  */
 public static NetworkLite getNetworkLite(URI networkURI, DbClient client) {
   List<URI> ids = new ArrayList<URI>();
   ids.add(networkURI);
   Set<String> fieldNames = new HashSet<String>();
   fieldNames.addAll(NetworkLite.getColumnNames());
   Collection<Network> networks = client.queryObjectFields(Network.class, fieldNames, ids);
   Iterator<Network> networkIter = networks.iterator();
   if (networkIter.hasNext()) {
     Network network = networkIter.next();
     return new NetworkLite(network);
   }
   throw DatabaseException.fatals.unableToFindEntity(networkURI);
 }
 /**
  * Given the initiator and port networks, check that they are connected that is either in the same
  * network or in different networks but routable.
  *
  * @param iniNetwork the initiator network
  * @param portNet the port network
  * @return true if the port and initiator network are connected.
  */
 public static boolean checkInitiatorAndPortConnected(
     NetworkLite iniNetwork, NetworkLite portNet) {
   if (iniNetwork.getId().equals(portNet.getId())) {
     _log.info(
         "Both the port and initiator are in the same network {}", iniNetwork.getNativeGuid());
     return true;
   } else if (iniNetwork.hasRoutedNetworks(portNet.getId())) {
     _log.info(
         "The port and initiators are in different but routed networks: {} and {}",
         new Object[] {iniNetwork.getNativeGuid(), portNet.getNativeGuid()});
     return true;
   }
   _log.info("The port and initiator are not connected.");
   return false;
 }
 /**
  * Parses the WWN from the network's Native GUID
  *
  * @param network the network
  * @return the network WWN
  */
 public static String getNetworkWwn(NetworkLite network) {
   return parseNetworkWwn(network == null ? "" : network.getNativeGuid());
 }