Remove some duplicate code in simple-pce 97/66797/2
authorY.Jace Liu <yang.jace.liu@linux.com>
Fri, 29 Dec 2017 02:26:40 +0000 (10:26 +0800)
committerKai Gao <godrickk@gmail.com>
Sat, 3 Feb 2018 08:15:45 +0000 (08:15 +0000)
    There are 2 route checkers: MaxBandwidthPathRouteChecker and
    ShortestRouteChecker. The procedure of constraints check share
    the same code. This patch extract the shared code into a single
    function, and move the function to a parent class.

Change-Id: I65d38a3b6147ffdae7362e6f377f348d7d9a1b7b
Signed-off-by: Y.Jace Liu <yang.jace.liu@linux.com>
alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/MaxBandwidthPathRouteChecker.java
alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/RouteChecker.java
alto-extensions/simple-pce/impl/src/main/java/org/opendaylight/alto/spce/impl/algorithm/ShortestPathRouteChecker.java

index b1aca6dcee96f4186e7dd9aa36e2e74be685322c..034f1be6e0e60dc41b6dcf5cddb09b64a8643829 100644 (file)
@@ -28,30 +28,13 @@ public class MaxBandwidthPathRouteChecker extends RouteChecker {
 
     @Override
     public boolean isStop(List<RouteViewerPath> pathList) {
-        LinkedList<RouteViewerPath> tmp = new LinkedList<>(pathList);
+        List<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(! this.checkConstraint(constraintMetrics, bandwidth, hopcount))
+            return false;
         if (bandwidth > this.bandwidth) {
             this.bandwidth = bandwidth;
             result = tmp;
index 6699f7a8118ff836e452f937ddd6674564c2268c..b4b16819ec311cc1aa9cd25f8db394814eae2b64 100644 (file)
@@ -8,6 +8,9 @@
 
 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.List;
 
 public abstract class RouteChecker {
@@ -21,4 +24,25 @@ public abstract class RouteChecker {
         }
         return result;
     }
+
+    protected boolean checkConstraint(List<ConstraintMetric> constraintMetrics, long bandwidth, long hopcount){
+        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;
+                }
+            }
+        }
+        return true;
+    }
 }
index 1bba1797f2680d7825a3cc03e54e6d644a42378a..1cb9d54a00b69ba8ed01ed34270677c12988c2ec 100644 (file)
@@ -27,29 +27,13 @@ public class ShortestPathRouteChecker extends RouteChecker {
 
     @Override
     public boolean isStop(List<RouteViewerPath> pathList) {
-        LinkedList<RouteViewerPath> tmp = new LinkedList<>(pathList);
+        List<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(! this.checkConstraint(constraintMetrics, bandwidth, hopcount))
+            return false;
         if (hopcount < this.hopcount) {
             this.hopcount = hopcount;
             result = tmp;