PortMapping Refactoring step 1
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingVersion221.java
index f96103e5d770dc107b4ccc99ac0b5cee54e161fe..cbc4eb575c2cac4064ac84d0280952aafc10c27c 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.transportpce.common.mapping;
 
 import com.google.common.util.concurrent.FluentFuture;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -217,7 +216,7 @@ public class PortMappingVersion221 {
         Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
             LogicalDatastoreType.OPERATIONAL, deviceIID,
             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-        if (!deviceObject.isPresent()) {
+        if (deviceObject.isEmpty()) {
             LOG.error(PortMappingUtils.CANNOT_GET_DEV_CONF_LOGMSG, nodeId);
             return false;
         }
@@ -301,27 +300,27 @@ public class PortMappingVersion221 {
         if (device.getConnectionMap() == null) {
             LOG.warn(PortMappingUtils.NO_CONMAP_LOGMSG, nodeId);
         } else {
-            Collection<ConnectionMap> connectionMap = deviceObject.get().nonnullConnectionMap().values();
-            for (ConnectionMap cm : connectionMap) {
+            for (ConnectionMap cm : deviceObject.get().nonnullConnectionMap().values()) {
                 String skey = cm.getSource().getCircuitPackName() + "+" + cm.getSource().getPortName();
-                String slcp = lcpMap.containsKey(skey) ? lcpMap.get(skey) : null;
                 Destination destination0 = cm.nonnullDestination().values().iterator().next();
                 String dkey = destination0.getCircuitPackName() + "+" + destination0.getPortName();
-                if (slcp == null) {
+                if (!lcpMap.containsKey(skey)) {
                     LOG.error(PortMappingUtils.CONMAP_ISSUE_LOGMSG, nodeId, skey, dkey);
                     continue;
                 }
-                String dlcp = lcpMap.containsKey(dkey) ? lcpMap.get(dkey) : null;
+                String slcp = lcpMap.get(skey);
                 Mapping mapping = mappingMap.get(slcp);
                 mappingMap.remove(slcp);
-                portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping, dlcp, null));
+                portMapList.add(createXpdrMappingObject(nodeId, null, null, null, null, mapping,
+                        //dlcp
+                        lcpMap.containsKey(dkey) ? lcpMap.get(dkey) : null,
+                        null));
             }
         }
 
         if (device.getOduSwitchingPools() != null) {
-            Collection<OduSwitchingPools> oduSwithcingPools = device.nonnullOduSwitchingPools().values();
             List<SwitchingPoolLcp> switchingPoolList = new ArrayList<>();
-            for (OduSwitchingPools odp : oduSwithcingPools) {
+            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()) {
@@ -595,7 +594,7 @@ public class PortMappingVersion221 {
         Optional<Protocols> protocolObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
             LogicalDatastoreType.OPERATIONAL, protocoliid, Timeouts.DEVICE_READ_TIMEOUT,
             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-        if (!protocolObject.isPresent() || protocolObject.get().augmentation(Protocols1.class).getLldp() == null) {
+        if (protocolObject.isEmpty() || protocolObject.get().augmentation(Protocols1.class).getLldp() == null) {
             LOG.warn(PortMappingUtils.PROCESSING_DONE_LOGMSG, nodeId, PortMappingUtils.CANNOT_GET_LLDP_CONF_LOGMSG);
             return new HashMap<>();
         }
@@ -610,7 +609,7 @@ public class PortMappingVersion221 {
             Optional<Interface> interfaceObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
                 LogicalDatastoreType.OPERATIONAL, interfaceIID, Timeouts.DEVICE_READ_TIMEOUT,
                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-            if (!interfaceObject.isPresent() || (interfaceObject.get().getSupportingCircuitPackName() == null)) {
+            if (interfaceObject.isEmpty() || interfaceObject.get().getSupportingCircuitPackName() == null) {
                 continue;
             }
             String supportingCircuitPackName = interfaceObject.get().getSupportingCircuitPackName();
@@ -620,7 +619,7 @@ public class PortMappingVersion221 {
             Optional<CircuitPacks> circuitPackObject = this.deviceTransactionManager.getDataFromDevice(
                 nodeId, LogicalDatastoreType.OPERATIONAL, circuitPacksIID, Timeouts.DEVICE_READ_TIMEOUT,
                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-            if (!circuitPackObject.isPresent() || (circuitPackObject.get().getParentCircuitPack() == null)) {
+            if (circuitPackObject.isEmpty() || circuitPackObject.get().getParentCircuitPack() == null) {
                 continue;
             }
             cpToInterfaceMap.put(circuitPackObject.get().getParentCircuitPack().getCircuitPackName(),
@@ -1038,24 +1037,22 @@ public class PortMappingVersion221 {
         LOG.info(PortMappingUtils.MAP_LOOKS_LOGMSG, nodeId, interfaceList);
         postPortMapping(nodeId, null, null, cpToDegreeList, null, null);
 
-        Map<Integer, List<ConnectionPorts>> connectionPortMap = getPerDegreePorts(nodeId, deviceInfo);
-        for (Entry<Integer, List<ConnectionPorts>> cpMapEntry : connectionPortMap.entrySet()) {
-            switch (connectionPortMap.get(cpMapEntry.getKey()).size()) {
+        for (Entry<Integer, List<ConnectionPorts>> cpMapEntry : getPerDegreePorts(nodeId, deviceInfo).entrySet()) {
+            List<ConnectionPorts> cpMapValue = cpMapEntry.getValue();
+            ConnectionPorts cp1 = cpMapValue.get(0);
+            String cp1Name = cp1.getCircuitPackName();
+            switch (cpMapValue.size()) {
                 case 1:
                     // port is bidirectional
-                    String cpName = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
                     InstanceIdentifier<Ports> portID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
-                        .child(CircuitPacks.class, new CircuitPacksKey(cpName))
-                        .child(Ports.class,
-                                new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG,
-                        nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(), cpName);
+                        .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
+                        .child(Ports.class, new PortsKey(cp1.getPortName()));
+                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
                     Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
                         LogicalDatastoreType.OPERATIONAL, portID, Timeouts.DEVICE_READ_TIMEOUT,
                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
-                    if (!portObject.isPresent()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG,
-                            nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(), cpName);
+                    if (portObject.isEmpty()) {
+                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
                         return false;
                     }
                     Ports port = portObject.get();
@@ -1064,51 +1061,45 @@ public class PortMappingVersion221 {
                     }
                     if (PortQual.RoadmExternal.getIntValue() != port.getPortQual().getIntValue()) {
                         LOG.error(PortMappingUtils.CANNOT_CREATE_LCP_LOGMSG + PortMappingUtils.PORTQUAL_ERROR_LOGMSG,
-                            nodeId, port.getPortName(), cpName);
+                            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(), cpName);
+                            nodeId, port.getPortName(), cp1Name);
                         continue;
                     }
 
                     String logicalConnectionPoint =
                             PortMappingUtils.degreeTtpNodeName(cpMapEntry.getKey().toString(), "TXRX");
                     LOG.info(PortMappingUtils.ASSOCIATED_LCP_LOGMSG,
-                        nodeId, port.getPortName(), cpName, logicalConnectionPoint);
-                    portMapList.add(createMappingObject(nodeId, port, cpName, logicalConnectionPoint));
+                        nodeId, port.getPortName(), cp1Name, logicalConnectionPoint);
+                    portMapList.add(createMappingObject(nodeId, port, cp1Name, logicalConnectionPoint));
                     break;
                 case 2:
                     // ports are unidirectionals
-                    String cp1Name = connectionPortMap.get(cpMapEntry.getKey()).get(0).getCircuitPackName();
-                    String cp2Name = connectionPortMap.get(cpMapEntry.getKey()).get(1).getCircuitPackName();
+                    ConnectionPorts cp2 = cpMapValue.get(1);
+                    String cp2Name = cp2.getCircuitPackName();
                     InstanceIdentifier<Ports> port1ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
                         .child(CircuitPacks.class, new CircuitPacksKey(cp1Name))
-                        .child(Ports.class,
-                            new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG,
-                        nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(), cp1Name);
+                        .child(Ports.class, new PortsKey(cp1.getPortName()));
+                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
                     Optional<Ports> port1Object = this.deviceTransactionManager.getDataFromDevice(nodeId,
                         LogicalDatastoreType.OPERATIONAL, port1ID, Timeouts.DEVICE_READ_TIMEOUT,
                         Timeouts.DEVICE_READ_TIMEOUT_UNIT);
                     InstanceIdentifier<Ports> port2ID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
                         .child(CircuitPacks.class, new CircuitPacksKey(cp2Name))
-                        .child(Ports.class,
-                            new PortsKey(connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName()));
-                    LOG.debug(PortMappingUtils.FETCH_CONNECTIONPORT_LOGMSG,
-                        nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName(), cp2Name);
+                        .child(Ports.class, new PortsKey(cp2.getPortName()));
+                    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);
-                    if (!port1Object.isPresent() || !port2Object.isPresent()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG,
-                            nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(0).getPortName(), cp1Name);
+                    if (port1Object.isEmpty()) {
+                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp1.getPortName(), cp1Name);
                         return false;
                     }
-                    if (!port2Object.isPresent()) {
-                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG,
-                            nodeId, connectionPortMap.get(cpMapEntry.getKey()).get(1).getPortName(), cp2Name);
+                    if (port2Object.isEmpty()) {
+                        LOG.error(PortMappingUtils.NO_PORT_ON_CP_LOGMSG, nodeId, cp2.getPortName(), cp2Name);
                         return false;
                     }
 
@@ -1169,12 +1160,11 @@ public class PortMappingVersion221 {
 
         NodeInfoBuilder nodeInfoBldr = new NodeInfoBuilder()
                 .setOpenroadmVersion(OpenroadmNodeVersion._221)
+                .setNodeClli(
+                    deviceInfo.getClli() == null || deviceInfo.getClli().isEmpty()
+                        ? "defaultCLLI"
+                        : deviceInfo.getClli())
                 .setNodeType(NodeTypes.forValue(deviceInfo.getNodeType().getIntValue()));
-        if (deviceInfo.getClli() != null && !deviceInfo.getClli().isEmpty()) {
-            nodeInfoBldr.setNodeClli(deviceInfo.getClli());
-        } else {
-            nodeInfoBldr.setNodeClli("defaultCLLI");
-        }
         if (deviceInfo.getModel() != null) {
             nodeInfoBldr.setNodeModel(deviceInfo.getModel());
         }