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 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.Arrays; |
| 20 | import java.util.Collections; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 27 | import org.onlab.packet.MplsLabel; |
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; |
| 32 | import org.onosproject.event.impl.TestEventDispatcher; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.PortNumber; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 35 | import org.onosproject.net.device.impl.DeviceManager; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.group.DefaultGroup; |
| 39 | import org.onosproject.net.group.DefaultGroupBucket; |
| 40 | import org.onosproject.net.group.DefaultGroupDescription; |
| 41 | import org.onosproject.net.group.Group; |
| 42 | import org.onosproject.net.group.GroupBucket; |
| 43 | import org.onosproject.net.group.GroupBuckets; |
| 44 | import org.onosproject.net.group.GroupDescription; |
| 45 | import org.onosproject.net.group.GroupEvent; |
| 46 | import org.onosproject.net.group.GroupKey; |
| 47 | import org.onosproject.net.group.GroupListener; |
| 48 | import org.onosproject.net.group.GroupOperation; |
| 49 | import org.onosproject.net.group.GroupOperations; |
| 50 | import org.onosproject.net.group.GroupProvider; |
| 51 | import org.onosproject.net.group.GroupProviderRegistry; |
| 52 | import org.onosproject.net.group.GroupProviderService; |
| 53 | import org.onosproject.net.group.GroupService; |
| 54 | import org.onosproject.net.group.StoredGroupEntry; |
| 55 | import org.onosproject.net.provider.AbstractProvider; |
| 56 | import org.onosproject.net.provider.ProviderId; |
| 57 | import org.onosproject.store.trivial.impl.SimpleGroupStore; |
| 58 | |
| 59 | import com.google.common.collect.Iterables; |
| 60 | |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 61 | import static org.junit.Assert.*; |
| 62 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Test codifying the group service & group provider service contracts. |
| 65 | */ |
| 66 | public class GroupManagerTest { |
| 67 | |
| 68 | private static final ProviderId PID = new ProviderId("of", "groupfoo"); |
| 69 | private static final DeviceId DID = DeviceId.deviceId("of:001"); |
| 70 | |
| 71 | private GroupManager mgr; |
| 72 | private GroupService groupService; |
| 73 | private GroupProviderRegistry providerRegistry; |
| 74 | private TestGroupListener internalListener = new TestGroupListener(); |
| 75 | private GroupListener listener = internalListener; |
| 76 | private TestGroupProvider internalProvider; |
| 77 | private GroupProvider provider; |
| 78 | private GroupProviderService providerService; |
| 79 | private ApplicationId appId; |
| 80 | |
| 81 | @Before |
| 82 | public void setUp() { |
| 83 | mgr = new GroupManager(); |
| 84 | groupService = mgr; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 85 | mgr.deviceService = new DeviceManager(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 86 | mgr.store = new SimpleGroupStore(); |
| 87 | mgr.eventDispatcher = new TestEventDispatcher(); |
| 88 | providerRegistry = mgr; |
| 89 | |
| 90 | mgr.activate(); |
| 91 | mgr.addListener(listener); |
| 92 | |
| 93 | internalProvider = new TestGroupProvider(PID); |
| 94 | provider = internalProvider; |
| 95 | providerService = providerRegistry.register(provider); |
| 96 | appId = new DefaultApplicationId(2, "org.groupmanager.test"); |
| 97 | assertTrue("provider should be registered", |
| 98 | providerRegistry.getProviders().contains(provider.id())); |
| 99 | } |
| 100 | |
| 101 | @After |
| 102 | public void tearDown() { |
| 103 | providerRegistry.unregister(provider); |
| 104 | assertFalse("provider should not be registered", |
| 105 | providerRegistry.getProviders().contains(provider.id())); |
| 106 | mgr.removeListener(listener); |
| 107 | mgr.deactivate(); |
| 108 | mgr.eventDispatcher = null; |
| 109 | } |
| 110 | |
| 111 | private class TestGroupKey implements GroupKey { |
| 112 | private String groupId; |
| 113 | |
| 114 | public TestGroupKey(String id) { |
| 115 | this.groupId = id; |
| 116 | } |
| 117 | |
| 118 | public String id() { |
| 119 | return this.groupId; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public int hashCode() { |
| 124 | return groupId.hashCode(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public boolean equals(Object obj) { |
| 129 | if (obj instanceof TestGroupKey) { |
| 130 | return this.groupId.equals(((TestGroupKey) obj).id()); |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Tests group service north bound and south bound interfaces. |
| 138 | * The following operations are tested: |
| 139 | * a)Tests group creation before the device group AUDIT completes |
| 140 | * b)Tests initial device group AUDIT process |
| 141 | * c)Tests deletion process of any extraneous groups |
| 142 | * d)Tests execution of any pending group creation requests |
| 143 | * after the device group AUDIT completes |
| 144 | * e)Tests re-apply process of any missing groups |
| 145 | * f)Tests event notifications after receiving confirmation for |
| 146 | * any operations from data plane |
| 147 | * g)Tests group bucket modifications (additions and deletions) |
| 148 | * h)Tests group deletion |
| 149 | */ |
| 150 | @Test |
| 151 | public void testGroupService() { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 152 | // Test Group creation before AUDIT process |
| 153 | testGroupCreationBeforeAudit(); |
| 154 | |
| 155 | // Test initial group audit process |
| 156 | testInitialAuditWithPendingGroupRequests(); |
| 157 | |
| 158 | // Test audit with extraneous and missing groups |
| 159 | testAuditWithExtraneousMissingGroups(); |
| 160 | |
| 161 | // Test audit with confirmed groups |
| 162 | testAuditWithConfirmedGroups(); |
| 163 | |
| 164 | // Test group add bucket operations |
| 165 | testAddBuckets(); |
| 166 | |
| 167 | // Test group remove bucket operations |
| 168 | testRemoveBuckets(); |
| 169 | |
| 170 | // Test group remove operations |
| 171 | testRemoveGroup(); |
| 172 | } |
| 173 | |
| 174 | // Test Group creation before AUDIT process |
| 175 | private void testGroupCreationBeforeAudit() { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 176 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
| 177 | PortNumber.portNumber(32)}; |
| 178 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
| 179 | PortNumber.portNumber(42)}; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 180 | TestGroupKey key = new TestGroupKey("group1BeforeAudit"); |
| 181 | List<GroupBucket> buckets = new ArrayList<GroupBucket>(); |
| 182 | List<PortNumber> outPorts = new ArrayList<PortNumber>(); |
| 183 | outPorts.addAll(Arrays.asList(ports1)); |
| 184 | outPorts.addAll(Arrays.asList(ports2)); |
| 185 | for (PortNumber portNumber: outPorts) { |
| 186 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 187 | tBuilder.setOutput(portNumber) |
| 188 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 189 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 190 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 191 | .setMpls(MplsLabel.mplsLabel(106)); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 192 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 193 | tBuilder.build())); |
| 194 | } |
| 195 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 196 | GroupDescription newGroupDesc = new DefaultGroupDescription(DID, |
| 197 | Group.Type.SELECT, |
| 198 | groupBuckets, |
| 199 | key, |
| 200 | appId); |
| 201 | groupService.addGroup(newGroupDesc); |
| 202 | internalProvider.validate(DID, null); |
| 203 | assertEquals(null, groupService.getGroup(DID, key)); |
| 204 | assertEquals(0, Iterables.size(groupService.getGroups(DID, appId))); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 205 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 206 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 207 | // Test initial AUDIT process with pending group requests |
| 208 | private void testInitialAuditWithPendingGroupRequests() { |
| 209 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
| 210 | PortNumber.portNumber(32)}; |
| 211 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
| 212 | PortNumber.portNumber(42)}; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 213 | GroupId gId1 = new DefaultGroupId(1); |
| 214 | Group group1 = createSouthboundGroupEntry(gId1, |
| 215 | Arrays.asList(ports1), |
| 216 | 0); |
| 217 | GroupId gId2 = new DefaultGroupId(2); |
| 218 | // Non zero reference count will make the group manager to queue |
| 219 | // the extraneous groups until reference count is zero. |
| 220 | Group group2 = createSouthboundGroupEntry(gId2, |
| 221 | Arrays.asList(ports2), |
| 222 | 2); |
| 223 | List<Group> groupEntries = Arrays.asList(group1, group2); |
| 224 | providerService.pushGroupMetrics(DID, groupEntries); |
| 225 | // First group metrics would trigger the device audit completion |
| 226 | // post which all pending group requests are also executed. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 227 | TestGroupKey key = new TestGroupKey("group1BeforeAudit"); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 228 | Group createdGroup = groupService.getGroup(DID, key); |
| 229 | int createdGroupId = createdGroup.id().id(); |
| 230 | assertNotEquals(gId1.id(), createdGroupId); |
| 231 | assertNotEquals(gId2.id(), createdGroupId); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 232 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 233 | List<GroupOperation> expectedGroupOps = Arrays.asList( |
| 234 | GroupOperation.createDeleteGroupOperation(gId1, |
| 235 | Group.Type.SELECT), |
| 236 | GroupOperation.createAddGroupOperation( |
| 237 | createdGroup.id(), |
| 238 | Group.Type.SELECT, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 239 | createdGroup.buckets())); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 240 | internalProvider.validate(DID, expectedGroupOps); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 241 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 242 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 243 | // Test AUDIT process with extraneous groups and missing groups |
| 244 | private void testAuditWithExtraneousMissingGroups() { |
| 245 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
| 246 | PortNumber.portNumber(32)}; |
| 247 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
| 248 | PortNumber.portNumber(42)}; |
| 249 | GroupId gId1 = new DefaultGroupId(1); |
| 250 | Group group1 = createSouthboundGroupEntry(gId1, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 251 | Arrays.asList(ports1), |
| 252 | 0); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 253 | GroupId gId2 = new DefaultGroupId(2); |
| 254 | Group group2 = createSouthboundGroupEntry(gId2, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 255 | Arrays.asList(ports2), |
| 256 | 0); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 257 | List<Group> groupEntries = Arrays.asList(group1, group2); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 258 | providerService.pushGroupMetrics(DID, groupEntries); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 259 | TestGroupKey key = new TestGroupKey("group1BeforeAudit"); |
| 260 | Group createdGroup = groupService.getGroup(DID, key); |
| 261 | List<GroupOperation> expectedGroupOps = Arrays.asList( |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 262 | GroupOperation.createDeleteGroupOperation(gId1, |
| 263 | Group.Type.SELECT), |
| 264 | GroupOperation.createDeleteGroupOperation(gId2, |
| 265 | Group.Type.SELECT), |
| 266 | GroupOperation.createAddGroupOperation(createdGroup.id(), |
| 267 | Group.Type.SELECT, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 268 | createdGroup.buckets())); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 269 | internalProvider.validate(DID, expectedGroupOps); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 270 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 271 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 272 | // Test AUDIT with confirmed groups |
| 273 | private void testAuditWithConfirmedGroups() { |
| 274 | TestGroupKey key = new TestGroupKey("group1BeforeAudit"); |
| 275 | Group createdGroup = groupService.getGroup(DID, key); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 276 | createdGroup = new DefaultGroup(createdGroup.id(), |
| 277 | DID, |
| 278 | Group.Type.SELECT, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 279 | createdGroup.buckets()); |
| 280 | List<Group> groupEntries = Arrays.asList(createdGroup); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 281 | providerService.pushGroupMetrics(DID, groupEntries); |
| 282 | internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_ADDED)); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 283 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 284 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 285 | // Test group add bucket operations |
| 286 | private void testAddBuckets() { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 287 | TestGroupKey addKey = new TestGroupKey("group1AddBuckets"); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 288 | |
| 289 | TestGroupKey prevKey = new TestGroupKey("group1BeforeAudit"); |
| 290 | Group createdGroup = groupService.getGroup(DID, prevKey); |
| 291 | List<GroupBucket> buckets = new ArrayList<GroupBucket>(); |
| 292 | buckets.addAll(createdGroup.buckets().buckets()); |
| 293 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 294 | PortNumber[] addPorts = {PortNumber.portNumber(51), |
| 295 | PortNumber.portNumber(52)}; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 296 | List<PortNumber> outPorts = new ArrayList<PortNumber>(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 297 | outPorts.addAll(Arrays.asList(addPorts)); |
| 298 | List<GroupBucket> addBuckets = new ArrayList<GroupBucket>(); |
| 299 | for (PortNumber portNumber: outPorts) { |
| 300 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 301 | tBuilder.setOutput(portNumber) |
| 302 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 303 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 304 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 305 | .setMpls(MplsLabel.mplsLabel(106)); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 306 | addBuckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 307 | tBuilder.build())); |
| 308 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 309 | tBuilder.build())); |
| 310 | } |
| 311 | GroupBuckets groupAddBuckets = new GroupBuckets(addBuckets); |
| 312 | groupService.addBucketsToGroup(DID, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 313 | prevKey, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 314 | groupAddBuckets, |
| 315 | addKey, |
| 316 | appId); |
| 317 | GroupBuckets updatedBuckets = new GroupBuckets(buckets); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 318 | List<GroupOperation> expectedGroupOps = Arrays.asList( |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 319 | GroupOperation.createModifyGroupOperation(createdGroup.id(), |
| 320 | Group.Type.SELECT, |
| 321 | updatedBuckets)); |
| 322 | internalProvider.validate(DID, expectedGroupOps); |
| 323 | Group existingGroup = groupService.getGroup(DID, addKey); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 324 | List<Group> groupEntries = Arrays.asList(existingGroup); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 325 | providerService.pushGroupMetrics(DID, groupEntries); |
| 326 | internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_UPDATED)); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 327 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 328 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 329 | // Test group remove bucket operations |
| 330 | private void testRemoveBuckets() { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 331 | TestGroupKey removeKey = new TestGroupKey("group1RemoveBuckets"); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 332 | |
| 333 | TestGroupKey prevKey = new TestGroupKey("group1AddBuckets"); |
| 334 | Group createdGroup = groupService.getGroup(DID, prevKey); |
| 335 | List<GroupBucket> buckets = new ArrayList<GroupBucket>(); |
| 336 | buckets.addAll(createdGroup.buckets().buckets()); |
| 337 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 338 | PortNumber[] removePorts = {PortNumber.portNumber(31), |
| 339 | PortNumber.portNumber(32)}; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 340 | List<PortNumber> outPorts = new ArrayList<PortNumber>(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 341 | outPorts.addAll(Arrays.asList(removePorts)); |
| 342 | List<GroupBucket> removeBuckets = new ArrayList<GroupBucket>(); |
| 343 | for (PortNumber portNumber: outPorts) { |
| 344 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 345 | tBuilder.setOutput(portNumber) |
| 346 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 347 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 348 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 349 | .setMpls(MplsLabel.mplsLabel(106)); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 350 | removeBuckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 351 | tBuilder.build())); |
| 352 | buckets.remove(DefaultGroupBucket.createSelectGroupBucket( |
| 353 | tBuilder.build())); |
| 354 | } |
| 355 | GroupBuckets groupRemoveBuckets = new GroupBuckets(removeBuckets); |
| 356 | groupService.removeBucketsFromGroup(DID, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 357 | prevKey, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 358 | groupRemoveBuckets, |
| 359 | removeKey, |
| 360 | appId); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 361 | GroupBuckets updatedBuckets = new GroupBuckets(buckets); |
| 362 | List<GroupOperation> expectedGroupOps = Arrays.asList( |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 363 | GroupOperation.createModifyGroupOperation(createdGroup.id(), |
| 364 | Group.Type.SELECT, |
| 365 | updatedBuckets)); |
| 366 | internalProvider.validate(DID, expectedGroupOps); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 367 | Group existingGroup = groupService.getGroup(DID, removeKey); |
| 368 | List<Group> groupEntries = Arrays.asList(existingGroup); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 369 | providerService.pushGroupMetrics(DID, groupEntries); |
| 370 | internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_UPDATED)); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 371 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 372 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 373 | // Test group remove operations |
| 374 | private void testRemoveGroup() { |
| 375 | TestGroupKey currKey = new TestGroupKey("group1RemoveBuckets"); |
| 376 | Group existingGroup = groupService.getGroup(DID, currKey); |
| 377 | groupService.removeGroup(DID, currKey, appId); |
| 378 | List<GroupOperation> expectedGroupOps = Arrays.asList( |
| 379 | GroupOperation.createDeleteGroupOperation(existingGroup.id(), |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 380 | Group.Type.SELECT)); |
| 381 | internalProvider.validate(DID, expectedGroupOps); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 382 | List<Group> groupEntries = Collections.emptyList(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 383 | providerService.pushGroupMetrics(DID, groupEntries); |
| 384 | internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_REMOVED)); |
| 385 | } |
| 386 | |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 387 | /** |
| 388 | * Test GroupOperationFailure function in Group Manager. |
| 389 | * a)GroupAddFailure |
| 390 | * b)GroupUpdateFailure |
| 391 | * c)GroupRemoteFailure |
| 392 | */ |
| 393 | @Test |
| 394 | public void testGroupOperationFailure() { |
| 395 | PortNumber[] ports1 = {PortNumber.portNumber(31), |
| 396 | PortNumber.portNumber(32)}; |
| 397 | PortNumber[] ports2 = {PortNumber.portNumber(41), |
| 398 | PortNumber.portNumber(42)}; |
| 399 | // Test Group creation before AUDIT process |
| 400 | TestGroupKey key = new TestGroupKey("group1BeforeAudit"); |
| 401 | List<GroupBucket> buckets = new ArrayList<GroupBucket>(); |
| 402 | List<PortNumber> outPorts = new ArrayList<PortNumber>(); |
| 403 | outPorts.addAll(Arrays.asList(ports1)); |
| 404 | outPorts.addAll(Arrays.asList(ports2)); |
| 405 | for (PortNumber portNumber: outPorts) { |
| 406 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 407 | tBuilder.setOutput(portNumber) |
| 408 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 409 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 410 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 411 | .setMpls(MplsLabel.mplsLabel(106)); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 412 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 413 | tBuilder.build())); |
| 414 | } |
| 415 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 416 | GroupDescription newGroupDesc = new DefaultGroupDescription(DID, |
| 417 | Group.Type.SELECT, |
| 418 | groupBuckets, |
| 419 | key, |
| 420 | appId); |
| 421 | groupService.addGroup(newGroupDesc); |
| 422 | |
| 423 | // Test initial group audit process |
| 424 | GroupId gId1 = new DefaultGroupId(1); |
| 425 | Group group1 = createSouthboundGroupEntry(gId1, |
| 426 | Arrays.asList(ports1), |
| 427 | 0); |
| 428 | GroupId gId2 = new DefaultGroupId(2); |
| 429 | // Non zero reference count will make the group manager to queue |
| 430 | // the extraneous groups until reference count is zero. |
| 431 | Group group2 = createSouthboundGroupEntry(gId2, |
| 432 | Arrays.asList(ports2), |
| 433 | 2); |
| 434 | List<Group> groupEntries = Arrays.asList(group1, group2); |
| 435 | providerService.pushGroupMetrics(DID, groupEntries); |
| 436 | Group createdGroup = groupService.getGroup(DID, key); |
| 437 | |
| 438 | // Group Add failure test |
| 439 | GroupOperation groupAddOp = GroupOperation. |
| 440 | createAddGroupOperation(createdGroup.id(), |
| 441 | createdGroup.type(), |
| 442 | createdGroup.buckets()); |
| 443 | providerService.groupOperationFailed(DID, groupAddOp); |
| 444 | internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_ADD_FAILED)); |
| 445 | |
| 446 | // Group Mod failure test |
| 447 | groupService.addGroup(newGroupDesc); |
| 448 | createdGroup = groupService.getGroup(DID, key); |
| 449 | assertNotNull(createdGroup); |
| 450 | |
| 451 | GroupOperation groupModOp = GroupOperation. |
| 452 | createModifyGroupOperation(createdGroup.id(), |
| 453 | createdGroup.type(), |
| 454 | createdGroup.buckets()); |
| 455 | providerService.groupOperationFailed(DID, groupModOp); |
| 456 | internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_UPDATE_FAILED)); |
| 457 | |
| 458 | // Group Delete failure test |
| 459 | groupService.addGroup(newGroupDesc); |
| 460 | createdGroup = groupService.getGroup(DID, key); |
| 461 | assertNotNull(createdGroup); |
| 462 | |
| 463 | GroupOperation groupDelOp = GroupOperation. |
| 464 | createDeleteGroupOperation(createdGroup.id(), |
| 465 | createdGroup.type()); |
| 466 | providerService.groupOperationFailed(DID, groupDelOp); |
| 467 | internalListener.validateEvent(Arrays.asList(GroupEvent.Type.GROUP_REMOVE_FAILED)); |
| 468 | |
| 469 | } |
| 470 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 471 | private Group createSouthboundGroupEntry(GroupId gId, |
| 472 | List<PortNumber> ports, |
| 473 | long referenceCount) { |
| 474 | List<PortNumber> outPorts = new ArrayList<PortNumber>(); |
| 475 | outPorts.addAll(ports); |
| 476 | |
| 477 | List<GroupBucket> buckets = new ArrayList<GroupBucket>(); |
| 478 | for (PortNumber portNumber: outPorts) { |
| 479 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 480 | tBuilder.setOutput(portNumber) |
| 481 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 482 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 483 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 484 | .setMpls(MplsLabel.mplsLabel(106)); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 485 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 486 | tBuilder.build())); |
| 487 | } |
| 488 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 489 | StoredGroupEntry group = new DefaultGroup( |
| 490 | gId, DID, Group.Type.SELECT, groupBuckets); |
| 491 | group.setReferenceCount(referenceCount); |
| 492 | return group; |
| 493 | } |
| 494 | |
| 495 | private static class TestGroupListener implements GroupListener { |
| 496 | final List<GroupEvent> events = new ArrayList<>(); |
| 497 | |
| 498 | @Override |
| 499 | public void event(GroupEvent event) { |
| 500 | events.add(event); |
| 501 | } |
| 502 | |
| 503 | public void validateEvent(List<GroupEvent.Type> expectedEvents) { |
| 504 | int i = 0; |
| 505 | System.err.println("events :" + events); |
| 506 | for (GroupEvent e : events) { |
| 507 | assertEquals("unexpected event", expectedEvents.get(i), e.type()); |
| 508 | i++; |
| 509 | } |
| 510 | assertEquals("mispredicted number of events", |
| 511 | expectedEvents.size(), events.size()); |
| 512 | events.clear(); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | private class TestGroupProvider |
| 517 | extends AbstractProvider implements GroupProvider { |
| 518 | DeviceId lastDeviceId; |
| 519 | List<GroupOperation> groupOperations = new ArrayList<GroupOperation>(); |
| 520 | |
| 521 | protected TestGroupProvider(ProviderId id) { |
| 522 | super(id); |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public void performGroupOperation(DeviceId deviceId, |
| 527 | GroupOperations groupOps) { |
| 528 | lastDeviceId = deviceId; |
| 529 | groupOperations.addAll(groupOps.operations()); |
| 530 | } |
| 531 | |
| 532 | public void validate(DeviceId expectedDeviceId, |
| 533 | List<GroupOperation> expectedGroupOps) { |
| 534 | if (expectedGroupOps == null) { |
| 535 | assertTrue("events generated", groupOperations.isEmpty()); |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | assertEquals(lastDeviceId, expectedDeviceId); |
| 540 | assertTrue((this.groupOperations.containsAll(expectedGroupOps) && |
| 541 | expectedGroupOps.containsAll(groupOperations))); |
| 542 | |
| 543 | groupOperations.clear(); |
| 544 | lastDeviceId = null; |
| 545 | } |
| 546 | |
| 547 | } |
| 548 | |
| 549 | } |
| 550 | |
| 551 | |