blob: 151d978e5eacec6f0d4e1f01fe7c64ebd7c8f726 [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;
Yuta HIGUCHIfec9e192014-09-28 14:58:02 -070018import org.junit.Ignore;
Yuta HIGUCHIa8a53eb2014-09-25 17:47:55 -070019import 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.LinkStoreDelegate;
29import org.onlab.onos.net.provider.ProviderId;
Yuta HIGUCHIb5df76d2014-09-27 20:54:00 -070030import org.onlab.onos.store.common.StoreManager;
Yuta HIGUCHIa8a53eb2014-09-25 17:47:55 -070031import org.onlab.onos.store.common.StoreService;
Yuta HIGUCHIb5df76d2014-09-27 20:54:00 -070032import org.onlab.onos.store.common.TestStoreManager;
Yuta HIGUCHIa8a53eb2014-09-25 17:47:55 -070033
34import com.google.common.collect.Iterables;
35import com.hazelcast.config.Config;
36import com.hazelcast.core.Hazelcast;
37
Yuta HIGUCHIf5712ff2014-09-27 23:52:37 -070038/**
39 * Test of the Hazelcast based distributed LinkStore implementation.
40 */
Yuta HIGUCHIa8a53eb2014-09-25 17:47:55 -070041public class DistributedLinkStoreTest {
42
43 private static final ProviderId PID = new ProviderId("of", "foo");
44 private static final DeviceId DID1 = deviceId("of:foo");
45 private static final DeviceId DID2 = deviceId("of:bar");
Yuta HIGUCHIa8a53eb2014-09-25 17:47:55 -070046
47 private static final PortNumber P1 = PortNumber.portNumber(1);
48 private static final PortNumber P2 = PortNumber.portNumber(2);
49 private static final PortNumber P3 = PortNumber.portNumber(3);
50
51 private StoreManager storeManager;
52
53 private DistributedLinkStore linkStore;
54
55 @BeforeClass
56 public static void setUpBeforeClass() throws Exception {
57 }
58
59 @AfterClass
60 public static void tearDownAfterClass() throws Exception {
61 }
62
63 @Before
64 public void setUp() throws Exception {
65 // TODO should find a way to clean Hazelcast instance without shutdown.
66 Config config = TestStoreManager.getTestConfig();
67
68 storeManager = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
69 storeManager.activate();
70
71 linkStore = new TestDistributedLinkStore(storeManager);
72 linkStore.activate();
73 }
74
75 @After
76 public void tearDown() throws Exception {
77 linkStore.deactivate();
78 storeManager.deactivate();
79 }
80
81 private void putLink(DeviceId srcId, PortNumber srcNum,
82 DeviceId dstId, PortNumber dstNum, Type type) {
83 ConnectPoint src = new ConnectPoint(srcId, srcNum);
84 ConnectPoint dst = new ConnectPoint(dstId, dstNum);
85 linkStore.createOrUpdateLink(PID, new DefaultLinkDescription(src, dst, type));
86 }
87
88 private void putLink(LinkKey key, Type type) {
89 putLink(key.src().deviceId(), key.src().port(),
90 key.dst().deviceId(), key.dst().port(),
91 type);
92 }
93
94 private static void assertLink(DeviceId srcId, PortNumber srcNum,
95 DeviceId dstId, PortNumber dstNum, Type type,
96 Link link) {
97 assertEquals(srcId, link.src().deviceId());
98 assertEquals(srcNum, link.src().port());
99 assertEquals(dstId, link.dst().deviceId());
100 assertEquals(dstNum, link.dst().port());
101 assertEquals(type, link.type());
102 }
103
104 private static void assertLink(LinkKey key, Type type, Link link) {
105 assertLink(key.src().deviceId(), key.src().port(),
106 key.dst().deviceId(), key.dst().port(),
107 type, link);
108 }
109
110 @Test
111 public final void testGetLinkCount() {
112 assertEquals("initialy empty", 0, linkStore.getLinkCount());
113
114 putLink(DID1, P1, DID2, P2, DIRECT);
115 putLink(DID2, P2, DID1, P1, DIRECT);
116 putLink(DID1, P1, DID2, P2, DIRECT);
117
118 assertEquals("expecting 2 unique link", 2, linkStore.getLinkCount());
119 }
120
121 @Test
122 public final void testGetLinks() {
123 assertEquals("initialy empty", 0,
124 Iterables.size(linkStore.getLinks()));
125
126 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
127 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
128
129 putLink(linkId1, DIRECT);
130 putLink(linkId2, DIRECT);
131 putLink(linkId1, DIRECT);
132
133 assertEquals("expecting 2 unique link", 2,
134 Iterables.size(linkStore.getLinks()));
135
136 Map<LinkKey, Link> links = new HashMap<>();
137 for (Link link : linkStore.getLinks()) {
138 links.put(new LinkKey(link.src(), link.dst()), link);
139 }
140
141 assertLink(linkId1, DIRECT, links.get(linkId1));
142 assertLink(linkId2, DIRECT, links.get(linkId2));
143 }
144
145 @Test
146 public final void testGetDeviceEgressLinks() {
147 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
148 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
149 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
150
151 putLink(linkId1, DIRECT);
152 putLink(linkId2, DIRECT);
153 putLink(linkId3, DIRECT);
154
155 // DID1,P1 => DID2,P2
156 // DID2,P2 => DID1,P1
157 // DID1,P2 => DID2,P3
158
159 Set<Link> links1 = linkStore.getDeviceEgressLinks(DID1);
160 assertEquals(2, links1.size());
161 // check
162
163 Set<Link> links2 = linkStore.getDeviceEgressLinks(DID2);
164 assertEquals(1, links2.size());
165 assertLink(linkId2, DIRECT, links2.iterator().next());
166 }
167
168 @Test
169 public final void testGetDeviceIngressLinks() {
170 LinkKey linkId1 = new LinkKey(new ConnectPoint(DID1, P1), new ConnectPoint(DID2, P2));
171 LinkKey linkId2 = new LinkKey(new ConnectPoint(DID2, P2), new ConnectPoint(DID1, P1));
172 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
173
174 putLink(linkId1, DIRECT);
175 putLink(linkId2, DIRECT);
176 putLink(linkId3, DIRECT);
177
178 // DID1,P1 => DID2,P2
179 // DID2,P2 => DID1,P1
180 // DID1,P2 => DID2,P3
181
182 Set<Link> links1 = linkStore.getDeviceIngressLinks(DID2);
183 assertEquals(2, links1.size());
184 // check
185
186 Set<Link> links2 = linkStore.getDeviceIngressLinks(DID1);
187 assertEquals(1, links2.size());
188 assertLink(linkId2, DIRECT, links2.iterator().next());
189 }
190
191 @Test
192 public final void testGetLink() {
193 ConnectPoint src = new ConnectPoint(DID1, P1);
194 ConnectPoint dst = new ConnectPoint(DID2, P2);
195 LinkKey linkId1 = new LinkKey(src, dst);
196
197 putLink(linkId1, DIRECT);
198
199 Link link = linkStore.getLink(src, dst);
200 assertLink(linkId1, DIRECT, link);
201
202 assertNull("There shouldn't be reverese link",
203 linkStore.getLink(dst, src));
204 }
205
206 @Test
207 public final void testGetEgressLinks() {
208 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
209 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
210 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
211 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
212 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
213
214 putLink(linkId1, DIRECT);
215 putLink(linkId2, DIRECT);
216 putLink(linkId3, DIRECT);
217
218 // DID1,P1 => DID2,P2
219 // DID2,P2 => DID1,P1
220 // DID1,P2 => DID2,P3
221
222 Set<Link> links1 = linkStore.getEgressLinks(d1P1);
223 assertEquals(1, links1.size());
224 assertLink(linkId1, DIRECT, links1.iterator().next());
225
226 Set<Link> links2 = linkStore.getEgressLinks(d2P2);
227 assertEquals(1, links2.size());
228 assertLink(linkId2, DIRECT, links2.iterator().next());
229 }
230
231 @Test
232 public final void testGetIngressLinks() {
233 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
234 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
235 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
236 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
237 LinkKey linkId3 = new LinkKey(new ConnectPoint(DID1, P2), new ConnectPoint(DID2, P3));
238
239 putLink(linkId1, DIRECT);
240 putLink(linkId2, DIRECT);
241 putLink(linkId3, DIRECT);
242
243 // DID1,P1 => DID2,P2
244 // DID2,P2 => DID1,P1
245 // DID1,P2 => DID2,P3
246
247 Set<Link> links1 = linkStore.getIngressLinks(d2P2);
248 assertEquals(1, links1.size());
249 assertLink(linkId1, DIRECT, links1.iterator().next());
250
251 Set<Link> links2 = linkStore.getIngressLinks(d1P1);
252 assertEquals(1, links2.size());
253 assertLink(linkId2, DIRECT, links2.iterator().next());
254 }
255
256 @Test
257 public final void testCreateOrUpdateLink() {
258 ConnectPoint src = new ConnectPoint(DID1, P1);
259 ConnectPoint dst = new ConnectPoint(DID2, P2);
260
261 // add link
262 LinkEvent event = linkStore.createOrUpdateLink(PID,
263 new DefaultLinkDescription(src, dst, INDIRECT));
264
265 assertLink(DID1, P1, DID2, P2, INDIRECT, event.subject());
266 assertEquals(LINK_ADDED, event.type());
267
268 // update link type
269 LinkEvent event2 = linkStore.createOrUpdateLink(PID,
270 new DefaultLinkDescription(src, dst, DIRECT));
271
272 assertLink(DID1, P1, DID2, P2, DIRECT, event2.subject());
273 assertEquals(LINK_UPDATED, event2.type());
274
275 // no change
276 LinkEvent event3 = linkStore.createOrUpdateLink(PID,
277 new DefaultLinkDescription(src, dst, DIRECT));
278
279 assertNull("No change event expected", event3);
280 }
281
282 @Test
283 public final void testRemoveLink() {
284 final ConnectPoint d1P1 = new ConnectPoint(DID1, P1);
285 final ConnectPoint d2P2 = new ConnectPoint(DID2, P2);
286 LinkKey linkId1 = new LinkKey(d1P1, d2P2);
287 LinkKey linkId2 = new LinkKey(d2P2, d1P1);
288
289 putLink(linkId1, DIRECT);
290 putLink(linkId2, DIRECT);
291
292 // DID1,P1 => DID2,P2
293 // DID2,P2 => DID1,P1
294 // DID1,P2 => DID2,P3
295
296 LinkEvent event = linkStore.removeLink(d1P1, d2P2);
297 assertEquals(LINK_REMOVED, event.type());
298 LinkEvent event2 = linkStore.removeLink(d1P1, d2P2);
299 assertNull(event2);
300
301 assertLink(linkId2, DIRECT, linkStore.getLink(d2P2, d1P1));
302 }
303
Yuta HIGUCHIfec9e192014-09-28 14:58:02 -0700304 @Ignore("Ignore until Delegate spec. is clear.")
Yuta HIGUCHIa8a53eb2014-09-25 17:47:55 -0700305 @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}