split checkPartnerPort method in PortMapping
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingVersion710.java
index ed4b697cc59760aa87b918e6ecc5f64f9cabc713..ff115bfd254e51767c1b7fa4f8ffab2f4d27e931 100644 (file)
@@ -267,9 +267,7 @@ public class PortMappingVersion710 {
                             // TODO PortDirection treatment here is similar to the one in createPpPortMapping.
                             //      Some code alignment must be considered.
 
-                            if (port.getPartnerPort() == null
-                                || port.getPartnerPort().getCircuitPackName() == null
-                                || port.getPartnerPort().getPortName() == null) {
+                            if (!checkPartnerPortNotNull(port)) {
                                 LOG.warn("Error in the configuration of port {} of {} for {}",
                                     port.getPortName(), circuitPackName, nodeId);
                                 continue;
@@ -396,9 +394,7 @@ public class PortMappingVersion710 {
                             // TODO PortDirection treatment here is similar to the one in createPpPortMapping.
                             //      Some code alignment must be considered.
 
-                            if (port.getPartnerPort() == null
-                                || port.getPartnerPort().getCircuitPackName() == null
-                                || port.getPartnerPort().getPortName() == null) {
+                            if (!checkPartnerPortNotNull(port)) {
                                 LOG.warn("Error in the configuration of port {} of {} for {}",
                                     port.getPortName(), circuitPackName, nodeId);
                                 continue;
@@ -531,12 +527,26 @@ public class PortMappingVersion710 {
         return true;
     }
 
-    private boolean checkPartnerPort(String circuitPackName, Ports port1, Ports port2) {
-        if (port2.getPartnerPort() == null
-            || port2.getPartnerPort().getCircuitPackName() == null
-            || port2.getPartnerPort().getPortName() == null
+    private boolean checkPartnerPortNotNull(Ports port) {
+        if (port.getPartnerPort() == null
+            || port.getPartnerPort().getCircuitPackName() == null
+            || port.getPartnerPort().getPortName() == null) {
+            return false;
+        }
+        return true;
+    }
+
+    private boolean checkPartnerPortNoDir(String circuitPackName, Ports port1, Ports port2) {
+        if (!checkPartnerPortNotNull(port2)
             || !port2.getPartnerPort().getCircuitPackName().equals(circuitPackName)
-            || !port2.getPartnerPort().getPortName().equals(port1.getPortName())
+            || !port2.getPartnerPort().getPortName().equals(port1.getPortName())) {
+            return false;
+        }
+        return true;
+    }
+
+    private boolean checkPartnerPort(String circuitPackName, Ports port1, Ports port2) {
+        if (!checkPartnerPortNoDir(circuitPackName, port1, port2)
             || ((Direction.Rx.getIntValue() != port1.getPortDirection().getIntValue()
                     || Direction.Tx.getIntValue() != port2.getPortDirection().getIntValue())
                 &&
@@ -628,8 +638,8 @@ public class PortMappingVersion710 {
 
                         case Rx:
                         case Tx:
-                            if (port.getPartnerPort() == null) {
-                                LOG.info("{} : port {} on {} is unidirectional but has no partnerPort"
+                            if (!checkPartnerPortNotNull(port)) {
+                                LOG.info("{} : port {} on {} is unidirectional but has no valid partnerPort"
                                     + " - cannot assign  logicalConnectionPoint.",
                                     nodeId, port.getPortName(), circuitPackName);
                                 continue;
@@ -864,20 +874,16 @@ public class PortMappingVersion710 {
         }
         if (portMapList != null) {
             Map<MappingKey, Mapping> mappingMap = new HashMap<>();
+            // No element in the list below should be null at this stage
             for (Mapping mapping: portMapList) {
-                if (mapping == null) {
-                    continue;
-                }
                 mappingMap.put(mapping.key(), mapping);
             }
             nodesBldr.setMapping(mappingMap);
         }
         if (cp2DegreeList != null) {
             Map<CpToDegreeKey, CpToDegree> cpToDegreeMap = new HashMap<>();
+            // No element in the list below should be null at this stage
             for (CpToDegree cp2Degree: cp2DegreeList) {
-                if (cp2Degree == null) {
-                    continue;
-                }
                 cpToDegreeMap.put(cp2Degree.key(), cp2Degree);
             }
             nodesBldr.setCpToDegree(cpToDegreeMap);
@@ -885,10 +891,8 @@ public class PortMappingVersion710 {
 
         if (splList != null) {
             Map<SwitchingPoolLcpKey,SwitchingPoolLcp> splMap = new HashMap<>();
+            // No element in the list below should be null at this stage
             for (SwitchingPoolLcp spl: splList) {
-                if (spl == null) {
-                    continue;
-                }
                 splMap.put(spl.key(), spl);
             }
             nodesBldr.setSwitchingPoolLcp(splMap);
@@ -1171,22 +1175,24 @@ public class PortMappingVersion710 {
                         continue;
                     }
                     if (PortQual.RoadmExternal.getIntValue() != port1.getPortQual().getIntValue()
-                        || PortQual.RoadmExternal.getIntValue() != port2.getPortQual().getIntValue()
-                        || port1.getPartnerPort() == null
-                        || port2.getPartnerPort() == null
-                        || !port1.getPartnerPort().getCircuitPackName().equals(cp2Name)
-                        || !port1.getPartnerPort().getPortName().equals(port2.getPortName())
-                        || !port2.getPartnerPort().getCircuitPackName().equals(cp1Name)
-                        || !port2.getPartnerPort().getPortName().equals(port1.getPortName())
-                        || ((Direction.Rx.getIntValue() != port1.getPortDirection().getIntValue()
-                                || Direction.Tx.getIntValue() != port2.getPortDirection().getIntValue())
-                            && (Direction.Rx.getIntValue() != port2.getPortDirection().getIntValue()
-                                || Direction.Tx.getIntValue() != port1.getPortDirection().getIntValue()))) {
-                        LOG.error("Impossible to create logical connection point for port {} or port {} on node {} - "
-                                + "Error in configuration with port-qual, port-direction or partner-port configuration",
+                        || PortQual.RoadmExternal.getIntValue() != port2.getPortQual().getIntValue()) {
+                        LOG.error("Impossible to create logical connection point for port {} or port {} on node {}"
+                                + " - Error in configuration with port-qual",
                             port1.getPortName(), port2.getPortName(), nodeId);
                         continue;
                     }
+                    if (!checkPartnerPort(cp1Name, port1, port2)) {
+                        LOG.error("port {} on {} is not a correct partner port of {} on  {}",
+                            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("port {} on {} is not a correct partner port of {} on  {}",
+                            port1.getPortName(), cp1Name, port2.getPortName(), cp2Name);
+                        continue;
+                    }
+
                     String logicalConnectionPoint1 = new StringBuilder("DEG")
                         .append(cpMapEntry.getKey())
                         .append("-TTP-")