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