Refactor: remove duplicated code snippet w jsonToModelEntity method

Change-Id: I9d8ba515cf36ce3536c41802113cdb11c157a5e5
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/util/OpenstackUtil.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/util/OpenstackUtil.java
new file mode 100644
index 0000000..ec976c4
--- /dev/null
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/util/OpenstackUtil.java
@@ -0,0 +1,61 @@
+/*
+ * 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.openstacknetworking.util;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.openstack4j.core.transport.ObjectMapperSingleton;
+import org.openstack4j.model.ModelEntity;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.InputStream;
+
+import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
+
+/**
+ * An utility that used in openstack networking app.
+ */
+public final class OpenstackUtil {
+
+    protected static final Logger log = LoggerFactory.getLogger(OpenstackUtil.class);
+
+    /**
+     * Prevents object instantiation from external.
+     */
+    private OpenstackUtil() {
+    }
+
+    /**
+     * Interprets JSON string to corresponding openstack model entity object.
+     *
+     * @param input JSON string
+     * @param entityClazz openstack model entity class
+     * @return openstack model entity object
+     */
+    public static ModelEntity jsonToModelEntity(InputStream input, Class entityClazz) {
+        ObjectMapper mapper = new ObjectMapper();
+        try {
+            JsonNode jsonTree = mapper.enable(INDENT_OUTPUT).readTree(input);
+            log.trace(new ObjectMapper().writeValueAsString(jsonTree));
+            return ObjectMapperSingleton.getContext(entityClazz)
+                    .readerFor(entityClazz)
+                    .readValue(jsonTree);
+        } catch (Exception e) {
+            throw new IllegalArgumentException();
+        }
+    }
+}
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/util/package-info.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/util/package-info.java
new file mode 100644
index 0000000..4b6cfb8
--- /dev/null
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/util/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.
+ */
+
+/**
+ * Openstack utility package.
+ */
+package org.onosproject.openstacknetworking.util;
\ No newline at end of file
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResource.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResource.java
index 0132d35..3a1961a 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResource.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackFloatingIpWebResource.java
@@ -16,11 +16,9 @@
 
 package org.onosproject.openstacknetworking.web;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService;
 import org.onosproject.rest.AbstractWebResource;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
 import org.openstack4j.openstack.networking.domain.NeutronFloatingIP;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -39,10 +37,10 @@
 import javax.ws.rs.core.UriInfo;
 import java.io.InputStream;
 
-import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
 import static javax.ws.rs.core.Response.created;
 import static javax.ws.rs.core.Response.noContent;
 import static javax.ws.rs.core.Response.status;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
 
 /**
  * Handles REST API call of Neutron L3 plugin.
@@ -74,7 +72,9 @@
     public Response createFloatingIp(InputStream input) {
         log.trace(String.format(MESSAGE, "CREATE"));
 
-        final NeutronFloatingIP floatingIp = readFloatingIp(input);
+        final NeutronFloatingIP floatingIp = (NeutronFloatingIP)
+                                jsonToModelEntity(input, NeutronFloatingIP.class);
+
         adminService.createFloatingIp(floatingIp);
 
         UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
@@ -100,7 +100,9 @@
     public Response updateFloatingIp(@PathParam("id") String id, InputStream input) {
         log.trace(String.format(MESSAGE, "UPDATE " + id));
 
-        final NeutronFloatingIP floatingIp = readFloatingIp(input);
+        final NeutronFloatingIP floatingIp = (NeutronFloatingIP)
+                                jsonToModelEntity(input, NeutronFloatingIP.class);
+
         adminService.updateFloatingIp(floatingIp);
 
         return status(Response.Status.OK).build();
@@ -121,16 +123,4 @@
         adminService.removeFloatingIp(id);
         return noContent().build();
     }
-
-    private NeutronFloatingIP readFloatingIp(InputStream input) {
-        try {
-            JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
-            log.trace(mapper().writeValueAsString(jsonTree));
-            return ObjectMapperSingleton.getContext(NeutronFloatingIP.class)
-                    .readerFor(NeutronFloatingIP.class)
-                    .readValue(jsonTree);
-        } catch (Exception e) {
-            throw new IllegalArgumentException();
-        }
-    }
 }
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackNetworkWebResource.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackNetworkWebResource.java
index 6f0b4cf..de57311 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackNetworkWebResource.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackNetworkWebResource.java
@@ -15,11 +15,9 @@
  */
 package org.onosproject.openstacknetworking.web;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
 import org.onosproject.rest.AbstractWebResource;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
 import org.openstack4j.openstack.networking.domain.NeutronNetwork;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,10 +36,10 @@
 import javax.ws.rs.core.UriInfo;
 import java.io.InputStream;
 
