Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.rest; |
| 17 | |
| 18 | import java.util.Set; |
| 19 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 20 | import com.eclipsesource.json.Json; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
| 24 | import org.onlab.osgi.TestServiceDirectory; |
| 25 | import org.onlab.rest.BaseResource; |
| 26 | import org.onosproject.codec.CodecService; |
| 27 | import org.onosproject.codec.impl.CodecManager; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Link; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 31 | import org.onosproject.net.provider.ProviderId; |
| 32 | import org.onosproject.net.topology.ClusterId; |
| 33 | import org.onosproject.net.topology.DefaultTopologyCluster; |
| 34 | import org.onosproject.net.topology.DefaultTopologyVertex; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 35 | import org.onosproject.net.topology.Topology; |
| 36 | import org.onosproject.net.topology.TopologyCluster; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 37 | import org.onosproject.net.topology.TopologyService; |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 38 | import org.onosproject.net.topology.TopologyServiceAdapter; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 39 | |
| 40 | import com.eclipsesource.json.JsonArray; |
| 41 | import com.eclipsesource.json.JsonObject; |
| 42 | import com.google.common.collect.ImmutableSet; |
| 43 | import com.sun.jersey.api.client.WebResource; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 44 | |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 45 | import static org.hamcrest.Matchers.containsString; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 46 | import static org.hamcrest.Matchers.hasSize; |
| 47 | import static org.hamcrest.Matchers.is; |
| 48 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 49 | import static org.junit.Assert.assertThat; |
| 50 | import static org.onosproject.net.NetTestTools.did; |
| 51 | import static org.onosproject.net.NetTestTools.link; |
| 52 | |
| 53 | /** |
| 54 | * Unit tests for Topology REST APIs. |
| 55 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 56 | public class TopologyResourceTest extends ResourceTest { |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 57 | |
| 58 | private static class MockTopology implements Topology { |
| 59 | @Override |
| 60 | public long time() { |
| 61 | return 11111L; |
| 62 | } |
| 63 | |
| 64 | @Override |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 65 | public long creationTime() { |
| 66 | return 22222L; |
| 67 | } |
| 68 | |
| 69 | @Override |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 70 | 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 Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 95 | private static class MockTopologyService extends TopologyServiceAdapter { |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 96 | 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 Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 111 | 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 Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 137 | 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 Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 148 | /** |
| 149 | * Initializes the test harness. |
| 150 | */ |
| 151 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 152 | public void setUpTest() { |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 153 | 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 Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 171 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 172 | 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 Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 189 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 190 | 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 Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 205 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 206 | 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 Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 223 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 224 | 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 Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 240 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 241 | 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 Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 264 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 265 | 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 Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 277 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | c401e6e | 2015-01-24 10:40:03 -0800 | [diff] [blame] | 278 | assertThat(result, notNullValue()); |
| 279 | |
| 280 | assertThat(result.get("infrastructure").asBoolean(), is(true)); |
| 281 | } |
| 282 | } |