[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/core/common/src/main/java/org/onosproject/codec/impl/ApplicationIdCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/ApplicationIdCodec.java
new file mode 100644
index 0000000..446be58
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/ApplicationIdCodec.java
@@ -0,0 +1,62 @@
+/*
+ * 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);
+ }
+}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
index 908e8ee..91ad4ff 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
@@ -27,6 +27,7 @@
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 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());
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/ApplicationIdCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/ApplicationIdCodecTest.java
new file mode 100644
index 0000000..b246939
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/ApplicationIdCodecTest.java
@@ -0,0 +1,134 @@
+/*
+ * 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;
+ }
+}
diff --git a/core/common/src/test/resources/org/onosproject/codec/impl/ApplicationId.json b/core/common/src/test/resources/org/onosproject/codec/impl/ApplicationId.json
new file mode 100644
index 0000000..c24c91a
--- /dev/null
+++ b/core/common/src/test/resources/org/onosproject/codec/impl/ApplicationId.json
@@ -0,0 +1,4 @@
+{
+ "id": 1,
+ "name": "org.onosproject.foo"
+}
\ No newline at end of file