Allow to specify appId through FlowObjective REST API

Change-Id: Iadff74d379e1d5ec4f6e8ff2cda2ad96892d2cc1
This commit is contained in:
Jian Li 2016-05-30 21:02:33 +09:00 committed by Gerrit Code Review
parent 4b51d5c32e
commit a424a059eb
10 changed files with 74 additions and 27 deletions

View File

@ -20,7 +20,6 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.codec.CodecContext; import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec; import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService; import org.onosproject.core.CoreService;
import org.onosproject.net.flow.TrafficTreatment; import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flow.criteria.Criterion; import org.onosproject.net.flow.criteria.Criterion;
@ -37,7 +36,7 @@ import static org.slf4j.LoggerFactory.getLogger;
/** /**
* Filtering Objective Codec. * Filtering Objective Codec.
*/ */
public class FilteringObjectiveCodec extends JsonCodec<FilteringObjective> { public final class FilteringObjectiveCodec extends JsonCodec<FilteringObjective> {
private final Logger log = getLogger(getClass()); private final Logger log = getLogger(getClass());
// JSON field names // JSON field names
@ -45,6 +44,7 @@ public class FilteringObjectiveCodec extends JsonCodec<FilteringObjective> {
private static final String TYPE = "type"; private static final String TYPE = "type";
private static final String KEY = "key"; private static final String KEY = "key";
private static final String META = "meta"; private static final String META = "meta";
private static final String APP_ID = "appId";
private static final String OPERATION = "operation"; private static final String OPERATION = "operation";
private static final String CONDITIONS = "conditions"; private static final String CONDITIONS = "conditions";
@ -118,9 +118,12 @@ public class FilteringObjectiveCodec extends JsonCodec<FilteringObjective> {
final DefaultFilteringObjective.Builder builder = final DefaultFilteringObjective.Builder builder =
(DefaultFilteringObjective.Builder) och.decode(json, baseBuilder, context); (DefaultFilteringObjective.Builder) och.decode(json, baseBuilder, context);
// application id // application id
ApplicationId appId = coreService.registerApplication(REST_APP_ID); JsonNode appIdJson = json.get(APP_ID);
builder.fromApp(appId); String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
builder.fromApp(coreService.registerApplication(appId));
// decode type // decode type
String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText(); String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();

View File

@ -19,7 +19,6 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.codec.CodecContext; import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec; import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService; import org.onosproject.core.CoreService;
import org.onosproject.net.flow.TrafficSelector; import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment; import org.onosproject.net.flow.TrafficTreatment;
@ -34,11 +33,12 @@ import static org.slf4j.LoggerFactory.getLogger;
/** /**
* Forwarding Objective Codec. * Forwarding Objective Codec.
*/ */
public class ForwardingObjectiveCodec extends JsonCodec<ForwardingObjective> { public final class ForwardingObjectiveCodec extends JsonCodec<ForwardingObjective> {
private final Logger log = getLogger(getClass()); private final Logger log = getLogger(getClass());
// JSON field names // JSON field names
private static final String ID = "id"; private static final String ID = "id";
private static final String APP_ID = "appId";
private static final String SELECTOR = "selector"; private static final String SELECTOR = "selector";
private static final String FLAG = "flag"; private static final String FLAG = "flag";
private static final String OPERATION = "operation"; private static final String OPERATION = "operation";
@ -116,8 +116,9 @@ public class ForwardingObjectiveCodec extends JsonCodec<ForwardingObjective> {
(DefaultForwardingObjective.Builder) och.decode(json, baseBuilder, context); (DefaultForwardingObjective.Builder) och.decode(json, baseBuilder, context);
// application id // application id
ApplicationId appId = coreService.registerApplication(REST_APP_ID); JsonNode appIdJson = json.get(APP_ID);
builder.fromApp(appId); String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
builder.fromApp(coreService.registerApplication(appId));
// decode flag // decode flag
String flagStr = nullIsIllegal(json.get(FLAG), FLAG + MISSING_MEMBER_MESSAGE).asText(); String flagStr = nullIsIllegal(json.get(FLAG), FLAG + MISSING_MEMBER_MESSAGE).asText();

View File

@ -20,7 +20,6 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.codec.CodecContext; import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec; import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService; import org.onosproject.core.CoreService;
import org.onosproject.net.flow.TrafficSelector; import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment; import org.onosproject.net.flow.TrafficTreatment;
@ -37,12 +36,13 @@ import static org.slf4j.LoggerFactory.getLogger;
/** /**
* Next Objective Codec. * Next Objective Codec.
*/ */
public class NextObjectiveCodec extends JsonCodec<NextObjective> { public final class NextObjectiveCodec extends JsonCodec<NextObjective> {
private final Logger log = getLogger(getClass()); private final Logger log = getLogger(getClass());
// JSON field names // JSON field names
private static final String ID = "id"; private static final String ID = "id";
private static final String APP_ID = "appId";
private static final String TYPE = "type"; private static final String TYPE = "type";
private static final String OPERATION = "operation"; private static final String OPERATION = "operation";
private static final String TREATMENTS = "treatments"; private static final String TREATMENTS = "treatments";
@ -121,8 +121,9 @@ public class NextObjectiveCodec extends JsonCodec<NextObjective> {
builder.withId(idJson.asInt()); builder.withId(idJson.asInt());
// decode application id // decode application id
ApplicationId appId = coreService.registerApplication(REST_APP_ID); JsonNode appIdJson = json.get(APP_ID);
builder.fromApp(appId); String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
builder.fromApp(coreService.registerApplication(appId));
// decode type // decode type
String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText(); String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();

View File

@ -21,7 +21,9 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.onlab.packet.VlanId; import org.onlab.packet.VlanId;
import org.onosproject.codec.JsonCodec; import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService; import org.onosproject.core.CoreService;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.net.flow.criteria.Criteria; import org.onosproject.net.flow.criteria.Criteria;
import org.onosproject.net.flow.criteria.Criterion; import org.onosproject.net.flow.criteria.Criterion;
import org.onosproject.net.flowobjective.DefaultFilteringObjective; import org.onosproject.net.flowobjective.DefaultFilteringObjective;
@ -47,6 +49,7 @@ public class FilteringObjectiveCodecTest {
MockCodecContext context; MockCodecContext context;
JsonCodec<FilteringObjective> filteringObjectiveCodec; JsonCodec<FilteringObjective> filteringObjectiveCodec;
final CoreService mockCoreService = createMock(CoreService.class); final CoreService mockCoreService = createMock(CoreService.class);
static final String SAMPLE_APP_ID = "org.onosproject.sample";
/** /**
* Sets up for each test. * Sets up for each test.
@ -58,9 +61,6 @@ public class FilteringObjectiveCodecTest {
filteringObjectiveCodec = context.codec(FilteringObjective.class); filteringObjectiveCodec = context.codec(FilteringObjective.class);
assertThat(filteringObjectiveCodec, notNullValue()); assertThat(filteringObjectiveCodec, notNullValue());
expect(mockCoreService.registerApplication(FilteringObjectiveCodec.REST_APP_ID))
.andReturn(APP_ID).anyTimes();
replay(mockCoreService);
context.registerService(CoreService.class, mockCoreService); context.registerService(CoreService.class, mockCoreService);
} }
@ -93,6 +93,12 @@ public class FilteringObjectiveCodecTest {
*/ */
@Test @Test
public void testFilteringObjectiveDecode() throws IOException { public void testFilteringObjectiveDecode() throws IOException {
ApplicationId appId = new DefaultApplicationId(0, SAMPLE_APP_ID);
expect(mockCoreService.registerApplication(SAMPLE_APP_ID)).andReturn(appId).anyTimes();
replay(mockCoreService);
FilteringObjective filteringObjective = getFilteringObjective("FilteringObjective.json"); FilteringObjective filteringObjective = getFilteringObjective("FilteringObjective.json");
assertThat(filteringObjective.type(), is(FilteringObjective.Type.PERMIT)); assertThat(filteringObjective.type(), is(FilteringObjective.Type.PERMIT));

View File

@ -21,7 +21,9 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.onlab.packet.VlanId; import org.onlab.packet.VlanId;
import org.onosproject.codec.JsonCodec; import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService; import org.onosproject.core.CoreService;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.net.flow.DefaultTrafficSelector; import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.TrafficSelector; import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.criteria.Criteria; import org.onosproject.net.flow.criteria.Criteria;
@ -32,9 +34,7 @@ import org.onosproject.net.flowobjective.ForwardingObjective;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.*;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
@ -49,6 +49,7 @@ public class ForwardingObjectiveCodecTest {
MockCodecContext context; MockCodecContext context;
JsonCodec<ForwardingObjective> forwardingObjectiveCodec; JsonCodec<ForwardingObjective> forwardingObjectiveCodec;
final CoreService mockCoreService = createMock(CoreService.class); final CoreService mockCoreService = createMock(CoreService.class);
static final String SAMPLE_APP_ID = "org.onosproject.sample";
/** /**
* Sets up for each test. * Sets up for each test.
@ -60,9 +61,6 @@ public class ForwardingObjectiveCodecTest {
forwardingObjectiveCodec = context.codec(ForwardingObjective.class); forwardingObjectiveCodec = context.codec(ForwardingObjective.class);
assertThat(forwardingObjectiveCodec, notNullValue()); assertThat(forwardingObjectiveCodec, notNullValue());
expect(mockCoreService.registerApplication(ForwardingObjectiveCodec.REST_APP_ID))
.andReturn(APP_ID).anyTimes();
replay(mockCoreService);
context.registerService(CoreService.class, mockCoreService); context.registerService(CoreService.class, mockCoreService);
} }
@ -97,6 +95,12 @@ public class ForwardingObjectiveCodecTest {
*/ */
@Test @Test
public void testForwardingObjectiveDecode() throws IOException { public void testForwardingObjectiveDecode() throws IOException {
ApplicationId appId = new DefaultApplicationId(0, SAMPLE_APP_ID);
expect(mockCoreService.registerApplication(SAMPLE_APP_ID)).andReturn(appId).anyTimes();
replay(mockCoreService);
ForwardingObjective forwardingObjective = getForwardingObjective("ForwardingObjective.json"); ForwardingObjective forwardingObjective = getForwardingObjective("ForwardingObjective.json");
assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC));
@ -104,6 +108,7 @@ public class ForwardingObjectiveCodecTest {
assertThat(forwardingObjective.timeout(), is(1)); assertThat(forwardingObjective.timeout(), is(1));
assertThat(forwardingObjective.op(), is(ForwardingObjective.Operation.ADD)); assertThat(forwardingObjective.op(), is(ForwardingObjective.Operation.ADD));
assertThat(forwardingObjective.permanent(), is(false)); assertThat(forwardingObjective.permanent(), is(false));
assertThat(forwardingObjective.appId().name(), is(SAMPLE_APP_ID));
} }
/** /**

View File

@ -20,7 +20,9 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.onosproject.codec.JsonCodec; import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService; import org.onosproject.core.CoreService;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.net.flow.DefaultTrafficTreatment; import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficTreatment; import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flowobjective.DefaultNextObjective; import org.onosproject.net.flowobjective.DefaultNextObjective;
@ -46,6 +48,7 @@ public class NextObjectiveCodecTest {
MockCodecContext context; MockCodecContext context;
JsonCodec<NextObjective> nextObjectiveCodec; JsonCodec<NextObjective> nextObjectiveCodec;
final CoreService mockCoreService = createMock(CoreService.class); final CoreService mockCoreService = createMock(CoreService.class);
static final String SAMPLE_APP_ID = "org.onosproject.sample";
/** /**
* Sets up for each test. * Sets up for each test.
@ -57,9 +60,6 @@ public class NextObjectiveCodecTest {
nextObjectiveCodec = context.codec(NextObjective.class); nextObjectiveCodec = context.codec(NextObjective.class);
assertThat(nextObjectiveCodec, notNullValue()); assertThat(nextObjectiveCodec, notNullValue());
expect(mockCoreService.registerApplication(NextObjectiveCodec.REST_APP_ID))
.andReturn(APP_ID).anyTimes();
replay(mockCoreService);
context.registerService(CoreService.class, mockCoreService); context.registerService(CoreService.class, mockCoreService);
} }
@ -89,6 +89,12 @@ public class NextObjectiveCodecTest {
*/ */
@Test @Test
public void testNextObjectiveDecode() throws IOException { public void testNextObjectiveDecode() throws IOException {
ApplicationId appId = new DefaultApplicationId(0, SAMPLE_APP_ID);
expect(mockCoreService.registerApplication(SAMPLE_APP_ID)).andReturn(appId).anyTimes();
replay(mockCoreService);
NextObjective nextObjective = getNextObjective("NextObjective.json"); NextObjective nextObjective = getNextObjective("NextObjective.json");
assertThat(nextObjective.type(), is(NextObjective.Type.FAILOVER)); assertThat(nextObjective.type(), is(NextObjective.Type.FAILOVER));

View File

@ -1,5 +1,6 @@
{ {
"priority": 60, "priority": 60,
"appId": "org.onosproject.sample",
"isPermanent": "false", "isPermanent": "false",
"timeout": 1, "timeout": 1,
"type": "PERMIT", "type": "PERMIT",

View File

@ -1,5 +1,6 @@
{ {
"priority": 60, "priority": 60,
"appId": "org.onosproject.sample",
"isPermanent": "false", "isPermanent": "false",
"timeout": 1, "timeout": 1,
"flag": "SPECIFIC", "flag": "SPECIFIC",

View File

@ -1,5 +1,6 @@
{ {
"id": 1, "id": 1,
"appId": "org.onosproject.sample",
"type": "FAILOVER", "type": "FAILOVER",
"operation": "ADD", "operation": "ADD",
"treatments": [ "treatments": [

View File

@ -30,6 +30,7 @@ import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -57,6 +58,7 @@ public class FlowObjectiveWebResource extends AbstractWebResource {
/** /**
* Creates and installs a new filtering objective for the specified device. * Creates and installs a new filtering objective for the specified device.
* *
* @param appId application identifier
* @param deviceId device identifier * @param deviceId device identifier
* @param stream filtering objective JSON * @param stream filtering objective JSON
* @return status of the request - CREATED if the JSON is correct, * @return status of the request - CREATED if the JSON is correct,
@ -67,12 +69,18 @@ public class FlowObjectiveWebResource extends AbstractWebResource {
@Path("{deviceId}/filter") @Path("{deviceId}/filter")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response createFilteringObjective(@PathParam("deviceId") String deviceId, public Response createFilteringObjective(@QueryParam("appId") String appId,
@PathParam("deviceId") String deviceId,
InputStream stream) { InputStream stream) {
try { try {
UriBuilder locationBuilder = null; UriBuilder locationBuilder = null;
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
if (validateDeviceId(deviceId, jsonTree)) { if (validateDeviceId(deviceId, jsonTree)) {
if (appId != null) {
jsonTree.put("appId", appId);
}
DeviceId did = DeviceId.deviceId(deviceId); DeviceId did = DeviceId.deviceId(deviceId);
FilteringObjective filteringObjective = FilteringObjective filteringObjective =
codec(FilteringObjective.class).decode(jsonTree, this); codec(FilteringObjective.class).decode(jsonTree, this);
@ -94,6 +102,7 @@ public class FlowObjectiveWebResource extends AbstractWebResource {
/** /**
* Creates and installs a new forwarding objective for the specified device. * Creates and installs a new forwarding objective for the specified device.
* *
* @param appId application identifier
* @param deviceId device identifier * @param deviceId device identifier
* @param stream forwarding objective JSON * @param stream forwarding objective JSON
* @return status of the request - CREATED if the JSON is correct, * @return status of the request - CREATED if the JSON is correct,
@ -104,12 +113,18 @@ public class FlowObjectiveWebResource extends AbstractWebResource {
@Path("{deviceId}/forward") @Path("{deviceId}/forward")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response createForwardingObjective(@PathParam("deviceId") String deviceId, public Response createForwardingObjective(@QueryParam("appId") String appId,
@PathParam("deviceId") String deviceId,
InputStream stream) { InputStream stream) {
try { try {
UriBuilder locationBuilder = null; UriBuilder locationBuilder = null;
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
if (validateDeviceId(deviceId, jsonTree)) { if (validateDeviceId(deviceId, jsonTree)) {
if (appId != null) {
jsonTree.put("appId", appId);
}
DeviceId did = DeviceId.deviceId(deviceId); DeviceId did = DeviceId.deviceId(deviceId);
ForwardingObjective forwardingObjective = ForwardingObjective forwardingObjective =
codec(ForwardingObjective.class).decode(jsonTree, this); codec(ForwardingObjective.class).decode(jsonTree, this);
@ -131,6 +146,7 @@ public class FlowObjectiveWebResource extends AbstractWebResource {
/** /**
* Creates and installs a new next objective for the specified device. * Creates and installs a new next objective for the specified device.
* *
* @param appId application identifier
* @param deviceId device identifier * @param deviceId device identifier
* @param stream next objective JSON * @param stream next objective JSON
* @return status of the request - CREATED if the JSON is correct, * @return status of the request - CREATED if the JSON is correct,
@ -141,12 +157,18 @@ public class FlowObjectiveWebResource extends AbstractWebResource {
@Path("{deviceId}/next") @Path("{deviceId}/next")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response createNextObjective(@PathParam("deviceId") String deviceId, public Response createNextObjective(@QueryParam("appId") String appId,
@PathParam("deviceId") String deviceId,
InputStream stream) { InputStream stream) {
try { try {
UriBuilder locationBuilder = null; UriBuilder locationBuilder = null;
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
if (validateDeviceId(deviceId, jsonTree)) { if (validateDeviceId(deviceId, jsonTree)) {
if (appId != null) {
jsonTree.put("appId", appId);
}
DeviceId did = DeviceId.deviceId(deviceId); DeviceId did = DeviceId.deviceId(deviceId);
NextObjective nextObjective = NextObjective nextObjective =
codec(NextObjective.class).decode(jsonTree, this); codec(NextObjective.class).decode(jsonTree, this);