Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
| 26 | import org.onlab.rest.BaseResource; |
| 27 | import org.onosproject.codec.CodecService; |
| 28 | import org.onosproject.codec.impl.CodecManager; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.Link; |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 32 | import org.onosproject.net.Device; |
| 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.DefaultPort; |
| 35 | import org.onosproject.net.Port; |
| 36 | import org.onosproject.net.device.DeviceService; |
| 37 | import org.onosproject.net.device.DeviceServiceAdapter; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 38 | import org.onosproject.net.provider.ProviderId; |
| 39 | import org.onosproject.net.topology.ClusterId; |
| 40 | import org.onosproject.net.topology.DefaultTopologyCluster; |
| 41 | import org.onosproject.net.topology.DefaultTopologyVertex; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 42 | import org.onosproject.net.topology.Topology; |
| 43 | import org.onosproject.net.topology.TopologyCluster; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 44 | import org.onosproject.net.topology.TopologyService; |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 45 | import org.onosproject.net.topology.TopologyServiceAdapter; |
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 | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 48 | import static org.hamcrest.Matchers.containsString; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 49 | import static org.hamcrest.Matchers.hasSize; |
| 50 | import static org.hamcrest.Matchers.is; |
| 51 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 52 | import static org.junit.Assert.assertThat; |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 53 | import static org.onosproject.net.NetTestTools.device; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 54 | import static org.onosproject.net.NetTestTools.did; |
| 55 | import static org.onosproject.net.NetTestTools.link; |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 56 | import static org.onosproject.net.PortNumber.portNumber; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Unit tests for Topology REST APIs. |
| 60 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 61 | public class TopologyResourceTest extends ResourceTest { |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 62 | |
| 63 | private static class MockTopology implements Topology { |
| 64 | @Override |
| 65 | public long time() { |
| 66 | return 11111L; |
| 67 | } |
| 68 | |
| 69 | @Override |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 70 | public long creationTime() { |
| 71 | return 22222L; |
| 72 | } |
| 73 | |
| 74 | @Override |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 75 | public long computeCost() { |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int clusterCount() { |
| 81 | return 2; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int deviceCount() { |
| 86 | return 6; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public int linkCount() { |
| 91 | return 4; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public ProviderId providerId() { |
| 96 | return ProviderId.NONE; |
| 97 | } |
| 98 | } |
| 99 | |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 100 | private static class MockTopologyService extends TopologyServiceAdapter { |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 101 | final DefaultTopologyVertex root = new DefaultTopologyVertex(did("rootnode")); |
| 102 | final Topology topology = new MockTopology(); |
| 103 | final TopologyCluster cluster1 = |
| 104 | new DefaultTopologyCluster(ClusterId.clusterId(0), |
| 105 | 2, 1, root); |
| 106 | final TopologyCluster cluster2 = |
| 107 | new DefaultTopologyCluster(ClusterId.clusterId(1), |
| 108 | 4, 3, root); |
| 109 | |
| 110 | @Override |
| 111 | public Topology currentTopology() { |
| 112 | return topology; |
| 113 | } |
| 114 | |
| 115 | @Override |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 116 | public Set<TopologyCluster> getClusters(Topology topology) { |
| 117 | return ImmutableSet.of(cluster1, cluster2); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public TopologyCluster getCluster(Topology topology, ClusterId clusterId) { |
| 122 | return cluster1; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) { |
| 127 | DeviceId device1 = did("dev1"); |
| 128 | DeviceId device2 = did("dev2"); |
| 129 | |
| 130 | return ImmutableSet.of(device1, device2); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) { |
| 135 | Link link1 = link("src1", 1, "dst1", 1); |
| 136 | Link link2 = link("src2", 1, "dst2", 1); |
| 137 | Link link3 = link("src3", 1, "dst3", 1); |
| 138 | return ImmutableSet.of(link1, link2, link3); |
| 139 | } |
| 140 | |
| 141 | @Override |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 142 | public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 143 | return "dev2".equals(connectPoint.elementId().toString()); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) { |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 148 | return "dev1".equals(connectPoint.elementId().toString()); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 153 | private static class MockDeviceService extends DeviceServiceAdapter { |
| 154 | |
| 155 | @Override |
| 156 | public Device getDevice(DeviceId deviceId) { |
| 157 | String deviceId1 = "dev2"; |
| 158 | Device device = device(deviceId1); |
| 159 | return device; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 164 | String deviceIdString = "dev2"; |
| 165 | Device device = device(deviceIdString); |
| 166 | Port port = new DefaultPort(device, portNumber(1), true); |
| 167 | return port; |
| 168 | } |
| 169 | } |
| 170 | |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 171 | /** |
| 172 | * Initializes the test harness. |
| 173 | */ |
| 174 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 175 | public void setUpTest() { |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 176 | TopologyService topologyService = new MockTopologyService(); |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 177 | DeviceService mockDeviceService = new MockDeviceService(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 178 | CodecManager codecService = new CodecManager(); |
| 179 | codecService.activate(); |
| 180 | |
| 181 | ServiceDirectory testDirectory = |
| 182 | new TestServiceDirectory() |
Kavitha Alagesan | 98315a2 | 2016-08-17 08:13:32 +0530 | [diff] [blame] | 183 | .add(DeviceService.class, mockDeviceService) |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 184 | .add(TopologyService.class, topologyService) |
| 185 | .add(CodecService.class, codecService); |
| 186 | BaseResource.setServiceDirectory(testDirectory); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Tests the topology overview. |
| 191 | */ |
| 192 | @Test |
| 193 | public void getTopology() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 194 | WebTarget wt = target(); |
| 195 | String response = wt.path("topology").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 196 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 197 | assertThat(result, notNullValue()); |
| 198 | |
| 199 | assertThat(result.names(), hasSize(4)); |
| 200 | |
| 201 | assertThat(result.get("time").asLong(), is(11111L)); |
| 202 | assertThat(result.get("clusters").asLong(), is(2L)); |
| 203 | assertThat(result.get("devices").asLong(), is(6L)); |
| 204 | assertThat(result.get("links").asLong(), is(4L)); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Tests the clusters overview. |
| 209 | */ |
| 210 | @Test |
| 211 | public void getTopologyClusters() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 212 | WebTarget wt = target(); |
| 213 | String response = wt.path("topology/clusters").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 214 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 215 | assertThat(result, notNullValue()); |
| 216 | |
| 217 | assertThat(result.names(), hasSize(1)); |
| 218 | JsonArray clusters = result.get("clusters").asArray(); |
| 219 | assertThat(clusters, notNullValue()); |
| 220 | assertThat(clusters.size(), is(2)); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Tests an individual cluster overview. |
| 225 | */ |
| 226 | @Test |
| 227 | public void getCluster() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 228 | WebTarget wt = target(); |
| 229 | String response = wt.path("topology/clusters/0").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 230 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 231 | assertThat(result, notNullValue()); |
| 232 | |
| 233 | assertThat(result.get("id").asLong(), is(0L)); |
| 234 | assertThat(result.get("deviceCount").asLong(), is(2L)); |
| 235 | assertThat(result.get("linkCount").asLong(), is(1L)); |
| 236 | assertThat(result.get("root").asString(), containsString("rootnode")); |
| 237 | |
| 238 | assertThat(result.names(), hasSize(4)); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Tests an individual cluster's devices list. |
| 243 | */ |
| 244 | @Test |
| 245 | public void getClusterDevices() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 246 | WebTarget wt = target(); |
| 247 | String response = wt.path("topology/clusters/0/devices").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 248 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 249 | assertThat(result, notNullValue()); |
| 250 | |
| 251 | JsonArray devices = result.get("devices").asArray(); |
| 252 | assertThat(devices.size(), is(2)); |
| 253 | |
| 254 | assertThat(devices.get(0).asString(), is("of:dev1")); |
| 255 | assertThat(devices.get(1).asString(), is("of:dev2")); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Tests an individual cluster's links list. |
| 260 | */ |
| 261 | @Test |
| 262 | public void getClusterLinks() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 263 | WebTarget wt = target(); |
| 264 | String response = wt.path("topology/clusters/1/links").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 265 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 266 | assertThat(result, notNullValue()); |
| 267 | |
| 268 | JsonArray links = result.get("links").asArray(); |
| 269 | assertThat(links.size(), is(3)); |
| 270 | |
| 271 | JsonObject link0 = links.get(0).asObject(); |
| 272 | JsonObject src0 = link0.get("src").asObject(); |
| 273 | String device0 = src0.get("device").asString(); |
| 274 | assertThat(device0, is("of:src1")); |
| 275 | |
| 276 | JsonObject link2 = links.get(2).asObject(); |
| 277 | JsonObject src2 = link2.get("src").asObject(); |
| 278 | String device2 = src2.get("device").asString(); |
| 279 | assertThat(device2, is("of:src3")); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Tests a broadcast query. |
| 284 | */ |
| 285 | @Test |
| 286 | public void getBroadcast() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 287 | WebTarget wt = target(); |
| 288 | String response = wt.path("topology/broadcast/dev1:1").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 289 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 290 | assertThat(result, notNullValue()); |
| 291 | |
| 292 | assertThat(result.get("broadcast").asBoolean(), is(true)); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Tests an infrastructure query. |
| 297 | */ |
| 298 | @Test |
| 299 | public void getInfrastructure() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 300 | WebTarget wt = target(); |
| 301 | String response = wt.path("topology/infrastructure/dev2:1").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 302 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 303 | assertThat(result, notNullValue()); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 304 | assertThat(result.get("infrastructure").asBoolean(), is(true)); |
| 305 | } |
| 306 | } |