Add REST API for a device.
In this commit, only added /wm/onos/topology/devices/json to get all device information.
Change-Id: I9a56be1335914ba965f1e921960d3cabf813c61c
diff --git a/src/main/java/net/onrc/onos/core/topology/NetworkGraph.java b/src/main/java/net/onrc/onos/core/topology/NetworkGraph.java
index 99f7eda..d450784 100644
--- a/src/main/java/net/onrc/onos/core/topology/NetworkGraph.java
+++ b/src/main/java/net/onrc/onos/core/topology/NetworkGraph.java
@@ -83,4 +83,6 @@
* Release the read lock on the topology.
*/
public void releaseReadLock();
+
+ public Iterable<Device> getDevices();
}
diff --git a/src/main/java/net/onrc/onos/core/topology/NetworkGraphImpl.java b/src/main/java/net/onrc/onos/core/topology/NetworkGraphImpl.java
index 115afb5..b20ab6b 100644
--- a/src/main/java/net/onrc/onos/core/topology/NetworkGraphImpl.java
+++ b/src/main/java/net/onrc/onos/core/topology/NetworkGraphImpl.java
@@ -105,6 +105,11 @@
return mac2Device.get(address);
}
+ @Override
+ public Iterable<Device> getDevices() {
+ return Collections.unmodifiableCollection(mac2Device.values());
+ }
+
protected void putDevice(Device device) {
mac2Device.put(device.getMacAddress(), device);
}
diff --git a/src/main/java/net/onrc/onos/core/topology/serializers/DeviceSerializer.java b/src/main/java/net/onrc/onos/core/topology/serializers/DeviceSerializer.java
new file mode 100644
index 0000000..09a1f57
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/topology/serializers/DeviceSerializer.java
@@ -0,0 +1,36 @@
+package net.onrc.onos.core.topology.serializers;
+
+import java.io.IOException;
+
+import net.onrc.onos.core.topology.Device;
+import net.onrc.onos.core.topology.Port;
+import net.onrc.onos.core.topology.PortEvent.SwitchPort;
+
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.map.ser.std.SerializerBase;
+
+public class DeviceSerializer extends SerializerBase<Device> {
+
+ public DeviceSerializer() {
+ super(Device.class);
+ }
+
+ @Override
+ public void serialize(Device dev, JsonGenerator jsonGenerator,
+ SerializerProvider serializerProvider) throws IOException,
+ JsonGenerationException {
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeNumberField("mac", dev.getMacAddress().toLong());
+ jsonGenerator.writeFieldName("attachmentPoints");
+ jsonGenerator.writeStartArray();
+ for (Port port : dev.getAttachmentPoints()) {
+ SwitchPort sp = new SwitchPort(port.getDpid(), port.getNumber());
+ jsonGenerator.writeObject(sp);
+ }
+ jsonGenerator.writeEndArray();
+ jsonGenerator.writeEndObject();
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/NetworkGraphDevicesResource.java b/src/main/java/net/onrc/onos/core/topology/web/NetworkGraphDevicesResource.java
new file mode 100644
index 0000000..84aaeba
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/topology/web/NetworkGraphDevicesResource.java
@@ -0,0 +1,43 @@
+package net.onrc.onos.core.topology.web;
+
+import java.io.IOException;
+
+import net.onrc.onos.core.topology.INetworkGraphService;
+import net.onrc.onos.core.topology.NetworkGraph;
+import net.onrc.onos.core.topology.serializers.DeviceSerializer;
+
+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 NetworkGraphDevicesResource extends ServerResource {
+ private static final Logger log = LoggerFactory.getLogger(NetworkGraphDevicesResource.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 DeviceSerializer());
+ mapper.registerModule(module);
+
+ graph.acquireReadLock();
+ try {
+ return mapper.writeValueAsString(graph.getDevices());
+ } catch (IOException e) {
+ log.error("Error writing device list to JSON", e);
+ return "";
+ } finally {
+ graph.releaseReadLock();
+ }
+ }
+
+}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/NetworkGraphWebRoutable.java b/src/main/java/net/onrc/onos/core/topology/web/NetworkGraphWebRoutable.java
index b159fe6..c0cf272 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/NetworkGraphWebRoutable.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/NetworkGraphWebRoutable.java
@@ -19,6 +19,7 @@
// Topology API
router.attach("/switches/json", NetworkGraphSwitchesResource.class);
router.attach("/links/json", NetworkGraphLinksResource.class);
+ router.attach("/devices/json", NetworkGraphDevicesResource.class);
// TODO: Move the Shortest Path REST API to the Intent framework
router.attach("/shortest-path/{src-dpid}/{dst-dpid}/json", NetworkGraphShortestPathResource.class);