Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [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.net.group.impl; |
| 17 | |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.Iterables; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 25 | import org.onlab.packet.MplsLabel; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cfg.ComponentConfigAdapter; |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 27 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.DefaultApplicationId; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 30 | import org.onosproject.core.GroupId; |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 31 | import org.onosproject.net.AnnotationKeys; |
| 32 | import org.onosproject.net.DefaultAnnotations; |
| 33 | import org.onosproject.net.DefaultDevice; |
| 34 | import org.onosproject.net.Device; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.PortNumber; |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 38 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 39 | import org.onosproject.net.driver.DefaultDriver; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 40 | import org.onosproject.net.driver.DriverRegistry; |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 41 | import org.onosproject.net.driver.impl.DriverManager; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 42 | import org.onosproject.net.driver.impl.DriverRegistryManager; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.TrafficTreatment; |
| 45 | import org.onosproject.net.group.DefaultGroup; |
| 46 | import org.onosproject.net.group.DefaultGroupBucket; |
| 47 | import org.onosproject.net.group.DefaultGroupDescription; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 48 | import org.onosproject.net.group.DefaultGroupKey; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 49 | import org.onosproject.net.group.Group; |
| 50 | import org.onosproject.net.group.GroupBucket; |
| 51 | import org.onosproject.net.group.GroupBuckets; |
| 52 | import org.onosproject.net.group.GroupDescription; |
| 53 | import org.onosproject.net.group.GroupEvent; |
| 54 | import org.onosproject.net.group.GroupKey; |
| 55 | import org.onosproject.net.group.GroupListener; |
| 56 | import org.onosproject.net.group.GroupOperation; |
| 57 | import org.onosproject.net.group.GroupOperations; |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 58 | import org.onosproject.net.group.GroupProgrammable; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 59 | import org.onosproject.net.group.GroupProvider; |
| 60 | import org.onosproject.net.group.GroupProviderRegistry; |
| 61 | import org.onosproject.net.group.GroupProviderService; |
| 62 | import org.onosproject.net.group.GroupService; |
| 63 | import org.onosproject.net.group.StoredGroupEntry; |
| 64 | import org.onosproject.net.provider.AbstractProvider; |
| 65 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 66 | import org.onosproject.store.trivial.SimpleGroupStore; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 67 | |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 68 | import java.util.ArrayList; |
| 69 | import java.util.Arrays; |
| 70 | import java.util.Collections; |
| 71 | import java.util.List; |
| 72 | |
| 73 | import static org.junit.Assert.*; |
| 74 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Test codifying the group service & group provider service contracts. |
| 78 | */ |
| 79 | public class GroupManagerTest { |
| 80 | |
| 81 | private static final ProviderId PID = new ProviderId("of", "groupfoo"); |
| 82 | private static final DeviceId DID = DeviceId.deviceId("of:001"); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 83 | private static final ProviderId FOO_PID = new ProviderId("foo", "foo"); |
| 84 | private static final DeviceId FOO_DID = DeviceId.deviceId("foo:002"); |
| 85 | |
| 86 | private static final DefaultAnnotations ANNOTATIONS = |
| 87 | DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, "foo").build(); |
| 88 | |
| 89 | private static final Device FOO_DEV = |
| 90 | new DefaultDevice(FOO_PID, FOO_DID, Device.Type.SWITCH, "", "", "", "", null, ANNOTATIONS); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 91 | |
| 92 | private GroupManager mgr; |
| 93 | private GroupService groupService; |
| 94 | private GroupProviderRegistry providerRegistry; |
| 95 | private TestGroupListener internalListener = new TestGroupListener(); |
| 96 | private GroupListener listener = internalListener; |
| 97 | private TestGroupProvider internalProvider; |
| 98 | private GroupProvider provider; |
| 99 | private GroupProviderService providerService; |
| 100 | private ApplicationId appId; |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 101 | private TestDriverManager driverService; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 102 | |
| 103 | @Before |
| 104 | public void setUp() { |
| 105 | mgr = new GroupManager(); |
| 106 | groupService = mgr; |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 107 | //mgr.deviceService = new DeviceManager(); |
| 108 | mgr.deviceService = new TestDeviceService(); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 109 | mgr.cfgService = new ComponentConfigAdapter(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 110 | mgr.store = new SimpleGroupStore(); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 111 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 112 | providerRegistry = mgr; |
| 113 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 114 | mgr.activate(null); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 115 | mgr.addListener(listener); |
| 116 | |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 117 | DriverRegistryManager driverRegistry = new DriverRegistryManager(); |
| 118 | driverService = new TestDriverManager(driverRegistry); |
| 119 | driverRegistry.addDriver(new DefaultDriver("foo", ImmutableList.of(), "", "", "", |
| 120 | ImmutableMap.of(GroupProgrammable.class, |
| 121 | TestGroupProgrammable.class), |
| 122 | ImmutableMap.of())); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 123 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 124 | internalProvider = new TestGroupProvider(PID); |
| 125 | provider = internalProvider; |
| 126 | providerService = providerRegistry.register(provider); |
| 127 | appId = new DefaultApplicationId(2, "org.groupmanager.test"); |
| 128 | assertTrue("provider should be registered", |
| 129 | providerRegistry.getProviders().contains(provider.id())); |
| 130 | } |
| 131 | |
| 132 | @After |
| 133 | public void tearDown() { |
| 134 | providerRegistry.unregister(provider); |
| 135 | assertFalse("provider should not be registered", |
| 136 | providerRegistry.getProviders().contains(provider.id())); |
| 137 | mgr.removeListener(listener); |
| 138 | mgr.deactivate(); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 139 | injectEventDispatcher(mgr, null); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 142 | /** |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 143 | * Tests group creation before the device group AUDIT completes. |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 144 | */ |
| 145 | @Test |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 146 | public void testGroupServiceBasics() { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 147 | // Test Group creation before AUDIT process |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 148 | testGroupCreationBeforeAudit(DID); |
| 149 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 150 | |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 151 | /** |
| 152 | * Tests initial device group AUDIT process. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testGroupServiceInitialAudit() { |
| 156 | // Test Group creation before AUDIT process |
| 157 | testGroupCreationBeforeAudit(DID); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 158 | // Test initial group audit process |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 159 | testInitialAuditWithPendingGroupRequests(DID); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Tests deletion process of any extraneous groups. |
| 164 | */ |
| 165 | @Test |
| 166 | public void testGroupServiceAuditExtraneous() { |
| 167 | // Test Group creation before AUDIT process |
| 168 | testGroupCreationBeforeAudit(DID); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 169 | |
| 170 | // Test audit with extraneous and missing groups |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 171 | testAuditWithExtraneousMissingGroups(DID); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Tests re-apply process of any missing groups tests execution of |
| 176 | * any pending group creation request after the device group AUDIT completes |
| 177 | * and tests event notifications after receiving confirmation for any |
| 178 | * operations from data plane. |
| 179 | */ |
| 180 | @Test |
| 181 | public void testGroupServiceAuditConfirmed() { |
| 182 | // Test Group creation before AUDIT process |
| 183 | testGroupCreationBeforeAudit(DID); |
| 184 | |
| 185 | // Test audit with extraneous and missing groups |
| 186 | testAuditWithExtraneousMissingGroups(DID); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 187 | |
| 188 | // Test audit with confirmed groups |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 189 | testAuditWithConfirmedGroups(DID); |
| 190 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 191 | |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 192 | /** |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 193 | * Tests group Purge Operation. |
| 194 | */ |
| 195 | @Test |
| 196 | public void testPurgeGroups() { |
| 197 | //Test Group creation before AUDIT process |
| 198 | testGroupCreationBeforeAudit(DID); |
| 199 | programmableTestCleanUp(); |
| 200 | testAuditWithExtraneousMissingGroups(DID); |
| 201 | // Test group add bucket operations |
| 202 | testAddBuckets(DID); |
| 203 | // Test group Purge operations |
| 204 | testPurgeGroupEntry(DID); |
| 205 | } |
| 206 | |
| 207 | /** |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 208 | * Tests group bucket modifications (additions and deletions) and |
| 209 | * Tests group deletion. |
| 210 | */ |
| 211 | @Test |
| 212 | public void testGroupServiceBuckets() { |
| 213 | // Test Group creation before AUDIT process |
| 214 | testGroupCreationBeforeAudit(DID); |
| 215 | programmableTestCleanUp(); |
| 216 | |
| 217 | testAuditWithExtraneousMissingGroups(DID); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 218 | // Test group add bucket operations |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 219 | testAddBuckets(DID); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 220 | |
| 221 | // Test group remove bucket operations |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 222 | testRemoveBuckets(DID); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 223 | |
| 224 | // Test group remove operations |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 225 | testRemoveGroup(DID); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Tests group creation before the device group AUDIT completes with fallback |
| 230 | * provider. |
| 231 | */ |
| 232 | @Test |
| 233 | public void testGroupServiceFallbackBasics() { |
| 234 | // Test Group creation before AUDIT process |
| 235 | testGroupCreationBeforeAudit(FOO_DID); |
| 236 | programmableTestCleanUp(); |
| 237 | |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Tests initial device group AUDIT process with fallback provider. |
| 242 | */ |
| 243 | @Test |
| 244 | public void testGroupServiceFallbackInitialAudit() { |
| 245 | // Test Group creation before AUDIT process |
| 246 | testGroupCreationBeforeAudit(FOO_DID); |
| 247 | programmableTestCleanUp(); |
| 248 | // Test initial group audit process |
| 249 | testInitialAuditWithPendingGroupRequests(FOO_DID); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Tests deletion process of any extraneous groups with fallback provider. |
| 254 | */ |
| 255 | @Test |
| 256 | public void testGroupServiceFallbackAuditExtraneous() { |
| 257 | // Test Group creation before AUDIT process |
| 258 | testGroupCreationBeforeAudit(FOO_DID); |
| 259 | programmableTestCleanUp(); |
| 260 | |
| 261 | // Test audit with extraneous and missing groups |
| 262 | testAuditWithExtraneousMissingGroups(FOO_DID); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Tests re-apply process of any missing groups tests execution of |
| 267 | * any pending group creation request after the device group AUDIT completes |
| 268 | * and tests event notifications after receiving confirmation for any |
| 269 | * operations from data plane with fallback provider. |
| 270 | */ |
| 271 | @Test |
| 272 | public void testGroupServiceFallbackAuditConfirmed() { |
| 273 | // Test Group creation before AUDIT process |
| 274 | testGroupCreationBeforeAudit(FOO_DID); |
| 275 | programmableTestCleanUp(); |
| 276 | |
| 277 | // Test audit with extraneous and missing groups |
| 278 | testAuditWithExtraneousMissingGroups(FOO_DID); |
| 279 | |
| 280 | // Test audit with confirmed groups |
| 281 | testAuditWithConfirmedGroups(FOO_DID); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Tests group bucket modifications (additions and deletions) and |
| 286 | * Tests group deletion with fallback provider. |
| 287 | */ |
| 288 | @Test |
| 289 | public void testGroupServiceFallbackBuckets() { |
| 290 | // Test Group creation before AUDIT process |
| 291 | testGroupCreationBeforeAudit(FOO_DID); |
| 292 | programmableTestCleanUp(); |
| 293 | |
| 294 | testAuditWithExtraneousMissingGroups(FOO_DID); |
| 295 | // Test group add bucket operations |
| 296 | testAddBuckets(FOO_DID); |
| 297 | |
| 298 | // Test group remove bucket operations |
| 299 | testRemoveBuckets(FOO_DID); |
| 300 | |
| 301 | // Test group remove operations |
| 302 | testRemoveGroup(FOO_DID); |
| 303 | } |
| 304 | |
| 305 | private void programmableTestCleanUp() { |
| 306 | groupOperations.clear(); |
| 307 | lastDeviceIdProgrammable = null; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Test Group creation before AUDIT process |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 311 | private void testGroupCreationBeforeAudit(DeviceId deviceId) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 312 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 313 | PortNumber.portNumber(32)}; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 314 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 315 | PortNumber.portNumber(42)}; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 316 | GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 317 | List<GroupBucket> buckets = new ArrayList<>(); |
| 318 | List<PortNumber> outPorts = new ArrayList<>(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 319 | outPorts.addAll(Arrays.asList(ports1)); |
| 320 | outPorts.addAll(Arrays.asList(ports2)); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 321 | for (PortNumber portNumber : outPorts) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 322 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 323 | tBuilder.setOutput(portNumber) |
| 324 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 325 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 326 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 327 | .setMpls(MplsLabel.mplsLabel(106)); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 328 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 329 | tBuilder.build())); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 330 | } |
| 331 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 332 | GroupDescription newGroupDesc = new DefaultGroupDescription(deviceId, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 333 | Group.Type.SELECT, |
| 334 | groupBuckets, |
| 335 | key, |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 336 | null, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 337 | appId); |
| 338 | groupService.addGroup(newGroupDesc); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 339 | assertEquals(null, groupService.getGroup(deviceId, key)); |
| 340 | assertEquals(0, Iterables.size(groupService.getGroups(deviceId, appId))); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 341 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 342 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 343 | // Test initial AUDIT process with pending group requests |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 344 | private void testInitialAuditWithPendingGroupRequests(DeviceId deviceId) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 345 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 346 | PortNumber.portNumber(32)}; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 347 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 348 | PortNumber.portNumber(42)}; |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 349 | GroupId gId1 = new GroupId(1); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 350 | Group group1 = createSouthboundGroupEntry(gId1, |
| 351 | Arrays.asList(ports1), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 352 | 0, deviceId); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 353 | GroupId gId2 = new GroupId(2); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 354 | // Non zero reference count will make the group manager to queue |
| 355 | // the extraneous groups until reference count is zero. |
| 356 | Group group2 = createSouthboundGroupEntry(gId2, |
| 357 | Arrays.asList(ports2), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 358 | 2, deviceId); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 359 | List<Group> groupEntries = Arrays.asList(group1, group2); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 360 | providerService.pushGroupMetrics(deviceId, groupEntries); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 361 | // First group metrics would trigger the device audit completion |
| 362 | // post which all pending group requests are also executed. |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 363 | GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 364 | Group createdGroup = groupService.getGroup(deviceId, key); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 365 | int createdGroupId = createdGroup.id().id(); |
Prince Pereira | 3ff504c | 2016-08-30 14:23:43 +0530 | [diff] [blame] | 366 | assertNotEquals(gId1.id().intValue(), createdGroupId); |
| 367 | assertNotEquals(gId2.id().intValue(), createdGroupId); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 368 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 369 | List<GroupOperation> expectedGroupOps = Arrays.asList( |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 370 | GroupOperation.createDeleteGroupOperation(gId1, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 371 | Group.Type.SELECT), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 372 | GroupOperation.createAddGroupOperation( |
| 373 | createdGroup.id(), |
| 374 | Group.Type.SELECT, |
| 375 | createdGroup.buckets())); |
| 376 | if (deviceId.equals(DID)) { |
| 377 | internalProvider.validate(deviceId, expectedGroupOps); |
| 378 | } else { |
| 379 | this.validate(deviceId, expectedGroupOps); |
| 380 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 381 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 382 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 383 | // Test AUDIT process with extraneous groups and missing groups |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 384 | private void testAuditWithExtraneousMissingGroups(DeviceId deviceId) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 385 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 386 | PortNumber.portNumber(32)}; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 387 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 388 | PortNumber.portNumber(42)}; |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 389 | GroupId gId1 = new GroupId(1); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 390 | Group group1 = createSouthboundGroupEntry(gId1, |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 391 | Arrays.asList(ports1), |
| 392 | 0, deviceId); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 393 | GroupId gId2 = new GroupId(2); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 394 | Group group2 = createSouthboundGroupEntry(gId2, |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 395 | Arrays.asList(ports2), |
| 396 | 0, deviceId); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 397 | List<Group> groupEntries = Arrays.asList(group1, group2); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 398 | providerService.pushGroupMetrics(deviceId, groupEntries); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 399 | GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 400 | Group createdGroup = groupService.getGroup(deviceId, key); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 401 | List<GroupOperation> expectedGroupOps = Arrays.asList( |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 402 | GroupOperation.createDeleteGroupOperation(gId1, |
| 403 | Group.Type.SELECT), |
| 404 | GroupOperation.createDeleteGroupOperation(gId2, |
| 405 | Group.Type.SELECT), |
| 406 | GroupOperation.createAddGroupOperation(createdGroup.id(), |
| 407 | Group.Type.SELECT, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 408 | createdGroup.buckets())); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 409 | if (deviceId.equals(DID)) { |
| 410 | internalProvider.validate(deviceId, expectedGroupOps); |
| 411 | } else { |
| 412 | this.validate(deviceId, expectedGroupOps); |
| 413 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 414 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 415 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 416 | // Test AUDIT with confirmed groups |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 417 | private void testAuditWithConfirmedGroups(DeviceId deviceId) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 418 | GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 419 | Group createdGroup = groupService.getGroup(deviceId, key); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 420 | createdGroup = new DefaultGroup(createdGroup.id(), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 421 | deviceId, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 422 | Group.Type.SELECT, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 423 | createdGroup.buckets()); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 424 | List<Group> groupEntries = Collections.singletonList(createdGroup); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 425 | providerService.pushGroupMetrics(deviceId, groupEntries); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 426 | internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_ADDED)); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 427 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 428 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 429 | // Test group add bucket operations |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 430 | private void testAddBuckets(DeviceId deviceId) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 431 | GroupKey addKey = new DefaultGroupKey("group1AddBuckets".getBytes()); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 432 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 433 | GroupKey prevKey = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 434 | Group createdGroup = groupService.getGroup(deviceId, prevKey); |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 435 | List<GroupBucket> buckets = new ArrayList<>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 436 | buckets.addAll(createdGroup.buckets().buckets()); |
| 437 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 438 | PortNumber[] addPorts = {PortNumber.portNumber(51), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 439 | PortNumber.portNumber(52)}; |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 440 | List<PortNumber> outPorts; |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 441 | outPorts = new ArrayList<>(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 442 | outPorts.addAll(Arrays.asList(addPorts)); |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 443 | List<GroupBucket> addBuckets; |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 444 | addBuckets = new ArrayList<>(); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 445 | for (PortNumber portNumber : outPorts) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 446 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 447 | tBuilder.setOutput(portNumber) |
| 448 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 449 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 450 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 451 | .setMpls(MplsLabel.mplsLabel(106)); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 452 | addBuckets.add(DefaultGroupBucket.createSelectGroupBucket( |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 453 | tBuilder.build())); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 454 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 455 | tBuilder.build())); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 456 | } |
| 457 | GroupBuckets groupAddBuckets = new GroupBuckets(addBuckets); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 458 | groupService.addBucketsToGroup(deviceId, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 459 | prevKey, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 460 | groupAddBuckets, |
| 461 | addKey, |
| 462 | appId); |
| 463 | GroupBuckets updatedBuckets = new GroupBuckets(buckets); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 464 | List<GroupOperation> expectedGroupOps = Collections.singletonList( |
| 465 | GroupOperation.createModifyGroupOperation(createdGroup.id(), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 466 | Group.Type.SELECT, |
| 467 | updatedBuckets)); |
| 468 | if (deviceId.equals(DID)) { |
| 469 | internalProvider.validate(deviceId, expectedGroupOps); |
| 470 | } else { |
| 471 | this.validate(deviceId, expectedGroupOps); |
| 472 | } |
| 473 | Group existingGroup = groupService.getGroup(deviceId, addKey); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 474 | List<Group> groupEntries = Collections.singletonList(existingGroup); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 475 | providerService.pushGroupMetrics(deviceId, groupEntries); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 476 | internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED)); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 477 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 478 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 479 | // Test group remove bucket operations |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 480 | private void testRemoveBuckets(DeviceId deviceId) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 481 | GroupKey removeKey = new DefaultGroupKey("group1RemoveBuckets".getBytes()); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 482 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 483 | GroupKey prevKey = new DefaultGroupKey("group1AddBuckets".getBytes()); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 484 | Group createdGroup = groupService.getGroup(deviceId, prevKey); |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 485 | List<GroupBucket> buckets = new ArrayList<>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 486 | buckets.addAll(createdGroup.buckets().buckets()); |
| 487 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 488 | PortNumber[] removePorts = {PortNumber.portNumber(31), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 489 | PortNumber.portNumber(32)}; |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 490 | List<PortNumber> outPorts = new ArrayList<>(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 491 | outPorts.addAll(Arrays.asList(removePorts)); |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 492 | List<GroupBucket> removeBuckets = new ArrayList<>(); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 493 | for (PortNumber portNumber : outPorts) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 494 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 495 | tBuilder.setOutput(portNumber) |
| 496 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 497 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 498 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 499 | .setMpls(MplsLabel.mplsLabel(106)); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 500 | removeBuckets.add(DefaultGroupBucket.createSelectGroupBucket( |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 501 | tBuilder.build())); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 502 | buckets.remove(DefaultGroupBucket.createSelectGroupBucket( |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 503 | tBuilder.build())); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 504 | } |
| 505 | GroupBuckets groupRemoveBuckets = new GroupBuckets(removeBuckets); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 506 | groupService.removeBucketsFromGroup(deviceId, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 507 | prevKey, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 508 | groupRemoveBuckets, |
| 509 | removeKey, |
| 510 | appId); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 511 | GroupBuckets updatedBuckets = new GroupBuckets(buckets); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 512 | List<GroupOperation> expectedGroupOps = Collections.singletonList( |
| 513 | GroupOperation.createModifyGroupOperation(createdGroup.id(), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 514 | Group.Type.SELECT, |
| 515 | updatedBuckets)); |
| 516 | if (deviceId.equals(DID)) { |
| 517 | internalProvider.validate(deviceId, expectedGroupOps); |
| 518 | } else { |
| 519 | this.validate(deviceId, expectedGroupOps); |
| 520 | } |
| 521 | Group existingGroup = groupService.getGroup(deviceId, removeKey); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 522 | List<Group> groupEntries = Collections.singletonList(existingGroup); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 523 | providerService.pushGroupMetrics(deviceId, groupEntries); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 524 | internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATED)); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 525 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 526 | |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 527 | // Test purge group entry operations |
| 528 | private void testPurgeGroupEntry(DeviceId deviceId) { |
| 529 | assertEquals(1, Iterables.size(groupService.getGroups(deviceId, appId))); |
| 530 | groupService.purgeGroupEntries(deviceId); |
| 531 | assertEquals(0, Iterables.size(groupService.getGroups(deviceId, appId))); |
| 532 | } |
| 533 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 534 | // Test group remove operations |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 535 | private void testRemoveGroup(DeviceId deviceId) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 536 | GroupKey currKey = new DefaultGroupKey("group1RemoveBuckets".getBytes()); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 537 | Group existingGroup = groupService.getGroup(deviceId, currKey); |
| 538 | groupService.removeGroup(deviceId, currKey, appId); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 539 | List<GroupOperation> expectedGroupOps = Collections.singletonList( |
| 540 | GroupOperation.createDeleteGroupOperation(existingGroup.id(), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 541 | Group.Type.SELECT)); |
| 542 | if (deviceId.equals(DID)) { |
| 543 | internalProvider.validate(deviceId, expectedGroupOps); |
| 544 | } else { |
| 545 | this.validate(deviceId, expectedGroupOps); |
| 546 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 547 | List<Group> groupEntries = Collections.emptyList(); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 548 | providerService.pushGroupMetrics(deviceId, groupEntries); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 549 | internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_REMOVED)); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 550 | } |
| 551 | |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 552 | /** |
| 553 | * Test GroupOperationFailure function in Group Manager. |
| 554 | * a)GroupAddFailure |
| 555 | * b)GroupUpdateFailure |
| 556 | * c)GroupRemoteFailure |
| 557 | */ |
| 558 | @Test |
| 559 | public void testGroupOperationFailure() { |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 560 | groupOperationFaliure(DID); |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Test GroupOperationFailure function in Group Manager |
| 565 | * with fallback provider. |
| 566 | * a)GroupAddFailure |
| 567 | * b)GroupUpdateFailure |
| 568 | * c)GroupRemoteFailure |
| 569 | */ |
| 570 | @Test |
| 571 | public void testGroupOperationFailureFallBack() { |
| 572 | groupOperationFaliure(FOO_DID); |
| 573 | } |
| 574 | |
| 575 | private void groupOperationFaliure(DeviceId deviceId) { |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 576 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
| 577 | PortNumber.portNumber(32)}; |
| 578 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
| 579 | PortNumber.portNumber(42)}; |
| 580 | // Test Group creation before AUDIT process |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 581 | GroupKey key = new DefaultGroupKey("group1BeforeAudit".getBytes()); |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 582 | List<GroupBucket> buckets = new ArrayList<>(); |
| 583 | List<PortNumber> outPorts = new ArrayList<>(); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 584 | outPorts.addAll(Arrays.asList(ports1)); |
| 585 | outPorts.addAll(Arrays.asList(ports2)); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 586 | for (PortNumber portNumber : outPorts) { |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 587 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 588 | tBuilder.setOutput(portNumber) |
| 589 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 590 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 591 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 592 | .setMpls(MplsLabel.mplsLabel(106)); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 593 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 594 | tBuilder.build())); |
| 595 | } |
| 596 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 597 | GroupDescription newGroupDesc = new DefaultGroupDescription(deviceId, |
| 598 | Group.Type.SELECT, |
| 599 | groupBuckets, |
| 600 | key, |
| 601 | null, |
| 602 | appId); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 603 | groupService.addGroup(newGroupDesc); |
| 604 | |
| 605 | // Test initial group audit process |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 606 | GroupId gId1 = new GroupId(1); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 607 | Group group1 = createSouthboundGroupEntry(gId1, |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 608 | Arrays.asList(ports1), |
| 609 | 0, deviceId); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 610 | GroupId gId2 = new GroupId(2); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 611 | // Non zero reference count will make the group manager to queue |
| 612 | // the extraneous groups until reference count is zero. |
| 613 | Group group2 = createSouthboundGroupEntry(gId2, |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 614 | Arrays.asList(ports2), |
| 615 | 2, deviceId); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 616 | List<Group> groupEntries = Arrays.asList(group1, group2); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 617 | providerService.pushGroupMetrics(deviceId, groupEntries); |
| 618 | Group createdGroup = groupService.getGroup(deviceId, key); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 619 | |
| 620 | // Group Add failure test |
| 621 | GroupOperation groupAddOp = GroupOperation. |
| 622 | createAddGroupOperation(createdGroup.id(), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 623 | createdGroup.type(), |
| 624 | createdGroup.buckets()); |
| 625 | providerService.groupOperationFailed(deviceId, groupAddOp); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 626 | internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_ADD_FAILED)); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 627 | |
| 628 | // Group Mod failure test |
| 629 | groupService.addGroup(newGroupDesc); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 630 | createdGroup = groupService.getGroup(deviceId, key); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 631 | assertNotNull(createdGroup); |
| 632 | |
| 633 | GroupOperation groupModOp = GroupOperation. |
| 634 | createModifyGroupOperation(createdGroup.id(), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 635 | createdGroup.type(), |
| 636 | createdGroup.buckets()); |
| 637 | providerService.groupOperationFailed(deviceId, groupModOp); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 638 | internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_UPDATE_FAILED)); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 639 | |
| 640 | // Group Delete failure test |
| 641 | groupService.addGroup(newGroupDesc); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 642 | createdGroup = groupService.getGroup(deviceId, key); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 643 | assertNotNull(createdGroup); |
| 644 | |
| 645 | GroupOperation groupDelOp = GroupOperation. |
| 646 | createDeleteGroupOperation(createdGroup.id(), |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 647 | createdGroup.type()); |
| 648 | providerService.groupOperationFailed(deviceId, groupDelOp); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 649 | internalListener.validateEvent(Collections.singletonList(GroupEvent.Type.GROUP_REMOVE_FAILED)); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 652 | private Group createSouthboundGroupEntry(GroupId gId, |
| 653 | List<PortNumber> ports, |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 654 | long referenceCount, DeviceId deviceId) { |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 655 | List<PortNumber> outPorts = new ArrayList<>(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 656 | outPorts.addAll(ports); |
| 657 | |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 658 | List<GroupBucket> buckets = new ArrayList<>(); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 659 | for (PortNumber portNumber : outPorts) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 660 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 661 | tBuilder.setOutput(portNumber) |
| 662 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 663 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 664 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 665 | .setMpls(MplsLabel.mplsLabel(106)); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 666 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 667 | tBuilder.build())); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 668 | } |
| 669 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 670 | StoredGroupEntry group = new DefaultGroup( |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 671 | gId, deviceId, Group.Type.SELECT, groupBuckets); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 672 | group.setReferenceCount(referenceCount); |
| 673 | return group; |
| 674 | } |
| 675 | |
| 676 | private static class TestGroupListener implements GroupListener { |
| 677 | final List<GroupEvent> events = new ArrayList<>(); |
| 678 | |
| 679 | @Override |
| 680 | public void event(GroupEvent event) { |
| 681 | events.add(event); |
| 682 | } |
| 683 | |
| 684 | public void validateEvent(List<GroupEvent.Type> expectedEvents) { |
| 685 | int i = 0; |
| 686 | System.err.println("events :" + events); |
| 687 | for (GroupEvent e : events) { |
| 688 | assertEquals("unexpected event", expectedEvents.get(i), e.type()); |
| 689 | i++; |
| 690 | } |
| 691 | assertEquals("mispredicted number of events", |
| 692 | expectedEvents.size(), events.size()); |
| 693 | events.clear(); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | private class TestGroupProvider |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 698 | extends AbstractProvider implements GroupProvider { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 699 | DeviceId lastDeviceId; |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 700 | List<GroupOperation> groupOperations = new ArrayList<>(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 701 | |
| 702 | protected TestGroupProvider(ProviderId id) { |
| 703 | super(id); |
| 704 | } |
| 705 | |
| 706 | @Override |
| 707 | public void performGroupOperation(DeviceId deviceId, |
| 708 | GroupOperations groupOps) { |
| 709 | lastDeviceId = deviceId; |
| 710 | groupOperations.addAll(groupOps.operations()); |
| 711 | } |
| 712 | |
| 713 | public void validate(DeviceId expectedDeviceId, |
| 714 | List<GroupOperation> expectedGroupOps) { |
| 715 | if (expectedGroupOps == null) { |
| 716 | assertTrue("events generated", groupOperations.isEmpty()); |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | assertEquals(lastDeviceId, expectedDeviceId); |
| 721 | assertTrue((this.groupOperations.containsAll(expectedGroupOps) && |
| 722 | expectedGroupOps.containsAll(groupOperations))); |
| 723 | |
| 724 | groupOperations.clear(); |
| 725 | lastDeviceId = null; |
| 726 | } |
| 727 | |
| 728 | } |
| 729 | |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 730 | private static class TestDeviceService extends DeviceServiceAdapter { |
| 731 | @Override |
| 732 | public int getDeviceCount() { |
| 733 | return 1; |
| 734 | } |
| 735 | |
| 736 | @Override |
| 737 | public Iterable<Device> getDevices() { |
| 738 | return ImmutableList.of(FOO_DEV); |
| 739 | } |
| 740 | |
| 741 | @Override |
| 742 | public Iterable<Device> getAvailableDevices() { |
| 743 | return getDevices(); |
| 744 | } |
| 745 | |
| 746 | @Override |
| 747 | public Device getDevice(DeviceId deviceId) { |
| 748 | return FOO_DEV; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | private class TestDriverManager extends DriverManager { |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 753 | TestDriverManager(DriverRegistry registry) { |
| 754 | this.registry = registry; |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 755 | this.deviceService = mgr.deviceService; |
| 756 | activate(); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | private static DeviceId lastDeviceIdProgrammable; |
| 761 | private static List<GroupOperation> groupOperations = new ArrayList<>(); |
| 762 | |
| 763 | public static class TestGroupProgrammable extends AbstractHandlerBehaviour implements GroupProgrammable { |
| 764 | @Override |
| 765 | public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) { |
| 766 | lastDeviceIdProgrammable = deviceId; |
| 767 | groupOperations.addAll(groupOps.operations()); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | public void validate(DeviceId expectedDeviceId, |
| 772 | List<GroupOperation> expectedGroupOps) { |
| 773 | if (expectedGroupOps == null) { |
| 774 | assertTrue("events generated", groupOperations.isEmpty()); |
| 775 | return; |
| 776 | } |
| 777 | |
| 778 | assertEquals(lastDeviceIdProgrammable, expectedDeviceId); |
Sho SHIMIZU | 970d6e2 | 2016-08-12 15:13:22 -0700 | [diff] [blame] | 779 | assertTrue(groupOperations.containsAll(expectedGroupOps) && |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 780 | expectedGroupOps.containsAll(groupOperations)); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 781 | |
| 782 | groupOperations.clear(); |
| 783 | lastDeviceIdProgrammable = null; |
| 784 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | |