blob: 55b680319b525f4e7674adda2fb99ca03e31dc04 [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 */
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Ray Milkey82e50312015-01-22 17:01:42 -080017
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;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070029import org.onosproject.rest.AbstractWebResource;
Ray Milkey82e50312015-01-22 17:01:42 -080030
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070031import javax.ws.rs.GET;
32import javax.ws.rs.Path;
33import javax.ws.rs.PathParam;
34import javax.ws.rs.Produces;
35import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
37import java.util.List;
38
39import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey82e50312015-01-22 17:01:42 -080040
41/**
42 * REST resource for interacting with the inventory of clusters.
43 */
44
45@Path("topology")
46public class TopologyWebResource extends AbstractWebResource {
47
48 public static final String CLUSTER_NOT_FOUND = "Cluster is not found";
49
50 /**
51 * Gets the topology overview for a REST GET operation.
52 *
53 * @return topology overview
54 */
55 @GET
56 @Produces(MediaType.APPLICATION_JSON)
57 public Response getTopology() {
58 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070059 ObjectNode root = codec(Topology.class).encode(topology, this);
Ray Milkey3f025692015-01-26 11:15:41 -080060 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080061 }
62
63 /**
64 * Gets the topology clusters overview for a REST GET operation.
65 *
66 * @return topology clusters overview
67 */
68 @GET
69 @Produces(MediaType.APPLICATION_JSON)
70 @Path("clusters")
71 public Response getClusters() {
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070072 TopologyService service = get(TopologyService.class);
73 Topology topology = service.currentTopology();
74 Iterable<TopologyCluster> clusters = service.getClusters(topology);
75 ObjectNode root = encodeArray(TopologyCluster.class, "clusters", clusters);
Ray Milkey3f025692015-01-26 11:15:41 -080076 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080077 }
78
79 /**
80 * Gets details for a topology cluster for a REST GET operation.
81 *
Ray Milkeyf25d1352015-01-23 15:14:08 -080082 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -080083 * @return topology cluster details
84 */
85 @GET
86 @Produces(MediaType.APPLICATION_JSON)
87 @Path("clusters/{id}")
88 public Response getCluster(@PathParam("id") int clusterId) {
89 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070090 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
91 ObjectNode root = codec(TopologyCluster.class).encode(cluster, this);
Ray Milkey3f025692015-01-26 11:15:41 -080092 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -080093 }
94
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070095 private TopologyCluster getTopologyCluster(int clusterId, Topology topology) {
96 return nullIsNotFound(
97 get(TopologyService.class)
98 .getCluster(topology, ClusterId.clusterId(clusterId)),
99 CLUSTER_NOT_FOUND);
100 }
101
Ray Milkey82e50312015-01-22 17:01:42 -0800102 /**
103 * Gets devices for a topology cluster for a REST GET operation.
104 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800105 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -0800106 * @return topology cluster devices
107 */
108 @GET
109 @Produces(MediaType.APPLICATION_JSON)
110 @Path("clusters/{id}/devices")
111 public Response getClusterDevices(@PathParam("id") int clusterId) {
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700112 TopologyService service = get(TopologyService.class);
113 Topology topology = service.currentTopology();
114 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
Ray Milkey82e50312015-01-22 17:01:42 -0800115
116 List<DeviceId> deviceIds =
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700117 Lists.newArrayList(service.getClusterDevices(topology, cluster));
Ray Milkey82e50312015-01-22 17:01:42 -0800118
119 ObjectNode root = mapper().createObjectNode();
120 ArrayNode devicesNode = root.putArray("devices");
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700121 deviceIds.forEach(id -> devicesNode.add(id.toString()));
Ray Milkey3f025692015-01-26 11:15:41 -0800122 return ok(root).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800123 }
124
125 /**
126 * Gets links for a topology cluster for a REST GET operation.
127 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800128 * @param clusterId id of the cluster to query
Ray Milkey82e50312015-01-22 17:01:42 -0800129 * @return topology cluster links
130 */
131 @GET
132 @Produces(MediaType.APPLICATION_JSON)
133 @Path("clusters/{id}/links")
134 public Response getClusterLinks(@PathParam("id") int clusterId) {
135 Topology topology = get(TopologyService.class).currentTopology();
Thomas Vachuskaf8cac482015-04-08 19:40:12 -0700136 TopologyCluster cluster = getTopologyCluster(clusterId, topology);
Ray Milkey82e50312015-01-22 17:01:42 -0800137
138 List<Link> links =
139 Lists.newArrayList(get(TopologyService.class)
140 .getClusterLinks(topology, cluster));
141
Ray Milkey3f025692015-01-26 11:15:41 -0800142 return ok(encodeArray(Link.class, "links", links)).build();
Ray Milkey82e50312015-01-22 17:01:42 -0800143 }
144
145 /**
146 * Extracts the port number portion of the ConnectPoint.
147 *
148 * @param deviceString string representing the device/port
149 * @return port number as a string, empty string if the port is not found
150 */
151 private static String getPortNumber(String deviceString) {
152 int separator = deviceString.lastIndexOf(':');
153 if (separator <= 0) {
154 return "";
155 }
156 return deviceString.substring(separator + 1, deviceString.length());
157 }
158
159 /**
160 * Extracts the device ID portion of the ConnectPoint.
161 *
162 * @param deviceString string representing the device/port
163 * @return device ID string
164 */
165 private static String getDeviceId(String deviceString) {
166 int separator = deviceString.lastIndexOf(':');
167 if (separator <= 0) {
168 return "";
169 }
170 return deviceString.substring(0, separator);
171 }
172
173 /**
174 * Gets the broadcast flag of a connect point for a REST GET operation.
175 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800176 * @param connectPointString string representation of the connect point to query.
177 * Format is deviceid:portnumber
Ray Milkey82e50312015-01-22 17:01:42 -0800178 * @return JSON representation of true if the connect point is broadcast,
179 * false otherwise
180 */
181 @GET
182 @Produces(MediaType.APPLICATION_JSON)
183 @Path("broadcast/{connectPoint}")
184 public Response getConnectPointBroadcast(
185 @PathParam("connectPoint") String connectPointString) {
186 Topology topology = get(TopologyService.class).currentTopology();
187
188 DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
189 PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
190 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
191 boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint);
192
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800193 return ok(mapper()
194 .createObjectNode()
Ray Milkey3f025692015-01-26 11:15:41 -0800195 .put("broadcast", isBroadcast))
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800196 .build();
Ray Milkey82e50312015-01-22 17:01:42 -0800197 }
198
199 /**
200 * Gets the infrastructure flag of a connect point for a REST GET operation.
201 *
Ray Milkeyf25d1352015-01-23 15:14:08 -0800202 * @param connectPointString string representation of the connect point to query.
203 * Format is deviceid:portnumber
Ray Milkey82e50312015-01-22 17:01:42 -0800204 * @return JSON representation of true if the connect point is broadcast,
205 * false otherwise
206 */
207 @GET
208 @Produces(MediaType.APPLICATION_JSON)
209 @Path("infrastructure/{connectPoint}")
210 public Response getConnectPointInfrastructure(
211 @PathParam("connectPoint") String connectPointString) {
212 Topology topology = get(TopologyService.class).currentTopology();
213
214 DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
215 PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
216 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
217 boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint);
218
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800219 return ok(mapper()
220 .createObjectNode()
Ray Milkey3f025692015-01-26 11:15:41 -0800221 .put("infrastructure", isInfrastructure))
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800222 .build();
Ray Milkey82e50312015-01-22 17:01:42 -0800223 }
224
225}