Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 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 | package org.onosproject.vpls; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
DScano | 55a0103 | 2020-04-09 20:01:50 +0200 | [diff] [blame^] | 22 | import org.onosproject.codec.CodecService; |
| 23 | import org.onosproject.codec.JsonCodec; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 24 | import org.onosproject.net.EncapsulationType; |
| 25 | import org.onosproject.net.host.HostEvent; |
| 26 | import org.onosproject.vpls.api.VplsData; |
| 27 | import org.onosproject.vpls.api.VplsOperation; |
| 28 | import org.onosproject.vpls.api.VplsOperationService; |
| 29 | import org.onosproject.vpls.store.VplsStoreEvent; |
| 30 | |
| 31 | import java.util.Collection; |
DScano | 55a0103 | 2020-04-09 20:01:50 +0200 | [diff] [blame^] | 32 | import java.util.Set; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 33 | |
| 34 | import static org.junit.Assert.*; |
| 35 | import static org.onosproject.net.EncapsulationType.*; |
| 36 | import static org.onosproject.vpls.api.VplsData.VplsState.*; |
| 37 | |
| 38 | /** |
Yuta HIGUCHI | 9312a80 | 2017-06-12 20:01:27 -0700 | [diff] [blame] | 39 | * Test for {@link VplsManager}. |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 40 | */ |
| 41 | public class VplsManagerTest extends VplsTest { |
| 42 | private VplsManager vplsManager; |
DScano | 55a0103 | 2020-04-09 20:01:50 +0200 | [diff] [blame^] | 43 | private CodecService codecService = new TestCodecService(); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 44 | private TestVplsStore vplsStore = new TestVplsStore(); |
| 45 | private TestHostService hostService = new TestHostService(); |
| 46 | private TestInterfaceService interfaceService = new TestInterfaceService(); |
| 47 | private TestVplsOperationService vplsOperationService = new TestVplsOperationService(); |
| 48 | |
| 49 | @Before |
| 50 | public void setup() { |
| 51 | vplsManager = new VplsManager(); |
DScano | 55a0103 | 2020-04-09 20:01:50 +0200 | [diff] [blame^] | 52 | vplsManager.codecService = codecService; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 53 | vplsManager.hostService = hostService; |
| 54 | vplsManager.vplsStore = vplsStore; |
| 55 | vplsManager.operationService = vplsOperationService; |
| 56 | vplsManager.interfaceService = interfaceService; |
| 57 | vplsStore.clear(); |
| 58 | vplsOperationService.clear(); |
| 59 | vplsManager.activate(); |
| 60 | } |
| 61 | |
| 62 | @After |
| 63 | public void tearDown() { |
| 64 | vplsManager.deactivate(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Creates VPLS by given name and encapsulation type. |
| 69 | */ |
| 70 | @Test |
| 71 | public void testCreateVpls() { |
| 72 | VplsData vplsData = vplsManager.createVpls(VPLS1, NONE); |
| 73 | assertEquals(VPLS1, vplsData.name()); |
| 74 | assertEquals(NONE, vplsData.encapsulationType()); |
| 75 | |
| 76 | vplsData = vplsStore.getVpls(VPLS1); |
| 77 | assertEquals(vplsData.state(), ADDING); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Gets VPLS by VPLS name. |
| 82 | */ |
| 83 | @Test |
| 84 | public void testGetVpls() { |
| 85 | VplsData vplsData = VplsData.of(VPLS1); |
| 86 | vplsData.state(ADDED); |
| 87 | vplsStore.addVpls(vplsData); |
| 88 | |
| 89 | VplsData result = vplsManager.getVpls(VPLS1); |
| 90 | assertEquals(vplsData, result); |
| 91 | |
| 92 | result = vplsManager.getVpls(VPLS2); |
| 93 | assertNull(result); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Removes a VPLS. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testRemoveVpls() { |
| 101 | VplsData vplsData = vplsManager.createVpls(VPLS1, NONE); |
| 102 | vplsManager.removeVpls(vplsData); |
| 103 | assertEquals(vplsData.state(), REMOVING); |
| 104 | vplsData = vplsStore.getVpls(VPLS1); |
| 105 | assertNull(vplsData); |
| 106 | Collection<VplsData> allVpls = vplsStore.getAllVpls(); |
| 107 | assertEquals(0, allVpls.size()); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Removes all VPLS. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testRemoveAllVpls() { |
| 115 | VplsData vplsData = VplsData.of(VPLS1); |
| 116 | vplsData.state(ADDED); |
| 117 | vplsStore.addVpls(vplsData); |
| 118 | |
| 119 | vplsData = VplsData.of(VPLS2, VLAN); |
| 120 | vplsData.state(ADDED); |
| 121 | vplsStore.addVpls(vplsData); |
| 122 | |
| 123 | vplsManager.removeAllVpls(); |
| 124 | assertEquals(0, vplsStore.getAllVpls().size()); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Adds network interfaces one by one to a VPLS. |
| 129 | */ |
| 130 | @Test |
| 131 | public void testAddInterface() { |
| 132 | VplsData vplsData = vplsManager.createVpls(VPLS1, NONE); |
| 133 | vplsManager.addInterface(vplsData, V100H1); |
| 134 | vplsManager.addInterface(vplsData, V100H2); |
| 135 | vplsData = vplsStore.getVpls(VPLS1); |
| 136 | assertNotNull(vplsData); |
| 137 | assertEquals(vplsData.state(), UPDATING); |
| 138 | assertEquals(2, vplsData.interfaces().size()); |
| 139 | assertTrue(vplsData.interfaces().contains(V100H1)); |
| 140 | assertTrue(vplsData.interfaces().contains(V100H2)); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Adds network interfaces to a VPLS. |
| 145 | */ |
| 146 | @Test |
| 147 | public void testAddInterfaces() { |
| 148 | VplsData vplsData = vplsManager.createVpls(VPLS1, NONE); |
| 149 | vplsManager.addInterfaces(vplsData, ImmutableSet.of(V100H1, V100H2)); |
| 150 | vplsData = vplsStore.getVpls(VPLS1); |
| 151 | assertNotNull(vplsData); |
| 152 | assertEquals(vplsData.state(), UPDATING); |
| 153 | assertEquals(2, vplsData.interfaces().size()); |
| 154 | assertTrue(vplsData.interfaces().contains(V100H1)); |
| 155 | assertTrue(vplsData.interfaces().contains(V100H2)); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Removes network interfaces one by one from a VPLS. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testRemoveInterface() { |
| 163 | VplsData vplsData = vplsManager.createVpls(VPLS1, NONE); |
| 164 | vplsManager.addInterface(vplsData, V100H1); |
| 165 | vplsManager.addInterface(vplsData, V100H2); |
| 166 | vplsManager.removeInterface(vplsData, V100H1); |
| 167 | vplsData = vplsStore.getVpls(VPLS1); |
| 168 | assertNotNull(vplsData); |
| 169 | assertEquals(vplsData.state(), UPDATING); |
| 170 | assertEquals(1, vplsData.interfaces().size()); |
| 171 | assertTrue(vplsData.interfaces().contains(V100H2)); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Removes network interfaces from a VPLS. |
| 176 | */ |
| 177 | @Test |
| 178 | public void testRemoveInterfaces() { |
| 179 | VplsData vplsData = vplsManager.createVpls(VPLS1, NONE); |
| 180 | vplsManager.addInterface(vplsData, V100H1); |
| 181 | vplsManager.addInterface(vplsData, V100H2); |
| 182 | vplsManager.removeInterfaces(vplsData, ImmutableSet.of(V100H1, V100H2)); |
| 183 | vplsData = vplsStore.getVpls(VPLS1); |
| 184 | assertNotNull(vplsData); |
| 185 | assertEquals(vplsData.state(), UPDATING); |
| 186 | assertEquals(0, vplsData.interfaces().size()); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Sets encapsulation type for a VPLS. |
| 191 | */ |
| 192 | @Test |
| 193 | public void testSetEncapsulationType() { |
| 194 | VplsData vplsData = vplsManager.createVpls(VPLS1, NONE); |
| 195 | vplsManager.setEncapsulationType(vplsData, EncapsulationType.VLAN); |
| 196 | vplsData = vplsStore.getVpls(VPLS1); |
| 197 | assertNotNull(vplsData); |
| 198 | assertEquals(vplsData.state(), UPDATING); |
| 199 | assertEquals(vplsData.encapsulationType(), EncapsulationType.VLAN); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Adds hosts to a VPLS. |
| 204 | */ |
| 205 | @Test |
| 206 | public void testAddHost() { |
| 207 | VplsData vplsData = VplsData.of(VPLS1, NONE); |
| 208 | vplsData.addInterface(V100H1); |
| 209 | vplsData.state(ADDED); |
| 210 | vplsStore.addVpls(vplsData); |
| 211 | |
| 212 | HostEvent hostEvent = new HostEvent(HostEvent.Type.HOST_ADDED, V100HOST1); |
| 213 | hostService.postHostEvent(hostEvent); |
| 214 | |
| 215 | vplsData = vplsStore.getVpls(VPLS1); |
| 216 | assertNotNull(vplsData); |
| 217 | |
| 218 | assertEquals(vplsData.state(), UPDATING); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Removes hosts from a VPLS. |
| 223 | */ |
| 224 | @Test |
| 225 | public void testRemoveHost() { |
| 226 | VplsData vplsData = VplsData.of(VPLS1, NONE); |
| 227 | vplsData.addInterface(V100H1); |
| 228 | vplsData.state(ADDED); |
| 229 | vplsStore.addVpls(vplsData); |
| 230 | |
| 231 | HostEvent hostEvent = new HostEvent(HostEvent.Type.HOST_REMOVED, V100HOST1); |
| 232 | hostService.postHostEvent(hostEvent); |
| 233 | vplsData = vplsStore.getVpls(VPLS1); |
| 234 | assertNotNull(vplsData); |
| 235 | |
| 236 | assertEquals(vplsData.state(), UPDATING); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Pass different VPLS store event to store delegate. |
| 241 | * Include these cases: |
| 242 | * <ul> |
| 243 | * <li>VPLS added</li> |
| 244 | * <li>VPLS updated</li> |
| 245 | * <li>VPLS state updated</li> |
| 246 | * <li>VPLS removed</li> |
| 247 | * </ul> |
| 248 | */ |
| 249 | @Test |
| 250 | public void testStoreDelegate() { |
| 251 | // Add |
| 252 | VplsData vplsData = vplsManager.createVpls(VPLS1, NONE); |
| 253 | VplsStoreEvent event = new VplsStoreEvent(VplsStoreEvent.Type.ADD, vplsData); |
| 254 | vplsStore.delegate().notify(event); |
| 255 | |
| 256 | VplsOperation vplsOperation = vplsOperationService.operation(); |
| 257 | assertEquals(vplsOperation.op(), VplsOperation.Operation.ADD); |
| 258 | assertEquals(vplsOperation.vpls(), vplsData); |
| 259 | vplsOperationService.clear(); |
| 260 | |
| 261 | // Update info |
| 262 | vplsData.encapsulationType(EncapsulationType.VLAN); |
| 263 | vplsData.state(UPDATING); |
| 264 | event = new VplsStoreEvent(VplsStoreEvent.Type.UPDATE, vplsData); |
| 265 | vplsStore.delegate().notify(event); |
| 266 | vplsOperation = vplsOperationService.operation(); |
| 267 | assertEquals(vplsOperation.op(), VplsOperation.Operation.UPDATE); |
| 268 | assertEquals(vplsOperation.vpls(), vplsData); |
| 269 | vplsOperationService.clear(); |
| 270 | |
| 271 | // Update state (no operation) |
| 272 | vplsData.state(VplsData.VplsState.ADDED); |
| 273 | event = new VplsStoreEvent(VplsStoreEvent.Type.UPDATE, vplsData); |
| 274 | vplsStore.delegate().notify(event); |
| 275 | vplsOperation = vplsOperationService.operation(); |
| 276 | assertNull(vplsOperation); |
| 277 | vplsOperationService.clear(); |
| 278 | |
| 279 | // Remove |
| 280 | event = new VplsStoreEvent(VplsStoreEvent.Type.REMOVE, vplsData); |
| 281 | vplsStore.delegate().notify(event); |
| 282 | vplsOperation = vplsOperationService.operation(); |
| 283 | assertEquals(vplsOperation.op(), VplsOperation.Operation.REMOVE); |
| 284 | assertEquals(vplsOperation.vpls(), vplsData); |
| 285 | vplsOperationService.clear(); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Trigger host event listener by HOST_ADDED event. |
| 290 | */ |
| 291 | @Test |
| 292 | public void hostAddEventTest() { |
| 293 | VplsData vplsData = vplsManager.createVpls(VPLS1, NONE); |
| 294 | vplsManager.addInterface(vplsData, V100H1); |
| 295 | HostEvent hostEvent = new HostEvent(HostEvent.Type.HOST_ADDED, V100HOST1); |
| 296 | hostService.postHostEvent(hostEvent); |
| 297 | |
| 298 | vplsData = vplsStore.getVpls(VPLS1); |
| 299 | assertEquals(UPDATING, vplsData.state()); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Trigger host event listener by HOST_REMOVED event. |
| 304 | */ |
| 305 | @Test |
| 306 | public void hostRemoveEventTest() { |
| 307 | VplsData vplsData = vplsManager.createVpls(VPLS1, NONE); |
| 308 | vplsManager.addInterface(vplsData, V100H1); |
| 309 | HostEvent hostEvent = new HostEvent(HostEvent.Type.HOST_REMOVED, V100HOST1); |
| 310 | hostService.postHostEvent(hostEvent); |
| 311 | |
| 312 | vplsData = vplsStore.getVpls(VPLS1); |
| 313 | assertEquals(UPDATING, vplsData.state()); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Test VPLS operation service. |
| 318 | * Stores last operation submitted by VPLS manager. |
| 319 | */ |
| 320 | class TestVplsOperationService implements VplsOperationService { |
| 321 | VplsOperation operation; |
| 322 | |
| 323 | @Override |
| 324 | public void submit(VplsOperation vplsOperation) { |
| 325 | this.operation = vplsOperation; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Clears the VPLS operation. |
| 330 | */ |
| 331 | public void clear() { |
| 332 | operation = null; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Gets the latest VPLS operation. |
| 337 | * @return the latest VPLS operation. |
| 338 | */ |
| 339 | public VplsOperation operation() { |
| 340 | return operation; |
| 341 | } |
| 342 | } |
| 343 | |
DScano | 55a0103 | 2020-04-09 20:01:50 +0200 | [diff] [blame^] | 344 | /** |
| 345 | * Test Codec service. |
| 346 | */ |
| 347 | class TestCodecService implements CodecService { |
| 348 | |
| 349 | |
| 350 | @Override |
| 351 | public Set<Class<?>> getCodecs() { |
| 352 | return null; |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public <T> JsonCodec<T> getCodec(Class<T> entityClass) { |
| 357 | return null; |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) { |
| 362 | |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void unregisterCodec(Class<?> entityClass) { |
| 367 | |
| 368 | } |
| 369 | } |
| 370 | |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 371 | } |