ONOS-3658 - Adding REST APIs for query and management of device keys.

Change-Id: I3c70660a15d9085409e43d2d14120d29fd5020be
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 cbed1c2..de74801 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
@@ -53,6 +53,7 @@
 import org.onosproject.net.intent.HostToHostIntent;
 import org.onosproject.net.intent.Intent;
 import org.onosproject.net.intent.PointToPointIntent;
+import org.onosproject.net.key.DeviceKey;
 import org.onosproject.net.mcast.McastRoute;
 import org.onosproject.net.meter.Band;
 import org.onosproject.net.meter.Meter;
@@ -119,6 +120,7 @@
         registerCodec(ForwardingObjective.class, new ForwardingObjectiveCodec());
         registerCodec(NextObjective.class, new NextObjectiveCodec());
         registerCodec(McastRoute.class, new McastRouteCodec());
+        registerCodec(DeviceKey.class, new DeviceKeyCodec());
         log.info("Started");
     }
 
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/DeviceKeyCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/DeviceKeyCodec.java
new file mode 100644
index 0000000..7802ebd
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/DeviceKeyCodec.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2016 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.net.key.DeviceKey;
+import org.onosproject.net.key.DeviceKeyId;
+import org.onosproject.net.key.DeviceKeyService;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Device key JSON codec.
+ */
+public class DeviceKeyCodec extends AnnotatedCodec<DeviceKey> {
+
+    private final Logger log = getLogger(getClass());
+
+    // JSON fieldNames
+    private static final String ID = "id";
+    private static final String TYPE = "type";
+    private static final String LABEL = "label";
+    private static final String COMMUNITY_NAME = "community_name";
+    private static final String USERNAME = "username";
+    private static final String PASSWORD = "password";
+
+
+    @Override
+    public ObjectNode encode(DeviceKey deviceKey, CodecContext context) {
+        checkNotNull(deviceKey, "Device key cannot be null");
+        DeviceKeyService service = context.getService(DeviceKeyService.class);
+        ObjectNode result = context.mapper().createObjectNode()
+                .put(ID, deviceKey.deviceKeyId().id().toString())
+                .put(TYPE, deviceKey.type().toString())
+                .put(LABEL, deviceKey.label().toString());
+
+        if (deviceKey.type().equals(DeviceKey.Type.COMMUNITY_NAME)) {
+            result.put(COMMUNITY_NAME, deviceKey.asCommunityName().name());
+        } else if (deviceKey.type().equals(DeviceKey.Type.USERNAME_PASSWORD)) {
+            result.put(USERNAME, deviceKey.asUsernamePassword().username().toString());
+            result.put(PASSWORD, deviceKey.asUsernamePassword().password().toString());
+        }
+
+        return annotate(result, deviceKey, context);
+    }
+
+    @Override
+    public DeviceKey decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        DeviceKeyId id = DeviceKeyId.deviceKeyId(json.get(ID).asText());
+
+        DeviceKey.Type type = DeviceKey.Type.valueOf(json.get(TYPE).asText());
+        String label = json.get(LABEL).asText();
+
+        if (type.equals(DeviceKey.Type.COMMUNITY_NAME)) {
+            String communityName = json.get(COMMUNITY_NAME).asText();
+            return DeviceKey.createDeviceKeyUsingCommunityName(id, label, communityName);
+        } else if (type.equals(DeviceKey.Type.USERNAME_PASSWORD)) {
+            String username = json.get(USERNAME).asText();
+            String password = json.get(PASSWORD).asText();
+            return DeviceKey.createDeviceKeyUsingUsernamePassword(id, label, username, password);
+        } else {
+            log.error("Unknown device key type: ", type);
+            return null;
+        }
+    }
+}