diff --git a/src/main/java/net/floodlightcontroller/linkdiscovery/ILinkDiscovery.java b/src/main/java/net/floodlightcontroller/linkdiscovery/ILinkDiscovery.java index 08f7c50cd221eb577ac1177871f53871cb17a356..af731cea76b10533264a7eaa2a9661616f897e95 100644 --- a/src/main/java/net/floodlightcontroller/linkdiscovery/ILinkDiscovery.java +++ b/src/main/java/net/floodlightcontroller/linkdiscovery/ILinkDiscovery.java @@ -103,6 +103,29 @@ public interface ILinkDiscovery { }; public enum LinkType { - INVALID_LINK, DIRECT_LINK, MULTIHOP_LINK, TUNNEL + INVALID_LINK { + @Override + public String toString() { + return "invalid"; + } + }, + DIRECT_LINK{ + @Override + public String toString() { + return "internal"; + } + }, + MULTIHOP_LINK { + @Override + public String toString() { + return "external"; + } + }, + TUNNEL { + @Override + public String toString() { + return "tunnel"; + } + } }; } diff --git a/src/main/java/net/floodlightcontroller/linkdiscovery/web/LinkWithType.java b/src/main/java/net/floodlightcontroller/linkdiscovery/web/LinkWithType.java new file mode 100644 index 0000000000000000000000000000000000000000..514cfe4f62acd329e772879c38296f9c5f7b1ff4 --- /dev/null +++ b/src/main/java/net/floodlightcontroller/linkdiscovery/web/LinkWithType.java @@ -0,0 +1,56 @@ +package net.floodlightcontroller.linkdiscovery.web; + +import java.io.IOException; + +import org.codehaus.jackson.JsonGenerator; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.JsonSerializer; +import org.codehaus.jackson.map.SerializerProvider; +import org.codehaus.jackson.map.annotate.JsonSerialize; +import org.openflow.util.HexString; + +import net.floodlightcontroller.linkdiscovery.ILinkDiscovery.LinkType; +import net.floodlightcontroller.routing.Link; + +/** + * This class is both the datastructure and the serializer + * for a link with the corresponding type of link. + * @author alexreimers + */ +@JsonSerialize(using=LinkWithType.class) +public class LinkWithType extends JsonSerializer<LinkWithType> { + public long srcSwDpid; + public short srcPort; + public long dstSwDpid; + public short dstPort; + public LinkType type; + + // Do NOT delete this, it's required for the serializer + public LinkWithType() {} + + public LinkWithType(Link link, LinkType type) { + this.srcSwDpid = link.getSrc(); + this.srcPort = link.getSrcPort(); + this.dstSwDpid = link.getDst(); + this.dstPort = link.getDstPort(); + this.type = type; + } + + @Override + public void serialize(LinkWithType lwt, JsonGenerator jgen, SerializerProvider arg2) + throws IOException, JsonProcessingException { + // You ****MUST*** use lwt for the fields as it's actually a different object. + jgen.writeStartObject(); + jgen.writeStringField("src-switch", HexString.toHexString(lwt.srcSwDpid)); + jgen.writeNumberField("src-port", lwt.srcPort); + jgen.writeStringField("dst-switch", HexString.toHexString(lwt.dstSwDpid)); + jgen.writeNumberField("dst-port", lwt.dstPort); + jgen.writeStringField("type", lwt.type.toString()); + jgen.writeEndObject(); + } + + @Override + public Class<LinkWithType> handledType() { + return LinkWithType.class; + } +} \ No newline at end of file diff --git a/src/main/java/net/floodlightcontroller/linkdiscovery/web/LinksResource.java b/src/main/java/net/floodlightcontroller/linkdiscovery/web/LinksResource.java index eda84c591bc3645c42e0643861b044d0097df7a9..8fe1ff046f192cd0bcded0792a0bbf3a7030a737 100644 --- a/src/main/java/net/floodlightcontroller/linkdiscovery/web/LinksResource.java +++ b/src/main/java/net/floodlightcontroller/linkdiscovery/web/LinksResource.java @@ -5,7 +5,6 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import net.floodlightcontroller.linkdiscovery.ILinkDiscovery.LinkType; import net.floodlightcontroller.linkdiscovery.ILinkDiscoveryService; import net.floodlightcontroller.linkdiscovery.LinkInfo; import net.floodlightcontroller.routing.Link; @@ -15,26 +14,8 @@ import org.restlet.resource.ServerResource; public class LinksResource extends ServerResource { - public class LinkWithType { - long src; - short srcPort; - long dst; - short dstPort; - String type; - - public LinkWithType(Link link, String type) { - this.src = link.getSrc(); - this.srcPort = link.getSrcPort(); - this.dst = link.getDst(); - this.dstPort = link.getDstPort(); - this.type = type; - } - }; - @Get("json") public Set<LinkWithType> retrieve() { - String str; - ILinkDiscoveryService ld = (ILinkDiscoveryService)getContext().getAttributes(). get(ILinkDiscoveryService.class.getCanonicalName()); Map<Link, LinkInfo> links = new HashMap<Link, LinkInfo>(); @@ -44,17 +25,7 @@ public class LinksResource extends ServerResource { links.putAll(ld.getLinks()); for (Link link: links.keySet()) { LinkInfo info = links.get(link); - LinkType type = ld.getLinkType(info); - - if (type == LinkType.DIRECT_LINK) - str = "internal"; - else if (type == LinkType.MULTIHOP_LINK) - str = "external"; - else if (type == LinkType.TUNNEL) - str = "tunnel"; - else str = "invalid"; - - LinkWithType lwt = new LinkWithType(link, str); + LinkWithType lwt = new LinkWithType(link, ld.getLinkType(info)); returnLinkSet.add(lwt); } }