Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -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 | import java.util.Map.Entry; |
| 5 | |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.topology.MastershipData; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.topology.TopologyElement; |
| 8 | |
| 9 | import org.codehaus.jackson.JsonGenerator; |
| 10 | import org.codehaus.jackson.map.SerializerProvider; |
| 11 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
| 12 | |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 13 | /** |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 14 | * JSON serializer for MastershipData objects. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 15 | */ |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 16 | public class MastershipDataSerializer extends SerializerBase<MastershipData> { |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 17 | /** |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 18 | * Default constructor. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 19 | */ |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 20 | public MastershipDataSerializer() { |
| 21 | super(MastershipData.class); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | /** |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 25 | * Serializes a MastershipData object in JSON. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 26 | * |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 27 | * @param mastershipData the MastershipData that is being converted to |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 28 | * 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 |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 32 | */ |
| 33 | @Override |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 34 | public void serialize(final MastershipData mastershipData, |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 35 | final JsonGenerator jsonGenerator, |
| 36 | final SerializerProvider serializerProvider) |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 37 | throws IOException { |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 38 | |
| 39 | // |
| 40 | // TODO: For now, the JSON format of the serialized output should |
| 41 | // be same as the JSON format of the corresponding class Mastership |
| 42 | // (if such class exists). |
| 43 | // In the future, we will use a single serializer. |
| 44 | // |
| 45 | |
| 46 | jsonGenerator.writeStartObject(); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 47 | jsonGenerator.writeStringField(TopologyElement.TYPE, mastershipData.getType()); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 48 | jsonGenerator.writeStringField("dpid", |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 49 | mastershipData.getDpid().toString()); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 50 | jsonGenerator.writeStringField("onosInstanceId", |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 51 | mastershipData.getOnosInstanceId().toString()); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 52 | jsonGenerator.writeStringField("role", |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 53 | mastershipData.getRole().name()); |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 54 | jsonGenerator.writeObjectFieldStart("stringAttributes"); |
Yuta HIGUCHI | d8fd2f5 | 2014-09-01 23:19:45 -0700 | [diff] [blame] | 55 | for (Entry<String, String> entry : mastershipData.getAllStringAttributes().entrySet()) { |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 56 | jsonGenerator.writeStringField(entry.getKey(), entry.getValue()); |
| 57 | } |
| 58 | jsonGenerator.writeEndObject(); // stringAttributes |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 59 | jsonGenerator.writeEndObject(); |
| 60 | } |
| 61 | } |