/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */
package monitoring.web.action;

import ilog.cpl.IlpNetwork;
import ilog.cpl.datasource.IlpDataSource;
import ilog.cpl.datasource.IlpDefaultDataSource;
import ilog.cpl.graph.IlpGraphSelectionModel;
import ilog.cpl.model.IlpObject;
import ilog.cpl.model.IlpRepresentationObject;
import ilog.cpl.network.IlpNetworkAdapter;
import ilog.tgo.faces.network.dhtml.component.IltFacesDHTMLNetworkView;
import ilog.tgo.model.IltNetworkElement;
import ilog.views.IlvRect;
import ilog.views.faces.dhtml.event.FacesViewActionEvent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;

import javax.faces.component.UIComponent;

import monitoring.shared.CommonUtils;
import monitoring.shared.LoggingUtils;
import monitoring.shared.ServicesUtils;
import monitoring.shared.drilldown.EquipmentDrillDownManager;
import monitoring.shared.drilldown.NetworkDrillDownManager;
import monitoring.web.SampleDataModels;
import monitoring.web.utils.Lock;
import shared.ResourceUtils;

/**
 * Action provider that holds the network related actions.
 */
public class NetworkActionProvider extends BaseActionProvider {

  /**
   * Handles the logistics behind having the service details view ready to be displayed.
   */
  public void prepareServiceDetailViewToBeShown(FacesViewActionEvent event) throws Exception {

    //Get the active object
    IlpRepresentationObject representationObject = 
      (IlpRepresentationObject) event.getObject();

    IlpObject businessObject = representationObject.getIlpObject();

    if (businessObject != null) {

      ServicesUtils servicesUtil = sampleContext.getServicesUtils();

      String serviceName = servicesUtil.getServiceName(businessObject);

      if (serviceName != null) {
        IlpDefaultDataSource serviceDataSource = getDataStructures().getDataSources().getServiceDataSource();

        //1- Clear service detail datasource and load the current service in it
        serviceDataSource.clear();

        //2- Load the new info in the service datasource
        sampleContext.getIntegrationProvider().loadServiceInformation(serviceName, serviceDataSource);

        //The above call is assync, but we need to have the services details datasource loaded in order
        //to process the next step, so we have to wait for a bit here...
        waitForDataSourceToBeLoaded(serviceDataSource);

        //3- Handle the MonitoringNetworkElementShortcut objects special case
        processShortcutSpecialCase(serviceDataSource);

        //4- Update the title of the service window
        getControls().getNetworkControls().setServiceDetailNetworkWindowTitle(
            ResourceUtils.getString("titlePrefixServiceDetailWindow")+ serviceName);
        
        //5- Update the displayed area on the service detail view 
        updateServiceDetailDisplayArea(serviceName);
      }
    }
  }

  /**
   * Method triggered by the network context menu to show the details
   * of a given object.
   */
  public void showDetails(FacesViewActionEvent event) throws Exception {

    //Get the active object
    IlpRepresentationObject representationObject = (IlpRepresentationObject) event.getObject();
    
    IlpObject businessObject = representationObject.getIlpObject();

    showDetails(businessObject);
  }

  /**
   * Method triggered by the network context menu when triggered to show the 
   * enclosing network of a given object.
   */
  public void showEnclosingNetwork(FacesViewActionEvent event) throws Exception {

    Lock waitLock = sampleContext.COMPLETION_LOCK;

    synchronized (waitLock) {
      try {
        synchronized(getServiceNetwork().getManagerView().getRegion()) {
          //Trigger waiting scheme 
          waitLock.executingTask = true;
  
          //////////////////////////////////////////////////////////////////////////
          //Get the active object
          IlpNetworkAdapter adapter = 
            sampleContext.getControls().getNetworkControls().
            getNetworkNetworkView().getNetwork().getAdapter();
  
          IlpObject object = null;
  
          if (adapter != null) {
            List<Object> origins = adapter.getOrigins();
  
            if (!origins.isEmpty()) {
              IlpDataSource dataSource = 
                sampleContext.getDataStructures().getDataSources().getNetworkDataSource();
  
              object = dataSource.getObject(origins.get(0));
            }
          }
          if (object != null) {
            sampleContext.getActionProviders().getSharedActions().setSelectedObject(object);
          }
          //////////////////////////////////////////////////////////////////////////
        }       
      } catch (Exception e) {
        LoggingUtils.getSampleLogger().log(
            Level.SEVERE,
            "Could not properly show the enclosing network with this exception: "
            + e.getLocalizedMessage());
      } finally {        
        //Shutdown waiting scheme 
        waitLock.executingTask = false;        
      }
    }
  }

