Small improvement in PceGraph
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / graph / PceGraph.java
index 711cbcebe2d165e0d468a7b20fb1a4b3f7bd494c..0c90ce290f3cd350867bf8006c122957a837d7d2 100644 (file)
@@ -13,19 +13,19 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-
 import org.jgrapht.GraphPath;
-import org.jgrapht.alg.shortestpath.KShortestPaths;
+import org.jgrapht.alg.shortestpath.KShortestSimplePaths;
 import org.jgrapht.alg.shortestpath.PathValidator;
 import org.jgrapht.graph.DefaultDirectedWeightedGraph;
 import org.opendaylight.transportpce.common.ResponseCodes;
+import org.opendaylight.transportpce.common.StringConstants;
 import org.opendaylight.transportpce.pce.constraints.PceConstraints;
 import org.opendaylight.transportpce.pce.networkanalyzer.PceLink;
 import org.opendaylight.transportpce.pce.networkanalyzer.PceNode;
 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult;
 import org.opendaylight.transportpce.pce.networkanalyzer.PceResult.LocalCause;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -35,14 +35,16 @@ public class PceGraph {
 
     ////////////////////////// for Graph ///////////////////////////
     // how many paths to bring
-    int kpathsToBring = 10;
+    private int kpathsToBring = 10;
+
     // max #hops
-    int mhopsPerPath = 50;
+    private int mhopsPerPath = 50;
 
     // input
-    private Map<NodeId, PceNode> allPceNodes = new HashMap<NodeId, PceNode>();
+    private Map<NodeId, PceNode> allPceNodes = new HashMap<>();
     private PceNode apceNode = null;
     private PceNode zpceNode = null;
+    private String serviceType = "";
 
     PceConstraints pceHardConstraints;
     PceConstraints pceSoftConstraints;
@@ -54,10 +56,11 @@ public class PceGraph {
     // for path calculation
     List<GraphPath<String, PceGraphEdge>> allWPaths = null;
 
-    private List<PceLink> pathAtoZ = new ArrayList<PceLink>();
+    private List<PceLink> pathAtoZ = new ArrayList<>();
 
     public PceGraph(PceNode aendNode, PceNode zendNode, Map<NodeId, PceNode> allPceNodes,
-            PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult pceResult) {
+            PceConstraints pceHardConstraints, PceConstraints pceSoftConstraints, PceResult pceResult,
+            String serviceType) {
         super();
         this.apceNode = aendNode;
         this.zpceNode = zendNode;
@@ -65,10 +68,10 @@ public class PceGraph {
         this.pceResult = pceResult;
         this.pceHardConstraints = pceHardConstraints;
         this.pceSoftConstraints = pceSoftConstraints;
+        this.serviceType = serviceType;
 
-        LOG.info("In GraphCalculator: A and Z = {} / {} ", aendNode.toString(), zendNode.toString());
-        LOG.debug("In GraphCalculator: allPceNodes size {}, nodes {} ", allPceNodes.size(), allPceNodes.toString());
-
+        LOG.info("In GraphCalculator: A and Z = {} / {} ", aendNode, zendNode);
+        LOG.debug("In GraphCalculator: allPceNodes size {}, nodes {} ", allPceNodes.size(), allPceNodes);
     }
 
     public boolean calcPath() {
@@ -76,26 +79,25 @@ public class PceGraph {
         LOG.info(" In PCE GRAPH calcPath : K SHORT PATHS algorithm ");
 
         DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph =
-                new DefaultDirectedWeightedGraph<String, PceGraphEdge>(PceGraphEdge.class);
+                new DefaultDirectedWeightedGraph<>(PceGraphEdge.class);
         populateWithNodes(weightedGraph);
         populateWithLinks(weightedGraph);
 
         if (!runKgraphs(weightedGraph)) {
-            LOG.info("In calcPath : pceResult {}", pceResult.toString());
+            LOG.info("In calcPath : pceResult {}", pceResult);
             return false;
         }
 
         // validate found paths
         pceResult.setRC(ResponseCodes.RESPONSE_FAILED);
         for (GraphPath<String, PceGraphEdge> path : allWPaths) {
-
             PostAlgoPathValidator papv = new PostAlgoPathValidator();
-            pceResult = papv.checkPath(path, allPceNodes, pceResult);
+            pceResult = papv.checkPath(path, allPceNodes, pceResult, pceHardConstraints, serviceType);
             LOG.info("In calcPath after PostAlgoPathValidator {} {}",
                     pceResult.getResponseCode(), ResponseCodes.RESPONSE_OK);
 
             if (!pceResult.getResponseCode().equals(ResponseCodes.RESPONSE_OK)) {
-                LOG.info("In calcPath: post algo validations DROPPED the path {}", path.toString());
+                LOG.info("In calcPath: post algo validations DROPPED the path {}", path);
                 continue;
             }
 
@@ -106,16 +108,33 @@ public class PceGraph {
             }
 
             shortestPathAtoZ = new ArrayList<>(pathAtoZ);
-            LOG.info("In calcPath Path FOUND path for wl [{}], hops {}, distance per metrics {}, path AtoZ {}",
-                    pceResult.getResultWavelength(), pathAtoZ.size(), path.getWeight(), pathAtoZ.toString());
+            switch (serviceType) {
+
+                case StringConstants.SERVICE_TYPE_100GE:
+                case StringConstants.SERVICE_TYPE_OTU4:
+                    LOG.info(
+                        "In calcPath Path FOUND path for wl [{}], min Freq assignment {}, max Freq assignment {},"
+                        + " hops {}, distance per metrics {}, path AtoZ {}",
+                        pceResult.getResultWavelength(), pceResult.getMinFreq(), pceResult.getMaxFreq(),
+                        pathAtoZ.size(), path.getWeight(), pathAtoZ);
+                    break;
+
+                default:
+                    LOG.info(
+                        "In calcPath Path FOUND path for hops {}, distance per metrics {}, path AtoZ {}",
+                        pathAtoZ.size(), path.getWeight(), pathAtoZ);
+                    break;
+            }
             break;
+
         }
 
         if (shortestPathAtoZ != null) {
-            LOG.info("In calcPath CHOOSEN PATH for wl [{}], hops {}, path AtoZ {}",
-                    pceResult.getResultWavelength(), shortestPathAtoZ.size(), shortestPathAtoZ.toString());
+            LOG.info("In calcPath CHOOSEN PATH for wl [{}], min freq {}, max freq {}, hops {}, path AtoZ {}",
+                    pceResult.getResultWavelength(), pceResult.getMinFreq(), pceResult.getMaxFreq(),
+                    shortestPathAtoZ.size(), shortestPathAtoZ);
         }
-        LOG.info("In calcPath : pceResult {}", pceResult.toString());
+        LOG.info("In calcPath : pceResult {}", pceResult);
         return (pceResult.getStatus());
     }
 
@@ -124,22 +143,12 @@ public class PceGraph {
         if (weightedGraph.edgeSet().isEmpty() || weightedGraph.vertexSet().isEmpty()) {
             return false;
         }
-
-        PathValidator<String, PceGraphEdge> wpv = new InAlgoPathValidator(pceHardConstraints, zpceNode);
-
-        // local optimization. if 'include' constraint exists then increase amount of paths to return.
-        // it's because this constraint is checked at the last step when part of good paths
-        // are dropped by other constraints
-        if (!pceHardConstraints.getListToInclude().isEmpty()) {
-            kpathsToBring = kpathsToBring * 10;
-            LOG.info("k = {}",kpathsToBring);
-        }
+        PathValidator<String, PceGraphEdge> wpv = new InAlgoPathValidator();
 
         // KShortestPaths on weightedGraph
-        KShortestPaths<String, PceGraphEdge> swp =
-            new KShortestPaths<String, PceGraphEdge>(weightedGraph, kpathsToBring, mhopsPerPath, wpv);
-
-        allWPaths = swp.getPaths(apceNode.getNodeId().getValue(), zpceNode.getNodeId().getValue());
+        KShortestSimplePaths<String, PceGraphEdge> swp =
+            new KShortestSimplePaths<>(weightedGraph, mhopsPerPath, wpv);
+        allWPaths = swp.getPaths(apceNode.getNodeId().getValue(), zpceNode.getNodeId().getValue(), kpathsToBring);
 
         if (allWPaths.isEmpty()) {
             LOG.info(" In runKgraphs : algorithm didn't find any path");
@@ -150,9 +159,8 @@ public class PceGraph {
 
         // debug print
         for (GraphPath<String, PceGraphEdge> path : allWPaths) {
-            LOG.info("path Weight: {} : {}", path.getWeight(), path.getVertexList().toString());
+            LOG.debug("path Weight: {} : {}", path.getWeight(), path.getVertexList());
         }
-        // debug print
 
         return true;
     }
@@ -163,25 +171,25 @@ public class PceGraph {
         PceNode dest = allPceNodes.get(pcelink.getDestId());
 
         if (source == null) {
-            LOG.error("In addLinkToGraph link source node is null : {}", pcelink.toString());
+            LOG.error("In addLinkToGraph link source node is null : {}", pcelink);
             return false;
         }
         if (dest == null) {
-            LOG.error("In addLinkToGraph link dest node is null : {}", pcelink.toString());
+            LOG.error("In addLinkToGraph link dest node is null : {}", pcelink);
             return false;
         }
-
-        LOG.debug("In addLinkToGraph link to nodes : {}{} {}", pcelink.toString(), source.toString(), dest.toString());
+        LOG.debug("In addLinkToGraph link to nodes : {}{} {}", pcelink, source, dest);
         return true;
-
     }
 
     private void populateWithNodes(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
         Iterator<Map.Entry<NodeId, PceNode>> nodes = allPceNodes.entrySet().iterator();
         while (nodes.hasNext()) {
             Map.Entry<NodeId, PceNode> node = nodes.next();
-            weightedGraph.addVertex(node.getValue().getNodeId().getValue());
-            LOG.debug("In populateWithNodes in node :  {}", node.getValue().toString());
+            if (State.InService.equals(node.getValue().getState())) {
+                weightedGraph.addVertex(node.getValue().getNodeId().getValue());
+                LOG.debug("In populateWithNodes in node :  {}", node.getValue());
+            }
         }
     }
 
@@ -195,57 +203,65 @@ public class PceGraph {
             PceNode pcenode = node.getValue();
             List<PceLink> links = pcenode.getOutgoingLinks();
 
-            LOG.debug("In populateGraph: use node for graph {}", pcenode.toString());
+            LOG.debug("In populateGraph: use node for graph {}", pcenode);
 
             for (PceLink link : links) {
-                LOG.debug("In populateGraph node {} : add edge to graph {}", pcenode.toString(), link.toString());
+                LOG.debug("In populateGraph node {} : add edge to graph {}", pcenode, link);
 
                 if (!validateLinkforGraph(link)) {
                     continue;
                 }
+                if (State.InService.equals(link.getState())) {
+                    PceGraphEdge graphLink = new PceGraphEdge(link);
+                    weightedGraph.addEdge(link.getSourceId().getValue(), link.getDestId().getValue(), graphLink);
 
-                PceGraphEdge graphLink = new PceGraphEdge(link);
-                weightedGraph.addEdge(link.getSourceId().getValue(), link.getDestId().getValue(), graphLink);
-
-                weightedGraph.setEdgeWeight(graphLink, chooseWeight(link));
+                    weightedGraph.setEdgeWeight(graphLink, chooseWeight(link));
+                }
             }
         }
         return true;
     }
 
     private double chooseWeight(PceLink link) {
-
         // HopCount is default
         double weight = 1;
         switch (pceHardConstraints.getPceMetrics()) {
-            case IGPMetric :
-                // TODO implement IGPMetric - low priority.
-                LOG.warn("In PceGraph not implemented IGPMetric. HopCount works as a default");
-                break;
-
-            case TEMetric :
-                // TODO implement TEMetric - low priority
-                LOG.warn("In PceGraph not implemented TEMetric. HopCount works as a default");
-                break;
-
             case HopCount :
                 weight = 1;
-                LOG.debug("In PceGraph HopCount is used as a metrics. {}", link.toString());
+                LOG.debug("In PceGraph HopCount is used as a metrics. {}", link);
                 break;
-
             case PropagationDelay :
                 weight = link.getLatency();
-                LOG.debug("In PceGraph PropagationDelay is used as a metrics. {}", link.toString());
+                LOG.debug("In PceGraph PropagationDelay is used as a metrics. {}", link);
+                if ((weight == 0)
+                        && ("1GE".equals(serviceType) || "10GE".equals(serviceType) || "ODU4".equals(serviceType))) {
+                    LOG.warn("PropagationDelay set as metric, but latency is null: is latency set for OTN link {}?",
+                        link);
+                }
                 break;
-
+            // TODO implement IGPMetric and TEMetric - low priority.
+            case IGPMetric :
+            case TEMetric :
             default:
+                LOG.warn("In PceGraph {} not implemented. HopCount works as a default",
+                    pceHardConstraints.getPceMetrics());
                 break;
         }
-
         return weight;
     }
 
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
+    public int getKpathsToBring() {
+        return kpathsToBring;
+    }
+
+    public void setKpathsToBring(int kpathsToBring) {
+        this.kpathsToBring = kpathsToBring;
+    }
+
+    public void setMhopsPerPath(int mhopsPerPath) {
+        this.mhopsPerPath = mhopsPerPath;
+    }
+
     public List<PceLink> getPathAtoZ() {
         return shortestPathAtoZ;
     }
@@ -258,5 +274,4 @@ public class PceGraph {
         this.pceHardConstraints = pceHardConstraintsInput;
         this.pceSoftConstraints = pceSoftConstraintsInput;
     }
-
 }