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