Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 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 | */ |
Jian Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 17 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 19 | import com.eclipsesource.json.JsonArray; |
| 20 | import com.eclipsesource.json.JsonObject; |
| 21 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.osgi.ServiceDirectory; |
| 25 | import org.onlab.osgi.TestServiceDirectory; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 26 | import org.onosproject.codec.CodecService; |
| 27 | import org.onosproject.codec.impl.CodecManager; |
| 28 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 29 | import org.onosproject.net.DefaultPort; |
| 30 | import org.onosproject.net.Device; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.Link; |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 33 | import org.onosproject.net.Port; |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 34 | import org.onosproject.net.PortNumber; |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 35 | import org.onosproject.net.device.DeviceService; |
| 36 | import org.onosproject.net.device.DeviceServiceAdapter; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 37 | import org.onosproject.net.provider.ProviderId; |
| 38 | import org.onosproject.net.topology.ClusterId; |
| 39 | import org.onosproject.net.topology.DefaultTopologyCluster; |
| 40 | import org.onosproject.net.topology.DefaultTopologyVertex; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 41 | import org.onosproject.net.topology.Topology; |
| 42 | import org.onosproject.net.topology.TopologyCluster; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 43 | import org.onosproject.net.topology.TopologyService; |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 44 | import org.onosproject.net.topology.TopologyServiceAdapter; |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 45 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 46 | import javax.ws.rs.client.WebTarget; |
| 47 | import java.util.Set; |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 48 | |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 49 | import static org.hamcrest.Matchers.containsString; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 50 | import static org.hamcrest.Matchers.hasSize; |
| 51 | import static org.hamcrest.Matchers.is; |
| 52 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 53 | import static org.junit.Assert.assertThat; |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 54 | import static org.onosproject.net.NetTestTools.device; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 55 | import static org.onosproject.net.NetTestTools.did; |
| 56 | import static org.onosproject.net.NetTestTools.link; |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 57 | import static org.onosproject.net.PortNumber.portNumber; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Unit tests for Topology REST APIs. |
| 61 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 62 | public class TopologyResourceTest extends ResourceTest { |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 63 | |
| 64 | private static class MockTopology implements Topology { |
| 65 | @Override |
| 66 | public long time() { |
| 67 | return 11111L; |
| 68 | } |
| 69 | |
| 70 | @Override |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 71 | public long creationTime() { |
| 72 | return 22222L; |
| 73 | } |
| 74 | |
| 75 | @Override |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 76 | 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 Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 101 | private static class MockTopologyService extends TopologyServiceAdapter { |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 102 | 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 Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 117 | 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 Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 143 | public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 144 | return "dev2".equals(connectPoint.elementId().toString()); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) { |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 149 | return "dev1".equals(connectPoint.elementId().toString()); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 154 | 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 Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 172 | /** |
| 173 | * Initializes the test harness. |
| 174 | */ |
| 175 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 176 | public void setUpTest() { |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 177 | TopologyService topologyService = new MockTopologyService(); |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 178 | DeviceService mockDeviceService = new MockDeviceService(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 179 | CodecManager codecService = new CodecManager(); |
| 180 | codecService.activate(); |
| 181 | |
| 182 | ServiceDirectory testDirectory = |
| 183 | new TestServiceDirectory() |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 184 | .add(DeviceService.class, mockDeviceService) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 185 | .add(TopologyService.class, topologyService) |
| 186 | .add(CodecService.class, codecService); |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 187 | setServiceDirectory(testDirectory); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Tests the topology overview. |
| 192 | */ |
| 193 | @Test |
| 194 | public void getTopology() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 195 | WebTarget wt = target(); |
| 196 | String response = wt.path("topology").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 197 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 198 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 213 | WebTarget wt = target(); |
| 214 | String response = wt.path("topology/clusters").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 215 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 216 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 229 | WebTarget wt = target(); |
| 230 | String response = wt.path("topology/clusters/0").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 231 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 232 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 247 | WebTarget wt = target(); |
| 248 | String response = wt.path("topology/clusters/0/devices").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 249 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 250 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 264 | WebTarget wt = target(); |
| 265 | String response = wt.path("topology/clusters/1/links").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 266 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 267 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 288 | WebTarget wt = target(); |
| 289 | String response = wt.path("topology/broadcast/dev1:1").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 290 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 291 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 301 | WebTarget wt = target(); |
| 302 | String response = wt.path("topology/infrastructure/dev2:1").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 303 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 304 | assertThat(result, notNullValue()); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 305 | assertThat(result.get("infrastructure").asBoolean(), is(true)); |
| 306 | } |
| 307 | } |