blob: 95a74162043ddfdd08499a5fe91522ae2ee6ab92 [file] [log] [blame]
Ray Milkeyc401e6e2015-01-24 10:40:03 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Ray Milkeyc401e6e2015-01-24 10:40:03 -08003 *
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 */
Jian Li8ae91202016-03-24 14:36:16 -070016package org.onosproject.rest.resources;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080017
Jian Li80cfe452016-01-14 16:04:58 -080018import com.eclipsesource.json.Json;
Jian Li9d616492016-03-09 10:52:49 -080019import com.eclipsesource.json.JsonArray;
20import com.eclipsesource.json.JsonObject;
21import com.google.common.collect.ImmutableSet;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080022import org.junit.Before;
23import org.junit.Test;
24import org.onlab.osgi.ServiceDirectory;
25import org.onlab.osgi.TestServiceDirectory;
26import org.onlab.rest.BaseResource;
27import org.onosproject.codec.CodecService;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.Link;
Kavitha Alagesan98315a22016-08-17 08:13:32 +053032import org.onosproject.net.Device;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.DefaultPort;
35import org.onosproject.net.Port;
36import org.onosproject.net.device.DeviceService;
37import org.onosproject.net.device.DeviceServiceAdapter;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080038import org.onosproject.net.provider.ProviderId;
39import org.onosproject.net.topology.ClusterId;
40import org.onosproject.net.topology.DefaultTopologyCluster;
41import org.onosproject.net.topology.DefaultTopologyVertex;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080042import org.onosproject.net.topology.Topology;
43import org.onosproject.net.topology.TopologyCluster;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080044import org.onosproject.net.topology.TopologyService;
Ray Milkeycc53abd2015-02-19 12:31:33 -080045import org.onosproject.net.topology.TopologyServiceAdapter;
Jian Li9d616492016-03-09 10:52:49 -080046import javax.ws.rs.client.WebTarget;
47import java.util.Set;
Ray Milkeycc53abd2015-02-19 12:31:33 -080048import static org.hamcrest.Matchers.containsString;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080049import static org.hamcrest.Matchers.hasSize;
50import static org.hamcrest.Matchers.is;
51import static org.hamcrest.Matchers.notNullValue;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080052import static org.junit.Assert.assertThat;
Kavitha Alagesan98315a22016-08-17 08:13:32 +053053import static org.onosproject.net.NetTestTools.device;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080054import static org.onosproject.net.NetTestTools.did;
55import static org.onosproject.net.NetTestTools.link;
Kavitha Alagesan98315a22016-08-17 08:13:32 +053056import static org.onosproject.net.PortNumber.portNumber;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080057
58/**
59 * Unit tests for Topology REST APIs.
60 */
Ray Milkey9c3d3362015-01-28 10:39:56 -080061public class TopologyResourceTest extends ResourceTest {
Ray Milkeyc401e6e2015-01-24 10:40:03 -080062
63 private static class MockTopology implements Topology {
64 @Override
65 public long time() {
66 return 11111L;
67 }
68
69 @Override
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050070 public long creationTime() {
71 return 22222L;
72 }
73
74 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -080075 public long computeCost() {
76 return 0;
77 }
78
79 @Override
80 public int clusterCount() {
81 return 2;
82 }
83
84 @Override
85 public int deviceCount() {
86 return 6;
87 }
88
89 @Override
90 public int linkCount() {
91 return 4;
92 }
93
94 @Override
95 public ProviderId providerId() {
96 return ProviderId.NONE;
97 }
98 }
99
Ray Milkeycc53abd2015-02-19 12:31:33 -0800100 private static class MockTopologyService extends TopologyServiceAdapter {
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800101 final DefaultTopologyVertex root = new DefaultTopologyVertex(did("rootnode"));
102 final Topology topology = new MockTopology();
103 final TopologyCluster cluster1 =
104 new DefaultTopologyCluster(ClusterId.clusterId(0),
105 2, 1, root);
106 final TopologyCluster cluster2 =
107 new DefaultTopologyCluster(ClusterId.clusterId(1),
108 4, 3, root);
109
110 @Override
111 public Topology currentTopology() {
112 return topology;
113 }
114
115 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800116 public Set<TopologyCluster> getClusters(Topology topology) {
117 return ImmutableSet.of(cluster1, cluster2);
118 }
119
120 @Override
121 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
122 return cluster1;
123 }
124
125 @Override
126 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
127 DeviceId device1 = did("dev1");
128 DeviceId device2 = did("dev2");
129
130 return ImmutableSet.of(device1, device2);
131 }
132
133 @Override
134 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
135 Link link1 = link("src1", 1, "dst1", 1);
136 Link link2 = link("src2", 1, "dst2", 1);
137 Link link3 = link("src3", 1, "dst3", 1);
138 return ImmutableSet.of(link1, link2, link3);
139 }
140
141 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800142 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
Jon Halla3fcf672017-03-28 16:53:22 -0700143 return "dev2".equals(connectPoint.elementId().toString());
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800144 }
145
146 @Override
147 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
Jon Halla3fcf672017-03-28 16:53:22 -0700148 return "dev1".equals(connectPoint.elementId().toString());
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800149 }
150
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800151 }
152
Kavitha Alagesan98315a22016-08-17 08:13:32 +0530153 private static class MockDeviceService extends DeviceServiceAdapter {
154
155 @Override
156 public Device getDevice(DeviceId deviceId) {
157 String deviceId1 = "dev2";
158 Device device = device(deviceId1);
159 return device;
160 }
161
162 @Override
163 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
164 String deviceIdString = "dev2";
165 Device device = device(deviceIdString);
166 Port port = new DefaultPort(device, portNumber(1), true);
167 return port;
168 }
169 }
170
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800171 /**
172 * Initializes the test harness.
173 */
174 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800175 public void setUpTest() {
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800176 TopologyService topologyService = new MockTopologyService();
Kavitha Alagesan98315a22016-08-17 08:13:32 +0530177 DeviceService mockDeviceService = new MockDeviceService();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800178 CodecManager codecService = new CodecManager();
179 codecService.activate();
180
181 ServiceDirectory testDirectory =
182 new TestServiceDirectory()
Kavitha Alagesan98315a22016-08-17 08:13:32 +0530183 .add(DeviceService.class, mockDeviceService)
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800184 .add(TopologyService.class, topologyService)
185 .add(CodecService.class, codecService);
186 BaseResource.setServiceDirectory(testDirectory);
187 }
188
189 /**
190 * Tests the topology overview.
191 */
192 @Test
193 public void getTopology() {
Jian Li9d616492016-03-09 10:52:49 -0800194 WebTarget wt = target();
195 String response = wt.path("topology").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800196 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800197 assertThat(result, notNullValue());
198
199 assertThat(result.names(), hasSize(4));
200
201 assertThat(result.get("time").asLong(), is(11111L));
202 assertThat(result.get("clusters").asLong(), is(2L));
203 assertThat(result.get("devices").asLong(), is(6L));
204 assertThat(result.get("links").asLong(), is(4L));
205 }
206
207 /**
208 * Tests the clusters overview.
209 */
210 @Test
211 public void getTopologyClusters() {
Jian Li9d616492016-03-09 10:52:49 -0800212 WebTarget wt = target();
213 String response = wt.path("topology/clusters").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800214 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800215 assertThat(result, notNullValue());
216
217 assertThat(result.names(), hasSize(1));
218 JsonArray clusters = result.get("clusters").asArray();
219 assertThat(clusters, notNullValue());
220 assertThat(clusters.size(), is(2));
221 }
222
223 /**
224 * Tests an individual cluster overview.
225 */
226 @Test
227 public void getCluster() {
Jian Li9d616492016-03-09 10:52:49 -0800228 WebTarget wt = target();
229 String response = wt.path("topology/clusters/0").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800230 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800231 assertThat(result, notNullValue());
232
233 assertThat(result.get("id").asLong(), is(0L));
234 assertThat(result.get("deviceCount").asLong(), is(2L));
235 assertThat(result.get("linkCount").asLong(), is(1L));
236 assertThat(result.get("root").asString(), containsString("rootnode"));
237
238 assertThat(result.names(), hasSize(4));
239 }
240
241 /**
242 * Tests an individual cluster's devices list.
243 */
244 @Test
245 public void getClusterDevices() {
Jian Li9d616492016-03-09 10:52:49 -0800246 WebTarget wt = target();
247 String response = wt.path("topology/clusters/0/devices").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800248 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800249 assertThat(result, notNullValue());
250
251 JsonArray devices = result.get("devices").asArray();
252 assertThat(devices.size(), is(2));
253
254 assertThat(devices.get(0).asString(), is("of:dev1"));
255 assertThat(devices.get(1).asString(), is("of:dev2"));
256 }
257
258 /**
259 * Tests an individual cluster's links list.
260 */
261 @Test
262 public void getClusterLinks() {
Jian Li9d616492016-03-09 10:52:49 -0800263 WebTarget wt = target();
264 String response = wt.path("topology/clusters/1/links").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800265 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800266 assertThat(result, notNullValue());
267
268 JsonArray links = result.get("links").asArray();
269 assertThat(links.size(), is(3));
270
271 JsonObject link0 = links.get(0).asObject();
272 JsonObject src0 = link0.get("src").asObject();
273 String device0 = src0.get("device").asString();
274 assertThat(device0, is("of:src1"));
275
276 JsonObject link2 = links.get(2).asObject();
277 JsonObject src2 = link2.get("src").asObject();
278 String device2 = src2.get("device").asString();
279 assertThat(device2, is("of:src3"));
280 }
281
282 /**
283 * Tests a broadcast query.
284 */
285 @Test
286 public void getBroadcast() {
Jian Li9d616492016-03-09 10:52:49 -0800287 WebTarget wt = target();
288 String response = wt.path("topology/broadcast/dev1:1").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800289 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800290 assertThat(result, notNullValue());
291
292 assertThat(result.get("broadcast").asBoolean(), is(true));
293 }
294
295 /**
296 * Tests an infrastructure query.
297 */
298 @Test
299 public void getInfrastructure() {
Jian Li9d616492016-03-09 10:52:49 -0800300 WebTarget wt = target();
301 String response = wt.path("topology/infrastructure/dev2:1").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800302 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800303 assertThat(result, notNullValue());
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800304 assertThat(result.get("infrastructure").asBoolean(), is(true));
305 }
306}