Add portmapping methods to fill XpdR LCPs maps
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingVersion221.java
index 3f404ebc004bb52076f22038f51a5f969e7e8b27..cf144749fb56e60eed5a5fc0855946c66e7d480e 100644 (file)
@@ -123,7 +123,7 @@ public class PortMappingVersion221 {
     public boolean createMappingData(String nodeId) {
         LOG.info("{} : OpenROADM version 2.2.1 node - Creating Mapping Data", nodeId);
         List<Mapping> portMapList = new ArrayList<>();
-        List<McCapabilities> mcCapabilitiesList = new ArrayList<>();
+        Map<McCapabilitiesKey, McCapabilities> mcCapabilities = new HashMap<>();
         InstanceIdentifier<Info> infoIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Info.class);
         Optional<Info> deviceInfoOptional = this.deviceTransactionManager.getDataFromDevice(
                 nodeId, LogicalDatastoreType.OPERATIONAL, infoIID,
@@ -156,7 +156,7 @@ public class PortMappingVersion221 {
                     return false;
                 }
                 // Get MC capabilities
-                if (!createMcCapabilitiesList(nodeId, deviceInfo, mcCapabilitiesList)) {
+                if (!createMcCapabilitiesList(nodeId, deviceInfo, mcCapabilities)) {
                     // return false if MC capabilites failed
                     LOG.warn("{} : Unable to create MC capabilities", nodeId);
                     return false;
@@ -173,7 +173,7 @@ public class PortMappingVersion221 {
                 break;
 
         }
-        return postPortMapping(nodeId, nodeInfo, portMapList, null, null, mcCapabilitiesList);
+        return postPortMapping(nodeId, nodeInfo, portMapList, null, null, mcCapabilities);
     }
 
     public boolean updateMapping(String nodeId, Mapping oldMapping) {
@@ -247,66 +247,11 @@ public class PortMappingVersion221 {
                 List<Ports> portList = new ArrayList<>(cp.nonnullPorts().values());
                 portList.sort(Comparator.comparing(Ports::getPortName));
                 for (Ports port : portList) {
-                    if (port.getPortQual() == null) {
-                        LOG.warn("{} : port {} on {} - PortQual was not found",
-                                nodeId, port.getPortName(), circuitPackName);
-                        continue;
-                    }
-
-                    switch (port.getPortQual()) {
-
-                        case XpdrClient:
-                        case SwitchClient:
-                            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));
-                            client++;
-                            //continue;
-                            break;
-
-                        case XpdrNetwork:
-                        case SwitchNetwork:
-                            switch (port.getPortDirection()) {
-
-                                case Bidirectional:
-                                    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)
-                                    );
-                                    line++;
-                                    break;
-
-                                case Rx:
-                                case Tx:
-                                    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;
-                                    }
-
-                                    putXpdrLcpsInMaps(line, nodeId, 1, null,
-                                            circuitPackName, circuitPackName2.toString(), port, port2,
-                                            lcpMap, mappingMap);
-                                    line += 2;
-                                    break;
-
-                                default:
-                                    LOG.error("{} : port {} on {} - unsupported Direction {}",
-                                        nodeId, port.getPortName(), circuitPackName, port.getPortDirection());
-                            }
-                            break;
-
-                        default:
-                            LOG.error("{} : port {} on {} - unsupported PortQual {}",
-                                nodeId, port.getPortName(), circuitPackName, port.getPortQual());
-                    }
+                    int[] counters = fillXpdrLcpsMaps(line, client, nodeId,
+                        1, null, circuitPackName, port,
+                        circuitPackList, lcpMap, mappingMap);
+                    line = counters[0];
+                    client = counters[1];
                 }
             }
         } else {
@@ -343,66 +288,11 @@ public class PortMappingVersion221 {
                             .findFirst().get().nonnullPorts().values().stream()
                             .filter(p -> p.getPortName().equals(portName))
                             .findFirst().get();
-                    if (port.getPortQual() == null) {
-                        LOG.warn("{} : port {} on {} - PortQual was not found",
-                                nodeId, port.getPortName(), circuitPackName);
-                        continue;
-                    }
-
-                    switch (port.getPortQual()) {
-
-                        case XpdrClient:
-                        case SwitchClient:
-                            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));
-                            client++;
-                            //continue;
-                            break;
-
-                        case XpdrNetwork:
-                        case SwitchNetwork:
-                            switch (port.getPortDirection()) {
-
-                                case Bidirectional:
-                                    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,
-                                            xponderType));
-                                    line++;
-                                    break;
-
-                                case Rx:
-                                case Tx:
-                                    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;
-                                    }
-
-                                    putXpdrLcpsInMaps(line, nodeId, xponderNb, xponderType,
-                                            circuitPackName, circuitPackName2.toString(), port, port2,
-                                            lcpMap, mappingMap);
-                                    line += 2;
-                                    break;
-
-                                default:
-                                    LOG.error("{} : port {} on {} - unsupported Direction {}",
-                                        nodeId, port.getPortName(), circuitPackName, port.getPortDirection());
-                            }
-                            break;
-
-                        default:
-                            LOG.error("{} : port {} on {} - unsupported PortQual {}",
-                                nodeId, port.getPortName(), circuitPackName, port.getPortQual());
-                    }
+                    int[] counters = fillXpdrLcpsMaps(line, client, nodeId,
+                        xponderNb, xponderType, circuitPackName, port,
+                        circuitPackList, lcpMap, mappingMap);
+                    line = counters[0];
+                    client = counters[1];
                 }
             }
         }
