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
Showing
8 changed files
with
327 additions
and
2 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 19 | +import org.onosproject.codec.CodecContext; | ||
| 20 | +import org.onosproject.codec.JsonCodec; | ||
| 21 | +import org.onosproject.core.ApplicationId; | ||
| 22 | +import org.onosproject.core.DefaultApplicationId; | ||
| 23 | + | ||
| 24 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 25 | +import static org.onlab.util.Tools.nullIsIllegal; | ||
| 26 | + | ||
| 27 | +/** | ||
| 28 | + * ApplicationId JSON codec. | ||
| 29 | + */ | ||
| 30 | +public final class ApplicationIdCodec extends JsonCodec<ApplicationId> { | ||
| 31 | + | ||
| 32 | + private static final String APP_ID = "id"; | ||
| 33 | + private static final String APP_NAME = "name"; | ||
| 34 | + | ||
| 35 | + private static final String MISSING_MEMBER_MESSAGE = " member is required in ApplicationId"; | ||
| 36 | + | ||
| 37 | + @Override | ||
| 38 | + public ObjectNode encode(ApplicationId appId, CodecContext context) { | ||
| 39 | + checkNotNull(appId, "ApplicationId cannot be null"); | ||
| 40 | + | ||
| 41 | + ObjectNode result = context.mapper().createObjectNode() | ||
| 42 | + .put("id", appId.id()) | ||
| 43 | + .put("name", appId.name()); | ||
| 44 | + | ||
| 45 | + return result; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + @Override | ||
| 49 | + public ApplicationId decode(ObjectNode json, CodecContext context) { | ||
| 50 | + if (json == null || !json.isObject()) { | ||
| 51 | + return null; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + // parse application identifier | ||
| 55 | + int id = nullIsIllegal(json.get(APP_ID), APP_ID + MISSING_MEMBER_MESSAGE).asInt(); | ||
| 56 | + | ||
| 57 | + // parse application name | ||
| 58 | + String name = nullIsIllegal(json.get(APP_NAME), APP_NAME + MISSING_MEMBER_MESSAGE).asText(); | ||
| 59 | + | ||
| 60 | + return new DefaultApplicationId(id, name); | ||
| 61 | + } | ||
| 62 | +} |
| ... | @@ -27,6 +27,7 @@ import org.onosproject.cluster.RoleInfo; | ... | @@ -27,6 +27,7 @@ import org.onosproject.cluster.RoleInfo; |
| 27 | import org.onosproject.codec.CodecService; | 27 | import org.onosproject.codec.CodecService; |
| 28 | import org.onosproject.codec.JsonCodec; | 28 | import org.onosproject.codec.JsonCodec; |
| 29 | import org.onosproject.core.Application; | 29 | import org.onosproject.core.Application; |
| 30 | +import org.onosproject.core.ApplicationId; | ||
| 30 | import org.onosproject.incubator.net.virtual.TenantId; | 31 | import org.onosproject.incubator.net.virtual.TenantId; |
| 31 | import org.onosproject.incubator.net.virtual.VirtualDevice; | 32 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 32 | import org.onosproject.incubator.net.virtual.VirtualLink; | 33 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| ... | @@ -95,6 +96,7 @@ public class CodecManager implements CodecService { | ... | @@ -95,6 +96,7 @@ public class CodecManager implements CodecService { |
| 95 | public void activate() { | 96 | public void activate() { |
| 96 | codecs.clear(); | 97 | codecs.clear(); |
| 97 | registerCodec(Application.class, new ApplicationCodec()); | 98 | registerCodec(Application.class, new ApplicationCodec()); |
| 99 | + registerCodec(ApplicationId.class, new ApplicationIdCodec()); | ||
| 98 | registerCodec(ControllerNode.class, new ControllerNodeCodec()); | 100 | registerCodec(ControllerNode.class, new ControllerNodeCodec()); |
| 99 | registerCodec(Annotations.class, new AnnotationsCodec()); | 101 | registerCodec(Annotations.class, new AnnotationsCodec()); |
| 100 | registerCodec(Device.class, new DeviceCodec()); | 102 | registerCodec(Device.class, new DeviceCodec()); | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.codec.impl; | ||
| 17 | + | ||
| 18 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 19 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 20 | +import org.hamcrest.Description; | ||
| 21 | +import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
| 22 | +import org.junit.Before; | ||
| 23 | +import org.junit.Test; | ||
| 24 | +import org.onosproject.codec.JsonCodec; | ||
| 25 | +import org.onosproject.core.ApplicationId; | ||
| 26 | +import org.onosproject.core.DefaultApplicationId; | ||
| 27 | + | ||
| 28 | +import java.io.IOException; | ||
| 29 | +import java.io.InputStream; | ||
| 30 | + | ||
| 31 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 32 | +import static org.hamcrest.Matchers.is; | ||
| 33 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 34 | + | ||
| 35 | +/** | ||
| 36 | + * Unit tests for ApplicationId codec. | ||
| 37 | + */ | ||
| 38 | +public final class ApplicationIdCodecTest { | ||
| 39 | + | ||
| 40 | + MockCodecContext context; | ||
| 41 | + JsonCodec<ApplicationId> applicationIdCodec; | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Sets up for each test. Creates a context and fetches the applicationId | ||
| 45 | + * codec. | ||
| 46 | + */ | ||
| 47 | + @Before | ||
| 48 | + public void setUp() { | ||
| 49 | + context = new MockCodecContext(); | ||
| 50 | + applicationIdCodec = context.codec(ApplicationId.class); | ||
| 51 | + assertThat(applicationIdCodec, notNullValue()); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * Tests encoding of an application id object. | ||
| 56 | + */ | ||
| 57 | + @Test | ||
| 58 | + public void testApplicationIdEncode() { | ||
| 59 | + | ||
| 60 | + int id = 1; | ||
| 61 | + String name = "org.onosproject.foo"; | ||
| 62 | + ApplicationId appId = new DefaultApplicationId(id, name); | ||
| 63 | + | ||
| 64 | + ObjectNode applicationIdJson = applicationIdCodec.encode(appId, context); | ||
| 65 | + assertThat(applicationIdJson, ApplicationIdJsonMatcher.matchesApplicationId(appId)); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + /** | ||
| 69 | + * Tests decoding of an application id object. | ||
| 70 | + */ | ||
| 71 | + @Test | ||
| 72 | + public void testApplicationIdDecode() throws IOException { | ||
| 73 | + ApplicationId appId = getApplicationId("ApplicationId.json"); | ||
| 74 | + | ||
| 75 | + assertThat((int) appId.id(), is(1)); | ||
| 76 | + assertThat(appId.name(), is("org.onosproject.foo")); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + private static final class ApplicationIdJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { | ||
| 80 | + | ||
| 81 | + private final ApplicationId applicationId; | ||
| 82 | + | ||
| 83 | + private ApplicationIdJsonMatcher(ApplicationId applicationId) { | ||
| 84 | + this.applicationId = applicationId; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + @Override | ||
| 88 | + protected boolean matchesSafely(JsonNode jsonNode, Description description) { | ||
| 89 | + | ||
| 90 | + // check application role | ||
| 91 | + int jsonAppId = jsonNode.get("id").asInt(); | ||
| 92 | + int appId = applicationId.id(); | ||
| 93 | + if (jsonAppId != appId) { | ||
| 94 | + description.appendText("application ID was " + jsonAppId); | ||
| 95 | + return false; | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + String jsonAppName = jsonNode.get("name").asText(); | ||
| 99 | + String appName = applicationId.name(); | ||
| 100 | + | ||
| 101 | + if (!jsonAppName.equals(appName)) { | ||
| 102 | + description.appendText("application name was " + jsonAppName); | ||
| 103 | + return false; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + return true; | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + @Override | ||
| 110 | + public void describeTo(Description description) { | ||
| 111 | + description.appendText(applicationId.toString()); | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + static ApplicationIdJsonMatcher matchesApplicationId(ApplicationId applicationId) { | ||
| 115 | + return new ApplicationIdJsonMatcher(applicationId); | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + /** | ||
| 120 | + * Reads in a application id from the given resource and decodes it. | ||
| 121 | + * | ||
| 122 | + * @param resourceName resource to use to read the JSON for the rule | ||
| 123 | + * @return decoded application id | ||
| 124 | + * @throws IOException if processing the resource fails | ||
| 125 | + */ | ||
| 126 | + private ApplicationId getApplicationId(String resourceName) throws IOException { | ||
| 127 | + InputStream jsonStream = ApplicationIdCodecTest.class.getResourceAsStream(resourceName); | ||
| 128 | + JsonNode json = context.mapper().readTree(jsonStream); | ||
| 129 | + assertThat(json, notNullValue()); | ||
| 130 | + ApplicationId applicationId = applicationIdCodec.decode((ObjectNode) json, context); | ||
| 131 | + assertThat(applicationId, notNullValue()); | ||
| 132 | + return applicationId; | ||
| 133 | + } | ||
| 134 | +} |
| ... | @@ -18,6 +18,7 @@ package org.onosproject.rest.resources; | ... | @@ -18,6 +18,7 @@ package org.onosproject.rest.resources; |
| 18 | import org.onosproject.app.ApplicationAdminService; | 18 | import org.onosproject.app.ApplicationAdminService; |
| 19 | import org.onosproject.core.Application; | 19 | import org.onosproject.core.Application; |
| 20 | import org.onosproject.core.ApplicationId; | 20 | import org.onosproject.core.ApplicationId; |
| 21 | +import org.onosproject.core.CoreService; | ||
| 21 | import org.onosproject.rest.AbstractWebResource; | 22 | import org.onosproject.rest.AbstractWebResource; |
| 22 | 23 | ||
| 23 | import javax.ws.rs.Consumes; | 24 | import javax.ws.rs.Consumes; |
| ... | @@ -44,8 +45,8 @@ public class ApplicationsWebResource extends AbstractWebResource { | ... | @@ -44,8 +45,8 @@ public class ApplicationsWebResource extends AbstractWebResource { |
| 44 | * Get all installed applications. | 45 | * Get all installed applications. |
| 45 | * Returns array of all installed applications. | 46 | * Returns array of all installed applications. |
| 46 | * | 47 | * |
| 47 | - * @onos.rsModel Applications | ||
| 48 | * @return 200 OK | 48 | * @return 200 OK |
| 49 | + * @onos.rsModel Applications | ||
| 49 | */ | 50 | */ |
| 50 | @GET | 51 | @GET |
| 51 | public Response getApps() { | 52 | public Response getApps() { |
| ... | @@ -57,9 +58,10 @@ public class ApplicationsWebResource extends AbstractWebResource { | ... | @@ -57,9 +58,10 @@ public class ApplicationsWebResource extends AbstractWebResource { |
| 57 | /** | 58 | /** |
| 58 | * Get application details. | 59 | * Get application details. |
| 59 | * Returns details of the specified application. | 60 | * Returns details of the specified application. |
| 60 | - * @onos.rsModel Application | 61 | + * |
| 61 | * @param name application name | 62 | * @param name application name |
| 62 | * @return 200 OK; 404; 401 | 63 | * @return 200 OK; 404; 401 |
| 64 | + * @onos.rsModel Application | ||
| 63 | */ | 65 | */ |
| 64 | @GET | 66 | @GET |
| 65 | @Path("{name}") | 67 | @Path("{name}") |
| ... | @@ -143,9 +145,76 @@ public class ApplicationsWebResource extends AbstractWebResource { | ... | @@ -143,9 +145,76 @@ public class ApplicationsWebResource extends AbstractWebResource { |
| 143 | return response(service, appId); | 145 | return response(service, appId); |
| 144 | } | 146 | } |
| 145 | 147 | ||
| 148 | + /** | ||
| 149 | + * Registers an on or off platform application. | ||
| 150 | + * | ||
| 151 | + * @param name application name | ||
| 152 | + * @return 200 OK; 404; 401 | ||
| 153 | + * @onos.rsModel ApplicationId | ||
| 154 | + */ | ||
| 155 | + @POST | ||
| 156 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 157 | + @Path("{name}/register") | ||
| 158 | + public Response registerAppId(@PathParam("name") String name) { | ||
| 159 | + CoreService service = get(CoreService.class); | ||
| 160 | + ApplicationId appId = service.registerApplication(name); | ||
| 161 | + return response(appId); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + /** | ||
| 165 | + * Gets application Id entry by short id. | ||
| 166 | + * | ||
| 167 | + * @param shortId numerical id of application | ||
| 168 | + * @return 200 OK; 404; 401 | ||
| 169 | + * @onos.rsModel ApplicationId | ||
| 170 | + */ | ||
| 171 | + @GET | ||
| 172 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 173 | + @Path("ids/short") | ||
| 174 | + public Response getAppIdByShortId(@QueryParam("id") int shortId) { | ||
| 175 | + CoreService service = get(CoreService.class); | ||
| 176 | + ApplicationId appId = service.getAppId((short) shortId); | ||
| 177 | + return response(appId); | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + /** | ||
| 181 | + * Gets application Id entry by name. | ||
| 182 | + * | ||
| 183 | + * @param name name of application | ||
| 184 | + * @return 200 OK; 404; 401 | ||
| 185 | + * @onos.rsModel ApplicationId | ||
| 186 | + */ | ||
| 187 | + @GET | ||
| 188 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 189 | + @Path("ids/name") | ||
| 190 | + public Response getAppIdByName(@QueryParam("name") String name) { | ||
| 191 | + CoreService service = get(CoreService.class); | ||
| 192 | + ApplicationId appId = service.getAppId(name); | ||
| 193 | + return response(appId); | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + /** | ||
| 197 | + * Gets a collection of application ids. | ||
| 198 | + * Returns array of all registered application ids. | ||
| 199 | + * | ||
| 200 | + * @return 200 OK; 404; 401 | ||
| 201 | + * @onos.rsModel ApplicationIds | ||
| 202 | + */ | ||
| 203 | + @GET | ||
| 204 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 205 | + @Path("ids") | ||
| 206 | + public Response getAppIds() { | ||
| 207 | + CoreService service = get(CoreService.class); | ||
| 208 | + Set<ApplicationId> appIds = service.getAppIds(); | ||
| 209 | + return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build(); | ||
| 210 | + } | ||
| 211 | + | ||
| 146 | private Response response(ApplicationAdminService service, ApplicationId appId) { | 212 | private Response response(ApplicationAdminService service, ApplicationId appId) { |
| 147 | Application app = service.getApplication(appId); | 213 | Application app = service.getApplication(appId); |
| 148 | return ok(codec(Application.class).encode(app, this)).build(); | 214 | return ok(codec(Application.class).encode(app, this)).build(); |
| 149 | } | 215 | } |
| 150 | 216 | ||
| 217 | + private Response response(ApplicationId appId) { | ||
| 218 | + return ok(codec(ApplicationId.class).encode(appId, this)).build(); | ||
| 219 | + } | ||
| 151 | } | 220 | } | ... | ... |
| 1 | +{ | ||
| 2 | + "type": "object", | ||
| 3 | + "title": "applicationId", | ||
| 4 | + "required": [ | ||
| 5 | + "name", | ||
| 6 | + "id" | ||
| 7 | + ], | ||
| 8 | + "properties": { | ||
| 9 | + "name": { | ||
| 10 | + "type": "string", | ||
| 11 | + "example": "org.onosproject.distributedprimitives" | ||
| 12 | + }, | ||
| 13 | + "id": { | ||
| 14 | + "type": "integer", | ||
| 15 | + "format": "int64", | ||
| 16 | + "example": 1 | ||
| 17 | + } | ||
| 18 | + } | ||
| 19 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | +{ | ||
| 2 | + "type": "object", | ||
| 3 | + "title": "applicationIds", | ||
| 4 | + "required": [ | ||
| 5 | + "applicationIds" | ||
| 6 | + ], | ||
| 7 | + "properties": { | ||
| 8 | + "applicationIds": { | ||
| 9 | + "type": "array", | ||
| 10 | + "xml": { | ||
| 11 | + "name": "applicationIds", | ||
| 12 | + "wrapped": true | ||
| 13 | + }, | ||
| 14 | + "items": { | ||
| 15 | + "type": "object", | ||
| 16 | + "title": "applicationId", | ||
| 17 | + "required": [ | ||
| 18 | + "name", | ||
| 19 | + "id" | ||
| 20 | + ], | ||
| 21 | + "properties": { | ||
| 22 | + "name": { | ||
| 23 | + "type": "string", | ||
| 24 | + "example": "org.onosproject.distributedprimitives" | ||
| 25 | + }, | ||
| 26 | + "id": { | ||
| 27 | + "type": "integer", | ||
| 28 | + "format": "int64", | ||
| 29 | + "example": 1 | ||
| 30 | + } | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment