[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
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/ApplicationsWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/ApplicationsWebResource.java
index 41e238b..ee6a6b5 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/ApplicationsWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/ApplicationsWebResource.java
@@ -18,6 +18,7 @@
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 @@
* 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 @@
/**
* 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 @@
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();
+ }
}
diff --git a/web/api/src/main/resources/definitions/ApplicationId.json b/web/api/src/main/resources/definitions/ApplicationId.json
new file mode 100644
index 0000000..e02321c
--- /dev/null
+++ b/web/api/src/main/resources/definitions/ApplicationId.json
@@ -0,0 +1,19 @@
+{
+ "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
diff --git a/web/api/src/main/resources/definitions/ApplicationIds.json b/web/api/src/main/resources/definitions/ApplicationIds.json
new file mode 100644
index 0000000..e415a7f
--- /dev/null
+++ b/web/api/src/main/resources/definitions/ApplicationIds.json
@@ -0,0 +1,35 @@
+{
+ "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
diff --git a/web/api/src/test/java/org/onosproject/rest/resources/ApplicationsResourceTest.java b/web/api/src/test/java/org/onosproject/rest/resources/ApplicationsResourceTest.java
index 052c4320..e2a6bb1 100644
--- a/web/api/src/test/java/org/onosproject/rest/resources/ApplicationsResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/resources/ApplicationsResourceTest.java
@@ -22,7 +22,6 @@
import com.google.common.collect.ImmutableSet;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.osgi.ServiceDirectory;
@@ -38,6 +37,7 @@
import org.onosproject.core.Application;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.ApplicationRole;
+import org.onosproject.core.CoreService;
import org.onosproject.core.DefaultApplication;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.core.Version;
@@ -67,21 +67,36 @@
public class ApplicationsResourceTest extends ResourceTest {
- private static class MockCodecContextWithService extends MockCodecContext {
- private ApplicationAdminService service;
+ private static class MockCodecContextWithAppService extends MockCodecContext {
+ private ApplicationAdminService appService;
- MockCodecContextWithService(ApplicationAdminService service) {
- this.service = service;
+ MockCodecContextWithAppService(ApplicationAdminService appService) {
+ this.appService = appService;
}
@Override
@SuppressWarnings("unchecked")
public <T> T getService(Class<T> serviceClass) {
- return (T) service;
+ return (T) appService;
}
}
- private ApplicationAdminService service;
+ private static class MockCodecContextWithCoreService extends MockCodecContext {
+ private CoreService coreService;
+
+ MockCodecContextWithCoreService(CoreService coreService) {
+ this.coreService = coreService;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> T getService(Class<T> serviceClass) {
+ return (T) coreService;
+ }
+ }
+
+ private ApplicationAdminService appService;
+ private CoreService coreService;
private ApplicationId id1 = new DefaultApplicationId(1, "app1");
private ApplicationId id2 = new DefaultApplicationId(2, "app2");
private ApplicationId id3 = new DefaultApplicationId(3, "app3");
@@ -92,28 +107,28 @@
private Application app1 =
new DefaultApplication(id1, VER, "title1",
- "desc1", "origin1", "category1", "url1",
- "readme1", new byte[0], ApplicationRole.ADMIN,
- ImmutableSet.of(), Optional.of(FURL),
- ImmutableList.of("My Feature"), ImmutableList.of());
+ "desc1", "origin1", "category1", "url1",
+ "readme1", new byte[0], ApplicationRole.ADMIN,
+ ImmutableSet.of(), Optional.of(FURL),
+ ImmutableList.of("My Feature"), ImmutableList.of());
private Application app2 =
new DefaultApplication(id2, VER, "title2",
- "desc2", "origin2", "category2", "url2",
- "readme2", new byte[0], ApplicationRole.ADMIN,
- ImmutableSet.of(), Optional.of(FURL),
- ImmutableList.of("My Feature"), ImmutableList.of());
+ "desc2", "origin2", "category2", "url2",
+ "readme2", new byte[0], ApplicationRole.ADMIN,
+ ImmutableSet.of(), Optional.of(FURL),
+ ImmutableList.of("My Feature"), ImmutableList.of());
private Application app3 =
new DefaultApplication(id3, VER, "title3",
- "desc3", "origin3", "category3", "url3",
- "readme3", new byte[0], ApplicationRole.ADMIN,
- ImmutableSet.of(), Optional.of(FURL),
- ImmutableList.of("My Feature"), ImmutableList.of());
+ "desc3", "origin3", "category3", "url3",
+ "readme3", new byte[0], ApplicationRole.ADMIN,
+ ImmutableSet.of(), Optional.of(FURL),
+ ImmutableList.of("My Feature"), ImmutableList.of());
private Application app4 =
new DefaultApplication(id4, VER, "title4",
- "desc4", "origin4", "category4", "url4",
- "readme4", new byte[0], ApplicationRole.ADMIN,
- ImmutableSet.of(), Optional.of(FURL),
- ImmutableList.of("My Feature"), ImmutableList.of());
+ "desc4", "origin4", "category4", "url4",
+ "readme4", new byte[0], ApplicationRole.ADMIN,
+ ImmutableSet.of(), Optional.of(FURL),
+ ImmutableList.of("My Feature"), ImmutableList.of());
/**
* Hamcrest matcher to check that an application representation in JSON matches
@@ -160,6 +175,42 @@
}
/**
+ * Hamcrest matcher to check that an application id representation in JSON.
+ */
+ private static final class AppIdJsonMatcher extends TypeSafeMatcher<JsonObject> {
+ private final ApplicationId appId;
+ private String reason = "";
+
+ private AppIdJsonMatcher(ApplicationId appId) {
+ this.appId = appId;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonObject jsonAppId) {
+ // check id
+ short jsonId = (short) jsonAppId.get("id").asInt();
+ if (jsonId != appId.id()) {
+ reason = "id " + appId.id();
+ return false;
+ }
+
+ // check name
+ String jsonName = jsonAppId.get("name").asString();
+ if (!jsonName.equals(appId.name())) {
+ reason = "name " + appId.name();
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(reason);
+ }
+ }
+
+ /**
* Factory to allocate an application matcher.
*
* @param app application object we are looking for
@@ -170,29 +221,40 @@
}
/**
+ * Factory to allocate an application Id matcher.
+ *
+ * @param appId application Id object we are looking for
+ * @return matcher
+ */
+ private static AppIdJsonMatcher matchesAppId(ApplicationId appId) {
+ return new AppIdJsonMatcher(appId);
+ }
+
+ /**
* Initializes test mocks and environment.
*/
@Before
public void setUpMocks() {
- service = createMock(ApplicationAdminService.class);
+ appService = createMock(ApplicationAdminService.class);
+ coreService = createMock(CoreService.class);
- expect(service.getId("one"))
+ expect(appService.getId("one"))
.andReturn(id1)
.anyTimes();
- expect(service.getId("two"))
+ expect(appService.getId("two"))
.andReturn(id2)
.anyTimes();
- expect(service.getId("three"))
+ expect(appService.getId("three"))
.andReturn(id3)
.anyTimes();
- expect(service.getId("four"))
+ expect(appService.getId("four"))
.andReturn(id4)
.anyTimes();
- expect(service.getApplication(id3))
+ expect(appService.getApplication(id3))
.andReturn(app3)
.anyTimes();
- expect(service.getState(isA(ApplicationId.class)))
+ expect(appService.getState(isA(ApplicationId.class)))
.andReturn(ApplicationState.ACTIVE)
.anyTimes();
@@ -201,33 +263,28 @@
codecService.activate();
ServiceDirectory testDirectory =
new TestServiceDirectory()
- .add(ApplicationAdminService.class, service)
- .add(ApplicationService.class, service)
+ .add(ApplicationAdminService.class, appService)
+ .add(ApplicationService.class, appService)
+ .add(CoreService.class, coreService)
.add(CodecService.class, codecService);
BaseResource.setServiceDirectory(testDirectory);
}
/**
- * Verifies test mocks.
- */
- @After
- public void tearDownMocks() {
- verify(service);
- }
-
- /**
* Tests a GET of all applications when no applications are present.
*/
@Test
public void getAllApplicationsEmpty() {
- expect(service.getApplications())
+ expect(appService.getApplications())
.andReturn(ImmutableSet.of());
- replay(service);
+ replay(appService);
WebTarget wt = target();
String response = wt.path("applications").request().get(String.class);
assertThat(response, is("{\"applications\":[]}"));
+
+ verify(appService);
}
/**
@@ -235,9 +292,9 @@
*/
@Test
public void getAllApplicationsPopulated() {
- expect(service.getApplications())
+ expect(appService.getApplications())
.andReturn(ImmutableSet.of(app1, app2, app3, app4));
- replay(service);
+ replay(appService);
WebTarget wt = target();
String response = wt.path("applications").request().get(String.class);
@@ -257,6 +314,8 @@
assertThat(jsonApps.get(1).asObject(), matchesApp(app2));
assertThat(jsonApps.get(2).asObject(), matchesApp(app3));
assertThat(jsonApps.get(3).asObject(), matchesApp(app4));
+
+ verify(appService);
}
/**
@@ -264,7 +323,7 @@
*/
@Test
public void getSingleApplication() {
- replay(service);
+ replay(appService);
WebTarget wt = target();
String response = wt.path("applications/three").request().get(String.class);
@@ -273,6 +332,8 @@
assertThat(result, notNullValue());
assertThat(result, matchesApp(app3));
+
+ verify(appService);
}
/**
@@ -281,13 +342,15 @@
*/
@Test
public void deleteApplication() {
- service.uninstall(id3);
+ appService.uninstall(id3);
expectLastCall();
- replay(service);
+ replay(appService);
WebTarget wt = target();
wt.path("applications/three").request().delete();
+
+ verify(appService);
}
/**
@@ -296,13 +359,15 @@
*/
@Test
public void deleteActiveApplication() {
- service.deactivate(id3);
+ appService.deactivate(id3);
expectLastCall();
- replay(service);
+ replay(appService);
WebTarget wt = target();
wt.path("applications/three/active").request().delete();
+
+ verify(appService);
}
/**
@@ -311,13 +376,15 @@
*/
@Test
public void postActiveApplication() {
- service.activate(id3);
+ appService.activate(id3);
expectLastCall();
- replay(service);
+ replay(appService);
WebTarget wt = target();
wt.path("applications/three/active").request().post(null);
+
+ verify(appService);
}
/**
@@ -326,15 +393,15 @@
*/
@Test
public void postApplication() {
- expect(service.install(isA(InputStream.class)))
+ expect(appService.install(isA(InputStream.class)))
.andReturn(app4)
.once();
- replay(service);
+ replay(appService);
ApplicationCodec codec = new ApplicationCodec();
String app4Json = codec.encode(app4,
- new MockCodecContextWithService(service))
+ new MockCodecContextWithAppService(appService))
.asText();
WebTarget wt = target();
@@ -345,5 +412,109 @@
assertThat(result, notNullValue());
assertThat(result, matchesApp(app4));
+
+ verify(appService);
+ }
+
+ /**
+ * Tests a POST operation. This should attempt to register an on/off platform
+ * application ID.
+ */
+ @Test
+ public void postRegisterAppId() {
+ expect(coreService.registerApplication("app1")).andReturn(id1).anyTimes();
+ replay(coreService);
+
+ WebTarget wt = target();
+ wt.path("applications/app1/register").request().post(null);
+
+ verify(coreService);
+ }
+
+ /**
+ * Tests a GET of all application Ids when no applications are present.
+ */
+ @Test
+ public void getAllApplicationIdsEmpty() {
+ expect(coreService.getAppIds()).andReturn(ImmutableSet.of());
+ replay(coreService);
+
+ WebTarget wt = target();
+ String response = wt.path("applications/ids").request().get(String.class);
+ assertThat(response, is("{\"applicationIds\":[]}"));
+
+ verify(coreService);
+ }
+
+ /**
+ * Tests a GET of all application Ids.
+ */
+ @Test
+ public void getAllApplicationIdsPopulated() {
+ expect(coreService.getAppIds())
+ .andReturn(ImmutableSet.of(id1, id2, id3, id4));
+ replay(coreService);
+
+ WebTarget wt = target();
+ String response = wt.path("applications/ids").request().get(String.class);
+
+ assertThat(response, containsString("{\"applicationIds\":["));
+
+ JsonObject result = Json.parse(response).asObject();
+ assertThat(result, notNullValue());
+
+ assertThat(result.names(), hasSize(1));
+ assertThat(result.names().get(0), is("applicationIds"));
+
+ JsonArray jsonApps = result.get("applicationIds").asArray();
+ assertThat(jsonApps, notNullValue());
+ assertThat(jsonApps.size(), is(4));
+
+ assertThat(jsonApps.get(0).asObject(), matchesAppId(id1));
+ assertThat(jsonApps.get(1).asObject(), matchesAppId(id2));
+ assertThat(jsonApps.get(2).asObject(), matchesAppId(id3));
+ assertThat(jsonApps.get(3).asObject(), matchesAppId(id4));
+
+ verify(coreService);
+ }
+
+ /**
+ * Tests a GET of an applicationId entry with the given numeric id.
+ */
+ @Test
+ public void getAppIdByShortId() {
+ expect(coreService.getAppId((short) 1)).andReturn(id1);
+ replay(coreService);
+
+ WebTarget wt = target();
+ String response = wt.path("applications/ids/short")
+ .queryParam("id", 1).request().get(String.class);
+
+ JsonObject result = Json.parse(response).asObject();
+ assertThat(result, notNullValue());
+
+ assertThat(result, matchesAppId(id1));
+
+ verify(coreService);
+ }
+
+ /**
+ * Tests a GET of an applicationId entry with the given application name.
+ */
+ @Test
+ public void getAppIdByName() {
+ expect(coreService.getAppId("app2")).andReturn(id2);
+ replay(coreService);
+
+ WebTarget wt = target();
+ String response = wt.path("applications/ids/name")
+ .queryParam("name", "app2").request().get(String.class);
+
+ JsonObject result = Json.parse(response).asObject();
+ assertThat(result, notNullValue());
+
+ assertThat(result, matchesAppId(id2));
+
+ verify(coreService);
}
}