blob: 78a58b592891d1797ee3c49ac34251f37281c18d [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 };
Thomas Vachuska23235962017-02-03 11:44:15 -0800251 Intent.unbindIdGenerator(idGenerator);
Naoki Shiota03c29e12016-05-16 16:58:07 -0700252 Intent.bindIdGenerator(idGenerator);
253 }
254
255 @After
256 public void tearDown() {
257 Intent.unbindIdGenerator(idGenerator);
258 target.removeListener(listener);
259 target = null;
260 }
261
262 /**
263 * Checks setupConnectivity method works.
264 */
265 @Test
266 public void testSetupConnectivity() {
267 Bandwidth bandwidth = Bandwidth.bps(100);
268 Duration latency = Duration.ofMillis(10);
269
270 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
271 assertNotNull(cid);
272
273 // Checks path computation is called as expected
274 assertEquals(1, pathService.edges.size());
275 assertEquals(CP12.deviceId(), pathService.edges.get(0).getKey());
276 assertEquals(CP71.deviceId(), pathService.edges.get(0).getValue());
277
278 // Checks intents are installed as expected
279 assertEquals(1, intentService.submitted.size());
280 assertEquals(OpticalConnectivityIntent.class, intentService.submitted.get(0).getClass());
281 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.submitted.get(0);
282 assertEquals(CP31, connIntent.getSrc());
283 assertEquals(CP52, connIntent.getDst());
284 }
285
286 /**
287 * Checks setupPath method works.
288 */
289 @Test
290 public void testSetupPath() {
291 Bandwidth bandwidth = Bandwidth.bps(100);
292 Duration latency = Duration.ofMillis(10);
293 List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
294 .collect(Collectors.toList());
295 Path path = new DefaultPath(PROVIDER_ID, links, 0);
296
297 OpticalConnectivityId cid = target.setupPath(path, bandwidth, latency);
298 assertNotNull(cid);
299
300 // Checks intents are installed as expected
301 assertEquals(1, intentService.submitted.size());
302 assertEquals(OpticalConnectivityIntent.class, intentService.submitted.get(0).getClass());
303 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.submitted.get(0);
304 assertEquals(CP31, connIntent.getSrc());
305 assertEquals(CP52, connIntent.getDst());
306 }
307
308 /**
309 * Checks removeConnectivity method works.
310 */
311 @Test
312 public void testRemoveConnectivity() {
313 Bandwidth bandwidth = Bandwidth.bps(100);
314 Duration latency = Duration.ofMillis(10);
315
316 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
317
318 // Checks intents are withdrawn
319 assertTrue(target.removeConnectivity(cid));
320 assertEquals(1, intentService.withdrawn.size());
321 assertEquals(OpticalConnectivityIntent.class, intentService.withdrawn.get(0).getClass());
322 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.withdrawn.get(0);
323 assertEquals(CP31, connIntent.getSrc());
324 assertEquals(CP52, connIntent.getDst());
325 }
326
327 /**
328 * Checks getPath method works.
329 */
330 @Test
331 public void testGetPath() {
332 Bandwidth bandwidth = Bandwidth.bps(100);
333 Duration latency = Duration.ofMillis(10);
334 List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
335 .collect(Collectors.toList());
336
337 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
338 Optional<List<Link>> path = target.getPath(cid);
339
340 // Checks returned path is as expected
341 assertTrue(path.isPresent());
342 assertEquals(links, path.get());
343 }
344
345 /**
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700346 * Checks if PATH_INSTALLED event comes up after intent whose master is this node is installed.
Naoki Shiota03c29e12016-05-16 16:58:07 -0700347 */
348 @Test
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700349 public void testInstalledEventLocal() {
Naoki Shiota03c29e12016-05-16 16:58:07 -0700350 Bandwidth bandwidth = Bandwidth.bps(100);
351 Duration latency = Duration.ofMillis(10);
352
353 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
354
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700355 // notify all intents are installed
Naoki Shiota03c29e12016-05-16 16:58:07 -0700356 intentService.notifyInstalled();
357
358 assertEquals(1, listener.events.size());
359 assertEquals(OpticalPathEvent.Type.PATH_INSTALLED, listener.events.get(0).type());
360 assertEquals(cid, listener.events.get(0).subject());
361 }
362
363 /**
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700364 * Checks if PATH_INSTALLED event comes up after intent whose master is remote node is installed.
Naoki Shiota03c29e12016-05-16 16:58:07 -0700365 */
366 @Test
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700367 public void testInstalledEventRemote() {
368 // set the master for ingress device of intent to remote node
369 mastershipService.setMastership(DEVICE2.id(), MastershipRole.NONE);
370
Naoki Shiota03c29e12016-05-16 16:58:07 -0700371 Bandwidth bandwidth = Bandwidth.bps(100);
372 Duration latency = Duration.ofMillis(10);
373
374 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
375
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700376 // notify all intents are installed
377 intentService.notifyInstalled();
378
379 // remote nodes must not receive event before distributed map is updated
380 assertEquals(0, listener.events.size());
381 }
382
383 /**
384 * Checks if PATH_REMOVED event comes up after packet link is removed.
385 */
386 @Test
387 public void testRemovedEventLocal() {
388 Bandwidth bandwidth = Bandwidth.bps(100);
389 Duration latency = Duration.ofMillis(10);
390
391 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
392
393 // notify all intents are installed
Naoki Shiota03c29e12016-05-16 16:58:07 -0700394 intentService.notifyInstalled();
395
396 target.removeConnectivity(cid);
397
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700398 // notify all intents are withdrawn
Naoki Shiota03c29e12016-05-16 16:58:07 -0700399 intentService.notifyWithdrawn();
400
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700401 // must have received "INSTALLED" and "REMOVED" events
Naoki Shiota03c29e12016-05-16 16:58:07 -0700402 assertEquals(2, listener.events.size());
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700403 assertEquals(OpticalPathEvent.Type.PATH_INSTALLED, listener.events.get(0).type());
404 assertEquals(cid, listener.events.get(0).subject());
Naoki Shiota03c29e12016-05-16 16:58:07 -0700405 assertEquals(OpticalPathEvent.Type.PATH_REMOVED, listener.events.get(1).type());
406 assertEquals(cid, listener.events.get(1).subject());
407 }
408
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700409
410 /**
411 * Checks if PATH_REMOVED event comes up after packet link is removed.
412 */
413 @Test
414 public void testRemovedEventRemote() {
415 // set the master for ingress device of intent to remote node
416 mastershipService.setMastership(DEVICE2.id(), MastershipRole.NONE);
417
418 Bandwidth bandwidth = Bandwidth.bps(100);
419 Duration latency = Duration.ofMillis(10);
420
421 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
422
423 // notify all intents are installed
424 intentService.notifyInstalled();
425
426 target.removeConnectivity(cid);
427
428 // notify all intents are withdrawn
429 intentService.notifyWithdrawn();
430
431 // remote nodes must not receive event before distributed map is updated
432 assertEquals(0, listener.events.size());
433 }
434
Naoki Shiota03c29e12016-05-16 16:58:07 -0700435 private static ConnectPoint createConnectPoint(long devIdNum, long portIdNum) {
436 return new ConnectPoint(
437 deviceIdOf(devIdNum),
438 PortNumber.portNumber(portIdNum));
439 }
440
441 private static Link createLink(ConnectPoint src, ConnectPoint dst, Link.Type type) {
442 return DefaultLink.builder()
443 .providerId(PROVIDER_ID)
444 .src(src)
445 .dst(dst)
446 .state(Link.State.ACTIVE)
447 .type(type).build();
448 }
449
450 private static Device createDevice(long devIdNum, Device.Type type) {
451 return new DefaultDevice(PROVIDER_ID,
452 deviceIdOf(devIdNum),
453 type,
454 "manufacturer",
455 "hwVersion",
456 "swVersion",
457 "serialNumber",
458 new ChassisId(1));
459 }
460
461 private static Port createPacketPort(Device device, ConnectPoint cp) {
462 return new DefaultPort(device, cp.port(), true);
463 }
464
465 private static Port createOchPort(Device device, ConnectPoint cp) {
466 return new DefaultOchPort(new DefaultPort(device, cp.port(), true),
467 OduSignalType.ODU4,
468 true,
469 OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1));
470 }
471
472 private static Port createOduCltPort(Device device, ConnectPoint cp) {
473 return new DefaultOduCltPort(new DefaultPort(device, cp.port(), true),
474 CltSignalType.CLT_100GBE);
475 }
476
477 private static Port createOmsPort(Device device, ConnectPoint cp) {
478 return new DefaultOmsPort(new DefaultPort(device, cp.port(), true),
479 Frequency.ofKHz(3),
480 Frequency.ofKHz(33),
481 Frequency.ofKHz(2));
482 }
483
484 private static DeviceId deviceIdOf(long devIdNum) {
485 return DeviceId.deviceId(String.format("of:%016d", devIdNum));
486 }
487
488 private static class TestListener implements OpticalPathListener {
489 final List<OpticalPathEvent> events = new ArrayList<>();
490
491 @Override
492 public void event(OpticalPathEvent event) {
493 events.add(event);
494 }
495 }
496
497 private static class TestPathService extends PathServiceAdapter {
498 List<Pair<DeviceId, DeviceId>> edges = new ArrayList<>();
499
500 @Override
501 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
502 if (!(src instanceof DeviceId && dst instanceof DeviceId)) {
503 return Collections.emptySet();
504 }
505
Yuta HIGUCHIfc0529e2016-07-10 00:28:08 -0700506 edges.add(Pair.of((DeviceId) src, (DeviceId) dst));
Naoki Shiota03c29e12016-05-16 16:58:07 -0700507
508 Set<Path> paths = new HashSet<>();
509 List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
510 .collect(Collectors.toList());
511 paths.add(new DefaultPath(PROVIDER_ID, links, 0));
512
513 // returns paths containing single path
514 return paths;
515 }
516
517 }
518
519 private static class TestIntentService extends IntentServiceAdapter {
520 List<Intent> submitted = new ArrayList<>();
521 List<Intent> withdrawn = new ArrayList<>();
522 List<IntentListener> listeners = new ArrayList<>();
523
524 @Override
525 public void submit(Intent intent) {
526 submitted.add(intent);
527 }
528
529 @Override
530 public void withdraw(Intent intent) {
531 withdrawn.add(intent);
532 }
533
534 @Override
535 public void addListener(IntentListener listener) {
536 listeners.add(listener);
537 }
538
539 @Override
540 public Intent getIntent(Key intentKey) {
541 Intent intent = submitted.stream().filter(i -> i.key().equals(intentKey))
542 .findAny()
543 .get();
544 return intent;
545 }
546
547 void notifyInstalled() {
548 submitted.forEach(i -> {
549 IntentEvent event = new IntentEvent(IntentEvent.Type.INSTALLED, i);
550 listeners.forEach(l -> l.event(event));
551 });
552 }
553
554 void notifyWithdrawn() {
555 withdrawn.forEach(i -> {
556 IntentEvent event = new IntentEvent(IntentEvent.Type.WITHDRAWN, i);
557 listeners.forEach(l -> l.event(event));
558 });
559 }
560
561 }
562
563 private static class TestLinkService extends LinkServiceAdapter {
564 List<Link> links = new ArrayList<>();
565
566 @Override
567 public Set<Link> getLinks(ConnectPoint connectPoint) {
568 return links.stream()
569 .filter(l -> l.src().equals(connectPoint) || l.dst().equals(connectPoint))
570 .collect(Collectors.toSet());
571 }
572
573 }
574
575 private static class TestCoreService extends CoreServiceAdapter {
576 @Override
577 public ApplicationId registerApplication(String name) {
578 return new DefaultApplicationId(0, name);
579 }
580 }
581
582 private static class TestMastershipService extends MastershipServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700583 private Map<DeviceId, MastershipRole> mastershipMap = new HashMap<>();
584
585 public void setMastership(DeviceId deviceId, MastershipRole role) {
586 mastershipMap.put(deviceId, role);
587 }
588
589 public void clear() {
590 mastershipMap.clear();
591 }
592
593 @Override
594 public MastershipRole getLocalRole(DeviceId deviceId) {
595 return mastershipMap.get(deviceId);
596 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700597
598 }
599
600 private static class TestClusterService extends ClusterServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700601 private NodeId nodeId;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700602
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700603 public void setLocalNode(String nodeIdStr) {
604 nodeId = NodeId.nodeId(nodeIdStr);
605 }
606
607 @Override
608 public ControllerNode getLocalNode() {
609 return new ControllerNode() {
610 @Override
611 public NodeId id() {
612 return nodeId;
613 }
614
615 @Override
616 public IpAddress ip() {
617 return null;
618 }
619
620 @Override
621 public int tcpPort() {
622 return 0;
623 }
624 };
625 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700626 }
627
628 private static class TestStorageService extends StorageServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700629
630 @Override
631 public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() {
632 ConsistentMapBuilder<K, V> builder = new ConsistentMapBuilder<K, V>() {
633 @Override
634 public AsyncConsistentMap<K, V> buildAsyncMap() {
635 return null;
636 }
637
638 @Override
639 public ConsistentMap<K, V> build() {
Yuta HIGUCHIfc0529e2016-07-10 00:28:08 -0700640 return new TestConsistentMap<>();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700641 }
642 };
643
644 return builder;
645 }
646
647 @Override
648 public <E> DistributedSetBuilder<E> setBuilder() {
649 DistributedSetBuilder<E> builder = new DistributedSetBuilder<E>() {
650 @Override
651 public AsyncDistributedSet<E> build() {
652 return new DistributedSetAdapter<E>() {
653 @Override
654 public DistributedSet<E> asDistributedSet() {
Yuta HIGUCHIfc0529e2016-07-10 00:28:08 -0700655 return new TestDistributedSet<>();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700656 }
657 };
658 }
659 };
660
661 return builder;
662 }
663
Naoki Shiota03c29e12016-05-16 16:58:07 -0700664 @Override
665 public AtomicCounter getAtomicCounter(String name) {
666 return new MockAtomicCounter();
667 }
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700668
669 // Mock ConsistentMap that behaves as a HashMap
670 class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> {
671 private Map<K, Versioned<V>> map = new HashMap<>();
672 private Map<MapEventListener<K, V>, Executor> listeners = new HashMap<>();
673
674 public void notifyListeners(MapEvent<K, V> event) {
675 listeners.forEach((c, e) -> e.execute(() -> c.event(event)));
676 }
677
678 @Override
679 public int size() {
680 return map.size();
681 }
682
683 @Override
684 public Versioned<V> put(K key, V value) {
685 Versioned<V> oldValue = map.get(key);
686 Versioned<V> newValue = new Versioned<>(value, oldValue == null ? 0 : oldValue.version() + 1);
687 map.put(key, newValue);
688 notifyListeners(new MapEvent<>(name(), key, newValue, oldValue));
689 return newValue;
690 }
691
692 @Override
693 public Versioned<V> get(K key) {
694 return map.get(key);
695 }
696
697 @Override
698 public Versioned<V> remove(K key) {
699 Versioned<V> oldValue = map.remove(key);
700 notifyListeners(new MapEvent<>(name(), key, oldValue, null));
701 return oldValue;
702 }
703
704 @Override
705 public Versioned<V> computeIfPresent(K key,
706 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
707 Versioned<V> oldValue = map.get(key);
708 Versioned<V> newValue = new Versioned<>(remappingFunction.apply(key, oldValue.value()),
709 oldValue == null ? 0 : oldValue.version() + 1);
710 map.put(key, newValue);
711 notifyListeners(new MapEvent<>(name(), key, newValue, oldValue));
712 return newValue;
713 }
714
715
716 @Override
717 public Set<Map.Entry<K, Versioned<V>>> entrySet() {
718 return map.entrySet();
719 }
720
721 @Override
722 public Set<K> keySet() {
723 return map.keySet();
724 }
725
726 @Override
727 public Collection<Versioned<V>> values() {
728 return map.values();
729 }
730
731 @Override
732 public void clear() {
733 map.clear();
734 }
735
736 @Override
737 public void addListener(MapEventListener<K, V> listener, Executor executor) {
738 listeners.put(listener, executor);
739 }
740
741 @Override
742 public void removeListener(MapEventListener<K, V> listener) {
743 listeners.remove(listener);
744 }
745 }
746
747 // Mock DistributedSet that behaves as a HashSet
748 class TestDistributedSet<E> extends HashSet<E> implements DistributedSet<E> {
749
750 @Override
751 public void addListener(SetEventListener<E> listener) {
752 }
753
754 @Override
755 public void removeListener(SetEventListener<E> listener) {
756 }
757
758 @Override
759 public String name() {
760 return null;
761 }
762
763 @Override
764 public Type primitiveType() {
765 return null;
766 }
767 }
768
Naoki Shiota03c29e12016-05-16 16:58:07 -0700769 }
770
771 private static class TestDeviceService extends DeviceServiceAdapter {
772 Map<DeviceId, Device> devMap = new HashMap<>();
773 Map<ConnectPoint, Port> portMap = new HashMap<>();
774
775 @Override
776 public Device getDevice(DeviceId deviceId) {
777 return devMap.get(deviceId);
778 }
779
780 @Override
781 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
782 return portMap.get(new ConnectPoint(deviceId, portNumber));
783 }
784 }
785
786 private static class TestNetworkConfigService extends NetworkConfigServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700787 @Override
788 @SuppressWarnings("unchecked")
789 public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
790 if (BandwidthCapacity.class.equals(configClass)) {
791 return (C) new BandwidthCapacity() {
792 @Override
793 public void apply() {
794 // do nothing
795 }
796
797 @Override
798 public BandwidthCapacity capacity(Bandwidth bandwidth) {
799 // do nothing
800 return this;
801 }
802 };
803 }
804 return null;
805 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700806
807 }
808
809 private static class TestResourceService implements ResourceService {
810
811 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700812 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) {
Naoki Shiota03c29e12016-05-16 16:58:07 -0700813 List<ResourceAllocation> allocations = new ArrayList<>();
814
815 resources.forEach(r -> allocations.add(new ResourceAllocation(r, consumer.consumerId())));
816
817 return allocations;
818 }
819
820 @Override
821 public boolean release(List<ResourceAllocation> allocations) {
822 return false;
823 }
824
825 @Override
826 public boolean release(ResourceConsumer consumer) {
827
828 return true;
829 }
830
831 @Override
832 public void addListener(ResourceListener listener) {
833
834 }
835
836 @Override
837 public void removeListener(ResourceListener listener) {
838
839 }
840
841 @Override
842 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
843 return null;
844 }
845
846 @Override
847 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
848 return null;
849 }
850
851 @Override
852 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
853 return null;
854 }
855
856 @Override
857 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
858 return null;
859 }
860
861 @Override
862 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
863 return null;
864 }
865
866 @Override
867 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
868 return null;
869 }
870
871 @Override
872 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
873 return null;
874 }
875
876 @Override
877 public boolean isAvailable(Resource resource) {
878 return true;
879 }
880 }
881
882 private static class MockAtomicCounter implements AtomicCounter {
883 long id = 0;
884
885 @Override
886 public long incrementAndGet() {
887 return ++id;
888 }
889
890 @Override
891 public long getAndIncrement() {
892 return id++;
893 }
894
895 @Override
896 public long getAndAdd(long delta) {
897 long oldId = id;
898 id += delta;
899 return oldId;
900 }
901
902 @Override
903 public long addAndGet(long delta) {
904 id += delta;
905 return id;
906 }
907
908 @Override
909 public void set(long value) {
910 id = value;
911 }
912
913 @Override
914 public boolean compareAndSet(long expectedValue, long updateValue) {
915 if (id == expectedValue) {
916 id = updateValue;
917 return true;
918 } else {
919 return false;
920 }
921 }
922
923 @Override
924 public long get() {
925 return id;
926 }
927
928 @Override
929 public String name() {
930 return "MockAtomicCounter";
931 }
932 }
933
934 // copied from org.onosproject.common.event.impl.TestEventDispatcher
935 /**
936 * Implements event delivery system that delivers events synchronously, or
937 * in-line with the post method invocation.
938 */
939 public class TestEventDispatcher extends DefaultEventSinkRegistry
940 implements EventDeliveryService {
941
942 @Override
943 @SuppressWarnings("unchecked")
944 public synchronized void post(Event event) {
945 EventSink sink = getSink(event.getClass());
946 checkState(sink != null, "No sink for event %s", event);
947 sink.process(event);
948 }
949
950 @Override
951 public void setDispatchTimeLimit(long millis) {
952 }
953
954 @Override
955 public long getDispatchTimeLimit() {
956 return 0;
957 }
958 }
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700959}