blob: 50d0e479aced23d26b25c31efaefe3721e9edc3e [file] [log] [blame]
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -07001package org.onlab.onos.net.trivial.impl;
2
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.*;
7
8import java.util.HashMap;
9import java.util.Map;
10import java.util.Set;
11import java.util.concurrent.CountDownLatch;
12import java.util.concurrent.TimeUnit;
13
14import org.junit.After;
15import org.junit.AfterClass;
16import org.junit.Before;
17import org.junit.BeforeClass;
18import org.junit.Ignore;
19import org.junit.Test;
20import org.onlab.onos.net.ConnectPoint;
21import org.onlab.onos.net.DeviceId;
22import org.onlab.onos.net.Link;
23import org.onlab.onos.net.LinkKey;
24import org.onlab.onos.net.PortNumber;
25import org.onlab.onos.net.Link.Type;
26import org.onlab.onos.net.link.DefaultLinkDescription;
27import org.onlab.onos.net.link.LinkEvent;
28import org.onlab.onos.net.link.LinkStore;
29import org.onlab.onos.net.link.LinkStoreDelegate;
30import org.onlab.onos.net.provider.ProviderId;
31
32import com.google.common.collect.Iterables;
33
34/**
35 * Test of the simple LinkStore implementation.
36 */
37public class SimpleLinkStoreTest {
38
39 private static final ProviderId PID = new ProviderId("of", "foo");
40 private static final DeviceId DID1 = deviceId("of:foo");
41 private static final DeviceId DID2 = deviceId("of:bar");
42
43 private static final PortNumber P1 = PortNumber.portNumber(1);
44 private static final PortNumber P2 = PortNumber.portNumber(2);
45 private static final PortNumber P3 = PortNumber.portNumber(3);
46
47
48 private SimpleLinkStore simpleLinkStore;
49 private LinkStore linkStore;
50
51 @BeforeClass
52 public static void setUpBeforeClass() throws Exception {
53 }
54
55 @AfterClass
56 public static void tearDownAfterClass() throws Exception {
57 }
58
59 @Before
60 public void setUp() throws Exception {
61 simpleLinkStore = new SimpleLinkStore();
62 simpleLinkStore.activate();
63 linkStore = simpleLinkStore;
64 }
65
66 @After
67 public void tearDown() throws Exception {
68 simpleLinkStore.deactivate();
69 }
70
71 private void putLink(DeviceId srcId, PortNumber srcNum,
72 DeviceId dstId, PortNumber dstNum, Type type) {
73 ConnectPoint src = new ConnectPoint(srcId, srcNum);
74 ConnectPoint dst = new ConnectPoint(dstId, dstNum);
75 linkStore.createOrUpdateLink(PID, new DefaultLinkDescription(src, dst, type));
76 }
77
78 private void putLink(LinkKey key, Type type) {
79 putLink(key.src().deviceId(), key.src().port(),
80 key.dst().deviceId(), key.dst().port(),
81 type);
82 }
83
84 private static void assertLink(DeviceId srcId, PortNumber srcNum,
85 DeviceId dstId, PortNumber dstNum, Type type,
86 Link link) {
87 assertEquals(srcId, link.src().deviceId());
88 assertEquals(srcNum, link.src().port());
89 assertEquals(dstId, link.dst().deviceId());
90 assertEquals(dstNum, link.dst().port());
91 assertEquals(type, link.type());
92 }
93
94 private static void assertLink(LinkKey key, Type type, Link link) {
95 assertLink(key.src().deviceId(), key.src().port(),
96 key.dst().deviceId(), key.dst().port(),
97 type, link);
98 }
99
100 @Test
101 public final void testGetLinkCount() {
102 assertEquals("initialy empty", 0, linkStore.getLinkCount());
103
104 putLink(DID1, P1, DID2, P2, DIRECT);
105 putLink(DID2, P2, DID1, P1, DIRECT);
106 putLink(DID1, P1, DID2, P2, DIRECT);
107
108 assertEquals("expecting 2 unique link", 2, linkStore.getLinkCount());
109 }
110
111 @Test
112 public final void testGetLinks() {
113 assertEquals("initialy empty", 0,
114 Iterables.size(linkStore.getLinks()));
115
116 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
117 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
118
119 putLink(linkId1, DIRECT);
120 putLink(linkId2, DIRECT);
121 putLink(linkId1, DIRECT);
122
123 assertEquals("expecting 2 unique link", 2,
124 Iterables.size(linkStore.getLinks()));
125
126 Map<LinkKey, Link> links = new HashMap<>();
127 for (Link link : linkStore.getLinks()) {
128 links.put(new LinkKey(link.src(), link.dst()), link);
129 }
130
131 assertLink(linkId1, DIRECT, links.get(linkId1));
132 assertLink(linkId2, DIRECT, links.get(linkId2));
133 }
134
135 @Test
136 public final void testGetDeviceEgressLinks() {
137 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
138 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
139 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
140
141 putLink(linkId1, DIRECT);
142 putLink(linkId2, DIRECT);
143 putLink(linkId3, DIRECT);
144
145 // DID1,P1 => DID2,P2
146 // DID2,P2 => DID1,P1
147 // DID1,P2 => DID2,P3
148
149 Set<Link> links1 = linkStore.getDeviceEgressLinks(DID1);
150 assertEquals(2, links1.size());
151 // check
152
153 Set<Link> links2 = linkStore.getDeviceEgressLinks(DID2);
154 assertEquals(1, links2.size());
155 assertLink(linkId2, DIRECT, links2.iterator().next());
156 }
157
158 @Test
159 public final void testGetDeviceIngressLinks() {
160 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
161 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
162 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
163
164 putLink(linkId1, DIRECT);
165 putLink(linkId2, DIRECT);
166 putLink(linkId3, DIRECT);
167
168 // DID1,P1 => DID2,P2
169 // DID2,P2 => DID1,P1
170 // DID1,P2 => DID2,P3
171
172 Set<Link> links1 = linkStore.getDeviceIngressLinks(DID2);
173 assertEquals(2, links1.size());
174 // check
175
176 Set<Link> links2 = linkStore.getDeviceIngressLinks(DID1);
177 assertEquals(1, links2.size());
178 assertLink(linkId2, DIRECT, links2.iterator().next());
179 }
180
181 @Test
182 public final void testGetLink() {
183 ConnectPoint src = new ConnectPoint(DID1, P1);
184 ConnectPoint dst = new ConnectPoint(DID2, P2);
185 LinkKey linkId1 = new LinkKey(src, dst);
186
187 putLink(linkId1, DIRECT);
188
189 Link link = linkStore.getLink(src, dst);
190 assertLink(linkId1, DIRECT, link);
191
192 assertNull("There shouldn't be reverese link",
193 linkStore.getLink(dst, src));
194 }
195
196 @Test
197 public final void testGetEgressLinks() {
198 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
199 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
200 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
201 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
202 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
203
204 putLink(linkId1, DIRECT);
205 putLink(linkId2, DIRECT);
206 putLink(linkId3, DIRECT);
207
208 // DID1,P1 => DID2,P2
209 // DID2,P2 => DID1,P1
210 // DID1,P2 => DID2,P3
211
212 Set<Link> links1 = linkStore.getEgressLinks(d1P1);
213 assertEquals(1, links1.size());
214 assertLink(linkId1, DIRECT, links1.iterator().next());
215
216 Set<Link> links2 = linkStore.getEgressLinks(d2P2);
217 assertEquals(1, links2.size());
218 assertLink(linkId2, DIRECT, links2.iterator().next());
219 }
220
221 @Test
222 public final void testGetIngressLinks() {
223 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
224 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
225 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
226 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
227 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
228
229 putLink(linkId1, DIRECT);
230 putLink(linkId2, DIRECT);
231 putLink(linkId3, DIRECT);
232
233 // DID1,P1 => DID2,P2
234 // DID2,P2 => DID1,P1
235 // DID1,P2 => DID2,P3
236
237 Set<Link> links1 = linkStore.getIngressLinks(d2P2);
238 assertEquals(1, links1.size());
239 assertLink(linkId1, DIRECT, links1.iterator().next());
240
241 Set<Link> links2 = linkStore.getIngressLinks(d1P1);
242 assertEquals(1, links2.size());
243 assertLink(linkId2, DIRECT, links2.iterator().next());
244 }
245
246 @Test
247 public final void testCreateOrUpdateLink() {
248 ConnectPoint src = new ConnectPoint(DID1, P1);
249 ConnectPoint dst = new ConnectPoint(DID2, P2);
250
251 // add link
252 LinkEvent event = linkStore.createOrUpdateLink(PID,
253 new DefaultLinkDescription(src, dst, INDIRECT));
254
255 assertLink(DID1, P1, DID2, P2, INDIRECT, event.subject());
256 assertEquals(LINK_ADDED, event.type());
257
258 // update link type
259 LinkEvent event2 = linkStore.createOrUpdateLink(PID,
260 new DefaultLinkDescription(src, dst, DIRECT));
261
262 assertLink(DID1, P1, DID2, P2, DIRECT, event2.subject());
263 assertEquals(LINK_UPDATED, event2.type());
264
265 // no change
266 LinkEvent event3 = linkStore.createOrUpdateLink(PID,
267 new DefaultLinkDescription(src, dst, DIRECT));
268
269 assertNull("No change event expected", event3);
270 }
271
272 @Test
273 public final void testRemoveLink() {
274 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
275 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
276 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
277 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
278
279 putLink(linkId1, DIRECT);
280 putLink(linkId2, DIRECT);
281
282 // DID1,P1 => DID2,P2
283 // DID2,P2 => DID1,P1
284 // DID1,P2 => DID2,P3
285
286 LinkEvent event = linkStore.removeLink(d1P1, d2P2);
287 assertEquals(LINK_REMOVED, event.type());
288 LinkEvent event2 = linkStore.removeLink(d1P1, d2P2);
289 assertNull(event2);
290
291 assertLink(linkId2, DIRECT, linkStore.getLink(d2P2, d1P1));
292 }
293
294 // If Delegates should be called only on remote events,
295 // then Simple* should never call them, thus not test required.
296 @Ignore("Ignore until Delegate spec. is clear.")
297 @Test
298 public final void testEvents() throws InterruptedException {
299
300 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
301 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
302 final LinkKey linkId1 = new LinkKey(d1P1, d2P2);
303
304 final CountDownLatch addLatch = new CountDownLatch(1);
305 LinkStoreDelegate checkAdd = new LinkStoreDelegate() {
306 @Override
307 public void notify(LinkEvent event) {
308 assertEquals(LINK_ADDED, event.type());
309 assertLink(linkId1, INDIRECT, event.subject());
310 addLatch.countDown();
311 }
312 };
313 final CountDownLatch updateLatch = new CountDownLatch(1);
314 LinkStoreDelegate checkUpdate = new LinkStoreDelegate() {
315 @Override
316 public void notify(LinkEvent event) {
317 assertEquals(LINK_UPDATED, event.type());
318 assertLink(linkId1, DIRECT, event.subject());
319 updateLatch.countDown();
320 }
321 };
322 final CountDownLatch removeLatch = new CountDownLatch(1);
323 LinkStoreDelegate checkRemove = new LinkStoreDelegate() {
324 @Override
325 public void notify(LinkEvent event) {
326 assertEquals(LINK_REMOVED, event.type());
327 assertLink(linkId1, DIRECT, event.subject());
328 removeLatch.countDown();
329 }
330 };
331
332 linkStore.setDelegate(checkAdd);
333 putLink(linkId1, INDIRECT);
334 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
335
336 linkStore.unsetDelegate(checkAdd);
337 linkStore.setDelegate(checkUpdate);
338 putLink(linkId1, DIRECT);
339 assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
340
341 linkStore.unsetDelegate(checkUpdate);
342 linkStore.setDelegate(checkRemove);
343 linkStore.removeLink(d1P1, d2P2);
344 assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
345 }
346}