-import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
 import static javax.ws.rs.core.Response.created;
 import static javax.ws.rs.core.Response.noContent;
 import static javax.ws.rs.core.Response.status;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
 
 /**
  * Handles REST API call of Neutron ML2 plugin.
@@ -73,7 +71,9 @@
     public Response createNetwork(InputStream input) {
         log.trace(String.format(MESSAGE, "CREATE"));
 
-        final NeutronNetwork net = readNetwork(input);
+        final NeutronNetwork net = (NeutronNetwork)
+                             jsonToModelEntity(input, NeutronNetwork.class);
+
         adminService.createNetwork(net);
 
         UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
@@ -99,7 +99,9 @@
     public Response updateNetwork(@PathParam("id") String id, InputStream input) {
         log.trace(String.format(MESSAGE, "UPDATE " + id));
 
-        final NeutronNetwork net = readNetwork(input);
+        final NeutronNetwork net = (NeutronNetwork)
+                             jsonToModelEntity(input, NeutronNetwork.class);
+
         adminService.updateNetwork(net);
 
         return status(Response.Status.OK).build();
@@ -121,16 +123,4 @@
         adminService.removeNetwork(id);
         return noContent().build();
     }
-
-    private NeutronNetwork readNetwork(InputStream input) {
-        try {
-            JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
-            log.trace(mapper().writeValueAsString(jsonTree));
-            return ObjectMapperSingleton.getContext(NeutronNetwork.class)
-                    .readerFor(NeutronNetwork.class)
-                    .readValue(jsonTree);
-        } catch (Exception e) {
-            throw new IllegalArgumentException();
-        }
-    }
 }
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackPortWebResource.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackPortWebResource.java
index e657d4b..79d6ddd 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackPortWebResource.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackPortWebResource.java
@@ -15,11 +15,9 @@
  */
 package org.onosproject.openstacknetworking.web;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
 import org.onosproject.rest.AbstractWebResource;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
 import org.openstack4j.openstack.networking.domain.NeutronPort;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,10 +36,10 @@
 import javax.ws.rs.core.UriInfo;
 import java.io.InputStream;
 
-import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
 import static javax.ws.rs.core.Response.created;
 import static javax.ws.rs.core.Response.noContent;
 import static javax.ws.rs.core.Response.status;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
 
 /**
  * Handles Rest API call from Neutron ML2 plugin.
@@ -73,7 +71,9 @@
     public Response createPorts(InputStream input) {
         log.trace(String.format(MESSAGE, "CREATE"));
 
-        final NeutronPort port = readPort(input);
+        final NeutronPort port = (NeutronPort)
+                                 jsonToModelEntity(input, NeutronPort.class);
+
         adminService.createPort(port);
         UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
                 .path(PORTS)
@@ -98,7 +98,9 @@
     public Response updatePort(@PathParam("id") String id, InputStream input) {
         log.trace(String.format(MESSAGE, "UPDATE " + id));
 
-        final NeutronPort port = readPort(input);
+        final NeutronPort port = (NeutronPort)
+                                 jsonToModelEntity(input, NeutronPort.class);
+
         adminService.updatePort(port);
 
         return status(Response.Status.OK).build();
@@ -120,16 +122,4 @@
         adminService.removePort(id);
         return noContent().build();
     }
-
-    private NeutronPort readPort(InputStream input) {
-        try {
-            JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
-            log.trace(mapper().writeValueAsString(jsonTree));
-            return ObjectMapperSingleton.getContext(NeutronPort.class)
-                    .readerFor(NeutronPort.class)
-                    .readValue(jsonTree);
-        } catch (Exception e) {
-            throw new IllegalArgumentException();
-        }
-    }
 }
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackRouterWebResource.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackRouterWebResource.java
index 5959101..0f6f729 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackRouterWebResource.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackRouterWebResource.java
@@ -15,11 +15,9 @@
  */
 package org.onosproject.openstacknetworking.web;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService;
 import org.onosproject.rest.AbstractWebResource;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
 import org.openstack4j.openstack.networking.domain.NeutronRouter;
 import org.openstack4j.openstack.networking.domain.NeutronRouterInterface;
 import org.slf4j.Logger;
@@ -39,10 +37,10 @@
 import javax.ws.rs.core.UriInfo;
 import java.io.InputStream;
 
