Adapt PCE code for OTN services
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / graph / PceGraph.java
index 711cbcebe2d165e0d468a7b20fb1a4b3f7bd494c..279d23ef7be7748b18ad3a1b918a644db99364b3 100644 (file)
@@ -15,7 +15,7 @@ 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;
@@ -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 PceNode apceNode = null;
     private PceNode zpceNode = null;
+    private String serviceType = "";
 
     PceConstraints pceHardConstraints;
     PceConstraints pceSoftConstraints;
@@ -57,7 +59,8 @@ public class PceGraph {
     private List<PceLink> pathAtoZ = new ArrayList<PceLink>();
 
     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());
-
     }
 
     public boolean calcPath() {
@@ -88,9 +91,8 @@ public class PceGraph {
         // 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);
 
@@ -106,9 +108,17 @@ 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());
-            break;
+            if (("100GE".equals(serviceType)) || ("OTU4".equals(serviceType))) {
+                LOG.info("In calcPath Path FOUND path for wl [{}], hops {}, distance per metrics {}, path AtoZ {}",
+                        pceResult.getResultWavelength(), pathAtoZ.size(), path.getWeight(), pathAtoZ.toString());
+                break;
+            } else {
+                // Service is at OTN layer and is relying on a supporting wavelength service
+                LOG.info("In calcPath Path FOUND path for hops {}, distance per metrics {}, path AtoZ {}",
+                        pathAtoZ.size(), path.getWeight(), pathAtoZ.toString());
+                break;
+            }
+
         }
 
         if (shortestPathAtoZ != null) {
@@ -124,22 +134,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<String, PceGraphEdge>(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 +150,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().toString());
         }
-        // debug print
 
         return true;
     }
@@ -170,10 +169,8 @@ public class PceGraph {
             LOG.error("In addLinkToGraph link dest node is null : {}", pcelink.toString());
             return false;
         }
-
         LOG.debug("In addLinkToGraph link to nodes : {}{} {}", pcelink.toString(), source.toString(), dest.toString());
         return true;
-
     }
 
     private void populateWithNodes(DefaultDirectedWeightedGraph<String, PceGraphEdge> weightedGraph) {
@@ -214,38 +211,45 @@ public class PceGraph {
     }
 
     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());
                 break;
-
             case PropagationDelay :
                 weight = link.getLatency();
                 LOG.debug("In PceGraph PropagationDelay is used as a metrics. {}", link.toString());
+                if ((("1GE".equals(serviceType)) || ("10GE".equals(serviceType)) || ("ODU4".equals(serviceType)))
+                        && (weight == 0)) {
+                    LOG.warn("PropagationDelay set as metric, but latency is null: is latency set for OTN link {}?",
+                        link.toString());
+                }
                 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 +262,4 @@ public class PceGraph {
         this.pceHardConstraints = pceHardConstraintsInput;
         this.pceSoftConstraints = pceSoftConstraintsInput;
     }
-
 }