Align and improve Portmapping versions Step 1
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingVersion710.java
index 077c949550ee54e3a8053534405cf4ff5b603e79..0987dfe7f3b9b1b9110ba24385b1f301c1fb646f 100644 (file)
@@ -99,6 +99,8 @@ import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+// FIXME: many common pieces of code between PortMapping Versions 121 and 221 and 710
+// some mutualization would be helpful
 public class PortMappingVersion710 {
     private static final Logger LOG = LoggerFactory.getLogger(PortMappingVersion710.class);
 
@@ -121,14 +123,12 @@ public class PortMappingVersion710 {
         Optional<Info> deviceInfoOptional = this.deviceTransactionManager.getDataFromDevice(
             nodeId, LogicalDatastoreType.OPERATIONAL, infoIID,
             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-        Info deviceInfo;
-        NodeInfo nodeInfo;
         if (!deviceInfoOptional.isPresent()) {
             LOG.warn("Device info subtree is absent for {}", nodeId);
             return false;
         }
-        deviceInfo = deviceInfoOptional.get();
-        nodeInfo = createNodeInfo(deviceInfo, nodeId);
+        Info deviceInfo = deviceInfoOptional.get();
+        NodeInfo nodeInfo = createNodeInfo(deviceInfo);
         if (nodeInfo == null) {
             return false;
         }
@@ -211,25 +211,22 @@ public class PortMappingVersion710 {
         Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
             LogicalDatastoreType.OPERATIONAL, deviceIID,
             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-        OrgOpenroadmDevice device = null;
         if (!deviceObject.isPresent()) {
             LOG.error("Impossible to get device configuration for node {}", nodeId);
             return false;
         }
-        device = deviceObject.get();
+        OrgOpenroadmDevice device = deviceObject.get();
+        if (device.getCircuitPacks() == null) {
+            LOG.warn("Circuit Packs are not present for {}", nodeId);
+            return false;
+        }
         // Variable to keep track of number of line ports
         int line = 1;
         // Variable to keep track of number of client ports
         int client = 1;
         Map<String, String> lcpMap = new HashMap<>();
         Map<String, Mapping> mappingMap = new HashMap<>();
-
-        List<CircuitPacks> circuitPackList = null;
-        if (device.getCircuitPacks() == null) {
-            LOG.warn("Circuit Packs are not present for {}", nodeId);
-            return false;
-        }
-        circuitPackList = new ArrayList<>(deviceObject.get().nonnullCircuitPacks().values());
+        List<CircuitPacks> circuitPackList = new ArrayList<>(device.nonnullCircuitPacks().values());
         circuitPackList.sort(Comparator.comparing(CircuitPack::getCircuitPackName));
 
         if (device.getXponder() == null) {
@@ -857,11 +854,10 @@ public class PortMappingVersion710 {
         Nodes nodes = nodesBldr.build();
         nodesList.put(nodes.key(),nodes);
 
-        NetworkBuilder nwBldr = new NetworkBuilder().setNodes(nodesList);
+        Network network = new NetworkBuilder().setNodes(nodesList).build();
 
         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
         InstanceIdentifier<Network> nodesIID = InstanceIdentifier.builder(Network.class).build();
-        Network network = nwBldr.build();
         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, nodesIID, network);
         FluentFuture<? extends @NonNull CommitInfo> commit = writeTransaction.commit();
         try {
@@ -952,11 +948,14 @@ public class PortMappingVersion710 {
     }
 
     private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
-        String logicalConnectionPoint) {
-        MappingBuilder mpBldr = new MappingBuilder();
-        mpBldr.withKey(new MappingKey(logicalConnectionPoint)).setLogicalConnectionPoint(logicalConnectionPoint)
-            .setSupportingCircuitPackName(circuitPackName).setSupportingPort(port.getPortName())
-            .setPortDirection(port.getPortDirection().getName());
+            String logicalConnectionPoint) {
+
+        MappingBuilder mpBldr = new MappingBuilder()
+                .withKey(new MappingKey(logicalConnectionPoint))
+                .setLogicalConnectionPoint(logicalConnectionPoint)
+                .setSupportingCircuitPackName(circuitPackName)
+                .setSupportingPort(port.getPortName())
+                .setPortDirection(port.getPortDirection().getName());
 
         // Get OMS and OTS interface provisioned on the TTP's
         if ((logicalConnectionPoint.contains(StringConstants.TTP_TOKEN)
@@ -993,37 +992,34 @@ public class PortMappingVersion710 {
     }
 
     private Mapping createXpdrMappingObject(String nodeId, Ports ports, String circuitPackName,
-        String logicalConnectionPoint, String partnerLcp, Mapping mapping, String connectionMapLcp,
-        XpdrNodeTypes xpdrNodeType) {
-        MappingBuilder mpBldr;
+            String logicalConnectionPoint, String partnerLcp, Mapping mapping, String connectionMapLcp,
+            XpdrNodeTypes xpdrNodeType) {
+
         if (mapping != null && connectionMapLcp != null) {
             // update existing mapping
-            mpBldr = new MappingBuilder(mapping).setConnectionMapLcp(connectionMapLcp);
-        } else {
-            // create a new mapping
-            String nodeIdLcp = nodeId + "-" + logicalConnectionPoint;
-            mpBldr = new MappingBuilder()
+            return new MappingBuilder(mapping).setConnectionMapLcp(connectionMapLcp).build();
+        }
+
+        // create a new mapping
+        String nodeIdLcp = nodeId + "-" + logicalConnectionPoint;
+        MappingBuilder mpBldr = new MappingBuilder()
                 .withKey(new MappingKey(logicalConnectionPoint))
                 .setLogicalConnectionPoint(logicalConnectionPoint)
                 .setSupportingCircuitPackName(circuitPackName)
                 .setSupportingPort(ports.getPortName())
                 .setPortDirection(ports.getPortDirection().getName())
                 .setLcpHashVal(FnvUtils.fnv1_64(nodeIdLcp));
-
-            if (ports.getPortQual() != null) {
-                mpBldr.setPortQual(ports.getPortQual().getName());
-            }
-
-            if (ports.augmentation(Ports1.class).getPortCapabilities().getSupportedInterfaceCapability() != null) {
-                mpBldr.setSupportedInterfaceCapability(getSupportedIfCapability(ports));
-            }
-
-            if (xpdrNodeType != null) {
-                mpBldr.setXponderType(xpdrNodeType);
-            }
-            if (partnerLcp != null) {
-                mpBldr.setPartnerLcp(partnerLcp);
-            }
+        if (ports.getPortQual() != null) {
+            mpBldr.setPortQual(ports.getPortQual().getName());
+        }
+        if (ports.augmentation(Ports1.class).getPortCapabilities().getSupportedInterfaceCapability() != null) {
+            mpBldr.setSupportedInterfaceCapability(getSupportedIfCapability(ports));
+        }
+        if (xpdrNodeType != null) {
+            mpBldr.setXponderType(xpdrNodeType);
+        }
+        if (partnerLcp != null) {
+            mpBldr.setPartnerLcp(partnerLcp);
         }
         return mpBldr.build();
     }
@@ -1179,29 +1175,29 @@ public class PortMappingVersion710 {
         return true;
     }
 
-    private NodeInfo createNodeInfo(Info deviceInfo, String nodeId) {
-        NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder();
+    private NodeInfo createNodeInfo(Info deviceInfo) {
+
         if (deviceInfo.getNodeType() == null) {
             // TODO make mandatory in yang
             LOG.error("Node type field is missing");
             return null;
         }
 
-        nodeInfoBldr.setOpenroadmVersion(OpenroadmVersion._710).setNodeType(deviceInfo.getNodeType());
+        NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
+                .setOpenroadmVersion(OpenroadmVersion._710)
+                .setNodeType(deviceInfo.getNodeType());
+        // TODO: 221 versions expects an int value - need to check whether it is bug or an evolution here
         if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
             nodeInfoBldr.setNodeClli(deviceInfo.getClli());
         } else {
             nodeInfoBldr.setNodeClli("defaultCLLI");
         }
-
         if (deviceInfo.getModel() != null) {
             nodeInfoBldr.setNodeModel(deviceInfo.getModel());
         }
-
         if (deviceInfo.getVendor() != null) {
             nodeInfoBldr.setNodeVendor(deviceInfo.getVendor());
         }
-
         if (deviceInfo.getIpAddress() != null) {
             nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
         }