Cosmetics: Move JSON serializers under web package.
Change-Id: I9ecdfe358c2861463456450cd59cdd9d66d39503
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/DeviceSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/DeviceSerializer.java
new file mode 100644
index 0000000..933fde3
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/DeviceSerializer.java
@@ -0,0 +1,37 @@
+package net.onrc.onos.core.topology.web.serializers;
+
+import java.io.IOException;
+
+import net.onrc.onos.core.topology.Device;
+import net.onrc.onos.core.topology.Port;
+
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.map.ser.std.SerializerBase;
+import org.openflow.util.HexString;
+
+public class DeviceSerializer extends SerializerBase<Device> {
+
+ public DeviceSerializer() {
+ super(Device.class);
+ }
+
+ @Override
+ public void serialize(Device dev, JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider) throws IOException,
+ JsonGenerationException {
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeStringField("mac", dev.getMacAddress().toString());
+ jsonGenerator.writeFieldName("attachmentPoints");
+ jsonGenerator.writeStartArray();
+ for (Port port : dev.getAttachmentPoints()) {
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeStringField("dpid", HexString.toHexString(port.getDpid()));
+ jsonGenerator.writeNumberField("port", port.getNumber());
+ jsonGenerator.writeEndObject();
+ }
+ jsonGenerator.writeEndArray();
+ jsonGenerator.writeEndObject();
+ }
+}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/LinkSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/LinkSerializer.java
new file mode 100644
index 0000000..a287814
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/LinkSerializer.java
@@ -0,0 +1,35 @@
+package net.onrc.onos.core.topology.web.serializers;
+
+import java.io.IOException;
+
+import net.onrc.onos.core.topology.Link;
+
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.map.ser.std.SerializerBase;
+import org.openflow.util.HexString;
+
+public class LinkSerializer extends SerializerBase<Link> {
+
+ public LinkSerializer() {
+ super(Link.class);
+ }
+
+ @Override
+ public void serialize(Link link, JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException, JsonGenerationException {
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeStringField("src-switch",
+ HexString.toHexString(link.getSrcSwitch().getDpid()));
+ jsonGenerator.writeNumberField("src-port",
+ link.getSrcPort().getNumber());
+ jsonGenerator.writeStringField("dst-switch",
+ HexString.toHexString(link.getDstSwitch().getDpid()));
+ jsonGenerator.writeNumberField("dst-port",
+ link.getDstPort().getNumber());
+ jsonGenerator.writeEndObject();
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/PortSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/PortSerializer.java
new file mode 100644
index 0000000..41626b6
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/PortSerializer.java
@@ -0,0 +1,31 @@
+package net.onrc.onos.core.topology.web.serializers;
+
+import java.io.IOException;
+
+import net.onrc.onos.core.topology.Port;
+
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.JsonProcessingException;
+import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.map.ser.std.SerializerBase;
+import org.openflow.util.HexString;
+
+public class PortSerializer extends SerializerBase<Port> {
+
+ public PortSerializer() {
+ super(Port.class);
+ }
+
+ @Override
+ public void serialize(Port port, JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException, JsonProcessingException {
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeStringField("state", "ACTIVE");
+ jsonGenerator.writeStringField("dpid", HexString.toHexString(port.getDpid()));
+ jsonGenerator.writeNumberField("number", port.getNumber());
+ jsonGenerator.writeStringField("desc", port.getDescription());
+ jsonGenerator.writeEndObject();
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/SwitchSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/SwitchSerializer.java
new file mode 100644
index 0000000..e8f85dc
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/SwitchSerializer.java
@@ -0,0 +1,36 @@
+package net.onrc.onos.core.topology.web.serializers;
+
+import java.io.IOException;
+
+import net.onrc.onos.core.topology.Port;
+import net.onrc.onos.core.topology.Switch;
+
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.JsonProcessingException;
+import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.map.ser.std.SerializerBase;
+import org.openflow.util.HexString;
+
+public class SwitchSerializer extends SerializerBase<Switch> {
+
+ public SwitchSerializer() {
+ super(Switch.class);
+ }
+
+ @Override
+ public void serialize(Switch sw, JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider) throws IOException,
+ JsonProcessingException {
+
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeStringField("dpid", HexString.toHexString(sw.getDpid()));
+ jsonGenerator.writeStringField("state", "ACTIVE");
+ jsonGenerator.writeArrayFieldStart("ports");
+ for (Port port : sw.getPorts()) {
+ jsonGenerator.writeObject(port);
+ }
+ jsonGenerator.writeEndArray();
+ jsonGenerator.writeEndObject();
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/TopologySerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/TopologySerializer.java
new file mode 100644
index 0000000..0769805
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/TopologySerializer.java
@@ -0,0 +1,68 @@
+package net.onrc.onos.core.topology.web.serializers;
+
+import net.onrc.onos.core.topology.Device;
+import net.onrc.onos.core.topology.Link;
+import net.onrc.onos.core.topology.Switch;
+import net.onrc.onos.core.topology.Topology;
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.map.ser.std.SerializerBase;
+
+import java.io.IOException;
+
+/**
+ * JSON serializer for Topology objects. Used by REST implementation of the
+ * topology APIs.
+ */
+public class TopologySerializer extends SerializerBase<Topology> {
+
+ /**
+ * Default constructor. Performs basic initialization of the JSON
+ * serializer.
+ */
+ public TopologySerializer() {
+ super(Topology.class);
+ }
+
+ /**
+ * Serialize a Topology object in JSON. The resulting JSON contains the
+ * switches, links and ports provided by the Topology object.
+ *
+ * @param topology the Topology that is being converted to JSON
+ * @param jsonGenerator generator to place the serialized JSON into
+ * @param serializerProvider unused but required for method override
+ * @throws IOException if the JSON serialization process fails
+ */
+ @Override
+ public void serialize(Topology topology,
+ JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException {
+ // Start the object
+ jsonGenerator.writeStartObject();
+
+ // Output the switches array
+ jsonGenerator.writeArrayFieldStart("switches");
+ for (final Switch swtch : topology.getSwitches()) {
+ jsonGenerator.writeObject(swtch);
+ }
+ jsonGenerator.writeEndArray();
+
+ // Output the links array
+ jsonGenerator.writeArrayFieldStart("links");
+ for (final Link link : topology.getLinks()) {
+ jsonGenerator.writeObject(link);
+ }
+ jsonGenerator.writeEndArray();
+
+ // Output the devices array
+ jsonGenerator.writeArrayFieldStart("devices");
+ for (final Device device : topology.getDevices()) {
+ jsonGenerator.writeObject(device);
+ }
+ jsonGenerator.writeEndArray();
+
+ // All done
+ jsonGenerator.writeEndObject();
+ }
+}