Ray Milkey

Add intent state to JSON output

Change-Id: I3a5995e0dfdc83e1253b8e414e825b796a57264c
......@@ -54,7 +54,7 @@ public class IntentServiceAdapter implements IntentService {
@Override
public IntentState getIntentState(Key intentKey) {
return null;
return IntentState.INSTALLED;
}
@Override
......
......@@ -19,6 +19,8 @@ import org.onosproject.codec.CodecContext;
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 @@ public final class IntentCodec extends JsonCodec<Intent> {
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;
}
}
......
......@@ -18,12 +18,12 @@ package org.onosproject.codec.impl;
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.flow.TrafficTreatment;
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 @@ public class IntentCodecTest extends AbstractIntentTest {
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 @@ public class IntentCodecTest extends AbstractIntentTest {
.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 @@ public class IntentCodecTest extends AbstractIntentTest {
.build();
final CodecContext context = new MockCodecContext();
final JsonCodec<PointToPointIntent> intentCodec =
context.codec(PointToPointIntent.class);
assertThat(intentCodec, notNullValue());
......
......@@ -35,6 +35,7 @@ import org.onosproject.core.IdGenerator;
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.eclipsesource.json.JsonValue;
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 @@ public class IntentsResourceTest extends ResourceTest {
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 @@ public class IntentsResourceTest extends ResourceTest {
@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 @@ public class IntentsResourceTest extends ResourceTest {
@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();
......