Technical debt - Fix PCE sonar issues
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / PceOtnNode.java
index 65f17b78e97bc6c63e157c258e4a006aa8aedd25..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;
@@ -34,12 +33,13 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.TpId;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.node.TerminationPoint;
+import org.opendaylight.yangtools.yang.common.Uint16;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class PceOtnNode implements PceNode {
     /* Logging. */
-    private static final Logger LOG = LoggerFactory.getLogger(PceCalculation.class);
+    private static final Logger LOG = LoggerFactory.getLogger(PceOtnNode.class);
     ////////////////////////// OTN NODES ///////////////////////////
     /*
      * For This Class the node passed shall be at the otn-openroadm Layer
@@ -52,19 +52,20 @@ public class PceOtnNode implements PceNode {
     private final OpenroadmNodeType nodeType;
     private final String pceNodeType;
     private final String otnServiceType;
+    private String modeType;
 
-    private Map<String, List<Integer>> tpAvailableTribPort = new TreeMap<String, List<Integer>>();
-    private Map<String, List<Integer>> tpAvailableTribSlot = new TreeMap<String, List<Integer>>();
-    private Map<String, OpenroadmTpType> availableXponderTp = new TreeMap<String, OpenroadmTpType>();
-    private List<String> usedXpdrNWTps = new ArrayList<String>();
+    private Map<String, List<Uint16>> tpAvailableTribPort = new TreeMap<>();
+    private Map<String, List<Uint16>> tpAvailableTribSlot = new TreeMap<>();
+    private Map<String, OpenroadmTpType> availableXponderTp = new TreeMap<>();
+    private List<String> usedXpdrNWTps = new ArrayList<>();
     private List<TpId> availableXpdrNWTps;
     private List<TpId> usableXpdrNWTps;
-    private List<String> usedXpdrClientTps = new ArrayList<String>();
+    private List<String> usedXpdrClientTps = new ArrayList<>();
     private List<TpId> availableXpdrClientTps;
     private List<TpId> usableXpdrClientTps;
 
-    private List<PceLink> outgoingLinks = new ArrayList<PceLink>();
-    private Map<String, String> clientPerNwTp = new HashMap<String, String>();
+    private List<PceLink> outgoingLinks = new ArrayList<>();
+    private Map<String, String> clientPerNwTp = new HashMap<>();
 
     public PceOtnNode(Node node, OpenroadmNodeType nodeType, NodeId nodeId, String pceNodeType, String serviceType) {
         this.node = node;
@@ -74,11 +75,11 @@ public class PceOtnNode implements PceNode {
         this.otnServiceType = serviceType;
         this.tpAvailableTribSlot.clear();
         this.usedXpdrNWTps.clear();
-        this.availableXpdrNWTps = new ArrayList<TpId>();
-        this.usableXpdrNWTps = new ArrayList<TpId>();
+        this.availableXpdrNWTps = new ArrayList<>();
+        this.usableXpdrNWTps = new ArrayList<>();
         this.usedXpdrClientTps.clear();
-        this.availableXpdrClientTps = new ArrayList<TpId>();
-        this.usableXpdrClientTps = new ArrayList<TpId>();
+        this.availableXpdrClientTps = new ArrayList<>();
+        this.usableXpdrClientTps = new ArrayList<>();
         this.tpAvailableTribPort.clear();
         checkAvailableTribPort();
         this.tpAvailableTribSlot.clear();
@@ -94,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) {
-            LOG.error("PceOtnNode: initXndrTps: XPONDER TerminationPoint list is empty for node {}", this.toString());
+        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);
@@ -169,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;
@@ -185,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);
@@ -204,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)) {
@@ -221,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) {
@@ -235,18 +239,20 @@ 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().size() >= 1
-            && ontTp1.getXpdrTpPortConnectionAttributes().getTsPool().size() >= tsNb) {
+            && !ontTp1.getXpdrTpPortConnectionAttributes().getOdtuTpnPool().values()
+                .stream().findFirst().get().getTpnPool().isEmpty()
+            && (ontTp1.getXpdrTpPortConnectionAttributes().getTsPool().size() >= tsNb)) {
             return true;
-        } else {
-            return false;
         }
+        return false;
     }
 
     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,36 +295,34 @@ 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;
-        } else {
-            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 node1 =
+            node.augmentation(
                     org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev181130.Node1.class);
-            SwitchingPools sp = node1.getSwitchingPools();
-            List<OduSwitchingPools> osp = new ArrayList<OduSwitchingPools>();
-            osp = sp.getOduSwitchingPools();
-            for (OduSwitchingPools ospx : osp) {
-                List<NonBlockingList> nbl = ospx.getNonBlockingList();
-                for (NonBlockingList nbll : nbl) {
-                    if (nbll.getAvailableInterconnectBandwidth() >= neededBW) {
-                        List<TpId> tplist = new ArrayList<TpId>(nbll.getTpList());
-                        if ((tplist.contains(tp1.getTpId())) & (tplist.contains(tp2.getTpId()))) {
-                            LOG.debug("validateSwitchingPoolBandwidth: couple  of tp {} x {} valid for crossconnection",
-                                tp1.getTpId().toString(), tp2.getTpId().toString());
-                            return true;
-                        }
-                    }
+        SwitchingPools sp = node1.getSwitchingPools();
+        List<OduSwitchingPools> osp = new ArrayList<>(sp.nonnullOduSwitchingPools().values());
+        for (OduSwitchingPools ospx : osp) {
+            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())) {
+                    LOG.debug("validateSwitchingPoolBandwidth: couple  of tp {} x {} valid for crossconnection",
+                        tp1.getTpId(), tp2.getTpId());
+                    return true;
                 }
             }
-            LOG.debug("validateSwitchingPoolBandwidth: No valid Switching pool for crossconnecting tp {} and {}",
-                tp1.getTpId().toString(), tp2.getTpId().toString());
-            return false;
         }
+
+        LOG.debug("validateSwitchingPoolBandwidth: No valid Switching pool for crossconnecting tp {} and {}",
+            tp1.getTpId(), tp2.getTpId());
+        return false;
+
     }
 
     public void validateIntermediateSwitch() {
@@ -396,13 +339,12 @@ public class PceOtnNode implements PceNode {
         } else {
             LOG.info("validateIntermediateSwitch: Switch usable for transit == {}", nodeId.getValue());
         }
-        return;
     }
 
     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)
@@ -411,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<Integer> tpnPool = tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes()
-                    .getOdtuTpnPool().get(0).getTpnPool();
+                List<Uint16> tpnPool = tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes()
+                    .getOdtuTpnPool().values().stream().findFirst().get().getTpnPool();
                 if (tpnPool != null) {
                     tpAvailableTribPort.put(tp.getTpId().getValue(), tpnPool);
                 }
@@ -426,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)
@@ -436,7 +378,7 @@ public class PceOtnNode implements PceNode {
         for (TerminationPoint tp : networkTpList) {
             if (tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getTsPool() != null) {
                 @Nullable
-                List<Integer> tsPool = tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes()
+                List<Uint16> tsPool = tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes()
                     .getTsPool();
                 tpAvailableTribSlot.put(tp.getTpId().getValue(), tsPool);
             }
@@ -452,10 +394,64 @@ 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);
     }
 
+    @Override
     public List<PceLink> getOutgoingLinks() {
         return outgoingLinks;
     }
@@ -465,21 +461,22 @@ public class PceOtnNode implements PceNode {
         return this.clientPerNwTp.get(tp);
     }
 
+    @Override
     public String toString() {
         return "PceNode type=" + nodeType + " ID=" + nodeId.getValue() + " CLLI=" + this.getSupClliNodeId();
     }
 
     public void printLinksOfNode() {
-        LOG.info(" outgoing links of node {} : {} ", nodeId.getValue(), this.getOutgoingLinks().toString());
+        LOG.info(" outgoing links of node {} : {} ", nodeId.getValue(), this.getOutgoingLinks());
     }
 
     @Override
-    public Map<String, List<Integer>> getAvailableTribPorts() {
+    public Map<String, List<Uint16>> getAvailableTribPorts() {
         return tpAvailableTribPort;
     }
 
     @Override
-    public Map<String, List<Integer>> getAvailableTribSlots() {
+    public Map<String, List<Uint16>> getAvailableTribSlots() {
         return tpAvailableTribSlot;
     }
 
@@ -508,7 +505,6 @@ public class PceOtnNode implements PceNode {
 
     @Override
     public String getRdmSrgClient(String tp) {
-        // TODO Auto-generated method stub
         return null;
     }
 
@@ -519,13 +515,11 @@ public class PceOtnNode implements PceNode {
 
     @Override
     public boolean checkTP(String tp) {
-        // TODO Auto-generated method stub
         return false;
     }
 
     @Override
     public boolean checkWL(long index) {
-        // TODO Auto-generated method stub
         return false;
     }
 }