Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.core.topology.web.serializers; |
| 2 | |
| 3 | import net.onrc.onos.core.topology.MastershipEvent; |
| 4 | import net.onrc.onos.core.topology.TopologyElement; |
| 5 | |
| 6 | import org.codehaus.jackson.JsonGenerator; |
| 7 | import org.codehaus.jackson.map.SerializerProvider; |
| 8 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
| 9 | |
| 10 | import java.io.IOException; |
| 11 | |
| 12 | /** |
| 13 | * JSON serializer for MastershipEvents. |
| 14 | */ |
| 15 | public class MastershipEventSerializer extends SerializerBase<MastershipEvent> { |
| 16 | |
| 17 | /** |
| 18 | * Public constructor - just calls its super class constructor. |
| 19 | */ |
| 20 | public MastershipEventSerializer() { |
| 21 | super(MastershipEvent.class); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Serializes a MastershipEvent object. |
| 26 | * |
| 27 | * @param mastershipEvent MastershipEvent to serialize |
| 28 | * @param jsonGenerator generator to add the serialized object to |
| 29 | * @param serializerProvider not used |
| 30 | * @throws IOException if the JSON serialization fails |
| 31 | */ |
| 32 | @Override |
| 33 | public void serialize(final MastershipEvent mastershipEvent, |
| 34 | final JsonGenerator jsonGenerator, |
| 35 | final SerializerProvider serializerProvider) |
| 36 | throws IOException { |
| 37 | |
| 38 | // |
| 39 | // TODO: For now, the JSON format of the serialized output should |
| 40 | // be same as the JSON format of the corresponding class Mastership |
| 41 | // (if such class exists). |
| 42 | // In the future, we will use a single serializer. |
| 43 | // |
| 44 | |
| 45 | jsonGenerator.writeStartObject(); |
| 46 | jsonGenerator.writeStringField(TopologyElement.TYPE, mastershipEvent.getType()); |
| 47 | jsonGenerator.writeStringField("dpid", |
| 48 | mastershipEvent.getDpid().toString()); |
| 49 | jsonGenerator.writeStringField("onosInstanceId", |
| 50 | mastershipEvent.getOnosInstanceId()); |
| 51 | jsonGenerator.writeStringField("role", |
| 52 | mastershipEvent.getRole().name()); |
| 53 | jsonGenerator.writeEndObject(); |
| 54 | } |
| 55 | } |