blob: 34d411f8a2eef28ccd2703aac3ec5f8d9398ab97 [file] [log] [blame]
Yuta HIGUCHIa7f1cdd2014-06-09 15:05:20 -07001package net.onrc.onos.core.topology.web.serializers;
Jonathan Hart891d0502014-02-10 10:04:08 -08002
3import java.io.IOException;
Yuta HIGUCHId830aad2014-07-06 15:02:01 -07004import java.util.Map.Entry;
Jonathan Hart891d0502014-02-10 10:04:08 -08005
Jonathan Hart472062d2014-04-03 10:56:48 -07006import net.onrc.onos.core.topology.Port;
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -07007import net.onrc.onos.core.topology.TopologyElement;
Jonathan Hart891d0502014-02-10 10:04:08 -08008
9import org.codehaus.jackson.JsonGenerator;
Jonathan Hart891d0502014-02-10 10:04:08 -080010import org.codehaus.jackson.map.SerializerProvider;
11import org.codehaus.jackson.map.ser.std.SerializerBase;
12
13public class PortSerializer extends SerializerBase<Port> {
14
Ray Milkey269ffb92014-04-03 14:43:30 -070015 public PortSerializer() {
16 super(Port.class);
17 }
Jonathan Hart891d0502014-02-10 10:04:08 -080018
Ray Milkey269ffb92014-04-03 14:43:30 -070019 @Override
20 public void serialize(Port port, JsonGenerator jsonGenerator,
21 SerializerProvider serializerProvider)
Ray Milkey7d7f0a02014-06-18 12:52:13 -070022 throws IOException {
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070023 //
24 // TODO: For now, the JSON format of the serialized output should
25 // be same as the JSON format of the corresponding class PortEvent.
26 // In the future, we will use a single serializer.
27 //
28
Ray Milkey269ffb92014-04-03 14:43:30 -070029 jsonGenerator.writeStartObject();
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -070030 jsonGenerator.writeStringField(TopologyElement.TYPE, port.getType());
Ray Milkey269ffb92014-04-03 14:43:30 -070031 jsonGenerator.writeStringField("state", "ACTIVE");
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070032 jsonGenerator.writeStringField("dpid", port.getDpid().toString());
Pavlin Radoslavov5cf1fe02014-07-03 22:52:25 -070033 //
34 // FIXME: The solution below to preresent the "short" port number
35 // as an unsigned value is a hack. The fix should be elsewhere
36 // (e.g., in class PortNumber itself).
37 //
38 jsonGenerator.writeNumberField("portNumber",
39 (0xffff & port.getNumber().value()));
Ray Milkey269ffb92014-04-03 14:43:30 -070040 jsonGenerator.writeStringField("desc", port.getDescription());
Yuta HIGUCHId830aad2014-07-06 15:02:01 -070041 jsonGenerator.writeObjectFieldStart("stringAttributes");
42 for (Entry<String, String> entry : port.getAllStringAttributes().entrySet()) {
43 jsonGenerator.writeStringField(entry.getKey(), entry.getValue());
44 }
45 jsonGenerator.writeEndObject(); // stringAttributes
Ray Milkey269ffb92014-04-03 14:43:30 -070046 jsonGenerator.writeEndObject();
47 }
Jonathan Hart891d0502014-02-10 10:04:08 -080048}