blob: b84565a7b14d67002a98c7372f0ae65a17f2cead [file] [log] [blame]
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -08001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.link.impl;
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080017
18import com.google.common.collect.Iterables;
Madan Jampani2af244a2015-02-22 13:12:01 -080019
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080020import org.junit.After;
21import org.junit.AfterClass;
22import org.junit.Before;
23import org.junit.BeforeClass;
24import org.junit.Ignore;
25import org.junit.Test;
Jonathan Hart7d656f42015-01-27 14:07:23 -080026import org.onlab.packet.IpAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.cluster.ControllerNode;
28import org.onosproject.cluster.DefaultControllerNode;
29import org.onosproject.cluster.NodeId;
Marc De Leenheerb473b9d2015-02-06 15:21:03 -080030import org.onosproject.mastership.MastershipServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.DefaultAnnotations;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.Link;
35import org.onosproject.net.Link.Type;
36import org.onosproject.net.LinkKey;
37import org.onosproject.net.PortNumber;
38import org.onosproject.net.SparseAnnotations;
39import org.onosproject.net.device.DeviceClockService;
Madan Jampani565a66a2015-07-25 17:01:13 -070040import org.onosproject.net.device.DeviceClockServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.link.DefaultLinkDescription;
42import org.onosproject.net.link.LinkDescription;
43import org.onosproject.net.link.LinkEvent;
44import org.onosproject.net.link.LinkStore;
45import org.onosproject.net.link.LinkStoreDelegate;
46import org.onosproject.net.provider.ProviderId;
Madan Jampani565a66a2015-07-25 17:01:13 -070047import org.onosproject.store.Timestamp;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.store.cluster.StaticClusterService;
49import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.store.cluster.messaging.ClusterMessageHandler;
51import org.onosproject.store.cluster.messaging.MessageSubject;
Madan Jampani565a66a2015-07-25 17:01:13 -070052import org.onosproject.store.impl.MastershipBasedTimestamp;
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080053
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080054import java.util.HashMap;
55import java.util.Map;
56import java.util.Set;
57import java.util.concurrent.CountDownLatch;
Madan Jampani2af244a2015-02-22 13:12:01 -080058import java.util.concurrent.ExecutorService;
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080059import java.util.concurrent.TimeUnit;
Madan Jampani565a66a2015-07-25 17:01:13 -070060import java.util.concurrent.atomic.AtomicLong;
Jonathan Hart7d656f42015-01-27 14:07:23 -080061import static org.easymock.EasyMock.*;
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080062import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import static org.onosproject.cluster.ControllerNode.State.ACTIVE;
64import static org.onosproject.net.DeviceId.deviceId;
Jonathan Hart7d656f42015-01-27 14:07:23 -080065import static org.onosproject.net.Link.Type.DIRECT;
66import static org.onosproject.net.Link.Type.EDGE;
67import static org.onosproject.net.Link.Type.INDIRECT;
Brian O'Connorabafb502014-12-02 22:26:20 -080068import static org.onosproject.net.NetTestTools.assertAnnotationsEquals;
Jonathan Hart7d656f42015-01-27 14:07:23 -080069import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
70import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
71import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED;
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080072
73/**
74 * Test of the GossipLinkStoreTest implementation.
75 */
Madan Jampani78be2492016-06-03 23:27:07 -070076@Ignore
77public class ECLinkStoreTest {
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -080078
79 private static final ProviderId PID = new ProviderId("of", "foo");
80 private static final ProviderId PIDA = new ProviderId("of", "bar", true);
81 private static final DeviceId DID1 = deviceId("of:foo");
82 private static final DeviceId DID2 = deviceId("of:bar");
83
84 private static final PortNumber P1 = PortNumber.portNumber(1);
85 private static final PortNumber P2 = PortNumber.portNumber(2);
86 private static final PortNumber P3 = PortNumber.portNumber(3);
87
88 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
89 .set("A1", "a1")
90 .set("B1", "b1")
91 .build();
92 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
93 .remove("A1")
94 .set("B3", "b3")
95 .build();
96 private static final SparseAnnotations A2 = DefaultAnnotations.builder()
97 .set("A2", "a2")
98 .set("B2", "b2")
99 .build();
100 private static final SparseAnnotations A2_2 = DefaultAnnotations.builder()
101 .remove("A2")
102 .set("B4", "b4")
103 .build();
104
105 // local node
106 private static final NodeId NID1 = new NodeId("local");
107 private static final ControllerNode ONOS1 =
108 new DefaultControllerNode(NID1, IpAddress.valueOf("127.0.0.1"));
109
110 // remote node
111 private static final NodeId NID2 = new NodeId("remote");
112 private static final ControllerNode ONOS2 =
113 new DefaultControllerNode(NID2, IpAddress.valueOf("127.0.0.2"));
114
Madan Jampani78be2492016-06-03 23:27:07 -0700115 private ECLinkStore linkStoreImpl;
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800116 private LinkStore linkStore;
117
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800118 private DeviceClockService deviceClockService;
119 private ClusterCommunicationService clusterCommunicator;
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800120
121 @BeforeClass
122 public static void setUpBeforeClass() throws Exception {
123 }
124
125 @AfterClass
126 public static void tearDownAfterClass() throws Exception {
127 }
128
129 @Before
130 public void setUp() throws Exception {
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800131 // TODO mock clusterCommunicator
132 clusterCommunicator = createNiceMock(ClusterCommunicationService.class);
133 clusterCommunicator.addSubscriber(anyObject(MessageSubject.class),
Madan Jampani2af244a2015-02-22 13:12:01 -0800134 anyObject(ClusterMessageHandler.class),
135 anyObject(ExecutorService.class));
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800136 expectLastCall().anyTimes();
137 replay(clusterCommunicator);
138
Madan Jampani78be2492016-06-03 23:27:07 -0700139 linkStoreImpl = new ECLinkStore();
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800140 linkStoreImpl.deviceClockService = deviceClockService;
141 linkStoreImpl.clusterCommunicator = clusterCommunicator;
142 linkStoreImpl.clusterService = new TestClusterService();
Madan Jampani565a66a2015-07-25 17:01:13 -0700143 linkStoreImpl.deviceClockService = new TestDeviceClockService();
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800144 linkStoreImpl.mastershipService = new TestMastershipService();
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800145 linkStoreImpl.activate();
146 linkStore = linkStoreImpl;
147
148 verify(clusterCommunicator);
149 reset(clusterCommunicator);
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800150
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800151 }
152
153 @After
154 public void tearDown() throws Exception {
155 linkStoreImpl.deactivate();
156 }
157
158 private void putLink(DeviceId srcId, PortNumber srcNum,
159 DeviceId dstId, PortNumber dstNum, Type type,
160 SparseAnnotations... annotations) {
161 ConnectPoint src = new ConnectPoint(srcId, srcNum);
162 ConnectPoint dst = new ConnectPoint(dstId, dstNum);
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800163 linkStore.createOrUpdateLink(PID, new DefaultLinkDescription(src, dst, type, annotations));
164 verify(clusterCommunicator);
165 }
166
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800167 private void putLink(LinkKey key, Type type, SparseAnnotations... annotations) {
168 putLink(key.src().deviceId(), key.src().port(),
169 key.dst().deviceId(), key.dst().port(),
170 type, annotations);
171 }
172
173 private static void assertLink(DeviceId srcId, PortNumber srcNum,
174 DeviceId dstId, PortNumber dstNum, Type type,
175 Link link) {
176 assertEquals(srcId, link.src().deviceId());
177 assertEquals(srcNum, link.src().port());
178 assertEquals(dstId, link.dst().deviceId());
179 assertEquals(dstNum, link.dst().port());
180 assertEquals(type, link.type());
181 }
182
183 private static void assertLink(LinkKey key, Type type, Link link) {
184 assertLink(key.src().deviceId(), key.src().port(),
185 key.dst().deviceId(), key.dst().port(),
186 type, link);
187 }
188
189 @Test
190 public final void testGetLinkCount() {
191 assertEquals("initialy empty", 0, linkStore.getLinkCount());
192
193 putLink(DID1, P1, DID2, P2, DIRECT);
194 putLink(DID2, P2, DID1, P1, DIRECT);
195 putLink(DID1, P1, DID2, P2, DIRECT);
196
197 assertEquals("expecting 2 unique link", 2, linkStore.getLinkCount());
198 }
199
200 @Test
201 public final void testGetLinks() {
202 assertEquals("initialy empty", 0,
203 Iterables.size(linkStore.getLinks()));
204
205 LinkKey linkId1 = LinkKey.linkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
206 LinkKey linkId2 = LinkKey.linkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
207
208 putLink(linkId1, DIRECT);
209 putLink(linkId2, DIRECT);
210 putLink(linkId1, DIRECT);
211
212 assertEquals("expecting 2 unique link", 2,
213 Iterables.size(linkStore.getLinks()));
214
215 Map<LinkKey, Link> links = new HashMap<>();
216 for (Link link : linkStore.getLinks()) {
217 links.put(LinkKey.linkKey(link), link);
218 }
219
220 assertLink(linkId1, DIRECT, links.get(linkId1));
221 assertLink(linkId2, DIRECT, links.get(linkId2));
222 }
223
224 @Test
225 public final void testGetDeviceEgressLinks() {
226 LinkKey linkId1 = LinkKey.linkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
227 LinkKey linkId2 = LinkKey.linkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
228 LinkKey linkId3 = LinkKey.linkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
229
230 putLink(linkId1, DIRECT);
231 putLink(linkId2, DIRECT);
232 putLink(linkId3, DIRECT);
233
234 // DID1,P1 => DID2,P2
235 // DID2,P2 => DID1,P1
236 // DID1,P2 => DID2,P3
237
238 Set<Link> links1 = linkStore.getDeviceEgressLinks(DID1);
239 assertEquals(2, links1.size());
240 // check
241
242 Set<Link> links2 = linkStore.getDeviceEgressLinks(DID2);
243 assertEquals(1, links2.size());
244 assertLink(linkId2, DIRECT, links2.iterator().next());
245 }
246
247 @Test
248 public final void testGetDeviceIngressLinks() {
249 LinkKey linkId1 = LinkKey.linkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
250 LinkKey linkId2 = LinkKey.linkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
251 LinkKey linkId3 = LinkKey.linkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
252
253 putLink(linkId1, DIRECT);
254 putLink(linkId2, DIRECT);
255 putLink(linkId3, DIRECT);
256
257 // DID1,P1 => DID2,P2
258 // DID2,P2 => DID1,P1
259 // DID1,P2 => DID2,P3
260
261 Set<Link> links1 = linkStore.getDeviceIngressLinks(DID2);
262 assertEquals(2, links1.size());
263 // check
264
265 Set<Link> links2 = linkStore.getDeviceIngressLinks(DID1);
266 assertEquals(1, links2.size());
267 assertLink(linkId2, DIRECT, links2.iterator().next());
268 }
269
270 @Test
271 public final void testGetLink() {
272 ConnectPoint src = new ConnectPoint(DID1, P1);
273 ConnectPoint dst = new ConnectPoint(DID2, P2);
274 LinkKey linkId1 = LinkKey.linkKey(src, dst);
275
276 putLink(linkId1, DIRECT);
277
278 Link link = linkStore.getLink(src, dst);
279 assertLink(linkId1, DIRECT, link);
280
281 assertNull("There shouldn't be reverese link",
282 linkStore.getLink(dst, src));
283 }
284
285 @Test
286 public final void testGetEgressLinks() {
287 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
288 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
289 LinkKey linkId1 = LinkKey.linkKey(d1P1, d2P2);
290 LinkKey linkId2 = LinkKey.linkKey(d2P2, d1P1);
291 LinkKey linkId3 = LinkKey.linkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
292
293 putLink(linkId1, DIRECT);
294 putLink(linkId2, DIRECT);
295 putLink(linkId3, DIRECT);
296
297 // DID1,P1 => DID2,P2
298 // DID2,P2 => DID1,P1
299 // DID1,P2 => DID2,P3
300
301 Set<Link> links1 = linkStore.getEgressLinks(d1P1);
302 assertEquals(1, links1.size());
303 assertLink(linkId1, DIRECT, links1.iterator().next());
304
305 Set<Link> links2 = linkStore.getEgressLinks(d2P2);
306 assertEquals(1, links2.size());
307 assertLink(linkId2, DIRECT, links2.iterator().next());
308 }
309
310 @Test
311 public final void testGetIngressLinks() {
312 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
313 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
314 LinkKey linkId1 = LinkKey.linkKey(d1P1, d2P2);
315 LinkKey linkId2 = LinkKey.linkKey(d2P2, d1P1);
316 LinkKey linkId3 = LinkKey.linkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
317
318 putLink(linkId1, DIRECT);
319 putLink(linkId2, DIRECT);
320 putLink(linkId3, DIRECT);
321
322 // DID1,P1 => DID2,P2
323 // DID2,P2 => DID1,P1
324 // DID1,P2 => DID2,P3
325
326 Set<Link> links1 = linkStore.getIngressLinks(d2P2);
327 assertEquals(1, links1.size());
328 assertLink(linkId1, DIRECT, links1.iterator().next());
329
330 Set<Link> links2 = linkStore.getIngressLinks(d1P1);
331 assertEquals(1, links2.size());
332 assertLink(linkId2, DIRECT, links2.iterator().next());
333 }
334
335 @Test
336 public final void testCreateOrUpdateLink() {
337 ConnectPoint src = new ConnectPoint(DID1, P1);
338 ConnectPoint dst = new ConnectPoint(DID2, P2);
339
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800340 final DefaultLinkDescription linkDescription = new DefaultLinkDescription(src, dst, INDIRECT);
341 LinkEvent event = linkStore.createOrUpdateLink(PID,
342 linkDescription);
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800343
344 assertLink(DID1, P1, DID2, P2, INDIRECT, event.subject());
345 assertEquals(LINK_ADDED, event.type());
346
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800347 LinkEvent event2 = linkStore.createOrUpdateLink(PID,
348 new DefaultLinkDescription(src, dst, DIRECT));
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800349
350 assertLink(DID1, P1, DID2, P2, DIRECT, event2.subject());
351 assertEquals(LINK_UPDATED, event2.type());
352
353 // no change
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800354 LinkEvent event3 = linkStore.createOrUpdateLink(PID,
355 new DefaultLinkDescription(src, dst, DIRECT));
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800356
357 assertNull("No change event expected", event3);
358 }
359
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800360 private static void assertLinkDescriptionEquals(ConnectPoint src,
361 ConnectPoint dst,
362 Type type,
363 LinkDescription actual) {
364 assertEquals(src, actual.src());
365 assertEquals(dst, actual.dst());
366 assertEquals(type, actual.type());
367 // TODO check annotations
368 }
369
370 @Test
371 public final void testCreateOrUpdateLinkAncillary() {
372 ConnectPoint src = new ConnectPoint(DID1, P1);
373 ConnectPoint dst = new ConnectPoint(DID2, P2);
374
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800375 // add Ancillary link
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800376 LinkEvent event = linkStore.createOrUpdateLink(PIDA,
377 new DefaultLinkDescription(src, dst, INDIRECT, A1));
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800378
379 assertNotNull("Ancillary only link is ignored", event);
380
381 // add Primary link
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800382 LinkEvent event2 = linkStore.createOrUpdateLink(PID,
383 new DefaultLinkDescription(src, dst, INDIRECT, A2));
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800384
385 assertLink(DID1, P1, DID2, P2, INDIRECT, event2.subject());
386 assertAnnotationsEquals(event2.subject().annotations(), A2, A1);
387 assertEquals(LINK_UPDATED, event2.type());
388
389 // update link type
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800390 LinkEvent event3 = linkStore.createOrUpdateLink(PID,
391 new DefaultLinkDescription(src, dst, DIRECT, A2));
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800392
393 assertLink(DID1, P1, DID2, P2, DIRECT, event3.subject());
394 assertAnnotationsEquals(event3.subject().annotations(), A2, A1);
395 assertEquals(LINK_UPDATED, event3.type());
396
397
398 // no change
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800399 LinkEvent event4 = linkStore.createOrUpdateLink(PID,
400 new DefaultLinkDescription(src, dst, DIRECT));
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800401
402 assertNull("No change event expected", event4);
403
404 // update link annotation (Primary)
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800405 LinkEvent event5 = linkStore.createOrUpdateLink(PID,
406 new DefaultLinkDescription(src, dst, DIRECT, A2_2));
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800407
408 assertLink(DID1, P1, DID2, P2, DIRECT, event5.subject());
409 assertAnnotationsEquals(event5.subject().annotations(), A2, A2_2, A1);
410 assertEquals(LINK_UPDATED, event5.type());
411
412 // update link annotation (Ancillary)
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800413 LinkEvent event6 = linkStore.createOrUpdateLink(PIDA,
414 new DefaultLinkDescription(src, dst, DIRECT, A1_2));
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800415
416 assertLink(DID1, P1, DID2, P2, DIRECT, event6.subject());
417 assertAnnotationsEquals(event6.subject().annotations(), A2, A2_2, A1, A1_2);
418 assertEquals(LINK_UPDATED, event6.type());
419
420 // update link type (Ancillary) : ignored
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800421 LinkEvent event7 = linkStore.createOrUpdateLink(PIDA,
422 new DefaultLinkDescription(src, dst, EDGE));
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800423 assertNull("Ancillary change other than annotation is ignored", event7);
424 }
425
426
427 @Test
428 public final void testRemoveLink() {
429 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
430 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
431 LinkKey linkId1 = LinkKey.linkKey(d1P1, d2P2);
432 LinkKey linkId2 = LinkKey.linkKey(d2P2, d1P1);
433
434 putLink(linkId1, DIRECT, A1);
435 putLink(linkId2, DIRECT, A2);
436
437 // DID1,P1 => DID2,P2
438 // DID2,P2 => DID1,P1
439 // DID1,P2 => DID2,P3
440
441 LinkEvent event = linkStore.removeLink(d1P1, d2P2);
442 assertEquals(LINK_REMOVED, event.type());
443 assertAnnotationsEquals(event.subject().annotations(), A1);
444 LinkEvent event2 = linkStore.removeLink(d1P1, d2P2);
445 assertNull(event2);
446
447 assertLink(linkId2, DIRECT, linkStore.getLink(d2P2, d1P1));
448 assertAnnotationsEquals(linkStore.getLink(d2P2, d1P1).annotations(), A2);
449
450 // annotations, etc. should not survive remove
451 putLink(linkId1, DIRECT);
452 assertLink(linkId1, DIRECT, linkStore.getLink(d1P1, d2P2));
453 assertAnnotationsEquals(linkStore.getLink(d1P1, d2P2).annotations());
454 }
455
456 @Test
457 public final void testAncillaryVisible() {
458 ConnectPoint src = new ConnectPoint(DID1, P1);
459 ConnectPoint dst = new ConnectPoint(DID2, P2);
460
461 // add Ancillary link
462 linkStore.createOrUpdateLink(PIDA,
463 new DefaultLinkDescription(src, dst, INDIRECT, A1));
464
465 // Ancillary only link should not be visible
466 assertEquals(1, linkStore.getLinkCount());
467 assertNotNull(linkStore.getLink(src, dst));
468 }
469
470 // If Delegates should be called only on remote events,
471 // then Simple* should never call them, thus not test required.
472 @Ignore("Ignore until Delegate spec. is clear.")
473 @Test
474 public final void testEvents() throws InterruptedException {
475
476 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
477 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
478 final LinkKey linkId1 = LinkKey.linkKey(d1P1, d2P2);
479
480 final CountDownLatch addLatch = new CountDownLatch(1);
Sho SHIMIZU74626412015-09-11 11:46:27 -0700481 LinkStoreDelegate checkAdd = event -> {
482 assertEquals(LINK_ADDED, event.type());
483 assertLink(linkId1, INDIRECT, event.subject());
484 addLatch.countDown();
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800485 };
486 final CountDownLatch updateLatch = new CountDownLatch(1);
Sho SHIMIZU74626412015-09-11 11:46:27 -0700487 LinkStoreDelegate checkUpdate = event -> {
488 assertEquals(LINK_UPDATED, event.type());
489 assertLink(linkId1, DIRECT, event.subject());
490 updateLatch.countDown();
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800491 };
492 final CountDownLatch removeLatch = new CountDownLatch(1);
Sho SHIMIZU74626412015-09-11 11:46:27 -0700493 LinkStoreDelegate checkRemove = event -> {
494 assertEquals(LINK_REMOVED, event.type());
495 assertLink(linkId1, DIRECT, event.subject());
496 removeLatch.countDown();
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800497 };
498
499 linkStore.setDelegate(checkAdd);
500 putLink(linkId1, INDIRECT);
501 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
502
503 linkStore.unsetDelegate(checkAdd);
504 linkStore.setDelegate(checkUpdate);
505 putLink(linkId1, DIRECT);
506 assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
507
508 linkStore.unsetDelegate(checkUpdate);
509 linkStore.setDelegate(checkRemove);
510 linkStore.removeLink(d1P1, d2P2);
511 assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
512 }
513
514 private static final class TestClusterService extends StaticClusterService {
515
516 public TestClusterService() {
517 localNode = ONOS1;
518 nodes.put(NID1, ONOS1);
519 nodeStates.put(NID1, ACTIVE);
520
521 nodes.put(NID2, ONOS2);
522 nodeStates.put(NID2, ACTIVE);
523 }
524 }
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800525
Madan Jampani565a66a2015-07-25 17:01:13 -0700526 private final class TestDeviceClockService extends DeviceClockServiceAdapter {
527
528 private final AtomicLong ticker = new AtomicLong();
529
530 @Override
531 public Timestamp getTimestamp(DeviceId deviceId) {
532 if (DID1.equals(deviceId)) {
533 return new MastershipBasedTimestamp(1, ticker.getAndIncrement());
534 } else if (DID2.equals(deviceId)) {
535 return new MastershipBasedTimestamp(2, ticker.getAndIncrement());
536 } else {
537 throw new IllegalStateException();
538 }
539 }
540
541 @Override
542 public boolean isTimestampAvailable(DeviceId deviceId) {
543 return DID1.equals(deviceId) || DID2.equals(deviceId);
544 }
545 }
546
Marc De Leenheerb473b9d2015-02-06 15:21:03 -0800547 private final class TestMastershipService extends MastershipServiceAdapter {
548 @Override
549 public NodeId getMasterFor(DeviceId deviceId) {
550 return NID1;
551 }
552 }
Yuta HIGUCHI3e5d11a2014-11-04 14:16:44 -0800553}