Yuta HIGUCHI | a7f1cdd | 2014-06-09 15:05:20 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology.web.serializers; |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 2 | |
| 3 | import net.onrc.onos.core.topology.Device; |
| 4 | import net.onrc.onos.core.topology.Link; |
| 5 | import net.onrc.onos.core.topology.Switch; |
| 6 | import net.onrc.onos.core.topology.Topology; |
| 7 | import org.codehaus.jackson.JsonGenerator; |
| 8 | import org.codehaus.jackson.map.SerializerProvider; |
| 9 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
| 10 | |
| 11 | import java.io.IOException; |
| 12 | |
| 13 | /** |
| 14 | * JSON serializer for Topology objects. Used by REST implementation of the |
| 15 | * topology APIs. |
| 16 | */ |
| 17 | public class TopologySerializer extends SerializerBase<Topology> { |
| 18 | |
| 19 | /** |
| 20 | * Default constructor. Performs basic initialization of the JSON |
| 21 | * serializer. |
| 22 | */ |
| 23 | public TopologySerializer() { |
| 24 | super(Topology.class); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Serialize a Topology object in JSON. The resulting JSON contains the |
| 29 | * switches, links and ports provided by the Topology object. |
| 30 | * |
| 31 | * @param topology the Topology that is being converted to JSON |
| 32 | * @param jsonGenerator generator to place the serialized JSON into |
| 33 | * @param serializerProvider unused but required for method override |
| 34 | * @throws IOException if the JSON serialization process fails |
| 35 | */ |
| 36 | @Override |
| 37 | public void serialize(Topology topology, |
| 38 | JsonGenerator jsonGenerator, |
| 39 | SerializerProvider serializerProvider) |
| 40 | throws IOException { |
| 41 | // Start the object |
| 42 | jsonGenerator.writeStartObject(); |
| 43 | |
| 44 | // Output the switches array |
| 45 | jsonGenerator.writeArrayFieldStart("switches"); |
| 46 | for (final Switch swtch : topology.getSwitches()) { |
| 47 | jsonGenerator.writeObject(swtch); |
| 48 | } |
| 49 | jsonGenerator.writeEndArray(); |
| 50 | |
| 51 | // Output the links array |
| 52 | jsonGenerator.writeArrayFieldStart("links"); |
| 53 | for (final Link link : topology.getLinks()) { |
| 54 | jsonGenerator.writeObject(link); |
| 55 | } |
| 56 | jsonGenerator.writeEndArray(); |
| 57 | |
Pavlin Radoslavov | 308337c | 2014-06-11 10:25:44 -0700 | [diff] [blame] | 58 | // Output the hosts array |
| 59 | jsonGenerator.writeArrayFieldStart("hosts"); |
Ray Milkey | 0fe4385 | 2014-06-05 14:41:46 -0700 | [diff] [blame] | 60 | for (final Device device : topology.getDevices()) { |
| 61 | jsonGenerator.writeObject(device); |
| 62 | } |
| 63 | jsonGenerator.writeEndArray(); |
| 64 | |
| 65 | // All done |
| 66 | jsonGenerator.writeEndObject(); |
| 67 | } |
| 68 | } |