Corrects various javadoc problems
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / mapping / PortMapping.java
index 7945cf4edd2365f42a54d0c7b7c12c46b1e5dc77..1cd38e87da19531275c1f7cc8785d393ba88025f 100644 (file)
@@ -22,13 +22,13 @@ import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaces;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.NodeTypes;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.ConnectionPorts;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Degree;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.DegreeKey;
@@ -101,14 +101,19 @@ public class PortMapping {
      */
     public boolean createMappingData() {
 
-        LOG.info(" Create Mapping Data for node " + nodeId);
+        LOG.info("Create Mapping Data for node " + nodeId);
         DataBroker deviceDb = getDeviceDataBroker(nodeId, mps);
-        Info deviceInfo = getDeviceInfo(deviceDb);
         List<Mapping> portMapList = new ArrayList<>();
-
+        Info deviceInfo;
+        Integer nodeType = 1;
         if (deviceDb != null) {
+            deviceInfo = getDeviceInfo(deviceDb);
             if (deviceInfo != null) {
-                Integer nodeType = deviceInfo.getNodeType().getIntValue();
+                if (deviceInfo.getNodeType() == null) {
+                    LOG.info("Node type mandatory field is missing");
+                    return false;
+                }
+                nodeType = deviceInfo.getNodeType().getIntValue();
                 // Create Mapping for Roadm Node
                 if (nodeType == 1) {
                     // Get TTP port mapping
@@ -133,15 +138,15 @@ public class PortMapping {
                     }
                 }
             } else {
-                LOG.info(" Device info subtree is absent for " + nodeId);
+                LOG.info("Device info subtree is absent for " + nodeId);
                 return false;
             }
 
         } else {
-            LOG.info(" Unable to get Data broker for node " + nodeId);
+            LOG.info("Unable to get Data broker for node " + nodeId);
             return false;
         }
-        return postPortMapping(deviceInfo, portMapList);
+        return postPortMapping(deviceInfo, portMapList, nodeType);
     }
 
     /**
@@ -191,7 +196,7 @@ public class PortMapping {
                 }
             } catch (InterruptedException | ExecutionException ex) {
                 LOG.warn("Read failed for Logical Connection Point value missing for " + circuitPackName + " "
-                    + portName);
+                    + portName,ex);
                 return false;
             }
         }
@@ -273,7 +278,7 @@ public class PortMapping {
 
                 }
             } catch (InterruptedException | ExecutionException ex) {
-                LOG.warn("Read failed for " + circuitPackName);
+                LOG.warn("Read failed for " + circuitPackName,ex);
                 return false;
             }
         }
@@ -338,7 +343,7 @@ public class PortMapping {
             }
 
         } catch (InterruptedException | ExecutionException ex) {
-            LOG.warn("Read failed for CircuitPacks of " + nodeId);
+            LOG.warn("Read failed for CircuitPacks of " + nodeId,ex);
             return false;
         }
         return true;
@@ -370,8 +375,8 @@ public class PortMapping {
         // Get OMS and OTS interface provisioned on the TTP's
         if (logicalConnectionPoint.contains("TTP") && port.getInterfaces() != null) {
             for (Interfaces interfaces : port.getInterfaces()) {
-                Class<? extends InterfaceType> interfaceType = getInterfaceType(deviceDb, interfaces
-                    .getInterfaceName());
+                Class<? extends InterfaceType> interfaceType = new OpenRoadmInterfaces(db, mps, nodeId,
+                    logicalConnectionPoint).getInterface(interfaces.getInterfaceName()).getType();
                 // Check if interface type is OMS or OTS
                 if (interfaceType.equals(OpenROADMOpticalMultiplex.class)) {
                     String omsInterfaceName = interfaces.getInterfaceName();
@@ -386,37 +391,6 @@ public class PortMapping {
         return mpBldr.build();
     }
 
-    /**
-     * This private does a get on the interface subtree of the device with the
-     * interface name as the key and return the class corresponding to the
-     * interface type.
-     *
-     * @param interfaceName
-     *            Name of the interface
-     * @param deviceDb
-     *            Reference to device's databroker.
-     *
-     * @return true/false based on status of operation
-     */
-
-    private Class<? extends InterfaceType> getInterfaceType(DataBroker deviceDb, String interfaceName) {
-        ReadOnlyTransaction rtx = deviceDb.newReadOnlyTransaction();
-        InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
-            Interface.class, new InterfaceKey(interfaceName));
-        try {
-            Optional<Interface> interfaceObject = rtx.read(LogicalDatastoreType.OPERATIONAL, interfacesIID).get();
-            if (interfaceObject.isPresent()) {
-                return interfaceObject.get().getType();
-            } else {
-                LOG.info("Interface subtree is not present for " + interfaceName);
-            }
-        } catch (InterruptedException | ExecutionException ex) {
-            LOG.info("Read failed on interface subtree for");
-            return null;
-        }
-        return null;
-    }
-
     /**
      * This method does a get operation on info subtree of the netconf device's
      * configuration datastore and returns info object.It is required to get
@@ -433,12 +407,16 @@ public class PortMapping {
         try {
             Optional<Info> ordmInfoObject = rtx.read(LogicalDatastoreType.OPERATIONAL, infoIID).get();
             if (ordmInfoObject.isPresent()) {
+                LOG.info("Info subtree is present {}", ordmInfoObject.get());
                 return ordmInfoObject.get();
             } else {
-                LOG.info("Info subtree is not present");
+                LOG.error("Info subtree is not present");
             }
+        } catch (NullPointerException ex) {
+            LOG.warn("Try to get Info from a non Open ROADM device {}", deviceDb);
+            return null;
         } catch (InterruptedException | ExecutionException ex) {
-            LOG.info("Read failed on info subtree for");
+            LOG.error("Read failed on info subtree ", ex);
             return null;
         }
         return null;
@@ -478,14 +456,14 @@ public class PortMapping {
                 Optional<Degree> ordmDegreeObject = rtx.read(LogicalDatastoreType.CONFIGURATION, deviceIID).get();
 
                 if (ordmDegreeObject.isPresent()) {
-                    degreeConPorts.addAll(new ArrayList<ConnectionPorts>(ordmDegreeObject.get().getConnectionPorts()));
+                    degreeConPorts.addAll(new ArrayList<>(ordmDegreeObject.get().getConnectionPorts()));
 
                 } else {
                     LOG.info("Device has " + (degreeCounter - 1) + " degree");
                     break;
                 }
             } catch (InterruptedException | ExecutionException ex) {
-                LOG.info("Failed to read degree " + degreeCounter);
+                LOG.error("Failed to read degree " + degreeCounter,ex);
                 break;
 
             }
@@ -534,15 +512,14 @@ public class PortMapping {
                 if (ordmSrgObject.isPresent()) {
 
                     srgCps.addAll(
-                        new ArrayList<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
-                        .CircuitPacks>(ordmSrgObject.get().getCircuitPacks()));
+                        new ArrayList<>(ordmSrgObject.get().getCircuitPacks()));
 
                 } else {
                     LOG.info("Device has " + (srgCounter - 1) + " Srg");
                     break;
                 }
             } catch (InterruptedException | ExecutionException ex) {
-                LOG.warn("Failed to read Srg " + srgCounter);
+                LOG.warn("Failed to read Srg " + srgCounter,ex);
                 break;
             }
             srgCounter++;
@@ -567,12 +544,13 @@ public class PortMapping {
      *
      * @return Result true/false based on status of operation.
      */