-import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
 import static javax.ws.rs.core.Response.created;
 import static javax.ws.rs.core.Response.noContent;
 import static javax.ws.rs.core.Response.status;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
 
 /**
  * Handles REST API call of Neturon L3 plugin.
@@ -76,7 +74,9 @@
     public Response createRouter(InputStream input) {
         log.trace(String.format(MESSAGE_ROUTER, "CREATE"));
 
-        final NeutronRouter osRouter = readRouter(input);
+        final NeutronRouter osRouter = (NeutronRouter)
+                            jsonToModelEntity(input, NeutronRouter.class);
+
         adminService.createRouter(osRouter);
 
         UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
@@ -102,7 +102,9 @@
     public Response updateRouter(@PathParam("id") String id, InputStream input) {
         log.trace(String.format(MESSAGE_ROUTER, "UPDATE " + id));
 
-        final NeutronRouter osRouter = readRouter(input);
+        final NeutronRouter osRouter = (NeutronRouter)
+                            jsonToModelEntity(input, NeutronRouter.class);
+
         osRouter.setId(id);
         adminService.updateRouter(osRouter);
 
@@ -125,7 +127,9 @@
     public Response addRouterInterface(@PathParam("id") String id, InputStream input) {
         log.trace(String.format(MESSAGE_ROUTER_IFACE, "UPDATE " + id));
 
-        final NeutronRouterInterface osRouterIface = readRouterInterface(input);
+        final NeutronRouterInterface osRouterIface = (NeutronRouterInterface)
+                        jsonToModelEntity(input, NeutronRouterInterface.class);
+
         adminService.addRouterInterface(osRouterIface);
 
         return status(Response.Status.OK).build();
@@ -147,7 +151,9 @@
     public Response removeRouterInterface(@PathParam("id") String id, InputStream input) {
         log.trace(String.format(MESSAGE_ROUTER_IFACE, "DELETE " + id));
 
-        final NeutronRouterInterface osRouterIface = readRouterInterface(input);
+        final NeutronRouterInterface osRouterIface = (NeutronRouterInterface)
+                        jsonToModelEntity(input, NeutronRouterInterface.class);
+
         adminService.removeRouterInterface(osRouterIface.getPortId());
 
         return status(Response.Status.OK).build();
@@ -168,28 +174,4 @@
         adminService.removeRouter(id);
         return noContent().build();
     }
-
-    private NeutronRouter readRouter(InputStream input) {
-        try {
-            JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
-            log.trace(mapper().writeValueAsString(jsonTree));
-            return ObjectMapperSingleton.getContext(NeutronRouter.class)
-                    .readerFor(NeutronRouter.class)
-                    .readValue(jsonTree);
-        } catch (Exception e) {
-            throw new IllegalArgumentException();
-        }
-    }
-
-    private NeutronRouterInterface readRouterInterface(InputStream input) {
-        try {
-            JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
-            log.trace(mapper().writeValueAsString(jsonTree));
-            return ObjectMapperSingleton.getContext(NeutronRouterInterface.class)
-                    .readerFor(NeutronRouterInterface.class)
-                    .readValue(jsonTree);
-        } catch (Exception e) {
-            throw new IllegalArgumentException();
-        }
-    }
 }
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSecurityGroupRuleWebResource.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSecurityGroupRuleWebResource.java
index 669a3ad..5f5845b 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSecurityGroupRuleWebResource.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSecurityGroupRuleWebResource.java
@@ -15,11 +15,9 @@
  */
 package org.onosproject.openstacknetworking.web;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
 import org.onosproject.rest.AbstractWebResource;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
 import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -37,9 +35,9 @@
 import javax.ws.rs.core.UriInfo;
 import java.io.InputStream;
 
-import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
 import static javax.ws.rs.core.Response.created;
 import static javax.ws.rs.core.Response.noContent;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
 
 /**
  * Handles Security Group Rule Rest API call from Neutron ML2 plugin.
@@ -71,7 +69,9 @@
     public Response createSecurityGroupRules(InputStream input) {
         log.trace(String.format(MESSAGE, "CREATE"));
 
-        final NeutronSecurityGroupRule sgRule = readSecurityGroupRule(input);
+        final NeutronSecurityGroupRule sgRule = (NeutronSecurityGroupRule)
+                        jsonToModelEntity(input, NeutronSecurityGroupRule.class);
+
         adminService.createSecurityGroupRule(sgRule);
         UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
                 .path(SECURITY_GROUP_RULES)
@@ -96,16 +96,4 @@
         adminService.removeSecurityGroupRule(id);
         return noContent().build();
     }
-
-    private NeutronSecurityGroupRule readSecurityGroupRule(InputStream input) {
-        try {
-            JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
-            log.trace(mapper().writeValueAsString(jsonTree));
-            return ObjectMapperSingleton.getContext(NeutronSecurityGroupRule.class)
-                    .readerFor(NeutronSecurityGroupRule.class)
-                    .readValue(jsonTree);
-        } catch (Exception e) {
-            throw new IllegalArgumentException();
-        }
-    }
 }
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSecurityGroupWebResource.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSecurityGroupWebResource.java
index 7ba4538..b48617e 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSecurityGroupWebResource.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSecurityGroupWebResource.java
@@ -15,11 +15,9 @@
  */
 package org.onosproject.openstacknetworking.web;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
 import org.onosproject.rest.AbstractWebResource;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
 import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,9 +36,9 @@
 import javax.ws.rs.core.UriInfo;
 import java.io.InputStream;
 
