Light refactor on PceOpticalNode
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / PceCalculation.java
index d8c2caf77382240b6634e6c31a74ba03dfcdec52..bfd2822a14cda27c001694ad12a648f088966dbb 100644 (file)
@@ -11,23 +11,21 @@ package org.opendaylight.transportpce.pce.networkanalyzer;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
-
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.NetworkUtils;
 import org.opendaylight.transportpce.common.ResponseCodes;
 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.Node1;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmLinkType;
-import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev181130.OpenroadmNodeType;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.Node1;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmLinkType;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmNodeType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
@@ -59,9 +57,9 @@ public class PceCalculation {
     private PceConstraints pceHardConstraints;
 
     ///////////// Intermediate data/////////////////
-    private List<PceLink> addLinks = new ArrayList<PceLink>();
-    private List<PceLink> dropLinks = new ArrayList<PceLink>();
-    private HashSet<NodeId> azSrgs = new HashSet<NodeId>();
+    private List<PceLink> addLinks = new ArrayList<>();
+    private List<PceLink> dropLinks = new ArrayList<>();
+    private HashSet<NodeId> azSrgs = new HashSet<>();
 
     private PceNode aendPceNode = null;
     private PceNode zendPceNode = null;
@@ -70,11 +68,11 @@ public class PceCalculation {
     private List<Node> allNodes = null;
 
     // this List serves graph calculation
-    private Map<NodeId, PceNode> allPceNodes = new HashMap<NodeId, PceNode>();
+    private Map<NodeId, PceNode> allPceNodes = new HashMap<>();
     // this List serves calculation of ZtoA path description
     // TODO maybe better solution is possible
-    private Map<LinkId, PceLink> allPceLinks = new HashMap<LinkId, PceLink>();
-    private Set<LinkId> linksToExclude = new HashSet<LinkId>();
+    private Map<LinkId, PceLink> allPceLinks = new HashMap<>();
+    private Set<LinkId> linksToExclude = new HashSet<>();
     private PceResult returnStructure;
 
     private enum ConstraintTypes {
@@ -112,8 +110,6 @@ public class PceCalculation {
     }
 
     private boolean parseInput() {
-        anodeId = input.getServiceAEnd().getNodeId();
-        znodeId = input.getServiceZEnd().getNodeId();
         if (input.getServiceAEnd().getServiceFormat() == null || input.getServiceZEnd().getServiceFormat() == null
             || input.getServiceAEnd().getServiceRate() == null) {
             LOG.error("Service Format and Service Rate are required for a path calculation");
@@ -147,18 +143,25 @@ public class PceCalculation {
             //And casting to int bumps the limit here.
             //Passing by ENUM or String are possible alternatives.
             //Maybe HashMap and similar options should also be considered here.
-        } else if (serviceFormatA == "Ethernet") {
+        } else if ("Ethernet".equals(serviceFormatA)) {
         //only rate 100L is currently supported except in Ethernet
             if (serviceRate == 10L) {
                 serviceType = "10GE";
             } else if (serviceRate == 1L) {
                 serviceType = "1GE";
             } else {
-                LOG.debug("parseInput: unsupported service type: Format Ethernet Rate {}", String.valueOf(serviceRate));
+                LOG.debug("parseInput: unsupported service type: Format Ethernet Rate {}", serviceRate);
             }
         } else {
             LOG.debug("parseInput: unsupported service type: Format {} Rate {}",
-                serviceFormatA, String.valueOf(serviceRate));
+                serviceFormatA, serviceRate);
+        }
+        if ("ODU4".equals(serviceType) || "10GE".equals(serviceType)  || "1GE".equals(serviceType)) {
+            anodeId = input.getServiceAEnd().getTxDirection().getPort().getPortDeviceName();
+            znodeId = input.getServiceZEnd().getTxDirection().getPort().getPortDeviceName();
+        } else {
+            anodeId = input.getServiceAEnd().getNodeId();
+            znodeId = input.getServiceZEnd().getNodeId();
         }
 
         returnStructure.setRate(input.getServiceAEnd().getServiceRate().toJava());
@@ -190,7 +193,7 @@ public class PceCalculation {
                 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, nwInstanceIdentifier).get();
             if (nwOptional.isPresent()) {
                 nw = nwOptional.get();
-                LOG.debug("readMdSal: network nodes: nwOptional.isPresent = true {}", nw.toString());
+                LOG.debug("readMdSal: network nodes: nwOptional.isPresent = true {}", nw);
             }
         } catch (InterruptedException | ExecutionException e) {
             LOG.error("readMdSal: Error reading topology {}", nwInstanceIdentifier);
@@ -205,12 +208,13 @@ public class PceCalculation {
             LOG.error("readMdSal: network is null: {}", nwInstanceIdentifier);
             return false;
         }
-        allNodes = nw.getNode().stream().sorted((n1, n2) -> n1.getNodeId().getValue().compareTo(n2.getNodeId()
-            .getValue())).collect(Collectors.toList());
+        allNodes = nw.nonnullNode().values().stream().sorted((n1, n2)
+            -> n1.getNodeId().getValue().compareTo(n2.getNodeId().getValue())).collect(Collectors.toList());
         Network1 nw1 = nw.augmentation(Network1.class);
         if (nw1 != null) {
-            allLinks = nw1.getLink().stream().sorted((l1, l2) -> l1.getSource().getSourceTp().toString().compareTo(l2
-                .getSource().getSourceTp().toString())).collect(Collectors.toList());
+            allLinks = nw1.nonnullLink().values().stream().sorted((l1, l2)
+                -> l1.getSource().getSourceTp().toString().compareTo(l2.getSource().getSourceTp().toString()))
+                    .collect(Collectors.toList());
         } else {
             LOG.warn("no otn links in otn-topology");
         }
@@ -219,14 +223,14 @@ public class PceCalculation {
             return false;
         }
         LOG.info("readMdSal: network nodes: {} nodes added", allNodes.size());
-        LOG.debug("readMdSal: network nodes: {} nodes added", allNodes.toString());
+        LOG.debug("readMdSal: network nodes: {} nodes added", allNodes);
 
         if (allLinks == null || allLinks.isEmpty()) {
             LOG.error("readMdSal: no links ");
             return false;
         }
         LOG.info("readMdSal: network links: {} links added", allLinks.size());
-        LOG.debug("readMdSal: network links: {} links added", allLinks.toString());
+        LOG.debug("readMdSal: network links: {} links added", allLinks);
 
         return true;
     }
@@ -272,7 +276,7 @@ public class PceCalculation {
                 validateOtnNode(node);
             }
 
-            LOG.info("analyzeNw: allPceNodes {}", allPceNodes.toString());
+            LOG.info("analyzeNw: allPceNodes {}", allPceNodes);
 
             if (aendPceNode == null || zendPceNode == null) {
                 LOG.error("analyzeNw: Error in reading nodes: A or Z do not present in the network");
@@ -289,8 +293,8 @@ public class PceCalculation {
             return false;
         }
 
-        LOG.debug("analyzeNw: allPceNodes {}", allPceNodes.toString());
-        LOG.debug("analyzeNw: allPceLinks {}", allPceLinks.toString());
+        LOG.debug("analyzeNw: allPceNodes {}", allPceNodes);
+        LOG.debug("analyzeNw: allPceLinks {}", allPceLinks);
 
         return true;
     }
@@ -302,7 +306,7 @@ public class PceCalculation {
         if (azSrgs.contains(nodeId)) {
             allPceLinks.put(pcelink.getLinkId(), pcelink);
             allPceNodes.get(nodeId).addOutgoingLink(pcelink);
-            LOG.debug("analyzeNw: Add_LINK added to source and to allPceLinks {}", pcelink.getLinkId().toString());
+            LOG.debug("analyzeNw: Add_LINK added to source and to allPceLinks {}", pcelink.getLinkId());
             return true;
         }
 
@@ -320,7 +324,7 @@ public class PceCalculation {
         if (azSrgs.contains(nodeId)) {
             allPceLinks.put(pcelink.getLinkId(), pcelink);
             allPceNodes.get(nodeId).addOutgoingLink(pcelink);
-            LOG.debug("analyzeNw: Drop_LINK added to dest and to allPceLinks {}", pcelink.getLinkId().toString());
+            LOG.debug("analyzeNw: Drop_LINK added to dest and to allPceLinks {}", pcelink.getLinkId());
             return true;
         }
 
@@ -332,7 +336,7 @@ public class PceCalculation {
     }
 
     private boolean validateLink(Link link) {
-        LOG.info("validateLink: link {} ", link.toString());
+        LOG.info("validateLink: link {} ", link);
 
         NodeId sourceId = link.getSource().getSourceNode();
         NodeId destId = link.getDestination().getDestNode();
@@ -359,13 +363,10 @@ public class PceCalculation {
                 return false;
             }
             LinkId linkId = pcelink.getLinkId();
-            switch (validateLinkConstraints(pcelink)) {
-                case HARD_EXCLUDE:
-                    dropOppositeLink(link);
-                    LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
-                    return false;
-                default:
-                    break;
+            if (validateLinkConstraints(pcelink).equals(ConstraintTypes.HARD_EXCLUDE)) {
+                dropOppositeLink(link);
+                LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
+                return false;
             }
             switch (pcelink.getlinkType()) {
                 case ROADMTOROADM:
@@ -373,17 +374,17 @@ public class PceCalculation {
                     allPceLinks.put(linkId, pcelink);
                     source.addOutgoingLink(pcelink);
                     LOG.debug("validateLink: {}-LINK added to allPceLinks {}",
-                        pcelink.getlinkType(), pcelink.toString());
+                        pcelink.getlinkType(), pcelink);
                     break;
                 case ADDLINK:
                     pcelink.setClient(source.getRdmSrgClient(pcelink.getSourceTP().toString()));
                     addLinks.add(pcelink);
-                    LOG.debug("validateLink: ADD-LINK saved  {}", pcelink.toString());
+                    LOG.debug("validateLink: ADD-LINK saved  {}", pcelink);
                     break;
                 case DROPLINK:
                     pcelink.setClient(dest.getRdmSrgClient(pcelink.getDestTP().toString()));
                     dropLinks.add(pcelink);
-                    LOG.debug("validateLink: DROP-LINK saved  {}", pcelink.toString());
+                    LOG.debug("validateLink: DROP-LINK saved  {}", pcelink);
                     break;
                 case XPONDERINPUT:
                     // store separately all SRG links directly
@@ -391,7 +392,7 @@ public class PceCalculation {
                     // connected to A/Z
                     if (!dest.checkTP(pcelink.getDestTP().toString())) {
                         LOG.debug(
-                            "validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink.toString());
+                            "validateLink: XPONDER-INPUT is rejected as NW port is busy - {} ", pcelink);
                         return false;
                     }
                     if (dest.getXpdrClient(pcelink.getDestTP().toString()) != null) {
@@ -399,7 +400,7 @@ public class PceCalculation {
                     }
                     allPceLinks.put(linkId, pcelink);
                     source.addOutgoingLink(pcelink);
-                    LOG.debug("validateLink: XPONDER-INPUT link added to allPceLinks {}", pcelink.toString());
+                    LOG.debug("validateLink: XPONDER-INPUT link added to allPceLinks {}", pcelink);
                     break;
                 // does it mean XPONDER==>>SRG ?
                 case XPONDEROUTPUT:
@@ -408,7 +409,7 @@ public class PceCalculation {
                     // connected to A/Z
                     if (!source.checkTP(pcelink.getSourceTP().toString())) {
                         LOG.debug(
-                            "validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink.toString());
+                            "validateLink: XPONDER-OUTPUT is rejected as NW port is busy - {} ", pcelink);
                         return false;
                     }
                     if (source.getXpdrClient(pcelink.getSourceTP().toString()) != null) {
@@ -416,10 +417,10 @@ public class PceCalculation {
                     }
                     allPceLinks.put(linkId, pcelink);
                     source.addOutgoingLink(pcelink);
-                    LOG.debug("validateLink: XPONDER-OUTPUT link added to allPceLinks {}", pcelink.toString());
+                    LOG.debug("validateLink: XPONDER-OUTPUT link added to allPceLinks {}", pcelink);
                     break;
                 default:
-                    LOG.warn("validateLink: link type is not supported {}", pcelink.toString());
+                    LOG.warn("validateLink: link type is not supported {}", pcelink);
             }
             return true;
 
@@ -434,14 +435,12 @@ public class PceCalculation {
             }
 
             LinkId linkId = pceOtnLink.getLinkId();
-            switch (validateLinkConstraints(pceOtnLink)) {
-                case HARD_EXCLUDE:
-                    dropOppositeLink(link);
-                    LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
-                    return false;
-                default:
-                    break;
+            if (validateLinkConstraints(pceOtnLink).equals(ConstraintTypes.HARD_EXCLUDE)) {
+                dropOppositeLink(link);
+                LOG.debug("validateLink: constraints : link is ignored == {}", linkId.getValue());
+                return false;
             }
+
             switch (pceOtnLink.getlinkType()) {
                 case OTNLINK:
                     if (dest.getXpdrClient(pceOtnLink.getDestTP().toString()) != null) {
@@ -450,10 +449,10 @@ public class PceCalculation {
 
                     allPceLinks.put(linkId, pceOtnLink);
                     source.addOutgoingLink(pceOtnLink);
-                    LOG.info("validateLink: OTN-LINK added to allPceLinks {}", pceOtnLink.toString());
+                    LOG.info("validateLink: OTN-LINK added to allPceLinks {}", pceOtnLink);
                     break;
                 default:
-                    LOG.warn("validateLink: link type is not supported {}", pceOtnLink.toString());
+                    LOG.warn("validateLink: link type is not supported {}", pceOtnLink);
             }
             return true;
 
@@ -465,7 +464,7 @@ public class PceCalculation {
     }
 
     private boolean validateNode(Node node) {
-        LOG.debug("validateNode: node {} ", node.toString());
+        LOG.debug("validateNode: node {} ", node);
 
         // PceNode will be used in Graph algorithm
         Node1 node1 = node.augmentation(Node1.class);
@@ -475,29 +474,23 @@ public class PceCalculation {
         }
         OpenroadmNodeType nodeType = node1.getNodeType();
 
-        PceOpticalNode pceNode = new PceOpticalNode(node, nodeType, node.getNodeId(),
-            input.getServiceAEnd().getServiceFormat(), "optical");
-        pceNode.validateAZxponder(anodeId, znodeId);
+        PceOpticalNode pceNode = new PceOpticalNode(node, nodeType);
+        pceNode.validateAZxponder(anodeId, znodeId, input.getServiceAEnd().getServiceFormat());
         pceNode.initWLlist();
 
         if (!pceNode.isValid()) {
             LOG.warn(" validateNode: Node is ignored");
             return false;
         }
-
-        switch (validateNodeConstraints(pceNode)) {
-            case HARD_EXCLUDE:
-                return false;
-
-            default:
-                break;
+        if (validateNodeConstraints(pceNode).equals(ConstraintTypes.HARD_EXCLUDE)) {
+            return false;
         }
-        if ((pceNode.getSupNetworkNodeId().equals(anodeId) && (this.aendPceNode == null))
-            && (endPceNode(nodeType, pceNode.getNodeId(), pceNode))) {
+        if (endPceNode(nodeType, pceNode.getNodeId(), pceNode) && this.aendPceNode == null
+            && isAZendPceNode(this.serviceFormatA, pceNode, anodeId, "A")) {
             this.aendPceNode = pceNode;
         }
-        if ((pceNode.getSupNetworkNodeId().equals(znodeId) && (this.zendPceNode == null))
-            && (endPceNode(nodeType, pceNode.getNodeId(), pceNode))) {
+        if (endPceNode(nodeType, pceNode.getNodeId(), pceNode) && this.zendPceNode == null
+            && isAZendPceNode(this.serviceFormatZ, pceNode, znodeId, "Z")) {
             this.zendPceNode = pceNode;
         }
 
@@ -506,8 +499,31 @@ public class PceCalculation {
         return true;
     }
 
-    private boolean validateOtnNode(Node node) {
+    private boolean isAZendPceNode(String serviceFormat, PceOpticalNode pceNode, String azNodeId, String azEndPoint) {
+        switch (serviceFormat) {
+            case "Ethernet":
+            case "OC":
+                if (pceNode.getSupNetworkNodeId().equals(azNodeId)) {
+                    return true;
+                }
+                return false;
+            case "OTU":
+                if ("A".equals(azEndPoint) && pceNode.getNodeId().getValue()
+                    .equals(this.input.getServiceAEnd().getRxDirection().getPort().getPortDeviceName())) {
+                    return true;
+                }
+                if ("Z".equals(azEndPoint) && pceNode.getNodeId().getValue()
+                    .equals(this.input.getServiceZEnd().getRxDirection().getPort().getPortDeviceName())) {
+                    return true;
+                }
+                return false;
+            default:
+                LOG.debug("Unsupported service Format {} for node {}", serviceFormat, pceNode.getNodeId().getValue());
+                return false;
+        }
+    }
 
+    private boolean validateOtnNode(Node node) {
         LOG.info("validateOtnNode: {} ", node.getNodeId().getValue());
         // PceOtnNode will be used in Graph algorithm
         if (node.augmentation(Node1.class) != null) {
@@ -520,11 +536,8 @@ public class PceCalculation {
                 LOG.warn(" validateOtnNode: Node {} is ignored", node.getNodeId().getValue());
                 return false;
             }
-            switch (validateNodeConstraints(pceOtnNode)) {
-                case HARD_EXCLUDE:
-                    return false;
-                default:
-                    break;
+            if (validateNodeConstraints(pceOtnNode).equals(ConstraintTypes.HARD_EXCLUDE)) {
+                return false;
             }
             if (pceOtnNode.getNodeId().getValue().equals(anodeId) && this.aendPceNode == null) {
                 this.aendPceNode = pceOtnNode;
@@ -575,7 +588,7 @@ public class PceCalculation {
             return ConstraintTypes.NONE;
         }
 
-        List<Long> constraints = new ArrayList<Long>(pceHardConstraints.getExcludeSRLG());
+        List<Long> constraints = new ArrayList<>(pceHardConstraints.getExcludeSRLG());
         constraints.retainAll(link.getsrlgList());
         if (!constraints.isEmpty()) {
             LOG.info("validateLinkConstraints: {}", link.getLinkId().getValue());
@@ -588,9 +601,8 @@ public class PceCalculation {
     private void dropOppositeLink(Link link) {
         LinkId opplink = MapUtils.extractOppositeLink(link);
 
-        PceLink oppPceLink = allPceLinks.get(opplink);
-        if (oppPceLink != null) {
-            allPceLinks.remove(oppPceLink);
+        if (allPceLinks.containsKey(opplink)) {
+            allPceLinks.remove(opplink);
         } else {
             linksToExclude.add(opplink);
         }
@@ -603,7 +615,7 @@ public class PceCalculation {
                 this.azSrgs.add(nodeId);
                 break;
             case XPONDER:
-                pceNode.initXndrTps();
+                pceNode.initXndrTps(input.getServiceAEnd().getServiceFormat());
                 break;
             default:
                 LOG.warn("endPceNode: Node {} is not SRG or XPONDER !", nodeId);
@@ -641,12 +653,10 @@ public class PceCalculation {
         return returnStructure;
     }
 
-    private static void printNodesInfo(Map<NodeId, PceNode> allpcenodes) {
-        Iterator<Map.Entry<NodeId, PceNode>> nodes = allpcenodes.entrySet().iterator();
-        while (nodes.hasNext()) {
-            PceNode pcenode = nodes.next().getValue();
-            List<PceLink> links = pcenode.getOutgoingLinks();
-            LOG.info("In printNodes in node {} : outgoing links {} ", pcenode.getNodeId().getValue(), links.toString());
-        }
+    private static void printNodesInfo(Map<NodeId, PceNode> allPceNodes) {
+        allPceNodes.forEach(((nodeId, pceNode) -> {
+            LOG.info("In printNodes in node {} : outgoing links {} ", pceNode.getNodeId().getValue(),
+                    pceNode.getOutgoingLinks());
+        }));
     }
 }