-    private boolean postPortMapping(Info deviceInfo, List<Mapping> portMapList) {
+    private boolean postPortMapping(Info deviceInfo, List<Mapping> portMapList, Integer nodeType) {
 
-        List<Nodes> nodesList = new ArrayList<>();
         NodesBuilder nodesBldr = new NodesBuilder();
         nodesBldr.setKey(new NodesKey(deviceInfo.getNodeId())).setNodeId(deviceInfo.getNodeId());
+        nodesBldr.setNodeType(NodeTypes.forValue(nodeType));
         nodesBldr.setMapping(portMapList);
+        List<Nodes> nodesList = new ArrayList<>();
         nodesList.add(nodesBldr.build());
         NetworkBuilder nwBldr = new NetworkBuilder();
         nwBldr.setNodes(nodesList);
@@ -611,6 +589,8 @@ public class PortMapping {
      *            Unique Identifier for the node of interest.
      * @param logicalConnPoint
      *            Name of the logical point
+     * @param db
+     *            Databroker / MD-SAL data store
      *
      * @return Result Mapping object if success otherwise null.
      */
@@ -634,8 +614,8 @@ public class PortMapping {
                 return null;
             }
         } catch (InterruptedException | ExecutionException ex) {
-            LOG.info("Unable to read mapping for logical connection point : " + logicalConnPoint + " for nodeId "
-                + nodeId);
+            LOG.error("Unable to read mapping for logical connection point : " + logicalConnPoint + " for nodeId "
+                + nodeId,ex);
         }
         return null;
     }
@@ -650,20 +630,30 @@ public class PortMapping {
      * @return Databroker for the given device
      */
     public static DataBroker getDeviceDataBroker(String nodeId, MountPointService mps) {
+        MountPoint netconfNode = getDeviceMountPoint(nodeId, mps);
+        if (netconfNode != null) {
+            DataBroker netconfNodeDataBroker = netconfNode.getService(DataBroker.class).get();
+            return netconfNodeDataBroker;
+        } else {
+            LOG.error("Device Data broker not found for :" + nodeId);
+            return null;
+        }
+    }
+
+    public static MountPoint getDeviceMountPoint(String nodeId, MountPointService mps) {
         InstanceIdentifier<Node> netconfNodeIID = InstanceIdentifier.builder(NetworkTopology.class).child(
             Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName()))).child(Node.class,
                 new NodeKey(new NodeId(nodeId))).build();
 
-        // Get the mount point for the specified node
+        // Get mount point for specified device
         final Optional<MountPoint> netconfNodeOptional = mps.getMountPoint(netconfNodeIID);
         if (netconfNodeOptional.isPresent()) {
             MountPoint netconfNode = netconfNodeOptional.get();
-            // Get the DataBroker for the mounted node
-            DataBroker netconfNodeDataBroker = netconfNode.getService(DataBroker.class).get();
-            return netconfNodeDataBroker;
+            return netconfNode;
         } else {
-            LOG.info("Device Data broker not found for :" + nodeId);
+            LOG.error("Mount Point not found for :" + nodeId);
+            return null;
         }
-        return null;
+
     }
-}
\ No newline at end of file
+}