  /**
   * This method is invoked by the ServerClientActionListener, as configured 
   * in AbstractContextualMenuFactory. 
   * <p>
   * The implementation shows the details of the target object either in the 
   * network or equipment view.
   */
  public void showDetails(IlpObject businessObject) throws Exception {

    Lock waitLock = sampleContext.COMPLETION_LOCK;

    synchronized (waitLock) {
      try {
        synchronized(getServiceNetwork().getManagerView().getRegion()) {
          //Trigger waiting scheme 
          waitLock.executingTask = true;
  
          //////////////////////////////////////////////////////////////////////////
          /////////////////////////////////////////////////////////////////
          //Network Module drilldown
          /////////////////////////////////////////////////////////////////
  
          //1- Get the drill down manager
          NetworkDrillDownManager drillDownManager = 
            getControls().getNetworkControls().getNetworkDrillDownManager();
          
          //2- Show subnetwork if applicable
          if (drillDownManager.canDrillDownGroup(businessObject)) {
            showNetwork(businessObject);
                waitLock.executingTask = false;
            return;
          }
  
          /////////////////////////////////////////////////////////////////
          //Inventory Module drilldown
          /////////////////////////////////////////////////////////////////
  
          //Assert object's an NE and can be drilled down
          if (!drillDownManager.canDrillDownNE(businessObject)) {
            waitLock.executingTask = false;
            return;
          }
  
          //1- Switch to the inventory module
          getActionProviders().getSharedActions().showInventoryModule();
  
          //2- Show equipment in view
          showEquipment((IltNetworkElement) businessObject);
          //////////////////////////////////////////////////////////////////////////
        }  
      } catch (Exception e) {
        LoggingUtils.getSampleLogger().log(
            Level.SEVERE,
            "Could not properly show the details of object with this exception: "
            + e.getLocalizedMessage());
      } finally {        
        //Shutdown waiting scheme 
        waitLock.executingTask = false;        
      }
    }
  }

  /**
   * Returns whether or not it is necessary to switch to the inventory module
   * if a "show details" action is to be invoked on the provide object.
   */
  public boolean needToSwitchToInventoryDuringShowDetails(IlpObject businessObject) {
    
    //Get the drill down manager
    NetworkDrillDownManager drillDownManager = 
      getControls().getNetworkControls().getNetworkDrillDownManager();

    //If we can drilldown on a group then we do not need to switch
    if (drillDownManager.canDrillDownGroup(businessObject)) {
      return false; 
    } 
    //If we can drill down on an NE then we need to switch
    else if (drillDownManager.canDrillDownNE(businessObject)) {
       return true;
    } else {
      //By default do not switch 
      return false;
    }
  }

  /**
   * This method is invoked by the ServerClientActionListener, as configured 
   * in AbstractContextualMenuFactory. 
   * <p>
   * The implementation shows the services of the target object in the
   * service details view.
   */
  public void showServices(IlpObject businessObject) throws Exception {
    NetworkDrillDownManager drillDownManager = 
      getControls().getNetworkControls().getNetworkDrillDownManager();

    // make sure we can show service
    if (!drillDownManager.canShowService(businessObject)) {
      return;
    }

    // get services names (won't be a null as we checked already)
    String[] servicesStrArr = 
      drillDownManager.getServiceNames(businessObject);

    // save matched services
    ArrayList<IlpObject> servicesList = new ArrayList<IlpObject>();
    IlpDefaultDataSource ds = 
      getDataStructures().getDataSources().getServicesDataSource();
    
    for (int i = 0; i < servicesStrArr.length; i++) {
      IlpObject serviceObj = ds.getObject(servicesStrArr[i]);
      if (serviceObj != null) {
        servicesList.add(serviceObj);
      }
    }

    NetworkActionProvider networkAction = 
      getActionProviders().getNetworkActions();
    
    TreeActionProvider treeActions = getActionProviders().getTreeActions();

    //1- Switch to the service module
    getActionProviders().getSharedActions().showServiceModule();

    //2- Clear selection, expansion state and highlighting
    treeActions.clearExpansionOnCurrentTree();
    treeActions.clearSelectionOnCurrentTree();
    getSampleContext().getServicesUtils().clearHighlightsInNetwork(
        getControls().getNetworkControls().getServiceNetworkView().getNetwork());

    //3- Select and highlight object is service view
    for (IlpObject object : servicesList) {
      networkAction.selectRelatedObjectsOnCurrentView(object, true, false);
      treeActions.selectObjectOnCurrentTree(object, false, false);
    }
  }

