PortMapping Refactoring step 4 60/97860/3
authorguillaume.lambert <guillaume.lambert@orange.com>
Sun, 10 Oct 2021 21:03:43 +0000 (23:03 +0200)
committerguillaume.lambert <guillaume.lambert@orange.com>
Mon, 11 Oct 2021 10:41:59 +0000 (12:41 +0200)
This refactoring intends to optimize some parts of the code
and to fix sonar issues about cyclomatic complexity.
To this purpose, it introduces a few intermediate functions.

JIRA: TRNSPRTPCE-355 TRNSPRTPCE-356
Signed-off-by: guillaume.lambert <guillaume.lambert@orange.com>
Change-Id: I933c58d12444f928fc0aca692f37423b205d3cd9

common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion121.java
common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion221.java
common/src/main/java/org/opendaylight/transportpce/common/mapping/PortMappingVersion710.java

index 7994c4bd80d1c9798e2d9ec69bd0502921131f67..9862ec9ad978533bf1ae55a9cc24e9323387acd0 100644 (file)
@@ -323,41 +323,13 @@ public class PortMappingVersion121 {
                             break;
                         case Rx:
                         case Tx:
-                            if (!checkPartnerPortNotNull(port)) {
-                                LOG.info(PortMappingUtils.NO_VALID_PARTNERPORT_LOGMSG
-                                        + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
-                                    nodeId, port.getPortName(), circuitPackName);
+                            Ports port2 = getPartnerPort(port, circuitPackName, nodeId);
+                            if (port2 == null) {
                                 continue;
                             }
                             String lcp1 = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
                             LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
-                                    nodeId, port.getPortName(), circuitPackName, lcp1);
-                            InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                                .child(CircuitPacks.class,
-                                    new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
-                                .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName()));
-                            Optional<Ports> port2Object = this.deviceTransactionManager
-                                .getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, port2ID,
-                                    Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                            if (port2Object.isEmpty()
-                                || port2Object.get().getPortQual().getIntValue()
-                                    != Port.PortQual.RoadmExternal.getIntValue()) {
-                                LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG
-                                        + PortMappingUtils.PARTNERPORT_GET_ERROR_LOGMSG,
-                                    nodeId, port.getPartnerPort().getPortName(),
-                                    port.getPartnerPort().getCircuitPackName(),
-                                    port.getPortName(), circuitPackName);
-                                continue;
-                            }
-                            Ports port2 = port2Object.get();
-                            if (!checkPartnerPort(circuitPackName, port, port2)) {
-                                LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG
-                                        + PortMappingUtils.PARTNERPORT_CONF_ERROR_LOGMSG,
-                                    nodeId, port2.getPortName(), port.getPartnerPort().getCircuitPackName(),
-                                    port.getPortName(), circuitPackName);
-                                portIndex++;
-                                continue;
-                            }
+                                nodeId, port.getPortName(), circuitPackName, lcp1);
                             String lcp2 = createLogicalConnectionPort(port2, srgCpEntry.getKey(),portIndex);
                             LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
                                 nodeId, port2.getPortName(), circuitPackName, lcp2);
@@ -378,6 +350,38 @@ public class PortMappingVersion121 {
         return true;
     }
 
