Skip to content
Snippets Groups Projects
Commit e2ff3bf5 authored by abat's avatar abat
Browse files

Merge into master from pull request #228:

Fix Links REST API and remove an unused serializer. (https://github.com/floodlight/floodlight/pull/228)
parents 6860c43f d54a1a56
No related branches found
No related tags found
No related merge requests found
/**
* Copyright 2011, Big Switch Networks, Inc.
* Originally created by David Erickson, Stanford University
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
**/
package net.floodlightcontroller.core.web.serializers;
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.openflow.protocol.OFMatch;
import org.openflow.util.HexString;
public class OFMatchJSONSerializer extends JsonSerializer<OFMatch> {
/**
* Converts an IP in a 32 bit integer to a dotted-decimal string
* @param i The IP address in a 32 bit integer
* @return An IP address string in dotted-decimal
*/
private String intToIp(int i) {
return ((i >> 24 ) & 0xFF) + "." +
((i >> 16 ) & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
( i & 0xFF);
}
/**
* Performs the serialization of a OFMatch object
*/
@Override
public void serialize(OFMatch match, JsonGenerator jGen,
SerializerProvider serializer)
throws IOException, JsonProcessingException {
jGen.writeStartObject();
jGen.writeStringField("dataLayerDestination",
HexString.toHexString(match.getDataLayerDestination()));
jGen.writeStringField("dataLayerSource",
HexString.toHexString(match.getDataLayerSource()));
String dataType = Integer.toHexString(match.getDataLayerType());
while (dataType.length() < 4) {
dataType = "0".concat(dataType);
}
jGen.writeStringField("dataLayerType", "0x" + dataType);
jGen.writeNumberField("dataLayerVirtualLan",
match.getDataLayerVirtualLan());
jGen.writeNumberField("dataLayerVirtualLanPriorityCodePoint",
match.getDataLayerVirtualLanPriorityCodePoint());
jGen.writeNumberField("inputPort", match.getInputPort());
jGen.writeStringField("networkDestination",
intToIp(match.getNetworkDestination()));
jGen.writeNumberField("networkDestinationMaskLen",
match.getNetworkDestinationMaskLen());
jGen.writeNumberField("networkProtocol", match.getNetworkProtocol());
jGen.writeStringField("networkSource",
intToIp(match.getNetworkSource()));
jGen.writeNumberField("networkSourceMaskLen",
match.getNetworkSourceMaskLen());
jGen.writeNumberField("networkTypeOfService",
match.getNetworkTypeOfService());
jGen.writeNumberField("transportDestination",
match.getTransportDestination());
jGen.writeNumberField("transportSource",
match.getTransportSource());
jGen.writeNumberField("wildcards", match.getWildcards());
jGen.writeEndObject();
}
/**
* Tells SimpleModule that we are the serializer for OFMatch
*/
@Override
public Class<OFMatch> handledType() {
return OFMatch.class;
}
}
...@@ -103,6 +103,29 @@ public interface ILinkDiscovery { ...@@ -103,6 +103,29 @@ public interface ILinkDiscovery {
}; };
public enum LinkType { 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";
}
}
}; };
} }
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
...@@ -5,7 +5,6 @@ import java.util.HashSet; ...@@ -5,7 +5,6 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import net.floodlightcontroller.linkdiscovery.ILinkDiscovery.LinkType;
import net.floodlightcontroller.linkdiscovery.ILinkDiscoveryService; import net.floodlightcontroller.linkdiscovery.ILinkDiscoveryService;
import net.floodlightcontroller.linkdiscovery.LinkInfo; import net.floodlightcontroller.linkdiscovery.LinkInfo;
import net.floodlightcontroller.routing.Link; import net.floodlightcontroller.routing.Link;
...@@ -15,26 +14,8 @@ import org.restlet.resource.ServerResource; ...@@ -15,26 +14,8 @@ import org.restlet.resource.ServerResource;
public class LinksResource extends 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") @Get("json")
public Set<LinkWithType> retrieve() { public Set<LinkWithType> retrieve() {
String str;
ILinkDiscoveryService ld = (ILinkDiscoveryService)getContext().getAttributes(). ILinkDiscoveryService ld = (ILinkDiscoveryService)getContext().getAttributes().
get(ILinkDiscoveryService.class.getCanonicalName()); get(ILinkDiscoveryService.class.getCanonicalName());
Map<Link, LinkInfo> links = new HashMap<Link, LinkInfo>(); Map<Link, LinkInfo> links = new HashMap<Link, LinkInfo>();
...@@ -44,17 +25,7 @@ public class LinksResource extends ServerResource { ...@@ -44,17 +25,7 @@ public class LinksResource extends ServerResource {
links.putAll(ld.getLinks()); links.putAll(ld.getLinks());
for (Link link: links.keySet()) { for (Link link: links.keySet()) {
LinkInfo info = links.get(link); LinkInfo info = links.get(link);
LinkType type = ld.getLinkType(info); LinkWithType lwt = new LinkWithType(link, 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);
returnLinkSet.add(lwt); returnLinkSet.add(lwt);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment