blob: 4499a25f339da9933e2a930d039e768268d8060e [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;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080026import org.onosproject.codec.CodecService;
27import org.onosproject.codec.impl.CodecManager;
28import org.onosproject.net.ConnectPoint;
Ray Milkey094a1352018-01-22 14:03:54 -080029import org.onosproject.net.DefaultPort;
30import org.onosproject.net.Device;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080031import org.onosproject.net.DeviceId;
32import org.onosproject.net.Link;
Kavitha Alagesan98315a22016-08-17 08:13:32 +053033import org.onosproject.net.Port;
Ray Milkey094a1352018-01-22 14:03:54 -080034import org.onosproject.net.PortNumber;
Kavitha Alagesan98315a22016-08-17 08:13:32 +053035import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.device.DeviceServiceAdapter;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080037import org.onosproject.net.provider.ProviderId;
38import org.onosproject.net.topology.ClusterId;
39import org.onosproject.net.topology.DefaultTopologyCluster;
40import org.onosproject.net.topology.DefaultTopologyVertex;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080041import org.onosproject.net.topology.Topology;
42import org.onosproject.net.topology.TopologyCluster;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080043import org.onosproject.net.topology.TopologyService;
Ray Milkeycc53abd2015-02-19 12:31:33 -080044import org.onosproject.net.topology.TopologyServiceAdapter;
Ray Milkey094a1352018-01-22 14:03:54 -080045
Jian Li9d616492016-03-09 10:52:49 -080046import javax.ws.rs.client.WebTarget;
47import java.util.Set;
Ray Milkey094a1352018-01-22 14:03:54 -080048
Ray Milkeycc53abd2015-02-19 12:31:33 -080049import static org.hamcrest.Matchers.containsString;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080050import static org.hamcrest.Matchers.hasSize;
51import static org.hamcrest.Matchers.is;
52import static org.hamcrest.Matchers.notNullValue;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080053import static org.junit.Assert.assertThat;
Kavitha Alagesan98315a22016-08-17 08:13:32 +053054import static org.onosproject.net.NetTestTools.device;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080055import static org.onosproject.net.NetTestTools.did;
56import static org.onosproject.net.NetTestTools.link;
Kavitha Alagesan98315a22016-08-17 08:13:32 +053057import static org.onosproject.net.PortNumber.portNumber;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080058
59/**
60 * Unit tests for Topology REST APIs.
61 */
Ray Milkey9c3d3362015-01-28 10:39:56 -080062public class TopologyResourceTest extends ResourceTest {
Ray Milkeyc401e6e2015-01-24 10:40:03 -080063
64 private static class MockTopology implements Topology {
65 @Override
66 public long time() {
67 return 11111L;
68 }
69
70 @Override
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050071 public long creationTime() {
72 return 22222L;
73 }
74
75 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -080076 public long computeCost() {
77 return 0;
78 }
79
80 @Override
81 public int clusterCount() {
82 return 2;
83 }
84
85 @Override
86 public int deviceCount() {
87 return 6;
88 }
89
90 @Override
91 public int linkCount() {
92 return 4;
93 }
94
95 @Override
96 public ProviderId providerId() {
97 return ProviderId.NONE;
98 }
99 }
100
Ray Milkeycc53abd2015-02-19 12:31:33 -0800101 private static class MockTopologyService extends TopologyServiceAdapter {
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800102 final DefaultTopologyVertex root = new DefaultTopologyVertex(did("rootnode"));
103 final Topology topology = new MockTopology();
104 final TopologyCluster cluster1 =
105 new DefaultTopologyCluster(ClusterId.clusterId(0),
106 2, 1, root);
107 final TopologyCluster cluster2 =
108 new DefaultTopologyCluster(ClusterId.clusterId(1),
109 4, 3, root);
110
111 @Override
112 public Topology currentTopology() {
113 return topology;
114 }
115
116 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800117 public Set<TopologyCluster> getClusters(Topology topology) {
118 return ImmutableSet.of(cluster1, cluster2);
119 }
120
121 @Override
122 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
123 return cluster1;
124 }
125
126 @Override
127 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
128 DeviceId device1 = did("dev1");
129 DeviceId device2 = did("dev2");
130
131 return ImmutableSet.of(device1, device2);
132 }
133
134 @Override
135 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
136 Link link1 = link("src1", 1, "dst1", 1);
137 Link link2 = link("src2", 1, "dst2", 1);
138 Link link3 = link("src3", 1, "dst3", 1);
139 return ImmutableSet.of(link1, link2, link3);
140 }
141
142 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800143 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
Jon Halla3fcf672017-03-28 16:53:22 -0700144 return "dev2".equals(connectPoint.elementId().toString());
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800145 }
146
147 @Override
148 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
Jon Halla3fcf672017-03-28 16:53:22 -0700149 return "dev1".equals(connectPoint.elementId().toString());
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800150 }
151
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800152 }
153
Kavitha Alagesan98315a22016-08-17 08:13:32 +0530154 private static class MockDeviceService extends DeviceServiceAdapter {
155
156 @Override
157 public Device getDevice(DeviceId deviceId) {
158 String deviceId1 = "dev2";
159 Device device = device(deviceId1);
160 return device;
161 }
162
163 @Override
164 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
165 String deviceIdString = "dev2";
166 Device device = device(deviceIdString);
167 Port port = new DefaultPort(device, portNumber(1), true);
168 return port;
169 }
170 }
171
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800172 /**
173 * Initializes the test harness.
174 */
175 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800176 public void setUpTest() {
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800177 TopologyService topologyService = new MockTopologyService();
Kavitha Alagesan98315a22016-08-17 08:13:32 +0530178 DeviceService mockDeviceService = new MockDeviceService();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800179 CodecManager codecService = new CodecManager();
180 codecService.activate();
181
182 ServiceDirectory testDirectory =
183 new TestServiceDirectory()
Kavitha Alagesan98315a22016-08-17 08:13:32 +0530184 .add(DeviceService.class, mockDeviceService)
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800185 .add(TopologyService.class, topologyService)
186 .add(CodecService.class, codecService);
Ray Milkey094a1352018-01-22 14:03:54 -0800187 setServiceDirectory(testDirectory);
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800188 }
189
190 /**
191 * Tests the topology overview.
192 */
193 @Test
194 public void getTopology() {
Jian Li9d616492016-03-09 10:52:49 -0800195 WebTarget wt = target();
196 String response = wt.path("topology").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800197 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800198 assertThat(result, notNullValue());
199
200 assertThat(result.names(), hasSize(4));
201
202 assertThat(result.get("time").asLong(), is(11111L));
203 assertThat(result.get("clusters").asLong(), is(2L));
204 assertThat(result.get("devices").asLong(), is(6L));
205 assertThat(result.get("links").asLong(), is(4L));
206 }
207
208 /**
209 * Tests the clusters overview.
210 */
211 @Test
212 public void getTopologyClusters() {
Jian Li9d616492016-03-09 10:52:49 -0800213 WebTarget wt = target();
214 String response = wt.path("topology/clusters").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800215 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800216 assertThat(result, notNullValue());
217
218 assertThat(result.names(), hasSize(1));
219 JsonArray clusters = result.get("clusters").asArray();
220 assertThat(clusters, notNullValue());
221 assertThat(clusters.size(), is(2));
222 }
223
224 /**
225 * Tests an individual cluster overview.
226 */
227 @Test
228 public void getCluster() {
Jian Li9d616492016-03-09 10:52:49 -0800229 WebTarget wt = target();
230 String response = wt.path("topology/clusters/0").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800231 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800232 assertThat(result, notNullValue());
233
234 assertThat(result.get("id").asLong(), is(0L));
235 assertThat(result.get("deviceCount").asLong(), is(2L));
236 assertThat(result.get("linkCount").asLong(), is(1L));
237 assertThat(result.get("root").asString(), containsString("rootnode"));
238
239 assertThat(result.names(), hasSize(4));
240 }
241
242 /**
243 * Tests an individual cluster's devices list.
244 */
245 @Test
246 public void getClusterDevices() {
Jian Li9d616492016-03-09 10:52:49 -0800247 WebTarget wt = target();
248 String response = wt.path("topology/clusters/0/devices").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800249 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800250 assertThat(result, notNullValue());
251
252 JsonArray devices = result.get("devices").asArray();
253 assertThat(devices.size(), is(2));
254
255 assertThat(devices.get(0).asString(), is("of:dev1"));
256 assertThat(devices.get(1).asString(), is("of:dev2"));
257 }
258
259 /**
260 * Tests an individual cluster's links list.
261 */
262 @Test
263 public void getClusterLinks() {
Jian Li9d616492016-03-09 10:52:49 -0800264 WebTarget wt = target();
265 String response = wt.path("topology/clusters/1/links").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800266 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800267 assertThat(result, notNullValue());
268
269 JsonArray links = result.get("links").asArray();
270 assertThat(links.size(), is(3));
271
272 JsonObject link0 = links.get(0).asObject();
273 JsonObject src0 = link0.get("src").asObject();
274 String device0 = src0.get("device").asString();
275 assertThat(device0, is("of:src1"));
276
277 JsonObject link2 = links.get(2).asObject();
278 JsonObject src2 = link2.get("src").asObject();
279 String device2 = src2.get("device").asString();
280 assertThat(device2, is("of:src3"));
281 }
282
283 /**
284 * Tests a broadcast query.
285 */
286 @Test
287 public void getBroadcast() {
Jian Li9d616492016-03-09 10:52:49 -0800288 WebTarget wt = target();
289 String response = wt.path("topology/broadcast/dev1:1").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800290 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800291 assertThat(result, notNullValue());
292
293 assertThat(result.get("broadcast").asBoolean(), is(true));
294 }
295
296 /**
297 * Tests an infrastructure query.
298 */
299 @Test
300 public void getInfrastructure() {
Jian Li9d616492016-03-09 10:52:49 -0800301 WebTarget wt = target();
302 String response = wt.path("topology/infrastructure/dev2:1").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800303 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800304 assertThat(result, notNullValue());
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800305 assertThat(result.get("infrastructure").asBoolean(), is(true));
306 }
307}