Fix to prevent a NullPointerException and instead return an invalid link. Occurs when an intent's path is calculated and there are links that are not annotated with the key identified in the intent's AnnotationConstraint.
Change-Id: Iffb15b1f33c474f16bb8b097e2a6c04993add895
(cherry picked from commit 0b3d1aba7ebcb261ab5d2027dd08c0f691e3d88c)
diff --git a/core/api/src/main/java/org/onosproject/net/intent/constraint/AnnotationConstraint.java b/core/api/src/main/java/org/onosproject/net/intent/constraint/AnnotationConstraint.java
index 192eff5..fe91de3 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/constraint/AnnotationConstraint.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/constraint/AnnotationConstraint.java
@@ -76,9 +76,11 @@
}
private boolean isValid(Link link) {
- double value = getAnnotatedValue(link, key);
-
- return value <= threshold;
+ if (link.annotations().value(key) != null) {
+ return getAnnotatedValue(link, key) <= threshold;
+ } else {
+ return false;
+ }
}
// doesn't use LinkResourceService
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java
index afcbace..e21d480 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java
@@ -30,6 +30,7 @@
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;
+import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.onosproject.net.DefaultLinkTest.cp;
import static org.onosproject.net.DeviceId.deviceId;
@@ -47,6 +48,7 @@
private static final PortNumber PID1 = portNumber(1);
private static final PortNumber PID2 = portNumber(2);
private static final String KEY = "distance";
+ private static final String MISSING_KEY = "loss";
private static final double VALUE = 100;
private AnnotationConstraint sut;
@@ -99,4 +101,16 @@
.addEqualityGroup(new AnnotationConstraint("latency", 100))
.testEquals();
}
+
+ /**
+ * Tests the annotated constraint is invalid and has negative cost if the key is not annotated on the link.
+ */
+ @Test
+ public void testNotAnnotated() {
+ sut = new AnnotationConstraint(MISSING_KEY, 80);
+
+ assertThat(link.annotations().value(MISSING_KEY), nullValue());
+ assertThat(sut.isValid(link, resourceContext), is(false));
+ assertThat(sut.cost(link, resourceContext), is(lessThan(0.0)));
+ }
}