Mild REST API refactoring.
Change-Id: Ieddbbd02328043118ad64077f8eda746eb2b3a9f
diff --git a/web/api/src/main/java/org/onosproject/rest/AbstractWebResource.java b/web/api/src/main/java/org/onosproject/rest/AbstractWebResource.java
index 0a8f2c1..49e4dca 100644
--- a/web/api/src/main/java/org/onosproject/rest/AbstractWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/AbstractWebResource.java
@@ -17,11 +17,10 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import org.onlab.util.ItemNotFoundException;
+import org.onlab.rest.BaseResource;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.CodecService;
import org.onosproject.codec.JsonCodec;
-import org.onlab.rest.BaseResource;
/**
* Abstract REST resource.
@@ -61,21 +60,4 @@
return result;
}
- /**
- * Returns the specified item if that items is null; otherwise throws
- * not found exception.
- *
- * @param item item to check
- * @param message not found message
- * @param <T> item type
- * @return item if not null
- * @throws org.onlab.util.ItemNotFoundException if item is null
- */
- protected <T> T nullIsNotFound(T item, String message) {
- if (item == null) {
- throw new ItemNotFoundException(message);
- }
- return item;
- }
-
}
diff --git a/web/api/src/main/java/org/onosproject/rest/ClusterWebResource.java b/web/api/src/main/java/org/onosproject/rest/ClusterWebResource.java
index 5f2fde9..7f15bdc 100644
--- a/web/api/src/main/java/org/onosproject/rest/ClusterWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/ClusterWebResource.java
@@ -33,6 +33,8 @@
import java.util.HashSet;
import java.util.List;
+import static org.onlab.util.Tools.nullIsNotFound;
+
/**
* REST resource for interacting with the ONOS cluster subsystem.
*/
diff --git a/web/api/src/main/java/org/onosproject/rest/ComponentConfigWebResource.java b/web/api/src/main/java/org/onosproject/rest/ComponentConfigWebResource.java
index 07fe8a1..7de3d70 100644
--- a/web/api/src/main/java/org/onosproject/rest/ComponentConfigWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/ComponentConfigWebResource.java
@@ -29,6 +29,8 @@
import java.io.InputStream;
import java.util.Set;
+import static org.onlab.util.Tools.nullIsNotFound;
+
/**
* REST resource for cluster-wide component configuration.
*/
diff --git a/web/api/src/main/java/org/onosproject/rest/DevicesWebResource.java b/web/api/src/main/java/org/onosproject/rest/DevicesWebResource.java
index 3f8417b..91668d8 100644
--- a/web/api/src/main/java/org/onosproject/rest/DevicesWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/DevicesWebResource.java
@@ -27,6 +27,7 @@
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsNotFound;
import static org.onosproject.net.DeviceId.deviceId;
/**
diff --git a/web/api/src/main/java/org/onosproject/rest/HostsWebResource.java b/web/api/src/main/java/org/onosproject/rest/HostsWebResource.java
index 925df13..58787de 100644
--- a/web/api/src/main/java/org/onosproject/rest/HostsWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/HostsWebResource.java
@@ -27,6 +27,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
+import static org.onlab.util.Tools.nullIsNotFound;
import static org.onosproject.net.HostId.hostId;
/**
diff --git a/web/api/src/main/java/org/onosproject/rest/IntentsWebResource.java b/web/api/src/main/java/org/onosproject/rest/IntentsWebResource.java
index 7e62953..9bbcd6b 100644
--- a/web/api/src/main/java/org/onosproject/rest/IntentsWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/IntentsWebResource.java
@@ -41,6 +41,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
+import static org.onlab.util.Tools.nullIsNotFound;
import static org.onosproject.net.intent.IntentState.FAILED;
import static org.onosproject.net.intent.IntentState.WITHDRAWN;
import static org.slf4j.LoggerFactory.getLogger;
diff --git a/web/api/src/main/java/org/onosproject/rest/TopologyWebResource.java b/web/api/src/main/java/org/onosproject/rest/TopologyWebResource.java
index a10232c..c1a8fcf 100644
--- a/web/api/src/main/java/org/onosproject/rest/TopologyWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/TopologyWebResource.java
@@ -15,15 +15,9 @@
*/
package org.onosproject.rest;
-import java.util.List;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Lists;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
@@ -33,9 +27,15 @@
import org.onosproject.net.topology.TopologyCluster;
import org.onosproject.net.topology.TopologyService;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.google.common.collect.Lists;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.util.List;
+
+import static org.onlab.util.Tools.nullIsNotFound;
/**
* REST resource for interacting with the inventory of clusters.
@@ -55,8 +55,7 @@
@Produces(MediaType.APPLICATION_JSON)
public Response getTopology() {
Topology topology = get(TopologyService.class).currentTopology();
- ObjectNode root =
- codec(Topology.class).encode(topology, this);
+ ObjectNode root = codec(Topology.class).encode(topology, this);
return ok(root).build();
}
@@ -69,11 +68,10 @@
@Produces(MediaType.APPLICATION_JSON)
@Path("clusters")
public Response getClusters() {
- Topology topology = get(TopologyService.class).currentTopology();
- Iterable<TopologyCluster> clusters =
- get(TopologyService.class).getClusters(topology);
- ObjectNode root =
- encodeArray(TopologyCluster.class, "clusters", clusters);
+ TopologyService service = get(TopologyService.class);
+ Topology topology = service.currentTopology();
+ Iterable<TopologyCluster> clusters = service.getClusters(topology);
+ ObjectNode root = encodeArray(TopologyCluster.class, "clusters", clusters);
return ok(root).build();
}
@@ -88,17 +86,18 @@
@Path("clusters/{id}")
public Response getCluster(@PathParam("id") int clusterId) {
Topology topology = get(TopologyService.class).currentTopology();
- TopologyCluster cluster =
- nullIsNotFound(
- get(TopologyService.class)
- .getCluster(topology,
- ClusterId.clusterId(clusterId)),
- CLUSTER_NOT_FOUND);
- ObjectNode root =
- codec(TopologyCluster.class).encode(cluster, this);
+ TopologyCluster cluster = getTopologyCluster(clusterId, topology);
+ ObjectNode root = codec(TopologyCluster.class).encode(cluster, this);
return ok(root).build();
}
+ private TopologyCluster getTopologyCluster(int clusterId, Topology topology) {
+ return nullIsNotFound(
+ get(TopologyService.class)
+ .getCluster(topology, ClusterId.clusterId(clusterId)),
+ CLUSTER_NOT_FOUND);
+ }
+
/**
* Gets devices for a topology cluster for a REST GET operation.
*
@@ -109,24 +108,16 @@
@Produces(MediaType.APPLICATION_JSON)
@Path("clusters/{id}/devices")
public Response getClusterDevices(@PathParam("id") int clusterId) {
- Topology topology = get(TopologyService.class).currentTopology();
- TopologyCluster cluster =
- nullIsNotFound(
- get(TopologyService.class)
- .getCluster(topology,
- ClusterId.clusterId(clusterId)),
- CLUSTER_NOT_FOUND);
+ TopologyService service = get(TopologyService.class);
+ Topology topology = service.currentTopology();
+ TopologyCluster cluster = getTopologyCluster(clusterId, topology);
List<DeviceId> deviceIds =
- Lists.newArrayList(get(TopologyService.class)
- .getClusterDevices(topology, cluster));
+ Lists.newArrayList(service.getClusterDevices(topology, cluster));
ObjectNode root = mapper().createObjectNode();
ArrayNode devicesNode = root.putArray("devices");
-
- for (DeviceId deviceId : deviceIds) {
- devicesNode.add(deviceId.toString());
- }
+ deviceIds.forEach(id -> devicesNode.add(id.toString()));
return ok(root).build();
}
@@ -141,10 +132,7 @@
@Path("clusters/{id}/links")
public Response getClusterLinks(@PathParam("id") int clusterId) {
Topology topology = get(TopologyService.class).currentTopology();
- TopologyCluster cluster =
- nullIsNotFound(get(TopologyService.class).getCluster(topology,
- ClusterId.clusterId(clusterId)),
- CLUSTER_NOT_FOUND);
+ TopologyCluster cluster = getTopologyCluster(clusterId, topology);
List<Link> links =
Lists.newArrayList(get(TopologyService.class)