Add JSON to CLI commands

- Drivers
- Groups

Change-Id: Ib47dc75d9db839329e6cf8fc4439150848f604f5
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 4e7bde9..50a35c3 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
@@ -33,11 +33,14 @@
 import org.onosproject.net.Link;
 import org.onosproject.net.Path;
 import org.onosproject.net.Port;
+import org.onosproject.net.driver.Driver;
 import org.onosproject.net.flow.FlowEntry;
 import org.onosproject.net.flow.TrafficSelector;
 import org.onosproject.net.flow.TrafficTreatment;
 import org.onosproject.net.flow.criteria.Criterion;
 import org.onosproject.net.flow.instructions.Instruction;
+import org.onosproject.net.group.Group;
+import org.onosproject.net.group.GroupBucket;
 import org.onosproject.net.intent.ConnectivityIntent;
 import org.onosproject.net.intent.Constraint;
 import org.onosproject.net.intent.HostToHostIntent;
@@ -89,6 +92,9 @@
         registerCodec(Topology.class, new TopologyCodec());
         registerCodec(TopologyCluster.class, new TopologyClusterCodec());
         registerCodec(Path.class, new PathCodec());
+        registerCodec(Group.class, new GroupCodec());
+        registerCodec(Driver.class, new DriverCodec());
+        registerCodec(GroupBucket.class, new GroupBucketCodec());
         log.info("Started");
     }
 
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/DriverCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/DriverCodec.java
new file mode 100644
index 0000000..4935d99
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/DriverCodec.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2015 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 org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.driver.Driver;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * JSON codec for the Driver class.
+ */
+public final class DriverCodec extends JsonCodec<Driver> {
+    private static final String PARENT = "parent";
+    private static final String NAME = "name";
+    private static final String MANUFACTURER = "manufacturer";
+    private static final String HW_VERSION = "hwVersion";
+    private static final String SW_VERSION = "swVersion";
+    private static final String BEHAVIOURS = "behaviours";
+    private static final String BEHAVIORS_NAME = "name";
+    private static final String BEHAVIORS_IMPLEMENTATION_NAME = "implementationName";
+    private static final String PROPERTIES = "properties";
+
+    @Override
+    public ObjectNode encode(Driver driver, CodecContext context) {
+        checkNotNull(driver, "Driver cannot be null");
+
+        ObjectNode result = context.mapper().createObjectNode()
+                .put(NAME, driver.name())
+                .put(MANUFACTURER, driver.manufacturer())
+                .put(HW_VERSION, driver.hwVersion())
+                .put(SW_VERSION, driver.swVersion());
+
+        if (driver.parent() != null) {
+            result.put(PARENT, driver.parent().name());
+        }
+
+        ArrayNode behaviours = context.mapper().createArrayNode();
+        driver.behaviours().forEach(behaviour -> {
+            ObjectNode entry = context.mapper().createObjectNode()
+                    .put(BEHAVIORS_NAME, behaviour.getCanonicalName())
+                    .put(BEHAVIORS_IMPLEMENTATION_NAME,
+                            driver.implementation(behaviour).getCanonicalName());
+
+            behaviours.add(entry);
+        });
+        result.set(BEHAVIOURS, behaviours);
+
+        ArrayNode properties = context.mapper().createArrayNode();
+        driver.properties().forEach((name, value) -> {
+            ObjectNode entry = context.mapper().createObjectNode()
+                    .put("name", name)
+                    .put("value", value);
+
+            properties.add(entry);
+        });
+        result.set(PROPERTIES, properties);
+
+        return result;
+    }
+}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/GroupBucketCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/GroupBucketCodec.java
new file mode 100644
index 0000000..c710514
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/GroupBucketCodec.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2015 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 org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.group.GroupBucket;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Group bucket JSON codec.
+ */
+public class GroupBucketCodec extends JsonCodec<GroupBucket> {
+
+    private static final String TYPE = "type";
+    private static final String TREATMENT = "treatment";
+    private static final String WEIGHT = "weight";
+    private static final String WATCH_PORT = "watchPort";
+    private static final String WATCH_GROUP = "watchGroup";
+    private static final String PACKETS = "packets";
+    private static final String BYTES = "bytes";
+
+    @Override
+    public ObjectNode encode(GroupBucket bucket, CodecContext context) {
+        checkNotNull(bucket, "Driver cannot be null");
+
+        ObjectNode result = context.mapper().createObjectNode()
+                .put(TYPE, bucket.type().toString())
+                .put(WEIGHT, bucket.weight())
+                .put(PACKETS, bucket.packets())
+                .put(BYTES, bucket.bytes());
+
+        if (bucket.watchPort() != null) {
+            result.put(WATCH_PORT, bucket.watchPort().toString());
+        }
+
+        if (bucket.watchGroup() != null) {
+            result.put(WATCH_GROUP, bucket.watchGroup().toString());
+        }
+
+        if (bucket.treatment() != null) {
+            result.set(TREATMENT, context.codec(TrafficTreatment.class).encode(bucket.treatment(), context));
+        }
+
+        return result;
+    }
+}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/GroupCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/GroupCodec.java
new file mode 100644
index 0000000..a2f33ce
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/GroupCodec.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2015 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 org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.group.Group;
+import org.onosproject.net.group.GroupBucket;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Group JSON codec.
+ */
+public final class GroupCodec extends JsonCodec<Group> {
+    // JSON field names
+    private static final String ID = "id";
+    private static final String STATE = "state";
+    private static final String LIFE = "life";
+    private static final String PACKETS = "packets";
+    private static final String BYTES = "bytes";
+    private static final String REFERENCE_COUNT = "referenceCount";
+    private static final String TYPE = "type";
+    private static final String DEVICE_ID = "deviceId";
+    private static final String APP_ID = "appId";
+    private static final String APP_COOKIE  = "appCookie";
+    private static final String GIVEN_GROUP_ID  = "givenGroupId";
+    private static final String BUCKETS = "buckets";
+
+    @Override
+    public ObjectNode encode(Group group, CodecContext context) {
+        checkNotNull(group, "Group cannot be null");
+        ObjectNode result = context.mapper().createObjectNode()
+                .put(ID, group.id().toString())
+                .put(STATE, group.state().toString())
+                .put(LIFE, group.life())
+                .put(PACKETS, group.packets())
+                .put(BYTES, group.bytes())
+                .put(REFERENCE_COUNT, group.referenceCount())
+                .put(TYPE, group.type().toString())
+                .put(DEVICE_ID, group.deviceId().toString());
+
+        if (group.appId() != null) {
+            result.put(APP_ID, group.appId().toString());
+        }
+
+        if (group.appCookie() != null) {
+            result.put(APP_COOKIE, group.appCookie().toString());
+        }
+
+        if (group.givenGroupId() != null) {
+            result.put(GIVEN_GROUP_ID, group.givenGroupId());
+        }
+
+        ArrayNode buckets = context.mapper().createArrayNode();
+        group.buckets().buckets().forEach(bucket -> {
+                    ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context);
+                    buckets.add(bucketJson);
+                });
+        result.set(BUCKETS, buckets);
+        return result;
+    }
+}