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; |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 4 | import java.util.Map.Entry; |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 5 | |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.topology.HostData; |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.topology.TopologyElement; |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.util.SwitchPort; |
| 9 | |
| 10 | import org.codehaus.jackson.JsonGenerator; |
| 11 | import org.codehaus.jackson.map.SerializerProvider; |
| 12 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
| 13 | |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 14 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 15 | * JSON serializer for HostData objects. |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 16 | */ |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 17 | public class HostDataSerializer extends SerializerBase<HostData> { |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 18 | /** |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 19 | * Default constructor. |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 20 | */ |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 21 | public HostDataSerializer() { |
| 22 | super(HostData.class); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 25 | /** |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 26 | * Serializes a HostData object in JSON. |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 27 | * |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 28 | * @param hostData the HostData that is being converted to JSON |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 29 | * @param jsonGenerator generator to place the serialized JSON into |
| 30 | * @param serializerProvider unused but required for method override |
| 31 | * @throws IOException if the JSON serialization process fails |
| 32 | */ |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 33 | @Override |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 34 | public void serialize(HostData hostData, JsonGenerator jsonGenerator, |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 35 | SerializerProvider serializerProvider) |
| 36 | throws IOException { |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 37 | |
| 38 | // |
| 39 | // TODO: For now, the JSON format of the serialized output should |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 40 | // be same as the JSON format of the corresponding class Host. |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 41 | // In the future, we will use a single serializer. |
| 42 | // |
| 43 | |
| 44 | jsonGenerator.writeStartObject(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 45 | jsonGenerator.writeStringField(TopologyElement.TYPE, hostData.getType()); |
| 46 | jsonGenerator.writeStringField("mac", hostData.getMac().toString()); |
Fahad Naeem Khan | 6459712 | 2014-09-29 11:06:47 -0700 | [diff] [blame] | 47 | jsonGenerator.writeStringField("ipv4", IPAddressToString(hostData.getIp())); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 48 | jsonGenerator.writeFieldName("attachmentPoints"); |
| 49 | jsonGenerator.writeStartArray(); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 50 | for (SwitchPort switchPort : hostData.getAttachmentPoints()) { |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 51 | jsonGenerator.writeObject(switchPort); |
| 52 | } |
| 53 | jsonGenerator.writeEndArray(); |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 54 | jsonGenerator.writeObjectFieldStart("stringAttributes"); |
Yuta HIGUCHI | 93d35ea | 2014-08-31 23:26:13 -0700 | [diff] [blame] | 55 | for (Entry<String, String> entry : hostData.getAllStringAttributes().entrySet()) { |
Pavlin Radoslavov | 1766038 | 2014-07-24 21:21:07 -0700 | [diff] [blame] | 56 | jsonGenerator.writeStringField(entry.getKey(), entry.getValue()); |
| 57 | } |
| 58 | jsonGenerator.writeEndObject(); // stringAttributes |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 59 | jsonGenerator.writeEndObject(); |
| 60 | } |
Fahad Naeem Khan | 6459712 | 2014-09-29 11:06:47 -0700 | [diff] [blame] | 61 | public String IPAddressToString(int ip) { |
| 62 | return ((ip >> 24) & 0xFF) + "." + |
| 63 | ((ip >> 16) & 0xFF) + "." + |
| 64 | ((ip >> 8) & 0xFF) + "." + |
| 65 | (ip & 0xFF); |
| 66 | } |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 67 | } |