-import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
 import static javax.ws.rs.core.Response.created;
 import static javax.ws.rs.core.Response.noContent;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
 
 /**
  * Handles Security Group Rest API call from Neutron ML2 plugin.
@@ -72,7 +70,9 @@
     public Response createSecurityGroups(InputStream input) {
         log.trace(String.format(MESSAGE, "CREATE"));
 
-        final NeutronSecurityGroup sg = readSecurityGroup(input);
+        final NeutronSecurityGroup sg = (NeutronSecurityGroup)
+                            jsonToModelEntity(input, NeutronSecurityGroup.class);
+
         adminService.createSecurityGroup(sg);
         UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
                 .path(SECURITY_GROUPS)
@@ -95,7 +95,9 @@
     public Response updateSecurityGroups(InputStream input) {
         log.trace(String.format(MESSAGE, "UPDATE"));
 
-        final NeutronSecurityGroup sg = readSecurityGroup(input);
+        final NeutronSecurityGroup sg = (NeutronSecurityGroup)
+                            jsonToModelEntity(input, NeutronSecurityGroup.class);
+
         // Do nothing ..
         UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
                 .path(SECURITY_GROUPS)
@@ -120,16 +122,4 @@
         adminService.removeSecurityGroup(id);
         return noContent().build();
     }
-
-    private NeutronSecurityGroup readSecurityGroup(InputStream input) {
-        try {
-            JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
-            log.trace(mapper().writeValueAsString(jsonTree));
-            return ObjectMapperSingleton.getContext(NeutronSecurityGroup.class)
-                    .readerFor(NeutronSecurityGroup.class)
-                    .readValue(jsonTree);
-        } catch (Exception e) {
-            throw new IllegalArgumentException();
-        }
-    }
 }
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSubnetWebResource.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSubnetWebResource.java
index fb6192c..79bc676 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSubnetWebResource.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/web/OpenstackSubnetWebResource.java
@@ -19,11 +19,9 @@
  * Handles Rest API call from Neutron ML2 plugin.
  */
 
-import com.fasterxml.jackson.databind.JsonNode;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
 import org.onosproject.rest.AbstractWebResource;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
 import org.openstack4j.openstack.networking.domain.NeutronSubnet;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -42,10 +40,10 @@
 import javax.ws.rs.core.UriInfo;
 import java.io.InputStream;
 
-import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
 import static javax.ws.rs.core.Response.created;
 import static javax.ws.rs.core.Response.noContent;
 import static javax.ws.rs.core.Response.status;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
 
 @Path("subnets")
 public class OpenstackSubnetWebResource extends AbstractWebResource {
@@ -74,7 +72,9 @@
     public Response createSubnet(InputStream input) {
         log.trace(String.format(MESSAGE, "CREATE"));
 
-        final NeutronSubnet subnet = readSubnet(input);
+        final NeutronSubnet subnet = (NeutronSubnet)
+                            jsonToModelEntity(input, NeutronSubnet.class);
+
         adminService.createSubnet(subnet);
         UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
                 .path(SUBNETS)
@@ -100,7 +100,9 @@
     public Response updateSubnet(@PathParam("id") String id, InputStream input) {
         log.trace(String.format(MESSAGE, "UPDATE " + id));
 
-        final NeutronSubnet subnet = readSubnet(input);
+        final NeutronSubnet subnet = (NeutronSubnet)
+                            jsonToModelEntity(input, NeutronSubnet.class);
+
         adminService.updateSubnet(subnet);
 
         return status(Response.Status.OK).build();
@@ -122,16 +124,4 @@
         adminService.removeSubnet(id);
         return noContent().build();
     }
-
-    private NeutronSubnet readSubnet(InputStream input) {
-        try {
-            JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
-            log.trace(mapper().writeValueAsString(jsonTree));
-            return ObjectMapperSingleton.getContext(NeutronSubnet.class)
-                    .readerFor(NeutronSubnet.class)
-                    .readValue(jsonTree);
-        } catch (Exception e) {
-            throw new IllegalArgumentException();
-        }
-    }
 }