blob: d5a98f0b6471d3cbd81ef26e2f29a2ea0c1a1af8 [file] [log] [blame]
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -07001package net.onrc.onos.core.topology.web.serializers;
2
3import java.io.IOException;
4
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -07005import net.onrc.onos.core.topology.HostEvent;
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -07006import net.onrc.onos.core.topology.TopologyElement;
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -07007import net.onrc.onos.core.util.SwitchPort;
8
9import 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 HostEvents as JSON.
15 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070016public class HostEventSerializer extends SerializerBase<HostEvent> {
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070017
Ray Milkey92897212014-07-21 10:33:16 -070018 /**
19 * Constructs a HostEvent Serializer.
20 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070021 public HostEventSerializer() {
22 super(HostEvent.class);
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070023 }
24
25 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070026 public void serialize(HostEvent hostEvent, JsonGenerator jsonGenerator,
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070027 SerializerProvider serializerProvider) throws IOException {
28
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 Host.
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070032 // In the future, we will use a single serializer.
33 //
34
35 jsonGenerator.writeStartObject();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070036 jsonGenerator.writeStringField(TopologyElement.TYPE, hostEvent.getType());
37 jsonGenerator.writeStringField("mac", hostEvent.getMac().toString());
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070038 jsonGenerator.writeFieldName("attachmentPoints");
39 jsonGenerator.writeStartArray();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070040 for (SwitchPort switchPort : hostEvent.getAttachmentPoints()) {
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070041 jsonGenerator.writeObject(switchPort);
42 }
43 jsonGenerator.writeEndArray();
44 jsonGenerator.writeEndObject();
45 }
46}