Decouple the Path Computation algorithm class with the route checker in simple-pce 84/66784/2
authorY.Jace Liu <yang.jace.liu@linux.com>
Thu, 28 Dec 2017 08:54:42 +0000 (16:54 +0800)
committerKai Gao <godrickk@gmail.com>
Fri, 2 Feb 2018 05:25:46 +0000 (05:25 +0000)
Change-Id: I8ef6ebb57d7992bc29e4e8c1b3c60dbd6cd8306a
Signed-off-by: Y.Jace Liu <yang.jace.liu@linux.com>
alto-extensions/simple-pce/api/src/main/yang/alto-spce.yang
alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/MaxBandwidthPathRouteChecker.java [new file with mode: 0644]
alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/PathComputation.java
alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/RouteChecker.java [new file with mode: 0644]
alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/RouteViewer.java
alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/RouteViewerPath.java [new file with mode: 0644]
alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/ShortestPathRouteChecker.java [new file with mode: 0644]
alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/util/RouteManager.java
alto-extensions/simple-pce/impl/src/test/java/org/opendaylight/alto/spce/impl/algorithm/PathComputationTest.java

index f5bbe6744f7dfba493edd275aa18114e363727ab..635176fc6fef257ccad6d10aa1fb3f018e678b71 100644 (file)
@@ -31,7 +31,7 @@ module alto-spce {
             enum SETTING_UP_METER_ERROR;
             enum UNDEFINED_ERROR;
             enum REMOVING_ROUTE_ERROR;
-            enum REMOVING_METTER_ERROR;
+            enum REMOVING_METER_ERROR;
             enum MISSING_ROUTE_ERROR;
             enum UPDATING_RATE_LIMITING_ERROR;
             enum INVALID_INPUT;
@@ -62,7 +62,7 @@ module alto-spce {
 
     rpc setup-route {
         description "Given the endpoints, this service computes a route
-           satisfying the constraints; among those satisfying the constraints, 
+           satisfying the constraints; among those satisfying the constraints,
            it computes the one with the best object metrics. If a route is
            found, the service will setup the route by installing flow rules
            in FRM. This service does not handle the complexity of re-route.";
diff --git a/alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/MaxBandwidthPathRouteChecker.java b/alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/MaxBandwidthPathRouteChecker.java
new file mode 100644 (file)
index 0000000..b1aca6d
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2015 SNLAB and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.alto.spce.impl.algorithm;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.spce.rev160718.AltoSpceMetric;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.spce.rev160718.setup.route.input.ConstraintMetric;
+
+import java.util.LinkedList;
+import java.util.List;
+
+public class MaxBandwidthPathRouteChecker extends RouteChecker {
+    private List<RouteViewerPath> result;
+    private long bandwidth = 0;
+    private List<ConstraintMetric> constraintMetrics = null;
+    private RouteViewerPath finalPath = null;
+
+    MaxBandwidthPathRouteChecker(RouteViewerPath finalPath, List<ConstraintMetric> constraintMetrics){
+        this.finalPath = finalPath;
+        this.constraintMetrics = constraintMetrics;
+        this.bandwidth = 0;
+    }
+
+    @Override
+    public boolean isStop(List<RouteViewerPath> pathList) {
+        LinkedList<RouteViewerPath> tmp = new LinkedList<>(pathList);
+        tmp.add(finalPath);
+        long hopcount = tmp.size();
+        long bandwidth = getBandwidth(pathList);
+        List<ConstraintMetric> constraintMetrics = this.constraintMetrics;
+        if (constraintMetrics != null) {
+            for (ConstraintMetric eachConstraint : constraintMetrics) {
+                if (eachConstraint.getMetric() == null) continue;
+                long max = (eachConstraint.getMax() != null) ?
+                        eachConstraint.getMax().longValue() : Long.MAX_VALUE;
+                long min = (eachConstraint.getMin() != null) ?
+                        eachConstraint.getMin().longValue() : 0;
+                long value = 0;
+                if (eachConstraint.getMetric().equals(AltoSpceMetric.Bandwidth)) {
+                    value = bandwidth;
+                } else {
+                    value = hopcount;
+                }
+                if (value < min || value > max) {
+                    return false;
+                }
+            }
+        }
+
+        if (bandwidth > this.bandwidth) {
+            this.bandwidth = bandwidth;
+            result = tmp;
+        }
+        return false;
+    }
+
+    @Override
+    public List<RouteViewerPath> getResult() {
+        return result;
+    }
+}
index ca3dd4586155fa3e7f467a387215353272c35cfd..578456175bee82c9b3c1ed26917a05f811f34fe4 100644 (file)
@@ -50,59 +50,18 @@ public class PathComputation {
 
     public List<TpId> shortestPath(TpId srcTpId, TpId dstTpId, Topology topology,
                                    final List<ConstraintMetric> constraintMetrics) {
-        final RouteViewer.Path finalPath = new RouteViewer.Path();
+        final RouteViewerPath finalPath = new RouteViewerPath();
         finalPath.src = dstTpId;
         finalPath.bandwidth = getBandwidthByTp(dstTpId.getValue()).longValue();
-        RouteViewer.RouteChecker checker = new RouteViewer.RouteChecker() {
-            private List<RouteViewer.Path> result;
-            private long hopcount = Long.MAX_VALUE;
-            @Override
-            public boolean isStop(List<RouteViewer.Path> pathList) {
-                LinkedList<RouteViewer.Path> tmp = new LinkedList<>(pathList);
-                tmp.add(finalPath);
-                long hopcount = tmp.size();
-                long bandwidth = getBandwidth(tmp);
-                if (constraintMetrics != null) {
-                    for (ConstraintMetric eachConstraint : constraintMetrics) {
-                        if (eachConstraint.getMetric() == null) {
-                            continue;
-                        }
-                        long max = (eachConstraint.getMax() != null) ?
-                                eachConstraint.getMax().longValue() : Long.MAX_VALUE;
-                        long min = (eachConstraint.getMin() != null) ?
-                                eachConstraint.getMin().longValue() : 0;
-                        long value = 0;
-                        if (eachConstraint.getMetric().equals(AltoSpceMetric.Bandwidth)) {
-                            value = bandwidth;
-                        } else {
-                            value = hopcount;
-                        }
-                        if (value < min || value > max) {
-                            return false;
-                        }
-                    }
-                }
-                if (hopcount < this.hopcount) {
-                    this.hopcount = hopcount;
-                    result = tmp;
-                }
-                return false;
-            }
-
-            @Override
-            public List<RouteViewer.Path> getResult() {
-                return result;
-            }
-        };
-
+        RouteChecker checker = new ShortestPathRouteChecker(finalPath, constraintMetrics);
         RouteViewer rv = new RouteViewer(getGraphFromTopology(topology, (long) 0), checker);
-        List<RouteViewer.Path> result = rv.viewRoutes(
+        List<RouteViewerPath> result = rv.viewRoutes(
                     RouteViewer.extractNodeId(srcTpId),
                     RouteViewer.extractNodeId(dstTpId))
                 .getResult();
         logger.info(result.toString());
         List<TpId> output = new LinkedList<>();
-        for (RouteViewer.Path eachPath : result) {
+        for (RouteViewerPath eachPath : result) {
             output.add(eachPath.src);
         }
         return output;
@@ -110,72 +69,23 @@ public class PathComputation {
 
     public List<TpId> maxBandwidthPath(TpId srcTpId, TpId dstTpId, Topology topology,
                                        final List<ConstraintMetric> constraintMetrics) {
-        final RouteViewer.Path finalPath = new RouteViewer.Path();
+        final RouteViewerPath finalPath = new RouteViewerPath();
         finalPath.src = dstTpId;
         finalPath.bandwidth = getBandwidthByTp(dstTpId.getValue()).longValue();
-        RouteViewer.RouteChecker checker = new RouteViewer.RouteChecker() {
-            private List<RouteViewer.Path> result;
-            private long bandwidth = 0;
-            @Override
-            public boolean isStop(List<RouteViewer.Path> pathList) {
-                LinkedList<RouteViewer.Path> tmp = new LinkedList<>(pathList);
-                tmp.add(finalPath);
-                long hopcount = tmp.size();
-                long bandwidth = getBandwidth(tmp);
-                if (constraintMetrics != null) {
-                    for (ConstraintMetric eachConstraint : constraintMetrics) {
-                        if (eachConstraint.getMetric() == null) {
-                            continue;
-                        }
-                        long max = (eachConstraint.getMax() != null) ?
-                                eachConstraint.getMax().longValue() : Long.MAX_VALUE;
-                        long min = (eachConstraint.getMin() != null) ?
-                                eachConstraint.getMin().longValue() : 0;
-                        long value = 0;
-                        if (eachConstraint.getMetric().equals(AltoSpceMetric.Bandwidth)) {
-                            value = bandwidth;
-                        } else {
-                            value = hopcount;
-                        }
-                        if (value < min || value > max) {
-                            return false;
-                        }
-                    }
-                }
-                if (bandwidth > this.bandwidth) {
-                    this.bandwidth = bandwidth;
-                    result = tmp;
-                }
-                return false;
-            }
-
-            @Override
-            public List<RouteViewer.Path> getResult() {
-                return result;
-            }
-        };
-
+        RouteChecker checker = new MaxBandwidthPathRouteChecker(finalPath, constraintMetrics);
         RouteViewer rv = new RouteViewer(getGraphFromTopology(topology, (long) 0), checker);
-        List<RouteViewer.Path> result = rv.viewRoutes(
+        List<RouteViewerPath> result = rv.viewRoutes(
                 RouteViewer.extractNodeId(srcTpId),
                 RouteViewer.extractNodeId(dstTpId))
                 .getResult();
 
         List<TpId> output = new LinkedList<>();
-        for (RouteViewer.Path eachPath : result) {
+        for (RouteViewerPath eachPath : result) {
             output.add(eachPath.src);
         }
         return output;
     }
 
-    long getBandwidth(List<RouteViewer.Path> pathList) {
-        Long result = Long.MAX_VALUE;
-        for (RouteViewer.Path eachPath : pathList) {
-            result = (result < eachPath.bandwidth) ? result : eachPath.bandwidth;
-        }
-        return result;
-    }
-
     public List<TpId> shortestPathOpti(TpId srcTpId, TpId dstTpId, Topology topology, List<ConstraintMetric> constraintMetrics) {
         String src = srcTpId.getValue();
         String dst = dstTpId.getValue();
@@ -186,11 +96,11 @@ public class PathComputation {
                         minBw : eachConstraint.getMin().longValue();
             }
         }
-        Graph<String, RouteViewer.Path> networkGraph = getGraphFromTopology(topology, minBw);
-        DijkstraShortestPath<String, RouteViewer.Path> shortestPath = new DijkstraShortestPath<>(networkGraph);
-        List<RouteViewer.Path> path = shortestPath.getPath(extractNodeId(src), extractNodeId(dst));
+        Graph<String, RouteViewerPath> networkGraph = getGraphFromTopology(topology, minBw);
+        DijkstraShortestPath<String, RouteViewerPath> shortestPath = new DijkstraShortestPath<>(networkGraph);
+        List<RouteViewerPath> path = shortestPath.getPath(extractNodeId(src), extractNodeId(dst));
         List<TpId> output = new LinkedList<>();
-        for (RouteViewer.Path eachPath : path) {
+        for (RouteViewerPath eachPath : path) {
             output.add(eachPath.src);
         }
         return output;
@@ -199,7 +109,7 @@ public class PathComputation {
     public List<TpId> maxBandwidthPathOpti(TpId srcTpId, TpId dstTpId, Topology topology, List<ConstraintMetric> constraintMetrics) {
         String src = srcTpId.getValue();
         String dst = dstTpId.getValue();
-        Graph<String, RouteViewer.Path> networkGraph = getGraphFromTopology(topology, null);
+        Graph<String, RouteViewerPath> networkGraph = getGraphFromTopology(topology, null);
         Long maxHop = Long.MAX_VALUE;
         for (ConstraintMetric eachConstraint : constraintMetrics) {
             if (AltoSpceMetric.Hopcount == eachConstraint.getMetric() && eachConstraint.getMax() != null) {
@@ -207,9 +117,9 @@ public class PathComputation {
                         maxHop : eachConstraint.getMax().longValue();
             }
         }
-        List<RouteViewer.Path> path = maxBandwidth(networkGraph, extractNodeId(src), extractNodeId(dst), maxHop);
+        List<RouteViewerPath> path = maxBandwidth(networkGraph, extractNodeId(src), extractNodeId(dst), maxHop);
         List<TpId> output = new LinkedList<>();
-        for (RouteViewer.Path eachPath : path) {
+        for (RouteViewerPath eachPath : path) {
             output.add(eachPath.src);
         }
         return output;
@@ -220,22 +130,22 @@ public class PathComputation {
      **       return this route;
      ** (3) else: continue the adding.
      **/
-    public List<RouteViewer.Path> maxBandwidth(Graph<String, RouteViewer.Path> networkGraph, String src, String dst, Long maxHop) {
+    public List<RouteViewerPath> maxBandwidth(Graph<String, RouteViewerPath> networkGraph, String src, String dst, Long maxHop) {
         Map<String, Long> hopCount = new HashMap<>();
-        Map<String, RouteViewer.Path> pre = new HashMap<>();
+        Map<String, RouteViewerPath> pre = new HashMap<>();
         hopCount.put(src, (long) 0);
-        List<RouteViewer.Path> paths = new ArrayList<>(networkGraph.getEdges());
-        Collections.sort(paths, new Comparator<RouteViewer.Path>() {
+        List<RouteViewerPath> paths = new ArrayList<>(networkGraph.getEdges());
+        Collections.sort(paths, new Comparator<RouteViewerPath>() {
             @Override
-            public int compare(RouteViewer.Path x, RouteViewer.Path y) {
+            public int compare(RouteViewerPath x, RouteViewerPath y) {
                 return (Objects.equals(x.bandwidth, y.bandwidth) ? 0 : (x.bandwidth > y.bandwidth ? -1 : 1));
             }
         });
-        Graph<String, RouteViewer.Path> graph = new SparseMultigraph<>();
+        Graph<String, RouteViewerPath> graph = new SparseMultigraph<>();
         // add every node into the graph
         for (String eachNode : networkGraph.getVertices())
             graph.addVertex(eachNode);
-        for (RouteViewer.Path eachPath : paths) {
+        for (RouteViewerPath eachPath : paths) {
             String srcNode = extractNodeId(eachPath.src.getValue());
             String dstNode = extractNodeId(eachPath.dst.getValue());
             graph.addEdge(eachPath, srcNode, dstNode, EdgeType.DIRECTED);
@@ -246,7 +156,7 @@ public class PathComputation {
                 while (!queue.isEmpty()) {
                     srcNode = queue.pop();
                     if (graph.getOutEdges(srcNode) != null) {
-                        for (RouteViewer.Path outPath : graph.getOutEdges(srcNode)) {
+                        for (RouteViewerPath outPath : graph.getOutEdges(srcNode)) {
                             dstNode = extractNodeId(outPath.dst.getValue());
                             if (!hopCount.containsKey(dstNode) ||
                                     (hopCount.get(dstNode) > hopCount.get(srcNode) + 1)) {
@@ -259,7 +169,7 @@ public class PathComputation {
                 }
                 if (hopCount.containsKey(dst) && hopCount.get(dst) <= maxHop) {
                     // finally, build the route
-                    List<RouteViewer.Path> output = new LinkedList<>();
+                    List<RouteViewerPath> output = new LinkedList<>();
                     output.add(0, pre.get(dst));
                     while (!extractNodeId(output.get(0).src.getValue()).equals(src)) {
                         dst = extractNodeId(output.get(0).src.getValue());
@@ -272,8 +182,8 @@ public class PathComputation {
         return null;
     }
 
-    private Graph<String, RouteViewer.Path> getGraphFromTopology(Topology topology, Long minBw) {
-        Graph<String, RouteViewer.Path> networkGraph = new SparseMultigraph();
+    private Graph<String, RouteViewerPath> getGraphFromTopology(Topology topology, Long minBw) {
+        Graph<String, RouteViewerPath> networkGraph = new SparseMultigraph();
         if (minBw == null) {
             minBw = (long) 0;
         }
@@ -288,7 +198,7 @@ public class PathComputation {
             }
             TpId linkSrcTp = eachLink.getSource().getSourceTp();
             TpId linkDstTp = eachLink.getDestination().getDestTp();
-            RouteViewer.Path srcPath = new RouteViewer.Path();
+            RouteViewerPath srcPath = new RouteViewerPath();
             srcPath.src = linkSrcTp;
             srcPath.dst = linkDstTp;
             srcPath.bandwidth = getBandwidthByTp(srcPath.src.getValue()).longValue();
diff --git a/alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/RouteChecker.java b/alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/RouteChecker.java
new file mode 100644 (file)
index 0000000..6699f7a
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2015 SNLAB and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.alto.spce.impl.algorithm;
+
+import java.util.List;
+
+public abstract class RouteChecker {
+    abstract boolean isStop(List<RouteViewerPath> pathList);
+    abstract List<RouteViewerPath> getResult();
+
+    protected long getBandwidth(List<RouteViewerPath> pathList) {
+        Long result = Long.MAX_VALUE;
+        for (RouteViewerPath eachPath : pathList) {
+            result = (result < eachPath.bandwidth) ? result : eachPath.bandwidth;
+        }
+        return result;
+    }
+}
index 6ea0781122e952201e0f7f05a93ceeec27d93955..1c7a397cb3b45fc6729fe48c179ab58cd8c7357c 100644 (file)
@@ -14,33 +14,16 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 
 import java.util.HashMap;
 import java.util.LinkedList;
-import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 
 public class RouteViewer {
-    static public interface RouteChecker {
-        boolean isStop(List<Path> pathList);
-        List<Path> getResult();
-    }
-
-    static public class Path {
-        TpId src;
-        TpId dst;
-        Long bandwidth;
-
-        @Override
-        public String toString() {
-            return "" + src + "->" + dst + "@" + bandwidth;
-        }
-    }
-
     private RouteChecker routeChecker;
     private Map<String, Boolean> hasVisited;
-    private Graph<String, Path> networkGraph;
-    private LinkedList<Path> route;
+    private Graph<String, RouteViewerPath> networkGraph;
+    private LinkedList<RouteViewerPath> route;
 
-    RouteViewer(Graph<String, Path> networkGraph, RouteChecker routeChecker) {
+    RouteViewer(Graph<String, RouteViewerPath> networkGraph, RouteChecker routeChecker) {
         this.networkGraph = networkGraph;
         this.routeChecker = routeChecker;
     }
@@ -63,7 +46,7 @@ public class RouteViewer {
         }
         else {
             hasVisited.put(srcNode, true);
-            for (Path eachPath : this.networkGraph.getOutEdges(srcNode)) {
+            for (RouteViewerPath eachPath : this.networkGraph.getOutEdges(srcNode)) {
                 route.addLast(eachPath);
                 if (visitor(extractNodeId(eachPath.dst), dstNode))
                     return true;
diff --git a/alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/RouteViewerPath.java b/alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/RouteViewerPath.java
new file mode 100644 (file)
index 0000000..9fbab3d
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2015 SNLAB and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.alto.spce.impl.algorithm;
+
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
+
+public class RouteViewerPath {
+    TpId src;
+    TpId dst;
+    Long bandwidth;
+
+    @Override
+    public String toString() {
+        return "" + src + "->" + dst + "@" + bandwidth;
+    }
+}
diff --git a/alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/ShortestPathRouteChecker.java b/alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/ShortestPathRouteChecker.java
new file mode 100644 (file)
index 0000000..1bba179
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2015 SNLAB and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.alto.spce.impl.algorithm;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.spce.rev160718.AltoSpceMetric;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.spce.rev160718.setup.route.input.ConstraintMetric;
+
+import java.util.LinkedList;
+import java.util.List;
+
+public class ShortestPathRouteChecker extends RouteChecker {
+    private List<RouteViewerPath> result;
+    private long hopcount = Long.MAX_VALUE;
+    private List<ConstraintMetric> constraintMetrics = null;
+    private RouteViewerPath finalPath = null;
+
+    ShortestPathRouteChecker(RouteViewerPath finalPath, List<ConstraintMetric> constraintMetrics){
+        this.finalPath = finalPath;
+        this.constraintMetrics = constraintMetrics;
+    }
+
+    @Override
+    public boolean isStop(List<RouteViewerPath> pathList) {
+        LinkedList<RouteViewerPath> tmp = new LinkedList<>(pathList);
+        tmp.add(finalPath);
+        long hopcount = tmp.size();
+        long bandwidth = getBandwidth(pathList);
+        List<ConstraintMetric> constraintMetrics = this.constraintMetrics;
+        if (constraintMetrics != null) {
+            for (ConstraintMetric eachConstraint : constraintMetrics) {
+                if (eachConstraint.getMetric() == null) continue;
+                long max = (eachConstraint.getMax() != null) ?
+                        eachConstraint.getMax().longValue() : Long.MAX_VALUE;
+                long min = (eachConstraint.getMin() != null) ?
+                        eachConstraint.getMin().longValue() : 0;
+                long value = 0;
+                if (eachConstraint.getMetric().equals(AltoSpceMetric.Bandwidth)) {
+                    value = bandwidth;
+                } else {
+                    value = hopcount;
+                }
+                if (value < min || value > max) {
+                    return false;
+                }
+            }
+        }
+        if (hopcount < this.hopcount) {
+            this.hopcount = hopcount;
+            result = tmp;
+        }
+        return false;
+    }
+
+    @Override
+    public List<RouteViewerPath> getResult() {
+        return result;
+    }
+}
index b8fc65792827bfd47665d355725c44f7034a45c5..9d78fcc00cfb965ec49990f4dd8b97c9e1a60dec 100644 (file)
@@ -133,15 +133,7 @@ public class RouteManager {
                     LOG.info("Remove a flow from the switch" + InstanceIdentifierUtils.generateNodeInstanceIdentifier(ncr).firstKeyOf(Node.class).getId().getValue());
                     this.flowManager.removeFlow(srcMac, dstMac, new NodeConnectorRef(ncid));
                 }
-                if (routeInfoValue.getLimitedRate() > 0 && routeInfoValue.getBurstSize() > 0) {
-                    try {
-                        this.meterManager.removeMeterFromSwitch(endpoints, ncr, routeInfoValue.getLimitedRate(), routeInfoValue.getBurstSize());
-                    } catch (Exception e) {
-                        LOG.info("Exception occurs when remove the meter from " +
-                                InstanceIdentifierUtils.generateNodeInstanceIdentifier(ncr).firstKeyOf(Node.class).getId().getValue());
-                        return ErrorCodeType.REMOVINGMETTERERROR;
-                    }
-                }
+                if (! removeMeter(endpoints, routeInfoValue, ncr)) return ErrorCodeType.REMOVINGMETERERROR;
             }
         } catch (Exception e) {
             LOG.info("Exception occurs when setup a route: " + e.getMessage());
@@ -152,6 +144,19 @@ public class RouteManager {
         return ErrorCodeType.OK;
     }
 
+    private boolean removeMeter(Endpoints endpoints, RouteInfoValue routeInfoValue, NodeConnectorRef ncr) {
+        if (routeInfoValue.getLimitedRate() > 0 && routeInfoValue.getBurstSize() > 0) {
+            try {
+                this.meterManager.removeMeterFromSwitch(endpoints, ncr, routeInfoValue.getLimitedRate(), routeInfoValue.getBurstSize());
+            } catch (Exception e) {
+                LOG.info("Exception occurs when remove the meter from " +
+                        InstanceIdentifierUtils.generateNodeInstanceIdentifier(ncr).firstKeyOf(Node.class).getId().getValue());
+                return false;
+            }
+        }
+        return true;
+    }
+
     public ErrorCodeType removeRateLimiting(Endpoints endpoints) {
         RouteInfoKey routeInfoKey = new RouteInfoKey(endpoints.getSrc().getValue(), endpoints.getDst().getValue());
         RouteInfoValue routeInfoValue = this.routeInfo.get(routeInfoKey);
@@ -190,20 +195,11 @@ public class RouteManager {
                     } catch (Exception e) {
                         LOG.info("Exception occurs when remove the meter from " +
                                 InstanceIdentifierUtils.generateNodeInstanceIdentifier(ncr).firstKeyOf(Node.class).getId().getValue());
-                        return ErrorCodeType.REMOVINGMETTERERROR;
+                        return ErrorCodeType.REMOVINGMETERERROR;
                     }
 
-                    try {
-                        if (flowLayer == FlowType.L3) {
-                            this.flowManager.writeFlow(srcIp, dstIp, new NodeConnectorRef(ncid), NO_METER_SPECIFIED);
-                        } else if (flowLayer == FlowType.L2) {
-                            this.flowManager.writeFlow(srcMac, dstMac, new NodeConnectorRef(ncid), NO_METER_SPECIFIED);
-                        }
-                    } catch (Exception e) {
-                        LOG.info("Exception occurs when update the flow in " +
-                                InstanceIdentifierUtils.generateNodeInstanceIdentifier(ncr).firstKeyOf(Node.class).getId().getValue());
+                    if (! writeFlow(srcIp, dstIp, srcMac, dstMac, flowLayer, ncid, ncr, NO_METER_SPECIFIED))
                         return ErrorCodeType.SETTINGUPROUTEERROR;
-                    }
                 }
             } catch (Exception e) {
                 LOG.info("Exception occurs when remove a rate limiting: " + e.getMessage());
@@ -250,15 +246,7 @@ public class RouteManager {
                                     new NodeConnectorKey(new NodeConnectorId(nc_value)))
                             .build();
                     NodeConnectorRef ncr = new NodeConnectorRef(ncid);
-                    if (routeInfoValue.getLimitedRate() > 0 && routeInfoValue.getBurstSize() > 0) {
-                        try {
-                            this.meterManager.removeMeterFromSwitch(endpoints, ncr, routeInfoValue.getLimitedRate(), routeInfoValue.getBurstSize());
-                        } catch (Exception e) {
-                            LOG.info("Exception occurs when remove the meter from " +
-                                    InstanceIdentifierUtils.generateNodeInstanceIdentifier(ncr).firstKeyOf(Node.class).getId().getValue());
-                            return ErrorCodeType.REMOVINGMETTERERROR;
-                        }
-                    }
+                    if (! removeMeter(endpoints, routeInfoValue, ncr)) return ErrorCodeType.REMOVINGMETERERROR;
 
                     long meterId = NO_METER_SPECIFIED;
                     if (limitedRate > 0 && burstSize > 0) {
@@ -271,17 +259,8 @@ public class RouteManager {
                         }
                     }
 
-                    try {
-                        if (flowLayer == FlowType.L3) {
-                            this.flowManager.writeFlow(srcIp, dstIp, new NodeConnectorRef(ncid), meterId);
-                        } else if (flowLayer == FlowType.L2) {
-                            this.flowManager.writeFlow(srcMac, dstMac, new NodeConnectorRef(ncid), meterId);
-                        }
-                    } catch (Exception e) {
-                        LOG.info("Exception occurs when update the flow in " +
-                                InstanceIdentifierUtils.generateNodeInstanceIdentifier(ncr).firstKeyOf(Node.class).getId().getValue());
+                    if (! writeFlow(srcIp, dstIp, srcMac, dstMac, flowLayer, ncid, ncr, meterId))
                         return ErrorCodeType.SETTINGUPROUTEERROR;
-                    }
                 }
             } catch (Exception e) {
                 LOG.info("Exception occurs when update a rate limiting: " + e.getMessage());
@@ -293,6 +272,21 @@ public class RouteManager {
         }
     }
 
+    private boolean writeFlow(Ipv4Address srcIp, Ipv4Address dstIp, MacAddress srcMac, MacAddress dstMac, FlowType flowLayer, InstanceIdentifier<NodeConnector> ncid, NodeConnectorRef ncr, long meterId) {
+        try {
+            if (flowLayer == FlowType.L3) {
+                this.flowManager.writeFlow(srcIp, dstIp, new NodeConnectorRef(ncid), meterId);
+            } else if (flowLayer == FlowType.L2) {
+                this.flowManager.writeFlow(srcMac, dstMac, new NodeConnectorRef(ncid), meterId);
+            }
+        } catch (Exception e) {
+            LOG.info("Exception occurs when update the flow in " +
+                    InstanceIdentifierUtils.generateNodeInstanceIdentifier(ncr).firstKeyOf(Node.class).getId().getValue());
+            return false;
+        }
+        return true;
+    }
+
 
     public ErrorCodeType setupRoute(Endpoints endpoints, List<TpId> route, FlowType flowLayer, long limitedRate, long burstSize) {
         Ipv4Address srcIp = endpoints.getSrc();
index 3ffd1f5f6777097c56eff80043fb32d29832ed6a..9e3b1d24399e87e0fa7f445c67a6647ab37ba518 100644 (file)
@@ -29,7 +29,7 @@ public class PathComputationTest {
 
     @Test
     public void onTestMaxBandwidth() {
-        Graph<String, RouteViewer.Path> networkGraph = new SparseMultigraph<>();
+        Graph<String, RouteViewerPath> networkGraph = new SparseMultigraph<>();
         for (int i = 0; i < 5; ++i) {
             networkGraph.addVertex("openflow:"+i);
         }
@@ -40,7 +40,7 @@ public class PathComputationTest {
         addEdge(networkGraph, getTp(4, 1), getTp(5, 0), (long) 10);
         addEdge(networkGraph, getTp(1, 2), getTp(6, 0), (long) 5);
         addEdge(networkGraph, getTp(6, 1), getTp(4, 2), (long) 5);
-        List<RouteViewer.Path> output
+        List<RouteViewerPath> output
                 = pathComputer.maxBandwidth(networkGraph, getNode(0), getNode(5), (long) 4);
         LinkedList<String> result = new LinkedList<>();
         result.add(getTp(0, 0));
@@ -61,15 +61,15 @@ public class PathComputationTest {
         return "openflow:" + i;
     }
 
-    private RouteViewer.Path addEdge (Graph<String, RouteViewer.Path> networkGraph,
-                          String src, String dst, Long bw) {
-        RouteViewer.Path p = new RouteViewer.Path();
+    private RouteViewerPath addEdge (Graph<String, RouteViewerPath> networkGraph,
+                                                 String src, String dst, Long bw) {
+        RouteViewerPath p = new RouteViewerPath();
         p.src = TpId.getDefaultInstance(src);
         p.dst = TpId.getDefaultInstance(dst);
         p.bandwidth = bw;
         networkGraph.addEdge(p, PathComputation.extractNodeId(src), PathComputation.extractNodeId(dst),
                 EdgeType.DIRECTED);
-        p = new RouteViewer.Path();
+        p = new RouteViewerPath();
         p.src = TpId.getDefaultInstance(dst);
         p.dst = TpId.getDefaultInstance(src);
         p.bandwidth = bw;