+    private Ports getPartnerPort(Ports port, String circuitPackName, String nodeId) {
+        if (!checkPartnerPortNotNull(port)) {
+            LOG.info(PortMappingUtils.NO_VALID_PARTNERPORT_LOGMSG + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
+                nodeId, port.getPortName(), circuitPackName);
+            return null;
+        }
+        InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
+            .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName()));
+        Optional<Ports> port2Object = this.deviceTransactionManager
+            .getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, port2ID,
+                Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
+        if (port2Object.isEmpty()
+                || port2Object.get().getPortQual().getIntValue() != Port.PortQual.RoadmExternal.getIntValue()) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG + PortMappingUtils.PARTNERPORT_GET_ERROR_LOGMSG,
+                nodeId, port.getPartnerPort().getPortName(), port.getPartnerPort().getCircuitPackName(),
+                port.getPortName(), circuitPackName);
+            return null;
+        }
+        Ports port2 = port2Object.get();
+        if (!checkPartnerPort(circuitPackName, port, port2)) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG + PortMappingUtils.PARTNERPORT_CONF_ERROR_LOGMSG,
+                nodeId, port2.getPortName(), port.getPartnerPort().getCircuitPackName(),
+                port.getPortName(), circuitPackName);
+            //portIndex++;
+            //TODO check if we really needed to increase portIndex
+            //     if yes this block should not be in getPartnerPort
+            return null;
+        }
+        return port2;
+    }
+
     private List<Ports> getPortList(String circuitPackName, String nodeId) {
         InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
             .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
@@ -801,22 +805,13 @@ public class PortMappingVersion121 {
             switch (cpMapValue.size()) {
                 case 1:
                     // port is bidirectional
-                    InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                        .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
-                        .child(Ports.class, new PortsKey(cp1.getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
-                    Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
-                        LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
-                        Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                    if (portObject.isEmpty()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
+                    Ports port = getTtpPort(cp1, cp1Name, nodeId);
+                    if (port == null) {
                         return false;
                     }
-                    Ports port = portObject.get();
                     if (!checkTtpPort(port, cp1Name, nodeId, true)) {
                         continue;
                     }
-
                     String logicalConnectionPoint =
                             PortMappingUtils.degreeTtpNodeName(cpMapEntry.getKey().toString(), "TXRX");
                     LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
@@ -825,52 +820,19 @@ public class PortMappingVersion121 {
                     break;
                 case 2:
                     // ports are unidirectionals
-                    ConnectionPorts cp2 = cpMapValue.get(1);
-                    String cp2Name = cp2.getCircuitPackName();
-                    InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                        .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
-                        .child(Ports.class, new PortsKey(cp1.getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
-                    Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
-                        LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
-                        Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                    InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                        .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
-                        .child(Ports.class, new PortsKey(cp2.getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp2.getPortName(), cp2Name);
-                    Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
-                        LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
-                        Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                    if (port1Object.isEmpty()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
+                    Ports port1 = getTtpPort(cp1, cp1Name, nodeId);
+                    if (port1 == null) {
                         return false;
                     }
-                    if (port2Object.isEmpty()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp2.getPortName(), cp2Name);
+                    ConnectionPorts cp2 = cpMapValue.get(1);
+                    String cp2Name = cp2.getCircuitPackName();
+                    Ports port2 = getTtpPort(cp2, cp2Name, nodeId);
+                    if (port2 == null) {
                         return false;
                     }
-
-                    Ports port1 = port1Object.get();
-                    if (!checkTtpPort(port1, cp1Name, nodeId, false)) {
-                        continue;
-                    }
-                    Ports port2 = port2Object.get();
-                    if (!checkTtpPort(port2, cp2Name, nodeId, false)) {
+                    if (!checkTtpPortsUnidir(port1, port2, cp1Name, cp2Name, nodeId)) {
                         continue;
                     }
-
-                    if (!checkPartnerPort(cp1Name, port1, port2)) {
-                        LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
-                            nodeId, port2.getPortName(), cp2Name, port1.getPortName(), cp1Name);
-                        continue;
-                    }
-                    // Directions checks are the same for cp1 and cp2, no need to check them twice.
-                    if (!checkPartnerPortNoDir(cp2Name, port2, port1)) {
-                        LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
-                            nodeId, port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
-                        continue;
-                    }
-
                     String logicalConnectionPoint1 = PortMappingUtils.degreeTtpNodeName(cpMapEntry.getKey().toString(),
                             port1.getPortDirection().getName().toUpperCase(Locale.getDefault()));
                     LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
@@ -885,11 +847,27 @@ public class PortMappingVersion121 {
                 default:
                     LOG.error(PortMappingUtils.NOT_CORRECT_CONPORT_LOGMSG, nodeId, cpMapEntry.getKey());
                     continue;
+                    //TODO should it be continue or return false ?
             }
         }
         return true;
     }
 
+    private Ports getTtpPort(ConnectionPorts cp, String cpName, String nodeId) {
+        InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey(cpName))
+            .child(Ports.class, new PortsKey(cp.getPortName()));
+        LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp.getPortName(), cpName);
+        Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
+            LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
+            Timeouts.DEVICE_READ_TIMEOUT_UNIT);
+        if (portObject.isEmpty()) {
+            LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp.getPortName(), cpName);
+            return null;
+        }
+        return portObject.get();
+    }
+
     private boolean checkPortQual(Ports port, String cpName, String nodeId) {
         if (port.getPortQual() == null) {
             return false;
@@ -916,14 +894,33 @@ public class PortMappingVersion121 {
         return true;
     }
 
-    private NodeInfo createNodeInfo(Info deviceInfo) {
+    private boolean checkTtpPortsUnidir(Ports port1, Ports port2, String cp1Name, String cp2Name, String nodeId) {
+        if (!checkTtpPort(port1, cp1Name, nodeId, false)) {
+            return false;
+        }
+        if (!checkTtpPort(port2, cp2Name, nodeId, false)) {
+            return false;
+        }
+        if (!checkPartnerPort(cp1Name, port1, port2)) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
+                nodeId, port2.getPortName(), cp2Name, port1.getPortName(), cp1Name);
+            return false;
+        }
+        // Directions checks are the same for cp1 and cp2, no need to check them twice.
+        if (!checkPartnerPortNoDir(cp2Name, port2, port1)) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
+                nodeId, port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
+            return false;
+        }
+        return true;
+    }
 
+    private NodeInfo createNodeInfo(Info deviceInfo) {
         if (deviceInfo.getNodeType() == null) {
             // TODO make mandatory in yang
             LOG.error(PortMappingUtils.NODE_TYPE_LOGMSG, deviceInfo.getNodeId(), "field missing");
             return null;
         }
-
         NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
                 .setOpenroadmVersion(OpenroadmNodeVersion._121)
                 .setNodeClli(
@@ -949,7 +946,6 @@ public class PortMappingVersion121 {
         if (deviceInfo.getIpAddress() != null) {
             nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
         }
-
         return nodeInfoBldr.build();
     }
 
index 595af60fa2e4f56e17bb62bf43264e37e8a1134c..1f659aec3575211388d133cacf2022ff4a6e03d3 100644 (file)
@@ -430,41 +430,13 @@ public class PortMappingVersion221 {
                             break;
                         case Rx:
                         case Tx:
-                            if (!checkPartnerPortNotNull(port)) {
-                                LOG.info(PortMappingUtils.NO_VALID_PARTNERPORT_LOGMSG
-                                        + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
-                                    nodeId, port.getPortName(), circuitPackName);
+                            Ports port2 = getPartnerPort(port, circuitPackName, nodeId);
+                            if (port2 == null) {
                                 continue;
                             }
                             String lcp1 = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
                             LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
                                 nodeId, port.getPortName(), circuitPackName, lcp1);
-                            InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                                .child(CircuitPacks.class,
-                                    new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
-                                .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName()));
-                            Optional<Ports> port2Object = this.deviceTransactionManager
-                                .getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, port2ID,
-                                    Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                            if (port2Object.isEmpty()
-                                || port2Object.get().getPortQual().getIntValue()
-                                    != PortQual.RoadmExternal.getIntValue()) {
-                                LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG
-                                        + PortMappingUtils.PARTNERPORT_GET_ERROR_LOGMSG,
-                                    nodeId, port.getPartnerPort().getPortName(),
-                                    port.getPartnerPort().getCircuitPackName(),
-                                    port.getPortName(), circuitPackName);
-                                continue;
-                            }
-                            Ports port2 = port2Object.get();
-                            if (!checkPartnerPort(circuitPackName, port, port2)) {
-                                LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG
-                                        + PortMappingUtils.PARTNERPORT_CONF_ERROR_LOGMSG,
-                                    nodeId, port2.getPortName(), port.getPartnerPort().getCircuitPackName(),
-                                    port.getPortName(), circuitPackName);
-                                portIndex++;
-                                continue;
-                            }
                             String lcp2 = createLogicalConnectionPort(port2, srgCpEntry.getKey(),portIndex);
                             LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
                                     nodeId, port2.getPortName(), circuitPackName, lcp2);
@@ -485,6 +457,38 @@ public class PortMappingVersion221 {
         return true;
     }
 
+    private Ports getPartnerPort(Ports port, String circuitPackName, String nodeId) {
+        if (!checkPartnerPortNotNull(port)) {
+            LOG.info(PortMappingUtils.NO_VALID_PARTNERPORT_LOGMSG + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
+                nodeId, port.getPortName(), circuitPackName);
+            return null;
+        }
+        InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
+            .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName()));
+        Optional<Ports> port2Object = this.deviceTransactionManager
+            .getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, port2ID,
+                Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
+        if (port2Object.isEmpty()
+                || port2Object.get().getPortQual().getIntValue() != PortQual.RoadmExternal.getIntValue()) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG + PortMappingUtils.PARTNERPORT_GET_ERROR_LOGMSG,
+                nodeId, port.getPartnerPort().getPortName(), port.getPartnerPort().getCircuitPackName(),
+                port.getPortName(), circuitPackName);
+            return null;
+        }
+        Ports port2 = port2Object.get();
+        if (!checkPartnerPort(circuitPackName, port, port2)) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG + PortMappingUtils.PARTNERPORT_CONF_ERROR_LOGMSG,
+                nodeId, port2.getPortName(), port.getPartnerPort().getCircuitPackName(),
+                port.getPortName(), circuitPackName);
+            //portIndex++;
+            //TODO check if we really needed to increase portIndex
+            //     if yes this block should not be in getPartnerPort
+            return null;
+        }
+        return port2;
+    }
+
     private List<Ports> getPortList(String circuitPackName, String nodeId) {
         InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
             .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
@@ -1020,22 +1024,13 @@ public class PortMappingVersion221 {
             switch (cpMapValue.size()) {
                 case 1:
                     // port is bidirectional
-                    InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                        .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
-                        .child(Ports.class, new PortsKey(cp1.getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
-                    Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
-                        LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
-                        Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                    if (portObject.isEmpty()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
+                    Ports port = getTtpPort(cp1, cp1Name, nodeId);
+                    if (port == null) {
                         return false;
                     }
-                    Ports port = portObject.get();
                     if (!checkTtpPort(port, cp1Name, nodeId, true)) {
                         continue;
                     }
-
                     String logicalConnectionPoint =
                             PortMappingUtils.degreeTtpNodeName(cpMapEntry.getKey().toString(), "TXRX");
                     LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
@@ -1044,52 +1039,19 @@ public class PortMappingVersion221 {
                     break;
                 case 2:
                     // ports are unidirectionals
-                    ConnectionPorts cp2 = cpMapValue.get(1);
-                    String cp2Name = cp2.getCircuitPackName();
-                    InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                        .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
-                        .child(Ports.class, new PortsKey(cp1.getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
-                    Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
-                        LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
-                        Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                    InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                        .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
-                        .child(Ports.class, new PortsKey(cp2.getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp2.getPortName(), cp2Name);
-                    Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
-                        LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
-                        Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                    if (port1Object.isEmpty()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
+                    Ports port1 = getTtpPort(cp1, cp1Name, nodeId);
+                    if (port1 == null) {
                         return false;
                     }
-                    if (port2Object.isEmpty()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp2.getPortName(), cp2Name);
+                    ConnectionPorts cp2 = cpMapValue.get(1);
+                    String cp2Name = cp2.getCircuitPackName();
+                    Ports port2 = getTtpPort(cp2, cp2Name, nodeId);
+                    if (port2 == null) {
                         return false;
                     }
-
-                    Ports port1 = port1Object.get();
-                    if (!checkTtpPort(port1, cp1Name, nodeId, false)) {
-                        continue;
-                    }
-                    Ports port2 = port2Object.get();
-                    if (!checkTtpPort(port2, cp2Name, nodeId, false)) {
+                    if (!checkTtpPortsUnidir(port1, port2, cp1Name, cp2Name, nodeId)) {
                         continue;
                     }
-
-                    if (!checkPartnerPort(cp1Name, port1, port2)) {
-                        LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
-                            nodeId, port2.getPortName(), cp2Name, port1.getPortName(), cp1Name);
-                        continue;
-                    }
-                    // Directions checks are the same for cp1 and cp2, no need to check them twice.
-                    if (!checkPartnerPortNoDir(cp2Name, port2, port1)) {
-                        LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
-                            nodeId, port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
-                        continue;
-                    }
-
                     String logicalConnectionPoint1 = PortMappingUtils.degreeTtpNodeName(cpMapEntry.getKey().toString(),
                             port1.getPortDirection().getName().toUpperCase(Locale.getDefault()));
                     LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
@@ -1104,11 +1066,27 @@ public class PortMappingVersion221 {
                 default:
                     LOG.error(PortMappingUtils.NOT_CORRECT_CONPORT_LOGMSG, nodeId, cpMapEntry.getKey());
                     continue;
+                    //TODO should it be continue or return false ?
             }
         }
         return true;
     }
 
+    private Ports getTtpPort(ConnectionPorts cp, String cpName, String nodeId) {
+        InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey(cpName))
+            .child(Ports.class, new PortsKey(cp.getPortName()));
+        LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp.getPortName(), cpName);
+        Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
+            LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
+            Timeouts.DEVICE_READ_TIMEOUT_UNIT);
+        if (portObject.isEmpty()) {
+            LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp.getPortName(), cpName);
+            return null;
+        }
+        return portObject.get();
+    }
+
     private boolean checkPortQual(Ports port, String cpName, String nodeId) {
         if (port.getPortQual() == null) {
             return false;
@@ -1135,14 +1113,33 @@ public class PortMappingVersion221 {
         return true;
     }
 
-    private NodeInfo createNodeInfo(Info deviceInfo) {
+    private boolean checkTtpPortsUnidir(Ports port1, Ports port2, String cp1Name, String cp2Name, String nodeId) {
+        if (!checkTtpPort(port1, cp1Name, nodeId, false)) {
+            return false;
+        }
+        if (!checkTtpPort(port2, cp2Name, nodeId, false)) {
+            return false;
+        }
+        if (!checkPartnerPort(cp1Name, port1, port2)) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
+                nodeId, port2.getPortName(), cp2Name, port1.getPortName(), cp1Name);
+            return false;
+        }
+        // Directions checks are the same for cp1 and cp2, no need to check them twice.
+        if (!checkPartnerPortNoDir(cp2Name, port2, port1)) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
+                nodeId, port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
+            return false;
+        }
+        return true;
+    }
 
+    private NodeInfo createNodeInfo(Info deviceInfo) {
         if (deviceInfo.getNodeType() == null) {
             // TODO make mandatory in yang
             LOG.error(PortMappingUtils.NODE_TYPE_LOGMSG, deviceInfo.getNodeId(), "field missing");
             return null;
         }
-
         NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
                 .setOpenroadmVersion(OpenroadmNodeVersion._221)
                 .setNodeClli(
@@ -1159,7 +1156,6 @@ public class PortMappingVersion221 {
         if (deviceInfo.getIpAddress() != null) {
             nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
         }
-
         return nodeInfoBldr.build();
     }
 
index 7c13bf054dcba3fc0cb35b544d1d0007f40a4a87..14ae5fb826487378475ba99957f2ebe074845598 100644 (file)
@@ -515,41 +515,13 @@ public class PortMappingVersion710 {
                             break;
                         case Rx:
                         case Tx:
-                            if (!checkPartnerPortNotNull(port)) {
-                                LOG.info(PortMappingUtils.NO_VALID_PARTNERPORT_LOGMSG
-                                        + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
-                                    nodeId, port.getPortName(), circuitPackName);
+                            Ports port2 = getPartnerPort(port, circuitPackName, nodeId);
+                            if (port2 == null) {
                                 continue;
                             }
                             String lcp1 = createLogicalConnectionPort(port, srgCpEntry.getKey(), portIndex);
                             LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
                                 nodeId, port.getPortName(), circuitPackName, lcp1);
-                            InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                                .child(CircuitPacks.class,
-                                    new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
-                                .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName()));
-                            Optional<Ports> port2Object = this.deviceTransactionManager
-                                .getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, port2ID,
-                                    Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                            if (port2Object.isEmpty()
-                                || port2Object.get().getPortQual().getIntValue()
-                                    != PortQual.RoadmExternal.getIntValue()) {
-                                LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG
-                                        + PortMappingUtils.PARTNERPORT_GET_ERROR_LOGMSG,
-                                    nodeId, port.getPartnerPort().getPortName(),
-                                    port.getPartnerPort().getCircuitPackName(),
-                                    port.getPortName(), circuitPackName);
-                                continue;
-                            }
-                            Ports port2 = port2Object.get();
-                            if (!checkPartnerPort(circuitPackName, port, port2)) {
-                                LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG
-                                        + PortMappingUtils.PARTNERPORT_CONF_ERROR_LOGMSG,
-                                    nodeId, port2.getPortName(), port.getPartnerPort().getCircuitPackName(),
-                                    port.getPortName(), circuitPackName);
-                                portIndex++;
-                                continue;
-                            }
                             String lcp2 = createLogicalConnectionPort(port2, srgCpEntry.getKey(),portIndex);
                             LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
                                 nodeId, port2.getPortName(), circuitPackName, lcp2);
@@ -570,6 +542,38 @@ public class PortMappingVersion710 {
         return true;
     }
 
+    private Ports getPartnerPort(Ports port, String circuitPackName, String nodeId) {
+        if (!checkPartnerPortNotNull(port)) {
+            LOG.info(PortMappingUtils.NO_VALID_PARTNERPORT_LOGMSG + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
+                nodeId, port.getPortName(), circuitPackName);
+            return null;
+        }
+        InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey(port.getPartnerPort().getCircuitPackName()))
+            .child(Ports.class, new PortsKey(port.getPartnerPort().getPortName()));
+        Optional<Ports> port2Object = this.deviceTransactionManager
+            .getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, port2ID,
+                Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
+        if (port2Object.isEmpty()
+                || port2Object.get().getPortQual().getIntValue() != PortQual.RoadmExternal.getIntValue()) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG + PortMappingUtils.PARTNERPORT_GET_ERROR_LOGMSG,
+                nodeId, port.getPartnerPort().getPortName(), port.getPartnerPort().getCircuitPackName(),
+                port.getPortName(), circuitPackName);
+            return null;
+        }
+        Ports port2 = port2Object.get();
+        if (!checkPartnerPort(circuitPackName, port, port2)) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG + PortMappingUtils.PARTNERPORT_CONF_ERROR_LOGMSG,
+                nodeId, port2.getPortName(), port.getPartnerPort().getCircuitPackName(),
+                port.getPortName(), circuitPackName);
+            //portIndex++;
+            //TODO check if we really needed to increase portIndex
+            //     if yes this block should not be in getPartnerPort
+            return null;
+        }
+        return port2;
+    }
+
     private List<Ports> getPortList(String circuitPackName, String nodeId) {
         InstanceIdentifier<CircuitPacks> cpIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
             .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName));
