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