Update the JSON serialization of Topology-related objects.
* Added missing fields (synchronize the JSON format for
Foo and FooEvent objects)
* Cosmetics: use common Javadoc, spacing, etc.
Change-Id: Id7974df08a9adf07aca6b71bfef5e99d953f8b7b
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/HostEventSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/HostEventSerializer.java
index d5a98f0..cfb1247 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/serializers/HostEventSerializer.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/HostEventSerializer.java
@@ -1,6 +1,7 @@
package net.onrc.onos.core.topology.web.serializers;
import java.io.IOException;
+import java.util.Map.Entry;
import net.onrc.onos.core.topology.HostEvent;
import net.onrc.onos.core.topology.TopologyElement;
@@ -11,20 +12,28 @@
import org.codehaus.jackson.map.ser.std.SerializerBase;
/**
- * Serializes HostEvents as JSON.
+ * JSON serializer for HostEvent objects.
*/
public class HostEventSerializer extends SerializerBase<HostEvent> {
-
/**
- * Constructs a HostEvent Serializer.
+ * Default constructor.
*/
public HostEventSerializer() {
super(HostEvent.class);
}
+ /**
+ * Serializes a HostEvent object in JSON.
+ *
+ * @param hostEvent the HostEvent 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(HostEvent hostEvent, JsonGenerator jsonGenerator,
- SerializerProvider serializerProvider) throws IOException {
+ SerializerProvider serializerProvider)
+ throws IOException {
//
// TODO: For now, the JSON format of the serialized output should
@@ -41,6 +50,11 @@
jsonGenerator.writeObject(switchPort);
}
jsonGenerator.writeEndArray();
+ jsonGenerator.writeObjectFieldStart("stringAttributes");
+ for (Entry<String, String> entry : hostEvent.getAllStringAttributes().entrySet()) {
+ jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
+ }
+ jsonGenerator.writeEndObject(); // stringAttributes
jsonGenerator.writeEndObject();
}
}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/HostSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/HostSerializer.java
index 80bbe24..b979922 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/serializers/HostSerializer.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/HostSerializer.java
@@ -11,20 +11,28 @@
import org.codehaus.jackson.map.ser.std.SerializerBase;
/**
- * Serializes Host objects as JSON.
+ * JSON serializer for Host objects.
*/
public class HostSerializer extends SerializerBase<Host> {
-
/**
- * Constructs a Host serializer.
+ * Default constructor.
*/
public HostSerializer() {
super(Host.class);
}
+ /**
+ * Serializes a Host object in JSON.
+ *
+ * @param host the Host 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(Host host, JsonGenerator jsonGenerator,
- SerializerProvider serializerProvider) throws IOException {
+ SerializerProvider serializerProvider)
+ throws IOException {
//
// TODO: For now, the JSON format of the serialized output should
@@ -41,6 +49,17 @@
jsonGenerator.writeObject(port.getSwitchPort());
}
jsonGenerator.writeEndArray();
+ //
+ // NOTE: Class Host itself doesn't have stringAttributes.
+ // Adding empty object for now for consistency with HostEventSerializer
+ //
+ jsonGenerator.writeObjectFieldStart("stringAttributes");
+ /*
+ for (Entry<String, String> entry : host.getAllStringAttributes().entrySet()) {
+ jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
+ }
+ */
+ jsonGenerator.writeEndObject(); // stringAttributes
jsonGenerator.writeEndObject();
}
}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/LinkEventSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/LinkEventSerializer.java
index 47985b0..f57db5c 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/serializers/LinkEventSerializer.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/LinkEventSerializer.java
@@ -1,5 +1,8 @@
package net.onrc.onos.core.topology.web.serializers;
+import java.io.IOException;
+import java.util.Map.Entry;
+
import net.onrc.onos.core.topology.LinkEvent;
import net.onrc.onos.core.topology.TopologyElement;
@@ -7,33 +10,30 @@
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.std.SerializerBase;
-import java.io.IOException;
-
/**
- * JSON serializer for LinkEvents.
+ * JSON serializer for LinkEvents objects.
*/
public class LinkEventSerializer extends SerializerBase<LinkEvent> {
-
/**
- * Public constructor - just calls its super class constructor.
+ * Default constructor.
*/
public LinkEventSerializer() {
super(LinkEvent.class);
}
/**
- * Serializes a LinkEvent object.
+ * Serializes a LinkEvent object in JSON.
*
- * @param linkEvent LinkEvent to serialize
- * @param jsonGenerator generator to add the serialized object to
- * @param serializerProvider not used
- * @throws IOException if the JSON serialization fails
+ * @param linkEvent the LinkEvent 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(final LinkEvent linkEvent,
final JsonGenerator jsonGenerator,
final SerializerProvider serializerProvider)
- throws IOException {
+ throws IOException {
//
// TODO: For now, the JSON format of the serialized output should
@@ -45,6 +45,11 @@
jsonGenerator.writeStringField(TopologyElement.TYPE, linkEvent.getType());
jsonGenerator.writeObjectField("src", linkEvent.getSrc());
jsonGenerator.writeObjectField("dst", linkEvent.getDst());
+ jsonGenerator.writeObjectFieldStart("stringAttributes");
+ for (Entry<String, String> entry : linkEvent.getAllStringAttributes().entrySet()) {
+ jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
+ }
+ jsonGenerator.writeEndObject(); // stringAttributes
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
index 78f344f..bbc08ef 100644
--- 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
@@ -11,27 +11,35 @@
import org.codehaus.jackson.map.ser.std.SerializerBase;
/**
- * Serializes a Link object as JSON.
+ * JSON serializer for Link objects.
*/
public class LinkSerializer extends SerializerBase<Link> {
-
/**
- * Constructs a Link serializer.
+ * Default constructor.
*/
public LinkSerializer() {
super(Link.class);
}
+ /**
+ * Serializes a Link object in JSON.
+ *
+ * @param link the Link 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(Link link, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
- throws IOException {
+ throws IOException {
//
// TODO: For now, the JSON format of the serialized output should
// be same as the JSON format of the corresponding class LinkEvent.
// In the future, we will use a single serializer.
//
+
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(TopologyElement.TYPE, link.getType());
jsonGenerator.writeStringField(TopologyElement.ELEMENT_TYPE, link.getLinkType().toString());
@@ -41,7 +49,7 @@
for (Entry<String, String> entry : link.getAllStringAttributes().entrySet()) {
jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
}
- jsonGenerator.writeEndObject(); // stringAttributes
+ jsonGenerator.writeEndObject(); // stringAttributes
jsonGenerator.writeEndObject();
}
}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/MastershipEventSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/MastershipEventSerializer.java
index 65198f0..296c605 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/serializers/MastershipEventSerializer.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/MastershipEventSerializer.java
@@ -1,5 +1,8 @@
package net.onrc.onos.core.topology.web.serializers;
+import java.io.IOException;
+import java.util.Map.Entry;
+
import net.onrc.onos.core.topology.MastershipEvent;
import net.onrc.onos.core.topology.TopologyElement;
@@ -7,33 +10,31 @@
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.std.SerializerBase;
-import java.io.IOException;
-
/**
- * JSON serializer for MastershipEvents.
+ * JSON serializer for MastershipEvent objects.
*/
public class MastershipEventSerializer extends SerializerBase<MastershipEvent> {
-
/**
- * Public constructor - just calls its super class constructor.
+ * Default constructor.
*/
public MastershipEventSerializer() {
super(MastershipEvent.class);
}
/**
- * Serializes a MastershipEvent object.
+ * Serializes a MastershipEvent object in JSON.
*
- * @param mastershipEvent MastershipEvent to serialize
- * @param jsonGenerator generator to add the serialized object to
- * @param serializerProvider not used
- * @throws IOException if the JSON serialization fails
+ * @param mastershipEvent the MastershipEvent 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(final MastershipEvent mastershipEvent,
final JsonGenerator jsonGenerator,
final SerializerProvider serializerProvider)
- throws IOException {
+ throws IOException {
//
// TODO: For now, the JSON format of the serialized output should
@@ -50,6 +51,11 @@
mastershipEvent.getOnosInstanceId());
jsonGenerator.writeStringField("role",
mastershipEvent.getRole().name());
+ jsonGenerator.writeObjectFieldStart("stringAttributes");
+ for (Entry<String, String> entry : mastershipEvent.getAllStringAttributes().entrySet()) {
+ jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
+ }
+ jsonGenerator.writeEndObject(); // stringAttributes
jsonGenerator.writeEndObject();
}
}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/PortEventSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/PortEventSerializer.java
index 60c984d..8d81d03 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/serializers/PortEventSerializer.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/PortEventSerializer.java
@@ -1,6 +1,7 @@
package net.onrc.onos.core.topology.web.serializers;
import java.io.IOException;
+import java.util.Map.Entry;
import net.onrc.onos.core.topology.PortEvent;
import net.onrc.onos.core.topology.TopologyElement;
@@ -10,21 +11,29 @@
import org.codehaus.jackson.map.ser.std.SerializerBase;
/**
- * Serializes a PortEvent object as JSON.
+ * JSON serializer for PortEvent objects.
*/
public class PortEventSerializer extends SerializerBase<PortEvent> {
-
/**
- * Constructs a PortEvent serializer.
+ * Default constructor.
*/
public PortEventSerializer() {
super(PortEvent.class);
}
+ /**
+ * Serializes a PortEvent object in JSON.
+ *
+ * @param portEvent the PortEvent 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(PortEvent portEvent, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
- throws IOException {
+ throws IOException {
+
//
// TODO: For now, the JSON format of the serialized output should
// be same as the JSON format of the corresponding class Port.
@@ -39,6 +48,11 @@
portEvent.getPortNumber().value());
jsonGenerator.writeStringField("desc",
null /* port.getDescription() */);
+ jsonGenerator.writeObjectFieldStart("stringAttributes");
+ for (Entry<String, String> entry : portEvent.getAllStringAttributes().entrySet()) {
+ jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
+ }
+ jsonGenerator.writeEndObject(); // stringAttributes
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
index 2da7cfd..092336e 100644
--- 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
@@ -11,21 +11,29 @@
import org.codehaus.jackson.map.ser.std.SerializerBase;
/**
- * Serializes a Port object as JSON.
+ * JSON serializer for Port objects.
*/
public class PortSerializer extends SerializerBase<Port> {
-
/**
- * Constructs a Port serializer.
+ * Default constructor.
*/
public PortSerializer() {
super(Port.class);
}
+ /**
+ * Serializes a Port object in JSON.
+ *
+ * @param port the Port 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(Port port, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
- throws IOException {
+ throws IOException {
+
//
// TODO: For now, the JSON format of the serialized output should
// be same as the JSON format of the corresponding class PortEvent.
@@ -44,7 +52,7 @@
for (Entry<String, String> entry : port.getAllStringAttributes().entrySet()) {
jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
}
- jsonGenerator.writeEndObject(); // stringAttributes
+ jsonGenerator.writeEndObject(); // stringAttributes
jsonGenerator.writeEndObject();
}
}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/SwitchEventSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/SwitchEventSerializer.java
index 78e3536..a377d4b 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/serializers/SwitchEventSerializer.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/SwitchEventSerializer.java
@@ -1,6 +1,7 @@
package net.onrc.onos.core.topology.web.serializers;
import java.io.IOException;
+import java.util.Map.Entry;
import net.onrc.onos.core.topology.SwitchEvent;
import net.onrc.onos.core.topology.TopologyElement;
@@ -10,20 +11,29 @@
import org.codehaus.jackson.map.ser.std.SerializerBase;
/**
- * Serializes a SwitchEvent as JSON.
+ * JSON serializer for SwitchEvent objects.
*/
public class SwitchEventSerializer extends SerializerBase<SwitchEvent> {
-
/**
- * Constructs a SwitchEvent serializer.
+ * Default constructor.
*/
public SwitchEventSerializer() {
super(SwitchEvent.class);
}
+ /**
+ * Serializes a SwitchEvent object in JSON.
+ *
+ * @param switchEvent the SwitchEvent 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(SwitchEvent switchEvent, JsonGenerator jsonGenerator,
- SerializerProvider serializerProvider) throws IOException {
+ SerializerProvider serializerProvider)
+ throws IOException {
+
//
// TODO: For now, the JSON format of the serialized output should
// be same as the JSON format of the corresponding class Switch.
@@ -46,6 +56,11 @@
}
*/
jsonGenerator.writeEndArray();
+ jsonGenerator.writeObjectFieldStart("stringAttributes");
+ for (Entry<String, String> entry : switchEvent.getAllStringAttributes().entrySet()) {
+ jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
+ }
+ jsonGenerator.writeEndObject(); // stringAttributes
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
index 3f49a98..df331b7 100644
--- 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
@@ -12,20 +12,29 @@
import org.codehaus.jackson.map.ser.std.SerializerBase;
/**
- * Serializes a Switch object as JSON.
+ * JSON serializer for Switch objects.
*/
public class SwitchSerializer extends SerializerBase<Switch> {
-
/**
- * Constructs a Switch object serializer.
+ * Default constructor.
*/
public SwitchSerializer() {
super(Switch.class);
}
+ /**
+ * Serializes a Switch object in JSON.
+ *
+ * @param sw the Switch 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(Switch sw, JsonGenerator jsonGenerator,
- SerializerProvider serializerProvider) throws IOException {
+ SerializerProvider serializerProvider)
+ throws IOException {
+
//
// TODO: For now, the JSON format of the serialized output should
// be same as the JSON format of the corresponding class SwitchEvent.
@@ -46,7 +55,7 @@
for (Entry<String, String> entry : sw.getAllStringAttributes().entrySet()) {
jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
}
- jsonGenerator.writeEndObject(); // stringAttributes
+ jsonGenerator.writeEndObject(); // stringAttributes
jsonGenerator.writeEndObject();
}
}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/serializers/TopologyEventsSerializer.java b/src/main/java/net/onrc/onos/core/topology/web/serializers/TopologyEventsSerializer.java
index 713969b..1d0a38b 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/serializers/TopologyEventsSerializer.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/serializers/TopologyEventsSerializer.java
@@ -1,5 +1,7 @@
package net.onrc.onos.core.topology.web.serializers;
+import java.io.IOException;
+
import net.onrc.onos.core.topology.HostEvent;
import net.onrc.onos.core.topology.LinkEvent;
import net.onrc.onos.core.topology.PortEvent;
@@ -9,24 +11,19 @@
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.std.SerializerBase;
-import java.io.IOException;
-
/**
- * JSON serializer for TopologyEvents objects. Used by WebSocket
- * implementation of the topology APIs.
+ * JSON serializer for TopologyEvents objects.
*/
public class TopologyEventsSerializer extends SerializerBase<TopologyEvents> {
-
/**
- * Default constructor. Performs basic initialization of the JSON
- * serializer.
+ * Default constructor.
*/
public TopologyEventsSerializer() {
super(TopologyEvents.class);
}
/**
- * Serialize a TopologyEvents object in JSON. The resulting JSON contains
+ * Serializes a TopologyEvents object in JSON. The resulting JSON contains
* the added and removed topology objects: switches, links and ports.
*
* @param topologyEvents the TopologyEvents that is being converted to JSON
@@ -38,7 +35,7 @@
public void serialize(TopologyEvents topologyEvents,
JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
- throws IOException {
+ throws IOException {
// Start the object
jsonGenerator.writeStartObject();
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
index db3bdeb..b241ee4 100644
--- 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
@@ -1,31 +1,29 @@
package net.onrc.onos.core.topology.web.serializers;
+import java.io.IOException;
+
import net.onrc.onos.core.topology.Host;
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.
+ * JSON serializer for Topology objects.
*/
public class TopologySerializer extends SerializerBase<Topology> {
-
/**
- * Default constructor. Performs basic initialization of the JSON
- * serializer.
+ * Default constructor.
*/
public TopologySerializer() {
super(Topology.class);
}
/**
- * Serialize a Topology object in JSON. The resulting JSON contains the
+ * Serializes 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
@@ -37,7 +35,8 @@
public void serialize(Topology topology,
JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
- throws IOException {
+ throws IOException {
+
// Start the object
jsonGenerator.writeStartObject();