Thomas Vachuska

Mild REST API refactoring.

Change-Id: Ieddbbd02328043118ad64077f8eda746eb2b3a9f
...@@ -17,11 +17,10 @@ package org.onosproject.rest; ...@@ -17,11 +17,10 @@ package org.onosproject.rest;
17 17
18 import com.fasterxml.jackson.databind.ObjectMapper; 18 import com.fasterxml.jackson.databind.ObjectMapper;
19 import com.fasterxml.jackson.databind.node.ObjectNode; 19 import com.fasterxml.jackson.databind.node.ObjectNode;
20 -import org.onlab.util.ItemNotFoundException; 20 +import org.onlab.rest.BaseResource;
21 import org.onosproject.codec.CodecContext; 21 import org.onosproject.codec.CodecContext;
22 import org.onosproject.codec.CodecService; 22 import org.onosproject.codec.CodecService;
23 import org.onosproject.codec.JsonCodec; 23 import org.onosproject.codec.JsonCodec;
24 -import org.onlab.rest.BaseResource;
25 24
26 /** 25 /**
27 * Abstract REST resource. 26 * Abstract REST resource.
...@@ -61,21 +60,4 @@ public class AbstractWebResource extends BaseResource implements CodecContext { ...@@ -61,21 +60,4 @@ public class AbstractWebResource extends BaseResource implements CodecContext {
61 return result; 60 return result;
62 } 61 }
63 62
64 - /**
65 - * Returns the specified item if that items is null; otherwise throws
66 - * not found exception.
67 - *
68 - * @param item item to check
69 - * @param message not found message
70 - * @param <T> item type
71 - * @return item if not null
72 - * @throws org.onlab.util.ItemNotFoundException if item is null
73 - */
74 - protected <T> T nullIsNotFound(T item, String message) {
75 - if (item == null) {
76 - throw new ItemNotFoundException(message);
77 - }
78 - return item;
79 - }
80 -
81 } 63 }
......
...@@ -33,6 +33,8 @@ import java.io.InputStream; ...@@ -33,6 +33,8 @@ import java.io.InputStream;
33 import java.util.HashSet; 33 import java.util.HashSet;
34 import java.util.List; 34 import java.util.List;
35 35
36 +import static org.onlab.util.Tools.nullIsNotFound;
37 +
36 /** 38 /**
37 * REST resource for interacting with the ONOS cluster subsystem. 39 * REST resource for interacting with the ONOS cluster subsystem.
38 */ 40 */
......
...@@ -29,6 +29,8 @@ import java.io.IOException; ...@@ -29,6 +29,8 @@ import java.io.IOException;
29 import java.io.InputStream; 29 import java.io.InputStream;
30 import java.util.Set; 30 import java.util.Set;
31 31
32 +import static org.onlab.util.Tools.nullIsNotFound;
33 +
32 /** 34 /**
33 * REST resource for cluster-wide component configuration. 35 * REST resource for cluster-wide component configuration.
34 */ 36 */
......
...@@ -27,6 +27,7 @@ import javax.ws.rs.core.Response; ...@@ -27,6 +27,7 @@ import javax.ws.rs.core.Response;
27 import java.util.List; 27 import java.util.List;
28 28
29 import static com.google.common.base.Preconditions.checkNotNull; 29 import static com.google.common.base.Preconditions.checkNotNull;
30 +import static org.onlab.util.Tools.nullIsNotFound;
30 import static org.onosproject.net.DeviceId.deviceId; 31 import static org.onosproject.net.DeviceId.deviceId;
31 32
32 /** 33 /**
......
...@@ -27,6 +27,7 @@ import org.onosproject.net.host.HostService; ...@@ -27,6 +27,7 @@ import org.onosproject.net.host.HostService;
27 27
28 import com.fasterxml.jackson.databind.node.ObjectNode; 28 import com.fasterxml.jackson.databind.node.ObjectNode;
29 29
30 +import static org.onlab.util.Tools.nullIsNotFound;
30 import static org.onosproject.net.HostId.hostId; 31 import static org.onosproject.net.HostId.hostId;
31 32
32 /** 33 /**
......
...@@ -41,6 +41,7 @@ import org.slf4j.Logger; ...@@ -41,6 +41,7 @@ import org.slf4j.Logger;
41 41
42 import com.fasterxml.jackson.databind.node.ObjectNode; 42 import com.fasterxml.jackson.databind.node.ObjectNode;
43 43
44 +import static org.onlab.util.Tools.nullIsNotFound;
44 import static org.onosproject.net.intent.IntentState.FAILED; 45 import static org.onosproject.net.intent.IntentState.FAILED;
45 import static org.onosproject.net.intent.IntentState.WITHDRAWN; 46 import static org.onosproject.net.intent.IntentState.WITHDRAWN;
46 import static org.slf4j.LoggerFactory.getLogger; 47 import static org.slf4j.LoggerFactory.getLogger;
......
...@@ -15,15 +15,9 @@ ...@@ -15,15 +15,9 @@
15 */ 15 */
16 package org.onosproject.rest; 16 package org.onosproject.rest;
17 17
18 -import java.util.List; 18 +import com.fasterxml.jackson.databind.node.ArrayNode;
19 - 19 +import com.fasterxml.jackson.databind.node.ObjectNode;
20 -import javax.ws.rs.GET; 20 +import com.google.common.collect.Lists;
21 -import javax.ws.rs.Path;
22 -import javax.ws.rs.PathParam;
23 -import javax.ws.rs.Produces;
24 -import javax.ws.rs.core.MediaType;
25 -import javax.ws.rs.core.Response;
26 -
27 import org.onosproject.net.ConnectPoint; 21 import org.onosproject.net.ConnectPoint;
28 import org.onosproject.net.DeviceId; 22 import org.onosproject.net.DeviceId;
29 import org.onosproject.net.Link; 23 import org.onosproject.net.Link;
...@@ -33,9 +27,15 @@ import org.onosproject.net.topology.Topology; ...@@ -33,9 +27,15 @@ import org.onosproject.net.topology.Topology;
33 import org.onosproject.net.topology.TopologyCluster; 27 import org.onosproject.net.topology.TopologyCluster;
34 import org.onosproject.net.topology.TopologyService; 28 import org.onosproject.net.topology.TopologyService;
35 29
36 -import com.fasterxml.jackson.databind.node.ArrayNode; 30 +import javax.ws.rs.GET;
37 -import com.fasterxml.jackson.databind.node.ObjectNode; 31 +import javax.ws.rs.Path;
38 -import com.google.common.collect.Lists; 32 +import javax.ws.rs.PathParam;
33 +import javax.ws.rs.Produces;
34 +import javax.ws.rs.core.MediaType;
35 +import javax.ws.rs.core.Response;
36 +import java.util.List;
37 +
38 +import static org.onlab.util.Tools.nullIsNotFound;
39 39
40 /** 40 /**
41 * REST resource for interacting with the inventory of clusters. 41 * REST resource for interacting with the inventory of clusters.
...@@ -55,8 +55,7 @@ public class TopologyWebResource extends AbstractWebResource { ...@@ -55,8 +55,7 @@ public class TopologyWebResource extends AbstractWebResource {
55 @Produces(MediaType.APPLICATION_JSON) 55 @Produces(MediaType.APPLICATION_JSON)
56 public Response getTopology() { 56 public Response getTopology() {
57 Topology topology = get(TopologyService.class).currentTopology(); 57 Topology topology = get(TopologyService.class).currentTopology();
58 - ObjectNode root = 58 + ObjectNode root = codec(Topology.class).encode(topology, this);
59 - codec(Topology.class).encode(topology, this);
60 return ok(root).build(); 59 return ok(root).build();
61 } 60 }
62 61
...@@ -69,11 +68,10 @@ public class TopologyWebResource extends AbstractWebResource { ...@@ -69,11 +68,10 @@ public class TopologyWebResource extends AbstractWebResource {
69 @Produces(MediaType.APPLICATION_JSON) 68 @Produces(MediaType.APPLICATION_JSON)
70 @Path("clusters") 69 @Path("clusters")
71 public Response getClusters() { 70 public Response getClusters() {
72 - Topology topology = get(TopologyService.class).currentTopology(); 71 + TopologyService service = get(TopologyService.class);
73 - Iterable<TopologyCluster> clusters = 72 + Topology topology = service.currentTopology();
74 - get(TopologyService.class).getClusters(topology); 73 + Iterable<TopologyCluster> clusters = service.getClusters(topology);
75 - ObjectNode root = 74 + ObjectNode root = encodeArray(TopologyCluster.class, "clusters", clusters);
76 - encodeArray(TopologyCluster.class, "clusters", clusters);
77 return ok(root).build(); 75 return ok(root).build();
78 } 76 }
79 77
...@@ -88,17 +86,18 @@ public class TopologyWebResource extends AbstractWebResource { ...@@ -88,17 +86,18 @@ public class TopologyWebResource extends AbstractWebResource {
88 @Path("clusters/{id}") 86 @Path("clusters/{id}")
89 public Response getCluster(@PathParam("id") int clusterId) { 87 public Response getCluster(@PathParam("id") int clusterId) {
90 Topology topology = get(TopologyService.class).currentTopology(); 88 Topology topology = get(TopologyService.class).currentTopology();
91 - TopologyCluster cluster = 89 + TopologyCluster cluster = getTopologyCluster(clusterId, topology);
92 - nullIsNotFound( 90 + ObjectNode root = codec(TopologyCluster.class).encode(cluster, this);
93 - get(TopologyService.class)
94 - .getCluster(topology,
95 - ClusterId.clusterId(clusterId)),
96 - CLUSTER_NOT_FOUND);
97 - ObjectNode root =
98 - codec(TopologyCluster.class).encode(cluster, this);
99 return ok(root).build(); 91 return ok(root).build();
100 } 92 }
101 93
94 + private TopologyCluster getTopologyCluster(int clusterId, Topology topology) {
95 + return nullIsNotFound(
96 + get(TopologyService.class)
97 + .getCluster(topology, ClusterId.clusterId(clusterId)),
98 + CLUSTER_NOT_FOUND);
99 + }
100 +
102 /** 101 /**
103 * Gets devices for a topology cluster for a REST GET operation. 102 * Gets devices for a topology cluster for a REST GET operation.
104 * 103 *
...@@ -109,24 +108,16 @@ public class TopologyWebResource extends AbstractWebResource { ...@@ -109,24 +108,16 @@ public class TopologyWebResource extends AbstractWebResource {
109 @Produces(MediaType.APPLICATION_JSON) 108 @Produces(MediaType.APPLICATION_JSON)
110 @Path("clusters/{id}/devices") 109 @Path("clusters/{id}/devices")
111 public Response getClusterDevices(@PathParam("id") int clusterId) { 110 public Response getClusterDevices(@PathParam("id") int clusterId) {
112 - Topology topology = get(TopologyService.class).currentTopology(); 111 + TopologyService service = get(TopologyService.class);
113 - TopologyCluster cluster = 112 + Topology topology = service.currentTopology();
114 - nullIsNotFound( 113 + TopologyCluster cluster = getTopologyCluster(clusterId, topology);
115 - get(TopologyService.class)
116 - .getCluster(topology,
117 - ClusterId.clusterId(clusterId)),
118 - CLUSTER_NOT_FOUND);
119 114
120 List<DeviceId> deviceIds = 115 List<DeviceId> deviceIds =
121 - Lists.newArrayList(get(TopologyService.class) 116 + Lists.newArrayList(service.getClusterDevices(topology, cluster));
122 - .getClusterDevices(topology, cluster));
123 117
124 ObjectNode root = mapper().createObjectNode(); 118 ObjectNode root = mapper().createObjectNode();
125 ArrayNode devicesNode = root.putArray("devices"); 119 ArrayNode devicesNode = root.putArray("devices");
126 - 120 + deviceIds.forEach(id -> devicesNode.add(id.toString()));
127 - for (DeviceId deviceId : deviceIds) {
128 - devicesNode.add(deviceId.toString());
129 - }
130 return ok(root).build(); 121 return ok(root).build();
131 } 122 }
132 123
...@@ -141,10 +132,7 @@ public class TopologyWebResource extends AbstractWebResource { ...@@ -141,10 +132,7 @@ public class TopologyWebResource extends AbstractWebResource {
141 @Path("clusters/{id}/links") 132 @Path("clusters/{id}/links")
142 public Response getClusterLinks(@PathParam("id") int clusterId) { 133 public Response getClusterLinks(@PathParam("id") int clusterId) {
143 Topology topology = get(TopologyService.class).currentTopology(); 134 Topology topology = get(TopologyService.class).currentTopology();
144 - TopologyCluster cluster = 135 + TopologyCluster cluster = getTopologyCluster(clusterId, topology);
145 - nullIsNotFound(get(TopologyService.class).getCluster(topology,
146 - ClusterId.clusterId(clusterId)),
147 - CLUSTER_NOT_FOUND);
148 136
149 List<Link> links = 137 List<Link> links =
150 Lists.newArrayList(get(TopologyService.class) 138 Lists.newArrayList(get(TopologyService.class)
......