Update portmapping model
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingVersion221.java
index 1f659aec3575211388d133cacf2022ff4a6e03d3..57cc2b18d8e1e1898531de010c4c4c021526a378 100644 (file)
@@ -31,29 +31,29 @@ import org.opendaylight.transportpce.common.Timeouts;
 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.Network;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.NetworkBuilder;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.OpenroadmNodeVersion;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.cp.to.degree.CpToDegree;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.cp.to.degree.CpToDegreeBuilder;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.cp.to.degree.CpToDegreeKey;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.mapping.Mapping;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.mapping.MappingBuilder;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.mapping.MappingKey;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.mc.capabilities.McCapabilities;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.mc.capabilities.McCapabilitiesBuilder;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.mc.capabilities.McCapabilitiesKey;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.network.Nodes;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.network.NodesBuilder;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.network.NodesKey;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.network.nodes.NodeInfo;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.network.nodes.NodeInfoBuilder;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.switching.pool.lcp.SwitchingPoolLcp;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.switching.pool.lcp.SwitchingPoolLcpBuilder;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.switching.pool.lcp.SwitchingPoolLcpKey;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.switching.pool.lcp.switching.pool.lcp.NonBlockingList;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.switching.pool.lcp.switching.pool.lcp.NonBlockingListBuilder;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.switching.pool.lcp.switching.pool.lcp.NonBlockingListKey;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.Network;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.NetworkBuilder;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.OpenroadmNodeVersion;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.cp.to.degree.CpToDegree;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.cp.to.degree.CpToDegreeBuilder;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.cp.to.degree.CpToDegreeKey;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mapping.Mapping;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mapping.MappingBuilder;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mapping.MappingKey;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mc.capabilities.McCapabilities;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mc.capabilities.McCapabilitiesBuilder;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mc.capabilities.McCapabilitiesKey;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.network.Nodes;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.network.NodesBuilder;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.network.NodesKey;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.network.nodes.NodeInfo;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.network.nodes.NodeInfoBuilder;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.switching.pool.lcp.SwitchingPoolLcp;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.switching.pool.lcp.SwitchingPoolLcpBuilder;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.switching.pool.lcp.SwitchingPoolLcpKey;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.switching.pool.lcp.switching.pool.lcp.NonBlockingList;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.switching.pool.lcp.switching.pool.lcp.NonBlockingListBuilder;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.switching.pool.lcp.switching.pool.lcp.NonBlockingListKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev200529.FrequencyGHz;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.Direction;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.PortQual;
@@ -214,22 +214,59 @@ public class PortMappingVersion221 {
 
     private boolean createXpdrPortMapping(String nodeId, List<Mapping> portMapList) {
         // Creating for Xponder Line and Client Ports
+        OrgOpenroadmDevice device = getXpdrDevice(nodeId);
+        if (device == null) {
+            return false;
+        }
+        Map<String, String> lcpMap = new HashMap<>();
+        Map<String, Mapping> mappingMap = new HashMap<>();
+        getXpdrMaps(device, nodeId, lcpMap, mappingMap);
+        if (device.getConnectionMap() == null) {
+            LOG.warn(PortMappingUtils.NO_CONMAP_LOGMSG, nodeId);
+        } else {
+            for (ConnectionMap cm : device.nonnullConnectionMap().values()) {
+                String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
+                Destination destination0 = cm.nonnullDestination().values().iterator().next();
+                String dkey = destination0.getCircuitPackName() + "+" + destination0.getPortName();
+                if (!lcpMap.containsKey(skey)) {
+                    LOG.error(PortMappingUtils.CONMAP_ISSUE_LOGMSG, nodeId, skey, dkey);
+                    continue;
+                }
+                String slcp = lcpMap.get(skey);
+                Mapping mapping = mappingMap.get(slcp);
+                mappingMap.remove(slcp);
+                portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping,
+                        //dlcp
+                        lcpMap.containsKey(dkey) ? lcpMap.get(dkey) : null,
+                        null));
+            }
+        }
+        if (device.getOduSwitchingPools() != null) {
+            postPortMapping(nodeId, null, null, null, getSwitchingPoolList(device, lcpMap, nodeId), null);
+        }
+        mappingMap.forEach((k,v) -> portMapList.add(v));
+        return true;
+    }
+
+    private OrgOpenroadmDevice getXpdrDevice(String nodeId) {
         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class);
         Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
             LogicalDatastoreType.OPERATIONAL, deviceIID,
             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
         if (deviceObject.isEmpty()) {
             LOG.error(PortMappingUtils.CANNOT_GET_DEV_CONF_LOGMSG, nodeId);
-            return false;
+            return null;
         }
         OrgOpenroadmDevice device = deviceObject.get();
         if (device.getCircuitPacks() == null) {
             LOG.warn(PortMappingUtils.MISSING_CP_LOGMSG, nodeId, PortMappingUtils.FOUND);
-            return false;
+            return null;
         }
