Refactor openstacknode let app use codec to serialize JSON string

Change-Id: I9c3273823fabf18d793246d5e3211fe3124f284b
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java
new file mode 100644
index 0000000..9341725
--- /dev/null
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackNodeCodec.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.openstacknode.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.DeviceId;
+import org.onosproject.openstacknode.api.NodeState;
+import org.onosproject.openstacknode.api.OpenstackNode;
+import org.onosproject.openstacknode.impl.DefaultOpenstackNode;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.onosproject.openstacknode.api.Constants.DATA_IP;
+import static org.onosproject.openstacknode.api.Constants.GATEWAY;
+import static org.onosproject.openstacknode.api.Constants.HOST_NAME;
+import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
+import static org.onosproject.openstacknode.api.Constants.UPLINK_PORT;
+import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Openstack node codec used for serializing and de-serializing JSON string.
+ */
+public final class OpenstackNodeCodec extends JsonCodec<OpenstackNode> {
+
+    private final Logger log = getLogger(getClass());
+
+    private static final String TYPE = "type";
+    private static final String INTEGRATION_BRIDGE = "integrationBridge";
+
+    private static final String MISSING_MESSAGE = " is required in OpenstackNode";
+
+    @Override
+    public ObjectNode encode(OpenstackNode node, CodecContext context) {
+        checkNotNull(node, "Openstack node cannot be null");
+
+        ObjectNode result = context.mapper().createObjectNode()
+                .put(HOST_NAME, node.hostname())
+                .put(TYPE, node.type().name())
+                .put(MANAGEMENT_IP, node.managementIp().toString())
+                .put(INTEGRATION_BRIDGE, node.intgBridge().toString());
+
+        OpenstackNode.NodeType type = node.type();
+
+        if (type == OpenstackNode.NodeType.GATEWAY) {
+            result.put(UPLINK_PORT, node.uplinkPort());
+        }
+
+        if (node.vlanIntf() != null) {
+            result.put(VLAN_INTF_NAME, node.vlanIntf());
+        }
+
+        if (node.dataIp() != null) {
+            result.put(DATA_IP, node.dataIp().toString());
+        }
+
+        return result;
+    }
+
+    @Override
+    public OpenstackNode decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        String hostname = nullIsIllegal(json.get(HOST_NAME).asText(),
+                HOST_NAME + MISSING_MESSAGE);
+        String type = nullIsIllegal(json.get(TYPE).asText(),
+                TYPE + MISSING_MESSAGE);
+        String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
+                MANAGEMENT_IP + MISSING_MESSAGE);
+        String iBridge = nullIsIllegal(json.get(INTEGRATION_BRIDGE).asText(),
+                INTEGRATION_BRIDGE + MISSING_MESSAGE);
+
+        DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder()
+                .hostname(hostname)
+                .type(OpenstackNode.NodeType.valueOf(type))
+                .managementIp(IpAddress.valueOf(mIp))
+                .intgBridge(DeviceId.deviceId(iBridge))
+                .state(NodeState.INIT);
+
+        if (type.equals(GATEWAY)) {
+            nodeBuilder.uplinkPort(nullIsIllegal(json.get(UPLINK_PORT).asText(),
+                    UPLINK_PORT + MISSING_MESSAGE));
+        }
+        if (json.get(VLAN_INTF_NAME) != null) {
+            nodeBuilder.vlanIntf(json.get(VLAN_INTF_NAME).asText());
+        }
+        if (json.get(DATA_IP) != null) {
+            nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
+        }
+
+        log.trace("node is {}", nodeBuilder.build().toString());
+
+        return nodeBuilder.build();
+    }
+}
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/package-info.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/package-info.java
new file mode 100644
index 0000000..98ecbd5
--- /dev/null
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/**
+ * Implementations of the codec broker and openstacknode entity JSON codecs.
+ */
+package org.onosproject.openstacknode.codec;
\ No newline at end of file
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeCodecRegister.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeCodecRegister.java
new file mode 100644
index 0000000..86947d8
--- /dev/null
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeCodecRegister.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.openstacknode.web;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.codec.CodecService;
+import org.onosproject.openstacknode.api.OpenstackNode;
+import org.onosproject.openstacknode.codec.OpenstackNodeCodec;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of the JSON codec brokering service for OpenstackNode.
+ */
+@Component(immediate = true)
+public class OpenstackNodeCodecRegister {
+
+    private final org.slf4j.Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CodecService codecService;
+
+    @Activate
+    public void activate() {
+        codecService.registerCodec(OpenstackNode.class, new OpenstackNodeCodec());
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        codecService.unregisterCodec(OpenstackNode.class);
+
+        log.info("Stopped");
+    }
+}
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java
index 3f8936e..591b1d1 100644
--- a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java
@@ -17,23 +17,13 @@
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.collect.Sets;
 import org.onlab.osgi.DefaultServiceDirectory;
