Thomas Vachuska

Mild REST API refactoring.

Change-Id: Ieddbbd02328043118ad64077f8eda746eb2b3a9f
......@@ -17,11 +17,10 @@ package org.onosproject.rest;
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 @@ public class AbstractWebResource extends BaseResource implements CodecContext {
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;
}
}
......
......@@ -33,6 +33,8 @@ import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import static org.onlab.util.Tools.nullIsNotFound;
/**
* REST resource for interacting with the ONOS cluster subsystem.
*/
......
......@@ -29,6 +29,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import static org.onlab.util.Tools.nullIsNotFound;
/**
* REST resource for cluster-wide component configuration.
*/
......
......@@ -27,6 +27,7 @@ import javax.ws.rs.core.Response;
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;
/**
......
......@@ -27,6 +27,7 @@ import org.onosproject.net.host.HostService;
import com.fasterxml.jackson.databind.node.ObjectNode;
import static org.onlab.util.Tools.nullIsNotFound;
import static org.onosproject.net.HostId.hostId;
/**
......
......@@ -41,6 +41,7 @@ import org.slf4j.Logger;
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;
......
......@@ -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.Topology;
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 @@ public class TopologyWebResource extends AbstractWebResource {
@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 @@ public class TopologyWebResource extends AbstractWebResource {
@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 @@ public class TopologyWebResource extends AbstractWebResource {
@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 @@ public class TopologyWebResource extends AbstractWebResource {
@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 @@ public class TopologyWebResource extends AbstractWebResource {
@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)
......