@@ -1237,22 +1241,13 @@ public class PortMappingVersion710 {
             switch (cpMapValue.size()) {
                 case 1:
                     // port is bidirectional
-                    InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                        .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
-                        .child(Ports.class, new PortsKey(cp1.getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
-                    Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
-                        LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
-                        Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                    if (portObject.isEmpty()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
+                    Ports port = getTtpPort(cp1, cp1Name, nodeId);
+                    if (port == null) {
                         return false;
                     }
-                    Ports port = portObject.get();
                     if (!checkTtpPort(port, cp1Name, nodeId, true)) {
                         continue;
                     }
-
                     String logicalConnectionPoint =
                             PortMappingUtils.degreeTtpNodeName(cpMapEntry.getKey().toString(), "TXRX");
                     LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
@@ -1261,52 +1256,19 @@ public class PortMappingVersion710 {
                     break;
                 case 2:
                     // ports are unidirectionals
-                    ConnectionPorts cp2 = cpMapValue.get(1);
-                    String cp2Name = cp2.getCircuitPackName();
-                    InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                        .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
-                        .child(Ports.class, new PortsKey(cp1.getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
-                    Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
-                        LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
-                        Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                    InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                        .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
-                        .child(Ports.class, new PortsKey(cp2.getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp2.getPortName(), cp2Name);
-                    Optional<Ports> port2Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
-                        LogicalDatastoreType.OPERATIONAL, port2ID, Timeouts.DEVICE_READ_TIMEOUT,
-                        Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                    if (port1Object.isEmpty()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
+                    Ports port1 = getTtpPort(cp1, cp1Name, nodeId);
+                    if (port1 == null) {
                         return false;
                     }
-                    if (port2Object.isEmpty()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp2.getPortName(), cp2Name);
+                    ConnectionPorts cp2 = cpMapValue.get(1);
+                    String cp2Name = cp2.getCircuitPackName();
+                    Ports port2 = getTtpPort(cp2, cp2Name, nodeId);
+                    if (port2 == null) {
                         return false;
                     }
-
-                    Ports port1 = port1Object.get();
-                    if (!checkTtpPort(port1, cp1Name, nodeId, false)) {
-                        continue;
-                    }
-                    Ports port2 = port2Object.get();
-                    if (!checkTtpPort(port2, cp2Name, nodeId, false)) {
+                    if (!checkTtpPortsUnidir(port1, port2, cp1Name, cp2Name, nodeId)) {
                         continue;
                     }
-
-                    if (!checkPartnerPort(cp1Name, port1, port2)) {
-                        LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
-                            nodeId, port2.getPortName(), cp2Name, port1.getPortName(), cp1Name);
-                        continue;
-                    }
-                    // Directions checks are the same for cp1 and cp2, no need to check them twice.
-                    if (!checkPartnerPortNoDir(cp2Name, port2, port1)) {
-                        LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
-                            nodeId, port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
-                        continue;
-                    }
-
                     String logicalConnectionPoint1 = PortMappingUtils.degreeTtpNodeName(cpMapEntry.getKey().toString(),
                             port1.getPortDirection().getName().toUpperCase(Locale.getDefault()));
                     LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
@@ -1321,11 +1283,27 @@ public class PortMappingVersion710 {
                 default:
                     LOG.error(PortMappingUtils.NOT_CORRECT_CONPORT_LOGMSG, nodeId, cpMapEntry.getKey());
                     continue;
+                    //TODO should it be continue or return false ?
             }
         }
         return true;
     }
 
+    private Ports getTtpPort(ConnectionPorts cp, String cpName, String nodeId) {
+        InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
+            .child(CircuitPacks.class, new CircuitPacksKey(cpName))
+            .child(Ports.class, new PortsKey(cp.getPortName()));
+        LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp.getPortName(), cpName);
+        Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
+            LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
+            Timeouts.DEVICE_READ_TIMEOUT_UNIT);
+        if (portObject.isEmpty()) {
+            LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp.getPortName(), cpName);
+            return null;
+        }
+        return portObject.get();
+    }
+
     private boolean checkPortQual(Ports port, String cpName, String nodeId) {
         if (port.getPortQual() == null) {
             return false;
@@ -1352,14 +1330,33 @@ public class PortMappingVersion710 {
         return true;
     }
 
-    private NodeInfo createNodeInfo(Info deviceInfo) {
+    private boolean checkTtpPortsUnidir(Ports port1, Ports port2, String cp1Name, String cp2Name, String nodeId) {
+        if (!checkTtpPort(port1, cp1Name, nodeId, false)) {
+            return false;
+        }
+        if (!checkTtpPort(port2, cp2Name, nodeId, false)) {
+            return false;
+        }
+        if (!checkPartnerPort(cp1Name, port1, port2)) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
+                nodeId, port2.getPortName(), cp2Name, port1.getPortName(), cp1Name);
+            return false;
+        }
+        // Directions checks are the same for cp1 and cp2, no need to check them twice.
+        if (!checkPartnerPortNoDir(cp2Name, port2, port1)) {
+            LOG.error(PortMappingUtils.NOT_CORRECT_PARTNERPORT_LOGMSG,
+                nodeId, port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
+            return false;
+        }
+        return true;
+    }
 
+    private NodeInfo createNodeInfo(Info deviceInfo) {
         if (deviceInfo.getNodeType() == null) {
             // TODO make mandatory in yang
             LOG.error(PortMappingUtils.NODE_TYPE_LOGMSG, deviceInfo.getNodeId(), "field missing");
             return null;
         }
-
         NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
                 .setOpenroadmVersion(OpenroadmNodeVersion._71)
                 .setNodeClli(
@@ -1377,7 +1374,6 @@ public class PortMappingVersion710 {
         if (deviceInfo.getIpAddress() != null) {
             nodeInfoBldr.setNodeIpAddress(deviceInfo.getIpAddress());
         }
-
         return nodeInfoBldr.build();
     }