Add intent state to JSON output
Change-Id: I3a5995e0dfdc83e1253b8e414e825b796a57264c
diff --git a/core/api/src/test/java/org/onosproject/net/intent/IntentServiceAdapter.java b/core/api/src/test/java/org/onosproject/net/intent/IntentServiceAdapter.java
index 3a70fcf..dea1e64 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/IntentServiceAdapter.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/IntentServiceAdapter.java
@@ -54,7 +54,7 @@
@Override
public IntentState getIntentState(Key intentKey) {
- return null;
+ return IntentState.INSTALLED;
}
@Override
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/IntentCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/IntentCodec.java
index ce14c3f..6b50718 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/IntentCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/IntentCodec.java
@@ -19,6 +19,8 @@
import org.onosproject.codec.JsonCodec;
import org.onosproject.net.NetworkResource;
import org.onosproject.net.intent.Intent;
+import org.onosproject.net.intent.IntentService;
+import org.onosproject.net.intent.IntentState;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -44,6 +46,11 @@
for (final NetworkResource resource : intent.resources()) {
jsonResources.add(resource.toString());
}
+
+ IntentService service = context.getService(IntentService.class);
+ IntentState state = service.getIntentState(intent.key());
+ result.put("state", state.toString());
+
return result;
}
}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/IntentCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/IntentCodecTest.java
index 0318b8f..2104654 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/IntentCodecTest.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/IntentCodecTest.java
@@ -18,12 +18,12 @@
import java.time.Duration;
import java.util.List;
+import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onlab.packet.MplsLabel;
import org.onlab.util.Bandwidth;
-import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.DefaultApplicationId;
@@ -39,6 +39,8 @@
import org.onosproject.net.intent.AbstractIntentTest;
import org.onosproject.net.intent.Constraint;
import org.onosproject.net.intent.HostToHostIntent;
+import org.onosproject.net.intent.IntentService;
+import org.onosproject.net.intent.IntentServiceAdapter;
import org.onosproject.net.intent.PointToPointIntent;
import org.onosproject.net.intent.constraint.AnnotationConstraint;
import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
@@ -71,7 +73,13 @@
DefaultTrafficSelector.emptySelector();
final TrafficTreatment emptyTreatment =
DefaultTrafficTreatment.emptyTreatment();
- private final CodecContext context = new MockCodecContext();
+ private final MockCodecContext context = new MockCodecContext();
+
+ @Before
+ public void setUpIntentService() {
+ final IntentService mockIntentService = new IntentServiceAdapter();
+ context.registerService(IntentService.class, mockIntentService);
+ }
/**
* Tests the encoding of a host to host intent.
@@ -109,7 +117,6 @@
.ingressPoint(ingress)
.egressPoint(egress).build();
- final CodecContext context = new MockCodecContext();
final JsonCodec<PointToPointIntent> intentCodec =
context.codec(PointToPointIntent.class);
assertThat(intentCodec, notNullValue());
@@ -165,7 +172,6 @@
.build();
- final CodecContext context = new MockCodecContext();
final JsonCodec<PointToPointIntent> intentCodec =
context.codec(PointToPointIntent.class);
assertThat(intentCodec, notNullValue());
diff --git a/web/api/src/test/java/org/onosproject/rest/IntentsResourceTest.java b/web/api/src/test/java/org/onosproject/rest/IntentsResourceTest.java
index 3307d20..08a6ce5 100644
--- a/web/api/src/test/java/org/onosproject/rest/IntentsResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/IntentsResourceTest.java
@@ -35,6 +35,7 @@
import org.onosproject.net.NetworkResource;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentService;
+import org.onosproject.net.intent.IntentState;
import org.onosproject.net.intent.Key;
import org.onosproject.net.intent.MockIdGenerator;
@@ -44,6 +45,7 @@
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
+import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
@@ -121,6 +123,13 @@
return false;
}
+ // check state field
+ final String jsonState = jsonIntent.get("state").asString();
+ if (!jsonState.equals("INSTALLED")) {
+ reason = "state INSTALLED";
+ return false;
+ }
+
// check resources array
final JsonArray jsonResources = jsonIntent.get("resources").asArray();
if (intent.resources() != null) {
@@ -180,7 +189,7 @@
@Override
public boolean matchesSafely(JsonArray json) {
boolean intentFound = false;
- final int expectedAttributes = 5;
+ final int expectedAttributes = 6;
for (int jsonIntentIndex = 0; jsonIntentIndex < json.size();
jsonIntentIndex++) {
@@ -229,6 +238,9 @@
@Before
public void setUpTest() {
expect(mockIntentService.getIntents()).andReturn(intents).anyTimes();
+ expect(mockIntentService.getIntentState(anyObject()))
+ .andReturn(IntentState.INSTALLED)
+ .anyTimes();
// Register the services needed for the test
final CodecManager codecService = new CodecManager();
codecService.activate();