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 | /** |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 14 | * JSON serializer for Host objects. |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 15 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 16 | public class HostSerializer extends SerializerBase<Host> { |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 17 | /** |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 18 | * Default constructor. |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 19 | */ |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 20 | public HostSerializer() { |
| 21 | super(Host.class); |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Serializes a Host object in JSON. |
| 26 | * |
| 27 | * @param host the Host that is being converted to JSON |
| 28 | * @param jsonGenerator generator to place the serialized JSON into |
| 29 | * @param serializerProvider unused but required for method override |
| 30 | * @throws IOException if the JSON serialization process fails |
| 31 | */ |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 32 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 33 | public void serialize(Host host, JsonGenerator jsonGenerator, |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 34 | SerializerProvider serializerProvider) |
| 35 | throws IOException { |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 36 | |
| 37 | // |
| 38 | // TODO: For now, the JSON format of the serialized output should |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame^] | 39 | // be same as the JSON format of the corresponding class HostData. |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 40 | // In the future, we will use a single serializer. |
| 41 | // |
| 42 | |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 43 | jsonGenerator.writeStartObject(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 44 | jsonGenerator.writeStringField(TopologyElement.TYPE, host.getType()); |
| 45 | jsonGenerator.writeStringField("mac", host.getMacAddress().toString()); |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 46 | jsonGenerator.writeFieldName("attachmentPoints"); |
| 47 | jsonGenerator.writeStartArray(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 48 | for (Port port : host.getAttachmentPoints()) { |
Yuta HIGUCHI | cd14dda | 2014-07-24 09:57:22 -0700 | [diff] [blame] | 49 | jsonGenerator.writeObject(port.getSwitchPort()); |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 50 | } |
| 51 | jsonGenerator.writeEndArray(); |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 52 | // |
| 53 | // NOTE: Class Host itself doesn't have stringAttributes. |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame^] | 54 | // Adding empty object for now for consistency with HostDataSerializer |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 55 | // |
| 56 | jsonGenerator.writeObjectFieldStart("stringAttributes"); |
| 57 | /* |
| 58 | for (Entry<String, String> entry : host.getAllStringAttributes().entrySet()) { |
| 59 | jsonGenerator.writeStringField(entry.getKey(), entry.getValue()); |
| 60 | } |
| 61 | */ |
| 62 | jsonGenerator.writeEndObject(); // stringAttributes |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 63 | jsonGenerator.writeEndObject(); |
| 64 | } |
TeruU | 65bc5ff | 2014-04-23 22:22:32 -0700 | [diff] [blame] | 65 | } |