Technical debt - Fix PCE sonar issues
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / PceOtnNode.java
index 4966b9a4405263be5463ff5399fa994a08343dc5..ec1043fc123a27887a32f41bd14d97381f53fe30 100644 (file)
@@ -15,7 +15,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 import java.util.stream.Collectors;
-
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.types.rev181130.xpdr.odu.switching.pools.OduSwitchingPools;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.types.rev181130.xpdr.odu.switching.pools.odu.switching.pools.NonBlockingList;
@@ -53,6 +52,7 @@ public class PceOtnNode implements PceNode {
     private final OpenroadmNodeType nodeType;
     private final String pceNodeType;
     private final String otnServiceType;
+    private String modeType;
 
     private Map<String, List<Uint16>> tpAvailableTribPort = new TreeMap<>();
     private Map<String, List<Uint16>> tpAvailableTribSlot = new TreeMap<>();
@@ -95,19 +95,21 @@ public class PceOtnNode implements PceNode {
         LOG.info("PceOtnNode: initXndrTps for node {}", this.nodeId.getValue());
         this.availableXponderTp.clear();
 
+        this.modeType = mode;
+
         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 nodeTp
             = this.node.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
                 .ietf.network.topology.rev180226.Node1.class);
         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
-            .node.TerminationPoint> allTps = nodeTp.getTerminationPoint();
+                .node.TerminationPoint> allTps = new ArrayList<>(nodeTp.nonnullTerminationPoint().values());
         this.valid = false;
-        if (allTps == null) {
+        if (allTps.isEmpty()) {
             LOG.error("PceOtnNode: initXndrTps: XPONDER TerminationPoint list is empty for node {}", this);
             return;
         }
 
         for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network
-            .node.TerminationPoint tp : allTps) {
+                .node.TerminationPoint tp : allTps) {
             org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.@Nullable TerminationPoint1 ocnTp1
                 = tp.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130
                 .TerminationPoint1.class);
@@ -170,9 +172,9 @@ public class PceOtnNode implements PceNode {
 
         if ((this.otnServiceType.equals("ODU4") && mode.equals("AZ"))
             || ((this.otnServiceType.equals("10GE") || this.otnServiceType.equals("1GE"))
-                && mode.equals("AZ") && checkSwPool(availableXpdrClientTps, availableXpdrNWTps, 1, 1))
-            || ((this.otnServiceType.equals("10GE") || this.otnServiceType.equals("1GE"))
-                && mode.equals("intermediate") && checkSwPool(null, availableXpdrNWTps, 0, 2))) {
+                && ((mode.equals("AZ") && checkSwPool(availableXpdrClientTps, availableXpdrNWTps, 1, 1))
+                     || (mode.equals("intermediate") && checkSwPool(null, availableXpdrNWTps, 0, 2)))
+               )) {
             this.valid = true;
         } else {
             this.valid = false;
@@ -186,8 +188,8 @@ public class PceOtnNode implements PceNode {
             for (TpId nwTp : netwTps) {
                 for (TpId clTp : clientTps) {
                     @Nullable
-                    List<NonBlockingList> nblList = node.augmentation(Node1.class).getSwitchingPools()
-                        .getOduSwitchingPools().get(0).getNonBlockingList();
+                    List<NonBlockingList> nblList = new ArrayList<>(node.augmentation(Node1.class).getSwitchingPools()
+                        .nonnullOduSwitchingPools().values().stream().findFirst().get().getNonBlockingList().values());
                     for (NonBlockingList nbl : nblList) {
                         if (nbl.getTpList().contains(clTp) && nbl.getTpList().contains(nwTp)) {
                             usableXpdrClientTps.add(clTp);
@@ -205,8 +207,8 @@ public class PceOtnNode implements PceNode {
         if (clientTps == null && netwTps != null && nbClient == 0 && nbNetw == 2) {
             netwTps.sort(Comparator.comparing(TpId::getValue));
             @Nullable
-            List<NonBlockingList> nblList = node.augmentation(Node1.class).getSwitchingPools().getOduSwitchingPools()
-                .get(0).getNonBlockingList();
+            List<NonBlockingList> nblList = new ArrayList<>(node.augmentation(Node1.class).getSwitchingPools()
+                .nonnullOduSwitchingPools().values().stream().findFirst().get().getNonBlockingList().values());
             for (NonBlockingList nbl : nblList) {
                 for (TpId nwTp : netwTps) {
                     if (nbl.getTpList().contains(nwTp)) {
@@ -222,7 +224,8 @@ public class PceOtnNode implements PceNode {
     }
 
     private boolean checkTpForOdtuTermination(TerminationPoint1 ontTp1) {
-        for (SupportedInterfaceCapability sic : ontTp1.getTpSupportedInterfaces().getSupportedInterfaceCapability()) {
+        for (SupportedInterfaceCapability sic : ontTp1.getTpSupportedInterfaces().getSupportedInterfaceCapability()
+                .values()) {
             LOG.debug("in checkTpForOduTermination - sic = {}", sic.getIfCapType());
             if (sic.getIfCapType().equals(IfOCHOTU4ODU4.class)
                 && ontTp1.getXpdrTpPortConnectionAttributes().getTsPool() == null) {
@@ -236,9 +239,11 @@ public class PceOtnNode implements PceNode {
         if (ontTp1.getXpdrTpPortConnectionAttributes() != null
             && ontTp1.getXpdrTpPortConnectionAttributes().getTsPool() != null
             && ontTp1.getXpdrTpPortConnectionAttributes().getOdtuTpnPool() != null
-            && ontTp1.getXpdrTpPortConnectionAttributes().getOdtuTpnPool().get(0).getOdtuType()
+            && ontTp1.getXpdrTpPortConnectionAttributes().getOdtuTpnPool().values()
+                .stream().findFirst().get().getOdtuType()
                 .equals(ODTU4TsAllocated.class)
-            && ontTp1.getXpdrTpPortConnectionAttributes().getOdtuTpnPool().get(0).getTpnPool().isEmpty()
+            && !ontTp1.getXpdrTpPortConnectionAttributes().getOdtuTpnPool().values()
+                .stream().findFirst().get().getTpnPool().isEmpty()
             && (ontTp1.getXpdrTpPortConnectionAttributes().getTsPool().size() >= tsNb)) {
             return true;
         }
@@ -246,7 +251,8 @@ public class PceOtnNode implements PceNode {
     }
 
     private boolean checkClientTp(TerminationPoint1 ontTp1) {
-        for (SupportedInterfaceCapability sic : ontTp1.getTpSupportedInterfaces().getSupportedInterfaceCapability()) {
+        for (SupportedInterfaceCapability sic : ontTp1.getTpSupportedInterfaces().getSupportedInterfaceCapability()
+                .values()) {
             LOG.debug("in checkTpForOduTermination - sic = {}", sic.getIfCapType());
             switch (otnServiceType) {
                 case "1GE":
@@ -272,67 +278,6 @@ public class PceOtnNode implements PceNode {
         return false;
     }
 
-    private Boolean findClientCompliantInterface(List<SupportedInterfaceCapability> sic) {
-        boolean compliant = false;
-        for (SupportedInterfaceCapability sit : sic) {
-            String interfacetype = sit.getIfCapType().getName();
-            switch (interfacetype) {
-                case "If1GEODU0":
-                case "If1GE":
-                    if ("1GE".equals(this.otnServiceType)) {
-                        compliant = true;
-                    }
-                    break;
-                case "If10GEODU2e":
-                case "If10GE":
-                    if ("10GE".equals(this.otnServiceType)) {
-                        compliant = true;
-                    }
-                    break;
-                case "If100GEODU4":
-                case "If100GE":
-                    if ("100GE".equals(this.otnServiceType)) {
-                        compliant = true;
-                    }
-                    break;
-                case "IfOTU4ODU4":
-                case "IfOCHOTU4ODU4":
-                    if ("OTU4".equals(this.otnServiceType) || "ODU4".equals(this.otnServiceType)) {
-                        compliant = true;
-                    }
-                    break;
-                default:
-                    compliant = false;
-                    break;
-            }
-        }
-        return compliant;
-    }
-
-    private Boolean findNetworkCompliantInterface(List<SupportedInterfaceCapability> sic) {
-        boolean compliant = false;
-        for (SupportedInterfaceCapability sit : sic) {
-            String interfacetype = sit.getIfCapType().toString();
-            switch (interfacetype) {
-                case "IfOTU4ODU4":
-                case "IfOCHOTU4ODU4":
-                    compliant = true;
-                    break;
-                case "IfOTU2ODU2":
-                case "IfOCHOTU2ODU2":
-                    if (("1GE".equals(this.otnServiceType)) || ("10GE".equals(this.otnServiceType))) {
-                        compliant = true;
-                    }
-                    break;
-                // add all use case with higher rate interfaces when it shows up
-                default:
-                    compliant = false;
-                    break;
-            }
-        }
-        return compliant;
-    }
-
     public void validateXponder(String anodeId, String znodeId) {
         if (!isValid()) {
             return;
@@ -350,20 +295,20 @@ public class PceOtnNode implements PceNode {
 
     public boolean validateSwitchingPoolBandwidth(
             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
-                .ietf.network.topology.rev180226.networks.network.node.TerminationPoint tp1,
+                    .ietf.network.topology.rev180226.networks.network.node.TerminationPoint tp1,
             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
-                .ietf.network.topology.rev180226.networks.network.node.TerminationPoint tp2,
+                    .ietf.network.topology.rev180226.networks.network.node.TerminationPoint tp2,
             Long neededBW) {
         if (this.nodeType != OpenroadmNodeType.TPDR) {
             return true;
         }
         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.Node1 node1 =
             node.augmentation(
-                org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.Node1.class);
+                    org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.Node1.class);
         SwitchingPools sp = node1.getSwitchingPools();
-        List<OduSwitchingPools> osp = sp.getOduSwitchingPools();
+        List<OduSwitchingPools> osp = new ArrayList<>(sp.nonnullOduSwitchingPools().values());
         for (OduSwitchingPools ospx : osp) {
-            List<NonBlockingList> nbl = ospx.getNonBlockingList();
+            List<NonBlockingList> nbl = new ArrayList<>(ospx.nonnullNonBlockingList().values());
             for (NonBlockingList nbll : nbl) {
                 if (nbll.getAvailableInterconnectBandwidth().toJava() >= neededBW && nbll.getTpList() != null
                         && nbll.getTpList().contains(tp1.getTpId()) && nbll.getTpList().contains(tp2.getTpId())) {
@@ -399,7 +344,7 @@ public class PceOtnNode implements PceNode {
     public void checkAvailableTribPort() {
         List<TerminationPoint> networkTpList = node.augmentation(
             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1.class)
-            .getTerminationPoint().stream()
+            .getTerminationPoint().values().stream()
             .filter(type -> type
                 .augmentation(
                     org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.TerminationPoint1.class)
@@ -408,11 +353,11 @@ public class PceOtnNode implements PceNode {
 
         for (TerminationPoint tp : networkTpList) {
             if (tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getOdtuTpnPool() != null
-                && tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getOdtuTpnPool().get(0)
-                    .getOdtuType().equals(ODTU4TsAllocated.class)) {
+                && tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getOdtuTpnPool()
+                    .values().stream().findFirst().get().getOdtuType().equals(ODTU4TsAllocated.class)) {
                 @Nullable
                 List<Uint16> tpnPool = tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes()
-                    .getOdtuTpnPool().get(0).getTpnPool();
+                    .getOdtuTpnPool().values().stream().findFirst().get().getTpnPool();
                 if (tpnPool != null) {
                     tpAvailableTribPort.put(tp.getTpId().getValue(), tpnPool);
                 }
@@ -423,7 +368,7 @@ public class PceOtnNode implements PceNode {
     public void checkAvailableTribSlot() {
         List<TerminationPoint> networkTpList = node.augmentation(
             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1.class)
-            .getTerminationPoint().stream()
+            .getTerminationPoint().values().stream()
             .filter(type -> type
                 .augmentation(
                     org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.TerminationPoint1.class)
@@ -449,6 +394,58 @@ public class PceOtnNode implements PceNode {
         return valid;
     }
 
+    public boolean isPceOtnNodeValid(final PceOtnNode pceOtnNode) {
+        if (pceOtnNode == null || pceOtnNode.node == null
+            || pceOtnNode.getNodeId() == null || pceOtnNode.nodeType == null || pceOtnNode.getSupNetworkNodeId() == null
+            || pceOtnNode.getSupClliNodeId() == null || pceOtnNode.otnServiceType == null) {
+            LOG.error(
+                "PceOtnNode: one of parameters is not populated : nodeId, node type, supporting nodeId, otnServiceType"
+            );
+            return false;
+        }
+
+        if (!isNodeTypeValid(pceOtnNode)) {
+            LOG.error("PceOtnNode node type: node type isn't one of MUXPDR or SWITCH or TPDR");
+            return false;
+        }
+
+        return isOtnServiceTypeValid(pceOtnNode);
+    }
+
+    private boolean isOtnServiceTypeValid(PceOtnNode pceOtnNode) {
+        if (pceOtnNode.modeType == null) {
+            return false;
+        }
+
+        //Todo refactor Strings (mode and otnServiceType ) to enums
+        if ((pceOtnNode.otnServiceType.equals("ODU4") && pceOtnNode.modeType.equals("AZ"))) {
+            return true;
+        }
+
+        if ((pceOtnNode.otnServiceType.equals("10GE") || pceOtnNode.otnServiceType.equals("1GE"))
+                && (isAz(pceOtnNode) || isIntermediate(pceOtnNode))) {
+            return true;
+        }
+
+        return false;
+    }
+
+    private boolean isIntermediate(PceOtnNode pceOtnNode) {
+        return pceOtnNode.modeType.equals("intermediate")
+                && checkSwPool(null, pceOtnNode.availableXpdrNWTps, 0, 2);
+    }
+
+    private boolean isAz(PceOtnNode pceOtnNode) {
+        return pceOtnNode.modeType.equals("AZ")
+                && checkSwPool(pceOtnNode.availableXpdrClientTps, pceOtnNode.availableXpdrNWTps, 1, 1);
+    }
+
+    private boolean isNodeTypeValid(final PceOtnNode pceOtnNode) {
+        return (pceOtnNode.nodeType == OpenroadmNodeType.MUXPDR)
+                || (pceOtnNode.nodeType  == OpenroadmNodeType.SWITCH)
+                || (pceOtnNode.nodeType  == OpenroadmNodeType.TPDR);
+    }
+
     @Override
     public void addOutgoingLink(PceLink outLink) {
         this.outgoingLinks.add(outLink);