Built REST APIs to look at the in-memory network graph state.
Amended to clean up serializer code.
Change-Id: Ie020d1173fa108eec12e324e3ae37353c7ca8a00
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/FloodlightToOnosMappers.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/FloodlightToOnosMappers.java
index 8139737..ebff81d 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/FloodlightToOnosMappers.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/FloodlightToOnosMappers.java
@@ -18,15 +18,17 @@
public static Port map(NetworkGraph graph, SwitchImpl sw, OFPhysicalPort port) {
PortImpl onosPort = new PortImpl(graph, sw, new Long(port.getPortNumber()));
+ onosPort.setDescription(port.getName());
return onosPort;
}
public static Link map(NetworkGraph graph, net.floodlightcontroller.routing.Link link) {
- Port srcPort = graph.getSwitch(link.getSrc()).getPort(Long.valueOf(link.getSrcPort()));
- Port dstPort = graph.getSwitch(link.getDst()).getPort(Long.valueOf(link.getDstPort()));
- LinkImpl onosLink = new LinkImpl(graph, srcPort, dstPort);
-
+ //Port srcPort = graph.getSwitch(link.getSrc()).getPort(Long.valueOf(link.getSrcPort()));
+ //Port dstPort = graph.getSwitch(link.getDst()).getPort(Long.valueOf(link.getDstPort()));
+ //LinkImpl onosLink = new LinkImpl(graph, srcPort, dstPort);
+ LinkImpl onosLink = new LinkImpl(graph, link.getSrc(), (long)link.getSrcPort(),
+ link.getDst(), (long)link.getDstPort());
+
return onosLink;
-
}
}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/LinkImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/LinkImpl.java
index b1fc9b8..9b43171 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/LinkImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/LinkImpl.java
@@ -15,7 +15,38 @@
protected static final int DEFAULT_COST = 1;
protected int cost = DEFAULT_COST;
+
+ private long srcSwitchDpid;
+ private long srcPortNumber;
+ private long dstSwitchDpid;
+ private long dstPortNumber;
+
+ /**
+ * Constructor for when a new link is being created because of a link
+ * discovery event.
+ * @param graph
+ * @param srcSwitchDpid
+ * @param srcPortNumber
+ * @param dstSwitchDpid
+ * @param dstPortNumber
+ */
+ public LinkImpl(NetworkGraph graph, long srcSwitchDpid, long srcPortNumber,
+ long dstSwitchDpid, long dstPortNumber) {
+ super(graph);
+
+ this.srcSwitchDpid = srcSwitchDpid;
+ this.srcPortNumber = srcPortNumber;
+ this.dstSwitchDpid = dstSwitchDpid;
+ this.dstPortNumber = dstPortNumber;
+ }
+ /**
+ * Constructor for when a link is read from the database and the Ports
+ * already exist in the in-memory network graph.
+ * @param graph
+ * @param srcPort
+ * @param dstPort
+ */
public LinkImpl(NetworkGraph graph, Port srcPort, Port dstPort) {
super(graph);
this.srcPort = srcPort;
@@ -70,22 +101,22 @@
@Override
public Long getSourceSwitchDpid() {
- return srcPort.getSwitch().getDpid();
+ return srcSwitchDpid;
}
@Override
public Long getSourcePortNumber() {
- return srcPort.getNumber();
+ return srcPortNumber;
}
@Override
public Long getDestinationSwitchDpid() {
- return dstPort.getSwitch().getDpid();
+ return dstSwitchDpid;
}
@Override
public Long getDestinationPortNumber() {
- return dstPort.getNumber();
+ return dstPortNumber;
}
@Override
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/Port.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/Port.java
index ae955d1..753532f 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/Port.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/Port.java
@@ -12,6 +12,7 @@
public interface Port {
public Long getNumber();
public Long getHardwareAddress();
+ public String getDescription();
public Switch getSwitch();
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortImpl.java
index de18631..6f860d0 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortImpl.java
@@ -13,7 +13,10 @@
public class PortImpl extends NetworkGraphObject implements Port {
private Switch sw;
+
private Long number;
+ private String description;
+
protected Link outgoingLink;
protected Link incomingLink;
protected Set<Device> devices;
@@ -29,6 +32,15 @@
public Long getNumber() {
return number;
}
+
+ @Override
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
@Override
public Long getHardwareAddress() {
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SouthboundNetworkGraph.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SouthboundNetworkGraph.java
index e9de665..1bbad88 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SouthboundNetworkGraph.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SouthboundNetworkGraph.java
@@ -133,6 +133,8 @@
RCPort rcPort = new RCPort(port.getSwitch().getDpid(), (long)port.getNumber());
rcPort.setStatus(RCPort.STATUS.ACTIVE);
+ // TODO add description into RCPort
+ //rcPort.setDescription(port.getDescription());
rcSwitch.addPortId(rcPort.getId());
writeObject(rcPort);
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/serializers/LinkSerializer.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/serializers/LinkSerializer.java
new file mode 100644
index 0000000..7708bf1
--- /dev/null
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/serializers/LinkSerializer.java
@@ -0,0 +1,35 @@
+package net.onrc.onos.ofcontroller.networkgraph.serializers;
+
+import java.io.IOException;
+
+import net.onrc.onos.ofcontroller.networkgraph.Link;
+
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.map.ser.std.SerializerBase;
+import org.openflow.util.HexString;
+
+public class LinkSerializer extends SerializerBase<Link> {
+
+ public LinkSerializer() {
+ super(Link.class);
+ }
+
+ @Override
+ public void serialize(Link link, JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException, JsonGenerationException {
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeStringField("src-switch",
+ HexString.toHexString(link.getSourceSwitch().getDpid()));
+ jsonGenerator.writeNumberField("src-port",
+ link.getSourcePort().getNumber());
+ jsonGenerator.writeStringField("dst-switch",
+ HexString.toHexString(link.getDestinationSwitch().getDpid()));
+ jsonGenerator.writeNumberField("src-port",
+ link.getDestinationPort().getNumber());
+ jsonGenerator.writeEndObject();
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/serializers/PortSerializer.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/serializers/PortSerializer.java
new file mode 100644
index 0000000..7469dbe
--- /dev/null
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/serializers/PortSerializer.java
@@ -0,0 +1,31 @@
+package net.onrc.onos.ofcontroller.networkgraph.serializers;
+
+import java.io.IOException;
+
+import net.onrc.onos.ofcontroller.networkgraph.Port;
+
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.JsonProcessingException;
+import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.map.ser.std.SerializerBase;
+
+public class PortSerializer extends SerializerBase<Port> {
+
+ public PortSerializer() {
+ super(Port.class);
+ }
+
+ @Override
+ public void serialize(Port port, JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider)
+ throws IOException, JsonProcessingException {
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeStringField("state", "ACTIVE");
+ jsonGenerator.writeNumberField("number", port.getNumber());
+ jsonGenerator.writeStringField("desc", port.getDescription());
+ jsonGenerator.writeArrayFieldStart("devices");
+ jsonGenerator.writeEndArray();
+ jsonGenerator.writeEndObject();
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/serializers/SwitchSerializer.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/serializers/SwitchSerializer.java
new file mode 100644
index 0000000..774377d
--- /dev/null
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/serializers/SwitchSerializer.java
@@ -0,0 +1,36 @@
+package net.onrc.onos.ofcontroller.networkgraph.serializers;
+
+import java.io.IOException;
+
+import net.onrc.onos.ofcontroller.networkgraph.Port;
+import net.onrc.onos.ofcontroller.networkgraph.Switch;
+
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.JsonProcessingException;
+import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.map.ser.std.SerializerBase;
+import org.openflow.util.HexString;
+
+public class SwitchSerializer extends SerializerBase<Switch> {
+
+ public SwitchSerializer() {
+ super(Switch.class);
+ }
+
+ @Override
+ public void serialize(Switch sw, JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider) throws IOException,
+ JsonProcessingException {
+
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeStringField("dpid", HexString.toHexString(sw.getDpid()));
+ jsonGenerator.writeStringField("state", "ACTIVE");
+ jsonGenerator.writeArrayFieldStart("ports");
+ for (Port port : sw.getPorts()) {
+ jsonGenerator.writeObject(port);
+ }
+ jsonGenerator.writeEndArray();
+ jsonGenerator.writeEndObject();
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/NetworkGraphLinksResource.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/NetworkGraphLinksResource.java
new file mode 100644
index 0000000..a244dc1
--- /dev/null
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/NetworkGraphLinksResource.java
@@ -0,0 +1,40 @@
+package net.onrc.onos.ofcontroller.networkgraph.web;
+
+import java.io.IOException;
+
+import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
+import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
+import net.onrc.onos.ofcontroller.networkgraph.serializers.LinkSerializer;
+
+import org.codehaus.jackson.Version;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.module.SimpleModule;
+import org.restlet.resource.Get;
+import org.restlet.resource.ServerResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class NetworkGraphLinksResource extends ServerResource {
+
+ private static final Logger log = LoggerFactory.getLogger(NetworkGraphLinksResource.class);
+
+ @Get("json")
+ public String retrieve() {
+ INetworkGraphService networkGraphService = (INetworkGraphService) getContext().getAttributes().
+ get(INetworkGraphService.class.getCanonicalName());
+
+ NetworkGraph graph = networkGraphService.getNetworkGraph();
+
+ ObjectMapper mapper = new ObjectMapper();
+ SimpleModule module = new SimpleModule("module", new Version(1, 0, 0, null));
+ module.addSerializer(new LinkSerializer());
+ mapper.registerModule(module);
+
+ try {
+ return mapper.writeValueAsString(graph.getLinks());
+ } catch (IOException e) {
+ log.error("Error writing link list to JSON", e);
+ return "";
+ }
+ }
+}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/NetworkGraphSwitchesResource.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/NetworkGraphSwitchesResource.java
new file mode 100644
index 0000000..92e94f6
--- /dev/null
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/NetworkGraphSwitchesResource.java
@@ -0,0 +1,43 @@
+package net.onrc.onos.ofcontroller.networkgraph.web;
+
+import java.io.IOException;
+
+import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
+import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
+import net.onrc.onos.ofcontroller.networkgraph.serializers.PortSerializer;
+import net.onrc.onos.ofcontroller.networkgraph.serializers.SwitchSerializer;
+
+import org.codehaus.jackson.Version;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.module.SimpleModule;
+import org.restlet.resource.Get;
+import org.restlet.resource.ServerResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class NetworkGraphSwitchesResource extends ServerResource {
+
+ private static final Logger log = LoggerFactory.getLogger(NetworkGraphSwitchesResource.class);
+
+ @Get("json")
+ public String retrieve() {
+ INetworkGraphService networkGraphService = (INetworkGraphService) getContext().getAttributes().
+ get(INetworkGraphService.class.getCanonicalName());
+
+ NetworkGraph graph = networkGraphService.getNetworkGraph();
+
+ ObjectMapper mapper = new ObjectMapper();
+ SimpleModule module = new SimpleModule("module", new Version(1, 0, 0, null));
+ module.addSerializer(new SwitchSerializer());
+ module.addSerializer(new PortSerializer());
+ mapper.registerModule(module);
+
+ try {
+ return mapper.writeValueAsString(graph.getSwitches());
+ } catch (IOException e) {
+ log.error("Error writing switch list to JSON", e);
+ return "";
+ }
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/NetworkGraphWebRoutable.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/NetworkGraphWebRoutable.java
index 4dd82e6..d6f1f95 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/NetworkGraphWebRoutable.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/NetworkGraphWebRoutable.java
@@ -11,15 +11,17 @@
@Override
public Restlet getRestlet(Context context) {
Router router = new Router(context);
- router.attach("/switches/json", RamcloudSwitchesResource.class);
- router.attach("/links/json", RamcloudLinksResource.class);
+ router.attach("/rc/switches/json", RamcloudSwitchesResource.class);
+ router.attach("/rc/links/json", RamcloudLinksResource.class);
+ router.attach("/ng/switches/json", NetworkGraphSwitchesResource.class);
+ router.attach("/ng/links/json", NetworkGraphLinksResource.class);
return router;
}
@Override
public String basePath() {
// TODO Auto-generated method stub
- return "/wm/onos/rc";
+ return "/wm/onos";
}
}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/RamcloudSwitchesResource.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/RamcloudSwitchesResource.java
index 98e1050..e8998b0 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/RamcloudSwitchesResource.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/web/RamcloudSwitchesResource.java
@@ -1,7 +1,6 @@
package net.onrc.onos.ofcontroller.networkgraph.web;
import net.onrc.onos.datastore.topology.RCSwitch;
-import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
@@ -10,10 +9,6 @@
@Get("json")
public Iterable<RCSwitch> retrieve() {
- INetworkGraphService networkGraphService =
- (INetworkGraphService) getContext().getAttributes().
- get(INetworkGraphService.class.getCanonicalName());
-
return RCSwitch.getAllSwitches();
}