-import org.onlab.packet.IpAddress;
-import org.onosproject.net.DeviceId;
-import org.onosproject.openstacknode.api.NodeState;
 import org.onosproject.openstacknode.api.OpenstackNode;
 import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
 import org.onosproject.openstacknode.api.OpenstackNodeService;
-import org.onosproject.openstacknode.impl.DefaultOpenstackNode;
 import org.onosproject.rest.AbstractWebResource;
-import static org.onosproject.openstacknode.api.Constants.GATEWAY;
-import static org.onosproject.openstacknode.api.Constants.HOST_NAME;
-import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP;
-import static org.onosproject.openstacknode.api.Constants.DATA_IP;
-import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
-import static org.onosproject.openstacknode.api.Constants.UPLINK_PORT;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,9 +54,6 @@
     private static final String UPDATE = "UPDATE";
     private static final String NODE_ID = "NODE_ID";
     private static final String DELETE = "DELETE";
-    private static final String TYPE = "type";
-    private static final String INTEGRATION_BRIDGE = "integrationBridge";
-
 
     private final OpenstackNodeAdminService osNodeAdminService =
             DefaultServiceDirectory.getService(OpenstackNodeAdminService.class);
@@ -143,33 +130,14 @@
              ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
              nodes.forEach(node -> {
                  try {
-                     String hostname = node.get(HOST_NAME).asText();
-                     String type = node.get(TYPE).asText();
-                     String mIp = node.get(MANAGEMENT_IP).asText();
-                     String iBridge = node.get(INTEGRATION_BRIDGE).asText();
+                     ObjectNode objectNode = node.deepCopy();
+                     OpenstackNode openstackNode =
+                             codec(OpenstackNode.class).decode(objectNode, this);
 
-                     DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder()
-                             .hostname(hostname)
-                             .type(OpenstackNode.NodeType.valueOf(type))
-                             .managementIp(IpAddress.valueOf(mIp))
-                             .intgBridge(DeviceId.deviceId(iBridge))
-                             .state(NodeState.INIT);
-
-                     if (type.equals(GATEWAY)) {
-                         nodeBuilder.uplinkPort(node.get(UPLINK_PORT).asText());
-                     }
-                     if (node.get(VLAN_INTF_NAME) != null) {
-                         nodeBuilder.vlanIntf(node.get(VLAN_INTF_NAME).asText());
-                     }
-                     if (node.get(DATA_IP) != null) {
-                         nodeBuilder.dataIp(IpAddress.valueOf(node.get(DATA_IP).asText()));
-                     }
-
-                     log.trace("node is {}", nodeBuilder.build().toString());
-                     nodeSet.add(nodeBuilder.build());
+                     nodeSet.add(openstackNode);
                  } catch (Exception e) {
                      log.error(e.toString());
-                     throw  new IllegalArgumentException();
+                     throw new IllegalArgumentException();
                  }
              });
         } catch (Exception e) {