  /**
   * Shows the roots in the network module's network component.
   */
  public void showHomeOnNetworkNetwork() {

    Lock waitLock = sampleContext.COMPLETION_LOCK;

    synchronized (waitLock) {
      try {
        synchronized(getServiceNetwork().getManagerView().getRegion()) {
          //Trigger waiting scheme 
          waitLock.executingTask = true;
  
          //////////////////////////////////////////////////////////////////////////
          //1- Get the drill down manager
          NetworkDrillDownManager drillDownManager = 
            getControls().getNetworkControls().getNetworkDrillDownManager();
      
          //2- Update network origin
          drillDownManager.showRootNetwork();
      
          //3- Update network table filter's origins
          updateNetworkTableOrigin(getNetworkNetwork(), null);
          //////////////////////////////////////////////////////////////////////////
        }
      } catch (Exception e) {
        LoggingUtils.getSampleLogger().log(
            Level.SEVERE,
            "Could not properly show home with this exception: "
            + e.getLocalizedMessage());
      } finally {        
        //Shutdown waiting scheme 
        waitLock.executingTask = false;        
      }
    }
  }

  /**
   * Triggers the drill down process on the network module's network with the
   * provided object as the root.
   * 
   * @param showParent if false, the provided object will be made the root, otherwise 
   * its parent will be used as the root.
   */
  public void triggerDrillDownOnNetworkNetwork(IlpObject object, boolean showParent) {

    //1- Get the drill down manager
    NetworkDrillDownManager drillDownManager = 
      getControls().getNetworkControls().getNetworkDrillDownManager();

    IlpObject objectToFilterOn = null;

    //2- Update network origin
    if (showParent) {
      drillDownManager.showParentNetwork(object);

      //The parent is the object to filter the table on 
      objectToFilterOn = 
        CommonUtils.getParent(object, getNetworkNetwork().getDataSource());
    } else {
      drillDownManager.show(object);

      //The object itself is the object to filter the table on 
      objectToFilterOn = object;      
    }

    //3- Update network table filter's origins
    updateNetworkTableOrigin(getNetworkNetwork(), objectToFilterOn);
  }

  /**
   * Changes the visibility of the network module's overview and handles the logistics involved.
   */
  public void changeNetworkOverviewVisibility() {

    //1- Find the overview panel
    UIComponent overviewPanel = 
      getControls().getNetworkControls().getNetworkOverviewPanel();

    //2- Toggle its visibility 
    overviewPanel.setRendered(!overviewPanel.isRendered());
  }

  /**
   * Changes the visibility of the service module's overview and handles the logistics involved.
   */
  public void changeServiceOverviewVisibility() {

    //1- Find the overview panel
    UIComponent overviewPanel = 
      getControls().getNetworkControls().getServiceOverviewPanel();

    //2- Toggle its visibility 
    overviewPanel.setRendered(!overviewPanel.isRendered());
  }

  /**
   * Checks if the provided object is selected on the current view (being it 
   * either the service or the network module's network view).
   */
  public boolean isObjectSelectedOnCurrentView(IlpObject object) {

    //1- Find the selection model
    IlpGraphSelectionModel selectionModel = getCurrentSelectionModel();

    //2- Find if it is selected 
    return selectionModel.isObjectSelected(object);
  }

