blob: d03c820011a1604fda23c4aa96284574d1c3c44d [file] [log] [blame]
Yuta HIGUCHIa7f1cdd2014-06-09 15:05:20 -07001package net.onrc.onos.core.topology.web.serializers;
TeruU65bc5ff2014-04-23 22:22:32 -07002
3import java.io.IOException;
4
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07005import net.onrc.onos.core.topology.Host;
TeruU65bc5ff2014-04-23 22:22:32 -07006import net.onrc.onos.core.topology.Port;
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -07007import net.onrc.onos.core.topology.TopologyElement;
TeruU65bc5ff2014-04-23 22:22:32 -07008
TeruU65bc5ff2014-04-23 22:22:32 -07009import org.codehaus.jackson.JsonGenerator;
10import org.codehaus.jackson.map.SerializerProvider;
11import org.codehaus.jackson.map.ser.std.SerializerBase;
12
Ray Milkey92897212014-07-21 10:33:16 -070013/**
Pavlin Radoslavov17660382014-07-24 21:21:07 -070014 * JSON serializer for Host objects.
Ray Milkey92897212014-07-21 10:33:16 -070015 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070016public class HostSerializer extends SerializerBase<Host> {
Ray Milkey92897212014-07-21 10:33:16 -070017 /**
Pavlin Radoslavov17660382014-07-24 21:21:07 -070018 * Default constructor.
Ray Milkey92897212014-07-21 10:33:16 -070019 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070020 public HostSerializer() {
21 super(Host.class);
TeruU65bc5ff2014-04-23 22:22:32 -070022 }
23
Pavlin Radoslavov17660382014-07-24 21:21:07 -070024 /**
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 */
TeruU65bc5ff2014-04-23 22:22:32 -070032 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070033 public void serialize(Host host, JsonGenerator jsonGenerator,
Pavlin Radoslavov17660382014-07-24 21:21:07 -070034 SerializerProvider serializerProvider)
35 throws IOException {
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070036
37 //
38 // TODO: For now, the JSON format of the serialized output should
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070039 // be same as the JSON format of the corresponding class HostData.
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070040 // In the future, we will use a single serializer.
41 //
42
TeruU65bc5ff2014-04-23 22:22:32 -070043 jsonGenerator.writeStartObject();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070044 jsonGenerator.writeStringField(TopologyElement.TYPE, host.getType());
45 jsonGenerator.writeStringField("mac", host.getMacAddress().toString());
Fahad Naeem Khan64597122014-09-29 11:06:47 -070046 jsonGenerator.writeStringField("ipv4", IPAddressToString(host.getIpAddress()));
TeruU65bc5ff2014-04-23 22:22:32 -070047 jsonGenerator.writeFieldName("attachmentPoints");
48 jsonGenerator.writeStartArray();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070049 for (Port port : host.getAttachmentPoints()) {
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -070050 jsonGenerator.writeObject(port.getSwitchPort());
TeruU65bc5ff2014-04-23 22:22:32 -070051 }
52 jsonGenerator.writeEndArray();
Pavlin Radoslavov17660382014-07-24 21:21:07 -070053 //
54 // NOTE: Class Host itself doesn't have stringAttributes.
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -070055 // Adding empty object for now for consistency with HostDataSerializer
Pavlin Radoslavov17660382014-07-24 21:21:07 -070056 //
57 jsonGenerator.writeObjectFieldStart("stringAttributes");
58 /*
59 for (Entry<String, String> entry : host.getAllStringAttributes().entrySet()) {
60 jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
61 }
62 */
63 jsonGenerator.writeEndObject(); // stringAttributes
TeruU65bc5ff2014-04-23 22:22:32 -070064 jsonGenerator.writeEndObject();
65 }
Fahad Naeem Khan64597122014-09-29 11:06:47 -070066 public String IPAddressToString(int ip) {
67 return ((ip >> 24) & 0xFF) + "." +
68 ((ip >> 16) & 0xFF) + "." +
69 ((ip >> 8) & 0xFF) + "." +
70 (ip & 0xFF);
71 }
TeruU65bc5ff2014-04-23 22:22:32 -070072}