blob: d7df71b273b35f556d1f5a5b3ce04333acdfa536 [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
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
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700715 @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 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700767 }
768
769 private static class TestDeviceService extends DeviceServiceAdapter {
770 Map<DeviceId, Device> devMap = new HashMap<>();
771 Map<ConnectPoint, Port> portMap = new HashMap<>();
772
773 @Override
774 public Device getDevice(DeviceId deviceId) {
775 return devMap.get(deviceId);
776 }
777
778 @Override
779 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
780 return portMap.get(new ConnectPoint(deviceId, portNumber));
781 }
782 }
783
784 private static class TestNetworkConfigService extends NetworkConfigServiceAdapter {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700785 @Override
786 @SuppressWarnings("unchecked")
787 public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
788 if (BandwidthCapacity.class.equals(configClass)) {
789 return (C) new BandwidthCapacity() {
790 @Override
791 public void apply() {
792 // do nothing
793 }
794
795 @Override
796 public BandwidthCapacity capacity(Bandwidth bandwidth) {
797 // do nothing
798 return this;
799 }
800 };
801 }
802 return null;
803 }
Naoki Shiota03c29e12016-05-16 16:58:07 -0700804
805 }
806
807 private static class TestResourceService implements ResourceService {
808
809 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700810 public List<ResourceAllocation> allocate(ResourceConsumer consumer, List<? extends Resource> resources) {
Naoki Shiota03c29e12016-05-16 16:58:07 -0700811 List<ResourceAllocation> allocations = new ArrayList<>();
812
813 resources.forEach(r -> allocations.add(new ResourceAllocation(r, consumer.consumerId())));
814
815 return allocations;
816 }
817
818 @Override
819 public boolean release(List<ResourceAllocation> allocations) {
820 return false;
821 }
822
823 @Override
824 public boolean release(ResourceConsumer consumer) {
825
826 return true;
827 }
828
829 @Override
830 public void addListener(ResourceListener listener) {
831
832 }
833
834 @Override
835 public void removeListener(ResourceListener listener) {
836
837 }
838
839 @Override
840 public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
841 return null;
842 }
843
844 @Override
845 public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
846 return null;
847 }
848
849 @Override
850 public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
851 return null;
852 }
853
854 @Override
855 public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
856 return null;
857 }
858
859 @Override
860 public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
861 return null;
862 }
863
864 @Override
865 public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
866 return null;
867 }
868
869 @Override
870 public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
871 return null;
872 }
873
874 @Override
875 public boolean isAvailable(Resource resource) {
876 return true;
877 }
878 }
879
880 private static class MockAtomicCounter implements AtomicCounter {
881 long id = 0;
882
883 @Override
884 public long incrementAndGet() {
885 return ++id;
886 }
887
888 @Override
889 public long getAndIncrement() {
890 return id++;
891 }
892
893 @Override
894 public long getAndAdd(long delta) {
895 long oldId = id;
896 id += delta;
897 return oldId;
898 }
899
900 @Override
901 public long addAndGet(long delta) {
902 id += delta;
903 return id;
904 }
905
906 @Override
907 public void set(long value) {
908 id = value;
909 }
910
911 @Override
912 public boolean compareAndSet(long expectedValue, long updateValue) {
913 if (id == expectedValue) {
914 id = updateValue;
915 return true;
916 } else {
917 return false;
918 }
919 }
920
921 @Override
922 public long get() {
923 return id;
924 }
925
926 @Override
927 public String name() {
928 return "MockAtomicCounter";
929 }
930 }
931
932 // copied from org.onosproject.common.event.impl.TestEventDispatcher
933 /**
934 * Implements event delivery system that delivers events synchronously, or
935 * in-line with the post method invocation.
936 */
937 public class TestEventDispatcher extends DefaultEventSinkRegistry
938 implements EventDeliveryService {
939
940 @Override
941 @SuppressWarnings("unchecked")
942 public synchronized void post(Event event) {
943 EventSink sink = getSink(event.getClass());
944 checkState(sink != null, "No sink for event %s", event);
945 sink.process(event);
946 }
947
948 @Override
949 public void setDispatchTimeLimit(long millis) {
950 }
951
952 @Override
953 public long getDispatchTimeLimit() {
954 return 0;
955 }
956 }
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700957}