Naoki Shiota | 03c29e1 | 2016-05-16 16:58:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 17 | package org.onosproject.newoptical; |
| 18 | |
| 19 | import javafx.util.Pair; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.ChassisId; |
| 24 | import org.onlab.util.Bandwidth; |
| 25 | import org.onlab.util.Frequency; |
| 26 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreServiceAdapter; |
| 29 | import org.onosproject.core.DefaultApplicationId; |
| 30 | import org.onosproject.core.IdGenerator; |
| 31 | import org.onosproject.event.DefaultEventSinkRegistry; |
| 32 | import org.onosproject.event.Event; |
| 33 | import org.onosproject.event.EventDeliveryService; |
| 34 | import org.onosproject.event.EventSink; |
| 35 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 36 | import org.onosproject.net.ChannelSpacing; |
| 37 | import org.onosproject.net.CltSignalType; |
| 38 | import org.onosproject.net.ConnectPoint; |
| 39 | import org.onosproject.net.DefaultDevice; |
| 40 | import org.onosproject.net.DefaultLink; |
| 41 | import org.onosproject.net.DefaultPath; |
| 42 | import org.onosproject.net.DefaultPort; |
| 43 | import org.onosproject.net.Device; |
| 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.onosproject.net.ElementId; |
| 46 | import org.onosproject.net.Link; |
| 47 | import org.onosproject.net.OchSignal; |
| 48 | import org.onosproject.net.OduSignalType; |
| 49 | import org.onosproject.net.Path; |
| 50 | import org.onosproject.net.Port; |
| 51 | import org.onosproject.net.PortNumber; |
| 52 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
| 53 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 54 | import org.onosproject.net.intent.Intent; |
| 55 | import org.onosproject.net.intent.IntentEvent; |
| 56 | import org.onosproject.net.intent.IntentListener; |
| 57 | import org.onosproject.net.intent.IntentServiceAdapter; |
| 58 | import org.onosproject.net.intent.Key; |
| 59 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 60 | import org.onosproject.net.link.LinkServiceAdapter; |
| 61 | import org.onosproject.net.optical.impl.DefaultOchPort; |
| 62 | import org.onosproject.net.optical.impl.DefaultOduCltPort; |
| 63 | import org.onosproject.net.optical.impl.DefaultOmsPort; |
| 64 | import org.onosproject.net.provider.ProviderId; |
| 65 | import org.onosproject.net.resource.DiscreteResourceId; |
| 66 | import org.onosproject.net.resource.Resource; |
| 67 | import org.onosproject.net.resource.ResourceAllocation; |
| 68 | import org.onosproject.net.resource.ResourceConsumer; |
| 69 | import org.onosproject.net.resource.ResourceId; |
| 70 | import org.onosproject.net.resource.ResourceListener; |
| 71 | import org.onosproject.net.resource.ResourceService; |
| 72 | import org.onosproject.net.topology.LinkWeight; |
| 73 | import org.onosproject.net.topology.PathServiceAdapter; |
| 74 | import org.onosproject.newoptical.api.OpticalConnectivityId; |
| 75 | import org.onosproject.newoptical.api.OpticalPathEvent; |
| 76 | import org.onosproject.newoptical.api.OpticalPathListener; |
| 77 | import org.onosproject.store.service.AtomicCounter; |
| 78 | import org.onosproject.store.service.StorageServiceAdapter; |
| 79 | |
| 80 | import java.time.Duration; |
| 81 | import java.util.ArrayList; |
| 82 | import java.util.Collection; |
| 83 | import java.util.Collections; |
| 84 | import java.util.HashMap; |
| 85 | import java.util.HashSet; |
| 86 | import java.util.List; |
| 87 | import java.util.Map; |
| 88 | import java.util.Optional; |
| 89 | import java.util.Set; |
| 90 | import java.util.stream.Collectors; |
| 91 | import java.util.stream.Stream; |
| 92 | |
| 93 | import static com.google.common.base.Preconditions.checkState; |
| 94 | import static org.junit.Assert.assertEquals; |
| 95 | import static org.junit.Assert.assertNotNull; |
| 96 | import static org.junit.Assert.assertTrue; |
| 97 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
| 98 | |
| 99 | /** |
| 100 | * Tests for OpticalPathProvisioner class. |
| 101 | */ |
| 102 | public class OpticalPathProvisionerTest { |
| 103 | |
| 104 | private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo"); |
| 105 | |
| 106 | // 7-nodes linear topology containing packet/cross-connect/optical links |
| 107 | private static final ConnectPoint CP11 = createConnectPoint(1, 1); |
| 108 | private static final ConnectPoint CP12 = createConnectPoint(1, 2); |
| 109 | private static final ConnectPoint CP21 = createConnectPoint(2, 1); |
| 110 | private static final ConnectPoint CP22 = createConnectPoint(2, 2); // cross connect port (packet) |
| 111 | private static final ConnectPoint CP31 = createConnectPoint(3, 1); // cross connect port (oductl) |
| 112 | private static final ConnectPoint CP32 = createConnectPoint(3, 2); |
| 113 | private static final ConnectPoint CP41 = createConnectPoint(4, 1); |
| 114 | private static final ConnectPoint CP42 = createConnectPoint(4, 2); |
| 115 | private static final ConnectPoint CP51 = createConnectPoint(5, 1); |
| 116 | private static final ConnectPoint CP52 = createConnectPoint(5, 2); // cross connect port (oductl) |
| 117 | private static final ConnectPoint CP61 = createConnectPoint(6, 1); // cross connect port (packet) |
| 118 | private static final ConnectPoint CP62 = createConnectPoint(6, 2); |
| 119 | private static final ConnectPoint CP71 = createConnectPoint(7, 1); |
| 120 | private static final ConnectPoint CP72 = createConnectPoint(7, 2); |
| 121 | |
| 122 | private static final Link LINK1 = createLink(CP12, CP21, Link.Type.DIRECT); |
| 123 | private static final Link LINK2 = createLink(CP22, CP31, Link.Type.OPTICAL); // cross connect link |
| 124 | private static final Link LINK3 = createLink(CP32, CP41, Link.Type.OPTICAL); |
| 125 | private static final Link LINK4 = createLink(CP42, CP51, Link.Type.OPTICAL); |
| 126 | private static final Link LINK5 = createLink(CP52, CP61, Link.Type.OPTICAL); // cross connect link |
| 127 | private static final Link LINK6 = createLink(CP62, CP71, Link.Type.DIRECT); |
| 128 | |
| 129 | private static final Device DEVICE1 = createDevice(1, Device.Type.SWITCH); |
| 130 | private static final Device DEVICE2 = createDevice(2, Device.Type.SWITCH); |
| 131 | private static final Device DEVICE3 = createDevice(3, Device.Type.ROADM); |
| 132 | private static final Device DEVICE4 = createDevice(4, Device.Type.ROADM); |
| 133 | private static final Device DEVICE5 = createDevice(5, Device.Type.ROADM); |
| 134 | private static final Device DEVICE6 = createDevice(6, Device.Type.SWITCH); |
| 135 | private static final Device DEVICE7 = createDevice(7, Device.Type.SWITCH); |
| 136 | |
| 137 | private static final Port PORT11 = createPacketPort(DEVICE1, CP11); |
| 138 | private static final Port PORT12 = createPacketPort(DEVICE1, CP12); |
| 139 | private static final Port PORT21 = createPacketPort(DEVICE2, CP21); |
| 140 | private static final Port PORT22 = createOduCltPort(DEVICE2, CP22); |
| 141 | private static final Port PORT31 = createOchPort(DEVICE3, CP31); |
| 142 | private static final Port PORT32 = createOmsPort(DEVICE3, CP32); |
| 143 | private static final Port PORT41 = createOmsPort(DEVICE4, CP41); |
| 144 | private static final Port PORT42 = createOmsPort(DEVICE4, CP42); |
| 145 | private static final Port PORT51 = createOmsPort(DEVICE5, CP51); |
| 146 | private static final Port PORT52 = createOchPort(DEVICE5, CP52); |
| 147 | private static final Port PORT61 = createOduCltPort(DEVICE6, CP61); |
| 148 | private static final Port PORT62 = createPacketPort(DEVICE6, CP62); |
| 149 | private static final Port PORT71 = createPacketPort(DEVICE7, CP71); |
| 150 | private static final Port PORT72 = createPacketPort(DEVICE7, CP72); |
| 151 | |
| 152 | protected OpticalPathProvisioner target; |
| 153 | protected TestListener listener = new TestListener(); |
| 154 | protected TestDeviceService deviceService; |
| 155 | protected TestLinkService linkService; |
| 156 | protected TestPathService pathService; |
| 157 | protected TestIntentService intentService; |
| 158 | protected IdGenerator idGenerator; |
| 159 | |
| 160 | @Before |
| 161 | public void setUp() { |
| 162 | this.deviceService = new TestDeviceService(); |
| 163 | deviceService.devMap.put(deviceIdOf(1), DEVICE1); |
| 164 | deviceService.devMap.put(deviceIdOf(2), DEVICE2); |
| 165 | deviceService.devMap.put(deviceIdOf(3), DEVICE3); |
| 166 | deviceService.devMap.put(deviceIdOf(4), DEVICE4); |
| 167 | deviceService.devMap.put(deviceIdOf(5), DEVICE5); |
| 168 | deviceService.devMap.put(deviceIdOf(6), DEVICE6); |
| 169 | deviceService.devMap.put(deviceIdOf(7), DEVICE7); |
| 170 | deviceService.portMap.put(CP11, PORT11); |
| 171 | deviceService.portMap.put(CP12, PORT12); |
| 172 | deviceService.portMap.put(CP21, PORT21); |
| 173 | deviceService.portMap.put(CP22, PORT22); |
| 174 | deviceService.portMap.put(CP31, PORT31); |
| 175 | deviceService.portMap.put(CP32, PORT32); |
| 176 | deviceService.portMap.put(CP41, PORT41); |
| 177 | deviceService.portMap.put(CP42, PORT42); |
| 178 | deviceService.portMap.put(CP51, PORT51); |
| 179 | deviceService.portMap.put(CP52, PORT52); |
| 180 | deviceService.portMap.put(CP61, PORT61); |
| 181 | deviceService.portMap.put(CP62, PORT62); |
| 182 | deviceService.portMap.put(CP71, PORT71); |
| 183 | deviceService.portMap.put(CP72, PORT72); |
| 184 | |
| 185 | this.linkService = new TestLinkService(); |
| 186 | linkService.links.addAll(Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6) |
| 187 | .collect(Collectors.toList())); |
| 188 | |
| 189 | this.pathService = new TestPathService(); |
| 190 | this.intentService = new TestIntentService(); |
| 191 | |
| 192 | this.target = new OpticalPathProvisioner(); |
| 193 | target.coreService = new TestCoreService(); |
| 194 | target.intentService = this.intentService; |
| 195 | target.pathService = this.pathService; |
| 196 | target.linkService = this.linkService; |
| 197 | target.mastershipService = new TestMastershipService(); |
| 198 | target.clusterService = new TestClusterService(); |
| 199 | target.storageService = new TestStorageService(); |
| 200 | target.deviceService = this.deviceService; |
| 201 | target.networkConfigService = new TestNetworkConfigService(); |
| 202 | target.resourceService = new TestResourceService(); |
| 203 | injectEventDispatcher(target, new TestEventDispatcher()); |
| 204 | target.addListener(listener); |
| 205 | |
| 206 | target.activate(); |
| 207 | |
| 208 | // To overwrite opticalView-ed deviceService |
| 209 | target.deviceService = this.deviceService; |
| 210 | |
| 211 | idGenerator = new IdGenerator() { |
| 212 | int counter = 1; |
| 213 | |
| 214 | @Override |
| 215 | public long getNewId() { |
| 216 | return counter++; |
| 217 | } |
| 218 | }; |
| 219 | Intent.bindIdGenerator(idGenerator); |
| 220 | } |
| 221 | |
| 222 | @After |
| 223 | public void tearDown() { |
| 224 | Intent.unbindIdGenerator(idGenerator); |
| 225 | target.removeListener(listener); |
| 226 | target = null; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Checks setupConnectivity method works. |
| 231 | */ |
| 232 | @Test |
| 233 | public void testSetupConnectivity() { |
| 234 | Bandwidth bandwidth = Bandwidth.bps(100); |
| 235 | Duration latency = Duration.ofMillis(10); |
| 236 | |
| 237 | OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency); |
| 238 | assertNotNull(cid); |
| 239 | |
| 240 | // Checks path computation is called as expected |
| 241 | assertEquals(1, pathService.edges.size()); |
| 242 | assertEquals(CP12.deviceId(), pathService.edges.get(0).getKey()); |
| 243 | assertEquals(CP71.deviceId(), pathService.edges.get(0).getValue()); |
| 244 | |
| 245 | // Checks intents are installed as expected |
| 246 | assertEquals(1, intentService.submitted.size()); |
| 247 | assertEquals(OpticalConnectivityIntent.class, intentService.submitted.get(0).getClass()); |
| 248 | OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.submitted.get(0); |
| 249 | assertEquals(CP31, connIntent.getSrc()); |
| 250 | assertEquals(CP52, connIntent.getDst()); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Checks setupPath method works. |
| 255 | */ |
| 256 | @Test |
| 257 | public void testSetupPath() { |
| 258 | Bandwidth bandwidth = Bandwidth.bps(100); |
| 259 | Duration latency = Duration.ofMillis(10); |
| 260 | List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6) |
| 261 | .collect(Collectors.toList()); |
| 262 | Path path = new DefaultPath(PROVIDER_ID, links, 0); |
| 263 | |
| 264 | OpticalConnectivityId cid = target.setupPath(path, bandwidth, latency); |
| 265 | assertNotNull(cid); |
| 266 | |
| 267 | // Checks intents are installed as expected |
| 268 | assertEquals(1, intentService.submitted.size()); |
| 269 | assertEquals(OpticalConnectivityIntent.class, intentService.submitted.get(0).getClass()); |
| 270 | OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.submitted.get(0); |
| 271 | assertEquals(CP31, connIntent.getSrc()); |
| 272 | assertEquals(CP52, connIntent.getDst()); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Checks removeConnectivity method works. |
| 277 | */ |
| 278 | @Test |
| 279 | public void testRemoveConnectivity() { |
| 280 | Bandwidth bandwidth = Bandwidth.bps(100); |
| 281 | Duration latency = Duration.ofMillis(10); |
| 282 | |
| 283 | OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency); |
| 284 | |
| 285 | // Checks intents are withdrawn |
| 286 | assertTrue(target.removeConnectivity(cid)); |
| 287 | assertEquals(1, intentService.withdrawn.size()); |
| 288 | assertEquals(OpticalConnectivityIntent.class, intentService.withdrawn.get(0).getClass()); |
| 289 | OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.withdrawn.get(0); |
| 290 | assertEquals(CP31, connIntent.getSrc()); |
| 291 | assertEquals(CP52, connIntent.getDst()); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Checks getPath method works. |
| 296 | */ |
| 297 | @Test |
| 298 | public void testGetPath() { |
| 299 | Bandwidth bandwidth = Bandwidth.bps(100); |
| 300 | Duration latency = Duration.ofMillis(10); |
| 301 | List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6) |
| 302 | .collect(Collectors.toList()); |
| 303 | |
| 304 | OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency); |
| 305 | Optional<List<Link>> path = target.getPath(cid); |
| 306 | |
| 307 | // Checks returned path is as expected |
| 308 | assertTrue(path.isPresent()); |
| 309 | assertEquals(links, path.get()); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Checks if PATH_INSTALLED event comes up after intent is installed. |
| 314 | */ |
| 315 | @Test |
| 316 | public void testInstalledEvent() { |
| 317 | Bandwidth bandwidth = Bandwidth.bps(100); |
| 318 | Duration latency = Duration.ofMillis(10); |
| 319 | |
| 320 | OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency); |
| 321 | |
| 322 | intentService.notifyInstalled(); |
| 323 | |
| 324 | assertEquals(1, listener.events.size()); |
| 325 | assertEquals(OpticalPathEvent.Type.PATH_INSTALLED, listener.events.get(0).type()); |
| 326 | assertEquals(cid, listener.events.get(0).subject()); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Checks if PATH_REMOVED event comes up after packet link is removed. |
| 331 | */ |
| 332 | @Test |
| 333 | public void testRemovedEvent() { |
| 334 | Bandwidth bandwidth = Bandwidth.bps(100); |
| 335 | Duration latency = Duration.ofMillis(10); |
| 336 | |
| 337 | OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency); |
| 338 | |
| 339 | intentService.notifyInstalled(); |
| 340 | |
| 341 | target.removeConnectivity(cid); |
| 342 | |
| 343 | intentService.notifyWithdrawn(); |
| 344 | |
| 345 | assertEquals(2, listener.events.size()); |
| 346 | assertEquals(OpticalPathEvent.Type.PATH_REMOVED, listener.events.get(1).type()); |
| 347 | assertEquals(cid, listener.events.get(1).subject()); |
| 348 | } |
| 349 | |
| 350 | private static ConnectPoint createConnectPoint(long devIdNum, long portIdNum) { |
| 351 | return new ConnectPoint( |
| 352 | deviceIdOf(devIdNum), |
| 353 | PortNumber.portNumber(portIdNum)); |
| 354 | } |
| 355 | |
| 356 | private static Link createLink(ConnectPoint src, ConnectPoint dst, Link.Type type) { |
| 357 | return DefaultLink.builder() |
| 358 | .providerId(PROVIDER_ID) |
| 359 | .src(src) |
| 360 | .dst(dst) |
| 361 | .state(Link.State.ACTIVE) |
| 362 | .type(type).build(); |
| 363 | } |
| 364 | |
| 365 | private static Device createDevice(long devIdNum, Device.Type type) { |
| 366 | return new DefaultDevice(PROVIDER_ID, |
| 367 | deviceIdOf(devIdNum), |
| 368 | type, |
| 369 | "manufacturer", |
| 370 | "hwVersion", |
| 371 | "swVersion", |
| 372 | "serialNumber", |
| 373 | new ChassisId(1)); |
| 374 | } |
| 375 | |
| 376 | private static Port createPacketPort(Device device, ConnectPoint cp) { |
| 377 | return new DefaultPort(device, cp.port(), true); |
| 378 | } |
| 379 | |
| 380 | private static Port createOchPort(Device device, ConnectPoint cp) { |
| 381 | return new DefaultOchPort(new DefaultPort(device, cp.port(), true), |
| 382 | OduSignalType.ODU4, |
| 383 | true, |
| 384 | OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1)); |
| 385 | } |
| 386 | |
| 387 | private static Port createOduCltPort(Device device, ConnectPoint cp) { |
| 388 | return new DefaultOduCltPort(new DefaultPort(device, cp.port(), true), |
| 389 | CltSignalType.CLT_100GBE); |
| 390 | } |
| 391 | |
| 392 | private static Port createOmsPort(Device device, ConnectPoint cp) { |
| 393 | return new DefaultOmsPort(new DefaultPort(device, cp.port(), true), |
| 394 | Frequency.ofKHz(3), |
| 395 | Frequency.ofKHz(33), |
| 396 | Frequency.ofKHz(2)); |
| 397 | } |
| 398 | |
| 399 | private static DeviceId deviceIdOf(long devIdNum) { |
| 400 | return DeviceId.deviceId(String.format("of:%016d", devIdNum)); |
| 401 | } |
| 402 | |
| 403 | private static class TestListener implements OpticalPathListener { |
| 404 | final List<OpticalPathEvent> events = new ArrayList<>(); |
| 405 | |
| 406 | @Override |
| 407 | public void event(OpticalPathEvent event) { |
| 408 | events.add(event); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | private static class TestPathService extends PathServiceAdapter { |
| 413 | List<Pair<DeviceId, DeviceId>> edges = new ArrayList<>(); |
| 414 | |
| 415 | @Override |
| 416 | public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) { |
| 417 | if (!(src instanceof DeviceId && dst instanceof DeviceId)) { |
| 418 | return Collections.emptySet(); |
| 419 | } |
| 420 | |
| 421 | edges.add(new Pair<>((DeviceId) src, (DeviceId) dst)); |
| 422 | |
| 423 | Set<Path> paths = new HashSet<>(); |
| 424 | List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6) |
| 425 | .collect(Collectors.toList()); |
| 426 | paths.add(new DefaultPath(PROVIDER_ID, links, 0)); |
| 427 | |
| 428 | // returns paths containing single path |
| 429 | return paths; |
| 430 | } |
| 431 | |
| 432 | } |
| 433 | |
| 434 | private static class TestIntentService extends IntentServiceAdapter { |
| 435 | List<Intent> submitted = new ArrayList<>(); |
| 436 | List<Intent> withdrawn = new ArrayList<>(); |
| 437 | List<IntentListener> listeners = new ArrayList<>(); |
| 438 | |
| 439 | @Override |
| 440 | public void submit(Intent intent) { |
| 441 | submitted.add(intent); |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public void withdraw(Intent intent) { |
| 446 | withdrawn.add(intent); |
| 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public void addListener(IntentListener listener) { |
| 451 | listeners.add(listener); |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public Intent getIntent(Key intentKey) { |
| 456 | Intent intent = submitted.stream().filter(i -> i.key().equals(intentKey)) |
| 457 | .findAny() |
| 458 | .get(); |
| 459 | return intent; |
| 460 | } |
| 461 | |
| 462 | void notifyInstalled() { |
| 463 | submitted.forEach(i -> { |
| 464 | IntentEvent event = new IntentEvent(IntentEvent.Type.INSTALLED, i); |
| 465 | listeners.forEach(l -> l.event(event)); |
| 466 | }); |
| 467 | } |
| 468 | |
| 469 | void notifyWithdrawn() { |
| 470 | withdrawn.forEach(i -> { |
| 471 | IntentEvent event = new IntentEvent(IntentEvent.Type.WITHDRAWN, i); |
| 472 | listeners.forEach(l -> l.event(event)); |
| 473 | }); |
| 474 | } |
| 475 | |
| 476 | } |
| 477 | |
| 478 | private static class TestLinkService extends LinkServiceAdapter { |
| 479 | List<Link> links = new ArrayList<>(); |
| 480 | |
| 481 | @Override |
| 482 | public Set<Link> getLinks(ConnectPoint connectPoint) { |
| 483 | return links.stream() |
| 484 | .filter(l -> l.src().equals(connectPoint) || l.dst().equals(connectPoint)) |
| 485 | .collect(Collectors.toSet()); |
| 486 | } |
| 487 | |
| 488 | } |
| 489 | |
| 490 | private static class TestCoreService extends CoreServiceAdapter { |
| 491 | @Override |
| 492 | public ApplicationId registerApplication(String name) { |
| 493 | return new DefaultApplicationId(0, name); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | private static class TestMastershipService extends MastershipServiceAdapter { |
| 498 | |
| 499 | } |
| 500 | |
| 501 | private static class TestClusterService extends ClusterServiceAdapter { |
| 502 | |
| 503 | } |
| 504 | |
| 505 | private static class TestStorageService extends StorageServiceAdapter { |
| 506 | @Override |
| 507 | public AtomicCounter getAtomicCounter(String name) { |
| 508 | return new MockAtomicCounter(); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | private static class TestDeviceService extends DeviceServiceAdapter { |
| 513 | Map<DeviceId, Device> devMap = new HashMap<>(); |
| 514 | Map<ConnectPoint, Port> portMap = new HashMap<>(); |
| 515 | |
| 516 | @Override |
| 517 | public Device getDevice(DeviceId deviceId) { |
| 518 | return devMap.get(deviceId); |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 523 | return portMap.get(new ConnectPoint(deviceId, portNumber)); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | private static class TestNetworkConfigService extends NetworkConfigServiceAdapter { |
| 528 | |
| 529 | } |
| 530 | |
| 531 | private static class TestResourceService implements ResourceService { |
| 532 | |
| 533 | @Override |
| 534 | public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<Resource> resources) { |
| 535 | List<ResourceAllocation> allocations = new ArrayList<>(); |
| 536 | |
| 537 | resources.forEach(r -> allocations.add(new ResourceAllocation(r, consumer.consumerId()))); |
| 538 | |
| 539 | return allocations; |
| 540 | } |
| 541 | |
| 542 | @Override |
| 543 | public boolean release(List<ResourceAllocation> allocations) { |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public boolean release(ResourceConsumer consumer) { |
| 549 | |
| 550 | return true; |
| 551 | } |
| 552 | |
| 553 | @Override |
| 554 | public void addListener(ResourceListener listener) { |
| 555 | |
| 556 | } |
| 557 | |
| 558 | @Override |
| 559 | public void removeListener(ResourceListener listener) { |
| 560 | |
| 561 | } |
| 562 | |
| 563 | @Override |
| 564 | public List<ResourceAllocation> getResourceAllocations(ResourceId id) { |
| 565 | return null; |
| 566 | } |
| 567 | |
| 568 | @Override |
| 569 | public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) { |
| 570 | return null; |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) { |
| 575 | return null; |
| 576 | } |
| 577 | |
| 578 | @Override |
| 579 | public Set<Resource> getAvailableResources(DiscreteResourceId parent) { |
| 580 | return null; |
| 581 | } |
| 582 | |
| 583 | @Override |
| 584 | public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) { |
| 585 | return null; |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) { |
| 590 | return null; |
| 591 | } |
| 592 | |
| 593 | @Override |
| 594 | public Set<Resource> getRegisteredResources(DiscreteResourceId parent) { |
| 595 | return null; |
| 596 | } |
| 597 | |
| 598 | @Override |
| 599 | public boolean isAvailable(Resource resource) { |
| 600 | return true; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | private static class MockAtomicCounter implements AtomicCounter { |
| 605 | long id = 0; |
| 606 | |
| 607 | @Override |
| 608 | public long incrementAndGet() { |
| 609 | return ++id; |
| 610 | } |
| 611 | |
| 612 | @Override |
| 613 | public long getAndIncrement() { |
| 614 | return id++; |
| 615 | } |
| 616 | |
| 617 | @Override |
| 618 | public long getAndAdd(long delta) { |
| 619 | long oldId = id; |
| 620 | id += delta; |
| 621 | return oldId; |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public long addAndGet(long delta) { |
| 626 | id += delta; |
| 627 | return id; |
| 628 | } |
| 629 | |
| 630 | @Override |
| 631 | public void set(long value) { |
| 632 | id = value; |
| 633 | } |
| 634 | |
| 635 | @Override |
| 636 | public boolean compareAndSet(long expectedValue, long updateValue) { |
| 637 | if (id == expectedValue) { |
| 638 | id = updateValue; |
| 639 | return true; |
| 640 | } else { |
| 641 | return false; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | @Override |
| 646 | public long get() { |
| 647 | return id; |
| 648 | } |
| 649 | |
| 650 | @Override |
| 651 | public String name() { |
| 652 | return "MockAtomicCounter"; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | // copied from org.onosproject.common.event.impl.TestEventDispatcher |
| 657 | /** |
| 658 | * Implements event delivery system that delivers events synchronously, or |
| 659 | * in-line with the post method invocation. |
| 660 | */ |
| 661 | public class TestEventDispatcher extends DefaultEventSinkRegistry |
| 662 | implements EventDeliveryService { |
| 663 | |
| 664 | @Override |
| 665 | @SuppressWarnings("unchecked") |
| 666 | public synchronized void post(Event event) { |
| 667 | EventSink sink = getSink(event.getClass()); |
| 668 | checkState(sink != null, "No sink for event %s", event); |
| 669 | sink.process(event); |
| 670 | } |
| 671 | |
| 672 | @Override |
| 673 | public void setDispatchTimeLimit(long millis) { |
| 674 | } |
| 675 | |
| 676 | @Override |
| 677 | public long getDispatchTimeLimit() { |
| 678 | return 0; |
| 679 | } |
| 680 | } |
| 681 | } |