Yuta HIGUCHI | a7f1cdd | 2014-06-09 15:05:20 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology.web.serializers; |
Jonathan Hart | 891d050 | 2014-02-10 10:04:08 -0800 | [diff] [blame] | 2 | |
| 3 | import java.io.IOException; |
Yuta HIGUCHI | d830aad | 2014-07-06 15:02:01 -0700 | [diff] [blame] | 4 | import java.util.Map.Entry; |
Jonathan Hart | 891d050 | 2014-02-10 10:04:08 -0800 | [diff] [blame] | 5 | |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.topology.Port; |
| 7 | import net.onrc.onos.core.topology.Switch; |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.topology.TopologyElement; |
Jonathan Hart | 891d050 | 2014-02-10 10:04:08 -0800 | [diff] [blame] | 9 | |
| 10 | import org.codehaus.jackson.JsonGenerator; |
Jonathan Hart | 891d050 | 2014-02-10 10:04:08 -0800 | [diff] [blame] | 11 | import org.codehaus.jackson.map.SerializerProvider; |
| 12 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
Jonathan Hart | 891d050 | 2014-02-10 10:04:08 -0800 | [diff] [blame] | 13 | |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 14 | /** |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 15 | * JSON serializer for Switch objects. |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 16 | */ |
Jonathan Hart | 891d050 | 2014-02-10 10:04:08 -0800 | [diff] [blame] | 17 | public class SwitchSerializer extends SerializerBase<Switch> { |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 18 | /** |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 19 | * Default constructor. |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 20 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 21 | public SwitchSerializer() { |
| 22 | super(Switch.class); |
| 23 | } |
Jonathan Hart | 891d050 | 2014-02-10 10:04:08 -0800 | [diff] [blame] | 24 | |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Serializes a Switch object in JSON. |
| 27 | * |
| 28 | * @param sw the Switch that is being converted to JSON |
| 29 | * @param jsonGenerator generator to place the serialized JSON into |
| 30 | * @param serializerProvider unused but required for method override |
| 31 | * @throws IOException if the JSON serialization process fails |
| 32 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 33 | @Override |
| 34 | public void serialize(Switch sw, JsonGenerator jsonGenerator, |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 35 | SerializerProvider serializerProvider) |
| 36 | throws IOException { |
| 37 | |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 38 | // |
| 39 | // TODO: For now, the JSON format of the serialized output should |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 40 | // be same as the JSON format of the corresponding class SwitchData. |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 41 | // In the future, we will use a single serializer. |
| 42 | // |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | |
| 44 | jsonGenerator.writeStartObject(); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 45 | jsonGenerator.writeStringField(TopologyElement.TYPE, sw.getType()); |
Praseed Balakrishnan | fa21be1 | 2014-07-15 14:42:26 -0700 | [diff] [blame] | 46 | jsonGenerator.writeStringField(TopologyElement.ELEMENT_TYPE, sw.getSwitchType().toString()); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 47 | jsonGenerator.writeStringField("dpid", sw.getDpid().toString()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | jsonGenerator.writeStringField("state", "ACTIVE"); |
| 49 | jsonGenerator.writeArrayFieldStart("ports"); |
| 50 | for (Port port : sw.getPorts()) { |
| 51 | jsonGenerator.writeObject(port); |
| 52 | } |
| 53 | jsonGenerator.writeEndArray(); |
Yuta HIGUCHI | d830aad | 2014-07-06 15:02:01 -0700 | [diff] [blame] | 54 | jsonGenerator.writeObjectFieldStart("stringAttributes"); |
| 55 | for (Entry<String, String> entry : sw.getAllStringAttributes().entrySet()) { |
| 56 | jsonGenerator.writeStringField(entry.getKey(), entry.getValue()); |
| 57 | } |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 58 | jsonGenerator.writeEndObject(); // stringAttributes |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | jsonGenerator.writeEndObject(); |
| 60 | } |
Jonathan Hart | 891d050 | 2014-02-10 10:04:08 -0800 | [diff] [blame] | 61 | } |