blob: c1a8fcffc3ea47fab6e5042eae3160da4227a420 [file] [log] [blame]
Ray Milkey82e50312015-01-22 17:01:42 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.rest;
17
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070018import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.Lists;
Ray Milkey82e50312015-01-22 17:01:42 -080021import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.Link;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.topology.ClusterId;
26import org.onosproject.net.topology.Topology;
27import org.onosproject.net.topology.TopologyCluster;
28import org.onosproject.net.topology.TopologyService;
29
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070030import javax.ws.rs.GET;
31import javax.ws.rs.Path;
32import javax.ws.rs.PathParam;
33import javax.ws.rs.Produces;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36import java.util.List;
37
38import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey82e50312015-01-22 17:01:42 -080039
40/**
41 * REST resource for interacting with the inventory of clusters.
42 */
43
44@Path("topology")
45public class TopologyWebResource extends AbstractWebResource {
46
47 public static final String CLUSTER_NOT_FOUND = "Cluster is not found";
48
49 /**
50 * Gets the topology overview for a REST GET operation.
51 *
52 * @return topology overview
53 */
54 @GET
55 @Produces(MediaType.APPLICATION_JSON)
56 public Response getTopology() {
57 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070058 ObjectNode root = codec(Topology.class).encode(topology, this);
Ray Milkey3f025692015-01-26 11:15:41 -080059 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080060 }
61
62 /**
63 * Gets the topology clusters overview for a REST GET operation.
64 *
65 * @return topology clusters overview
66 */
67 @GET
68 @Produces(MediaType.APPLICATION_JSON)
69 @Path("clusters")
70 public Response getClusters() {
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070071 TopologyService service = get(TopologyService.class);
72 Topology topology = service.currentTopology();
73 Iterable<TopologyCluster> clusters = service.getClusters(topology);
74 ObjectNode root = encodeArray(TopologyCluster.class, "clusters", clusters);
Ray Milkey3f025692015-01-26 11:15:41 -080075 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080076 }
77
78 /**
79 * Gets details for a topology cluster for a REST GET operation.
80 *
Ray Milkeyf25d1352015-01-23 15:14:08 -080081 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -080082 * @return topology cluster details
83 */
84 @GET
85 @Produces(MediaType.APPLICATION_JSON)
86 @Path("clusters/{id}")
87 public Response getCluster(@PathParam("id") int clusterId) {
88 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070089 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
90 ObjectNode root = codec(TopologyCluster.class).encode(cluster, this);
Ray Milkey3f025692015-01-26 11:15:41 -080091 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080092 }
93
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070094 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
Ray Milkey82e50312015-01-22 17:01:42 -0800101 /**
102 * Gets devices for a topology cluster for a REST GET operation.
103 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800104 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -0800105 * @return topology cluster devices
106 */
107 @GET
108 @Produces(MediaType.APPLICATION_JSON)
109 @Path("clusters/{id}/devices")
110 public Response getClusterDevices(@PathParam("id") int clusterId) {
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700111 TopologyService service = get(TopologyService.class);
112 Topology topology = service.currentTopology();
113 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
Ray Milkey82e50312015-01-22 17:01:42 -0800114
115 List<DeviceId> deviceIds =
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700116 Lists.newArrayList(service.getClusterDevices(topology, cluster));
Ray Milkey82e50312015-01-22 17:01:42 -0800117
118 ObjectNode root = mapper().createObjectNode();
119 ArrayNode devicesNode = root.putArray("devices");
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700120 deviceIds.forEach(id -> devicesNode.add(id.toString()));
Ray Milkey3f025692015-01-26 11:15:41 -0800121 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800122 }
123
124 /**
125 * Gets links for a topology cluster for a REST GET operation.
126 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800127 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -0800128 * @return topology cluster links
129 */
130 @GET
131 @Produces(MediaType.APPLICATION_JSON)
132 @Path("clusters/{id}/links")
133 public Response getClusterLinks(@PathParam("id") int clusterId) {
134 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700135 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
Ray Milkey82e50312015-01-22 17:01:42 -0800136
137 List<Link> links =
138 Lists.newArrayList(get(TopologyService.class)
139 .getClusterLinks(topology, cluster));
140
Ray Milkey3f025692015-01-26 11:15:41 -0800141 return ok(encodeArray(Link.class, "links", links)).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800142 }
143
144 /**
145 * Extracts the port number portion of the ConnectPoint.
146 *
147 * @param deviceString string representing the device/port
148 * @return port number as a string, empty string if the port is not found
149 */
150 private static String getPortNumber(String deviceString) {
151 int separator = deviceString.lastIndexOf(':');
152 if (separator <= 0) {
153 return "";
154 }
155 return deviceString.substring(separator + 1, deviceString.length());
156 }
157
158 /**
159 * Extracts the device ID portion of the ConnectPoint.
160 *
161 * @param deviceString string representing the device/port
162 * @return device ID string
163 */
164 private static String getDeviceId(String deviceString) {
165 int separator = deviceString.lastIndexOf(':');
166 if (separator <= 0) {
167 return "";
168 }
169 return deviceString.substring(0, separator);
170 }
171
172 /**
173 * Gets the broadcast flag of a connect point for a REST GET operation.
174 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800175 * @param connectPointString string representation of the connect point to query.
176 * Format is deviceid:portnumber
Ray Milkey82e50312015-01-22 17:01:42 -0800177 * @return JSON representation of true if the connect point is broadcast,
178 * false otherwise
179 */
180 @GET
181 @Produces(MediaType.APPLICATION_JSON)
182 @Path("broadcast/{connectPoint}")
183 public Response getConnectPointBroadcast(
184 @PathParam("connectPoint") String connectPointString) {
185 Topology topology = get(TopologyService.class).currentTopology();
186
187 DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
188 PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
189 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
190 boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint);
191
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800192 return ok(mapper()
193 .createObjectNode()
Ray Milkey3f025692015-01-26 11:15:41 -0800194 .put("broadcast", isBroadcast))
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800195 .build();
Ray Milkey82e50312015-01-22 17:01:42 -0800196 }
197
198 /**
199 * Gets the infrastructure flag of a connect point for a REST GET operation.
200 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800201 * @param connectPointString string representation of the connect point to query.
202 * Format is deviceid:portnumber
Ray Milkey82e50312015-01-22 17:01:42 -0800203 * @return JSON representation of true if the connect point is broadcast,
204 * false otherwise
205 */
206 @GET
207 @Produces(MediaType.APPLICATION_JSON)
208 @Path("infrastructure/{connectPoint}")
209 public Response getConnectPointInfrastructure(
210 @PathParam("connectPoint") String connectPointString) {
211 Topology topology = get(TopologyService.class).currentTopology();
212
213 DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
214 PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
215 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
216 boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint);
217
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800218 return ok(mapper()
219 .createObjectNode()
Ray Milkey3f025692015-01-26 11:15:41 -0800220 .put("infrastructure", isInfrastructure))
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800221 .build();
Ray Milkey82e50312015-01-22 17:01:42 -0800222 }
223
224}