[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());