blob: 80bbe245c1928aa0b485fc08f9b9de70e40ba25a [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/**
14 * Serializes Host objects as JSON.
15 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070016public class HostSerializer extends SerializerBase<Host> {
TeruU65bc5ff2014-04-23 22:22:32 -070017
Ray Milkey92897212014-07-21 10:33:16 -070018 /**
19 * Constructs a Host serializer.
20 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070021 public HostSerializer() {
22 super(Host.class);
TeruU65bc5ff2014-04-23 22:22:32 -070023 }
24
25 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070026 public void serialize(Host host, JsonGenerator jsonGenerator,
Ray Milkey7d7f0a02014-06-18 12:52:13 -070027 SerializerProvider serializerProvider) throws IOException {
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070028
29 //
30 // TODO: For now, the JSON format of the serialized output should
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070031 // be same as the JSON format of the corresponding class HostEvent.
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070032 // In the future, we will use a single serializer.
33 //
34
TeruU65bc5ff2014-04-23 22:22:32 -070035 jsonGenerator.writeStartObject();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070036 jsonGenerator.writeStringField(TopologyElement.TYPE, host.getType());
37 jsonGenerator.writeStringField("mac", host.getMacAddress().toString());
TeruU65bc5ff2014-04-23 22:22:32 -070038 jsonGenerator.writeFieldName("attachmentPoints");
39 jsonGenerator.writeStartArray();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070040 for (Port port : host.getAttachmentPoints()) {
Yuta HIGUCHIcd14dda2014-07-24 09:57:22 -070041 jsonGenerator.writeObject(port.getSwitchPort());
TeruU65bc5ff2014-04-23 22:22:32 -070042 }
43 jsonGenerator.writeEndArray();
44 jsonGenerator.writeEndObject();
45 }
TeruU65bc5ff2014-04-23 22:22:32 -070046}