Fix latency constraint
1. Consider that the type "Duration" in the String is composed by
characters and numbers, it's not easy to convert it to double for
comparison, I modified the latency's annotation to
cfg.latency().toNanos().
2. Exclude two EdgeLinks for the calculation of the whole path's latency
3. The unit of latency in ONOS is not the same, so I set all the latency units to
nanoseconds.
4. Add the latency constraint option for ConnectivityIntentCommand.
Change-Id: Iddf5634880e43ed563db9978659db5eb9ee6c7f8
diff --git a/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java b/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java
index 4101738..665565d 100644
--- a/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java
+++ b/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java
@@ -101,7 +101,7 @@
/**
* Annotation key for latency.
- * The value of this key is expected to be latency in microsecond.
+ * The value of this key is expected to be latency in nanosecond.
*/
public static final String LATENCY = "latency";
diff --git a/core/api/src/main/java/org/onosproject/net/intent/constraint/LatencyConstraint.java b/core/api/src/main/java/org/onosproject/net/intent/constraint/LatencyConstraint.java
index 94ad04d..06959c4 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/constraint/LatencyConstraint.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/constraint/LatencyConstraint.java
@@ -62,7 +62,12 @@
}
private double cost(Link link) {
- return getAnnotatedValue(link, LATENCY);
+ //Check only links, not EdgeLinks
+ if (link.type() != Link.Type.EDGE) {
+ return link.annotations().value(LATENCY) != null ? getAnnotatedValue(link, LATENCY) : 0;
+ } else {
+ return 0;
+ }
}
// doesn't use LinkResourceService
@@ -73,8 +78,9 @@
}
private boolean validate(Path path) {
+ //Guarantee all the latency units in ONOS is nanoseconds.
double pathLatency = path.links().stream().mapToDouble(this::cost).sum();
- return Duration.of((long) pathLatency, ChronoUnit.MICROS).compareTo(latency) <= 0;
+ return Duration.of((long) pathLatency, ChronoUnit.NANOS).compareTo(latency) <= 0;
}
@Override
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java
index c1ee3aa..8b764a9 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java
@@ -91,7 +91,7 @@
*/
@Test
public void testLessThanLatency() {
- sut = new LatencyConstraint(Duration.of(10, ChronoUnit.MICROS));
+ sut = new LatencyConstraint(Duration.of(10, ChronoUnit.NANOS));
assertThat(sut.validate(path, resourceContext), is(true));
}
@@ -101,7 +101,7 @@
*/
@Test
public void testMoreThanLatency() {
- sut = new LatencyConstraint(Duration.of(3, ChronoUnit.MICROS));
+ sut = new LatencyConstraint(Duration.of(3, ChronoUnit.NANOS));
assertThat(sut.validate(path, resourceContext), is(false));
}
@@ -111,7 +111,7 @@
*/
@Test
public void testCost() {
- sut = new LatencyConstraint(Duration.of(10, ChronoUnit.MICROS));
+ sut = new LatencyConstraint(Duration.of(10, ChronoUnit.NANOS));
assertThat(sut.cost(link1, resourceContext), is(closeTo(Double.parseDouble(LATENCY1), 1.0e-6)));
assertThat(sut.cost(link2, resourceContext), is(closeTo(Double.parseDouble(LATENCY2), 1.0e-6)));