blob: 254df5302224de9db215010157f5a60ae5c3de44 [file] [log] [blame]
Naoki Shiota03c29e12016-05-16 16:58:07 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Naoki Shiota03c29e12016-05-16 16:58:07 -07003 *
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
17package org.onosproject.newoptical;
18
Yuta HIGUCHIfc0529e2016-07-10 00:28:08 -070019import org.apache.commons.lang3.tuple.Pair;
Naoki Shiota03c29e12016-05-16 16:58:07 -070020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.ChassisId;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070024import org.onlab.packet.IpAddress;
Naoki Shiota03c29e12016-05-16 16:58:07 -070025import org.onlab.util.Bandwidth;
26import org.onlab.util.Frequency;
27import org.onosproject.cluster.ClusterServiceAdapter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070028import org.onosproject.cluster.ControllerNode;
29import org.onosproject.cluster.NodeId;
Naoki Shiota03c29e12016-05-16 16:58:07 -070030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreServiceAdapter;
32import org.onosproject.core.DefaultApplicationId;
33import org.onosproject.core.IdGenerator;
34import org.onosproject.event.DefaultEventSinkRegistry;
35import org.onosproject.event.Event;
36import org.onosproject.event.EventDeliveryService;
37import org.onosproject.event.EventSink;
38import org.onosproject.mastership.MastershipServiceAdapter;
39import org.onosproject.net.ChannelSpacing;
40import org.onosproject.net.CltSignalType;
41import org.onosproject.net.ConnectPoint;
42import org.onosproject.net.DefaultDevice;
43import org.onosproject.net.DefaultLink;
44import org.onosproject.net.DefaultPath;
45import org.onosproject.net.DefaultPort;
46import org.onosproject.net.Device;
47import org.onosproject.net.DeviceId;
48import org.onosproject.net.ElementId;
49import org.onosproject.net.Link;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070050import org.onosproject.net.MastershipRole;
Naoki Shiota03c29e12016-05-16 16:58:07 -070051import org.onosproject.net.OchSignal;
52import org.onosproject.net.OduSignalType;
53import org.onosproject.net.Path;
54import org.onosproject.net.Port;
55import org.onosproject.net.PortNumber;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070056import org.onosproject.net.config.Config;
Naoki Shiota03c29e12016-05-16 16:58:07 -070057import org.onosproject.net.config.NetworkConfigServiceAdapter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070058import org.onosproject.net.config.basics.BandwidthCapacity;
Naoki Shiota03c29e12016-05-16 16:58:07 -070059import org.onosproject.net.device.DeviceServiceAdapter;
60import org.onosproject.net.intent.Intent;
61import org.onosproject.net.intent.IntentEvent;
62import org.onosproject.net.intent.IntentListener;
63import org.onosproject.net.intent.IntentServiceAdapter;
64import org.onosproject.net.intent.Key;
65import org.onosproject.net.intent.OpticalConnectivityIntent;
66import org.onosproject.net.link.LinkServiceAdapter;
67import org.onosproject.net.optical.impl.DefaultOchPort;
68import org.onosproject.net.optical.impl.DefaultOduCltPort;
69import org.onosproject.net.optical.impl.DefaultOmsPort;
70import org.onosproject.net.provider.ProviderId;
71import org.onosproject.net.resource.DiscreteResourceId;
72import org.onosproject.net.resource.Resource;
73import org.onosproject.net.resource.ResourceAllocation;
74import org.onosproject.net.resource.ResourceConsumer;
75import org.onosproject.net.resource.ResourceId;
76import org.onosproject.net.resource.ResourceListener;
77import org.onosproject.net.resource.ResourceService;
78import org.onosproject.net.topology.LinkWeight;
79import org.onosproject.net.topology.PathServiceAdapter;
80import org.onosproject.newoptical.api.OpticalConnectivityId;
81import org.onosproject.newoptical.api.OpticalPathEvent;
82import org.onosproject.newoptical.api.OpticalPathListener;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070083import org.onosproject.store.service.AsyncConsistentMap;
84import org.onosproject.store.service.AsyncDistributedSet;
Naoki Shiota03c29e12016-05-16 16:58:07 -070085import org.onosproject.store.service.AtomicCounter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070086import org.onosproject.store.service.ConsistentMap;
87import org.onosproject.store.service.ConsistentMapAdapter;
88import org.onosproject.store.service.ConsistentMapBuilder;
89import org.onosproject.store.service.DistributedSet;
90import org.onosproject.store.service.DistributedSetAdapter;
91import org.onosproject.store.service.DistributedSetBuilder;
92import org.onosproject.store.service.MapEvent;
93import org.onosproject.store.service.MapEventListener;
94import org.onosproject.store.service.SetEventListener;
Naoki Shiota03c29e12016-05-16 16:58:07 -070095import org.onosproject.store.service.StorageServiceAdapter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070096import org.onosproject.store.service.Versioned;
Naoki Shiota03c29e12016-05-16 16:58:07 -070097
98import java.time.Duration;
99import java.util.ArrayList;
100import java.util.Collection;
101import java.util.Collections;
102import java.util.HashMap;
103import java.util.HashSet;
104import java.util.List;
105import java.util.Map;
106import java.util.Optional;
107import java.util.Set;
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700108import java.util.concurrent.Executor;
109import java.util.function.BiFunction;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700110import java.util.stream.Collectors;
111import java.util.stream.Stream;
112
113import static com.google.common.base.Preconditions.checkState;
114import static org.junit.Assert.assertEquals;
115import static org.junit.Assert.assertNotNull;
116import static org.junit.Assert.assertTrue;
117import static org.onosproject.net.NetTestTools.injectEventDispatcher;
118
119/**
120 * Tests for OpticalPathProvisioner class.
121 */
122public class OpticalPathProvisionerTest {
123
124 private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo");
125
126 // 7-nodes linear topology containing packet/cross-connect/optical links
127 private static final ConnectPoint CP11 = createConnectPoint(1, 1);
128 private static final ConnectPoint CP12 = createConnectPoint(1, 2);
129 private static final ConnectPoint CP21 = createConnectPoint(2, 1);
130 private static final ConnectPoint CP22 = createConnectPoint(2, 2); // cross connect port (packet)
131 private static final ConnectPoint CP31 = createConnectPoint(3, 1); // cross connect port (oductl)
132 private static final ConnectPoint CP32 = createConnectPoint(3, 2);
133 private static final ConnectPoint CP41 = createConnectPoint(4, 1);
134 private static final ConnectPoint CP42 = createConnectPoint(4, 2);
135 private static final ConnectPoint CP51 = createConnectPoint(5, 1);
136 private static final ConnectPoint CP52 = createConnectPoint(5, 2); // cross connect port (oductl)
137 private static final ConnectPoint CP61 = createConnectPoint(6, 1); // cross connect port (packet)
138 private static final ConnectPoint CP62 = createConnectPoint(6, 2);
139 private static final ConnectPoint CP71 = createConnectPoint(7, 1);
140 private static final ConnectPoint CP72 = createConnectPoint(7, 2);
141
142 private static final Link LINK1 = createLink(CP12, CP21, Link.Type.DIRECT);
143 private static final Link LINK2 = createLink(CP22, CP31, Link.Type.OPTICAL); // cross connect link
144 private static final Link LINK3 = createLink(CP32, CP41, Link.Type.OPTICAL);
145 private static final Link LINK4 = createLink(CP42, CP51, Link.Type.OPTICAL);
146 private static final Link LINK5 = createLink(CP52, CP61, Link.Type.OPTICAL); // cross connect link
147 private static final Link LINK6 = createLink(CP62, CP71, Link.Type.DIRECT);
148
149 private static final Device DEVICE1 = createDevice(1, Device.Type.SWITCH);
150 private static final Device DEVICE2 = createDevice(2, Device.Type.SWITCH);
151 private static final Device DEVICE3 = createDevice(3, Device.Type.ROADM);
152 private static final Device DEVICE4 = createDevice(4, Device.Type.ROADM);
153 private static final Device DEVICE5 = createDevice(5, Device.Type.ROADM);
154 private static final Device DEVICE6 = createDevice(6, Device.Type.SWITCH);
155 private static final Device DEVICE7 = createDevice(7, Device.Type.SWITCH);
156
157 private static final Port PORT11 = createPacketPort(DEVICE1, CP11);
158 private static final Port PORT12 = createPacketPort(DEVICE1, CP12);
159 private static final Port PORT21 = createPacketPort(DEVICE2, CP21);
160 private static final Port PORT22 = createOduCltPort(DEVICE2, CP22);
161 private static final Port PORT31 = createOchPort(DEVICE3, CP31);
162 private static final Port PORT32 = createOmsPort(DEVICE3, CP32);
163 private static final Port PORT41 = createOmsPort(DEVICE4, CP41);
164 private static final Port PORT42 = createOmsPort(DEVICE4, CP42);
165 private static final Port PORT51 = createOmsPort(DEVICE5, CP51);
166 private static final Port PORT52 = createOchPort(DEVICE5, CP52);
167 private static final Port PORT61 = createOduCltPort(DEVICE6, CP61);
168 private static final Port PORT62 = createPacketPort(DEVICE6, CP62);
169 private static final Port PORT71 = createPacketPort(DEVICE7, CP71);
170 private static final Port PORT72 = createPacketPort(DEVICE7, CP72);
171
172 protected OpticalPathProvisioner target;
173 protected TestListener listener = new TestListener();
174 protected TestDeviceService deviceService;
175 protected TestLinkService linkService;
176 protected TestPathService pathService;
177 protected TestIntentService intentService;
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700178 protected TestMastershipService mastershipService;
179 protected TestClusterService clusterService;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700180 protected IdGenerator idGenerator;
181
182 @Before
183 public void setUp() {
184 this.deviceService = new TestDeviceService();
185 deviceService.devMap.put(deviceIdOf(1), DEVICE1);
186 deviceService.devMap.put(deviceIdOf(2), DEVICE2);
187 deviceService.devMap.put(deviceIdOf(3), DEVICE3);
188 deviceService.devMap.put(deviceIdOf(4), DEVICE4);
189 deviceService.devMap.put(deviceIdOf(5), DEVICE5);
190 deviceService.devMap.put(deviceIdOf(6), DEVICE6);
191 deviceService.devMap.put(deviceIdOf(7), DEVICE7);
192 deviceService.portMap.put(CP11, PORT11);
193 deviceService.portMap.put(CP12, PORT12);
194 deviceService.portMap.put(CP21, PORT21);
195 deviceService.portMap.put(CP22, PORT22);
196 deviceService.portMap.put(CP31, PORT31);
197 deviceService.portMap.put(CP32, PORT32);
198 deviceService.portMap.put(CP41, PORT41);
199 deviceService.portMap.put(CP42, PORT42);
200 deviceService.portMap.put(CP51, PORT51);
201 deviceService.portMap.put(CP52, PORT52);
202 deviceService.portMap.put(CP61, PORT61);
203 deviceService.portMap.put(CP62, PORT62);
204 deviceService.portMap.put(CP71, PORT71);
205 deviceService.portMap.put(CP72, PORT72);
206
207 this.linkService = new TestLinkService();
208 linkService.links.addAll(Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
209 .collect(Collectors.toList()));
210
211 this.pathService = new TestPathService();
212 this.intentService = new TestIntentService();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700213 this.mastershipService = new TestMastershipService();
214 this.clusterService = new TestClusterService();
215
216 mastershipService.setMastership(DEVICE1.id(), MastershipRole.MASTER);
217 mastershipService.setMastership(DEVICE2.id(), MastershipRole.MASTER);
218 mastershipService.setMastership(DEVICE3.id(), MastershipRole.MASTER);
219 mastershipService.setMastership(DEVICE4.id(), MastershipRole.MASTER);
220 mastershipService.setMastership(DEVICE5.id(), MastershipRole.MASTER);
221 mastershipService.setMastership(DEVICE6.id(), MastershipRole.MASTER);
222 mastershipService.setMastership(DEVICE7.id(), MastershipRole.MASTER);
Naoki Shiota03c29e12016-05-16 16:58:07 -0700223
224 this.target = new OpticalPathProvisioner();
225 target.coreService = new TestCoreService();
226 target.intentService = this.intentService;
227 target.pathService = this.pathService;
228 target.linkService = this.linkService;
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700229 target.mastershipService = this.mastershipService;
230 target.clusterService = this.clusterService;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700231 target.storageService = new TestStorageService();
232 target.deviceService = this.deviceService;
233 target.networkConfigService = new TestNetworkConfigService();
234 target.resourceService = new TestResourceService();
235 injectEventDispatcher(target, new TestEventDispatcher());
236 target.addListener(listener);
237
238 target.activate();
239
240 // To overwrite opticalView-ed deviceService
241 target.deviceService = this.deviceService;
242
243 idGenerator = new IdGenerator() {
244 int counter = 1;
245
246 @Override
247 public long getNewId() {
248 return counter++;
249 }
250 };
251 Intent.bindIdGenerator(idGenerator);
252 }
253
254 @After
255 public void tearDown() {
256 Intent.unbindIdGenerator(idGenerator);
257 target.removeListener(listener);
258 target = null;
259 }
260
261 /**
262 * Checks setupConnectivity method works.
263 */
264 @Test
265 public void testSetupConnectivity() {
266 Bandwidth bandwidth = Bandwidth.bps(100);
267 Duration latency = Duration.ofMillis(10);
268
269 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
270 assertNotNull(cid);
271
272 // Checks path computation is called as expected
273 assertEquals(1, pathService.edges.size());
274 assertEquals(CP12.deviceId(), pathService.edges.get(0).getKey());
275 assertEquals(CP71.deviceId(), pathService.edges.get(0).getValue());
276
277 // Checks intents are installed as expected
278 assertEquals(1, intentService.submitted.size());
279 assertEquals(OpticalConnectivityIntent.class, intentService.submitted.get(0).getClass());
280 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.submitted.get(0);
281 assertEquals(CP31, connIntent.getSrc());
282 assertEquals(CP52, connIntent.getDst());
283 }
284
285 /**
286 * Checks setupPath method works.
287 */
288 @Test
289 public void testSetupPath() {
290 Bandwidth bandwidth = Bandwidth.bps(100);
291 Duration latency = Duration.ofMillis(10);
292 List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
293 .collect(Collectors.toList());
294 Path path = new DefaultPath(PROVIDER_ID, links, 0);
295
296 OpticalConnectivityId cid = target.setupPath(path, bandwidth, latency);
297 assertNotNull(cid);
298
299 // Checks intents are installed as expected
300 assertEquals(1, intentService.submitted.size());
301 assertEquals(OpticalConnectivityIntent.class, intentService.submitted.get(0).getClass());
302 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.submitted.get(0);
303 assertEquals(CP31, connIntent.getSrc());
304 assertEquals(CP52, connIntent.getDst());
305 }
306
307 /**
308 * Checks removeConnectivity method works.
309 */
310 @Test
311 public void testRemoveConnectivity() {
312 Bandwidth bandwidth = Bandwidth.bps(100);
313 Duration latency = Duration.ofMillis(10);
314
315 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
316
317 // Checks intents are withdrawn
318 assertTrue(target.removeConnectivity(cid));
319 assertEquals(1, intentService.withdrawn.size());
320 assertEquals(OpticalConnectivityIntent.class, intentService.withdrawn.get(0).getClass());
321 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.withdrawn.get(0);
322 assertEquals(CP31, connIntent.getSrc());
323 assertEquals(CP52, connIntent.getDst());
324 }
325
326 /**
327 * Checks getPath method works.
328 */
329 @Test
330 public void testGetPath() {
331 Bandwidth bandwidth = Bandwidth.bps(100);
332 Duration latency = Duration.ofMillis(10);
333 List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
334 .collect(Collectors.toList());
335
336 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
337 Optional<List<Link>> path = target.getPath(cid);
338
339 // Checks returned path is as expected
340 assertTrue(path.isPresent());
341 assertEquals(links, path.get());
342 }
343
344 /**
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700345 * Checks if PATH_INSTALLED event comes up after intent whose master is this node is installed.
Naoki Shiota03c29e12016-05-16 16:58:07 -0700346 */
347 @Test
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700348 public void testInstalledEventLocal() {
Naoki Shiota03c29e12016-05-16 16:58:07 -0700349 Bandwidth bandwidth = Bandwidth.bps(100);
350 Duration latency = Duration.ofMillis(10);
351
352 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
353
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700354 // notify all intents are installed
Naoki Shiota03c29e12016-05-16 16:58:07 -0700355 intentService.notifyInstalled();
356
357 assertEquals(1, listener.events.size());
358 assertEquals(OpticalPathEvent.Type.PATH_INSTALLED, listener.events.get(0).type());
359 assertEquals(cid, listener.events.get(0).subject());
360 }
361
362 /**
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700363 * Checks if PATH_INSTALLED event comes up after intent whose master is remote node is installed.
Naoki Shiota03c29e12016-05-16 16:58:07 -0700364 */
365 @Test
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700366 public void testInstalledEventRemote() {
367 // set the master for ingress device of intent to remote node
368 mastershipService.setMastership(DEVICE2.id(), MastershipRole.NONE);
369
Naoki Shiota03c29e12016-05-16 16:58:07 -0700370 Bandwidth bandwidth = Bandwidth.bps(100);
371 Duration latency = Duration.ofMillis(10);
372
373 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
374
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700375 // notify all intents are installed
376 intentService.notifyInstalled();
377
378 // remote nodes must not receive event before distributed map is updated
379 assertEquals(0, listener.events.size());
380 }
381
382 /**
383 * Checks if PATH_REMOVED event comes up after packet link is removed.
384 */
385 @Test
386 public void testRemovedEventLocal() {
387 Bandwidth bandwidth = Bandwidth.bps(100);
388 Duration latency = Duration.ofMillis(10);
389
390 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
391
392 // notify all intents are installed
Naoki Shiota03c29e12016-05-16 16:58:07 -0700393 intentService.notifyInstalled();
394
395 target.removeConnectivity(cid);
396
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700397 // notify all intents are withdrawn
Naoki Shiota03c29e12016-05-16 16:58:07 -0700398 intentService.notifyWithdrawn();
399
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700400 // must have received "INSTALLED" and "REMOVED" events
Naoki Shiota03c29e12016-05-16 16:58:07 -0700401 assertEquals(2, listener.events.size());
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700402 assertEquals(OpticalPathEvent.Type.PATH_INSTALLED, listener.events.get(0).type());
403 assertEquals(cid, listener.events.get(0).subject());
Naoki Shiota03c29e12016-05-16 16:58:07 -0700404 assertEquals(OpticalPathEvent.Type.PATH_REMOVED, listener.events.get(1).type());
405 assertEquals(cid, listener.events.get(1).subject());
406 }
407
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700408
409 /**
410 * Checks if PATH_REMOVED event comes up after packet link is removed.
411 */
412 @Test
413 public void testRemovedEventRemote() {
414 // set the master for ingress device of intent to remote node
415 mastershipService.setMastership(DEVICE2.id(), MastershipRole.NONE);
416
417 Bandwidth bandwidth = Bandwidth.bps(100);
418 Duration latency = Duration.ofMillis(10);
419
420 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
421
422 // notify all intents are installed
423 intentService.notifyInstalled();
424
425 target.removeConnectivity(cid);
426
427 // notify all intents are withdrawn
428 intentService.notifyWithdrawn();
429
430 // remote nodes must not receive event before distributed map is updated
431 assertEquals(0, listener.events.size());
432 }
433
Naoki Shiota03c29e12016-05-16 16:58:07 -0700434 private static ConnectPoint createConnectPoint(long devIdNum, long portIdNum) {
435 return new ConnectPoint(
436 deviceIdOf(devIdNum),
437 PortNumber.portNumber(portIdNum));
438 }
439
440 private static Link createLink(ConnectPoint src, ConnectPoint dst, Link.Type type) {
441 return DefaultLink.builder()
442 .providerId(PROVIDER_ID)
443 .src(src)
444 .dst(dst)
445 .state(Link.State.ACTIVE)
446 .type(type).build();
447 }
448
449 private static Device createDevice(long devIdNum, Device.Type type) {
450 return new DefaultDevice(PROVIDER_ID,
451 deviceIdOf(devIdNum),
452 type,
453 "manufacturer",
454 "hwVersion",
455 "swVersion",
456 "serialNumber",
457 new ChassisId(1));
458 }
459
460 private static Port createPacketPort(Device device, ConnectPoint cp) {
461 return new DefaultPort(device, cp.port(), true);
462 }
463
464 private static Port createOchPort(Device device, ConnectPoint cp) {
465 return new DefaultOchPort(new DefaultPort(device, cp.port(), true),
466 OduSignalType.ODU4,
467 true,
468 OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1));
469 }
470
471 private static Port createOduCltPort(Device device, ConnectPoint cp) {
472 return new DefaultOduCltPort(new DefaultPort(device, cp.port(), true),
473 CltSignalType.CLT_100GBE);
474 }
475
476 private static Port createOmsPort(Device device, ConnectPoint cp) {
477 return new DefaultOmsPort(new DefaultPort(device, cp.port(), true),
478 Frequency.ofKHz(3),
479 Frequency.ofKHz(33),
480 Frequency.ofKHz(2));
481 }
482
483 private static DeviceId deviceIdOf(long devIdNum) {
484 return DeviceId.deviceId(String.format("of:%016d", devIdNum));
485 }
486
487 private static class TestListener implements OpticalPathListener {
488 final List<OpticalPathEvent> events = new ArrayList<>();
489
490 @Override
491 public void event(OpticalPathEvent event) {
492 events.add(event);
493 }
494 }
495
496 private static class TestPathService extends PathServiceAdapter {
497 List<Pair<DeviceId, DeviceId>> edges = new ArrayList<>();
498
499 @Override
500 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
501 if (!(src instanceof DeviceId && dst instanceof DeviceId)) {
502 return Collections.emptySet();
503 }
504
Yuta HIGUCHIfc0529e2016-07-10 00:28:08 -0700505 edges.add(Pair.of((DeviceId) src, (DeviceId) dst));
Naoki Shiota03c29e12016-05-16 16:58:07 -0700506
507 Set<Path> paths = new HashSet<>();
508 List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
509 .collect(Collectors.toList());
510 paths.add(new DefaultPath(PROVIDER_ID, links, 0));
511
512 // returns paths containing single path
513 return paths;
514 }
515
516 }
517
518 private static class TestIntentService extends IntentServiceAdapter {
519 List<Intent> submitted = new ArrayList<>();
520 List<Intent> withdrawn = new ArrayList<>();
521 List<IntentListener> listeners = new ArrayList<>();
522
523 @Override
524 public void submit(Intent intent) {
525 submitted.add(intent);
526 }
527
528 @Override
529 public void withdraw(Intent intent) {
530 withdrawn.add(intent);
531 }
532
533 @Override
534 public void addListener(IntentListener listener) {
535 listeners.add(listener);
536 }
537
538 @Override
539 public Intent getIntent(Key intentKey) {
540 Intent intent = submitted.stream().filter(i -> i.key().equals(intentKey))
541 .findAny()
542 .get();
543 return intent;
544 }
545
546 void notifyInstalled() {
547 submitted.forEach(i -> {
548 IntentEvent event = new IntentEvent(IntentEvent.Type.INSTALLED, i);
549 listeners.forEach(l -> l.event(event));
550 });
551 }
552
553 void notifyWithdrawn() {
554 withdrawn.forEach(i -> {
555 IntentEvent event = new IntentEvent(IntentEvent.Type.WITHDRAWN, i);
556 listeners.forEach(l -> l.event(event));
557 });
558 }
559
560 }
561
562 private static class TestLinkService extends LinkServiceAdapter {
563 List<Link> links = new ArrayList<>();
564
565 @Override
566 public Set<Link> getLinks(ConnectPoint connectPoint) {
567 return links.stream()
568 .filter(l -> l.src().equals(connectPoint) || l.dst().equals(connectPoint))
569 .collect(Collectors.toSet());
570 }
571
572 }
573
574 private static class TestCoreService extends CoreServiceAdapter {
575 @Override
576 public ApplicationId registerApplication(String name) {
577 return new DefaultApplicationId(0, name);
578 }
579 }
580
581 private static class TestMastershipService extends MastershipServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700582 private Map<DeviceId, MastershipRole> mastershipMap = new HashMap<>();
583
584 public void setMastership(DeviceId deviceId, MastershipRole role) {
585 mastershipMap.put(deviceId, role);
586 }
587
588 public void clear() {
589 mastershipMap.clear();
590 }
591
592 @Override
593 public MastershipRole getLocalRole(DeviceId deviceId) {
594 return mastershipMap.get(deviceId);
595 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700596
597 }
598
599 private static class TestClusterService extends ClusterServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700600 private NodeId nodeId;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700601
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700602 public void setLocalNode(String nodeIdStr) {
603 nodeId = NodeId.nodeId(nodeIdStr);
604 }
605
606 @Override
607 public ControllerNode getLocalNode() {
608 return new ControllerNode() {
609 @Override
610 public NodeId id() {
611 return nodeId;
612 }
613
614 @Override
615 public IpAddress ip() {
616 return null;
617 }
618
619 @Override
620 public int tcpPort() {
621 return 0;
622 }
623 };
624 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700625 }
626
627 private static class TestStorageService extends StorageServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700628
629 @Override
630 public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() {
631 ConsistentMapBuilder<K, V> builder = new ConsistentMapBuilder<K, V>() {
632 @Override
633 public AsyncConsistentMap<K, V> buildAsyncMap() {
634 return null;
635 }
636
637 @Override
638 public ConsistentMap<K, V> build() {
Yuta HIGUCHIfc0529e2016-07-10 00:28:08 -0700639 return new TestConsistentMap<>();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700640 }
641 };
642
643 return builder;
644 }
645
646 @Override
647 public <E> DistributedSetBuilder<E> setBuilder() {
648 DistributedSetBuilder<E> builder = new DistributedSetBuilder<E>() {
649 @Override
650 public AsyncDistributedSet<E> build() {
651 return new DistributedSetAdapter<E>() {
652 @Override
653 public DistributedSet<E> asDistributedSet() {
Yuta HIGUCHIfc0529e2016-07-10 00:28:08 -0700654 return new TestDistributedSet<>();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700655 }
656 };
657 }
658 };
659
660 return builder;
661 }
662
Naoki Shiota03c29e12016-05-16 16:58:07 -0700663 @Override
664 public AtomicCounter getAtomicCounter(String name) {
665 return new MockAtomicCounter();
666 }
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700667
668 // Mock ConsistentMap that behaves as a HashMap
669 class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> {
670 private Map<K, Versioned<V>> map = new HashMap<>();
671 private Map<MapEventListener<K, V>, Executor> listeners = new HashMap<>();
672
673 public void notifyListeners(MapEvent<K, V> event) {
674 listeners.forEach((c, e) -> e.execute(() -> c.event(event)));
675 }
676
677 @Override
678 public int size() {
679 return map.size();
680 }
681
682 @Override
683 public Versioned<V> put(K key, V value) {
684 Versioned<V> oldValue = map.get(key);
685 Versioned<V> newValue = new Versioned<>(value, oldValue == null ? 0 : oldValue.version() + 1);
686 map.put(key, newValue);
687 notifyListeners(new MapEvent<>(name(), key, newValue, oldValue));
688 return newValue;
689 }
690
691 @Override
692 public Versioned<V> get(K key) {
693 return map.get(key);
694 }
695
696 @Override
697 public Versioned<V> remove(K key) {
698 Versioned<V> oldValue = map.remove(key);
699 notifyListeners(new MapEvent<>(name(), key, oldValue, null));
700 return oldValue;
701 }
702
703 @Override
704 public Versioned<V> computeIfPresent(K key,
705 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
706 Versioned<V> oldValue = map.get(key);
707 Versioned<V> newValue = new Versioned<>(remappingFunction.apply(key, oldValue.value()),
708 oldValue == null ? 0 : oldValue.version() + 1);
709 map.put(key, newValue);
710 notifyListeners(new MapEvent<>(name(), key, newValue, oldValue));
711 return newValue;
712 }
713
714
715 @Override
716 public Set<Map.Entry<K, Versioned<V>>> entrySet() {
717 return map.entrySet();
718 }
719
720 @Override
721 public Set<K> keySet() {
722 return map.keySet();
723 }
724
725 @Override
726 public Collection<Versioned<V>> values() {
727 return map.values();
728 }
729
730 @Override
731 public void clear() {
732 map.clear();
733 }
734
735 @Override
736 public void addListener(MapEventListener<K, V> listener, Executor executor) {
737 listeners.put(listener, executor);
738 }
739
740 @Override
741 public void removeListener(MapEventListener<K, V> listener) {
742 listeners.remove(listener);
743 }
744 }
745
746 // Mock DistributedSet that behaves as a HashSet
747 class TestDistributedSet<E> extends HashSet<E> implements DistributedSet<E> {
748
749 @Override
750 public void addListener(SetEventListener<E> listener) {
751 }
752
753 @Override
754 public void removeListener(SetEventListener<E> listener) {
755 }
756
757 @Override
758 public String name() {
759 return null;
760 }
761
762 @Override
763 public Type primitiveType() {
764 return null;
765 }
766 }
767
Naoki Shiota03c29e12016-05-16 16:58:07 -0700768 }
769
770 private static class TestDeviceService extends DeviceServiceAdapter {
771 Map<DeviceId, Device> devMap = new HashMap<>();
772 Map<ConnectPoint, Port> portMap = new HashMap<>();
773
774 @Override
775 public Device getDevice(DeviceId deviceId) {
776 return devMap.get(deviceId);
777 }
778
779 @Override
780 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
781 return portMap.get(new ConnectPoint(deviceId, portNumber));
782 }
783 }
784
785 private static class TestNetworkConfigService extends NetworkConfigServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700786 @Override
787 @SuppressWarnings("unchecked")
788 public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
789 if (BandwidthCapacity.class.equals(configClass)) {
790 return (C) new BandwidthCapacity() {
791 @Override
792 public void apply() {
793 // do nothing
794 }
795
796 @Override
797 public BandwidthCapacity capacity(Bandwidth bandwidth) {
798 // do nothing
799 return this;
800 }
801 };
802 }
803 return null;
804 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700805
806 }
807
808 private static class TestResourceService implements ResourceService {
809
810 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700811 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) {
Naoki Shiota03c29e12016-05-16 16:58:07 -0700812 List<ResourceAllocation> allocations = new ArrayList<>();
813
814 resources.forEach(r -> allocations.add(new ResourceAllocation(r, consumer.consumerId())));
815
816 return allocations;
817 }
818
819 @Override
820 public boolean release(List<ResourceAllocation> allocations) {
821 return false;
822 }
823
824 @Override
825 public boolean release(ResourceConsumer consumer) {
826
827 return true;
828 }
829
830 @Override
831 public void addListener(ResourceListener listener) {
832
833 }
834
835 @Override
836 public void removeListener(ResourceListener listener) {
837
838 }
839
840 @Override
841 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
842 return null;
843 }
844
845 @Override
846 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
847 return null;
848 }
849
850 @Override
851 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
852 return null;
853 }
854
855 @Override
856 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
857 return null;
858 }
859
860 @Override
861 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
862 return null;
863 }
864
865 @Override
866 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
867 return null;
868 }
869
870 @Override
871 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
872 return null;
873 }
874
875 @Override
876 public boolean isAvailable(Resource resource) {
877 return true;
878 }
879 }
880
881 private static class MockAtomicCounter implements AtomicCounter {
882 long id = 0;
883
884 @Override
885 public long incrementAndGet() {
886 return ++id;
887 }
888
889 @Override
890 public long getAndIncrement() {
891 return id++;
892 }
893
894 @Override
895 public long getAndAdd(long delta) {
896 long oldId = id;
897 id += delta;
898 return oldId;
899 }
900
901 @Override
902 public long addAndGet(long delta) {
903 id += delta;
904 return id;
905 }
906
907 @Override
908 public void set(long value) {
909 id = value;
910 }
911
912 @Override
913 public boolean compareAndSet(long expectedValue, long updateValue) {
914 if (id == expectedValue) {
915 id = updateValue;
916 return true;
917 } else {
918 return false;
919 }
920 }
921
922 @Override
923 public long get() {
924 return id;
925 }
926
927 @Override
928 public String name() {
929 return "MockAtomicCounter";
930 }
931 }
932
933 // copied from org.onosproject.common.event.impl.TestEventDispatcher
934 /**
935 * Implements event delivery system that delivers events synchronously, or
936 * in-line with the post method invocation.
937 */
938 public class TestEventDispatcher extends DefaultEventSinkRegistry
939 implements EventDeliveryService {
940
941 @Override
942 @SuppressWarnings("unchecked")
943 public synchronized void post(Event event) {
944 EventSink sink = getSink(event.getClass());
945 checkState(sink != null, "No sink for event %s", event);
946 sink.process(event);
947 }
948
949 @Override
950 public void setDispatchTimeLimit(long millis) {
951 }
952
953 @Override
954 public long getDispatchTimeLimit() {
955 return 0;
956 }
957 }
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700958}