Sho SHIMIZU

Refactor: move method to get annotated double value

- Move to AnnotationKeys to remove code duplication

Change-Id: Icf8b4322fc88e43ab531402d0e403a38d186f48e
......@@ -28,4 +28,23 @@ public final class AnnotationKeys {
* 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;
}
}
......
......@@ -21,6 +21,8 @@ import org.onlab.onos.net.resource.LinkResourceService;
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 @@ public class AnnotationConstraint extends BooleanConstraint {
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)) {
......
......@@ -26,6 +26,7 @@ import java.time.temporal.ChronoUnit;
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 @@ public class LatencyConstraint implements Constraint {
@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
......