  /**
   * Selects the objects that are related to the provided object in the 
   * current network view (being it either the service or the network module's 
   * network view).
   * <p>
   * If this is the service network view, the proper highlights are added automatically.
   * 
   * @param clearCurrentSelection if false the current selection is kept, otherwise it is removed.
   * @param clearHighlighting if true (and if in the service module) the 
   * previous highlighting effect is cleared, otherwise it is kept in addition to
   * what will be added for the provided object's related objects.
   */
  public void selectRelatedObjectsOnCurrentView(IlpObject object,
      boolean clearCurrentSelection, boolean clearHighlighting) {

    //1- Find the selection model
    selectObjectsInCurrentView(object, clearCurrentSelection);

    //2- If this is the service view, process the highlighting of objects in view
    if (getActionProviders().getSharedActions().getCurrentModule() == SharedActionProvider.SERVICE_MODULE) {

      //3- Find the appropriate network 
      IlpNetwork network = getServiceNetwork();

      //4- Clear the current highlighting
      if (clearHighlighting) {
        getSampleContext().getServicesUtils().clearHighlightsInNetwork(network);
      }
      getSampleContext().getServicesUtils().highlightInNetwork(network, object);
    }
  }

  /**
   * Selects the objects that are related to the provided object in the 
   * current network view (being it either the service or the network module's 
   * network view).
   * 
   * @param clearCurrentSelection if false the current selection is kept, otherwise it is removed.
   */
  public void selectObjectsInCurrentView(IlpObject object, boolean clearCurrentSelection) {

    //1- Find the selection model
    IlpGraphSelectionModel selectionModel = getCurrentSelectionModel();

    //2- Find the objects to select on the view
    List<IlpObject> viewObjectsToBeSelected = getViewSpecificObjects(object);

    //3- Update the selection
    if (selectionModel != null) {
      getActionProviders().getSharedActions().selectObjects(
          selectionModel, viewObjectsToBeSelected, clearCurrentSelection);
    }
  }

  /**
   * Removes the selection of the provided object in the 
   * current network view (being it either the service or the network module's 
   * network view).
   */
  public void deselectObjectOnCurrentView(IlpObject object) {

    //1- Find the selection model
    IlpGraphSelectionModel selectionModel = getCurrentSelectionModel();

    //2- Update the selection
    if (selectionModel != null) {
      getActionProviders().getSharedActions().deselectObject(
          selectionModel, object);
    }
  }

  /**
   * Updates the display label of the network module's network with the 
   * appropriate identifier string representation of the current root. 
   */
  public void updateNetworkNetworkDisplayLabel() {

    String label = null;

    IlpNetworkAdapter adapter = getNetworkNetwork().getAdapter();

    if (adapter != null) {
      List<Object> origins = adapter.getOrigins();

      if (origins.isEmpty()) {
        label = ResourceUtils.getString("labelNetworkRootLevel");
      } else if (origins.size() == 1) {
        IlpDataSource dataSource = 
          getDataStructures().getDataSources().getNetworkDataSource();

        IlpObject object = dataSource.getObject(origins.get(0));
        String name = (String) object.getAttributeValue("name");
        label = (name != null) ? name : ResourceUtils.getString("labelNoObjectName");
      } else {
        label = ResourceUtils.getString("labelNetworkMultipleRoots");
      }
    }

    getControls().getNetworkControls().setNetworkNetworkDisplayLabel(label);
  }
  
  /**
   * Updates the display label of the service module's network with the 
   * appropriate identifier string representation of the current selection. 
   */
  public void updateNetworkServiceDisplayLabel() {

    String label = null;

    Collection<?> selection = getServiceNetwork().getSelectedObjects();

    if (selection.size() == 0) {
      label = ResourceUtils.getString("labelServiceRootLevel");
    } else if (selection.size() == 1) {
      
      IlpObject object = getServiceNetwork().getSelectedObject();
      String name = (String) object.getAttributeValue("name");
      label = (name != null) ? name : ResourceUtils.getString("labelNoObjectName");
    } else {
      label = ResourceUtils.getString("labelServiceMultipleRoots");
    }

    getControls().getNetworkControls().setServiceNetworkDisplayLabel(label);
  }

  //////////////////////////////////////////////////////////////////////////////
  //Private Methods
  //////////////////////////////////////////////////////////////////////////////

