/**
  * Get all the networks belongs to the given network system.
  *
  * @param networkSystem
  * @param allNetworks
  * @return
  */
 private List<Network> getLocalNetworks(NetworkSystem networkSystem, List<Network> allNetworks) {
   List<Network> realNetworks = new ArrayList<Network>();
   for (Network network : allNetworks) {
     if (network.getNetworkSystems() != null
         && network.getNetworkSystems().contains(networkSystem.getId().toString())) {
       realNetworks.add(network);
     }
   }
   return realNetworks;
 }
 /**
  * Get the connected network systems which are connected by the given transit network.
  *
  * @param transitNetwork
  * @param allNetworks
  * @return
  */
 private Set<String> getConnectedNetworkSystems(String transitNetwork, List<Network> allNetworks) {
   Set<String> connectedNetworkSystems = new HashSet<String>();
   for (Network network : allNetworks) {
     if (network.getNetworkSystems() != null && transitNetwork.equals(network.getNativeId())) {
       for (String networkSystem : network.getNetworkSystems()) {
         connectedNetworkSystems.add(networkSystem);
       }
     }
   }
   return connectedNetworkSystems;
 }
 /**
  * Check if the given network is a remote routed network of the current network system. If it
  * belongs to a connected network system which is not the current network system, it is a remote
  * routed network.
  *
  * @param network The given network.
  * @param currentNetworkSystemId The current network system ID.
  * @param connectedNetworkSystems The connected network systems list.
  * @return true/false
  */
 private boolean isRemoteRoutedNetwork(
     Network network, String currentNetworkSystemId, Set<String> connectedNetworkSystems) {
   if (network.getNetworkSystems() != null) {
     for (String networkSystem : network.getNetworkSystems()) {
       if (networkSystem != currentNetworkSystemId
           && connectedNetworkSystems.contains(networkSystem)) {
         return true;
       }
     }
   }
   return false;
 }
 /**
  * Remove the transport zone for a given network system. This typically means to dis-associated it
  * unless this is the last network system associated with the transport zone. In this case, the
  * transport zone will be deleted if:
  *
  * <ul>
  *   <li>It was discovered
  *   <li>It does not have any user-created ports
  *   <li>It does not have any registered ports
  * </ul>
  *
  * @param tzone
  * @param uri
  * @throws IOException
  */
 public List<String> removeNetworkSystemTransportZone(Network tzone, String uri)
     throws IOException {
   tzone.removeNetworkSystems(Collections.singletonList(uri)); // dis-associate
   // list of end points getting deleted
   ArrayList<String> toRemove = new ArrayList<String>();
   if (tzone.getNetworkSystems().isEmpty()) { // if this is the last network system
     List<String> userCreatedEndPoints = TransportZoneReconciler.getUserCreatedEndPoints(tzone);
     if (userCreatedEndPoints.isEmpty()
         && !tzone.assignedToVarray()) { // delete only if not modified by a user
       _log.info("Removing network {}", tzone.getLabel());
       toRemove.addAll(tzone.retrieveEndpoints());
       NetworkAssociationHelper.handleEndpointsRemoved(tzone, toRemove, dbClient, _coordinator);
       dbClient.markForDeletion(tzone);
       recordTransportZoneEvent(
           tzone,
           OperationTypeEnum.DELETE_NETWORK.getEvType(true),
           OperationTypeEnum.DELETE_NETWORK.getDescription());
     } else {
       _log.info(
           "Network {} is changed by the user and will "
               + "not be removed. Discovered end points will be removed.",
           tzone.getLabel());
       for (String pt : tzone.retrieveEndpoints()) {
         if (!userCreatedEndPoints.contains(pt)) {
           toRemove.add(pt);
         }
       }
       tzone.removeEndpoints(toRemove);
       NetworkAssociationHelper.handleEndpointsRemoved(tzone, toRemove, dbClient, _coordinator);
       _log.info("Discovered endpoints removed {}", toRemove.toArray());
       dbClient.persistObject(tzone);
       recordTransportZoneEvent(
           tzone,
           OperationTypeEnum.UPDATE_NETWORK.getEvType(true),
           OperationTypeEnum.UPDATE_NETWORK.getDescription());
     }
   } else {
     _log.info("Removing network {} from network system {}", tzone.getLabel(), uri);
     dbClient.persistObject(tzone);
     recordTransportZoneEvent(
         tzone,
         OperationTypeEnum.UPDATE_NETWORK.getEvType(true),
         OperationTypeEnum.UPDATE_NETWORK.getDescription());
   }
   return toRemove;
 }
 /**
  * Get the transit network set if there is, otherwise return empty set.
  *
  * @param networkSystem The current discovering network system.
  * @param allNetworks All the existing networks list.
  * @return The transit networks list.
  * @throws Exception The "getFabricIdsMap" may throw exception.
  */
 private Set<String> getTransitNetworks(NetworkSystem networkSystem, List<Network> allNetworks)
     throws Exception {
   Map<String, String> networkWwnIdMap = getDevice().getFabricIdsMap(networkSystem);
   _log.info("getTransitNetworks.networkWwnIdMap = {}", networkWwnIdMap);
   Set<String> transitNetworks = new HashSet<String>();
   for (Entry<String, String> entry : networkWwnIdMap.entrySet()) {
     String currentNetworkId = entry.getValue();
     String currentNetworkWwn = entry.getKey();
     Network currentNetwork = getNetworkByNativeId(allNetworks, currentNetworkId);
     // How to determine it's a transit network: 1. More than one network system have the same
     // network.
     if (currentNetwork != null && currentNetwork.getNetworkSystems().size() > 1) {
       _log.info("Network id={} is a transit VSAN", currentNetworkId);
       transitNetworks.add(currentNetworkId);
     } else {
       _log.info("Network id={} is NOT a transit VSAN", currentNetworkId);
     }
   }
   return transitNetworks;
 }