@@ -758,20 +648,22 @@ public class PortMappingVersion221 {
         return cpToDegreeList;
     }
 
-    private List<McCapabilities> getMcCapabilitiesList(Map<Integer, Degree> degrees, List<SharedRiskGroup> srgs,
-            String nodeId) {
+    private Map<McCapabilitiesKey, McCapabilities> getMcCapabilities(Map<Integer, Degree> degrees,
+            List<SharedRiskGroup> srgs, String nodeId) {
         LOG.info("{} : Getting the MC capabilities for degrees", nodeId);
-        List<McCapabilities> mcCapabilitiesList = degrees.values().stream()
-            .map(degree -> createMcCapDegreeObject(degree, nodeId)).collect(Collectors.toList());
+        Map<McCapabilitiesKey, McCapabilities> mcCapabilities = degrees.values().stream()
+            .map(degree -> createMcCapDegreeObject(degree, nodeId))
+            .collect(Collectors.toMap(McCapabilities::key, mcc -> mcc));
         // Add the SRG mc-capabilities
         LOG.info("{} : Getting the MC capabilities for SRGs", nodeId);
-        mcCapabilitiesList.addAll(srgs.stream().map(srg -> createMcCapSrgObject(srg, nodeId))
-            .collect(Collectors.toList()));
-        return mcCapabilitiesList;
+        mcCapabilities.putAll(srgs.stream().map(srg -> createMcCapSrgObject(srg, nodeId))
+            .collect(Collectors.toMap(McCapabilities::key, mcCapabilities2 -> mcCapabilities2)));
+        return mcCapabilities;
     }
 
     private boolean postPortMapping(String nodeId, NodeInfo nodeInfo, List<Mapping> portMapList,
-            List<CpToDegree> cp2DegreeList, List<SwitchingPoolLcp> splList, List<McCapabilities> mcCapList) {
+            List<CpToDegree> cp2DegreeList, List<SwitchingPoolLcp> splList,
+            Map<McCapabilitiesKey, McCapabilities> mcCapabilities) {
         NodesBuilder nodesBldr = new NodesBuilder().withKey(new NodesKey(nodeId)).setNodeId(nodeId);
         if (nodeInfo != null) {
             nodesBldr.setNodeInfo(nodeInfo);
@@ -801,8 +693,8 @@ public class PortMappingVersion221 {
             }
             nodesBldr.setSwitchingPoolLcp(splMap);
         }
-        if (mcCapList != null) {
-            nodesBldr.setMcCapabilities(mcCapList);
+        if (mcCapabilities != null) {
+            nodesBldr.setMcCapabilities(mcCapabilities);
         }
         Map<NodesKey,Nodes> nodesList = new HashMap<>();
         Nodes nodes = nodesBldr.build();
@@ -1021,11 +913,87 @@ public class PortMappingVersion221 {
         return;
     }
 
+    private int[] fillXpdrLcpsMaps(int line, int client, String nodeId,
+            Integer xponderNb, XpdrNodeTypes xponderType,
+            String circuitPackName,  Ports port,
+            List<CircuitPacks> circuitPackList, Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
+        if (port.getPortQual() == null) {
+            LOG.warn("{} : port {} on {} - PortQual was not found",
+                    nodeId, port.getPortName(), circuitPackName);
+            return new int[] {line, client};
+        }
+
+        switch (port.getPortQual()) {
+
+            case XpdrClient:
+            case SwitchClient:
+                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));
+                client++;
+                break;
+
+            case XpdrNetwork:
+            case SwitchNetwork:
+                line = fillXpdrNetworkLcpsMaps(line, nodeId,
+                        xponderNb, xponderType,
+                        circuitPackName,  port,
+                        circuitPackList,  lcpMap, mappingMap);
+                break;
+
+            default:
+                LOG.error("{} : port {} on {} - unsupported PortQual {}",
+                        nodeId, port.getPortName(), circuitPackName, port.getPortQual());
+        }
+        return new int[] {line, client};
+    }
+
+    private int fillXpdrNetworkLcpsMaps(int line, String nodeId,
+            Integer xponderNb, XpdrNodeTypes xponderType,
+            String circuitPackName,  Ports port,
+            List<CircuitPacks> circuitPackList, Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
+
+        switch (port.getPortDirection()) {
+
+            case Bidirectional:
+                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, xponderType));
+                line++;
+                break;
+
+            case Rx:
+            case Tx:
+                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
+                    return line;
+                }
+
+                putXpdrLcpsInMaps(line, nodeId, xponderNb, xponderType,
+                        circuitPackName, circuitPackName2.toString(), port, port2,
+                        lcpMap, mappingMap);
+                line += 2;
+                break;
+
+            default:
+                LOG.error("{} : port {} on {} - unsupported Direction {}",
+                     nodeId, port.getPortName(), circuitPackName, port.getPortDirection());
+        }
+
+        return line;
+    }
 
-    private boolean createMcCapabilitiesList(String nodeId, Info deviceInfo, List<McCapabilities> mcCapabilitiesList) {
+    private boolean createMcCapabilitiesList(String nodeId, Info deviceInfo,
+            Map<McCapabilitiesKey, McCapabilities> mcCapabilitiesMap) {
         Map<Integer, Degree> degrees = getDegreesMap(nodeId, deviceInfo);
         List<SharedRiskGroup> srgs = getSrgs(nodeId, deviceInfo);
-        mcCapabilitiesList.addAll(getMcCapabilitiesList(degrees, srgs, nodeId));
+        mcCapabilitiesMap.putAll(getMcCapabilities(degrees, srgs, nodeId));
         return true;
     }