blob: ddbca8ddf3b90599a6c6f9b3a8aa43a061da5f21 [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
18import java.util.Set;
19
Jian Li80cfe452016-01-14 16:04:58 -080020import com.eclipsesource.json.Json;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080021import org.junit.Before;
22import org.junit.Test;
23import org.onlab.osgi.ServiceDirectory;
24import org.onlab.osgi.TestServiceDirectory;
25import org.onlab.rest.BaseResource;
26import org.onosproject.codec.CodecService;
27import org.onosproject.codec.impl.CodecManager;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Link;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080031import org.onosproject.net.provider.ProviderId;
32import org.onosproject.net.topology.ClusterId;
33import org.onosproject.net.topology.DefaultTopologyCluster;
34import org.onosproject.net.topology.DefaultTopologyVertex;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080035import org.onosproject.net.topology.Topology;
36import org.onosproject.net.topology.TopologyCluster;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080037import org.onosproject.net.topology.TopologyService;
Ray Milkeycc53abd2015-02-19 12:31:33 -080038import org.onosproject.net.topology.TopologyServiceAdapter;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080039
40import com.eclipsesource.json.JsonArray;
41import com.eclipsesource.json.JsonObject;
42import com.google.common.collect.ImmutableSet;
43import com.sun.jersey.api.client.WebResource;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080044
Ray Milkeycc53abd2015-02-19 12:31:33 -080045import static org.hamcrest.Matchers.containsString;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080046import static org.hamcrest.Matchers.hasSize;
47import static org.hamcrest.Matchers.is;
48import static org.hamcrest.Matchers.notNullValue;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080049import static org.junit.Assert.assertThat;
50import static org.onosproject.net.NetTestTools.did;
51import static org.onosproject.net.NetTestTools.link;
52
53/**
54 * Unit tests for Topology REST APIs.
55 */
Ray Milkey9c3d3362015-01-28 10:39:56 -080056public class TopologyResourceTest extends ResourceTest {
Ray Milkeyc401e6e2015-01-24 10:40:03 -080057
58 private static class MockTopology implements Topology {
59 @Override
60 public long time() {
61 return 11111L;
62 }
63
64 @Override
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050065 public long creationTime() {
66 return 22222L;
67 }
68
69 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -080070 public long computeCost() {
71 return 0;
72 }
73
74 @Override
75 public int clusterCount() {
76 return 2;
77 }
78
79 @Override
80 public int deviceCount() {
81 return 6;
82 }
83
84 @Override
85 public int linkCount() {
86 return 4;
87 }
88
89 @Override
90 public ProviderId providerId() {
91 return ProviderId.NONE;
92 }
93 }
94
Ray Milkeycc53abd2015-02-19 12:31:33 -080095 private static class MockTopologyService extends TopologyServiceAdapter {
Ray Milkeyc401e6e2015-01-24 10:40:03 -080096 final DefaultTopologyVertex root = new DefaultTopologyVertex(did("rootnode"));
97 final Topology topology = new MockTopology();
98 final TopologyCluster cluster1 =
99 new DefaultTopologyCluster(ClusterId.clusterId(0),
100 2, 1, root);
101 final TopologyCluster cluster2 =
102 new DefaultTopologyCluster(ClusterId.clusterId(1),
103 4, 3, root);
104
105 @Override
106 public Topology currentTopology() {
107 return topology;
108 }
109
110 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800111 public Set<TopologyCluster> getClusters(Topology topology) {
112 return ImmutableSet.of(cluster1, cluster2);
113 }
114
115 @Override
116 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
117 return cluster1;
118 }
119
120 @Override
121 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
122 DeviceId device1 = did("dev1");
123 DeviceId device2 = did("dev2");
124
125 return ImmutableSet.of(device1, device2);
126 }
127
128 @Override
129 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
130 Link link1 = link("src1", 1, "dst1", 1);
131 Link link2 = link("src2", 1, "dst2", 1);
132 Link link3 = link("src3", 1, "dst3", 1);
133 return ImmutableSet.of(link1, link2, link3);
134 }
135
136 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800137 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
138 return connectPoint.elementId().toString().equals("dev2");
139 }
140
141 @Override
142 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
143 return connectPoint.elementId().toString().equals("dev1");
144 }
145
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800146 }
147
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800148 /**
149 * Initializes the test harness.
150 */
151 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800152 public void setUpTest() {
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800153 TopologyService topologyService = new MockTopologyService();
154 CodecManager codecService = new CodecManager();
155 codecService.activate();
156
157 ServiceDirectory testDirectory =
158 new TestServiceDirectory()
159 .add(TopologyService.class, topologyService)
160 .add(CodecService.class, codecService);
161 BaseResource.setServiceDirectory(testDirectory);
162 }
163
164 /**
165 * Tests the topology overview.
166 */
167 @Test
168 public void getTopology() {
169 WebResource rs = resource();
170 String response = rs.path("topology").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800171 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800172 assertThat(result, notNullValue());
173
174 assertThat(result.names(), hasSize(4));
175
176 assertThat(result.get("time").asLong(), is(11111L));
177 assertThat(result.get("clusters").asLong(), is(2L));
178 assertThat(result.get("devices").asLong(), is(6L));
179 assertThat(result.get("links").asLong(), is(4L));
180 }
181
182 /**
183 * Tests the clusters overview.
184 */
185 @Test
186 public void getTopologyClusters() {
187 WebResource rs = resource();
188 String response = rs.path("topology/clusters").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800189 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800190 assertThat(result, notNullValue());
191
192 assertThat(result.names(), hasSize(1));
193 JsonArray clusters = result.get("clusters").asArray();
194 assertThat(clusters, notNullValue());
195 assertThat(clusters.size(), is(2));
196 }
197
198 /**
199 * Tests an individual cluster overview.
200 */
201 @Test
202 public void getCluster() {
203 WebResource rs = resource();
204 String response = rs.path("topology/clusters/0").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800205 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800206 assertThat(result, notNullValue());
207
208 assertThat(result.get("id").asLong(), is(0L));
209 assertThat(result.get("deviceCount").asLong(), is(2L));
210 assertThat(result.get("linkCount").asLong(), is(1L));
211 assertThat(result.get("root").asString(), containsString("rootnode"));
212
213 assertThat(result.names(), hasSize(4));
214 }
215
216 /**
217 * Tests an individual cluster's devices list.
218 */
219 @Test
220 public void getClusterDevices() {
221 WebResource rs = resource();
222 String response = rs.path("topology/clusters/0/devices").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800223 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800224 assertThat(result, notNullValue());
225
226 JsonArray devices = result.get("devices").asArray();
227 assertThat(devices.size(), is(2));
228
229 assertThat(devices.get(0).asString(), is("of:dev1"));
230 assertThat(devices.get(1).asString(), is("of:dev2"));
231 }
232
233 /**
234 * Tests an individual cluster's links list.
235 */
236 @Test
237 public void getClusterLinks() {
238 WebResource rs = resource();
239 String response = rs.path("topology/clusters/1/links").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800240 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800241 assertThat(result, notNullValue());
242
243 JsonArray links = result.get("links").asArray();
244 assertThat(links.size(), is(3));
245
246 JsonObject link0 = links.get(0).asObject();
247 JsonObject src0 = link0.get("src").asObject();
248 String device0 = src0.get("device").asString();
249 assertThat(device0, is("of:src1"));
250
251 JsonObject link2 = links.get(2).asObject();
252 JsonObject src2 = link2.get("src").asObject();
253 String device2 = src2.get("device").asString();
254 assertThat(device2, is("of:src3"));
255 }
256
257 /**
258 * Tests a broadcast query.
259 */
260 @Test
261 public void getBroadcast() {
262 WebResource rs = resource();
263 String response = rs.path("topology/broadcast/dev1:1").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800264 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800265 assertThat(result, notNullValue());
266
267 assertThat(result.get("broadcast").asBoolean(), is(true));
268 }
269
270 /**
271 * Tests an infrastructure query.
272 */
273 @Test
274 public void getInfrastructure() {
275 WebResource rs = resource();
276 String response = rs.path("topology/infrastructure/dev2:1").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800277 JsonObject result = Json.parse(response).asObject();
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800278 assertThat(result, notNullValue());
279
280 assertThat(result.get("infrastructure").asBoolean(), is(true));
281 }
282}