blob: 9d18e2ddcb8a171e1ac635f386a131f4cc3860fe [file] [log] [blame]
Ray Milkeyc401e6e2015-01-24 10:40:03 -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
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;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080032import org.onosproject.net.provider.ProviderId;
33import org.onosproject.net.topology.ClusterId;
34import org.onosproject.net.topology.DefaultTopologyCluster;
35import org.onosproject.net.topology.DefaultTopologyVertex;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080036import org.onosproject.net.topology.Topology;
37import org.onosproject.net.topology.TopologyCluster;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080038import org.onosproject.net.topology.TopologyService;
Ray Milkeycc53abd2015-02-19 12:31:33 -080039import org.onosproject.net.topology.TopologyServiceAdapter;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080040
Jian Li9d616492016-03-09 10:52:49 -080041import javax.ws.rs.client.WebTarget;
42import java.util.Set;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080043
Ray Milkeycc53abd2015-02-19 12:31:33 -080044import static org.hamcrest.Matchers.containsString;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080045import static org.hamcrest.Matchers.hasSize;
46import static org.hamcrest.Matchers.is;
47import static org.hamcrest.Matchers.notNullValue;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080048import static org.junit.Assert.assertThat;
49import static org.onosproject.net.NetTestTools.did;
50import static org.onosproject.net.NetTestTools.link;
51
52/**
53 * Unit tests for Topology REST APIs.
54 */
Ray Milkey9c3d3362015-01-28 10:39:56 -080055public class TopologyResourceTest extends ResourceTest {
Ray Milkeyc401e6e2015-01-24 10:40:03 -080056
57 private static class MockTopology implements Topology {
58 @Override
59 public long time() {
60 return 11111L;
61 }
62
63 @Override
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050064 public long creationTime() {
65 return 22222L;
66 }
67
68 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -080069 public long computeCost() {
70 return 0;
71 }
72
73 @Override
74 public int clusterCount() {
75 return 2;
76 }
77
78 @Override
79 public int deviceCount() {
80 return 6;
81 }
82
83 @Override
84 public int linkCount() {
85 return 4;
86 }
87
88 @Override
89 public ProviderId providerId() {
90 return ProviderId.NONE;
91 }
92 }
93
Ray Milkeycc53abd2015-02-19 12:31:33 -080094 private static class MockTopologyService extends TopologyServiceAdapter {
Ray Milkeyc401e6e2015-01-24 10:40:03 -080095 final DefaultTopologyVertex root = new DefaultTopologyVertex(did("rootnode"));
96 final Topology topology = new MockTopology();
97 final TopologyCluster cluster1 =
98 new DefaultTopologyCluster(ClusterId.clusterId(0),
99 2, 1, root);
100 final TopologyCluster cluster2 =
101 new DefaultTopologyCluster(ClusterId.clusterId(1),
102 4, 3, root);
103
104 @Override
105 public Topology currentTopology() {
106 return topology;
107 }
108
109 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800110 public Set<TopologyCluster> getClusters(Topology topology) {
111 return ImmutableSet.of(cluster1, cluster2);
112 }
113
114 @Override
115 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
116 return cluster1;
117 }
118
119 @Override
120 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
121 DeviceId device1 = did("dev1");
122 DeviceId device2 = did("dev2");
123
124 return ImmutableSet.of(device1, device2);
125 }
126
127 @Override
128 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
129 Link link1 = link("src1", 1, "dst1", 1);
130 Link link2 = link("src2", 1, "dst2", 1);
131 Link link3 = link("src3", 1, "dst3", 1);
132 return ImmutableSet.of(link1, link2, link3);
133 }
134
135 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800136 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
137 return connectPoint.elementId().toString().equals("dev2");
138 }
139
140 @Override
141 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
142 return connectPoint.elementId().toString().equals("dev1");
143 }
144
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800145 }
146
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800147 /**
148 * Initializes the test harness.
149 */
150 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800151 public void setUpTest() {
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800152 TopologyService topologyService = new MockTopologyService();
153 CodecManager codecService = new CodecManager();
154 codecService.activate();
155
156 ServiceDirectory testDirectory =
157 new TestServiceDirectory()
158 .add(TopologyService.class, topologyService)
159 .add(CodecService.class, codecService);
160 BaseResource.setServiceDirectory(testDirectory);
161 }
162
163 /**
164 * Tests the topology overview.
165 */
166 @Test
167 public void getTopology() {
Jian Li9d616492016-03-09 10:52:49 -0800168 WebTarget wt = target();
169 String response = wt.path("topology").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800170 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800171 assertThat(result, notNullValue());
172
173 assertThat(result.names(), hasSize(4));
174
175 assertThat(result.get("time").asLong(), is(11111L));
176 assertThat(result.get("clusters").asLong(), is(2L));
177 assertThat(result.get("devices").asLong(), is(6L));
178 assertThat(result.get("links").asLong(), is(4L));
179 }
180
181 /**
182 * Tests the clusters overview.
183 */
184 @Test
185 public void getTopologyClusters() {
Jian Li9d616492016-03-09 10:52:49 -0800186 WebTarget wt = target();
187 String response = wt.path("topology/clusters").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800188 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800189 assertThat(result, notNullValue());
190
191 assertThat(result.names(), hasSize(1));
192 JsonArray clusters = result.get("clusters").asArray();
193 assertThat(clusters, notNullValue());
194 assertThat(clusters.size(), is(2));
195 }
196
197 /**
198 * Tests an individual cluster overview.
199 */
200 @Test
201 public void getCluster() {
Jian Li9d616492016-03-09 10:52:49 -0800202 WebTarget wt = target();
203 String response = wt.path("topology/clusters/0").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800204 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800205 assertThat(result, notNullValue());
206
207 assertThat(result.get("id").asLong(), is(0L));
208 assertThat(result.get("deviceCount").asLong(), is(2L));
209 assertThat(result.get("linkCount").asLong(), is(1L));
210 assertThat(result.get("root").asString(), containsString("rootnode"));
211
212 assertThat(result.names(), hasSize(4));
213 }
214
215 /**
216 * Tests an individual cluster's devices list.
217 */
218 @Test
219 public void getClusterDevices() {
Jian Li9d616492016-03-09 10:52:49 -0800220 WebTarget wt = target();
221 String response = wt.path("topology/clusters/0/devices").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800222 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800223 assertThat(result, notNullValue());
224
225 JsonArray devices = result.get("devices").asArray();
226 assertThat(devices.size(), is(2));
227
228 assertThat(devices.get(0).asString(), is("of:dev1"));
229 assertThat(devices.get(1).asString(), is("of:dev2"));
230 }
231
232 /**
233 * Tests an individual cluster's links list.
234 */
235 @Test
236 public void getClusterLinks() {
Jian Li9d616492016-03-09 10:52:49 -0800237 WebTarget wt = target();
238 String response = wt.path("topology/clusters/1/links").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800239 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800240 assertThat(result, notNullValue());
241
242 JsonArray links = result.get("links").asArray();
243 assertThat(links.size(), is(3));
244
245 JsonObject link0 = links.get(0).asObject();
246 JsonObject src0 = link0.get("src").asObject();
247 String device0 = src0.get("device").asString();
248 assertThat(device0, is("of:src1"));
249
250 JsonObject link2 = links.get(2).asObject();
251 JsonObject src2 = link2.get("src").asObject();
252 String device2 = src2.get("device").asString();
253 assertThat(device2, is("of:src3"));
254 }
255
256 /**
257 * Tests a broadcast query.
258 */
259 @Test
260 public void getBroadcast() {
Jian Li9d616492016-03-09 10:52:49 -0800261 WebTarget wt = target();
262 String response = wt.path("topology/broadcast/dev1:1").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800263 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800264 assertThat(result, notNullValue());
265
266 assertThat(result.get("broadcast").asBoolean(), is(true));
267 }
268
269 /**
270 * Tests an infrastructure query.
271 */
272 @Test
273 public void getInfrastructure() {
Jian Li9d616492016-03-09 10:52:49 -0800274 WebTarget wt = target();
275 String response = wt.path("topology/infrastructure/dev2:1").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800276 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800277 assertThat(result, notNullValue());
278
279 assertThat(result.get("infrastructure").asBoolean(), is(true));
280 }
281}