Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 19 | import static org.junit.Assert.assertThat; |
| 20 | import static org.hamcrest.Matchers.is; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 21 | import static org.onosproject.net.DeviceId.deviceId; |
| 22 | |
| 23 | import java.util.ArrayList; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 24 | import java.util.Arrays; |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 25 | import java.util.Collections; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 26 | import java.util.List; |
Sho SHIMIZU | 30d639b | 2015-05-05 09:30:35 -0700 | [diff] [blame] | 27 | import java.util.Optional; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 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 | 0599d51 | 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.GroupId; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 40 | import org.onosproject.net.flow.TrafficTreatment; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 41 | import org.onosproject.net.group.DefaultGroup; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 42 | import org.onosproject.net.group.DefaultGroupBucket; |
| 43 | import org.onosproject.net.group.DefaultGroupDescription; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 44 | import org.onosproject.net.group.DefaultGroupKey; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 45 | import org.onosproject.net.group.Group; |
| 46 | import org.onosproject.net.group.GroupBucket; |
| 47 | import org.onosproject.net.group.GroupBuckets; |
| 48 | import org.onosproject.net.group.GroupDescription; |
| 49 | import org.onosproject.net.group.GroupEvent; |
| 50 | import org.onosproject.net.group.GroupKey; |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 51 | import org.onosproject.net.group.GroupOperation; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 52 | import org.onosproject.net.group.GroupStore.UpdateType; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 53 | import org.onosproject.net.group.GroupStoreDelegate; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 54 | import org.onosproject.net.group.StoredGroupBucketEntry; |
| 55 | import org.onosproject.net.group.StoredGroupEntry; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 56 | |
| 57 | import com.google.common.collect.Iterables; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Test of the simple DeviceStore implementation. |
| 61 | */ |
| 62 | public class SimpleGroupStoreTest { |
| 63 | |
| 64 | private SimpleGroupStore simpleGroupStore; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 65 | private final ApplicationId appId = |
| 66 | new DefaultApplicationId(2, "org.groupstore.test"); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 67 | |
| 68 | public static final DeviceId D1 = deviceId("of:1"); |
| 69 | |
| 70 | @Before |
| 71 | public void setUp() throws Exception { |
| 72 | simpleGroupStore = new SimpleGroupStore(); |
| 73 | simpleGroupStore.activate(); |
| 74 | } |
| 75 | |
| 76 | @After |
| 77 | public void tearDown() throws Exception { |
| 78 | simpleGroupStore.deactivate(); |
| 79 | } |
| 80 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 81 | private class InternalGroupStoreDelegate |
| 82 | implements GroupStoreDelegate { |
| 83 | private GroupId createdGroupId = null; |
| 84 | private GroupKey createdGroupKey; |
| 85 | private GroupBuckets createdBuckets; |
| 86 | private GroupEvent.Type expectedEvent; |
| 87 | |
| 88 | public InternalGroupStoreDelegate(GroupKey key, |
| 89 | GroupBuckets buckets, |
| 90 | GroupEvent.Type expectedEvent) { |
| 91 | this.createdBuckets = buckets; |
| 92 | this.createdGroupKey = key; |
| 93 | this.expectedEvent = expectedEvent; |
| 94 | } |
| 95 | @Override |
| 96 | public void notify(GroupEvent event) { |
| 97 | assertEquals(expectedEvent, event.type()); |
| 98 | assertEquals(Group.Type.SELECT, event.subject().type()); |
| 99 | assertEquals(D1, event.subject().deviceId()); |
| 100 | assertEquals(createdGroupKey, event.subject().appCookie()); |
| 101 | assertEquals(createdBuckets.buckets(), event.subject().buckets().buckets()); |
| 102 | if (expectedEvent == GroupEvent.Type.GROUP_ADD_REQUESTED) { |
| 103 | createdGroupId = event.subject().id(); |
| 104 | assertEquals(Group.GroupState.PENDING_ADD, |
| 105 | event.subject().state()); |
| 106 | } else if (expectedEvent == GroupEvent.Type.GROUP_ADDED) { |
| 107 | createdGroupId = event.subject().id(); |
| 108 | assertEquals(Group.GroupState.ADDED, |
| 109 | event.subject().state()); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 110 | } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATED) { |
| 111 | createdGroupId = event.subject().id(); |
| 112 | assertEquals(true, |
| 113 | event.subject().buckets(). |
| 114 | buckets().containsAll(createdBuckets.buckets())); |
| 115 | assertEquals(true, |
| 116 | createdBuckets.buckets(). |
| 117 | containsAll(event.subject().buckets().buckets())); |
| 118 | for (GroupBucket bucket:event.subject().buckets().buckets()) { |
Sho SHIMIZU | 30d639b | 2015-05-05 09:30:35 -0700 | [diff] [blame] | 119 | Optional<GroupBucket> matched = createdBuckets.buckets() |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 120 | .stream() |
| 121 | .filter((expected) -> expected.equals(bucket)) |
| 122 | .findFirst(); |
| 123 | assertEquals(matched.get().packets(), |
| 124 | bucket.packets()); |
| 125 | assertEquals(matched.get().bytes(), |
| 126 | bucket.bytes()); |
| 127 | } |
| 128 | assertEquals(Group.GroupState.ADDED, |
| 129 | event.subject().state()); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 130 | } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATE_REQUESTED) { |
| 131 | assertEquals(Group.GroupState.PENDING_UPDATE, |
| 132 | event.subject().state()); |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame^] | 133 | for (GroupBucket bucket:event.subject().buckets().buckets()) { |
| 134 | Optional<GroupBucket> matched = createdBuckets.buckets() |
| 135 | .stream() |
| 136 | .filter((expected) -> expected.equals(bucket)) |
| 137 | .findFirst(); |
| 138 | assertEquals(matched.get().weight(), |
| 139 | bucket.weight()); |
| 140 | assertEquals(matched.get().watchGroup(), |
| 141 | bucket.watchGroup()); |
| 142 | assertEquals(matched.get().watchPort(), |
| 143 | bucket.watchPort()); |
| 144 | } |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 145 | } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVE_REQUESTED) { |
| 146 | assertEquals(Group.GroupState.PENDING_DELETE, |
| 147 | event.subject().state()); |
| 148 | } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVED) { |
| 149 | createdGroupId = event.subject().id(); |
| 150 | assertEquals(Group.GroupState.PENDING_DELETE, |
| 151 | event.subject().state()); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 152 | } else if (expectedEvent == GroupEvent.Type.GROUP_ADD_FAILED) { |
| 153 | createdGroupId = event.subject().id(); |
| 154 | assertEquals(Group.GroupState.PENDING_ADD, |
| 155 | event.subject().state()); |
| 156 | } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATE_FAILED) { |
| 157 | createdGroupId = event.subject().id(); |
| 158 | assertEquals(Group.GroupState.PENDING_UPDATE, |
| 159 | event.subject().state()); |
| 160 | } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVE_FAILED) { |
| 161 | createdGroupId = event.subject().id(); |
| 162 | assertEquals(Group.GroupState.PENDING_DELETE, |
| 163 | event.subject().state()); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | public void verifyGroupId(GroupId id) { |
| 168 | assertEquals(createdGroupId, id); |
| 169 | } |
| 170 | } |
| 171 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 172 | /** |
| 173 | * Tests group store operations. The following operations are tested: |
| 174 | * a)Tests device group audit completion status change |
| 175 | * b)Tests storeGroup operation |
| 176 | * c)Tests getGroupCount operation |
| 177 | * d)Tests getGroup operation |
| 178 | * e)Tests getGroups operation |
| 179 | * f)Tests addOrUpdateGroupEntry operation from southbound |
| 180 | * g)Tests updateGroupDescription for ADD operation from northbound |
| 181 | * h)Tests updateGroupDescription for REMOVE operation from northbound |
| 182 | * i)Tests deleteGroupDescription operation from northbound |
| 183 | * j)Tests removeGroupEntry operation from southbound |
| 184 | */ |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 185 | @Test |
| 186 | public void testGroupStoreOperations() { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 187 | // Set the Device AUDIT completed in the store |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 188 | simpleGroupStore.deviceInitialAuditCompleted(D1, true); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 189 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 190 | // Testing storeGroup operation |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 191 | GroupKey newKey = new DefaultGroupKey("group1".getBytes()); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 192 | testStoreAndGetGroup(newKey); |
| 193 | |
| 194 | // Testing addOrUpdateGroupEntry operation from southbound |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 195 | GroupKey currKey = newKey; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 196 | testAddGroupEntryFromSB(currKey); |
| 197 | |
| 198 | // Testing updateGroupDescription for ADD operation from northbound |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 199 | newKey = new DefaultGroupKey("group1AddBuckets".getBytes()); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 200 | testAddBuckets(currKey, newKey); |
| 201 | |
| 202 | // Testing updateGroupDescription for REMOVE operation from northbound |
| 203 | currKey = newKey; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 204 | newKey = new DefaultGroupKey("group1RemoveBuckets".getBytes()); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 205 | testRemoveBuckets(currKey, newKey); |
| 206 | |
| 207 | // Testing addOrUpdateGroupEntry operation from southbound |
| 208 | currKey = newKey; |
| 209 | testUpdateGroupEntryFromSB(currKey); |
| 210 | |
| 211 | // Testing deleteGroupDescription operation from northbound |
| 212 | testDeleteGroup(currKey); |
| 213 | |
| 214 | // Testing removeGroupEntry operation from southbound |
| 215 | testRemoveGroupFromSB(currKey); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 216 | |
| 217 | // Testing removing all groups on the given device |
| 218 | newKey = new DefaultGroupKey("group1".getBytes()); |
| 219 | testStoreAndGetGroup(newKey); |
| 220 | testDeleteGroupOnDevice(newKey); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Testing storeGroup operation |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 224 | private void testStoreAndGetGroup(GroupKey key) { |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 225 | PortNumber[] ports = {PortNumber.portNumber(31), |
| 226 | PortNumber.portNumber(32)}; |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 227 | List<PortNumber> outPorts = new ArrayList<>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 228 | outPorts.addAll(Arrays.asList(ports)); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 229 | |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 230 | List<GroupBucket> buckets = new ArrayList<>(); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 231 | for (PortNumber portNumber: outPorts) { |
| 232 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 233 | tBuilder.setOutput(portNumber) |
| 234 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 235 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 236 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 237 | .setMpls(MplsLabel.mplsLabel(106)); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 238 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 239 | tBuilder.build())); |
| 240 | } |
| 241 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 242 | GroupDescription groupDesc = new DefaultGroupDescription( |
| 243 | D1, |
| 244 | Group.Type.SELECT, |
| 245 | groupBuckets, |
| 246 | key, |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 247 | null, |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 248 | appId); |
| 249 | InternalGroupStoreDelegate checkStoreGroupDelegate = |
| 250 | new InternalGroupStoreDelegate(key, |
| 251 | groupBuckets, |
| 252 | GroupEvent.Type.GROUP_ADD_REQUESTED); |
| 253 | simpleGroupStore.setDelegate(checkStoreGroupDelegate); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 254 | // Testing storeGroup operation |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 255 | simpleGroupStore.storeGroupDescription(groupDesc); |
| 256 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 257 | // Testing getGroupCount operation |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 258 | assertEquals(1, simpleGroupStore.getGroupCount(D1)); |
| 259 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 260 | // Testing getGroup operation |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 261 | Group createdGroup = simpleGroupStore.getGroup(D1, key); |
| 262 | checkStoreGroupDelegate.verifyGroupId(createdGroup.id()); |
| 263 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 264 | // Testing getGroups operation |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 265 | Iterable<Group> createdGroups = simpleGroupStore.getGroups(D1); |
| 266 | int groupCount = 0; |
| 267 | for (Group group:createdGroups) { |
| 268 | checkStoreGroupDelegate.verifyGroupId(group.id()); |
| 269 | groupCount++; |
| 270 | } |
| 271 | assertEquals(1, groupCount); |
| 272 | simpleGroupStore.unsetDelegate(checkStoreGroupDelegate); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 273 | } |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 274 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 275 | // Testing addOrUpdateGroupEntry operation from southbound |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 276 | private void testAddGroupEntryFromSB(GroupKey currKey) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 277 | Group existingGroup = simpleGroupStore.getGroup(D1, currKey); |
| 278 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 279 | InternalGroupStoreDelegate addGroupEntryDelegate = |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 280 | new InternalGroupStoreDelegate(currKey, |
| 281 | existingGroup.buckets(), |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 282 | GroupEvent.Type.GROUP_ADDED); |
| 283 | simpleGroupStore.setDelegate(addGroupEntryDelegate); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 284 | simpleGroupStore.addOrUpdateGroupEntry(existingGroup); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 285 | simpleGroupStore.unsetDelegate(addGroupEntryDelegate); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 286 | } |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 287 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 288 | // Testing addOrUpdateGroupEntry operation from southbound |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 289 | private void testUpdateGroupEntryFromSB(GroupKey currKey) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 290 | Group existingGroup = simpleGroupStore.getGroup(D1, currKey); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 291 | int totalPkts = 0; |
| 292 | int totalBytes = 0; |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 293 | List<GroupBucket> newBucketList = new ArrayList<>(); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 294 | for (GroupBucket bucket:existingGroup.buckets().buckets()) { |
| 295 | StoredGroupBucketEntry newBucket = |
| 296 | (StoredGroupBucketEntry) |
| 297 | DefaultGroupBucket.createSelectGroupBucket(bucket.treatment()); |
| 298 | newBucket.setPackets(10); |
| 299 | newBucket.setBytes(10 * 256 * 8); |
| 300 | totalPkts += 10; |
| 301 | totalBytes += 10 * 256 * 8; |
| 302 | newBucketList.add(newBucket); |
| 303 | } |
| 304 | GroupBuckets updatedBuckets = new GroupBuckets(newBucketList); |
| 305 | Group updatedGroup = new DefaultGroup(existingGroup.id(), |
| 306 | existingGroup.deviceId(), |
| 307 | existingGroup.type(), |
| 308 | updatedBuckets); |
| 309 | ((StoredGroupEntry) updatedGroup).setPackets(totalPkts); |
| 310 | ((StoredGroupEntry) updatedGroup).setBytes(totalBytes); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 311 | |
| 312 | InternalGroupStoreDelegate updateGroupEntryDelegate = |
| 313 | new InternalGroupStoreDelegate(currKey, |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 314 | updatedBuckets, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 315 | GroupEvent.Type.GROUP_UPDATED); |
| 316 | simpleGroupStore.setDelegate(updateGroupEntryDelegate); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 317 | simpleGroupStore.addOrUpdateGroupEntry(updatedGroup); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 318 | simpleGroupStore.unsetDelegate(updateGroupEntryDelegate); |
| 319 | } |
| 320 | |
| 321 | // Testing updateGroupDescription for ADD operation from northbound |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 322 | private void testAddBuckets(GroupKey currKey, GroupKey addKey) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 323 | Group existingGroup = simpleGroupStore.getGroup(D1, currKey); |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 324 | List<GroupBucket> buckets = new ArrayList<>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 325 | buckets.addAll(existingGroup.buckets().buckets()); |
| 326 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 327 | PortNumber[] newNeighborPorts = {PortNumber.portNumber(41), |
| 328 | PortNumber.portNumber(42)}; |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 329 | List<PortNumber> newOutPorts = new ArrayList<>(); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 330 | newOutPorts.addAll(Collections.singletonList(newNeighborPorts[0])); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 331 | |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 332 | List<GroupBucket> toAddBuckets = new ArrayList<>(); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 333 | for (PortNumber portNumber: newOutPorts) { |
| 334 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 335 | tBuilder.setOutput(portNumber) |
| 336 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:03")) |
| 337 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 338 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 339 | .setMpls(MplsLabel.mplsLabel(106)); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 340 | toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 341 | tBuilder.build())); |
| 342 | } |
| 343 | GroupBuckets toAddGroupBuckets = new GroupBuckets(toAddBuckets); |
| 344 | buckets.addAll(toAddBuckets); |
| 345 | GroupBuckets updatedGroupBuckets = new GroupBuckets(buckets); |
| 346 | InternalGroupStoreDelegate updateGroupDescDelegate = |
| 347 | new InternalGroupStoreDelegate(addKey, |
| 348 | updatedGroupBuckets, |
| 349 | GroupEvent.Type.GROUP_UPDATE_REQUESTED); |
| 350 | simpleGroupStore.setDelegate(updateGroupDescDelegate); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 351 | simpleGroupStore.updateGroupDescription(D1, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 352 | currKey, |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 353 | UpdateType.ADD, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 354 | toAddGroupBuckets, |
| 355 | addKey); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 356 | simpleGroupStore.unsetDelegate(updateGroupDescDelegate); |
Victor Silva | df1eeae | 2016-08-12 15:28:57 -0300 | [diff] [blame^] | 357 | |
| 358 | short weight = 5; |
| 359 | toAddBuckets = new ArrayList<>(); |
| 360 | for (PortNumber portNumber: newOutPorts) { |
| 361 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 362 | tBuilder.setOutput(portNumber) |
| 363 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:03")) |
| 364 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 365 | .pushMpls() |
| 366 | .setMpls(MplsLabel.mplsLabel(106)); |
| 367 | toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 368 | tBuilder.build(), weight)); |
| 369 | } |
| 370 | |
| 371 | toAddGroupBuckets = new GroupBuckets(toAddBuckets); |
| 372 | buckets = new ArrayList<>(); |
| 373 | buckets.addAll(existingGroup.buckets().buckets()); |
| 374 | buckets.addAll(toAddBuckets); |
| 375 | updatedGroupBuckets = new GroupBuckets(buckets); |
| 376 | updateGroupDescDelegate = |
| 377 | new InternalGroupStoreDelegate(addKey, |
| 378 | updatedGroupBuckets, |
| 379 | GroupEvent.Type.GROUP_UPDATE_REQUESTED); |
| 380 | simpleGroupStore.setDelegate(updateGroupDescDelegate); |
| 381 | simpleGroupStore.updateGroupDescription(D1, |
| 382 | addKey, |
| 383 | UpdateType.ADD, |
| 384 | toAddGroupBuckets, |
| 385 | addKey); |
| 386 | simpleGroupStore.unsetDelegate(updateGroupDescDelegate); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 387 | } |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 388 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 389 | // Testing updateGroupDescription for REMOVE operation from northbound |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 390 | private void testRemoveBuckets(GroupKey currKey, GroupKey removeKey) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 391 | Group existingGroup = simpleGroupStore.getGroup(D1, currKey); |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 392 | List<GroupBucket> buckets = new ArrayList<>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 393 | buckets.addAll(existingGroup.buckets().buckets()); |
| 394 | |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 395 | List<GroupBucket> toRemoveBuckets = new ArrayList<>(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 396 | |
| 397 | // There should be 4 buckets in the current group |
| 398 | toRemoveBuckets.add(buckets.remove(0)); |
| 399 | toRemoveBuckets.add(buckets.remove(1)); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 400 | GroupBuckets toRemoveGroupBuckets = new GroupBuckets(toRemoveBuckets); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 401 | |
| 402 | GroupBuckets remainingGroupBuckets = new GroupBuckets(buckets); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 403 | InternalGroupStoreDelegate removeGroupDescDelegate = |
| 404 | new InternalGroupStoreDelegate(removeKey, |
| 405 | remainingGroupBuckets, |
| 406 | GroupEvent.Type.GROUP_UPDATE_REQUESTED); |
| 407 | simpleGroupStore.setDelegate(removeGroupDescDelegate); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 408 | simpleGroupStore.updateGroupDescription(D1, |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 409 | currKey, |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 410 | UpdateType.REMOVE, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 411 | toRemoveGroupBuckets, |
| 412 | removeKey); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 413 | simpleGroupStore.unsetDelegate(removeGroupDescDelegate); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 414 | } |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 415 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 416 | // Testing deleteGroupDescription operation from northbound |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 417 | private void testDeleteGroup(GroupKey currKey) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 418 | Group existingGroup = simpleGroupStore.getGroup(D1, currKey); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 419 | InternalGroupStoreDelegate deleteGroupDescDelegate = |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 420 | new InternalGroupStoreDelegate(currKey, |
| 421 | existingGroup.buckets(), |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 422 | GroupEvent.Type.GROUP_REMOVE_REQUESTED); |
| 423 | simpleGroupStore.setDelegate(deleteGroupDescDelegate); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 424 | simpleGroupStore.deleteGroupDescription(D1, currKey); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 425 | simpleGroupStore.unsetDelegate(deleteGroupDescDelegate); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 426 | } |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 427 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 428 | // Testing deleteGroupDescription operation from northbound |
| 429 | private void testDeleteGroupOnDevice(GroupKey currKey) { |
| 430 | assertThat(simpleGroupStore.getGroupCount(D1), is(1)); |
| 431 | simpleGroupStore.purgeGroupEntry(D1); |
| 432 | assertThat(simpleGroupStore.getGroupCount(D1), is(0)); |
| 433 | } |
| 434 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 435 | // Testing removeGroupEntry operation from southbound |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 436 | private void testRemoveGroupFromSB(GroupKey currKey) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 437 | Group existingGroup = simpleGroupStore.getGroup(D1, currKey); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 438 | InternalGroupStoreDelegate removeGroupEntryDelegate = |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 439 | new InternalGroupStoreDelegate(currKey, |
| 440 | existingGroup.buckets(), |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 441 | GroupEvent.Type.GROUP_REMOVED); |
| 442 | simpleGroupStore.setDelegate(removeGroupEntryDelegate); |
| 443 | simpleGroupStore.removeGroupEntry(existingGroup); |
| 444 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 445 | // Testing getGroup operation |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 446 | existingGroup = simpleGroupStore.getGroup(D1, currKey); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 447 | assertEquals(null, existingGroup); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 448 | assertEquals(0, Iterables.size(simpleGroupStore.getGroups(D1))); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 449 | assertEquals(0, simpleGroupStore.getGroupCount(D1)); |
| 450 | |
| 451 | simpleGroupStore.unsetDelegate(removeGroupEntryDelegate); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | @Test |
| 455 | public void testGroupOperationFailure() { |
| 456 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 457 | simpleGroupStore.deviceInitialAuditCompleted(D1, true); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 458 | |
| 459 | ApplicationId appId = |
| 460 | new DefaultApplicationId(2, "org.groupstore.test"); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 461 | GroupKey key = new DefaultGroupKey("group1".getBytes()); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 462 | PortNumber[] ports = {PortNumber.portNumber(31), |
| 463 | PortNumber.portNumber(32)}; |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 464 | List<PortNumber> outPorts = new ArrayList<>(); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 465 | outPorts.add(ports[0]); |
| 466 | outPorts.add(ports[1]); |
| 467 | |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 468 | List<GroupBucket> buckets = new ArrayList<>(); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 469 | for (PortNumber portNumber: outPorts) { |
| 470 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 471 | tBuilder.setOutput(portNumber) |
| 472 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 473 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 474 | .pushMpls() |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 475 | .setMpls(MplsLabel.mplsLabel(106)); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 476 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 477 | tBuilder.build())); |
| 478 | } |
| 479 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 480 | GroupDescription groupDesc = new DefaultGroupDescription( |
| 481 | D1, |
| 482 | Group.Type.SELECT, |
| 483 | groupBuckets, |
| 484 | key, |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 485 | null, |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 486 | appId); |
| 487 | InternalGroupStoreDelegate checkStoreGroupDelegate = |
| 488 | new InternalGroupStoreDelegate(key, |
| 489 | groupBuckets, |
| 490 | GroupEvent.Type.GROUP_ADD_REQUESTED); |
| 491 | simpleGroupStore.setDelegate(checkStoreGroupDelegate); |
| 492 | // Testing storeGroup operation |
| 493 | simpleGroupStore.storeGroupDescription(groupDesc); |
| 494 | simpleGroupStore.unsetDelegate(checkStoreGroupDelegate); |
| 495 | |
| 496 | // Testing Group add operation failure |
| 497 | Group createdGroup = simpleGroupStore.getGroup(D1, key); |
| 498 | checkStoreGroupDelegate.verifyGroupId(createdGroup.id()); |
| 499 | |
| 500 | GroupOperation groupAddOp = GroupOperation. |
| 501 | createAddGroupOperation(createdGroup.id(), |
| 502 | createdGroup.type(), |
| 503 | createdGroup.buckets()); |
| 504 | InternalGroupStoreDelegate checkGroupAddFailureDelegate = |
| 505 | new InternalGroupStoreDelegate(key, |
| 506 | groupBuckets, |
| 507 | GroupEvent.Type.GROUP_ADD_FAILED); |
| 508 | simpleGroupStore.setDelegate(checkGroupAddFailureDelegate); |
| 509 | simpleGroupStore.groupOperationFailed(D1, groupAddOp); |
| 510 | |
| 511 | |
| 512 | // Testing Group modify operation failure |
| 513 | simpleGroupStore.unsetDelegate(checkGroupAddFailureDelegate); |
| 514 | GroupOperation groupModOp = GroupOperation. |
| 515 | createModifyGroupOperation(createdGroup.id(), |
| 516 | createdGroup.type(), |
| 517 | createdGroup.buckets()); |
| 518 | InternalGroupStoreDelegate checkGroupModFailureDelegate = |
| 519 | new InternalGroupStoreDelegate(key, |
| 520 | groupBuckets, |
| 521 | GroupEvent.Type.GROUP_UPDATE_FAILED); |
| 522 | simpleGroupStore.setDelegate(checkGroupModFailureDelegate); |
| 523 | simpleGroupStore.groupOperationFailed(D1, groupModOp); |
| 524 | |
| 525 | // Testing Group modify operation failure |
| 526 | simpleGroupStore.unsetDelegate(checkGroupModFailureDelegate); |
| 527 | GroupOperation groupDelOp = GroupOperation. |
| 528 | createDeleteGroupOperation(createdGroup.id(), |
| 529 | createdGroup.type()); |
| 530 | InternalGroupStoreDelegate checkGroupDelFailureDelegate = |
| 531 | new InternalGroupStoreDelegate(key, |
| 532 | groupBuckets, |
| 533 | GroupEvent.Type.GROUP_REMOVE_FAILED); |
| 534 | simpleGroupStore.setDelegate(checkGroupDelFailureDelegate); |
| 535 | simpleGroupStore.groupOperationFailed(D1, groupDelOp); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |