blob: 745d341ecc41c6c6ab764638a8f213966d4840ba [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
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.osgi.ServiceDirectory;
23import org.onlab.osgi.TestServiceDirectory;
24import org.onlab.rest.BaseResource;
25import org.onosproject.codec.CodecService;
26import org.onosproject.codec.impl.CodecManager;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Link;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080030import org.onosproject.net.provider.ProviderId;
31import org.onosproject.net.topology.ClusterId;
32import org.onosproject.net.topology.DefaultTopologyCluster;
33import org.onosproject.net.topology.DefaultTopologyVertex;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080034import org.onosproject.net.topology.Topology;
35import org.onosproject.net.topology.TopologyCluster;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080036import org.onosproject.net.topology.TopologyService;
Ray Milkeycc53abd2015-02-19 12:31:33 -080037import org.onosproject.net.topology.TopologyServiceAdapter;
Ray Milkeyc401e6e2015-01-24 10:40:03 -080038
39import com.eclipsesource.json.JsonArray;
40import com.eclipsesource.json.JsonObject;
41import com.google.common.collect.ImmutableSet;
42import com.sun.jersey.api.client.WebResource;
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
64 public long computeCost() {
65 return 0;
66 }
67
68 @Override
69 public int clusterCount() {
70 return 2;
71 }
72
73 @Override
74 public int deviceCount() {
75 return 6;
76 }
77
78 @Override
79 public int linkCount() {
80 return 4;
81 }
82
83 @Override
84 public ProviderId providerId() {
85 return ProviderId.NONE;
86 }
87 }
88
Ray Milkeycc53abd2015-02-19 12:31:33 -080089 private static class MockTopologyService extends TopologyServiceAdapter {
Ray Milkeyc401e6e2015-01-24 10:40:03 -080090 final DefaultTopologyVertex root = new DefaultTopologyVertex(did("rootnode"));
91 final Topology topology = new MockTopology();
92 final TopologyCluster cluster1 =
93 new DefaultTopologyCluster(ClusterId.clusterId(0),
94 2, 1, root);
95 final TopologyCluster cluster2 =
96 new DefaultTopologyCluster(ClusterId.clusterId(1),
97 4, 3, root);
98
99 @Override
100 public Topology currentTopology() {
101 return topology;
102 }
103
104 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800105 public Set<TopologyCluster> getClusters(Topology topology) {
106 return ImmutableSet.of(cluster1, cluster2);
107 }
108
109 @Override
110 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
111 return cluster1;
112 }
113
114 @Override
115 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
116 DeviceId device1 = did("dev1");
117 DeviceId device2 = did("dev2");
118
119 return ImmutableSet.of(device1, device2);
120 }
121
122 @Override
123 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
124 Link link1 = link("src1", 1, "dst1", 1);
125 Link link2 = link("src2", 1, "dst2", 1);
126 Link link3 = link("src3", 1, "dst3", 1);
127 return ImmutableSet.of(link1, link2, link3);
128 }
129
130 @Override
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800131 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
132 return connectPoint.elementId().toString().equals("dev2");
133 }
134
135 @Override
136 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
137 return connectPoint.elementId().toString().equals("dev1");
138 }
139
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800140 }
141
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800142 /**
143 * Initializes the test harness.
144 */
145 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800146 public void setUpTest() {
Ray Milkeyc401e6e2015-01-24 10:40:03 -0800147 TopologyService topologyService = new MockTopologyService();
148 CodecManager codecService = new CodecManager();
149 codecService.activate();
150
151 ServiceDirectory testDirectory =
152 new TestServiceDirectory()
153 .add(TopologyService.class, topologyService)
154 .add(CodecService.class, codecService);
155 BaseResource.setServiceDirectory(testDirectory);
156 }
157
158 /**
159 * Tests the topology overview.
160 */
161 @Test
162 public void getTopology() {
163 WebResource rs = resource();
164 String response = rs.path("topology").get(String.class);
165 JsonObject result = JsonObject.readFrom(response);
166 assertThat(result, notNullValue());
167
168 assertThat(result.names(), hasSize(4));
169
170 assertThat(result.get("time").asLong(), is(11111L));
171 assertThat(result.get("clusters").asLong(), is(2L));
172 assertThat(result.get("devices").asLong(), is(6L));
173 assertThat(result.get("links").asLong(), is(4L));
174 }
175
176 /**
177 * Tests the clusters overview.
178 */
179 @Test
180 public void getTopologyClusters() {
181 WebResource rs = resource();
182 String response = rs.path("topology/clusters").get(String.class);
183 JsonObject result = JsonObject.readFrom(response);
184 assertThat(result, notNullValue());
185
186 assertThat(result.names(), hasSize(1));
187 JsonArray clusters = result.get("clusters").asArray();
188 assertThat(clusters, notNullValue());
189 assertThat(clusters.size(), is(2));
190 }
191
192 /**
193 * Tests an individual cluster overview.
194 */
195 @Test
196 public void getCluster() {
197 WebResource rs = resource();
198 String response = rs.path("topology/clusters/0").get(String.class);
199 JsonObject result = JsonObject.readFrom(response);
200 assertThat(result, notNullValue());
201
202 assertThat(result.get("id").asLong(), is(0L));
203 assertThat(result.get("deviceCount").asLong(), is(2L));
204 assertThat(result.get("linkCount").asLong(), is(1L));
205 assertThat(result.get("root").asString(), containsString("rootnode"));
206
207 assertThat(result.names(), hasSize(4));
208 }
209
210 /**
211 * Tests an individual cluster's devices list.
212 */
213 @Test
214 public void getClusterDevices() {
215 WebResource rs = resource();
216 String response = rs.path("topology/clusters/0/devices").get(String.class);
217 JsonObject result = JsonObject.readFrom(response);
218 assertThat(result, notNullValue());
219
220 JsonArray devices = result.get("devices").asArray();
221 assertThat(devices.size(), is(2));
222
223 assertThat(devices.get(0).asString(), is("of:dev1"));
224 assertThat(devices.get(1).asString(), is("of:dev2"));
225 }
226
227 /**
228 * Tests an individual cluster's links list.
229 */
230 @Test
231 public void getClusterLinks() {
232 WebResource rs = resource();
233 String response = rs.path("topology/clusters/1/links").get(String.class);
234 JsonObject result = JsonObject.readFrom(response);
235 assertThat(result, notNullValue());
236
237 JsonArray links = result.get("links").asArray();
238 assertThat(links.size(), is(3));
239
240 JsonObject link0 = links.get(0).asObject();
241 JsonObject src0 = link0.get("src").asObject();
242 String device0 = src0.get("device").asString();
243 assertThat(device0, is("of:src1"));
244
245 JsonObject link2 = links.get(2).asObject();
246 JsonObject src2 = link2.get("src").asObject();
247 String device2 = src2.get("device").asString();
248 assertThat(device2, is("of:src3"));
249 }
250
251 /**
252 * Tests a broadcast query.
253 */
254 @Test
255 public void getBroadcast() {
256 WebResource rs = resource();
257 String response = rs.path("topology/broadcast/dev1:1").get(String.class);
258 JsonObject result = JsonObject.readFrom(response);
259 assertThat(result, notNullValue());
260
261 assertThat(result.get("broadcast").asBoolean(), is(true));
262 }
263
264 /**
265 * Tests an infrastructure query.
266 */
267 @Test
268 public void getInfrastructure() {
269 WebResource rs = resource();
270 String response = rs.path("topology/infrastructure/dev2:1").get(String.class);
271 JsonObject result = JsonObject.readFrom(response);
272 assertThat(result, notNullValue());
273
274 assertThat(result.get("infrastructure").asBoolean(), is(true));
275 }
276}