Cleanup the REST processing of Topology-related REST calls, and
the Shortest Path computation REST call:
* Return value is Representation or null
* Use the default (explicit specified) Json serialization for each object,
instead of the explicit Jackson Mapper serialization.
* Added missing Javadoc comments
* Misc. cleanup (removed unused "log" variables, long lines breakdown cleanup,
etc).
Also, removed leftover check inside IntentHighObjectResource.java
that is not needed.
Change-Id: I7ce2dd7a686f9ab21d6222f57195e600cfc9f04c
diff --git a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighObjectResource.java b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighObjectResource.java
index dd6145d..fa66ea1 100644
--- a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighObjectResource.java
+++ b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighObjectResource.java
@@ -7,19 +7,17 @@
import net.onrc.onos.core.intent.IntentOperation;
import net.onrc.onos.core.intent.IntentOperationList;
import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
+
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* A class to access a single high-level intent.
*/
public class IntentHighObjectResource extends ServerResource {
- private static final Logger log = LoggerFactory.getLogger(IntentHighObjectResource.class);
// TODO need to assign proper application id.
private static final String APPLN_ID = "1";
@@ -32,15 +30,13 @@
*/
@Get("json")
public Representation retrieve() {
- IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
- getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
+ IPathCalcRuntimeService pathRuntime =
+ (IPathCalcRuntimeService) getContext().getAttributes()
+ .get(IPathCalcRuntimeService.class.getCanonicalName());
Representation result;
String intentId = (String) getRequestAttributes().get("intent-id");
- if (intentId == null) {
- return null; // Missing Intent ID
- }
//
// Get a single high-level Intent: use the Intent ID to find it
@@ -68,8 +64,9 @@
*/
@Delete("json")
public Representation remove() {
- IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
- getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
+ IPathCalcRuntimeService pathRuntime =
+ (IPathCalcRuntimeService) getContext().getAttributes()
+ .get(IPathCalcRuntimeService.class.getCanonicalName());
String intentId = (String) getRequestAttributes().get("intent-id");
diff --git a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighResource.java b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighResource.java
index d2ffdce..764c93f 100644
--- a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighResource.java
+++ b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentHighResource.java
@@ -1,5 +1,8 @@
package net.onrc.onos.core.intent.runtime.web;
+import java.io.IOException;
+import java.util.Collection;
+
import net.floodlightcontroller.util.MACAddress;
import net.onrc.onos.api.intent.ApplicationIntent;
import net.onrc.onos.api.rest.RestError;
@@ -12,6 +15,7 @@
import net.onrc.onos.core.intent.ShortestPathIntent;
import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
import net.onrc.onos.core.util.Dpid;
+
import org.codehaus.jackson.map.ObjectMapper;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
@@ -22,9 +26,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.IOException;
-import java.util.Collection;
-
/**
* A class to access the high-level intents.
*/
@@ -40,8 +41,9 @@
*/
@Get("json")
public Representation retrieve() throws IOException {
- IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
- getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
+ IPathCalcRuntimeService pathRuntime =
+ (IPathCalcRuntimeService) getContext().getAttributes()
+ .get(IPathCalcRuntimeService.class.getCanonicalName());
IntentMap intentMap = pathRuntime.getHighLevelIntents();
Collection<Intent> intents = intentMap.getAllIntents();
@@ -58,8 +60,9 @@
*/
@Post("json")
public Representation store(String jsonIntent) {
- IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext()
- .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
+ IPathCalcRuntimeService pathRuntime =
+ (IPathCalcRuntimeService) getContext().getAttributes()
+ .get(IPathCalcRuntimeService.class.getCanonicalName());
if (pathRuntime == null) {
log.warn("Failed to get path calc runtime");
return null;
@@ -143,8 +146,9 @@
*/
@Delete("json")
public Representation remove() {
- IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
- getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
+ IPathCalcRuntimeService pathRuntime =
+ (IPathCalcRuntimeService) getContext().getAttributes()
+ .get(IPathCalcRuntimeService.class.getCanonicalName());
// Delete all intents
// TODO: The implementation below is broken - waiting for the Java API
diff --git a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowObjectResource.java b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowObjectResource.java
index 1f99152..031acf1 100644
--- a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowObjectResource.java
+++ b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowObjectResource.java
@@ -5,18 +5,16 @@
import net.onrc.onos.core.intent.Intent;
import net.onrc.onos.core.intent.IntentMap;
import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
+
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* A class to access a single low-level intent.
*/
public class IntentLowObjectResource extends ServerResource {
- private static final Logger log = LoggerFactory.getLogger(IntentLowObjectResource.class);
// TODO need to assign proper application id.
private static final String APPLN_ID = "1";
@@ -28,8 +26,9 @@
*/
@Get("json")
public Representation retrieve() {
- IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
- getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
+ IPathCalcRuntimeService pathRuntime =
+ (IPathCalcRuntimeService) getContext().getAttributes()
+ .get(IPathCalcRuntimeService.class.getCanonicalName());
Representation result;
@@ -47,7 +46,7 @@
setStatus(Status.CLIENT_ERROR_NOT_FOUND);
final RestError notFound =
RestError.createRestError(RestErrorCodes.RestErrorCode.INTENT_NOT_FOUND,
- applnIntentId);
+ applnIntentId);
result = toRepresentation(notFound, null);
}
diff --git a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowResource.java b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowResource.java
index 7a15c26..c2e6e95 100644
--- a/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowResource.java
+++ b/src/main/java/net/onrc/onos/core/intent/runtime/web/IntentLowResource.java
@@ -1,22 +1,19 @@
package net.onrc.onos.core.intent.runtime.web;
+import java.util.Collection;
+
import net.onrc.onos.core.intent.Intent;
import net.onrc.onos.core.intent.IntentMap;
import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
+
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.Collection;
/**
* A class to access the low-level intents.
*/
public class IntentLowResource extends ServerResource {
- private static final Logger log = LoggerFactory.getLogger(IntentLowResource.class);
-
/**
* Gets all low-level intents.
*
@@ -24,8 +21,9 @@
*/
@Get("json")
public Representation retrieve() {
- IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
- getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
+ IPathCalcRuntimeService pathRuntime =
+ (IPathCalcRuntimeService) getContext().getAttributes()
+ .get(IPathCalcRuntimeService.class.getCanonicalName());
//
// Get all low-level intents
diff --git a/src/main/java/net/onrc/onos/core/intent/runtime/web/ShortestPathResource.java b/src/main/java/net/onrc/onos/core/intent/runtime/web/ShortestPathResource.java
index 4989d6d..28002ba 100644
--- a/src/main/java/net/onrc/onos/core/intent/runtime/web/ShortestPathResource.java
+++ b/src/main/java/net/onrc/onos/core/intent/runtime/web/ShortestPathResource.java
@@ -1,6 +1,5 @@
package net.onrc.onos.core.intent.runtime.web;
-import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
@@ -11,12 +10,9 @@
import net.onrc.onos.core.topology.LinkEvent;
import net.onrc.onos.core.topology.Switch;
import net.onrc.onos.core.topology.Topology;
-import net.onrc.onos.core.topology.serializers.LinkSerializer;
import net.onrc.onos.core.util.Dpid;
-import org.codehaus.jackson.Version;
-import org.codehaus.jackson.map.ObjectMapper;
-import org.codehaus.jackson.map.module.SimpleModule;
+import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
@@ -26,31 +22,20 @@
* A class to access Shortest-Path information between switches.
*/
public class ShortestPathResource extends ServerResource {
-
private static final Logger log = LoggerFactory.getLogger(ShortestPathResource.class);
/**
* Gets the Shortest-Path infomration between switches.
*
- * @return a JSON string with the Shortest-Path information between
- * switches. The Shortest-Path information is an ordered collection of
- * Links.
+ * @return a Representation with the Shortest-Path information between
+ * switches if found, otherwise null. The Shortest-Path information is an
+ * ordered collection of Links.
*/
@Get("json")
- public String retrieve() {
+ public Representation retrieve() {
ITopologyService topologyService =
- (ITopologyService) getContext().getAttributes().
- get(ITopologyService.class.getCanonicalName());
-
- Topology topology = topologyService.getTopology();
-
- //
- // Prepare the JSON serializer
- //
- ObjectMapper mapper = new ObjectMapper();
- SimpleModule module = new SimpleModule("module", new Version(1, 0, 0, null));
- module.addSerializer(new LinkSerializer());
- mapper.registerModule(module);
+ (ITopologyService) getContext().getAttributes()
+ .get(ITopologyService.class.getCanonicalName());
//
// Fetch the attributes
@@ -65,12 +50,13 @@
// Do the Shortest Path computation and return the result: a list of
// links.
//
+ Topology topology = topologyService.getTopology();
topology.acquireReadLock();
try {
Switch srcSwitch = topology.getSwitch(srcDpid.value());
Switch dstSwitch = topology.getSwitch(dstDpid.value());
if ((srcSwitch == null) || (dstSwitch == null)) {
- return "";
+ return null;
}
ConstrainedBFSTree bfsTree = new ConstrainedBFSTree(srcSwitch);
Path path = bfsTree.getPath(dstSwitch);
@@ -81,14 +67,11 @@
linkEvent.getDst().getDpid(),
linkEvent.getDst().getNumber());
if (link == null) {
- return "";
+ return null;
}
links.add(link);
}
- return mapper.writeValueAsString(links);
- } catch (IOException e) {
- log.error("Error writing Shortest Path to JSON", e);
- return "";
+ return toRepresentation(links, null);
} finally {
topology.releaseReadLock();
}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/TopologyDevicesResource.java b/src/main/java/net/onrc/onos/core/topology/web/TopologyDevicesResource.java
index 23f86e8..4ca9927 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/TopologyDevicesResource.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/TopologyDevicesResource.java
@@ -1,43 +1,34 @@
package net.onrc.onos.core.topology.web;
-import java.io.IOException;
-
import net.onrc.onos.core.topology.ITopologyService;
import net.onrc.onos.core.topology.Topology;
-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.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+/**
+ * A class to access hosts information from the network topology.
+ */
public class TopologyDevicesResource extends ServerResource {
- private static final Logger log = LoggerFactory.getLogger(TopologyDevicesResource.class);
-
+ /**
+ * Gets the hosts information from the network topology.
+ *
+ * @return a Representation of a Collection of hosts from the network
+ * topology.
+ */
@Get("json")
- public String retrieve() {
- ITopologyService topologyService = (ITopologyService) getContext().getAttributes().
- get(ITopologyService.class.getCanonicalName());
+ public Representation retrieve() {
+ ITopologyService topologyService =
+ (ITopologyService) getContext().getAttributes()
+ .get(ITopologyService.class.getCanonicalName());
Topology topology = topologyService.getTopology();
-
- ObjectMapper mapper = new ObjectMapper();
- SimpleModule module = new SimpleModule("module", new Version(1, 0, 0, null));
- module.addSerializer(new DeviceSerializer());
- mapper.registerModule(module);
-
topology.acquireReadLock();
try {
- return mapper.writeValueAsString(topology.getDevices());
- } catch (IOException e) {
- log.error("Error writing device list to JSON", e);
- return "";
+ return toRepresentation(topology.getDevices(), null);
} finally {
topology.releaseReadLock();
}
}
-
}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/TopologyLinksResource.java b/src/main/java/net/onrc/onos/core/topology/web/TopologyLinksResource.java
index b1b6de0..7d287d3 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/TopologyLinksResource.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/TopologyLinksResource.java
@@ -1,41 +1,32 @@
package net.onrc.onos.core.topology.web;
-import java.io.IOException;
-
import net.onrc.onos.core.topology.ITopologyService;
import net.onrc.onos.core.topology.Topology;
-import net.onrc.onos.core.topology.serializers.LinkSerializer;
-import org.codehaus.jackson.Version;
-import org.codehaus.jackson.map.ObjectMapper;
-import org.codehaus.jackson.map.module.SimpleModule;
+import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+/**
+ * A class to access links information from the network topology.
+ */
public class TopologyLinksResource extends ServerResource {
-
- private static final Logger log = LoggerFactory.getLogger(TopologyLinksResource.class);
-
+ /**
+ * Gets the links information from the network topology.
+ *
+ * @return a Representation of a Collection of links from the network
+ * topology.
+ */
@Get("json")
- public String retrieve() {
- ITopologyService topologyService = (ITopologyService) getContext().getAttributes().
- get(ITopologyService.class.getCanonicalName());
+ public Representation retrieve() {
+ ITopologyService topologyService =
+ (ITopologyService) getContext().getAttributes()
+ .get(ITopologyService.class.getCanonicalName());
Topology topology = topologyService.getTopology();
-
- ObjectMapper mapper = new ObjectMapper();
- SimpleModule module = new SimpleModule("module", new Version(1, 0, 0, null));
- module.addSerializer(new LinkSerializer());
- mapper.registerModule(module);
-
+ topology.acquireReadLock();
try {
- topology.acquireReadLock();
- return mapper.writeValueAsString(topology.getLinks());
- } catch (IOException e) {
- log.error("Error writing link list to JSON", e);
- return "";
+ return toRepresentation(topology.getLinks(), null);
} finally {
topology.releaseReadLock();
}
diff --git a/src/main/java/net/onrc/onos/core/topology/web/TopologySwitchesResource.java b/src/main/java/net/onrc/onos/core/topology/web/TopologySwitchesResource.java
index 0ac8321..0d41ddd 100644
--- a/src/main/java/net/onrc/onos/core/topology/web/TopologySwitchesResource.java
+++ b/src/main/java/net/onrc/onos/core/topology/web/TopologySwitchesResource.java
@@ -1,43 +1,32 @@
package net.onrc.onos.core.topology.web;
-import java.io.IOException;
-
import net.onrc.onos.core.topology.ITopologyService;
import net.onrc.onos.core.topology.Topology;
-import net.onrc.onos.core.topology.serializers.PortSerializer;
-import net.onrc.onos.core.topology.serializers.SwitchSerializer;
-import org.codehaus.jackson.Version;
-import org.codehaus.jackson.map.ObjectMapper;
-import org.codehaus.jackson.map.module.SimpleModule;
+import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+/**
+ * A class to access switches and ports information from the network topology.
+ */
public class TopologySwitchesResource extends ServerResource {
-
- private static final Logger log = LoggerFactory.getLogger(TopologySwitchesResource.class);
-
+ /**
+ * Gets the switches and ports information from the network topology.
+ *
+ * @return a Representation of a Collection of switches from the network
+ * topology. Each switch contains the switch ports.
+ */
@Get("json")
- public String retrieve() {
- ITopologyService topologyService = (ITopologyService) getContext().getAttributes().
- get(ITopologyService.class.getCanonicalName());
+ public Representation retrieve() {
+ ITopologyService topologyService =
+ (ITopologyService) getContext().getAttributes()
+ .get(ITopologyService.class.getCanonicalName());
Topology topology = topologyService.getTopology();
-
- 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);
-
+ topology.acquireReadLock();
try {
- topology.acquireReadLock();
- return mapper.writeValueAsString(topology.getSwitches());
- } catch (IOException e) {
- log.error("Error writing switch list to JSON", e);
- return "";
+ return toRepresentation(topology.getSwitches(), null);
} finally {
topology.releaseReadLock();
}