blob: 0f973582db18ec33bf5f545b92a2a08d8d5936de [file] [log] [blame]
Yuta HIGUCHIa8a53eb2014-09-25 17:47:55 -07001package org.onlab.onos.store.link.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.Test;
19import org.onlab.onos.net.ConnectPoint;
20import org.onlab.onos.net.DeviceId;
21import org.onlab.onos.net.Link;
22import org.onlab.onos.net.LinkKey;
23import org.onlab.onos.net.PortNumber;
24import org.onlab.onos.net.Link.Type;
25import org.onlab.onos.net.link.DefaultLinkDescription;
26import org.onlab.onos.net.link.LinkEvent;
27import org.onlab.onos.net.link.LinkStoreDelegate;
28import org.onlab.onos.net.provider.ProviderId;
Yuta HIGUCHIb5df76d2014-09-27 20:54:00 -070029import org.onlab.onos.store.common.StoreManager;
Yuta HIGUCHIa8a53eb2014-09-25 17:47:55 -070030import org.onlab.onos.store.common.StoreService;
Yuta HIGUCHIb5df76d2014-09-27 20:54:00 -070031import org.onlab.onos.store.common.TestStoreManager;
Yuta HIGUCHIa8a53eb2014-09-25 17:47:55 -070032
33import com.google.common.collect.Iterables;
34import com.hazelcast.config.Config;
35import com.hazelcast.core.Hazelcast;
36
37public class DistributedLinkStoreTest {
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// private static final String MFR = "whitebox";
43// private static final String HW = "1.1.x";
44// private static final String SW1 = "3.8.1";
45// private static final String SW2 = "3.9.5";
46// private static final String SN = "43311-12345";
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
52 private StoreManager storeManager;
53
54 private DistributedLinkStore linkStore;
55
56 @BeforeClass
57 public static void setUpBeforeClass() throws Exception {
58 }
59
60 @AfterClass
61 public static void tearDownAfterClass() throws Exception {
62 }
63
64 @Before
65 public void setUp() throws Exception {
66 // TODO should find a way to clean Hazelcast instance without shutdown.
67 Config config = TestStoreManager.getTestConfig();
68
69 storeManager = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
70 storeManager.activate();
71
72 linkStore = new TestDistributedLinkStore(storeManager);
73 linkStore.activate();
74 }
75
76 @After
77 public void tearDown() throws Exception {
78 linkStore.deactivate();
79 storeManager.deactivate();
80 }
81
82 private void putLink(DeviceId srcId, PortNumber srcNum,
83 DeviceId dstId, PortNumber dstNum, Type type) {
84 ConnectPoint src = new ConnectPoint(srcId, srcNum);
85 ConnectPoint dst = new ConnectPoint(dstId, dstNum);
86 linkStore.createOrUpdateLink(PID, new DefaultLinkDescription(src, dst, type));
87 }
88
89 private void putLink(LinkKey key, Type type) {
90 putLink(key.src().deviceId(), key.src().port(),
91 key.dst().deviceId(), key.dst().port(),
92 type);
93 }
94
95 private static void assertLink(DeviceId srcId, PortNumber srcNum,
96 DeviceId dstId, PortNumber dstNum, Type type,
97 Link link) {
98 assertEquals(srcId, link.src().deviceId());
99 assertEquals(srcNum, link.src().port());
100 assertEquals(dstId, link.dst().deviceId());
101 assertEquals(dstNum, link.dst().port());
102 assertEquals(type, link.type());
103 }
104
105 private static void assertLink(LinkKey key, Type type, Link link) {
106 assertLink(key.src().deviceId(), key.src().port(),
107 key.dst().deviceId(), key.dst().port(),
108 type, link);
109 }
110
111 @Test
112 public final void testGetLinkCount() {
113 assertEquals("initialy empty", 0, linkStore.getLinkCount());
114
115 putLink(DID1, P1, DID2, P2, DIRECT);
116 putLink(DID2, P2, DID1, P1, DIRECT);
117 putLink(DID1, P1, DID2, P2, DIRECT);
118
119 assertEquals("expecting 2 unique link", 2, linkStore.getLinkCount());
120 }
121
122 @Test
123 public final void testGetLinks() {
124 assertEquals("initialy empty", 0,
125 Iterables.size(linkStore.getLinks()));
126
127 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
128 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
129
130 putLink(linkId1, DIRECT);
131 putLink(linkId2, DIRECT);
132 putLink(linkId1, DIRECT);
133
134 assertEquals("expecting 2 unique link", 2,
135 Iterables.size(linkStore.getLinks()));
136
137 Map<LinkKey, Link> links = new HashMap<>();
138 for (Link link : linkStore.getLinks()) {
139 links.put(new LinkKey(link.src(), link.dst()), link);
140 }
141
142 assertLink(linkId1, DIRECT, links.get(linkId1));
143 assertLink(linkId2, DIRECT, links.get(linkId2));
144 }
145
146 @Test
147 public final void testGetDeviceEgressLinks() {
148 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
149 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
150 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
151
152 putLink(linkId1, DIRECT);
153 putLink(linkId2, DIRECT);
154 putLink(linkId3, DIRECT);
155
156 // DID1,P1 => DID2,P2
157 // DID2,P2 => DID1,P1
158 // DID1,P2 => DID2,P3
159
160 Set<Link> links1 = linkStore.getDeviceEgressLinks(DID1);
161 assertEquals(2, links1.size());
162 // check
163
164 Set<Link> links2 = linkStore.getDeviceEgressLinks(DID2);
165 assertEquals(1, links2.size());
166 assertLink(linkId2, DIRECT, links2.iterator().next());
167 }
168
169 @Test
170 public final void testGetDeviceIngressLinks() {
171 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
172 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
173 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
174
175 putLink(linkId1, DIRECT);
176 putLink(linkId2, DIRECT);
177 putLink(linkId3, DIRECT);
178
179 // DID1,P1 => DID2,P2
180 // DID2,P2 => DID1,P1
181 // DID1,P2 => DID2,P3
182
183 Set<Link> links1 = linkStore.getDeviceIngressLinks(DID2);
184 assertEquals(2, links1.size());
185 // check
186
187 Set<Link> links2 = linkStore.getDeviceIngressLinks(DID1);
188 assertEquals(1, links2.size());
189 assertLink(linkId2, DIRECT, links2.iterator().next());
190 }
191
192 @Test
193 public final void testGetLink() {
194 ConnectPoint src = new ConnectPoint(DID1, P1);
195 ConnectPoint dst = new ConnectPoint(DID2, P2);
196 LinkKey linkId1 = new LinkKey(src, dst);
197
198 putLink(linkId1, DIRECT);
199
200 Link link = linkStore.getLink(src, dst);
201 assertLink(linkId1, DIRECT, link);
202
203 assertNull("There shouldn't be reverese link",
204 linkStore.getLink(dst, src));
205 }
206
207 @Test
208 public final void testGetEgressLinks() {
209 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
210 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
211 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
212 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
213 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
214
215 putLink(linkId1, DIRECT);
216 putLink(linkId2, DIRECT);
217 putLink(linkId3, DIRECT);
218
219 // DID1,P1 => DID2,P2
220 // DID2,P2 => DID1,P1
221 // DID1,P2 => DID2,P3
222
223 Set<Link> links1 = linkStore.getEgressLinks(d1P1);
224 assertEquals(1, links1.size());
225 assertLink(linkId1, DIRECT, links1.iterator().next());
226
227 Set<Link> links2 = linkStore.getEgressLinks(d2P2);
228 assertEquals(1, links2.size());
229 assertLink(linkId2, DIRECT, links2.iterator().next());
230 }
231
232 @Test
233 public final void testGetIngressLinks() {
234 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
235 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
236 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
237 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
238 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
239
240 putLink(linkId1, DIRECT);
241 putLink(linkId2, DIRECT);
242 putLink(linkId3, DIRECT);
243
244 // DID1,P1 => DID2,P2
245 // DID2,P2 => DID1,P1
246 // DID1,P2 => DID2,P3
247
248 Set<Link> links1 = linkStore.getIngressLinks(d2P2);
249 assertEquals(1, links1.size());
250 assertLink(linkId1, DIRECT, links1.iterator().next());
251
252 Set<Link> links2 = linkStore.getIngressLinks(d1P1);
253 assertEquals(1, links2.size());
254 assertLink(linkId2, DIRECT, links2.iterator().next());
255 }
256
257 @Test
258 public final void testCreateOrUpdateLink() {
259 ConnectPoint src = new ConnectPoint(DID1, P1);
260 ConnectPoint dst = new ConnectPoint(DID2, P2);
261
262 // add link
263 LinkEvent event = linkStore.createOrUpdateLink(PID,
264 new DefaultLinkDescription(src, dst, INDIRECT));
265
266 assertLink(DID1, P1, DID2, P2, INDIRECT, event.subject());
267 assertEquals(LINK_ADDED, event.type());
268
269 // update link type
270 LinkEvent event2 = linkStore.createOrUpdateLink(PID,
271 new DefaultLinkDescription(src, dst, DIRECT));
272
273 assertLink(DID1, P1, DID2, P2, DIRECT, event2.subject());
274 assertEquals(LINK_UPDATED, event2.type());
275
276 // no change
277 LinkEvent event3 = linkStore.createOrUpdateLink(PID,
278 new DefaultLinkDescription(src, dst, DIRECT));
279
280 assertNull("No change event expected", event3);
281 }
282
283 @Test
284 public final void testRemoveLink() {
285 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
286 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
287 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
288 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
289
290 putLink(linkId1, DIRECT);
291 putLink(linkId2, DIRECT);
292
293 // DID1,P1 => DID2,P2
294 // DID2,P2 => DID1,P1
295 // DID1,P2 => DID2,P3
296
297 LinkEvent event = linkStore.removeLink(d1P1, d2P2);
298 assertEquals(LINK_REMOVED, event.type());
299 LinkEvent event2 = linkStore.removeLink(d1P1, d2P2);
300 assertNull(event2);
301
302 assertLink(linkId2, DIRECT, linkStore.getLink(d2P2, d1P1));
303 }
304
305 @Test
306 public final void testEvents() throws InterruptedException {
307
308 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
309 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
310 final LinkKey linkId1 = new LinkKey(d1P1, d2P2);
311
312 final CountDownLatch addLatch = new CountDownLatch(1);
313 LinkStoreDelegate checkAdd = new LinkStoreDelegate() {
314 @Override
315 public void notify(LinkEvent event) {
316 assertEquals(LINK_ADDED, event.type());
317 assertLink(linkId1, INDIRECT, event.subject());
318 addLatch.countDown();
319 }
320 };
321 final CountDownLatch updateLatch = new CountDownLatch(1);
322 LinkStoreDelegate checkUpdate = new LinkStoreDelegate() {
323 @Override
324 public void notify(LinkEvent event) {
325 assertEquals(LINK_UPDATED, event.type());
326 assertLink(linkId1, DIRECT, event.subject());
327 updateLatch.countDown();
328 }
329 };
330 final CountDownLatch removeLatch = new CountDownLatch(1);
331 LinkStoreDelegate checkRemove = new LinkStoreDelegate() {
332 @Override
333 public void notify(LinkEvent event) {
334 assertEquals(LINK_REMOVED, event.type());
335 assertLink(linkId1, DIRECT, event.subject());
336 removeLatch.countDown();
337 }
338 };
339
340 linkStore.setDelegate(checkAdd);
341 putLink(linkId1, INDIRECT);
342 assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
343
344 linkStore.unsetDelegate(checkAdd);
345 linkStore.setDelegate(checkUpdate);
346 putLink(linkId1, DIRECT);
347 assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
348
349 linkStore.unsetDelegate(checkUpdate);
350 linkStore.setDelegate(checkRemove);
351 linkStore.removeLink(d1P1, d2P2);
352 assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
353 }
354
355
356 class TestDistributedLinkStore extends DistributedLinkStore {
357 TestDistributedLinkStore(StoreService storeService) {
358 this.storeService = storeService;
359 }
360 }
361}