Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology.web.serializers; |
| 2 | |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 3 | import java.io.IOException; |
| 4 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.topology.HostData; |
| 6 | import net.onrc.onos.core.topology.LinkData; |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.topology.MastershipData; |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.topology.PortData; |
| 9 | import net.onrc.onos.core.topology.SwitchData; |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.topology.TopologyEvents; |
| 11 | import org.codehaus.jackson.JsonGenerator; |
| 12 | import org.codehaus.jackson.map.SerializerProvider; |
| 13 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
| 14 | |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 15 | /** |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 16 | * JSON serializer for TopologyEvents objects. |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 17 | */ |
| 18 | public class TopologyEventsSerializer extends SerializerBase<TopologyEvents> { |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 19 | /** |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 20 | * Default constructor. |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 21 | */ |
| 22 | public TopologyEventsSerializer() { |
| 23 | super(TopologyEvents.class); |
| 24 | } |
| 25 | |
| 26 | /** |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 27 | * Serializes a TopologyEvents object in JSON. The resulting JSON contains |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 28 | * the added and removed topology objects: switches, links and ports. |
| 29 | * |
| 30 | * @param topologyEvents the TopologyEvents that is being converted to JSON |
| 31 | * @param jsonGenerator generator to place the serialized JSON into |
| 32 | * @param serializerProvider unused but required for method override |
| 33 | * @throws IOException if the JSON serialization process fails |
| 34 | */ |
| 35 | @Override |
| 36 | public void serialize(TopologyEvents topologyEvents, |
| 37 | JsonGenerator jsonGenerator, |
| 38 | SerializerProvider serializerProvider) |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 39 | throws IOException { |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 40 | |
| 41 | // Start the object |
| 42 | jsonGenerator.writeStartObject(); |
| 43 | |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 44 | // Output the added switch mastership array |
| 45 | jsonGenerator.writeArrayFieldStart("addedSwitchMasterships"); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 46 | for (final MastershipData mastershipData : topologyEvents.getAddedMastershipDataEntries()) { |
| 47 | jsonGenerator.writeObject(mastershipData); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 48 | } |
| 49 | jsonGenerator.writeEndArray(); |
| 50 | |
| 51 | // Output the removed switch mastership array |
| 52 | jsonGenerator.writeArrayFieldStart("removedSwitchMasterships"); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 53 | for (final MastershipData mastershipData : topologyEvents.getRemovedMastershipDataEntries()) { |
| 54 | jsonGenerator.writeObject(mastershipData); |
Pavlin Radoslavov | cac157d | 2014-07-31 13:54:08 -0700 | [diff] [blame] | 55 | } |
| 56 | jsonGenerator.writeEndArray(); |
| 57 | |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 58 | // Output the added switches array |
| 59 | jsonGenerator.writeArrayFieldStart("addedSwitches"); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 60 | for (final SwitchData switchData : topologyEvents.getAddedSwitchDataEntries()) { |
| 61 | jsonGenerator.writeObject(switchData); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 62 | } |
| 63 | jsonGenerator.writeEndArray(); |
| 64 | |
| 65 | // Output the removed switches array |
| 66 | jsonGenerator.writeArrayFieldStart("removedSwitches"); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 67 | for (final SwitchData switchData : topologyEvents.getRemovedSwitchDataEntries()) { |
| 68 | jsonGenerator.writeObject(switchData); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 69 | } |
| 70 | jsonGenerator.writeEndArray(); |
| 71 | |
| 72 | // Output the added ports array |
| 73 | jsonGenerator.writeArrayFieldStart("addedPorts"); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 74 | for (final PortData portData : topologyEvents.getAddedPortDataEntries()) { |
| 75 | jsonGenerator.writeObject(portData); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 76 | } |
| 77 | jsonGenerator.writeEndArray(); |
| 78 | |
| 79 | // Output the removed ports array |
| 80 | jsonGenerator.writeArrayFieldStart("removedPorts"); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 81 | for (final PortData portData : topologyEvents.getRemovedPortDataEntries()) { |
| 82 | jsonGenerator.writeObject(portData); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 83 | } |
| 84 | jsonGenerator.writeEndArray(); |
| 85 | |
| 86 | // Output the added links array |
| 87 | jsonGenerator.writeArrayFieldStart("addedLinks"); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 88 | for (final LinkData linkData : topologyEvents.getAddedLinkDataEntries()) { |
| 89 | jsonGenerator.writeObject(linkData); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 90 | } |
| 91 | jsonGenerator.writeEndArray(); |
| 92 | |
| 93 | // Output the removed links array |
| 94 | jsonGenerator.writeArrayFieldStart("removedLinks"); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 95 | for (final LinkData linkData : topologyEvents.getRemovedLinkDataEntries()) { |
| 96 | jsonGenerator.writeObject(linkData); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 97 | } |
| 98 | jsonGenerator.writeEndArray(); |
| 99 | |
| 100 | // Output the added hosts array |
| 101 | jsonGenerator.writeArrayFieldStart("addedHosts"); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 102 | for (final HostData hostData : topologyEvents.getAddedHostDataEntries()) { |
| 103 | jsonGenerator.writeObject(hostData); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 104 | } |
| 105 | jsonGenerator.writeEndArray(); |
| 106 | |
| 107 | // Output the removed hosts array |
| 108 | jsonGenerator.writeArrayFieldStart("removedHosts"); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 109 | for (final HostData hostData : topologyEvents.getRemovedHostDataEntries()) { |
| 110 | jsonGenerator.writeObject(hostData); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 111 | } |
| 112 | jsonGenerator.writeEndArray(); |
| 113 | |
| 114 | // All done |
| 115 | jsonGenerator.writeEndObject(); |
| 116 | } |
| 117 | } |