blob: 50b0bdbba31b91caec259c5f1440b046fe98a0d9 [file] [log] [blame]
Naoki Shiota03c29e12016-05-16 16:58:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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;
Marc De Leenheer552eeb62017-05-15 17:43:27 -070023import org.onlab.graph.ScalarWeight;
24import org.onlab.osgi.ComponentContextAdapter;
Naoki Shiota03c29e12016-05-16 16:58:07 -070025import org.onlab.packet.ChassisId;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070026import org.onlab.packet.IpAddress;
Naoki Shiota03c29e12016-05-16 16:58:07 -070027import org.onlab.util.Bandwidth;
28import org.onlab.util.Frequency;
29import org.onosproject.cluster.ClusterServiceAdapter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070030import org.onosproject.cluster.ControllerNode;
31import org.onosproject.cluster.NodeId;
Naoki Shiota03c29e12016-05-16 16:58:07 -070032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreServiceAdapter;
34import org.onosproject.core.DefaultApplicationId;
35import org.onosproject.core.IdGenerator;
36import org.onosproject.event.DefaultEventSinkRegistry;
37import org.onosproject.event.Event;
38import org.onosproject.event.EventDeliveryService;
39import org.onosproject.event.EventSink;
40import org.onosproject.mastership.MastershipServiceAdapter;
41import org.onosproject.net.ChannelSpacing;
42import org.onosproject.net.CltSignalType;
43import org.onosproject.net.ConnectPoint;
44import org.onosproject.net.DefaultDevice;
45import org.onosproject.net.DefaultLink;
46import org.onosproject.net.DefaultPath;
47import org.onosproject.net.DefaultPort;
48import org.onosproject.net.Device;
49import org.onosproject.net.DeviceId;
Naoki Shiota03c29e12016-05-16 16:58:07 -070050import org.onosproject.net.Link;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070051import org.onosproject.net.MastershipRole;
Naoki Shiota03c29e12016-05-16 16:58:07 -070052import org.onosproject.net.OchSignal;
53import org.onosproject.net.OduSignalType;
54import org.onosproject.net.Path;
55import org.onosproject.net.Port;
56import org.onosproject.net.PortNumber;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070057import org.onosproject.net.config.Config;
Naoki Shiota03c29e12016-05-16 16:58:07 -070058import org.onosproject.net.config.NetworkConfigServiceAdapter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070059import org.onosproject.net.config.basics.BandwidthCapacity;
Naoki Shiota03c29e12016-05-16 16:58:07 -070060import org.onosproject.net.device.DeviceServiceAdapter;
61import org.onosproject.net.intent.Intent;
62import org.onosproject.net.intent.IntentEvent;
63import org.onosproject.net.intent.IntentListener;
64import org.onosproject.net.intent.IntentServiceAdapter;
65import org.onosproject.net.intent.Key;
66import org.onosproject.net.intent.OpticalConnectivityIntent;
67import org.onosproject.net.link.LinkServiceAdapter;
68import org.onosproject.net.optical.impl.DefaultOchPort;
69import org.onosproject.net.optical.impl.DefaultOduCltPort;
70import org.onosproject.net.optical.impl.DefaultOmsPort;
71import org.onosproject.net.provider.ProviderId;
72import org.onosproject.net.resource.DiscreteResourceId;
73import org.onosproject.net.resource.Resource;
74import org.onosproject.net.resource.ResourceAllocation;
75import org.onosproject.net.resource.ResourceConsumer;
76import org.onosproject.net.resource.ResourceId;
77import org.onosproject.net.resource.ResourceListener;
78import org.onosproject.net.resource.ResourceService;
Marc De Leenheer552eeb62017-05-15 17:43:27 -070079import org.onosproject.net.topology.LinkWeigher;
80import org.onosproject.net.topology.Topology;
81import org.onosproject.net.topology.TopologyServiceAdapter;
Naoki Shiota03c29e12016-05-16 16:58:07 -070082import org.onosproject.newoptical.api.OpticalConnectivityId;
83import org.onosproject.newoptical.api.OpticalPathEvent;
84import org.onosproject.newoptical.api.OpticalPathListener;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070085import org.onosproject.store.service.AsyncConsistentMap;
86import org.onosproject.store.service.AsyncDistributedSet;
Naoki Shiota03c29e12016-05-16 16:58:07 -070087import org.onosproject.store.service.AtomicCounter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070088import org.onosproject.store.service.ConsistentMap;
89import org.onosproject.store.service.ConsistentMapAdapter;
90import org.onosproject.store.service.ConsistentMapBuilder;
91import org.onosproject.store.service.DistributedSet;
92import org.onosproject.store.service.DistributedSetAdapter;
93import org.onosproject.store.service.DistributedSetBuilder;
94import org.onosproject.store.service.MapEvent;
95import org.onosproject.store.service.MapEventListener;
96import org.onosproject.store.service.SetEventListener;
Naoki Shiota03c29e12016-05-16 16:58:07 -070097import org.onosproject.store.service.StorageServiceAdapter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070098import org.onosproject.store.service.Versioned;
Naoki Shiota03c29e12016-05-16 16:58:07 -070099
100import java.time.Duration;
101import java.util.ArrayList;
102import java.util.Collection;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700103import java.util.HashMap;
104import java.util.HashSet;
105import java.util.List;
106import java.util.Map;
107import java.util.Optional;
108import java.util.Set;
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700109import java.util.concurrent.Executor;
110import java.util.function.BiFunction;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700111import java.util.stream.Collectors;
112import java.util.stream.Stream;
113
114import static com.google.common.base.Preconditions.checkState;
115import static org.junit.Assert.assertEquals;
116import static org.junit.Assert.assertNotNull;
117import static org.junit.Assert.assertTrue;
118import static org.onosproject.net.NetTestTools.injectEventDispatcher;
119
120/**
121 * Tests for OpticalPathProvisioner class.
122 */
123public class OpticalPathProvisionerTest {
124
125 private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo");
126
127 // 7-nodes linear topology containing packet/cross-connect/optical links
128 private static final ConnectPoint CP11 = createConnectPoint(1, 1);
129 private static final ConnectPoint CP12 = createConnectPoint(1, 2);
130 private static final ConnectPoint CP21 = createConnectPoint(2, 1);
131 private static final ConnectPoint CP22 = createConnectPoint(2, 2); // cross connect port (packet)
132 private static final ConnectPoint CP31 = createConnectPoint(3, 1); // cross connect port (oductl)
133 private static final ConnectPoint CP32 = createConnectPoint(3, 2);
134 private static final ConnectPoint CP41 = createConnectPoint(4, 1);
135 private static final ConnectPoint CP42 = createConnectPoint(4, 2);
136 private static final ConnectPoint CP51 = createConnectPoint(5, 1);
137 private static final ConnectPoint CP52 = createConnectPoint(5, 2); // cross connect port (oductl)
138 private static final ConnectPoint CP61 = createConnectPoint(6, 1); // cross connect port (packet)
139 private static final ConnectPoint CP62 = createConnectPoint(6, 2);
140 private static final ConnectPoint CP71 = createConnectPoint(7, 1);
141 private static final ConnectPoint CP72 = createConnectPoint(7, 2);
142
143 private static final Link LINK1 = createLink(CP12, CP21, Link.Type.DIRECT);
144 private static final Link LINK2 = createLink(CP22, CP31, Link.Type.OPTICAL); // cross connect link
145 private static final Link LINK3 = createLink(CP32, CP41, Link.Type.OPTICAL);
146 private static final Link LINK4 = createLink(CP42, CP51, Link.Type.OPTICAL);
147 private static final Link LINK5 = createLink(CP52, CP61, Link.Type.OPTICAL); // cross connect link
148 private static final Link LINK6 = createLink(CP62, CP71, Link.Type.DIRECT);
149
150 private static final Device DEVICE1 = createDevice(1, Device.Type.SWITCH);
151 private static final Device DEVICE2 = createDevice(2, Device.Type.SWITCH);
152 private static final Device DEVICE3 = createDevice(3, Device.Type.ROADM);
153 private static final Device DEVICE4 = createDevice(4, Device.Type.ROADM);
154 private static final Device DEVICE5 = createDevice(5, Device.Type.ROADM);
155 private static final Device DEVICE6 = createDevice(6, Device.Type.SWITCH);
156 private static final Device DEVICE7 = createDevice(7, Device.Type.SWITCH);
157
158 private static final Port PORT11 = createPacketPort(DEVICE1, CP11);
159 private static final Port PORT12 = createPacketPort(DEVICE1, CP12);
160 private static final Port PORT21 = createPacketPort(DEVICE2, CP21);
161 private static final Port PORT22 = createOduCltPort(DEVICE2, CP22);
162 private static final Port PORT31 = createOchPort(DEVICE3, CP31);
163 private static final Port PORT32 = createOmsPort(DEVICE3, CP32);
164 private static final Port PORT41 = createOmsPort(DEVICE4, CP41);
165 private static final Port PORT42 = createOmsPort(DEVICE4, CP42);
166 private static final Port PORT51 = createOmsPort(DEVICE5, CP51);
167 private static final Port PORT52 = createOchPort(DEVICE5, CP52);
168 private static final Port PORT61 = createOduCltPort(DEVICE6, CP61);
169 private static final Port PORT62 = createPacketPort(DEVICE6, CP62);
170 private static final Port PORT71 = createPacketPort(DEVICE7, CP71);
171 private static final Port PORT72 = createPacketPort(DEVICE7, CP72);
172
173 protected OpticalPathProvisioner target;
174 protected TestListener listener = new TestListener();
175 protected TestDeviceService deviceService;
176 protected TestLinkService linkService;
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700177 protected TestTopologyService topologyService;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700178 protected TestIntentService intentService;
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700179 protected TestMastershipService mastershipService;
180 protected TestClusterService clusterService;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700181 protected IdGenerator idGenerator;
182
183 @Before
184 public void setUp() {
185 this.deviceService = new TestDeviceService();
186 deviceService.devMap.put(deviceIdOf(1), DEVICE1);
187 deviceService.devMap.put(deviceIdOf(2), DEVICE2);
188 deviceService.devMap.put(deviceIdOf(3), DEVICE3);
189 deviceService.devMap.put(deviceIdOf(4), DEVICE4);
190 deviceService.devMap.put(deviceIdOf(5), DEVICE5);
191 deviceService.devMap.put(deviceIdOf(6), DEVICE6);
192 deviceService.devMap.put(deviceIdOf(7), DEVICE7);
193 deviceService.portMap.put(CP11, PORT11);
194 deviceService.portMap.put(CP12, PORT12);
195 deviceService.portMap.put(CP21, PORT21);
196 deviceService.portMap.put(CP22, PORT22);
197 deviceService.portMap.put(CP31, PORT31);
198 deviceService.portMap.put(CP32, PORT32);
199 deviceService.portMap.put(CP41, PORT41);
200 deviceService.portMap.put(CP42, PORT42);
201 deviceService.portMap.put(CP51, PORT51);
202 deviceService.portMap.put(CP52, PORT52);
203 deviceService.portMap.put(CP61, PORT61);
204 deviceService.portMap.put(CP62, PORT62);
205 deviceService.portMap.put(CP71, PORT71);
206 deviceService.portMap.put(CP72, PORT72);
207
208 this.linkService = new TestLinkService();
209 linkService.links.addAll(Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
210 .collect(Collectors.toList()));
211
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700212 this.topologyService = new TestTopologyService();
Naoki Shiota03c29e12016-05-16 16:58:07 -0700213 this.intentService = new TestIntentService();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700214 this.mastershipService = new TestMastershipService();
215 this.clusterService = new TestClusterService();
216
217 mastershipService.setMastership(DEVICE1.id(), MastershipRole.MASTER);
218 mastershipService.setMastership(DEVICE2.id(), MastershipRole.MASTER);
219 mastershipService.setMastership(DEVICE3.id(), MastershipRole.MASTER);
220 mastershipService.setMastership(DEVICE4.id(), MastershipRole.MASTER);
221 mastershipService.setMastership(DEVICE5.id(), MastershipRole.MASTER);
222 mastershipService.setMastership(DEVICE6.id(), MastershipRole.MASTER);
223 mastershipService.setMastership(DEVICE7.id(), MastershipRole.MASTER);
Naoki Shiota03c29e12016-05-16 16:58:07 -0700224
225 this.target = new OpticalPathProvisioner();
226 target.coreService = new TestCoreService();
227 target.intentService = this.intentService;
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700228 target.topologyService = this.topologyService;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700229 target.linkService = this.linkService;
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700230 target.mastershipService = this.mastershipService;
231 target.clusterService = this.clusterService;
Naoki Shiota03c29e12016-05-16 16:58:07 -0700232 target.storageService = new TestStorageService();
233 target.deviceService = this.deviceService;
234 target.networkConfigService = new TestNetworkConfigService();
235 target.resourceService = new TestResourceService();
236 injectEventDispatcher(target, new TestEventDispatcher());
237 target.addListener(listener);
238
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700239 target.activate(new ComponentContextAdapter());
Naoki Shiota03c29e12016-05-16 16:58:07 -0700240
241 // To overwrite opticalView-ed deviceService
242 target.deviceService = this.deviceService;
243
244 idGenerator = new IdGenerator() {
245 int counter = 1;
246
247 @Override
248 public long getNewId() {
249 return counter++;
250 }
251 };
Thomas Vachuska23235962017-02-03 11:44:15 -0800252 Intent.unbindIdGenerator(idGenerator);
Naoki Shiota03c29e12016-05-16 16:58:07 -0700253 Intent.bindIdGenerator(idGenerator);
254 }
255
256 @After
257 public void tearDown() {
258 Intent.unbindIdGenerator(idGenerator);
259 target.removeListener(listener);
260 target = null;
261 }
262
263 /**
264 * Checks setupConnectivity method works.
265 */
266 @Test
267 public void testSetupConnectivity() {
268 Bandwidth bandwidth = Bandwidth.bps(100);
269 Duration latency = Duration.ofMillis(10);
270
271 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
272 assertNotNull(cid);
273
274 // Checks path computation is called as expected
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700275 assertEquals(1, topologyService.edges.size());
276 assertEquals(CP12.deviceId(), topologyService.edges.get(0).getKey());
277 assertEquals(CP71.deviceId(), topologyService.edges.get(0).getValue());
Naoki Shiota03c29e12016-05-16 16:58:07 -0700278
279 // Checks intents are installed as expected
280 assertEquals(1, intentService.submitted.size());
281 assertEquals(OpticalConnectivityIntent.class, intentService.submitted.get(0).getClass());
282 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.submitted.get(0);
283 assertEquals(CP31, connIntent.getSrc());
284 assertEquals(CP52, connIntent.getDst());
285 }
286
287 /**
288 * Checks setupPath method works.
289 */
290 @Test
291 public void testSetupPath() {
292 Bandwidth bandwidth = Bandwidth.bps(100);
293 Duration latency = Duration.ofMillis(10);
294 List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
295 .collect(Collectors.toList());
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700296 Path path = new DefaultPath(PROVIDER_ID, links, new ScalarWeight(0));
Naoki Shiota03c29e12016-05-16 16:58:07 -0700297
298 OpticalConnectivityId cid = target.setupPath(path, bandwidth, latency);
299 assertNotNull(cid);
300
301 // Checks intents are installed as expected
302 assertEquals(1, intentService.submitted.size());
303 assertEquals(OpticalConnectivityIntent.class, intentService.submitted.get(0).getClass());
304 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.submitted.get(0);
305 assertEquals(CP31, connIntent.getSrc());
306 assertEquals(CP52, connIntent.getDst());
307 }
308
309 /**
310 * Checks removeConnectivity method works.
311 */
312 @Test
313 public void testRemoveConnectivity() {
314 Bandwidth bandwidth = Bandwidth.bps(100);
315 Duration latency = Duration.ofMillis(10);
316
317 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
318
319 // Checks intents are withdrawn
320 assertTrue(target.removeConnectivity(cid));
321 assertEquals(1, intentService.withdrawn.size());
322 assertEquals(OpticalConnectivityIntent.class, intentService.withdrawn.get(0).getClass());
323 OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intentService.withdrawn.get(0);
324 assertEquals(CP31, connIntent.getSrc());
325 assertEquals(CP52, connIntent.getDst());
326 }
327
328 /**
329 * Checks getPath method works.
330 */
331 @Test
332 public void testGetPath() {
333 Bandwidth bandwidth = Bandwidth.bps(100);
334 Duration latency = Duration.ofMillis(10);
335 List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
336 .collect(Collectors.toList());
337
338 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
339 Optional<List<Link>> path = target.getPath(cid);
340
341 // Checks returned path is as expected
342 assertTrue(path.isPresent());
343 assertEquals(links, path.get());
344 }
345
346 /**
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700347 * Checks if PATH_INSTALLED event comes up after intent whose master is this node is installed.
Naoki Shiota03c29e12016-05-16 16:58:07 -0700348 */
349 @Test
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700350 public void testInstalledEventLocal() {
Naoki Shiota03c29e12016-05-16 16:58:07 -0700351 Bandwidth bandwidth = Bandwidth.bps(100);
352 Duration latency = Duration.ofMillis(10);
353
354 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
355
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700356 // notify all intents are installed
Naoki Shiota03c29e12016-05-16 16:58:07 -0700357 intentService.notifyInstalled();
358
359 assertEquals(1, listener.events.size());
360 assertEquals(OpticalPathEvent.Type.PATH_INSTALLED, listener.events.get(0).type());
361 assertEquals(cid, listener.events.get(0).subject());
362 }
363
364 /**
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700365 * Checks if PATH_INSTALLED event comes up after intent whose master is remote node is installed.
Naoki Shiota03c29e12016-05-16 16:58:07 -0700366 */
367 @Test
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700368 public void testInstalledEventRemote() {
369 // set the master for ingress device of intent to remote node
370 mastershipService.setMastership(DEVICE2.id(), MastershipRole.NONE);
371
Naoki Shiota03c29e12016-05-16 16:58:07 -0700372 Bandwidth bandwidth = Bandwidth.bps(100);
373 Duration latency = Duration.ofMillis(10);
374
375 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
376
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700377 // notify all intents are installed
378 intentService.notifyInstalled();
379
380 // remote nodes must not receive event before distributed map is updated
381 assertEquals(0, listener.events.size());
382 }
383
384 /**
385 * Checks if PATH_REMOVED event comes up after packet link is removed.
386 */
387 @Test
388 public void testRemovedEventLocal() {
389 Bandwidth bandwidth = Bandwidth.bps(100);
390 Duration latency = Duration.ofMillis(10);
391
392 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
393
394 // notify all intents are installed
Naoki Shiota03c29e12016-05-16 16:58:07 -0700395 intentService.notifyInstalled();
396
397 target.removeConnectivity(cid);
398
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700399 // notify all intents are withdrawn
Naoki Shiota03c29e12016-05-16 16:58:07 -0700400 intentService.notifyWithdrawn();
401
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700402 // must have received "INSTALLED" and "REMOVED" events
Naoki Shiota03c29e12016-05-16 16:58:07 -0700403 assertEquals(2, listener.events.size());
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700404 assertEquals(OpticalPathEvent.Type.PATH_INSTALLED, listener.events.get(0).type());
405 assertEquals(cid, listener.events.get(0).subject());
Naoki Shiota03c29e12016-05-16 16:58:07 -0700406 assertEquals(OpticalPathEvent.Type.PATH_REMOVED, listener.events.get(1).type());
407 assertEquals(cid, listener.events.get(1).subject());
408 }
409
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700410
411 /**
412 * Checks if PATH_REMOVED event comes up after packet link is removed.
413 */
414 @Test
415 public void testRemovedEventRemote() {
416 // set the master for ingress device of intent to remote node
417 mastershipService.setMastership(DEVICE2.id(), MastershipRole.NONE);
418
419 Bandwidth bandwidth = Bandwidth.bps(100);
420 Duration latency = Duration.ofMillis(10);
421
422 OpticalConnectivityId cid = target.setupConnectivity(CP12, CP71, bandwidth, latency);
423
424 // notify all intents are installed
425 intentService.notifyInstalled();
426
427 target.removeConnectivity(cid);
428
429 // notify all intents are withdrawn
430 intentService.notifyWithdrawn();
431
432 // remote nodes must not receive event before distributed map is updated
433 assertEquals(0, listener.events.size());
434 }
435
Naoki Shiota03c29e12016-05-16 16:58:07 -0700436 private static ConnectPoint createConnectPoint(long devIdNum, long portIdNum) {
437 return new ConnectPoint(
438 deviceIdOf(devIdNum),
439 PortNumber.portNumber(portIdNum));
440 }
441
442 private static Link createLink(ConnectPoint src, ConnectPoint dst, Link.Type type) {
443 return DefaultLink.builder()
444 .providerId(PROVIDER_ID)
445 .src(src)
446 .dst(dst)
447 .state(Link.State.ACTIVE)
448 .type(type).build();
449 }
450
451 private static Device createDevice(long devIdNum, Device.Type type) {
452 return new DefaultDevice(PROVIDER_ID,
453 deviceIdOf(devIdNum),
454 type,
455 "manufacturer",
456 "hwVersion",
457 "swVersion",
458 "serialNumber",
459 new ChassisId(1));
460 }
461
462 private static Port createPacketPort(Device device, ConnectPoint cp) {
463 return new DefaultPort(device, cp.port(), true);
464 }
465
466 private static Port createOchPort(Device device, ConnectPoint cp) {
467 return new DefaultOchPort(new DefaultPort(device, cp.port(), true),
468 OduSignalType.ODU4,
469 true,
470 OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1));
471 }
472
473 private static Port createOduCltPort(Device device, ConnectPoint cp) {
474 return new DefaultOduCltPort(new DefaultPort(device, cp.port(), true),
475 CltSignalType.CLT_100GBE);
476 }
477
478 private static Port createOmsPort(Device device, ConnectPoint cp) {
479 return new DefaultOmsPort(new DefaultPort(device, cp.port(), true),
480 Frequency.ofKHz(3),
481 Frequency.ofKHz(33),
482 Frequency.ofKHz(2));
483 }
484
485 private static DeviceId deviceIdOf(long devIdNum) {
486 return DeviceId.deviceId(String.format("of:%016d", devIdNum));
487 }
488
489 private static class TestListener implements OpticalPathListener {
490 final List<OpticalPathEvent> events = new ArrayList<>();
491
492 @Override
493 public void event(OpticalPathEvent event) {
494 events.add(event);
495 }
496 }
497
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700498 private static class TestTopologyService extends TopologyServiceAdapter {
Naoki Shiota03c29e12016-05-16 16:58:07 -0700499 List<Pair<DeviceId, DeviceId>> edges = new ArrayList<>();
500
501 @Override
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700502 public Stream<Path> getKShortestPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeigher weigher) {
Naoki Shiota03c29e12016-05-16 16:58:07 -0700503 if (!(src instanceof DeviceId && dst instanceof DeviceId)) {
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700504 return Stream.empty();
Naoki Shiota03c29e12016-05-16 16:58:07 -0700505 }
506
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700507 edges.add(Pair.of(src, dst));
Naoki Shiota03c29e12016-05-16 16:58:07 -0700508
509 Set<Path> paths = new HashSet<>();
510 List<Link> links = Stream.of(LINK1, LINK2, LINK3, LINK4, LINK5, LINK6)
511 .collect(Collectors.toList());
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700512 paths.add(new DefaultPath(PROVIDER_ID, links, new ScalarWeight(0)));
Naoki Shiota03c29e12016-05-16 16:58:07 -0700513
514 // returns paths containing single path
Marc De Leenheer552eeb62017-05-15 17:43:27 -0700515 return paths.stream();
Naoki Shiota03c29e12016-05-16 16:58:07 -0700516 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700517 }
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
Jordan Haltermane458f002018-09-18 17:42:05 -0700621 public IpAddress ip(boolean resolve) {
622 return null;
623 }
624
625 @Override
626 public String host() {
627 return null;
628 }
629
630 @Override
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700631 public int tcpPort() {
632 return 0;
633 }
634 };
635 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700636 }
637
638 private static class TestStorageService extends StorageServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700639
640 @Override
641 public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() {
642 ConsistentMapBuilder<K, V> builder = new ConsistentMapBuilder<K, V>() {
643 @Override
644 public AsyncConsistentMap<K, V> buildAsyncMap() {
645 return null;
646 }
647
648 @Override
649 public ConsistentMap<K, V> build() {
Yuta HIGUCHIfc0529e2016-07-10 00:28:08 -0700650 return new TestConsistentMap<>();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700651 }
652 };
653
654 return builder;
655 }
656
657 @Override
658 public <E> DistributedSetBuilder<E> setBuilder() {
659 DistributedSetBuilder<E> builder = new DistributedSetBuilder<E>() {
660 @Override
661 public AsyncDistributedSet<E> build() {
662 return new DistributedSetAdapter<E>() {
663 @Override
664 public DistributedSet<E> asDistributedSet() {
Yuta HIGUCHIfc0529e2016-07-10 00:28:08 -0700665 return new TestDistributedSet<>();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700666 }
667 };
668 }
669 };
670
671 return builder;
672 }
673
Naoki Shiota03c29e12016-05-16 16:58:07 -0700674 @Override
675 public AtomicCounter getAtomicCounter(String name) {
676 return new MockAtomicCounter();
677 }
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700678
679 // Mock ConsistentMap that behaves as a HashMap
680 class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> {
681 private Map<K, Versioned<V>> map = new HashMap<>();
682 private Map<MapEventListener<K, V>, Executor> listeners = new HashMap<>();
683
684 public void notifyListeners(MapEvent<K, V> event) {
685 listeners.forEach((c, e) -> e.execute(() -> c.event(event)));
686 }
687
688 @Override
689 public int size() {
690 return map.size();
691 }
692
693 @Override
694 public Versioned<V> put(K key, V value) {
695 Versioned<V> oldValue = map.get(key);
696 Versioned<V> newValue = new Versioned<>(value, oldValue == null ? 0 : oldValue.version() + 1);
697 map.put(key, newValue);
698 notifyListeners(new MapEvent<>(name(), key, newValue, oldValue));
699 return newValue;
700 }
701
702 @Override
703 public Versioned<V> get(K key) {
704 return map.get(key);
705 }
706
707 @Override
708 public Versioned<V> remove(K key) {
709 Versioned<V> oldValue = map.remove(key);
710 notifyListeners(new MapEvent<>(name(), key, oldValue, null));
711 return oldValue;
712 }
713
714 @Override
715 public Versioned<V> computeIfPresent(K key,
716 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
717 Versioned<V> oldValue = map.get(key);
718 Versioned<V> newValue = new Versioned<>(remappingFunction.apply(key, oldValue.value()),
719 oldValue == null ? 0 : oldValue.version() + 1);
720 map.put(key, newValue);
721 notifyListeners(new MapEvent<>(name(), key, newValue, oldValue));
722 return newValue;
723 }
724
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700725 @Override
726 public Set<Map.Entry<K, Versioned<V>>> entrySet() {
727 return map.entrySet();
728 }
729
730 @Override
731 public Set<K> keySet() {
732 return map.keySet();
733 }
734
735 @Override
736 public Collection<Versioned<V>> values() {
737 return map.values();
738 }
739
740 @Override
741 public void clear() {
742 map.clear();
743 }
744
745 @Override
746 public void addListener(MapEventListener<K, V> listener, Executor executor) {
747 listeners.put(listener, executor);
748 }
749
750 @Override
751 public void removeListener(MapEventListener<K, V> listener) {
752 listeners.remove(listener);
753 }
754 }
755
756 // Mock DistributedSet that behaves as a HashSet
757 class TestDistributedSet<E> extends HashSet<E> implements DistributedSet<E> {
758
759 @Override
760 public void addListener(SetEventListener<E> listener) {
761 }
762
763 @Override
764 public void removeListener(SetEventListener<E> listener) {
765 }
766
767 @Override
768 public String name() {
769 return null;
770 }
771
772 @Override
773 public Type primitiveType() {
774 return null;
775 }
776 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700777 }
778
779 private static class TestDeviceService extends DeviceServiceAdapter {
780 Map<DeviceId, Device> devMap = new HashMap<>();
781 Map<ConnectPoint, Port> portMap = new HashMap<>();
782
783 @Override
784 public Device getDevice(DeviceId deviceId) {
785 return devMap.get(deviceId);
786 }
787
788 @Override
789 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
790 return portMap.get(new ConnectPoint(deviceId, portNumber));
791 }
792 }
793
794 private static class TestNetworkConfigService extends NetworkConfigServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700795 @Override
796 @SuppressWarnings("unchecked")
797 public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
798 if (BandwidthCapacity.class.equals(configClass)) {
799 return (C) new BandwidthCapacity() {
800 @Override
801 public void apply() {
802 // do nothing
803 }
804
805 @Override
806 public BandwidthCapacity capacity(Bandwidth bandwidth) {
807 // do nothing
808 return this;
809 }
810 };
811 }
812 return null;
813 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700814
815 }
816
817 private static class TestResourceService implements ResourceService {
818
819 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700820 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) {
Naoki Shiota03c29e12016-05-16 16:58:07 -0700821 List<ResourceAllocation> allocations = new ArrayList<>();
822
823 resources.forEach(r -> allocations.add(new ResourceAllocation(r, consumer.consumerId())));
824
825 return allocations;
826 }
827
828 @Override
829 public boolean release(List<ResourceAllocation> allocations) {
830 return false;
831 }
832
833 @Override
834 public boolean release(ResourceConsumer consumer) {
835
836 return true;
837 }
838
839 @Override
840 public void addListener(ResourceListener listener) {
841
842 }
843
844 @Override
845 public void removeListener(ResourceListener listener) {
846
847 }
848
849 @Override
850 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
851 return null;
852 }
853
854 @Override
855 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
856 return null;
857 }
858
859 @Override
860 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
861 return null;
862 }
863
864 @Override
865 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
866 return null;
867 }
868
869 @Override
870 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
871 return null;
872 }
873
874 @Override
875 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
876 return null;
877 }
878
879 @Override
880 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
881 return null;
882 }
883
884 @Override
885 public boolean isAvailable(Resource resource) {
886 return true;
887 }
888 }
889
890 private static class MockAtomicCounter implements AtomicCounter {
891 long id = 0;
892
893 @Override
894 public long incrementAndGet() {
895 return ++id;
896 }
897
898 @Override
899 public long getAndIncrement() {
900 return id++;
901 }
902
903 @Override
904 public long getAndAdd(long delta) {
905 long oldId = id;
906 id += delta;
907 return oldId;
908 }
909
910 @Override
911 public long addAndGet(long delta) {
912 id += delta;
913 return id;
914 }
915
916 @Override
917 public void set(long value) {
918 id = value;
919 }
920
921 @Override
922 public boolean compareAndSet(long expectedValue, long updateValue) {
923 if (id == expectedValue) {
924 id = updateValue;
925 return true;
926 } else {
927 return false;
928 }
929 }
930
931 @Override
932 public long get() {
933 return id;
934 }
935
936 @Override
937 public String name() {
938 return "MockAtomicCounter";
939 }
940 }
941
942 // copied from org.onosproject.common.event.impl.TestEventDispatcher
943 /**
944 * Implements event delivery system that delivers events synchronously, or
945 * in-line with the post method invocation.
946 */
947 public class TestEventDispatcher extends DefaultEventSinkRegistry
948 implements EventDeliveryService {
949
950 @Override
951 @SuppressWarnings("unchecked")
952 public synchronized void post(Event event) {
953 EventSink sink = getSink(event.getClass());
954 checkState(sink != null, "No sink for event %s", event);
955 sink.process(event);
956 }
957
958 @Override
959 public void setDispatchTimeLimit(long millis) {
960 }
961
962 @Override
963 public long getDispatchTimeLimit() {
964 return 0;
965 }
966 }
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700967}