+        return device;
+    }
 
-        Map<String, String> lcpMap = new HashMap<>();
-        Map<String, Mapping> mappingMap = new HashMap<>();
+    private void getXpdrMaps(OrgOpenroadmDevice device, String nodeId,
+            Map<String, String> lcpMap, Map<String, Mapping> mappingMap) {
         List<CircuitPacks> circuitPackList = new ArrayList<>(device.nonnullCircuitPacks().values());
         circuitPackList.sort(Comparator.comparing(CircuitPack::getCircuitPackName));
         if (device.getXponder() == null) {
@@ -249,7 +286,7 @@ public class PortMappingVersion221 {
                 portList.sort(Comparator.comparing(Ports::getPortName));
                 for (Ports port : portList) {
                     int[] counters = fillXpdrLcpsMaps(line, client, nodeId,
-                        1, null, circuitPackName, port,
+                        1, XpdrNodeTypes.Tpdr, circuitPackName, port,
                         circuitPackList, lcpMap, mappingMap);
                     line = counters[0];
                     client = counters[1];
@@ -257,7 +294,7 @@ public class PortMappingVersion221 {
             }
         } else {
             LOG.info(PortMappingUtils.XPDR_LIST_IN_CONF_LOGMSG, nodeId, PortMappingUtils.FOUND);
-            for (Xponder xponder : deviceObject.get().nonnullXponder().values()) {
+            for (Xponder xponder : device.nonnullXponder().values()) {
                 // Variables to keep track of number of line ports and client ports
                 int line = 1;
                 int client = 1;
@@ -265,93 +302,78 @@ public class PortMappingVersion221 {
                 XpdrNodeTypes xponderType = xponder.getXpdrType();
                 for (XpdrPort xpdrPort : xponder.nonnullXpdrPort().values().stream()
                         .sorted((xp1, xp2) -> xp1.getIndex().compareTo(xp2.getIndex())).collect(Collectors.toList())) {
-                    String circuitPackName = xpdrPort.getCircuitPackName();
-                    String portName = xpdrPort.getPortName();
-                    // If there xponder-subtree has missing circuit-packs or ports,
-                    // This gives a null-pointer expection,
-                    Optional<CircuitPacks> cpList = device.nonnullCircuitPacks().values().stream()
-                            .filter(cp -> cp.getCircuitPackName().equals(circuitPackName)).findFirst();
-                    if (cpList.isEmpty()) {
-                        LOG.warn(PortMappingUtils.MISSING_CP_LOGMSG + PortMappingUtils.PORTMAPPING_IGNORE_LOGMSG,
-                            nodeId, circuitPackName);
-                        continue;
-                    }
-                    Optional<Ports> portsList = cpList.get().nonnullPorts().values().stream()
-                            .filter(p -> p.getPortName().equals(portName)).findFirst();
-                    if (portsList.isEmpty()) {
-                        LOG.warn(PortMappingUtils.NO_ASSOC_FOUND_LOGMSG + PortMappingUtils.PORTMAPPING_IGNORE_LOGMSG,
-                            nodeId, portName, circuitPackName, "in the device");
+                    Ports port = getXpdrPorts(device, xpdrPort, nodeId);
+                    if (port == null) {
                         continue;
                     }
                     int[] counters = fillXpdrLcpsMaps(line, client, nodeId,
-                        xponderNb, xponderType, circuitPackName, portsList.get(),
+                        xponderNb, xponderType, xpdrPort.getCircuitPackName(), port,
                         circuitPackList, lcpMap, mappingMap);
                     line = counters[0];
                     client = counters[1];
                 }
             }
         }
+    }
 
-        if (device.getConnectionMap() == null) {
-            LOG.warn(PortMappingUtils.NO_CONMAP_LOGMSG, nodeId);
-        } else {
-            for (ConnectionMap cm : deviceObject.get().nonnullConnectionMap().values()) {
-                String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
-                Destination destination0 = cm.nonnullDestination().values().iterator().next();
-                String dkey = destination0.getCircuitPackName() + "+" + destination0.getPortName();
-                if (!lcpMap.containsKey(skey)) {
-                    LOG.error(PortMappingUtils.CONMAP_ISSUE_LOGMSG, nodeId, skey, dkey);
-                    continue;
-                }
-                String slcp = lcpMap.get(skey);
-                Mapping mapping = mappingMap.get(slcp);
-                mappingMap.remove(slcp);
-                portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping,
-                        //dlcp
-                        lcpMap.containsKey(dkey) ? lcpMap.get(dkey) : null,
-                        null));
-            }
+    private Ports getXpdrPorts(OrgOpenroadmDevice device, XpdrPort xpdrPort, String nodeId) {
+        String circuitPackName = xpdrPort.getCircuitPackName();
+        String portName = xpdrPort.getPortName();
+        // If there xponder-subtree has missing circuit-packs or ports,
+        // This gives a null-pointer expection,
+        Optional<CircuitPacks> cpList = device.nonnullCircuitPacks().values().stream()
+                .filter(cp -> cp.getCircuitPackName().equals(circuitPackName)).findFirst();
+        if (cpList.isEmpty()) {
+            LOG.warn(PortMappingUtils.MISSING_CP_LOGMSG + PortMappingUtils.PORTMAPPING_IGNORE_LOGMSG,
+                nodeId, circuitPackName);
+            return null;
         }
+        Optional<Ports> portsList = cpList.get().nonnullPorts().values().stream()
+                .filter(p -> p.getPortName().equals(portName)).findFirst();
+        if (portsList.isEmpty()) {
+            LOG.warn(PortMappingUtils.NO_ASSOC_FOUND_LOGMSG + PortMappingUtils.PORTMAPPING_IGNORE_LOGMSG,
+                nodeId, portName, circuitPackName, "in the device");
+            return null;
+        }
+        return portsList.get();
+    }
 
-        if (device.getOduSwitchingPools() != null) {
-            List<SwitchingPoolLcp> switchingPoolList = new ArrayList<>();
-            for (OduSwitchingPools odp : device.nonnullOduSwitchingPools().values()) {
-                Map<NonBlockingListKey,NonBlockingList> nbMap = new HashMap<>();
-                for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org
-                    .openroadm.device.odu.switching.pools.NonBlockingList nbl : odp.nonnullNonBlockingList().values()) {
-                    if (nbl.getPortList() == null) {
+    private List<SwitchingPoolLcp> getSwitchingPoolList(OrgOpenroadmDevice device,
+            Map<String, String> lcpMap, String nodeId) {
+        List<SwitchingPoolLcp> switchingPoolList = new ArrayList<>();
+        for (OduSwitchingPools odp : device.nonnullOduSwitchingPools().values()) {
+            Map<NonBlockingListKey,NonBlockingList> nbMap = new HashMap<>();
+            for (org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org
+                .openroadm.device.odu.switching.pools.NonBlockingList nbl : odp.nonnullNonBlockingList().values()) {
+                if (nbl.getPortList() == null) {
+                    continue;
+                }
+                List<String> lcpList = new ArrayList<>();
+                for (PortList item : nbl.nonnullPortList().values()) {
+                    String key = item.getCircuitPackName() + "+" + item.getPortName();
+                    if (!lcpMap.containsKey(key)) {
+                        LOG.error(PortMappingUtils.NO_ASSOC_FOUND_LOGMSG,
+                            nodeId, item.getPortName(), item.getCircuitPackName(), "to a logical connection point");
                         continue;
                     }
-                    List<String> lcpList = new ArrayList<>();
-                    for (PortList item : nbl.nonnullPortList().values()) {
-                        String key = item.getCircuitPackName() + "+" + item.getPortName();
-                        if (!lcpMap.containsKey(key)) {
-                            LOG.error(PortMappingUtils.NO_ASSOC_FOUND_LOGMSG,
-                                nodeId, item.getPortName(), item.getCircuitPackName(), "to a logical connection point");
-                            continue;
-                        }
-                        lcpList.add(lcpMap.get(key));
-                    }
-                    NonBlockingList nonBlockingList = new NonBlockingListBuilder()
-                        .setNblNumber(nbl.getNblNumber())
-                        .setInterconnectBandwidth(nbl.getInterconnectBandwidth())
-                        .setInterconnectBandwidthUnit(nbl.getInterconnectBandwidthUnit())
-                        .setLcpList(lcpList)
-                        .build();
-                    nbMap.put(nonBlockingList.key(), nonBlockingList);
+                    lcpList.add(lcpMap.get(key));
                 }
-                SwitchingPoolLcp splBldr = new SwitchingPoolLcpBuilder()
+                NonBlockingList nonBlockingList = new NonBlockingListBuilder()
+                    .setNblNumber(nbl.getNblNumber())
+                    .setInterconnectBandwidth(nbl.getInterconnectBandwidth())
+                    .setInterconnectBandwidthUnit(nbl.getInterconnectBandwidthUnit())
+                    .setLcpList(lcpList)
+                    .build();
+                nbMap.put(nonBlockingList.key(), nonBlockingList);
+            }
+            switchingPoolList.add(
+                new SwitchingPoolLcpBuilder()
                     .setSwitchingPoolNumber(odp.getSwitchingPoolNumber())
                     .setSwitchingPoolType(SwitchingPoolTypes.forValue(odp.getSwitchingPoolType().getIntValue()))
                     .setNonBlockingList(nbMap)
-                    .build();
-                switchingPoolList.add(splBldr);
-            }
-            postPortMapping(nodeId, null, null, null, switchingPoolList, null);
+                    .build());
         }
-
-        mappingMap.forEach((k,v) -> portMapList.add(v));
-        return true;
+        return switchingPoolList;
     }
 
     private boolean checkPartnerPortNotNull(Ports port) {
@@ -481,9 +503,8 @@ public class PortMappingVersion221 {
             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
+            //TODO check if we really needed to increment portIndex in this condition
+            //     if yes this block should not be in getPartnerPort and must move back to createPpPortMapping
             return null;
         }
         return port2;
@@ -941,7 +962,7 @@ public class PortMappingVersion221 {
                     PortMappingUtils.createXpdrLogicalConnectionPort(xponderNb, client, StringConstants.CLIENT_TOKEN);
                 lcpMap.put(circuitPackName + '+' + port.getPortName(), lcp0);
                 mappingMap.put(lcp0,
-                    createXpdrMappingObject(nodeId, port, circuitPackName, lcp0, null, null, null, null));
+                    createXpdrMappingObject(nodeId, port, circuitPackName, lcp0, null, null, null, xponderType));
                 client++;
                 break;