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; |
| 4 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.topology.HostEvent; |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.topology.TopologyElement; |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.util.SwitchPort; |
| 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 | /** |
| 14 | * Serializes HostEvents as JSON. |
| 15 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 16 | public class HostEventSerializer extends SerializerBase<HostEvent> { |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame^] | 18 | /** |
| 19 | * Constructs a HostEvent Serializer. |
| 20 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 21 | public HostEventSerializer() { |
| 22 | super(HostEvent.class); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 26 | public void serialize(HostEvent hostEvent, JsonGenerator jsonGenerator, |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 27 | SerializerProvider serializerProvider) throws IOException { |
| 28 | |
| 29 | // |
| 30 | // TODO: For now, the JSON format of the serialized output should |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 31 | // be same as the JSON format of the corresponding class Host. |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 32 | // In the future, we will use a single serializer. |
| 33 | // |
| 34 | |
| 35 | jsonGenerator.writeStartObject(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 36 | jsonGenerator.writeStringField(TopologyElement.TYPE, hostEvent.getType()); |
| 37 | jsonGenerator.writeStringField("mac", hostEvent.getMac().toString()); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 38 | jsonGenerator.writeFieldName("attachmentPoints"); |
| 39 | jsonGenerator.writeStartArray(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 40 | for (SwitchPort switchPort : hostEvent.getAttachmentPoints()) { |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 41 | jsonGenerator.writeObject(switchPort); |
| 42 | } |
| 43 | jsonGenerator.writeEndArray(); |
| 44 | jsonGenerator.writeEndObject(); |
| 45 | } |
| 46 | } |