Refactor: move method to get annotated double value
- Move to AnnotationKeys to remove code duplication
Change-Id: Icf8b4322fc88e43ab531402d0e403a38d186f48e
diff --git a/core/api/src/main/java/org/onlab/onos/net/AnnotationKeys.java b/core/api/src/main/java/org/onlab/onos/net/AnnotationKeys.java
index eace91c..5777022 100644
--- a/core/api/src/main/java/org/onlab/onos/net/AnnotationKeys.java
+++ b/core/api/src/main/java/org/onlab/onos/net/AnnotationKeys.java
@@ -28,4 +28,23 @@
* Annotation key for latency.
*/
public static final String LATENCY = "latency";
+
+ /**
+ * Returns the value annotated object for the specified annotation key.
+ * The annotated value is expected to be String that can be parsed as double.
+ * If parsing fails, the returned value will be 1.0.
+ *
+ * @param annotated annotated object whose annotated value is obtained
+ * @param key key of annotation
+ * @return double value of annotated object for the specified key
+ */
+ public static double getAnnotatedValue(Annotated annotated, String key) {
+ double value;
+ try {
+ value = Double.parseDouble(annotated.annotations().value(key));
+ } catch (NumberFormatException e) {
+ value = 1.0;
+ }
+ return value;
+ }
}
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/constraint/AnnotationConstraint.java b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/AnnotationConstraint.java
index ac76303..a093be9 100644
--- a/core/api/src/main/java/org/onlab/onos/net/intent/constraint/AnnotationConstraint.java
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/AnnotationConstraint.java
@@ -21,6 +21,8 @@
import java.util.Objects;
+import static org.onlab.onos.net.AnnotationKeys.getAnnotatedValue;
+
/**
* Constraint that evaluates an arbitrary link annotated value is under the specified threshold.
*/
@@ -65,25 +67,6 @@
return value <= threshold;
}
- /**
- * Returns the annotated value of the specified link. The annotated value
- * is expected to be String that can be parsed as double. If parsing fails,
- * the returned value will be 1.0.
- *
- * @param link link whose annotated value is obtained
- * @param key key of link annotation
- * @return double value of link annotation for the specified key
- */
- private double getAnnotatedValue(Link link, String key) {
- double value;
- try {
- value = Double.parseDouble(link.annotations().value(key));
- } catch (NumberFormatException e) {
- value = 1.0;
- }
- return value;
- }
-
@Override
public double cost(Link link, LinkResourceService resourceService) {
if (isValid(link, resourceService)) {
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/constraint/LatencyConstraint.java b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/LatencyConstraint.java
index 45151ef..7b1411a 100644
--- a/core/api/src/main/java/org/onlab/onos/net/intent/constraint/LatencyConstraint.java
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/LatencyConstraint.java
@@ -26,6 +26,7 @@
import java.util.Objects;
import static org.onlab.onos.net.AnnotationKeys.LATENCY;
+import static org.onlab.onos.net.AnnotationKeys.getAnnotatedValue;
/**
* Constraint that evaluates the latency through a path.
@@ -48,16 +49,7 @@
@Override
public double cost(Link link, LinkResourceService resourceService) {
- String value = link.annotations().value(LATENCY);
-
- double latencyInMicroSec;
- try {
- latencyInMicroSec = Double.parseDouble(value);
- } catch (NumberFormatException e) {
- latencyInMicroSec = 1.0;
- }
-
- return latencyInMicroSec;
+ return getAnnotatedValue(link, LATENCY);
}
@Override