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