yoonseon | dc3210d | 2017-01-25 16:03:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.incubator.net.virtual.impl; |
| 18 | |
| 19 | import com.google.common.collect.Iterables; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.junit.TestUtils; |
| 24 | import org.onlab.osgi.ServiceDirectory; |
| 25 | import org.onlab.osgi.TestServiceDirectory; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.MplsLabel; |
| 28 | import org.onosproject.TestApplicationId; |
| 29 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
| 32 | import org.onosproject.core.DefaultGroupId; |
| 33 | import org.onosproject.core.GroupId; |
| 34 | import org.onosproject.event.EventDeliveryService; |
| 35 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 36 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 37 | import org.onosproject.incubator.net.virtual.VirtualNetworkGroupStore; |
| 38 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
| 39 | import org.onosproject.incubator.net.virtual.event.VirtualEvent; |
| 40 | import org.onosproject.incubator.net.virtual.event.VirtualListenerRegistryManager; |
| 41 | import org.onosproject.incubator.net.virtual.impl.provider.VirtualProviderManager; |
| 42 | import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProvider; |
| 43 | import org.onosproject.incubator.net.virtual.provider.VirtualGroupProvider; |
| 44 | import org.onosproject.incubator.net.virtual.provider.VirtualGroupProviderService; |
| 45 | import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService; |
| 46 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
| 47 | import org.onosproject.incubator.store.virtual.impl.SimpleVirtualGroupStore; |
| 48 | import org.onosproject.net.DeviceId; |
| 49 | import org.onosproject.net.PortNumber; |
| 50 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 51 | import org.onosproject.net.flow.TrafficTreatment; |
| 52 | import org.onosproject.net.group.DefaultGroup; |
| 53 | import org.onosproject.net.group.DefaultGroupBucket; |
| 54 | import org.onosproject.net.group.DefaultGroupDescription; |
| 55 | import org.onosproject.net.group.DefaultGroupKey; |
| 56 | import org.onosproject.net.group.Group; |
| 57 | import org.onosproject.net.group.GroupBucket; |
| 58 | import org.onosproject.net.group.GroupBuckets; |
| 59 | import org.onosproject.net.group.GroupDescription; |
| 60 | import org.onosproject.net.group.GroupEvent; |
| 61 | import org.onosproject.net.group.GroupKey; |
| 62 | import org.onosproject.net.group.GroupListener; |
| 63 | import org.onosproject.net.group.GroupOperation; |
| 64 | import org.onosproject.net.group.GroupOperations; |
| 65 | import org.onosproject.net.group.StoredGroupEntry; |
| 66 | import org.onosproject.net.intent.FakeIntentManager; |
| 67 | import org.onosproject.net.intent.TestableIntentService; |
| 68 | import org.onosproject.net.provider.ProviderId; |
| 69 | import org.onosproject.store.service.TestStorageService; |
| 70 | |
| 71 | import java.util.ArrayList; |
| 72 | import java.util.Arrays; |
| 73 | import java.util.Collections; |
| 74 | import java.util.List; |
| 75 | |
| 76 | import static org.junit.Assert.*; |
| 77 | import static org.onosproject.incubator.net.virtual.impl.VirtualNetworkTestUtil.*; |
| 78 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
| 79 | |
| 80 | /** |
| 81 | * Test codifying the virtual group service & group provider service contracts. |
| 82 | */ |
| 83 | public class VirtualNetworkGroupManagerTest { |
| 84 | |
| 85 | private VirtualNetworkManager manager; |
| 86 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
| 87 | private TestableIntentService intentService = new FakeIntentManager(); |
| 88 | private ServiceDirectory testDirectory; |
| 89 | private VirtualProviderManager providerRegistryService; |
| 90 | |
| 91 | private EventDeliveryService eventDeliveryService; |
| 92 | VirtualListenerRegistryManager listenerRegistryManager = |
| 93 | VirtualListenerRegistryManager.getInstance(); |
| 94 | |
| 95 | private VirtualNetworkGroupManager groupManager1; |
| 96 | private VirtualNetworkGroupManager groupManager2; |
| 97 | |
| 98 | private VirtualNetworkGroupStore groupStore; |
| 99 | |
| 100 | private TestGroupProvider provider = new TestGroupProvider(); |
| 101 | private VirtualGroupProviderService providerService1; |
| 102 | private VirtualGroupProviderService providerService2; |
| 103 | |
| 104 | protected TestGroupListener listener1 = new TestGroupListener(); |
| 105 | protected TestGroupListener listener2 = new TestGroupListener(); |
| 106 | |
| 107 | private VirtualNetwork vnet1; |
| 108 | private VirtualNetwork vnet2; |
| 109 | |
| 110 | private ApplicationId appId; |
| 111 | |
| 112 | @Before |
| 113 | public void setUp() throws Exception { |
| 114 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
| 115 | |
| 116 | CoreService coreService = new TestCoreService(); |
| 117 | TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService); |
| 118 | TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); |
| 119 | virtualNetworkManagerStore.activate(); |
| 120 | |
| 121 | groupStore = new SimpleVirtualGroupStore(); |
| 122 | |
| 123 | providerRegistryService = new VirtualProviderManager(); |
| 124 | providerRegistryService.registerProvider(provider); |
| 125 | |
| 126 | manager = new VirtualNetworkManager(); |
| 127 | manager.store = virtualNetworkManagerStore; |
| 128 | manager.intentService = intentService; |
| 129 | TestUtils.setField(manager, "coreService", coreService); |
| 130 | |
| 131 | eventDeliveryService = new TestEventDispatcher(); |
| 132 | injectEventDispatcher(manager, eventDeliveryService); |
| 133 | eventDeliveryService.addSink(VirtualEvent.class, listenerRegistryManager); |
| 134 | |
| 135 | appId = new TestApplicationId("VirtualGroupManagerTest"); |
| 136 | |
| 137 | testDirectory = new TestServiceDirectory() |
| 138 | .add(VirtualNetworkStore.class, virtualNetworkManagerStore) |
| 139 | .add(CoreService.class, coreService) |
| 140 | .add(VirtualProviderRegistryService.class, providerRegistryService) |
| 141 | .add(EventDeliveryService.class, eventDeliveryService) |
| 142 | .add(VirtualNetworkGroupStore.class, groupStore); |
| 143 | TestUtils.setField(manager, "serviceDirectory", testDirectory); |
| 144 | |
| 145 | manager.activate(); |
| 146 | |
| 147 | vnet1 = setupVirtualNetworkTopology(manager, TID1); |
| 148 | vnet2 = setupVirtualNetworkTopology(manager, TID2); |
| 149 | |
| 150 | groupManager1 = new VirtualNetworkGroupManager(manager, vnet1.id()); |
| 151 | groupManager2 = new VirtualNetworkGroupManager(manager, vnet2.id()); |
| 152 | groupManager1.addListener(listener1); |
| 153 | groupManager2.addListener(listener2); |
| 154 | |
| 155 | providerService1 = (VirtualGroupProviderService) |
| 156 | providerRegistryService.getProviderService(vnet1.id(), |
| 157 | VirtualGroupProvider.class); |
| 158 | providerService2 = (VirtualGroupProviderService) |
| 159 | providerRegistryService.getProviderService(vnet2.id(), |
| 160 | VirtualGroupProvider.class); |
| 161 | } |
| 162 | |
| 163 | @After |
| 164 | public void tearDown() { |
| 165 | providerRegistryService.unregisterProvider(provider); |
| 166 | assertFalse("provider should not be registered", |
| 167 | providerRegistryService.getProviders().contains(provider.id())); |
| 168 | groupManager1.removeListener(listener1); |
| 169 | groupManager2.removeListener(listener2); |
| 170 | |
| 171 | manager.deactivate(); |
| 172 | virtualNetworkManagerStore.deactivate(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Tests group creation before the device group AUDIT completes. |
| 177 | */ |
| 178 | @Test |
| 179 | public void testGroupServiceBasics() { |
| 180 | // Test Group creation before AUDIT process |
| 181 | testGroupCreationBeforeAudit(vnet1.id(), VDID1); |
| 182 | testGroupCreationBeforeAudit(vnet2.id(), VDID1); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Tests initial device group AUDIT process. |
| 187 | */ |
| 188 | @Test |
| 189 | public void testGroupServiceInitialAudit() { |
| 190 | // Test Group creation before AUDIT process |
| 191 | testGroupCreationBeforeAudit(vnet1.id(), VDID1); |
| 192 | testGroupCreationBeforeAudit(vnet2.id(), VDID1); |
| 193 | // Test initial group audit process |
| 194 | testInitialAuditWithPendingGroupRequests(vnet1.id(), VDID1); |
| 195 | testInitialAuditWithPendingGroupRequests(vnet2.id(), VDID1); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Tests deletion process of any extraneous groups. |
| 200 | */ |
| 201 | @Test |
| 202 | public void testGroupServiceAuditExtraneous() { |
| 203 | // Test Group creation before AUDIT process |
| 204 | testGroupCreationBeforeAudit(vnet1.id(), VDID1); |
| 205 | testGroupCreationBeforeAudit(vnet2.id(), VDID1); |
| 206 | |
| 207 | // Test audit with extraneous and missing groups |
| 208 | testAuditWithExtraneousMissingGroups(vnet1.id(), VDID1); |
| 209 | testAuditWithExtraneousMissingGroups(vnet2.id(), VDID1); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Tests re-apply process of any missing groups tests execution of |
| 214 | * any pending group creation request after the device group AUDIT completes |
| 215 | * and tests event notifications after receiving confirmation for any |
| 216 | * operations from data plane. |
| 217 | */ |
| 218 | @Test |
| 219 | public void testGroupServiceAuditConfirmed() { |
| 220 | // Test Group creation before AUDIT process |
| 221 | testGroupCreationBeforeAudit(vnet1.id(), VDID1); |
| 222 | testGroupCreationBeforeAudit(vnet2.id(), VDID1); |
| 223 | |
| 224 | // Test audit with extraneous and missing groups |
| 225 | testAuditWithExtraneousMissingGroups(vnet1.id(), VDID1); |
| 226 | testAuditWithExtraneousMissingGroups(vnet2.id(), VDID1); |
| 227 | |
| 228 | // Test audit with confirmed groups |
| 229 | testAuditWithConfirmedGroups(vnet1.id(), VDID1); |
| 230 | testAuditWithConfirmedGroups(vnet2.id(), VDID1); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Tests group Purge Operation. |
| 235 | */ |
| 236 | @Test |
| 237 | public void testPurgeGroups() { |
| 238 | // Tests for virtual network 1 |
| 239 | // Test Group creation before AUDIT process |
| 240 | testGroupCreationBeforeAudit(vnet1.id(), VDID1); |
| 241 | testAuditWithExtraneousMissingGroups(vnet1.id(), VDID1); |
| 242 | // Test group add bucket operations |
| 243 | testAddBuckets(vnet1.id(), VDID1); |
| 244 | // Test group Purge operations |
| 245 | testPurgeGroupEntry(vnet1.id(), VDID1); |
| 246 | |
| 247 | // Tests for virtual network 2 |
| 248 | // Test Group creation before AUDIT process |
| 249 | testGroupCreationBeforeAudit(vnet2.id(), VDID1); |
| 250 | testAuditWithExtraneousMissingGroups(vnet2.id(), VDID1); |
| 251 | // Test group add bucket operations |
| 252 | testAddBuckets(vnet2.id(), VDID1); |
| 253 | // Test group Purge operations |
| 254 | testPurgeGroupEntry(vnet2.id(), VDID1); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Tests group bucket modifications (additions and deletions) and |
| 259 | * Tests group deletion. |
| 260 | */ |
| 261 | @Test |
| 262 | public void testGroupServiceBuckets() { |
| 263 | // Tests for virtual network 1 |
| 264 | // Test Group creation before AUDIT process |
| 265 | testGroupCreationBeforeAudit(vnet1.id(), VDID1); |
| 266 | |
| 267 | testAuditWithExtraneousMissingGroups(vnet1.id(), VDID1); |
| 268 | // Test group add bucket operations |
| 269 | testAddBuckets(vnet1.id(), VDID1); |
| 270 | |
| 271 | // Test group remove bucket operations |
| 272 | testRemoveBuckets(vnet1.id(), VDID1); |
| 273 | |
| 274 | // Test group remove operations |
| 275 | testRemoveGroup(vnet1.id(), VDID1); |
| 276 | |
| 277 | // Tests for virtual network 2 |
| 278 | // Test Group creation before AUDIT process |
| 279 | testGroupCreationBeforeAudit(vnet2.id(), VDID1); |
| 280 | |
| 281 | testAuditWithExtraneousMissingGroups(vnet2.id(), VDID1); |
| 282 | // Test group add bucket operations |
| 283 | testAddBuckets(vnet2.id(), VDID1); |
| 284 | |
| 285 | // Test group remove bucket operations |
| 286 | testRemoveBuckets(vnet2.id(), VDID1); |
| 287 | |
| 288 | // Test group remove operations |
| 289 | testRemoveGroup(vnet2.id(), VDID1); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Tests group creation before the device group AUDIT completes with fallback |
| 294 | * provider. |
| 295 | */ |
| 296 | @Test |
| 297 | public void testGroupServiceFallbackBasics() { |
| 298 | // Test Group creation before AUDIT process |
| 299 | testGroupCreationBeforeAudit(vnet1.id(), VDID2); |
| 300 | testGroupCreationBeforeAudit(vnet2.id(), VDID2); |
| 301 | } |
| 302 | |
| 303 | // Test Group creation before AUDIT process |
| 304 | private void testGroupCreationBeforeAudit(NetworkId networkId, DeviceId deviceId) { |
| 305 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
| 306 | PortNumber.portNumber(32)}; |
| 307 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
| 308 | PortNumber.portNumber(42)}; |
| 309 | GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
| 310 | List<GroupBucket> buckets = new ArrayList<>(); |
| 311 | List<PortNumber> outPorts = new ArrayList<>(); |
| 312 | outPorts.addAll(Arrays.asList(ports1)); |
| 313 | outPorts.addAll(Arrays.asList(ports2)); |
| 314 | for (PortNumber portNumber : outPorts) { |
| 315 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 316 | tBuilder.setOutput(portNumber) |
| 317 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 318 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 319 | .pushMpls() |
| 320 | .setMpls(MplsLabel.mplsLabel(106)); |
| 321 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 322 | tBuilder.build())); |
| 323 | } |
| 324 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 325 | GroupDescription newGroupDesc = new DefaultGroupDescription(deviceId, |
| 326 | Group.Type.SELECT, |
| 327 | groupBuckets, |
| 328 | key, |
| 329 | null, |
| 330 | appId); |
| 331 | VirtualNetworkGroupManager groupManager; |
| 332 | if (networkId.id() == 1) { |
| 333 | groupManager = groupManager1; |
| 334 | } else { |
| 335 | groupManager = groupManager2; |
| 336 | } |
| 337 | |
| 338 | groupManager.addGroup(newGroupDesc); |
| 339 | assertEquals(null, groupManager.getGroup(deviceId, key)); |
| 340 | assertEquals(0, Iterables.size(groupManager.getGroups(deviceId, appId))); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | // Test initial AUDIT process with pending group requests |
| 345 | private void testInitialAuditWithPendingGroupRequests(NetworkId networkId, |
| 346 | DeviceId deviceId) { |
| 347 | VirtualNetworkGroupManager groupManager; |
| 348 | VirtualGroupProviderService providerService; |
| 349 | if (networkId.id() == 1) { |
| 350 | groupManager = groupManager1; |
| 351 | providerService = providerService1; |
| 352 | } else { |
| 353 | groupManager = groupManager2; |
| 354 | providerService = providerService2; |
| 355 | } |
| 356 | |
| 357 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
| 358 | PortNumber.portNumber(32)}; |
| 359 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
| 360 | PortNumber.portNumber(42)}; |
| 361 | GroupId gId1 = new DefaultGroupId(1); |
| 362 | Group group1 = createSouthboundGroupEntry(gId1, |
| 363 | Arrays.asList(ports1), |
| 364 | 0, deviceId); |
| 365 | GroupId gId2 = new DefaultGroupId(2); |
| 366 | // Non zero reference count will make the group manager to queue |
| 367 | // the extraneous groups until reference count is zero. |
| 368 | Group group2 = createSouthboundGroupEntry(gId2, |
| 369 | Arrays.asList(ports2), |
| 370 | 2, deviceId); |
| 371 | List<Group> groupEntries = Arrays.asList(group1, group2); |
| 372 | providerService.pushGroupMetrics(deviceId, groupEntries); |
| 373 | // First group metrics would trigger the device audit completion |
| 374 | // post which all pending group requests are also executed. |
| 375 | GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
| 376 | Group createdGroup = groupManager.getGroup(deviceId, key); |
| 377 | int createdGroupId = createdGroup.id().id(); |
| 378 | assertNotEquals(gId1.id().intValue(), createdGroupId); |
| 379 | assertNotEquals(gId2.id().intValue(), createdGroupId); |
| 380 | |
| 381 | List<GroupOperation> expectedGroupOps = Arrays.asList( |
| 382 | GroupOperation.createDeleteGroupOperation(gId1, |
| 383 | Group.Type.SELECT), |
| 384 | GroupOperation.createAddGroupOperation( |
| 385 | createdGroup.id(), |
| 386 | Group.Type.SELECT, |
| 387 | createdGroup.buckets())); |
| 388 | if (deviceId.equals(VDID1)) { |
| 389 | provider.validate(networkId, deviceId, expectedGroupOps); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // Test AUDIT process with extraneous groups and missing groups |
| 394 | private void testAuditWithExtraneousMissingGroups(NetworkId networkId, |
| 395 | DeviceId deviceId) { |
| 396 | VirtualNetworkGroupManager groupManager; |
| 397 | VirtualGroupProviderService providerService; |
| 398 | if (networkId.id() == 1) { |
| 399 | groupManager = groupManager1; |
| 400 | providerService = providerService1; |
| 401 | } else { |
| 402 | groupManager = groupManager2; |
| 403 | providerService = providerService2; |
| 404 | } |
| 405 | |
| 406 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
| 407 | PortNumber.portNumber(32)}; |
| 408 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
| 409 | PortNumber.portNumber(42)}; |
| 410 | GroupId gId1 = new DefaultGroupId(1); |
| 411 | Group group1 = createSouthboundGroupEntry(gId1, |
| 412 | Arrays.asList(ports1), |
| 413 | 0, deviceId); |
| 414 | GroupId gId2 = new DefaultGroupId(2); |
| 415 | Group group2 = createSouthboundGroupEntry(gId2, |
| 416 | Arrays.asList(ports2), |
| 417 | 0, deviceId); |
| 418 | List<Group> groupEntries = Arrays.asList(group1, group2); |
| 419 | providerService.pushGroupMetrics(deviceId, groupEntries); |
| 420 | GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
| 421 | Group createdGroup = groupManager.getGroup(deviceId, key); |
| 422 | List<GroupOperation> expectedGroupOps = Arrays.asList( |
| 423 | GroupOperation.createDeleteGroupOperation(gId1, |
| 424 | Group.Type.SELECT), |
| 425 | GroupOperation.createDeleteGroupOperation(gId2, |
| 426 | Group.Type.SELECT), |
| 427 | GroupOperation.createAddGroupOperation(createdGroup.id(), |
| 428 | Group.Type.SELECT, |
| 429 | createdGroup.buckets())); |
| 430 | if (deviceId.equals(VDID1)) { |
| 431 | provider.validate(networkId, deviceId, expectedGroupOps); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Test AUDIT with confirmed groups |
| 436 | private void testAuditWithConfirmedGroups(NetworkId networkId, |
| 437 | DeviceId deviceId) { |
| 438 | VirtualNetworkGroupManager groupManager; |
| 439 | VirtualGroupProviderService providerService; |
| 440 | TestGroupListener listener; |
| 441 | |
| 442 | if (networkId.id() == 1) { |
| 443 | groupManager = groupManager1; |
| 444 | providerService = providerService1; |
| 445 | listener = listener1; |
| 446 | } else { |
| 447 | groupManager = groupManager2; |
| 448 | providerService = providerService2; |
| 449 | listener = listener2; |
| 450 | } |
| 451 | |
| 452 | GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
| 453 | Group createdGroup = groupManager.getGroup(deviceId, key); |
| 454 | createdGroup = new DefaultGroup(createdGroup.id(), |
| 455 | deviceId, |
| 456 | Group.Type.SELECT, |
| 457 | createdGroup.buckets()); |
| 458 | List<Group> groupEntries = Collections.singletonList(createdGroup); |
| 459 | providerService.pushGroupMetrics(deviceId, groupEntries); |
| 460 | listener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_ADDED)); |
| 461 | } |
| 462 | |
| 463 | private Group createSouthboundGroupEntry(GroupId gId, |
| 464 | List<PortNumber> ports, |
| 465 | long referenceCount, DeviceId deviceId) { |
| 466 | List<PortNumber> outPorts = new ArrayList<>(); |
| 467 | outPorts.addAll(ports); |
| 468 | |
| 469 | List<GroupBucket> buckets = new ArrayList<>(); |
| 470 | for (PortNumber portNumber : outPorts) { |
| 471 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 472 | tBuilder.setOutput(portNumber) |
| 473 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 474 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 475 | .pushMpls() |
| 476 | .setMpls(MplsLabel.mplsLabel(106)); |
| 477 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 478 | tBuilder.build())); |
| 479 | } |
| 480 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 481 | StoredGroupEntry group = new DefaultGroup( |
| 482 | gId, deviceId, Group.Type.SELECT, groupBuckets); |
| 483 | group.setReferenceCount(referenceCount); |
| 484 | return group; |
| 485 | } |
| 486 | |
| 487 | // Test group add bucket operations |
| 488 | private void testAddBuckets(NetworkId networkId, DeviceId deviceId) { |
| 489 | VirtualNetworkGroupManager groupManager; |
| 490 | VirtualGroupProviderService providerService; |
| 491 | TestGroupListener listener; |
| 492 | |
| 493 | if (networkId.id() == 1) { |
| 494 | groupManager = groupManager1; |
| 495 | providerService = providerService1; |
| 496 | listener = listener1; |
| 497 | } else { |
| 498 | groupManager = groupManager2; |
| 499 | providerService = providerService2; |
| 500 | listener = listener2; |
| 501 | } |
| 502 | |
| 503 | GroupKey addKey = new DefaultGroupKey("group1AddBuckets".getBytes()); |
| 504 | |
| 505 | GroupKey prevKey = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
| 506 | Group createdGroup = groupManager.getGroup(deviceId, prevKey); |
| 507 | List<GroupBucket> buckets = new ArrayList<>(); |
| 508 | buckets.addAll(createdGroup.buckets().buckets()); |
| 509 | |
| 510 | PortNumber[] addPorts = {PortNumber.portNumber(51), |
| 511 | PortNumber.portNumber(52)}; |
| 512 | List<PortNumber> outPorts; |
| 513 | outPorts = new ArrayList<>(); |
| 514 | outPorts.addAll(Arrays.asList(addPorts)); |
| 515 | List<GroupBucket> addBuckets; |
| 516 | addBuckets = new ArrayList<>(); |
| 517 | for (PortNumber portNumber : outPorts) { |
| 518 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 519 | tBuilder.setOutput(portNumber) |
| 520 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 521 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 522 | .pushMpls() |
| 523 | .setMpls(MplsLabel.mplsLabel(106)); |
| 524 | addBuckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 525 | tBuilder.build())); |
| 526 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 527 | tBuilder.build())); |
| 528 | } |
| 529 | GroupBuckets groupAddBuckets = new GroupBuckets(addBuckets); |
| 530 | groupManager.addBucketsToGroup(deviceId, |
| 531 | prevKey, |
| 532 | groupAddBuckets, |
| 533 | addKey, |
| 534 | appId); |
| 535 | GroupBuckets updatedBuckets = new GroupBuckets(buckets); |
| 536 | List<GroupOperation> expectedGroupOps = Collections.singletonList( |
| 537 | GroupOperation.createModifyGroupOperation(createdGroup.id(), |
| 538 | Group.Type.SELECT, |
| 539 | updatedBuckets)); |
| 540 | if (deviceId.equals(VDID1)) { |
| 541 | provider.validate(networkId, deviceId, expectedGroupOps); |
| 542 | } |
| 543 | |
| 544 | Group existingGroup = groupManager.getGroup(deviceId, addKey); |
| 545 | List<Group> groupEntries = Collections.singletonList(existingGroup); |
| 546 | providerService.pushGroupMetrics(deviceId, groupEntries); |
| 547 | listener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED)); |
| 548 | } |
| 549 | |
| 550 | // Test purge group entry operations |
| 551 | private void testPurgeGroupEntry(NetworkId networkId, DeviceId deviceId) { |
| 552 | VirtualNetworkGroupManager groupManager; |
| 553 | if (networkId.id() == 1) { |
| 554 | groupManager = groupManager1; |
| 555 | } else { |
| 556 | groupManager = groupManager2; |
| 557 | } |
| 558 | |
| 559 | assertEquals(1, Iterables.size(groupManager.getGroups(deviceId, appId))); |
| 560 | groupManager.purgeGroupEntries(deviceId); |
| 561 | assertEquals(0, Iterables.size(groupManager.getGroups(deviceId, appId))); |
| 562 | } |
| 563 | |
| 564 | // Test group remove bucket operations |
| 565 | private void testRemoveBuckets(NetworkId networkId, DeviceId deviceId) { |
| 566 | VirtualNetworkGroupManager groupManager; |
| 567 | VirtualGroupProviderService providerService; |
| 568 | TestGroupListener listener; |
| 569 | |
| 570 | if (networkId.id() == 1) { |
| 571 | groupManager = groupManager1; |
| 572 | providerService = providerService1; |
| 573 | listener = listener1; |
| 574 | } else { |
| 575 | groupManager = groupManager2; |
| 576 | providerService = providerService2; |
| 577 | listener = listener2; |
| 578 | } |
| 579 | |
| 580 | GroupKey removeKey = new DefaultGroupKey("group1RemoveBuckets".getBytes()); |
| 581 | |
| 582 | GroupKey prevKey = new DefaultGroupKey("group1AddBuckets".getBytes()); |
| 583 | Group createdGroup = groupManager.getGroup(deviceId, prevKey); |
| 584 | List<GroupBucket> buckets = new ArrayList<>(); |
| 585 | buckets.addAll(createdGroup.buckets().buckets()); |
| 586 | |
| 587 | PortNumber[] removePorts = {PortNumber.portNumber(31), |
| 588 | PortNumber.portNumber(32)}; |
| 589 | List<PortNumber> outPorts = new ArrayList<>(); |
| 590 | outPorts.addAll(Arrays.asList(removePorts)); |
| 591 | List<GroupBucket> removeBuckets = new ArrayList<>(); |
| 592 | for (PortNumber portNumber : outPorts) { |
| 593 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 594 | tBuilder.setOutput(portNumber) |
| 595 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 596 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 597 | .pushMpls() |
| 598 | .setMpls(MplsLabel.mplsLabel(106)); |
| 599 | removeBuckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 600 | tBuilder.build())); |
| 601 | buckets.remove(DefaultGroupBucket.createSelectGroupBucket( |
| 602 | tBuilder.build())); |
| 603 | } |
| 604 | GroupBuckets groupRemoveBuckets = new GroupBuckets(removeBuckets); |
| 605 | groupManager.removeBucketsFromGroup(deviceId, |
| 606 | prevKey, |
| 607 | groupRemoveBuckets, |
| 608 | removeKey, |
| 609 | appId); |
| 610 | GroupBuckets updatedBuckets = new GroupBuckets(buckets); |
| 611 | List<GroupOperation> expectedGroupOps = Collections.singletonList( |
| 612 | GroupOperation.createModifyGroupOperation(createdGroup.id(), |
| 613 | Group.Type.SELECT, |
| 614 | updatedBuckets)); |
| 615 | if (deviceId.equals(VDID1)) { |
| 616 | provider.validate(networkId, deviceId, expectedGroupOps); |
| 617 | } |
| 618 | |
| 619 | Group existingGroup = groupManager.getGroup(deviceId, removeKey); |
| 620 | List<Group> groupEntries = Collections.singletonList(existingGroup); |
| 621 | providerService.pushGroupMetrics(deviceId, groupEntries); |
| 622 | listener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED)); |
| 623 | } |
| 624 | |
| 625 | // Test group remove operations |
| 626 | private void testRemoveGroup(NetworkId networkId, DeviceId deviceId) { |
| 627 | VirtualNetworkGroupManager groupManager; |
| 628 | VirtualGroupProviderService providerService; |
| 629 | TestGroupListener listener; |
| 630 | |
| 631 | if (networkId.id() == 1) { |
| 632 | groupManager = groupManager1; |
| 633 | providerService = providerService1; |
| 634 | listener = listener1; |
| 635 | } else { |
| 636 | groupManager = groupManager2; |
| 637 | providerService = providerService2; |
| 638 | listener = listener2; |
| 639 | } |
| 640 | |
| 641 | GroupKey currKey = new DefaultGroupKey("group1RemoveBuckets".getBytes()); |
| 642 | Group existingGroup = groupManager.getGroup(deviceId, currKey); |
| 643 | groupManager.removeGroup(deviceId, currKey, appId); |
| 644 | List<GroupOperation> expectedGroupOps = Collections.singletonList( |
| 645 | GroupOperation.createDeleteGroupOperation(existingGroup.id(), |
| 646 | Group.Type.SELECT)); |
| 647 | if (deviceId.equals(VDID1)) { |
| 648 | provider.validate(networkId, deviceId, expectedGroupOps); |
| 649 | } |
| 650 | |
| 651 | List<Group> groupEntries = Collections.emptyList(); |
| 652 | providerService.pushGroupMetrics(deviceId, groupEntries); |
| 653 | listener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_REMOVED)); |
| 654 | } |
| 655 | |
| 656 | private class TestGroupProvider extends AbstractVirtualProvider |
| 657 | implements VirtualGroupProvider { |
| 658 | NetworkId lastNetworkId; |
| 659 | DeviceId lastDeviceId; |
| 660 | List<GroupOperation> groupOperations = new ArrayList<>(); |
| 661 | |
| 662 | protected TestGroupProvider() { |
| 663 | super(new ProviderId("test", "org.onosproject.virtual.testprovider")); |
| 664 | } |
| 665 | |
| 666 | @Override |
| 667 | public void performGroupOperation(NetworkId networkId, DeviceId deviceId, |
| 668 | GroupOperations groupOps) { |
| 669 | lastNetworkId = networkId; |
| 670 | lastDeviceId = deviceId; |
| 671 | groupOperations.addAll(groupOps.operations()); |
| 672 | } |
| 673 | |
| 674 | public void validate(NetworkId expectedNetworkId, DeviceId expectedDeviceId, |
| 675 | List<GroupOperation> expectedGroupOps) { |
| 676 | if (expectedGroupOps == null) { |
| 677 | assertTrue("events generated", groupOperations.isEmpty()); |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | assertEquals(lastNetworkId, expectedNetworkId); |
| 682 | assertEquals(lastDeviceId, expectedDeviceId); |
| 683 | assertTrue((this.groupOperations.containsAll(expectedGroupOps) && |
| 684 | expectedGroupOps.containsAll(groupOperations))); |
| 685 | |
| 686 | groupOperations.clear(); |
| 687 | lastDeviceId = null; |
| 688 | lastNetworkId = null; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | private static class TestGroupListener implements GroupListener { |
| 693 | final List<GroupEvent> events = new ArrayList<>(); |
| 694 | |
| 695 | @Override |
| 696 | public void event(GroupEvent event) { |
| 697 | events.add(event); |
| 698 | } |
| 699 | |
| 700 | public void validateEvent(List<GroupEvent.Type> expectedEvents) { |
| 701 | int i = 0; |
| 702 | System.err.println("events :" + events); |
| 703 | for (GroupEvent e : events) { |
| 704 | assertEquals("unexpected event", expectedEvents.get(i), e.type()); |
| 705 | i++; |
| 706 | } |
| 707 | assertEquals("mispredicted number of events", |
| 708 | expectedEvents.size(), events.size()); |
| 709 | events.clear(); |
| 710 | } |
| 711 | } |
| 712 | } |