PortMapping Refactoring step 2
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingVersion121.java
index 661a532bb95d9bbab9973a33c20f1510711adba6..409ad5ebfd73ab67b05c844092f91852aff2a6ce 100644 (file)
@@ -18,6 +18,7 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
+import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
@@ -85,6 +86,7 @@ public class PortMappingVersion121 {
 
     private static final Logger LOG = LoggerFactory.getLogger(PortMappingVersion121.class);
     private static final Map<Direction, String> SUFFIX;
+    private static final Set<Integer> TXRX_SET = Set.of(Direction.Tx.getIntValue(), Direction.Rx.getIntValue());
 
     private final DataBroker dataBroker;
     private final DeviceTransactionManager deviceTransactionManager;
@@ -262,12 +264,9 @@ public class PortMappingVersion121 {
     }
 
     private boolean checkPartnerPort(String circuitPackName, Ports port1, Ports port2) {
-        return (checkPartnerPortNoDir(circuitPackName, port1, port2)
-            && ((Direction.Rx.getIntValue() == port1.getPortDirection().getIntValue()
-                    && Direction.Tx.getIntValue() == port2.getPortDirection().getIntValue())
-                ||
-                (Direction.Tx.getIntValue() == port1.getPortDirection().getIntValue()
-                    && Direction.Rx.getIntValue() == port2.getPortDirection().getIntValue())));
+        return checkPartnerPortNoDir(circuitPackName, port1, port2)
+            && Set.of(port1.getPortDirection().getIntValue(), port2.getPortDirection().getIntValue())
+                .equals(TXRX_SET);
     }
 
     private HashMap<Integer, List<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg
@@ -329,17 +328,11 @@ public class PortMappingVersion121 {
                 Collections.sort(portList, new SortPort121ByName());
                 int portIndex = 1;
                 for (Ports port : portList) {
-                    String currentKey = circuitPackName + "-" + port.getPortName();
-                    if (port.getPortQual() == null) {
-                        continue;
-                    }
-
-                    if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()) {
-                        LOG.debug(PortMappingUtils.PORT_NOT_RDMEXT_LOGMSG + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
-                            nodeId, port.getPortName(), circuitPackName);
+                    if (!checkPortQual(port, circuitPackName, nodeId)) {
                         continue;
                     }
 
+                    String currentKey = circuitPackName + "-" + port.getPortName();
                     if (keys.contains(currentKey)) {
                         LOG.debug(PortMappingUtils.PORT_ALREADY_HANDLED_LOGMSG + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
                             nodeId, port.getPortName(), circuitPackName);
@@ -834,17 +827,7 @@ public class PortMappingVersion121 {
                         return false;
                     }
                     Ports port = portObject.get();
-                    if (port.getPortQual() == null) {
-                        continue;
-                    }
-                    if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()) {
-                        LOG.error(PortMappingUtils.CANNOT_CREATE_LCP_LOGMSG + PortMappingUtils.PORTQUAL_ERROR_LOGMSG,
-                            nodeId, port.getPortName(), cp1Name);
-                        continue;
-                    }
-                    if (Direction.Bidirectional.getIntValue() != port.getPortDirection().getIntValue()) {
-                        LOG.error(PortMappingUtils.CANNOT_CREATE_LCP_LOGMSG + PortMappingUtils.PORTDIR_ERROR_LOGMSG,
-                            nodeId, port.getPortName(), cp1Name);
+                    if (!checkTtpPort(port, cp1Name, nodeId, true)) {
                         continue;
                     }
 
@@ -868,8 +851,7 @@ public class PortMappingVersion121 {
                     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);
+                    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);
@@ -883,18 +865,11 @@ public class PortMappingVersion121 {
                     }
 
                     Ports port1 = port1Object.get();
-                    Ports port2 = port2Object.get();
-                    if (port1.getPortQual() == null || port2.getPortQual() == null) {
+                    if (!checkTtpPort(port1, cp1Name, nodeId, false)) {
                         continue;
                     }
-                    if (Port.PortQual.RoadmExternal.getIntValue() != port1.getPortQual().getIntValue()) {
-                        LOG.error(PortMappingUtils.CANNOT_CREATE_LCP_LOGMSG + PortMappingUtils.PORTQUAL_ERROR_LOGMSG,
-                            nodeId, port1.getPortName(), cp1Name);
-                        continue;
-                    }
-                    if (Port.PortQual.RoadmExternal.getIntValue() != port2.getPortQual().getIntValue()) {
-                        LOG.error(PortMappingUtils.CANNOT_CREATE_LCP_LOGMSG + PortMappingUtils.PORTQUAL_ERROR_LOGMSG,
-                            nodeId, port2.getPortName(), cp2Name);
+                    Ports port2 = port2Object.get();
+                    if (!checkTtpPort(port2, cp2Name, nodeId, false)) {
                         continue;
                     }
 
@@ -929,6 +904,32 @@ public class PortMappingVersion121 {
         return true;
     }
 
+    private boolean checkPortQual(Ports port, String cpName, String nodeId) {
+        if (port.getPortQual() == null) {
+            return false;
+        }
+        if (Port.PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()) {
+            //used to be LOG.error when called from createTtpPortMapping
+            LOG.debug(PortMappingUtils.PORT_NOT_RDMEXT_LOGMSG + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
+                nodeId, port.getPortName(), cpName);
+            return false;
+        }
+        return true;
+    }
+
+    private boolean checkTtpPort(Ports port, String cpName, String nodeId, boolean bidirectional) {
+        if (!checkPortQual(port, cpName, nodeId)) {
+            return false;
+        }
+        if (Direction.Bidirectional.getIntValue() == port.getPortDirection().getIntValue() ^ bidirectional) {
+        // (a ^ b) makes more sense than (!a && b) here since it can also work for unidirectional links
+            LOG.error(PortMappingUtils.PORTDIR_ERROR_LOGMSG + PortMappingUtils.CANNOT_AS_LCP_LOGMSG,
+                nodeId, port.getPortName(), cpName);
+            return false;
+        }
+        return true;
+    }
+
     private NodeInfo createNodeInfo(Info deviceInfo) {
 
         if (deviceInfo.getNodeType() == null) {