blob: 47da868678f47c7121b2d4ca655f5c9655156342 [file] [log] [blame]
tomea961ff2014-10-01 12:45:15 -07001package org.onlab.onos.store.trivial.impl;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -07002
3import static org.junit.Assert.*;
4import static org.onlab.onos.net.DeviceId.deviceId;
5import static org.onlab.onos.net.Link.Type.*;
6import static org.onlab.onos.net.link.LinkEvent.Type.*;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -07007import static org.onlab.onos.store.trivial.impl.SimpleDeviceStoreTest.assertAnnotationsEquals;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -07008
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -07009import java.util.Collections;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070010import java.util.HashMap;
11import java.util.Map;
12import java.util.Set;
13import java.util.concurrent.CountDownLatch;
14import java.util.concurrent.TimeUnit;
15
16import org.junit.After;
17import org.junit.AfterClass;
18import org.junit.Before;
19import org.junit.BeforeClass;
20import org.junit.Ignore;
21import org.junit.Test;
22import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070023import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070024import org.onlab.onos.net.DeviceId;
25import org.onlab.onos.net.Link;
26import org.onlab.onos.net.LinkKey;
27import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070028import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070029import org.onlab.onos.net.Link.Type;
30import org.onlab.onos.net.link.DefaultLinkDescription;
31import org.onlab.onos.net.link.LinkEvent;
32import org.onlab.onos.net.link.LinkStore;
33import org.onlab.onos.net.link.LinkStoreDelegate;
34import org.onlab.onos.net.provider.ProviderId;
35
36import com.google.common.collect.Iterables;
37
38/**
39 * Test of the simple LinkStore implementation.
40 */
41public class SimpleLinkStoreTest {
42
43 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070044 private static final ProviderId PIDA = new ProviderId("of", "bar", true);
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070045 private static final DeviceId DID1 = deviceId("of:foo");
46 private static final DeviceId DID2 = deviceId("of:bar");
47
48 private static final PortNumber P1 = PortNumber.portNumber(1);
49 private static final PortNumber P2 = PortNumber.portNumber(2);
50 private static final PortNumber P3 = PortNumber.portNumber(3);
51
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070052 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
53 .set("A1", "a1")
54 .set("B1", "b1")
55 .build();
56 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
57 .remove("A1")
58 .set("B3", "b3")
59 .build();
60 private static final SparseAnnotations A2 = DefaultAnnotations.builder()
61 .set("A2", "a2")
62 .set("B2", "b2")
63 .build();
64 private static final SparseAnnotations A2_2 = DefaultAnnotations.builder()
65 .remove("A2")
66 .set("B4", "b4")
67 .build();
68
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070069
70 private SimpleLinkStore simpleLinkStore;
71 private LinkStore linkStore;
72
73 @BeforeClass
74 public static void setUpBeforeClass() throws Exception {
75 }
76
77 @AfterClass
78 public static void tearDownAfterClass() throws Exception {
79 }
80
81 @Before
82 public void setUp() throws Exception {
83 simpleLinkStore = new SimpleLinkStore();
84 simpleLinkStore.activate();
85 linkStore = simpleLinkStore;
86 }
87
88 @After
89 public void tearDown() throws Exception {
90 simpleLinkStore.deactivate();
91 }
92
93 private void putLink(DeviceId srcId, PortNumber srcNum,
94 DeviceId dstId, PortNumber dstNum, Type type) {
95 ConnectPoint src = new ConnectPoint(srcId, srcNum);
96 ConnectPoint dst = new ConnectPoint(dstId, dstNum);
97 linkStore.createOrUpdateLink(PID, new DefaultLinkDescription(src, dst, type));
98 }
99
100 private void putLink(LinkKey key, Type type) {
101 putLink(key.src().deviceId(), key.src().port(),
102 key.dst().deviceId(), key.dst().port(),
103 type);
104 }
105
106 private static void assertLink(DeviceId srcId, PortNumber srcNum,
107 DeviceId dstId, PortNumber dstNum, Type type,
108 Link link) {
109 assertEquals(srcId, link.src().deviceId());
110 assertEquals(srcNum, link.src().port());
111 assertEquals(dstId, link.dst().deviceId());
112 assertEquals(dstNum, link.dst().port());
113 assertEquals(type, link.type());
114 }
115
116 private static void assertLink(LinkKey key, Type type, Link link) {
117 assertLink(key.src().deviceId(), key.src().port(),
118 key.dst().deviceId(), key.dst().port(),
119 type, link);
120 }
121
122 @Test
123 public final void testGetLinkCount() {
124 assertEquals("initialy empty", 0, linkStore.getLinkCount());
125
126 putLink(DID1, P1, DID2, P2, DIRECT);
127 putLink(DID2, P2, DID1, P1, DIRECT);
128 putLink(DID1, P1, DID2, P2, DIRECT);
129
130 assertEquals("expecting 2 unique link", 2, linkStore.getLinkCount());
131 }
132
133 @Test
134 public final void testGetLinks() {
135 assertEquals("initialy empty", 0,
136 Iterables.size(linkStore.getLinks()));
137
138 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
139 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
140
141 putLink(linkId1, DIRECT);
142 putLink(linkId2, DIRECT);
143 putLink(linkId1, DIRECT);
144
145 assertEquals("expecting 2 unique link", 2,
146 Iterables.size(linkStore.getLinks()));
147
148 Map<LinkKey, Link> links = new HashMap<>();
149 for (Link link : linkStore.getLinks()) {
150 links.put(new LinkKey(link.src(), link.dst()), link);
151 }
152
153 assertLink(linkId1, DIRECT, links.get(linkId1));
154 assertLink(linkId2, DIRECT, links.get(linkId2));
155 }
156
157 @Test
158 public final void testGetDeviceEgressLinks() {
159 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
160 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
161 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
162
163 putLink(linkId1, DIRECT);
164 putLink(linkId2, DIRECT);
165 putLink(linkId3, DIRECT);
166
167 // DID1,P1 => DID2,P2
168 // DID2,P2 => DID1,P1
169 // DID1,P2 => DID2,P3
170
171 Set<Link> links1 = linkStore.getDeviceEgressLinks(DID1);
172 assertEquals(2, links1.size());
173 // check
174
175 Set<Link> links2 = linkStore.getDeviceEgressLinks(DID2);
176 assertEquals(1, links2.size());
177 assertLink(linkId2, DIRECT, links2.iterator().next());
178 }
179
180 @Test
181 public final void testGetDeviceIngressLinks() {
182 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
183 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
184 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
185
186 putLink(linkId1, DIRECT);
187 putLink(linkId2, DIRECT);
188 putLink(linkId3, DIRECT);
189
190 // DID1,P1 => DID2,P2
191 // DID2,P2 => DID1,P1
192 // DID1,P2 => DID2,P3
193
194 Set<Link> links1 = linkStore.getDeviceIngressLinks(DID2);
195 assertEquals(2, links1.size());
196 // check
197
198 Set<Link> links2 = linkStore.getDeviceIngressLinks(DID1);
199 assertEquals(1, links2.size());
200 assertLink(linkId2, DIRECT, links2.iterator().next());
201 }
202
203 @Test
204 public final void testGetLink() {
205 ConnectPoint src = new ConnectPoint(DID1, P1);
206 ConnectPoint dst = new ConnectPoint(DID2, P2);
207 LinkKey linkId1 = new LinkKey(src, dst);
208
209 putLink(linkId1, DIRECT);
210
211 Link link = linkStore.getLink(src, dst);
212 assertLink(linkId1, DIRECT, link);
213
214 assertNull("There shouldn't be reverese link",
215 linkStore.getLink(dst, src));
216 }
217
218 @Test
219 public final void testGetEgressLinks() {
220 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
221 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
222 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
223 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
224 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
225
226 putLink(linkId1, DIRECT);
227 putLink(linkId2, DIRECT);
228 putLink(linkId3, DIRECT);
229
230 // DID1,P1 => DID2,P2
231 // DID2,P2 => DID1,P1
232 // DID1,P2 => DID2,P3
233
234 Set<Link> links1 = linkStore.getEgressLinks(d1P1);
235 assertEquals(1, links1.size());
236 assertLink(linkId1, DIRECT, links1.iterator().next());
237
238 Set<Link> links2 = linkStore.getEgressLinks(d2P2);
239 assertEquals(1, links2.size());
240 assertLink(linkId2, DIRECT, links2.iterator().next());
241 }
242
243 @Test
244 public final void testGetIngressLinks() {
245 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
246 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
247 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
248 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
249 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
250
251 putLink(linkId1, DIRECT);
252 putLink(linkId2, DIRECT);
253 putLink(linkId3, DIRECT);
254
255 // DID1,P1 => DID2,P2
256 // DID2,P2 => DID1,P1
257 // DID1,P2 => DID2,P3
258
259 Set<Link> links1 = linkStore.getIngressLinks(d2P2);
260 assertEquals(1, links1.size());
261 assertLink(linkId1, DIRECT, links1.iterator().next());
262
263 Set<Link> links2 = linkStore.getIngressLinks(d1P1);
264 assertEquals(1, links2.size());
265 assertLink(linkId2, DIRECT, links2.iterator().next());
266 }
267
268 @Test
269 public final void testCreateOrUpdateLink() {
270 ConnectPoint src = new ConnectPoint(DID1, P1);
271 ConnectPoint dst = new ConnectPoint(DID2, P2);
272
273 // add link
274 LinkEvent event = linkStore.createOrUpdateLink(PID,
275 new DefaultLinkDescription(src, dst, INDIRECT));
276
277 assertLink(DID1, P1, DID2, P2, INDIRECT, event.subject());
278 assertEquals(LINK_ADDED, event.type());
279
280 // update link type
281 LinkEvent event2 = linkStore.createOrUpdateLink(PID,
282 new DefaultLinkDescription(src, dst, DIRECT));
283
284 assertLink(DID1, P1, DID2, P2, DIRECT, event2.subject());
285 assertEquals(LINK_UPDATED, event2.type());
286
287 // no change
288 LinkEvent event3 = linkStore.createOrUpdateLink(PID,
289 new DefaultLinkDescription(src, dst, DIRECT));
290
291 assertNull("No change event expected", event3);
292 }
293
294 @Test
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700295 public final void testCreateOrUpdateLinkAncillary() {
296 ConnectPoint src = new ConnectPoint(DID1, P1);
297 ConnectPoint dst = new ConnectPoint(DID2, P2);
298
299 // add Ancillary link
300 LinkEvent event = linkStore.createOrUpdateLink(PIDA,
301 new DefaultLinkDescription(src, dst, INDIRECT, A1));
302
303 assertNull("Ancillary only link is ignored", event);
304
305 // add Primary link
306 LinkEvent event2 = linkStore.createOrUpdateLink(PID,
307 new DefaultLinkDescription(src, dst, INDIRECT, A2));
308
309 assertLink(DID1, P1, DID2, P2, INDIRECT, event2.subject());
310 assertAnnotationsEquals(event2.subject().annotations(), A2, A1);
311 assertEquals(LINK_ADDED, event2.type());
312
313 // update link type
314 LinkEvent event3 = linkStore.createOrUpdateLink(PID,
315 new DefaultLinkDescription(src, dst, DIRECT, A2));
316 assertLink(DID1, P1, DID2, P2, DIRECT, event3.subject());
317 assertAnnotationsEquals(event3.subject().annotations(), A2, A1);
318 assertEquals(LINK_UPDATED, event3.type());
319
320
321 // no change
322 LinkEvent event4 = linkStore.createOrUpdateLink(PID,
323 new DefaultLinkDescription(src, dst, DIRECT));
324 assertNull("No change event expected", event4);
325
326 // update link annotation (Primary)
327 LinkEvent event5 = linkStore.createOrUpdateLink(PID,
328 new DefaultLinkDescription(src, dst, DIRECT, A2_2));
329 assertLink(DID1, P1, DID2, P2, DIRECT, event5.subject());
330 assertAnnotationsEquals(event5.subject().annotations(), A2, A2_2, A1);
331 assertEquals(LINK_UPDATED, event5.type());
332
333 // update link annotation (Ancillary)
334 LinkEvent event6 = linkStore.createOrUpdateLink(PIDA,
335 new DefaultLinkDescription(src, dst, DIRECT, A1_2));
336 assertLink(DID1, P1, DID2, P2, DIRECT, event6.subject());
337 assertAnnotationsEquals(event6.subject().annotations(), A2, A2_2, A1, A1_2);
338 assertEquals(LINK_UPDATED, event6.type());
339
340 // update link type (Ancillary) : ignored
341 LinkEvent event7 = linkStore.createOrUpdateLink(PIDA,
342 new DefaultLinkDescription(src, dst, EDGE));
343 assertNull("Ancillary change other than annotation is ignored", event7);
344 }
345
346
347 @Test
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700348 public final void testRemoveLink() {
349 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
350 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
351 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
352 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
353
354 putLink(linkId1, DIRECT);
355 putLink(linkId2, DIRECT);
356
357 // DID1,P1 => DID2,P2
358 // DID2,P2 => DID1,P1
359 // DID1,P2 => DID2,P3
360
361 LinkEvent event = linkStore.removeLink(d1P1, d2P2);
362 assertEquals(LINK_REMOVED, event.type());
363 LinkEvent event2 = linkStore.removeLink(d1P1, d2P2);
364 assertNull(event2);
365
366 assertLink(linkId2, DIRECT, linkStore.getLink(d2P2, d1P1));
367 }
368
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700369 @Test
370 public final void testAncillaryOnlyNotVisible() {
371 ConnectPoint src = new ConnectPoint(DID1, P1);
372 ConnectPoint dst = new ConnectPoint(DID2, P2);
373
374 // add Ancillary link
375 linkStore.createOrUpdateLink(PIDA,
376 new DefaultLinkDescription(src, dst, INDIRECT, A1));
377
378 // Ancillary only link should not be visible
379 assertEquals(0, linkStore.getLinkCount());
380
381 assertTrue(Iterables.isEmpty(linkStore.getLinks()));
382
383 assertNull(linkStore.getLink(src, dst));
384
385 assertEquals(Collections.emptySet(), linkStore.getIngressLinks(dst));
386
387 assertEquals(Collections.emptySet(), linkStore.getEgressLinks(src));
388
389 assertEquals(Collections.emptySet(), linkStore.getDeviceEgressLinks(DID1));
390 assertEquals(Collections.emptySet(), linkStore.getDeviceIngressLinks(DID2));
391 }
392
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -0700393 // If Delegates should be called only on remote events,
394 // then Simple* should never call them, thus not test required.
395 @Ignore("Ignore until Delegate spec. is clear.")
396 @Test
397 public final void testEvents() throws InterruptedException {
398
399 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
400 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
401 final LinkKey linkId1 = new LinkKey(d1P1, d2P2);
402
403 final CountDownLatch addLatch = new CountDownLatch(1);
404 LinkStoreDelegate checkAdd = new LinkStoreDelegate() {
405 @Override
406 public void notify(LinkEvent event) {
407 assertEquals(LINK_ADDED, event.type());
408 assertLink(linkId1, INDIRECT, event.subject());
409 addLatch.countDown();
410 }
411 };
412 final CountDownLatch updateLatch = new CountDownLatch(1);
413 LinkStoreDelegate checkUpdate = new LinkStoreDelegate() {
414 @Override
415 public void notify(LinkEvent event) {
416 assertEquals(LINK_UPDATED, event.type());
417 assertLink(linkId1, DIRECT, event.subject());
418 updateLatch.countDown();
419 }
420 };
421 final CountDownLatch removeLatch = new CountDownLatch(1);
422 LinkStoreDelegate checkRemove = new LinkStoreDelegate() {
423 @Override
424 public void notify(LinkEvent event) {
425 assertEquals(LINK_REMOVED, event.type());
426 assertLink(linkId1, DIRECT, event.subject());
427 removeLatch.countDown();
428 }
429 };
430
431 linkStore.setDelegate(checkAdd);
432 putLink(linkId1, INDIRECT);
433 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
434
435 linkStore.unsetDelegate(checkAdd);
436 linkStore.setDelegate(checkUpdate);
437 putLink(linkId1, DIRECT);
438 assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
439
440 linkStore.unsetDelegate(checkUpdate);
441 linkStore.setDelegate(checkRemove);
442 linkStore.removeLink(d1P1, d2P2);
443 assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
444 }
445}