blob: 32865c7d55ddd90fe92bf6f8bc0fa5abc0222b09 [file] [log] [blame]
Carolina Fernandezad893432016-07-18 11:11:34 +02001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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 */
16
17package org.onosproject.sdxl2;
18
19
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
25import org.onosproject.TestApplicationId;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.intent.AbstractIntentTest;
33import org.onosproject.net.intent.Intent;
34import org.onosproject.net.intent.IntentUtils;
35import org.onosproject.net.intent.Key;
36import org.onosproject.net.intent.PointToPointIntent;
37
38import java.util.Arrays;
39import java.util.ArrayList;
40import java.util.Collections;
41import java.util.Iterator;
42import java.util.List;
43import java.util.Optional;
44
45import static java.lang.String.format;
46import static org.junit.Assert.assertEquals;
47import static org.junit.Assert.assertTrue;
48
49
50/**
51 * Unit tests for SDX-L2 Mac VC Manager.
52 */
53public class SdxL2MacVCManagerTest extends AbstractIntentTest {
54
55 private static final String SDXL2_2 = "sdxl2_test2";
56 private static final String CP1 = "of:00000000000001/1";
57 private static final String CP2 = "of:00000000000002/1";
58 private static final String CP5 = "of:00000000000002/4";
59 private static final String CP6 = "of:00000000000004/4";
60 private static final String CP7 = "of:0000000000000a/4";
61 private static final String CP8 = "of:00000000000009/4";
62 private static final String CP9 = "of:0000000000000a/4";
63 private static final String CP10 = "of:00000000000009/4";
64 private static final String VLANS1 = "2,3,4";
65 private static final ArrayList<String> VLANS1_ARRAY =
66 new ArrayList<String>(Arrays.asList(VLANS1.split(",")));
67 private static final String VLANS2 = "4,5,6";
68 private static final ArrayList<String> VLANS2_ARRAY =
69 new ArrayList<String>(Arrays.asList(VLANS2.split(",")));
70
71 private static final String VLANS5 = "100";
72 private static final String VLANS6 = "1";
73 private static final String VLANS7 = "1";
74 private static final String VLANS8 = "111";
75 private static final String VLANS9 = "1";
76 private static final String VLANS10 = "1";
77 private static final String CEMAC1 = "52:40:00:12:44:01";
78 private static final String CEMAC2 = "51:12:11:00:23:01";
79 private static final String CEMAC5 = "52:12:11:00:23:11";
80 private static final String CEMAC6 = "52:12:11:a0:23:11";
81 private static final String CEMAC7 = "52:12:21:00:25:11";
82 private static final String CEMAC8 = "52:12:14:a0:23:11";
83 private static final String CEMAC9 = "52:12:21:00:28:11";
84 private static final String CEMAC10 = "52:12:14:aa:23:11";
85 private static final String NAME_FORMAT = "%s:%s-%s";
86 private static final String KEY_FORMAT = "%s,%s";
87 private static final ApplicationId APPID = TestApplicationId.create("foo");
88 private static final int POINT_TO_POINT_INDEXES = 3;
89 private SdxL2MacVCManager manager;
90 private List<PointToPointIntent> intentList;
91
92 /**
93 * Prepare environment before starting testing MAC-based VCs.
94 */
95 @Before
96 public void setUp() throws Exception {
97 super.setUp();
98 SdxL2DistributedStore store = new SdxL2DistributedStore();
99 store.initForTest();
100 manager = new SdxL2MacVCManager(APPID, store, new IntentServiceTest());
101 intentList = setIntents();
102 }
103
104 /**
105 * Clean up environment after finishing testing MAC-based VCs.
106 */
107 @After
108 public void tearDown() {
109 super.tearDown();
110 }
111
112 public List<PointToPointIntent> setIntents() {
113 List<PointToPointIntent> intents = new ArrayList<PointToPointIntent>();
114 intents.addAll(setupConnectionPoints1To2());
115 intents.addAll(setupConnectionPoints5To6());
116 intents.addAll(setupConnectionPoints7To8());
117 intents.addAll(setupConnectionPoints9To10());
118 return intents;
119 }
120
121 private TrafficTreatment buildTreatment(VlanId setVlan,
122 VlanId pushVlan,
123 boolean popVlan) {
124
125 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
126 if (setVlan != null) {
127 treatmentBuilder.setVlanId(setVlan);
128 }
129 if (pushVlan != null) {
130 treatmentBuilder.pushVlan();
131 treatmentBuilder.setVlanId(pushVlan);
132 }
133 if (popVlan) {
134 treatmentBuilder.popVlan();
135 }
136 return treatmentBuilder.build();
137 }
138
139 private TrafficSelector buildSelector(MacAddress ingressMac,
140 MacAddress egressMac,
141 Short etherType,
142 VlanId ingressTag) {
143
144 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
145 selectorBuilder.matchEthSrc(ingressMac);
146 selectorBuilder.matchEthDst(egressMac);
147 if (etherType != null) {
148 selectorBuilder.matchEthType(etherType);
149 }
150 if (ingressTag != null) {
151 selectorBuilder.matchVlanId(ingressTag);
152 }
153 return selectorBuilder.build();
154 }
155
156 private Key generateIntentKey(String sdxl2, SdxL2ConnectionPoint cpOne, SdxL2ConnectionPoint cpTwo, String index) {
157 String cps = format(NAME_FORMAT, sdxl2, cpOne.name(), cpTwo.name());
158 String key = format(KEY_FORMAT, cps, index);
159 return Key.of(key, APPID);
160 }
161
162 @Test
163 public void testConnectionSetup() {
164 Iterator<SdxL2ConnectionPoint> lhs = setupLhsCPs().iterator();
165 Iterator<SdxL2ConnectionPoint> rhs = setupRhsCPs().iterator();
166 while (lhs.hasNext()) {
167 manager.addVC(SDXL2_2, lhs.next(), rhs.next());
168 }
169
170 assertEquals(intentList.size(), manager.intentService.getIntentCount());
171
172 for (Intent emulatedIntent : intentList) {
173 boolean found = false;
174 for (Intent realIntent : manager.intentService.getIntents()) {
175 if (emulatedIntent.key().equals(realIntent.key())) {
176 found = true;
177 assertTrue(format("Comparing %s and %s", emulatedIntent, realIntent),
178 IntentUtils.intentsAreEqual(emulatedIntent, realIntent));
179 break;
180 }
181 }
182 assertTrue(found);
183 }
184 }
185
186 public List<SdxL2ConnectionPoint> setupLhsCPs() {
187 List<SdxL2ConnectionPoint> cps = new ArrayList<SdxL2ConnectionPoint>();
188 SdxL2ConnectionPoint cpone = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1);
189 cps.add(cpone);
190 SdxL2ConnectionPoint cpfive = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST5", CP5, VLANS5, CEMAC5);
191 cps.add(cpfive);
192 SdxL2ConnectionPoint cpseven = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST7", CP7, VLANS7, CEMAC7);
193 cps.add(cpseven);
194 SdxL2ConnectionPoint cpnine = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST9", CP9, VLANS9, CEMAC9);
195 cps.add(cpnine);
196 return cps;
197 }
198
199 public List<SdxL2ConnectionPoint> setupRhsCPs() {
200 List<SdxL2ConnectionPoint> cps = new ArrayList<SdxL2ConnectionPoint>();
201 SdxL2ConnectionPoint cptwo = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC2);
202 cps.add(cptwo);
203 SdxL2ConnectionPoint cpsix = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST6", CP6, VLANS6, CEMAC6);
204 cps.add(cpsix);
205 SdxL2ConnectionPoint cpeight = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST8", CP8, VLANS8, CEMAC8);
206 cps.add(cpeight);
207 SdxL2ConnectionPoint cpten = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST10", CP10, VLANS10, CEMAC10);
208 cps.add(cpten);
209 return cps;
210 }
211
212 private List<PointToPointIntent> setupConnectionPoints(String keyIndex,
213 SdxL2ConnectionPoint lhs, String lhsID,
214 String lhsMac, String lhsVlan,
215 TrafficTreatment lhsBuiltTreatment,
216 SdxL2ConnectionPoint rhs, String rhsID,
217 String rhsMac, String rhsVlan,
218 TrafficTreatment rhsBuiltTreatment) {
219 List<PointToPointIntent> intents = new ArrayList<PointToPointIntent>();
220 VlanId lhsVlanValue = null, rhsVlanValue = null;
221 if (lhsVlan != null) {
222 lhsVlanValue = VlanId.vlanId(Short.parseShort(lhsVlan));
223 }
224 if (rhsVlan != null) {
225 rhsVlanValue = VlanId.vlanId(Short.parseShort(rhsVlan));
226 }
227
228 intents.add(PointToPointIntent.builder()
229 .appId(APPID)
230 .key(generateIntentKey(SDXL2_2, lhs, rhs, keyIndex))
231 .selector(buildSelector(MacAddress.valueOf(lhsMac),
232 MacAddress.valueOf(rhsMac),
233 null, lhsVlanValue))
234 .treatment(lhsBuiltTreatment)
235 .ingressPoint(ConnectPoint.deviceConnectPoint(lhsID))
236 .egressPoint(ConnectPoint.deviceConnectPoint(rhsID))
237 .priority(2000)
238 .build());
239 intents.add(PointToPointIntent.builder()
240 .appId(APPID)
241 .key(generateIntentKey(SDXL2_2, rhs, lhs, keyIndex))
242 .selector(buildSelector(MacAddress.valueOf(rhsMac),
243 MacAddress.valueOf(lhsMac),
244 null, rhsVlanValue))
245 .treatment(rhsBuiltTreatment)
246 .ingressPoint(ConnectPoint.deviceConnectPoint(rhsID))
247 .egressPoint(ConnectPoint.deviceConnectPoint(lhsID))
248 .priority(2000)
249 .build());
250 return intents;
251 }
252
253 private List<PointToPointIntent> setupConnectionPoints1To2() {
254 List<PointToPointIntent> intents = new ArrayList<PointToPointIntent>();
255 String lhsID = CP1;
256 ArrayList<String> lhsVlan = VLANS1_ARRAY;
257 String lhsMac = CEMAC1;
258 String rhsID = CP2;
259 ArrayList<String> rhsVlan = VLANS2_ARRAY;
260 String rhsMac = CEMAC2;
261 SdxL2ConnectionPoint lhs = SdxL2ConnectionPoint.sdxl2ConnectionPoint(
262 "TEST1", lhsID, VLANS1, lhsMac);
263 SdxL2ConnectionPoint rhs = SdxL2ConnectionPoint.sdxl2ConnectionPoint(
264 "TEST2", rhsID, VLANS2, rhsMac);
265 TrafficTreatment lhsBuiltTreatment, rhsBuiltTreatment;
266
267 for (int i = 0; i < POINT_TO_POINT_INDEXES; i++) {
268 lhsBuiltTreatment = buildTreatment(VlanId.vlanId(rhsVlan.get(i)), null, false);
269 rhsBuiltTreatment = buildTreatment(VlanId.vlanId(lhsVlan.get(i)), null, false);
270 intents.addAll(setupConnectionPoints(Integer.toString(i + 1),
271 lhs, lhsID, lhsMac, lhsVlan.get(i), lhsBuiltTreatment,
272 rhs, rhsID, rhsMac, rhsVlan.get(i), rhsBuiltTreatment));
273 }
274 return intents;
275 }
276
277 private List<PointToPointIntent> setupConnectionPoints5To6() {
278 String lhsID = CP5;
279 String lhsVlan = VLANS5;
280 String lhsMac = CEMAC5;
281 SdxL2ConnectionPoint lhs = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST5", lhsID, lhsVlan, lhsMac);
282 String rhsID = CP6;
283 String rhsVlan = VLANS6;
284 String rhsMac = CEMAC6;
285 SdxL2ConnectionPoint rhs = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST6", rhsID, rhsVlan, rhsMac);
286 TrafficTreatment lhsBuiltTreatment = buildTreatment(null, null, true);
287 TrafficTreatment rhsBuiltTreatment = buildTreatment(null, VlanId.vlanId(Short.parseShort(lhsVlan)), false);
288 return setupConnectionPoints("1", lhs, lhsID, lhsMac, lhsVlan, lhsBuiltTreatment,
289 rhs, rhsID, rhsMac, null, rhsBuiltTreatment);
290 }
291
292 private List<PointToPointIntent> setupConnectionPoints7To8() {
293 String lhsID = CP7;
294 String lhsVlan = VLANS7;
295 String lhsMac = CEMAC7;
296 SdxL2ConnectionPoint lhs = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST7", lhsID, lhsVlan, lhsMac);
297 String rhsID = CP8;
298 String rhsVlan = VLANS8;
299 String rhsMac = CEMAC8;
300 SdxL2ConnectionPoint rhs = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST8", rhsID, rhsVlan, rhsMac);
301 TrafficTreatment lhsBuiltTreatment = buildTreatment(null, VlanId.vlanId(Short.parseShort(rhsVlan)), false);
302 TrafficTreatment rhsBuiltTreatment = buildTreatment(null, null, true);
303 return setupConnectionPoints("1", lhs, lhsID, lhsMac, null, lhsBuiltTreatment,
304 rhs, rhsID, rhsMac, rhsVlan, rhsBuiltTreatment);
305 }
306
307 private List<PointToPointIntent> setupConnectionPoints9To10() {
308 String lhsID = CP9;
309 String lhsVlan = VLANS9;
310 String lhsMac = CEMAC9;
311 SdxL2ConnectionPoint lhs = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST9", lhsID, lhsVlan, lhsMac);
312 String rhsID = CP10;
313 String rhsVlan = VLANS10;
314 String rhsMac = CEMAC10;
315 SdxL2ConnectionPoint rhs = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST10", rhsID, rhsVlan, rhsMac);
316 TrafficTreatment nullTreatment = buildTreatment(null, null, false);
317 return setupConnectionPoints("1", lhs, lhsID, lhsMac, null, nullTreatment,
318 rhs, rhsID, rhsMac, null, nullTreatment);
319 }
320
321 @Test
322 public void removeConnection() {
323 testConnectionSetup();
324 List<PointToPointIntent> removedIntents = new ArrayList<PointToPointIntent>();
325
326 SdxL2ConnectionPoint cpone = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1);
327 SdxL2ConnectionPoint cptwo = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC2);
328 removedIntents.addAll(setupConnectionPoints1To2());
329
330 SdxL2ConnectionPoint cpfive = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST5", CP5, VLANS5, CEMAC5);
331 SdxL2ConnectionPoint cpsix = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST6", CP6, VLANS6, CEMAC6);
332 removedIntents.addAll(setupConnectionPoints5To6());
333
334 SdxL2ConnectionPoint cpseven = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST7", CP7, VLANS7, CEMAC7);
335 SdxL2ConnectionPoint cpeight = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST8", CP8, VLANS8, CEMAC8);
336 removedIntents.addAll(setupConnectionPoints7To8());
337
338 manager.removeVC(cpone, cptwo);
339 manager.removeVC(cpfive, cpsix);
340 manager.removeVC(cpseven, cpeight);
341
342 assertEquals(2, manager.intentService.getIntentCount());
343
344 for (Intent removedIntent : removedIntents) {
345 boolean found = false;
346 for (Intent existingIntent : manager.intentService.getIntents()) {
347 if (removedIntent.key().equals(existingIntent.key())) {
348 found = true;
349 assertTrue(format("Intent %s equal %s", removedIntent, existingIntent),
350 !IntentUtils.intentsAreEqual(removedIntent, existingIntent));
351 break;
352 }
353 }
354 assertTrue(!found);
355 }
356
357 }
358
359 @Test
360 public void testRemoveVCbyCP() {
361 testConnectionSetup();
362
363 List<PointToPointIntent> removedIntents = new ArrayList<PointToPointIntent>();
364
365 SdxL2ConnectionPoint cpone = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1);
366 removedIntents.addAll(setupConnectionPoints1To2());
367
368 SdxL2ConnectionPoint cpsix = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST6", CP6, VLANS6, CEMAC6);
369 removedIntents.addAll(setupConnectionPoints5To6());
370
371 SdxL2ConnectionPoint cpseven = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST7", CP7, VLANS7, CEMAC7);
372 removedIntents.addAll(setupConnectionPoints7To8());
373
374 SdxL2ConnectionPoint cpten = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST10", CP10, VLANS10, CEMAC10);
375 removedIntents.addAll(setupConnectionPoints9To10());
376
377 manager.removeVC(cpone);
378 manager.removeVC(cpsix);
379 manager.removeVC(cpseven);
380 manager.removeVC(cpten);
381
382 assertEquals(Collections.emptySet(), manager.getVCs(Optional.ofNullable(null)));
383
384 assertEquals(0, manager.intentService.getIntentCount());
385
386 for (Intent removedIntent : removedIntents) {
387 boolean found = false;
388 for (Intent existingIntent : manager.intentService.getIntents()) {
389 if (removedIntent.key().equals(existingIntent.key())) {
390 found = true;
391 assertTrue(format("Intent %s equal %s", removedIntent, existingIntent),
392 !IntentUtils.intentsAreEqual(removedIntent, existingIntent));
393 break;
394 }
395 }
396 assertTrue(!found);
397 }
398
399 }
400
401 @Test
402 public void testRemoveVCbySdx() {
403 testConnectionSetup();
404
405 List<PointToPointIntent> removedIntents = new ArrayList<PointToPointIntent>();
406
407 removedIntents.addAll(setupConnectionPoints1To2());
408 removedIntents.addAll(setupConnectionPoints5To6());
409 removedIntents.addAll(setupConnectionPoints7To8());
410 removedIntents.addAll(setupConnectionPoints9To10());
411 manager.removeVCs(SDXL2_2);
412
413 assertEquals(Collections.emptySet(), manager.getVCs(Optional.ofNullable(null)));
414 assertEquals(Collections.emptySet(), manager.getVCs(Optional.of(SDXL2_2)));
415
416 for (Intent removedIntent : removedIntents) {
417 boolean found = false;
418 for (Intent existingIntent : manager.intentService.getIntents()) {
419 if (removedIntent.key().equals(existingIntent.key())) {
420 found = true;
421 assertTrue(format("Intent %s equal %s", removedIntent, existingIntent),
422 !IntentUtils.intentsAreEqual(removedIntent, existingIntent));
423 break;
424 }
425 }
426 assertTrue(!found);
427 }
428
429 }
430}