Ray Milkey

ONOS-792 - topology related REST GET APIs

- GET /topology
- GET /topology/clusters
- GET /topology/cluster/{clusterId}
- GET /topology/cluster/{clusterId}/devices
- GET /topology/cluster/{clusterId}/links
- GET /topology/broadcast/{connectPoint}
- GET /topology/infrastructure/{connectPoint}

Change-Id: I2a5053b9e7f8bb7f4e3daa4b82b31376a47578cf
...@@ -43,6 +43,8 @@ import org.onosproject.net.intent.Constraint; ...@@ -43,6 +43,8 @@ import org.onosproject.net.intent.Constraint;
43 import org.onosproject.net.intent.HostToHostIntent; 43 import org.onosproject.net.intent.HostToHostIntent;
44 import org.onosproject.net.intent.Intent; 44 import org.onosproject.net.intent.Intent;
45 import org.onosproject.net.intent.PointToPointIntent; 45 import org.onosproject.net.intent.PointToPointIntent;
46 +import org.onosproject.net.topology.Topology;
47 +import org.onosproject.net.topology.TopologyCluster;
46 import org.slf4j.Logger; 48 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory; 49 import org.slf4j.LoggerFactory;
48 50
...@@ -80,6 +82,8 @@ public class CodecManager implements CodecService { ...@@ -80,6 +82,8 @@ public class CodecManager implements CodecService {
80 registerCodec(Criterion.class, new CriterionCodec()); 82 registerCodec(Criterion.class, new CriterionCodec());
81 registerCodec(Ethernet.class, new EthernetCodec()); 83 registerCodec(Ethernet.class, new EthernetCodec());
82 registerCodec(Constraint.class, new ConstraintCodec()); 84 registerCodec(Constraint.class, new ConstraintCodec());
85 + registerCodec(Topology.class, new TopologyCodec());
86 + registerCodec(TopologyCluster.class, new TopologyClusterCodec());
83 log.info("Started"); 87 log.info("Started");
84 } 88 }
85 89
......
1 +/*
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 + */
16 +package org.onosproject.codec.impl;
17 +
18 +import org.onosproject.codec.CodecContext;
19 +import org.onosproject.codec.JsonCodec;
20 +import org.onosproject.net.topology.TopologyCluster;
21 +
22 +import com.fasterxml.jackson.databind.node.ObjectNode;
23 +
24 +import static com.google.common.base.Preconditions.checkNotNull;
25 +
26 +/**
27 + * Topology cluster JSON codec.
28 + */
29 +public class TopologyClusterCodec extends JsonCodec<TopologyCluster> {
30 +
31 + @Override
32 + public ObjectNode encode(TopologyCluster cluster, CodecContext context) {
33 + checkNotNull(cluster, "Cluster cannot be null");
34 +
35 + return context.mapper().createObjectNode()
36 + .put("id", cluster.id().index())
37 + .put("deviceCount", cluster.deviceCount())
38 + .put("linkCount", cluster.linkCount())
39 + .put("root", cluster.root().toString());
40 + }
41 +}
1 +/*
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 + */
16 +package org.onosproject.codec.impl;
17 +
18 +import org.onosproject.codec.CodecContext;
19 +import org.onosproject.codec.JsonCodec;
20 +import org.onosproject.net.topology.Topology;
21 +
22 +import com.fasterxml.jackson.databind.node.ObjectNode;
23 +
24 +import static com.google.common.base.Preconditions.checkNotNull;
25 +
26 +/**
27 + * Topology JSON codec.
28 + */
29 +public class TopologyCodec extends JsonCodec<Topology> {
30 +
31 + @Override
32 + public ObjectNode encode(Topology topology, CodecContext context) {
33 + checkNotNull(topology, "Topology cannot be null");
34 +
35 + return context.mapper().createObjectNode()
36 + .put("time", topology.time())
37 + .put("devices", topology.deviceCount())
38 + .put("links", topology.linkCount())
39 + .put("clusters", topology.clusterCount());
40 + }
41 +}
1 +/*
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 + */
16 +package org.onosproject.rest;
17 +
18 +import java.util.List;
19 +
20 +import javax.ws.rs.GET;
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;
28 +import org.onosproject.net.DeviceId;
29 +import org.onosproject.net.Link;
30 +import org.onosproject.net.PortNumber;
31 +import org.onosproject.net.topology.ClusterId;
32 +import org.onosproject.net.topology.Topology;
33 +import org.onosproject.net.topology.TopologyCluster;
34 +import org.onosproject.net.topology.TopologyService;
35 +
36 +import com.fasterxml.jackson.databind.node.ArrayNode;
37 +import com.fasterxml.jackson.databind.node.ObjectNode;
38 +import com.google.common.collect.Lists;
39 +
40 +/**
41 + * REST resource for interacting with the inventory of clusters.
42 + */
43 +
44 +@Path("topology")
45 +public 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();
58 + ObjectNode root =
59 + codec(Topology.class).encode(topology, this);
60 + return ok(root.toString()).build();
61 + }
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() {
72 + Topology topology = get(TopologyService.class).currentTopology();
73 + Iterable<TopologyCluster> clusters =
74 + get(TopologyService.class).getClusters(topology);
75 + ObjectNode root =
76 + encodeArray(TopologyCluster.class, "clusters", clusters);
77 + return ok(root.toString()).build();
78 + }
79 +
80 + /**
81 + * Gets details for a topology cluster for a REST GET operation.
82 + *
83 + * @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();
90 + TopologyCluster cluster =
91 + nullIsNotFound(
92 + get(TopologyService.class)
93 + .getCluster(topology,
94 + ClusterId.clusterId(clusterId)),
95 + CLUSTER_NOT_FOUND);
96 + ObjectNode root =
97 + codec(TopologyCluster.class).encode(cluster, this);
98 + return ok(root.toString()).build();
99 + }
100 +
101 + /**
102 + * Gets devices for a topology cluster for a REST GET operation.
103 + *
104 + * @return topology cluster devices
105 + */
106 + @GET
107 + @Produces(MediaType.APPLICATION_JSON)
108 + @Path("clusters/{id}/devices")
109 + public Response getClusterDevices(@PathParam("id") int clusterId) {
110 + Topology topology = get(TopologyService.class).currentTopology();
111 + TopologyCluster cluster =
112 + nullIsNotFound(
113 + get(TopologyService.class)
114 + .getCluster(topology,
115 + ClusterId.clusterId(clusterId)),
116 + CLUSTER_NOT_FOUND);
117 +
118 + List<DeviceId> deviceIds =
119 + Lists.newArrayList(get(TopologyService.class)
120 + .getClusterDevices(topology, cluster));
121 +
122 + ObjectNode root = mapper().createObjectNode();
123 + ArrayNode devicesNode = root.putArray("devices");
124 +
125 + for (DeviceId deviceId : deviceIds) {
126 + devicesNode.add(deviceId.toString());
127 + }
128 + return ok(root).build();
129 + }
130 +
131 + /**
132 + * Gets links for a topology cluster for a REST GET operation.
133 + *
134 + * @return topology cluster links
135 + */
136 + @GET
137 + @Produces(MediaType.APPLICATION_JSON)
138 + @Path("clusters/{id}/links")
139 + public Response getClusterLinks(@PathParam("id") int clusterId) {
140 + Topology topology = get(TopologyService.class).currentTopology();
141 + TopologyCluster cluster =
142 + nullIsNotFound(get(TopologyService.class).getCluster(topology,
143 + ClusterId.clusterId(clusterId)),
144 + CLUSTER_NOT_FOUND);
145 +
146 + List<Link> links =
147 + Lists.newArrayList(get(TopologyService.class)
148 + .getClusterLinks(topology, cluster));
149 +
150 + return ok(encodeArray(Link.class, "links", links)).build();
151 + }
152 +
153 + /**
154 + * Extracts the port number portion of the ConnectPoint.
155 + *
156 + * @param deviceString string representing the device/port
157 + * @return port number as a string, empty string if the port is not found
158 + */
159 + private static String getPortNumber(String deviceString) {
160 + int separator = deviceString.lastIndexOf(':');
161 + if (separator <= 0) {
162 + return "";
163 + }
164 + return deviceString.substring(separator + 1, deviceString.length());
165 + }
166 +
167 + /**
168 + * Extracts the device ID portion of the ConnectPoint.
169 + *
170 + * @param deviceString string representing the device/port
171 + * @return device ID string
172 + */
173 + private static String getDeviceId(String deviceString) {
174 + int separator = deviceString.lastIndexOf(':');
175 + if (separator <= 0) {
176 + return "";
177 + }
178 + return deviceString.substring(0, separator);
179 + }
180 +
181 + /**
182 + * Gets the broadcast flag of a connect point for a REST GET operation.
183 + *
184 + * @return JSON representation of true if the connect point is broadcast,
185 + * false otherwise
186 + */
187 + @GET
188 + @Produces(MediaType.APPLICATION_JSON)
189 + @Path("broadcast/{connectPoint}")
190 + public Response getConnectPointBroadcast(
191 + @PathParam("connectPoint") String connectPointString) {
192 + Topology topology = get(TopologyService.class).currentTopology();
193 +
194 + DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
195 + PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
196 + ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
197 + boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint);
198 +
199 + return ok(mapper().createObjectNode().put("broadcast", isBroadcast)).build();
200 + }
201 +
202 + /**
203 + * Gets the infrastructure flag of a connect point for a REST GET operation.
204 + *
205 + * @return JSON representation of true if the connect point is broadcast,
206 + * false otherwise
207 + */
208 + @GET
209 + @Produces(MediaType.APPLICATION_JSON)
210 + @Path("infrastructure/{connectPoint}")
211 + public Response getConnectPointInfrastructure(
212 + @PathParam("connectPoint") String connectPointString) {
213 + Topology topology = get(TopologyService.class).currentTopology();
214 +
215 + DeviceId deviceId = DeviceId.deviceId(getDeviceId(connectPointString));
216 + PortNumber portNumber = PortNumber.portNumber(getPortNumber(connectPointString));
217 + ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber);
218 + boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint);
219 +
220 + return ok(mapper().createObjectNode().put("infrastructure", isInfrastructure)).build();
221 + }
222 +
223 +}
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
40 org.onosproject.rest.HostsWebResource, 40 org.onosproject.rest.HostsWebResource,
41 org.onosproject.rest.IntentsWebResource, 41 org.onosproject.rest.IntentsWebResource,
42 org.onosproject.rest.FlowsWebResource, 42 org.onosproject.rest.FlowsWebResource,
43 + org.onosproject.rest.TopologyWebResource,
43 org.onosproject.rest.ConfigResource 44 org.onosproject.rest.ConfigResource
44 </param-value> 45 </param-value>
45 </init-param> 46 </init-param>
......