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