HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.linkdiscovery.web; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import org.codehaus.jackson.JsonGenerator; |
| 6 | import org.codehaus.jackson.JsonProcessingException; |
| 7 | import org.codehaus.jackson.map.JsonSerializer; |
| 8 | import org.codehaus.jackson.map.SerializerProvider; |
| 9 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 10 | import org.openflow.util.HexString; |
| 11 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.routing.Link; |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 13 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery.LinkType; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 14 | |
| 15 | /** |
| 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) |
| 21 | public 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 | } |