Add a portmapping method to retrieve Xpdr port2
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingVersion221.java
index 3af8fc344725e875e855c9ae47de6d2e55594851..f8866dc42a3c5099b0d01bdc37e1ba240e9c9882 100644 (file)
@@ -255,7 +255,7 @@ public class PortMappingVersion221 {
                     switch (port.getPortQual()) {
 
                         case XpdrClient:
-                            String lcp0 = "XPDR1-" + StringConstants.CLIENT_TOKEN + client;
+                            String lcp0 = createXpdrLogicalConnectionPort(1, client, StringConstants.CLIENT_TOKEN);
                             lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp0);
                             mappingMap.put(lcp0,
                                 createXpdrMappingObject(nodeId, port, circuitPackName, lcp0, null, null, null, null));
@@ -265,7 +265,7 @@ public class PortMappingVersion221 {
 
                         case XpdrNetwork:
                             if (port.getPortDirection().getIntValue() == Direction.Bidirectional.getIntValue()) {
-                                String lcp = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
+                                String lcp = createXpdrLogicalConnectionPort(1, line, StringConstants.NETWORK_TOKEN);
                                 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
                                 mappingMap.put(lcp,
                                     createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null, null)
@@ -273,60 +273,20 @@ public class PortMappingVersion221 {
                                 line++;
                                 continue;
                             }
-                            // TODO PortDirection treatment here is similar to the one in createPpPortMapping.
-                            //      Some code alignment must be considered.
 
-                            if (!checkPartnerPortNotNull(port)) {
-                                LOG.warn("Error in the configuration of port {} of {} for {}",
-                                    port.getPortName(), circuitPackName, nodeId);
-                                continue;
-                            }
 
-                            if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
-                                continue;
-                            }
-                            Optional<CircuitPacks> cpOpt = circuitPackList.stream()
-                                .filter(
-                                    cP -> cP.getCircuitPackName().equals(port.getPartnerPort().getCircuitPackName()))
-                                .findFirst();
-                            if (!cpOpt.isPresent()) {
-                                LOG.error("Error fetching circuit-pack {} for {}",
-                                    port.getPartnerPort().getCircuitPackName(), nodeId);
-                                continue;
-                            }
-                            Optional<Ports> poOpt = cpOpt.get().nonnullPorts().values().stream()
-                                .filter(p -> p.getPortName().equals(port.getPartnerPort().getPortName().toString()))
-                                .findFirst();
-                            if (!poOpt.isPresent()) {
-                                LOG.error("Error fetching port {} on {} for {}", port.getPartnerPort().getPortName(),
-                                    port.getPartnerPort().getCircuitPackName(), nodeId);
-                                continue;
-                            }
-                            Ports port2 = poOpt.get();
-                            if (!checkPartnerPort(circuitPackName, port, port2)) {
-                                LOG.error("port {} on {} is not a correct partner port of {} on  {}",
-                                    port2.getPortName(), cpOpt.get().getCircuitPackName(), port.getPortName(),
-                                    circuitPackName);
-                                continue;
-                            }
-                            String lcp1 = "XPDR1-" + StringConstants.NETWORK_TOKEN + line;
-                            String lcp2 = new StringBuilder("XPDR1-")
-                                .append(StringConstants.NETWORK_TOKEN)
-                                .append(line + 1)
-                                .toString();
-                            if (lcpMap.containsKey(lcp1) || lcpMap.containsKey(lcp2)) {
-                                LOG.warn("mapping already exists for {} or {}", lcp1, lcp2);
-                                line += 2;
+                            StringBuilder circuitPackName2 = new StringBuilder();
+                            Ports port2 = getPort2(port, nodeId, circuitPackName, circuitPackName2,
+                                circuitPackList, lcpMap);
+
+                            if (port2 == null) {
+                                //key already present or an error occured and was logged
                                 continue;
                             }
-                            lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
-                            lcpMap.put(cpOpt.get().getCircuitPackName() + '+' + port2.getPortName(), lcp2);
-                            mappingMap.put(lcp1,
-                                createXpdrMappingObject(nodeId, port, circuitPackName,
-                                    lcp1, lcp2, null, null, null));
-                            mappingMap.put(lcp2,
-                                createXpdrMappingObject(nodeId, port2, cpOpt.get().getCircuitPackName(),
-                                    lcp2, lcp1, null, null, null));
+
+                            putXpdrLcpsInMaps(line, nodeId, 1, null,
+                                    circuitPackName, circuitPackName2.toString(), port, port2,
+                                    lcpMap, mappingMap);
                             line += 2;
                             break;
 
@@ -381,7 +341,8 @@ public class PortMappingVersion221 {
 
                         case XpdrClient:
                         case SwitchClient:
-                            String lcp0 = "XPDR" + xponderNb + "-" + StringConstants.CLIENT_TOKEN + client;
+                            String lcp0 =
+                                    createXpdrLogicalConnectionPort(xponderNb, client, StringConstants.CLIENT_TOKEN);
                             lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp0);
                             mappingMap.put(lcp0,
                                 createXpdrMappingObject(nodeId, port, circuitPackName, lcp0, null, null, null, null));
@@ -392,7 +353,8 @@ public class PortMappingVersion221 {
                         case XpdrNetwork:
                         case SwitchNetwork:
                             if (port.getPortDirection().getIntValue() == Direction.Bidirectional.getIntValue()) {
-                                String lcp = "XPDR" + xponderNb + "-" + StringConstants.NETWORK_TOKEN + line;
+                                String lcp =
+                                    createXpdrLogicalConnectionPort(xponderNb, line, StringConstants.NETWORK_TOKEN);
                                 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp);
                                 mappingMap.put(lcp,
                                     createXpdrMappingObject(nodeId, port, circuitPackName, lcp, null, null, null,
@@ -400,64 +362,19 @@ public class PortMappingVersion221 {
                                 line++;
                                 continue;
                             }
-                            // TODO PortDirection treatment here is similar to the one in createPpPortMapping.
-                            //      Some code alignment must be considered.
 
-                            if (!checkPartnerPortNotNull(port)) {
-                                LOG.warn("Error in the configuration of port {} of {} for {}",
-                                    port.getPortName(), circuitPackName, nodeId);
-                                continue;
-                            }
+                            StringBuilder circuitPackName2 = new StringBuilder();
+                            Ports port2 = getPort2(port, nodeId, circuitPackName, circuitPackName2,
+                                circuitPackList, lcpMap);
 
-                            if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
+                            if (port2 == null) {
+                                //key already present or an error occured and was logged
                                 continue;
                             }
 
-                            Optional<CircuitPacks> cpOpt = circuitPackList.stream()
-                                .filter(
-                                    cP -> cP.getCircuitPackName().equals(port.getPartnerPort().getCircuitPackName()))
-                                .findFirst();
-                            if (!cpOpt.isPresent()) {
-                                LOG.error("Error fetching circuit-pack {} for {}",
-                                    port.getPartnerPort().getCircuitPackName(), nodeId);
-                                continue;
-                            }
-
-                            Optional<Ports> poOpt = cpOpt.get().nonnullPorts().values().stream()
-                                .filter(p -> p.getPortName().equals(port.getPartnerPort().getPortName().toString()))
-                                .findFirst();
-                            if (!poOpt.isPresent()) {
-                                LOG.error("Error fetching port {} on {} for {}", port.getPartnerPort().getPortName(),
-                                    port.getPartnerPort().getCircuitPackName(), nodeId);
-                                continue;
-                            }
-                            Ports port2 = poOpt.get();
-                            if (!checkPartnerPort(circuitPackName, port, port2)) {
-                                LOG.error("port {} on {} is not a correct partner port of {} on  {}",
-                                    port2.getPortName(), cpOpt.get().getCircuitPackName(), port.getPortName(),
-                                    circuitPackName);
-                                continue;
-                            }
-                            String lcp1 = "XPDR" + xponderNb + "-" + StringConstants.NETWORK_TOKEN + line;
-                            String lcp2 = new StringBuilder("XPDR")
-                                .append(xponderNb)
-                                .append("-")
-                                .append(StringConstants.NETWORK_TOKEN)
-                                .append(line + 1)
-                                .toString();
-                            if (lcpMap.containsKey(lcp1) || lcpMap.containsKey(lcp2)) {
-                                LOG.warn("mapping already exists for {} or {}", lcp1, lcp2);
-                                line += 2;
-                                continue;
-                            }
-                            lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
-                            lcpMap.put(cpOpt.get().getCircuitPackName() + '+' + port2.getPortName(), lcp2);
-                            mappingMap.put(lcp1,
-                                createXpdrMappingObject(nodeId, port, circuitPackName,
-                                    lcp1, lcp2, null, null, xponderType));
-                            mappingMap.put(lcp2,
-                                createXpdrMappingObject(nodeId, port2, cpOpt.get().getCircuitPackName(),
-                                    lcp2, lcp1, null, null, xponderType));
+                            putXpdrLcpsInMaps(line, nodeId, xponderNb, xponderType,
+                                    circuitPackName, circuitPackName2.toString(), port, port2,
+                                    lcpMap, mappingMap);
                             line += 2;
                             break;
 
@@ -710,6 +627,14 @@ public class PortMappingVersion221 {
         return null;
     }
 
+    private String createXpdrLogicalConnectionPort(int xponderNb, int lcpNb, String token) {
+        return new StringBuilder("XPDR").append(xponderNb)
+                .append("-")
+                .append(token).append(lcpNb)
+                .toString();
+    }
+
+
     private Map<Integer, Degree> getDegreesMap(String deviceId, Info ordmInfo) {
         Map<Integer, Degree> degrees = new HashMap<>();
 
@@ -1019,6 +944,65 @@ public class PortMappingVersion221 {
         return mpBldr.build();
     }
 
+    private Ports getPort2(Ports port, String nodeId, String circuitPackName, StringBuilder circuitPackName2,
+            //circuitPackName2 will be updated by reference contrary to circuitPackName
+            List<CircuitPacks> circuitPackList, Map<String, String> lcpMap) {
+        if (!checkPartnerPortNotNull(port)) {
+            LOG.warn("Error in the configuration of port {} of {} for {}",
+                port.getPortName(), circuitPackName, nodeId);
+            return null;
+        }
+        if (lcpMap.containsKey(circuitPackName + '+' + port.getPortName())) {
+            return null;
+        }
+        Optional<CircuitPacks> cpOpt = circuitPackList.stream()
+            .filter(
+                cP -> cP.getCircuitPackName().equals(port.getPartnerPort().getCircuitPackName()))
+            .findFirst();
+        if (!cpOpt.isPresent()) {
+            LOG.error("Error fetching circuit-pack {} for {}",
+                port.getPartnerPort().getCircuitPackName(), nodeId);
+            return null;
+        }
+        Optional<Ports> poOpt = cpOpt.get().nonnullPorts().values().stream()
+            .filter(p -> p.getPortName().equals(port.getPartnerPort().getPortName().toString()))
+            .findFirst();
+        if (!poOpt.isPresent()) {
+            LOG.error("Error fetching port {} on {} for {}",
+                port.getPartnerPort().getPortName(), port.getPartnerPort().getCircuitPackName(), nodeId);
+            return null;
+        }
+        Ports port2 = poOpt.get();
+        circuitPackName2.append(cpOpt.get().getCircuitPackName());
+        if (!checkPartnerPort(circuitPackName, port, port2)) {
+            LOG.error("port {} on {} is not a correct partner port of {} on  {}",
+                port2.getPortName(), circuitPackName2, port.getPortName(), circuitPackName);
+            return null;
+        }
+        return port2;
+    }
+
+
+    private void putXpdrLcpsInMaps(int line, String nodeId,
+            Integer xponderNb, XpdrNodeTypes xponderType,
+            String circuitPackName, String circuitPackName2, Ports port, Ports port2,
+            Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
+        String lcp1 = createXpdrLogicalConnectionPort(xponderNb, line, StringConstants.NETWORK_TOKEN);
+        String lcp2 = createXpdrLogicalConnectionPort(xponderNb, line + 1, StringConstants.NETWORK_TOKEN);
+        if (lcpMap.containsKey(lcp1) || lcpMap.containsKey(lcp2)) {
+            LOG.warn("mapping already exists for {} or {}", lcp1, lcp2);
+            return;
+        }
+        lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp1);
+        lcpMap.put(circuitPackName2 + '+' + port2.getPortName(), lcp2);
+        mappingMap.put(lcp1,
+                createXpdrMappingObject(nodeId, port, circuitPackName, lcp1, lcp2, null, null, xponderType));
+        mappingMap.put(lcp2,
+                createXpdrMappingObject(nodeId, port2, circuitPackName2, lcp2, lcp1, null, null, xponderType));
+        return;
+    }
+
+
     private boolean createMcCapabilitiesList(String nodeId, Info deviceInfo, List<McCapabilities> mcCapabilitiesList) {
         Map<Integer, Degree> degrees = getDegreesMap(nodeId, deviceInfo);
         List<SharedRiskGroup> srgs = getSrgs(nodeId, deviceInfo);