Carolina Fernandez | ad89343 | 2016-07-18 11:11:34 +0200 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.sdxl2; |
| 18 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Rule; |
| 23 | import org.junit.Test; |
| 24 | import org.junit.rules.ExpectedException; |
| 25 | import org.onosproject.TestApplicationId; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.IdGenerator; |
| 28 | import org.onosproject.net.intent.Intent; |
| 29 | import org.onosproject.net.intent.Key; |
| 30 | import org.onosproject.net.intent.MockIdGenerator; |
| 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.Collections; |
| 34 | import java.util.Iterator; |
| 35 | import java.util.List; |
| 36 | import java.util.Optional; |
| 37 | import java.util.Set; |
| 38 | |
| 39 | import static java.lang.String.format; |
| 40 | import static org.junit.Assert.assertEquals; |
| 41 | import static org.junit.Assert.assertNotEquals; |
| 42 | import static org.junit.Assert.assertNull; |
| 43 | |
| 44 | |
| 45 | public class SdxL2VCManagerTest { |
| 46 | |
| 47 | private static final String SDXL2_1 = "sdxl2_test1"; |
| 48 | private static final String CP1 = "of:00000000000001/1"; |
| 49 | private static final String CP2 = "of:00000000000002/1"; |
| 50 | private static final String VLANS1 = "2,3,4"; |
| 51 | private static final String VLANS2 = "4,5,6"; |
| 52 | private static final String CEMAC1 = "52:40:00:12:44:01"; |
| 53 | private static final String CEMAC2 = "51:12:11:00:23:01"; |
| 54 | private static final String CP3 = "of:00000000000002/2"; |
| 55 | private static final String VLANS3 = "8,9,10"; |
| 56 | private static final String CEMAC3 = "52:12:11:00:23:01"; |
| 57 | private static final String SDXL2_2 = "sdxl2_test2"; |
| 58 | private static final String CP5 = "of:00000000000002/4"; |
| 59 | private static final String VLANS5 = "100"; |
| 60 | private static final String CEMAC5 = "52:12:11:00:23:11"; |
| 61 | private static final String CP6 = "of:00000000000004/4"; |
| 62 | private static final String VLANS6 = "1"; |
| 63 | private static final String CEMAC6 = "52:12:11:a0:23:11"; |
| 64 | private static final String CP7 = "of:0000000000000a/4"; |
| 65 | private static final String VLANS7 = "1"; |
| 66 | private static final String CEMAC7 = "52:12:21:00:25:11"; |
| 67 | private static final String CP8 = "of:00000000000009/4"; |
| 68 | private static final String VLANS8 = "111"; |
| 69 | private static final String CEMAC8 = "52:12:14:a0:23:11"; |
| 70 | private static final String CP9 = "of:0000000000000a/4"; |
| 71 | private static final String VLANS9 = "1"; |
| 72 | private static final String CEMAC9 = "52:12:21:00:28:11"; |
| 73 | private static final String CP10 = "of:00000000000009/4"; |
| 74 | private static final String VLANS10 = "1"; |
| 75 | private static final String CEMAC10 = "52:12:14:aa:23:11"; |
| 76 | private static final ApplicationId APPID = TestApplicationId.create("foo"); |
| 77 | @Rule |
| 78 | public ExpectedException exceptionAddVC = ExpectedException.none(); |
| 79 | @Rule |
| 80 | public ExpectedException exceptionRemoveVC = ExpectedException.none(); |
| 81 | @Rule |
| 82 | public ExpectedException exceptionGetVC = ExpectedException.none(); |
| 83 | private SdxL2MacVCManager manager; |
| 84 | private IdGenerator idGenerator = new MockIdGenerator(); |
| 85 | |
| 86 | /** |
| 87 | * Prepare environment before starting testing VCs. |
| 88 | */ |
| 89 | @Before |
| 90 | public void setUp() { |
| 91 | SdxL2DistributedStore store = new SdxL2DistributedStore(); |
| 92 | store.initForTest(); |
| 93 | manager = new SdxL2MacVCManager( |
| 94 | APPID, store, new IntentServiceTest()); |
| 95 | Intent.bindIdGenerator(idGenerator); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Clean up environment after finishing testing VCs. |
| 100 | */ |
| 101 | @After |
| 102 | public void tearDown() { |
| 103 | Intent.unbindIdGenerator(idGenerator); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testgenerateKey() { |
| 108 | SdxL2ConnectionPoint cpOne = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 109 | SdxL2ConnectionPoint cpOneAux = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC1); |
| 110 | |
| 111 | SdxL2ConnectionPoint cpTwo = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC1); |
| 112 | SdxL2ConnectionPoint cpTwoAux = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 113 | |
| 114 | Key key1 = manager.generateIntentKey(SDXL2_1, cpOne, cpTwo, "1"); |
| 115 | Key key2 = manager.generateIntentKey(SDXL2_1, cpOneAux, cpTwoAux, "1"); |
| 116 | assertNotEquals(key1, key2); |
| 117 | |
| 118 | Key key3 = manager.generateIntentKey(SDXL2_1, cpOne, cpTwoAux, "1"); |
| 119 | Key key4 = manager.generateIntentKey(SDXL2_1, cpOneAux, cpTwo, "1"); |
| 120 | assertNotEquals(key3, key4); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testAddVCChecks() { |
| 125 | |
| 126 | SdxL2ConnectionPoint cpFive = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 127 | SdxL2ConnectionPoint cpSix = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC2); |
| 128 | manager.addVC(SDXL2_1, cpFive, cpSix); |
| 129 | |
| 130 | SdxL2ConnectionPoint cpSeven = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC2); |
| 131 | SdxL2ConnectionPoint cpEight = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 132 | manager.addVC(SDXL2_1, cpSeven, cpEight); |
| 133 | |
| 134 | SdxL2ConnectionPoint cpNine = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 135 | SdxL2ConnectionPoint cpTen = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST3", CP3, VLANS3, CEMAC3); |
| 136 | manager.addVC(SDXL2_1, cpNine, cpTen); |
| 137 | |
| 138 | exceptionAddVC.expect(IllegalStateException.class); |
| 139 | SdxL2ConnectionPoint cpOne = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 140 | SdxL2ConnectionPoint cpTwo = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC1); |
| 141 | manager.addVC(SDXL2_1, cpOne, cpTwo); |
| 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void testremoveVCChecks() { |
| 146 | SdxL2ConnectionPoint cpOne = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 147 | SdxL2ConnectionPoint cpTwo = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC1); |
| 148 | |
| 149 | SdxL2ConnectionPoint cpFive = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 150 | SdxL2ConnectionPoint cpSix = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC2); |
| 151 | |
| 152 | SdxL2ConnectionPoint cpSeven = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC2); |
| 153 | SdxL2ConnectionPoint cpEight = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 154 | |
| 155 | SdxL2ConnectionPoint cpNine = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 156 | SdxL2ConnectionPoint cpTen = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST3", CP3, VLANS3, CEMAC3); |
| 157 | |
| 158 | manager.addVC(SDXL2_1, cpFive, cpSix); |
| 159 | manager.removeVC(cpFive, cpSix); |
| 160 | manager.removeVC(cpSeven, cpEight); |
| 161 | manager.removeVC(cpNine, cpTen); |
| 162 | |
| 163 | exceptionRemoveVC.expect(IllegalStateException.class); |
| 164 | manager.removeVC(cpOne, cpTwo); |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void testGetVC() { |
| 169 | connectionSetup(); |
| 170 | |
| 171 | Iterator<SdxL2ConnectionPoint> lhs = setupLhsCPs().iterator(); |
| 172 | Iterator<SdxL2ConnectionPoint> rhs = setupRhsCPs().iterator(); |
| 173 | String vc; |
| 174 | SdxL2ConnectionPoint one; |
| 175 | SdxL2ConnectionPoint two; |
| 176 | while (lhs.hasNext()) { |
| 177 | one = lhs.next(); |
| 178 | two = rhs.next(); |
| 179 | vc = one.toString().compareTo(two.toString()) < 0 ? |
| 180 | format(SdxL2VCManager.SDXL2_CPS_FORMAT, one, two) : |
| 181 | format(SdxL2VCManager.SDXL2_CPS_FORMAT, two, one); |
| 182 | assertEquals(vc, manager.getVC(one, two)); |
| 183 | } |
| 184 | |
| 185 | SdxL2ConnectionPoint cpLeft = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 186 | SdxL2ConnectionPoint cpRight = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP3, VLANS3, CEMAC1); |
| 187 | |
| 188 | // This VC has not been created before |
| 189 | vc = cpLeft.toString().compareTo(cpRight.toString()) < 0 ? |
| 190 | format(SdxL2VCManager.SDXL2_CPS_FORMAT, cpLeft, cpRight) : |
| 191 | format(SdxL2VCManager.SDXL2_CPS_FORMAT, cpRight, cpLeft); |
| 192 | |
| 193 | assertNull(manager.getVC(cpLeft, cpRight)); |
| 194 | } |
| 195 | |
| 196 | @Test |
| 197 | public void testgetVCs() { |
| 198 | connectionSetup(); |
| 199 | Iterator<SdxL2ConnectionPoint> lhs = setupLhsCPs().iterator(); |
| 200 | Iterator<SdxL2ConnectionPoint> rhs = setupRhsCPs().iterator(); |
| 201 | Set<String> expectedVCs = Sets.newHashSet(); |
| 202 | String vc; |
| 203 | String lhsName; |
| 204 | String rhsName; |
| 205 | while (lhs.hasNext()) { |
| 206 | lhsName = lhs.next().name(); |
| 207 | rhsName = rhs.next().name(); |
| 208 | vc = lhsName.compareTo(rhsName.toString()) < 0 ? |
| 209 | format(SdxL2VCManager.NAME_FORMAT, SDXL2_2, lhsName, rhsName) : |
| 210 | format(SdxL2VCManager.NAME_FORMAT, SDXL2_2, rhsName, lhsName); |
| 211 | expectedVCs.add(vc); |
| 212 | } |
| 213 | Set<String> vcs = manager.getVCs(Optional.of(SDXL2_2)); |
| 214 | assertEquals(expectedVCs, vcs); |
| 215 | vcs = manager.getVCs(Optional.of(SDXL2_1)); |
| 216 | assertEquals(Collections.emptySet(), vcs); |
| 217 | vcs = manager.getVCs(Optional.ofNullable(null)); |
| 218 | assertEquals(expectedVCs, vcs); |
| 219 | } |
| 220 | |
| 221 | public List<SdxL2ConnectionPoint> setupLhsCPs() { |
| 222 | List<SdxL2ConnectionPoint> cps = new ArrayList<SdxL2ConnectionPoint>(); |
| 223 | SdxL2ConnectionPoint cpone = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST1", CP1, VLANS1, CEMAC1); |
| 224 | cps.add(cpone); |
| 225 | SdxL2ConnectionPoint cpfive = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST5", CP5, VLANS5, CEMAC5); |
| 226 | cps.add(cpfive); |
| 227 | SdxL2ConnectionPoint cpseven = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST7", CP7, VLANS7, CEMAC7); |
| 228 | cps.add(cpseven); |
| 229 | SdxL2ConnectionPoint cpnine = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST9", CP9, VLANS9, CEMAC9); |
| 230 | cps.add(cpnine); |
| 231 | return cps; |
| 232 | } |
| 233 | |
| 234 | public List<SdxL2ConnectionPoint> setupRhsCPs() { |
| 235 | List<SdxL2ConnectionPoint> cps = new ArrayList<SdxL2ConnectionPoint>(); |
| 236 | SdxL2ConnectionPoint cptwo = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST2", CP2, VLANS2, CEMAC2); |
| 237 | cps.add(cptwo); |
| 238 | SdxL2ConnectionPoint cpsix = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST6", CP6, VLANS6, CEMAC6); |
| 239 | cps.add(cpsix); |
| 240 | SdxL2ConnectionPoint cpeight = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST8", CP8, VLANS8, CEMAC8); |
| 241 | cps.add(cpeight); |
| 242 | SdxL2ConnectionPoint cpten = SdxL2ConnectionPoint.sdxl2ConnectionPoint("TEST10", CP10, VLANS10, CEMAC10); |
| 243 | cps.add(cpten); |
| 244 | return cps; |
| 245 | } |
| 246 | |
| 247 | private void connectionSetup() { |
| 248 | Iterator<SdxL2ConnectionPoint> lhs = setupLhsCPs().iterator(); |
| 249 | Iterator<SdxL2ConnectionPoint> rhs = setupRhsCPs().iterator(); |
| 250 | while (lhs.hasNext()) { |
| 251 | manager.addVC(SDXL2_2, lhs.next(), rhs.next()); |
| 252 | } |
| 253 | } |
| 254 | } |