Refactor: remove duplicated code snippet w modelEntityToJson method
Change-Id: I047c6dda433d76eeb0a761ba0e1525b0dc6a5d5f
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackFloatingIpListCommand.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackFloatingIpListCommand.java
index 66cd2ea..a2ef491 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackFloatingIpListCommand.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackFloatingIpListCommand.java
@@ -18,13 +18,11 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.openstacknetworking.api.OpenstackRouterService;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
import org.openstack4j.model.network.NetFloatingIP;
import org.openstack4j.openstack.networking.domain.NeutronFloatingIP;
@@ -32,6 +30,7 @@
import java.util.List;
import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.modelEntityToJson;
/**
* Lists OpenStack floating IP addresses.
@@ -69,20 +68,8 @@
private JsonNode json(List<NetFloatingIP> floatingIps) {
ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
for (NetFloatingIP floatingIp: floatingIps) {
- result.add(writeFloatingIp(floatingIp));
+ result.add(modelEntityToJson(floatingIp, NeutronFloatingIP.class));
}
return result;
}
-
- private ObjectNode writeFloatingIp(NetFloatingIP floatingIp) {
- try {
- String strFloatingIp = ObjectMapperSingleton.getContext(NeutronFloatingIP.class)
- .writerFor(NeutronFloatingIP.class)
- .writeValueAsString(floatingIp);
- log.trace(strFloatingIp);
- return (ObjectNode) mapper().readTree(strFloatingIp.getBytes());
- } catch (Exception e) {
- throw new IllegalStateException();
- }
- }
}
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackNetworkListCommand.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackNetworkListCommand.java
index 49483f1..401dddc 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackNetworkListCommand.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackNetworkListCommand.java
@@ -18,12 +18,10 @@
import com.fasterxml.jackson.core.JsonProcessingException;
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.Lists;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
import org.openstack4j.model.network.Network;
import org.openstack4j.model.network.Subnet;
import org.openstack4j.openstack.networking.domain.NeutronNetwork;
@@ -33,6 +31,7 @@
import java.util.stream.Collectors;
import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.modelEntityToJson;
/**
* Lists OpenStack networks.
@@ -75,19 +74,8 @@
private JsonNode json(List<Network> networks) {
ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
for (Network net: networks) {
- result.add(writeNetwork(net));
+ result.add(modelEntityToJson(net, NeutronNetwork.class));
}
return result;
}
-
- private ObjectNode writeNetwork(Network net) {
- try {
- String strNet = ObjectMapperSingleton.getContext(NeutronNetwork.class)
- .writerFor(NeutronNetwork.class)
- .writeValueAsString(net);
- return (ObjectNode) mapper().readTree(strNet.getBytes());
- } catch (Exception e) {
- throw new IllegalStateException();
- }
- }
}
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackPortListCommand.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackPortListCommand.java
index e59169c..aa38ffe 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackPortListCommand.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackPortListCommand.java
@@ -18,14 +18,12 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
import org.openstack4j.model.network.IP;
import org.openstack4j.model.network.Network;
import org.openstack4j.model.network.Port;
@@ -36,6 +34,7 @@
import java.util.stream.Collectors;
import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.modelEntityToJson;
/**
* Lists OpenStack ports.
@@ -85,19 +84,8 @@
private JsonNode json(List<Port> ports) {
ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
for (Port port: ports) {
- result.add(writePort(port));
+ result.add(modelEntityToJson(port, NeutronPort.class));
}
return result;
}
-
- private ObjectNode writePort(Port port) {
- try {
- String strPort = ObjectMapperSingleton.getContext(NeutronPort.class)
- .writerFor(NeutronPort.class)
- .writeValueAsString(port);
- return (ObjectNode) mapper().readTree(strPort.getBytes());
- } catch (Exception e) {
- throw new IllegalStateException();
- }
- }
}
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackRouterListCommand.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackRouterListCommand.java
index f72853e..772fa9b 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackRouterListCommand.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackRouterListCommand.java
@@ -18,13 +18,11 @@
import com.fasterxml.jackson.core.JsonProcessingException;
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.Lists;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
import org.onosproject.openstacknetworking.api.OpenstackRouterService;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
import org.openstack4j.model.network.IP;
import org.openstack4j.model.network.Port;
import org.openstack4j.model.network.Router;
@@ -37,6 +35,7 @@
import java.util.stream.Collectors;
import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.modelEntityToJson;
/**
* Lists OpenStack routers.
@@ -100,19 +99,8 @@
private JsonNode json(List<Router> routers) {
ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
for (Router router: routers) {
- result.add(writeRouter(router));
+ result.add(modelEntityToJson(router, NeutronRouter.class));
}
return result;
}
-
- private ObjectNode writeRouter(Router osRouter) {
- try {
- String strNet = ObjectMapperSingleton.getContext(NeutronRouter.class)
- .writerFor(NeutronRouter.class)
- .writeValueAsString(osRouter);
- return (ObjectNode) mapper().readTree(strNet.getBytes());
- } catch (Exception e) {
- throw new IllegalStateException();
- }
- }
}
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSecurityGroupListCommand.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSecurityGroupListCommand.java
index 4cd216d..9342bca 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSecurityGroupListCommand.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSecurityGroupListCommand.java
@@ -18,13 +18,11 @@
import com.fasterxml.jackson.core.JsonProcessingException;
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.Lists;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService;
-import org.openstack4j.core.transport.ObjectMapperSingleton;
import org.openstack4j.model.network.SecurityGroup;
import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
@@ -32,6 +30,7 @@
import java.util.List;
import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
+import static org.onosproject.openstacknetworking.util.OpenstackUtil.modelEntityToJson;
/**
* Lists OpenStack security groups.
@@ -72,19 +71,8 @@
private JsonNode json(List<SecurityGroup> securityGroups) {
ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
for (SecurityGroup sg: securityGroups) {
- result.add(writeSecurityGroup(sg));
+ result.add(modelEntityToJson(sg, NeutronSecurityGroup.class));
}
return result;
}
-
- private ObjectNode writeSecurityGroup(SecurityGroup sg) {
- try {
- String strSg = ObjectMapperSingleton.getContext(NeutronSecurityGroup.class)
- .writerFor(NeutronSecurityGroup.class)
- .writeValueAsString(sg);
- return (ObjectNode) mapper().readTree(strSg.getBytes());
- } catch (Exception e) {
- throw new IllegalArgumentException();
- }
- }
}
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
index ec976c4..5fee7cc 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/util/OpenstackUtil.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/util/OpenstackUtil.java
@@ -17,6 +17,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import org.openstack4j.core.transport.ObjectMapperSingleton;
import org.openstack4j.model.ModelEntity;
import org.slf4j.Logger;
@@ -58,4 +59,24 @@
throw new IllegalArgumentException();
}
}
+
+ /**
+ * Converts openstack model entity object into JSON object.
+ *
+ * @param entity openstack model entity object
+ * @param entityClazz openstack model entity class
+ * @return JSON object
+ */
+ public static ObjectNode modelEntityToJson(ModelEntity entity, Class entityClazz) {
+ ObjectMapper mapper = new ObjectMapper();
+ try {
+ String strModelEntity = ObjectMapperSingleton.getContext(entityClazz)
+ .writerFor(entityClazz)
+ .writeValueAsString(entity);
+ log.trace(strModelEntity);
+ return (ObjectNode) mapper.readTree(strModelEntity.getBytes());
+ } catch (Exception e) {
+ throw new IllegalStateException();
+ }
+ }
}