  /**
   * Try to show a detailed equipment view for a network element
   * @param ne the network element
   * @return Return false if the equipment cannot be shown
   */
  private boolean showEquipment(IltNetworkElement object) {

    if (object == null)
      return false; // can not do anything, open failed.

    EquipmentDrillDownManager drillDownManager = getControls()
    .getEquipmentControls().getEquipmentDrillDownManager();

    // If this object is already the root, do nothing
    if (drillDownManager.getCurrentRoot() == object)
      return true;

    // Show the equipment
    getActionProviders().getEquipmentActions()
    .triggerDrillDownOnInventoryEquipment(object, true);

    //Select the object on the inventory tree
    getActionProviders().getEquipmentActions()
    .selectRelatedObjectsOnInventoryView(object, true);

    //Find the Inventory TGO object 
    IlpObject inventoryTgoObject = getDataStructures().getDataSources()
    .getInventoryDataSource().getObject(object.getIdentifier());

    //Update the inventory tree
    getActionProviders().getTreeActions().selectObjectOnCurrentTree(
        inventoryTgoObject, false, true);

    getActionProviders().getSharedActions().
    setCurrentlySelectedObject(inventoryTgoObject);
    
    return true;
  }

  /**
   * Show a network in the main component.
   * @return Return false if the network cannot be shown
   */
  private boolean showNetwork(IlpObject ilpObject) {
    if (ilpObject == null)
      return false; // do nothing

    NetworkDrillDownManager drillDownManager = getControls()
    .getNetworkControls().getNetworkDrillDownManager();

    if (drillDownManager.isOrigin(ilpObject.getIdentifier()))
      return true; // already loaded, return as normal

    // Ask the drill down manager to show the network
    getActionProviders().getNetworkActions()
    .triggerDrillDownOnNetworkNetwork(ilpObject, false);

    //Select the object on the network tree
    getActionProviders().getNetworkActions()
    .selectRelatedObjectsOnCurrentView(ilpObject, true, true);

    //Update the tree
    getActionProviders().getTreeActions().
    selectObjectOnCurrentTree(ilpObject, false, true);
    
    return true;
  }
  
  /**
   * Returns the area that is to be displayed on the network given the 
   * object as being the current root.
   * 
   * @param identifier identifier of target object to be displayed.
   * @return an IlvRect with the area to be displayed.
   */
  public IlvRect getNetworkNetworkDisplayArea(Object identifier) {
    return  getControls().getNetworkControls().getVisibleAreaForObjectId(identifier);     
  }
  
  /**
   * Updates the display area of the service detail view.
   */
  public void updateServiceDetailDisplayArea(String serviceName) {

    IlvRect areaToDisplay = this.getControls().getNetworkControls().getVisibleAreaForObjectId(serviceName);

    if(areaToDisplay != null) {
      this.getControls().getNetworkControls().setServiceDetailNetworkDisplayedArea(areaToDisplay);      
    }
  }
  
  /**
   * Handle the special case of instanced of the custom type "MonitoringNEShortcut" that have 
   * some of their state contained in other instances, thus needing additional processing in
   * order to extract this state into itself. 
   */
  private void processShortcutSpecialCase(IlpDefaultDataSource serviceDataSource) throws Exception {

    // Associate pointing NE attributes to service network element shortcuts 
    Collection<IlpObject> objs = serviceDataSource.getObjects();
    for (IlpObject obj : objs) {
      ServicesUtils utility = getSampleContext().getServicesUtils();

      // Process shortcut objects only 
      if (utility.getMonitoringNEShortcutClass().isAssignableFrom(obj.getIlpClass())) {
        String ownerID = (String) obj.getAttributeValue(utility.getOwnerIDAttrInMonitoringNEShortcutClass());

        // Go through all NE objects in main network datasource looking 
        // for objects with id's matching service shortcut objects' 
        // ownerID value
        IlpDefaultDataSource networkDataSource = getDataStructures().getDataSources().getNetworkDataSource();

        Collection<IlpObject> netObjs = networkDataSource.getObjects();

        for (IlpObject netObj : netObjs) {
          if (netObj instanceof IltNetworkElement) {
            IltNetworkElement netElemObj = (IltNetworkElement) netObj;

            if (ownerID.equals(netElemObj.getIdentifier())) {
              IltNetworkElement shortcutObj = (IltNetworkElement) obj;
              shortcutObj.setName(netElemObj.getName());
              shortcutObj.setType(netElemObj.getType());
              shortcutObj.setPosition(netElemObj.getPosition());
            }
          }
        }
      }
    }
  }
  
