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 | |
| 5 | import net.onrc.onos.core.topology.Device; |
| 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 | |
| 13 | public class DeviceSerializer extends SerializerBase<Device> { |
| 14 | |
| 15 | public DeviceSerializer() { |
| 16 | super(Device.class); |
| 17 | } |
| 18 | |
| 19 | @Override |
| 20 | public void serialize(Device dev, JsonGenerator jsonGenerator, |
Ray Milkey | 7d7f0a0 | 2014-06-18 12:52:13 -0700 | [diff] [blame] | 21 | SerializerProvider serializerProvider) throws IOException { |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 22 | |
| 23 | // |
| 24 | // TODO: For now, the JSON format of the serialized output should |
| 25 | // be same as the JSON format of the corresponding class DeviceEvent. |
| 26 | // In the future, we will use a single serializer. |
| 27 | // |
| 28 | |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 29 | jsonGenerator.writeStartObject(); |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 30 | jsonGenerator.writeStringField(TopologyElement.TYPE, dev.getType()); |
TeruU | 1c072a6 | 2014-04-28 14:49:25 -0700 | [diff] [blame] | 31 | jsonGenerator.writeStringField("mac", dev.getMacAddress().toString()); |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 32 | jsonGenerator.writeFieldName("attachmentPoints"); |
| 33 | jsonGenerator.writeStartArray(); |
| 34 | for (Port port : dev.getAttachmentPoints()) { |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 35 | jsonGenerator.writeObject(port.asSwitchPort()); |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 36 | } |
| 37 | jsonGenerator.writeEndArray(); |
| 38 | jsonGenerator.writeEndObject(); |
| 39 | } |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 40 | } |