Jian Li
Committed by Gerrit Code Review

[ONOS-4409] Support applicationId registration and query via REST

With this commit, we can register and query on/off platform
applications through REST API.

Change-Id: I82e1e0e55bbc017d6c0cce7d9a6af7a578d7196e
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.codec.impl;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.DefaultApplicationId;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onlab.util.Tools.nullIsIllegal;
/**
* ApplicationId JSON codec.
*/
public final class ApplicationIdCodec extends JsonCodec<ApplicationId> {
private static final String APP_ID = "id";
private static final String APP_NAME = "name";
private static final String MISSING_MEMBER_MESSAGE = " member is required in ApplicationId";
@Override
public ObjectNode encode(ApplicationId appId, CodecContext context) {
checkNotNull(appId, "ApplicationId cannot be null");
ObjectNode result = context.mapper().createObjectNode()
.put("id", appId.id())
.put("name", appId.name());
return result;
}
@Override
public ApplicationId decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
// parse application identifier
int id = nullIsIllegal(json.get(APP_ID), APP_ID + MISSING_MEMBER_MESSAGE).asInt();
// parse application name
String name = nullIsIllegal(json.get(APP_NAME), APP_NAME + MISSING_MEMBER_MESSAGE).asText();
return new DefaultApplicationId(id, name);
}
}
......@@ -27,6 +27,7 @@ import org.onosproject.cluster.RoleInfo;
import org.onosproject.codec.CodecService;
import org.onosproject.codec.JsonCodec;
import org.onosproject.core.Application;
import org.onosproject.core.ApplicationId;
import org.onosproject.incubator.net.virtual.TenantId;
import org.onosproject.incubator.net.virtual.VirtualDevice;
import org.onosproject.incubator.net.virtual.VirtualLink;
......@@ -95,6 +96,7 @@ public class CodecManager implements CodecService {
public void activate() {
codecs.clear();
registerCodec(Application.class, new ApplicationCodec());
registerCodec(ApplicationId.class, new ApplicationIdCodec());
registerCodec(ControllerNode.class, new ControllerNodeCodec());
registerCodec(Annotations.class, new AnnotationsCodec());
registerCodec(Device.class, new DeviceCodec());
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.codec.impl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.DefaultApplicationId;
import java.io.IOException;
import java.io.InputStream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
* Unit tests for ApplicationId codec.
*/
public final class ApplicationIdCodecTest {
MockCodecContext context;
JsonCodec<ApplicationId> applicationIdCodec;
/**
* Sets up for each test. Creates a context and fetches the applicationId
* codec.
*/
@Before
public void setUp() {
context = new MockCodecContext();
applicationIdCodec = context.codec(ApplicationId.class);
assertThat(applicationIdCodec, notNullValue());
}
/**
* Tests encoding of an application id object.
*/
@Test
public void testApplicationIdEncode() {
int id = 1;
String name = "org.onosproject.foo";
ApplicationId appId = new DefaultApplicationId(id, name);
ObjectNode applicationIdJson = applicationIdCodec.encode(appId, context);
assertThat(applicationIdJson, ApplicationIdJsonMatcher.matchesApplicationId(appId));
}
/**
* Tests decoding of an application id object.
*/
@Test
public void testApplicationIdDecode() throws IOException {
ApplicationId appId = getApplicationId("ApplicationId.json");
assertThat((int) appId.id(), is(1));
assertThat(appId.name(), is("org.onosproject.foo"));
}
private static final class ApplicationIdJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
private final ApplicationId applicationId;
private ApplicationIdJsonMatcher(ApplicationId applicationId) {
this.applicationId = applicationId;
}
@Override
protected boolean matchesSafely(JsonNode jsonNode, Description description) {
// check application role
int jsonAppId = jsonNode.get("id").asInt();
int appId = applicationId.id();
if (jsonAppId != appId) {
description.appendText("application ID was " + jsonAppId);
return false;
}
String jsonAppName = jsonNode.get("name").asText();
String appName = applicationId.name();
if (!jsonAppName.equals(appName)) {
description.appendText("application name was " + jsonAppName);
return false;
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText(applicationId.toString());
}
static ApplicationIdJsonMatcher matchesApplicationId(ApplicationId applicationId) {
return new ApplicationIdJsonMatcher(applicationId);
}
}
/**
* Reads in a application id from the given resource and decodes it.
*
* @param resourceName resource to use to read the JSON for the rule
* @return decoded application id
* @throws IOException if processing the resource fails
*/
private ApplicationId getApplicationId(String resourceName) throws IOException {
InputStream jsonStream = ApplicationIdCodecTest.class.getResourceAsStream(resourceName);
JsonNode json = context.mapper().readTree(jsonStream);
assertThat(json, notNullValue());
ApplicationId applicationId = applicationIdCodec.decode((ObjectNode) json, context);
assertThat(applicationId, notNullValue());
return applicationId;
}
}
{
"id": 1,
"name": "org.onosproject.foo"
}
\ No newline at end of file
......@@ -18,6 +18,7 @@ package org.onosproject.rest.resources;
import org.onosproject.app.ApplicationAdminService;
import org.onosproject.core.Application;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.rest.AbstractWebResource;
import javax.ws.rs.Consumes;
......@@ -44,8 +45,8 @@ public class ApplicationsWebResource extends AbstractWebResource {
* Get all installed applications.
* Returns array of all installed applications.
*
* @onos.rsModel Applications
* @return 200 OK
* @onos.rsModel Applications
*/
@GET
public Response getApps() {
......@@ -57,9 +58,10 @@ public class ApplicationsWebResource extends AbstractWebResource {
/**
* Get application details.
* Returns details of the specified application.
* @onos.rsModel Application
*
* @param name application name
* @return 200 OK; 404; 401
* @onos.rsModel Application
*/
@GET
@Path("{name}")
......@@ -143,9 +145,76 @@ public class ApplicationsWebResource extends AbstractWebResource {
return response(service, appId);
}
/**
* Registers an on or off platform application.
*
* @param name application name
* @return 200 OK; 404; 401
* @onos.rsModel ApplicationId
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("{name}/register")
public Response registerAppId(@PathParam("name") String name) {
CoreService service = get(CoreService.class);
ApplicationId appId = service.registerApplication(name);
return response(appId);
}
/**
* Gets application Id entry by short id.
*
* @param shortId numerical id of application
* @return 200 OK; 404; 401
* @onos.rsModel ApplicationId
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("ids/short")
public Response getAppIdByShortId(@QueryParam("id") int shortId) {
CoreService service = get(CoreService.class);
ApplicationId appId = service.getAppId((short) shortId);
return response(appId);
}
/**
* Gets application Id entry by name.
*
* @param name name of application
* @return 200 OK; 404; 401
* @onos.rsModel ApplicationId
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("ids/name")
public Response getAppIdByName(@QueryParam("name") String name) {
CoreService service = get(CoreService.class);
ApplicationId appId = service.getAppId(name);
return response(appId);
}
/**
* Gets a collection of application ids.
* Returns array of all registered application ids.
*
* @return 200 OK; 404; 401
* @onos.rsModel ApplicationIds
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("ids")
public Response getAppIds() {
CoreService service = get(CoreService.class);
Set<ApplicationId> appIds = service.getAppIds();
return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build();
}
private Response response(ApplicationAdminService service, ApplicationId appId) {
Application app = service.getApplication(appId);
return ok(codec(Application.class).encode(app, this)).build();
}
private Response response(ApplicationId appId) {
return ok(codec(ApplicationId.class).encode(appId, this)).build();
}
}
......
{
"type": "object",
"title": "applicationId",
"required": [
"name",
"id"
],
"properties": {
"name": {
"type": "string",
"example": "org.onosproject.distributedprimitives"
},
"id": {
"type": "integer",
"format": "int64",
"example": 1
}
}
}
\ No newline at end of file
{
"type": "object",
"title": "applicationIds",
"required": [
"applicationIds"
],
"properties": {
"applicationIds": {
"type": "array",
"xml": {
"name": "applicationIds",
"wrapped": true
},
"items": {
"type": "object",
"title": "applicationId",
"required": [
"name",
"id"
],
"properties": {
"name": {
"type": "string",
"example": "org.onosproject.distributedprimitives"
},
"id": {
"type": "integer",
"format": "int64",
"example": 1
}
}
}
}
}
}
\ No newline at end of file