blob: c956cb1e83c40b6c3968bd0d506016ccda40dbdb [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.linkdiscovery.web;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08002
3import java.io.IOException;
4
Jonathan Harta99ec672014-04-03 11:30:34 -07005import net.onrc.onos.core.linkdiscovery.ILinkDiscovery.LinkType;
6import net.onrc.onos.core.linkdiscovery.Link;
7
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08008import org.codehaus.jackson.JsonGenerator;
9import org.codehaus.jackson.JsonProcessingException;
10import org.codehaus.jackson.map.JsonSerializer;
11import org.codehaus.jackson.map.SerializerProvider;
12import org.codehaus.jackson.map.annotate.JsonSerialize;
13import org.openflow.util.HexString;
14
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080015/**
16 * This class is both the datastructure and the serializer
17 * for a link with the corresponding type of link.
18 * @author alexreimers
19 */
20@JsonSerialize(using=LinkWithType.class)
21public class LinkWithType extends JsonSerializer<LinkWithType> {
22 public long srcSwDpid;
23 public short srcPort;
24 public int srcPortState;
25 public long dstSwDpid;
26 public short dstPort;
27 public int dstPortState;
28 public LinkType type;
29
30 // Do NOT delete this, it's required for the serializer
31 public LinkWithType() {}
32
33 public LinkWithType(Link link,
34 int srcPortState,
35 int dstPortState,
36 LinkType type) {
37 this.srcSwDpid = link.getSrc();
38 this.srcPort = link.getSrcPort();
39 this.srcPortState = srcPortState;
40 this.dstSwDpid = link.getDst();
41 this.dstPort = link.getDstPort();
42 this.dstPortState = dstPortState;
43 this.type = type;
44 }
45
46 @Override
47 public void serialize(LinkWithType lwt, JsonGenerator jgen, SerializerProvider arg2)
48 throws IOException, JsonProcessingException {
49 // You ****MUST*** use lwt for the fields as it's actually a different object.
50 jgen.writeStartObject();
51 jgen.writeStringField("src-switch", HexString.toHexString(lwt.srcSwDpid));
52 jgen.writeNumberField("src-port", lwt.srcPort);
53 jgen.writeNumberField("src-port-state", lwt.srcPortState);
54 jgen.writeStringField("dst-switch", HexString.toHexString(lwt.dstSwDpid));
55 jgen.writeNumberField("dst-port", lwt.dstPort);
56 jgen.writeNumberField("dst-port-state", lwt.dstPortState);
57 jgen.writeStringField("type", lwt.type.toString());
58 jgen.writeEndObject();
59 }
60
61 @Override
62 public Class<LinkWithType> handledType() {
63 return LinkWithType.class;
64 }
65}