  /**
   * Holds the execution for a bit in order to buy some time for the service details 
   * datasource to be loaded.
   */
  private void waitForDataSourceToBeLoaded(IlpDefaultDataSource dataSource) {

    while (dataSource.getObjects().isEmpty()) {
      try {

        //Sleep until the data source has some objects
        Thread.sleep(300);

      } catch (Exception e) {
        LoggingUtils.getSampleLogger().log(
            Level.SEVERE,
            "Could not properly load service details data source this exception: "
            + e.getLocalizedMessage());
      }      
    }

    try {

      //Sleep again for one more second just to give whoever is loading the datasource
      //some more time to complete it. The datasource is not very large so it should not
      //take long...
      Thread.sleep(1000);
    } catch (Exception e) {
      LoggingUtils.getSampleLogger().log(
          Level.WARNING,
          "Service details data source was probably improperly loaded with this exception:"
          + e.getLocalizedMessage());
    }      
  }

  private void updateNetworkTableOrigin(IlpNetwork network, IlpObject origin) {

    SampleDataModels dataModels = getDataStructures().getDataModels();

    getActionProviders().getSharedActions().updateTableOrigin(
        dataModels.getNetworkObjectsTableModelProvider(), origin);
  }
  
  private List<IlpObject> getViewSpecificObjects(IlpObject object) {
    List<IlpObject> relatedObjects = new ArrayList<IlpObject>();

    int currentModule = 
      getActionProviders().getSharedActions().getCurrentModule();
    if (SharedActionProvider.NETWORK_MODULE == currentModule) {
      relatedObjects.add(object);
    } else if (SharedActionProvider.SERVICE_MODULE == currentModule) {
      relatedObjects.addAll(getSampleContext().getServicesUtils()
          .getServiceNetworkNodes(object));
    }
    return relatedObjects;
  }
  
  private IlpGraphSelectionModel getCurrentSelectionModel() {

    //1- Find the selection model
    IltFacesDHTMLNetworkView facesGraphView = getCurrentView();

    IlpGraphSelectionModel selectionModel = null;

    try {
      selectionModel = facesGraphView.getNetwork().getSelectionModel();
    } catch (Exception e) {
      LoggingUtils.getSampleLogger().log(
          Level.SEVERE,
          "Could not find the selection model of the current view with this exception: "
          + e.getLocalizedMessage());
    }

    return selectionModel;
  }

  private IltFacesDHTMLNetworkView getCurrentView() {
    IltFacesDHTMLNetworkView view = null;

    int currentModule = 
      getActionProviders().getSharedActions().getCurrentModule();
    if (SharedActionProvider.NETWORK_MODULE == currentModule) {
      view = getControls().getNetworkControls().getNetworkNetworkView();
    } else if (SharedActionProvider.SERVICE_MODULE == currentModule) {
      view = getControls().getNetworkControls().getServiceNetworkView();
    } else {
      LoggingUtils.getSampleLogger().log(Level.SEVERE,
      "Could not find the current view");
    }
    return view;
  }
  
  private IlpNetwork getNetworkNetwork() {

    IlpNetwork network = null;

    try {
      network = getControls().getNetworkControls().getNetworkNetworkView().getNetwork();
    } catch (Exception e) {
      LoggingUtils.getSampleLogger().log(
          Level.WARNING,
          "Could not find Network Network with this exception: "
          + e.getLocalizedMessage());
    }
    return network;
  }

  private IlpNetwork getServiceNetwork() {

    IlpNetwork network = null;

    try {
      network = getControls().getNetworkControls().getServiceNetworkView().getNetwork();
    } catch (Exception e) {
      LoggingUtils.getSampleLogger().log(
          Level.WARNING,
          "Could not find Service Network with this exception: "
          + e.getLocalizedMessage());
    }
    return network;
  }
}