Yuta HIGUCHI | a7f1cdd | 2014-06-09 15:05:20 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology.web.serializers; |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 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.Host; |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.topology.Port; |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.topology.TopologyElement; |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 8 | |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 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 Host objects as JSON. |
| 15 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 16 | public class HostSerializer extends SerializerBase<Host> { |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 18 | /** |
| 19 | * Constructs a Host serializer. |
| 20 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 21 | public HostSerializer() { |
| 22 | super(Host.class); |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 26 | public void serialize(Host host, JsonGenerator jsonGenerator, |
Ray Milkey | 7d7f0a0 | 2014-06-18 12:52:13 -0700 | [diff] [blame] | 27 | SerializerProvider serializerProvider) throws IOException { |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 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 HostEvent. |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 32 | // In the future, we will use a single serializer. |
| 33 | // |
| 34 | |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 35 | jsonGenerator.writeStartObject(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 36 | jsonGenerator.writeStringField(TopologyElement.TYPE, host.getType()); |
| 37 | jsonGenerator.writeStringField("mac", host.getMacAddress().toString()); |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 38 | jsonGenerator.writeFieldName("attachmentPoints"); |
| 39 | jsonGenerator.writeStartArray(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 40 | for (Port port : host.getAttachmentPoints()) { |
Yuta HIGUCHI | cd14dda | 2014-07-24 09:57:22 -0700 | [diff] [blame^] | 41 | jsonGenerator.writeObject(port.getSwitchPort()); |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 42 | } |
| 43 | jsonGenerator.writeEndArray(); |
| 44 | jsonGenerator.writeEndObject(); |
| 45 | } |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 46 | } |