Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology.web.serializers; |
| 2 | |
| 3 | import net.onrc.onos.core.topology.DeviceEvent; |
| 4 | import net.onrc.onos.core.topology.LinkEvent; |
| 5 | import net.onrc.onos.core.topology.PortEvent; |
| 6 | import net.onrc.onos.core.topology.SwitchEvent; |
| 7 | import net.onrc.onos.core.topology.TopologyEvents; |
| 8 | import org.codehaus.jackson.JsonGenerator; |
| 9 | import org.codehaus.jackson.map.SerializerProvider; |
| 10 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
| 11 | |
| 12 | import java.io.IOException; |
| 13 | |
| 14 | /** |
| 15 | * JSON serializer for TopologyEvents objects. Used by WebSocket |
| 16 | * implementation of the topology APIs. |
| 17 | */ |
| 18 | public class TopologyEventsSerializer extends SerializerBase<TopologyEvents> { |
| 19 | |
| 20 | /** |
| 21 | * Default constructor. Performs basic initialization of the JSON |
| 22 | * serializer. |
| 23 | */ |
| 24 | public TopologyEventsSerializer() { |
| 25 | super(TopologyEvents.class); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Serialize a TopologyEvents object in JSON. The resulting JSON contains |
| 30 | * the added and removed topology objects: switches, links and ports. |
| 31 | * |
| 32 | * @param topologyEvents the TopologyEvents that is being converted to JSON |
| 33 | * @param jsonGenerator generator to place the serialized JSON into |
| 34 | * @param serializerProvider unused but required for method override |
| 35 | * @throws IOException if the JSON serialization process fails |
| 36 | */ |
| 37 | @Override |
| 38 | public void serialize(TopologyEvents topologyEvents, |
| 39 | JsonGenerator jsonGenerator, |
| 40 | SerializerProvider serializerProvider) |
| 41 | throws IOException { |
| 42 | |
| 43 | // Start the object |
| 44 | jsonGenerator.writeStartObject(); |
| 45 | |
| 46 | // Output the added switches array |
| 47 | jsonGenerator.writeArrayFieldStart("addedSwitches"); |
| 48 | for (final SwitchEvent switchEvent : topologyEvents.getAddedSwitchEvents()) { |
| 49 | jsonGenerator.writeObject(switchEvent); |
| 50 | } |
| 51 | jsonGenerator.writeEndArray(); |
| 52 | |
| 53 | // Output the removed switches array |
| 54 | jsonGenerator.writeArrayFieldStart("removedSwitches"); |
| 55 | for (final SwitchEvent switchEvent : topologyEvents.getRemovedSwitchEvents()) { |
| 56 | jsonGenerator.writeObject(switchEvent); |
| 57 | } |
| 58 | jsonGenerator.writeEndArray(); |
| 59 | |
| 60 | // Output the added ports array |
| 61 | jsonGenerator.writeArrayFieldStart("addedPorts"); |
| 62 | for (final PortEvent portEvent : topologyEvents.getAddedPortEvents()) { |
| 63 | jsonGenerator.writeObject(portEvent); |
| 64 | } |
| 65 | jsonGenerator.writeEndArray(); |
| 66 | |
| 67 | // Output the removed ports array |
| 68 | jsonGenerator.writeArrayFieldStart("removedPorts"); |
| 69 | for (final PortEvent portEvent : topologyEvents.getRemovedPortEvents()) { |
| 70 | jsonGenerator.writeObject(portEvent); |
| 71 | } |
| 72 | jsonGenerator.writeEndArray(); |
| 73 | |
| 74 | // Output the added links array |
| 75 | jsonGenerator.writeArrayFieldStart("addedLinks"); |
| 76 | for (final LinkEvent linkEvent : topologyEvents.getAddedLinkEvents()) { |
| 77 | jsonGenerator.writeObject(linkEvent); |
| 78 | } |
| 79 | jsonGenerator.writeEndArray(); |
| 80 | |
| 81 | // Output the removed links array |
| 82 | jsonGenerator.writeArrayFieldStart("removedLinks"); |
| 83 | for (final LinkEvent linkEvent : topologyEvents.getRemovedLinkEvents()) { |
| 84 | jsonGenerator.writeObject(linkEvent); |
| 85 | } |
| 86 | jsonGenerator.writeEndArray(); |
| 87 | |
| 88 | // Output the added hosts array |
| 89 | jsonGenerator.writeArrayFieldStart("addedHosts"); |
| 90 | for (final DeviceEvent hostEvent : topologyEvents.getAddedHostEvents()) { |
| 91 | jsonGenerator.writeObject(hostEvent); |
| 92 | } |
| 93 | jsonGenerator.writeEndArray(); |
| 94 | |
| 95 | // Output the removed hosts array |
| 96 | jsonGenerator.writeArrayFieldStart("removedHosts"); |
| 97 | for (final DeviceEvent hostEvent : topologyEvents.getRemovedHostEvents()) { |
| 98 | jsonGenerator.writeObject(hostEvent); |
| 99 | } |
| 100 | jsonGenerator.writeEndArray(); |
| 101 | |
| 102 | // All done |
| 103 | jsonGenerator.writeEndObject(); |
| 104 | } |
| 105 | } |