Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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.store.trivial.impl; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.onosproject.net.DeviceId.deviceId; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.DefaultApplicationId; |
| 30 | import org.onosproject.core.GroupId; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
| 35 | import org.onosproject.net.group.DefaultGroupBucket; |
| 36 | import org.onosproject.net.group.DefaultGroupDescription; |
| 37 | import org.onosproject.net.group.Group; |
| 38 | import org.onosproject.net.group.GroupBucket; |
| 39 | import org.onosproject.net.group.GroupBuckets; |
| 40 | import org.onosproject.net.group.GroupDescription; |
| 41 | import org.onosproject.net.group.GroupEvent; |
| 42 | import org.onosproject.net.group.GroupKey; |
| 43 | import org.onosproject.net.group.GroupStoreDelegate; |
| 44 | import org.onosproject.net.group.GroupStore.UpdateType; |
| 45 | |
| 46 | /** |
| 47 | * Test of the simple DeviceStore implementation. |
| 48 | */ |
| 49 | public class SimpleGroupStoreTest { |
| 50 | |
| 51 | private SimpleGroupStore simpleGroupStore; |
| 52 | |
| 53 | public static final DeviceId D1 = deviceId("of:1"); |
| 54 | |
| 55 | @Before |
| 56 | public void setUp() throws Exception { |
| 57 | simpleGroupStore = new SimpleGroupStore(); |
| 58 | simpleGroupStore.activate(); |
| 59 | } |
| 60 | |
| 61 | @After |
| 62 | public void tearDown() throws Exception { |
| 63 | simpleGroupStore.deactivate(); |
| 64 | } |
| 65 | |
| 66 | public class TestGroupKey implements GroupKey { |
| 67 | private String groupId; |
| 68 | |
| 69 | public TestGroupKey(String id) { |
| 70 | this.groupId = id; |
| 71 | } |
| 72 | |
| 73 | public String id() { |
| 74 | return this.groupId; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public int hashCode() { |
| 79 | return groupId.hashCode(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean equals(Object obj) { |
| 84 | if (obj instanceof TestGroupKey) { |
| 85 | return this.groupId.equals(((TestGroupKey) obj).id()); |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private class InternalGroupStoreDelegate |
| 92 | implements GroupStoreDelegate { |
| 93 | private GroupId createdGroupId = null; |
| 94 | private GroupKey createdGroupKey; |
| 95 | private GroupBuckets createdBuckets; |
| 96 | private GroupEvent.Type expectedEvent; |
| 97 | |
| 98 | public InternalGroupStoreDelegate(GroupKey key, |
| 99 | GroupBuckets buckets, |
| 100 | GroupEvent.Type expectedEvent) { |
| 101 | this.createdBuckets = buckets; |
| 102 | this.createdGroupKey = key; |
| 103 | this.expectedEvent = expectedEvent; |
| 104 | } |
| 105 | @Override |
| 106 | public void notify(GroupEvent event) { |
| 107 | assertEquals(expectedEvent, event.type()); |
| 108 | assertEquals(Group.Type.SELECT, event.subject().type()); |
| 109 | assertEquals(D1, event.subject().deviceId()); |
| 110 | assertEquals(createdGroupKey, event.subject().appCookie()); |
| 111 | assertEquals(createdBuckets.buckets(), event.subject().buckets().buckets()); |
| 112 | if (expectedEvent == GroupEvent.Type.GROUP_ADD_REQUESTED) { |
| 113 | createdGroupId = event.subject().id(); |
| 114 | assertEquals(Group.GroupState.PENDING_ADD, |
| 115 | event.subject().state()); |
| 116 | } else if (expectedEvent == GroupEvent.Type.GROUP_ADDED) { |
| 117 | createdGroupId = event.subject().id(); |
| 118 | assertEquals(Group.GroupState.ADDED, |
| 119 | event.subject().state()); |
| 120 | } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATE_REQUESTED) { |
| 121 | assertEquals(Group.GroupState.PENDING_UPDATE, |
| 122 | event.subject().state()); |
| 123 | } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVE_REQUESTED) { |
| 124 | assertEquals(Group.GroupState.PENDING_DELETE, |
| 125 | event.subject().state()); |
| 126 | } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVED) { |
| 127 | createdGroupId = event.subject().id(); |
| 128 | assertEquals(Group.GroupState.PENDING_DELETE, |
| 129 | event.subject().state()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | public void verifyGroupId(GroupId id) { |
| 134 | assertEquals(createdGroupId, id); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testGroupStoreOperations() { |
| 140 | ApplicationId appId = |
| 141 | new DefaultApplicationId(2, "org.groupstore.test"); |
| 142 | TestGroupKey key = new TestGroupKey("group1"); |
| 143 | PortNumber[] ports = {PortNumber.portNumber(31), |
| 144 | PortNumber.portNumber(32)}; |
| 145 | List<PortNumber> outPorts = new ArrayList<PortNumber>(); |
| 146 | outPorts.add(ports[0]); |
| 147 | outPorts.add(ports[1]); |
| 148 | |
| 149 | List<GroupBucket> buckets = new ArrayList<GroupBucket>(); |
| 150 | for (PortNumber portNumber: outPorts) { |
| 151 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 152 | tBuilder.setOutput(portNumber) |
| 153 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:02")) |
| 154 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 155 | .pushMpls() |
| 156 | .setMpls(106); |
| 157 | buckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 158 | tBuilder.build())); |
| 159 | } |
| 160 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 161 | GroupDescription groupDesc = new DefaultGroupDescription( |
| 162 | D1, |
| 163 | Group.Type.SELECT, |
| 164 | groupBuckets, |
| 165 | key, |
| 166 | appId); |
| 167 | InternalGroupStoreDelegate checkStoreGroupDelegate = |
| 168 | new InternalGroupStoreDelegate(key, |
| 169 | groupBuckets, |
| 170 | GroupEvent.Type.GROUP_ADD_REQUESTED); |
| 171 | simpleGroupStore.setDelegate(checkStoreGroupDelegate); |
| 172 | /* Testing storeGroup operation */ |
| 173 | simpleGroupStore.storeGroupDescription(groupDesc); |
| 174 | |
| 175 | /* Testing getGroupCount operation */ |
| 176 | assertEquals(1, simpleGroupStore.getGroupCount(D1)); |
| 177 | |
| 178 | /* Testing getGroup operation */ |
| 179 | Group createdGroup = simpleGroupStore.getGroup(D1, key); |
| 180 | checkStoreGroupDelegate.verifyGroupId(createdGroup.id()); |
| 181 | |
| 182 | /* Testing getGroups operation */ |
| 183 | Iterable<Group> createdGroups = simpleGroupStore.getGroups(D1); |
| 184 | int groupCount = 0; |
| 185 | for (Group group:createdGroups) { |
| 186 | checkStoreGroupDelegate.verifyGroupId(group.id()); |
| 187 | groupCount++; |
| 188 | } |
| 189 | assertEquals(1, groupCount); |
| 190 | simpleGroupStore.unsetDelegate(checkStoreGroupDelegate); |
| 191 | |
| 192 | /* Testing addOrUpdateGroupEntry operation from southbound */ |
| 193 | InternalGroupStoreDelegate addGroupEntryDelegate = |
| 194 | new InternalGroupStoreDelegate(key, |
| 195 | groupBuckets, |
| 196 | GroupEvent.Type.GROUP_ADDED); |
| 197 | simpleGroupStore.setDelegate(addGroupEntryDelegate); |
| 198 | simpleGroupStore.addOrUpdateGroupEntry(createdGroup); |
| 199 | simpleGroupStore.unsetDelegate(addGroupEntryDelegate); |
| 200 | |
| 201 | /* Testing updateGroupDescription for ADD operation from northbound */ |
| 202 | TestGroupKey addKey = new TestGroupKey("group1AddBuckets"); |
| 203 | PortNumber[] newNeighborPorts = {PortNumber.portNumber(41), |
| 204 | PortNumber.portNumber(42)}; |
| 205 | List<PortNumber> newOutPorts = new ArrayList<PortNumber>(); |
| 206 | newOutPorts.add(newNeighborPorts[0]); |
| 207 | newOutPorts.add(newNeighborPorts[1]); |
| 208 | |
| 209 | List<GroupBucket> toAddBuckets = new ArrayList<GroupBucket>(); |
| 210 | for (PortNumber portNumber: newOutPorts) { |
| 211 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 212 | tBuilder.setOutput(portNumber) |
| 213 | .setEthDst(MacAddress.valueOf("00:00:00:00:00:03")) |
| 214 | .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01")) |
| 215 | .pushMpls() |
| 216 | .setMpls(106); |
| 217 | toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket( |
| 218 | tBuilder.build())); |
| 219 | } |
| 220 | GroupBuckets toAddGroupBuckets = new GroupBuckets(toAddBuckets); |
| 221 | buckets.addAll(toAddBuckets); |
| 222 | GroupBuckets updatedGroupBuckets = new GroupBuckets(buckets); |
| 223 | InternalGroupStoreDelegate updateGroupDescDelegate = |
| 224 | new InternalGroupStoreDelegate(addKey, |
| 225 | updatedGroupBuckets, |
| 226 | GroupEvent.Type.GROUP_UPDATE_REQUESTED); |
| 227 | simpleGroupStore.setDelegate(updateGroupDescDelegate); |
| 228 | GroupDescription newGroupDesc = new DefaultGroupDescription( |
| 229 | D1, |
| 230 | Group.Type.SELECT, |
| 231 | toAddGroupBuckets, |
| 232 | addKey, |
| 233 | appId); |
| 234 | simpleGroupStore.updateGroupDescription(D1, |
| 235 | key, |
| 236 | UpdateType.ADD, |
| 237 | newGroupDesc); |
| 238 | simpleGroupStore.unsetDelegate(updateGroupDescDelegate); |
| 239 | |
| 240 | /* Testing updateGroupDescription for REMOVE operation from northbound */ |
| 241 | TestGroupKey removeKey = new TestGroupKey("group1RemoveBuckets"); |
| 242 | List<GroupBucket> toRemoveBuckets = new ArrayList<GroupBucket>(); |
| 243 | toRemoveBuckets.add(updatedGroupBuckets.buckets().get(0)); |
| 244 | toRemoveBuckets.add(updatedGroupBuckets.buckets().get(1)); |
| 245 | GroupBuckets toRemoveGroupBuckets = new GroupBuckets(toRemoveBuckets); |
| 246 | List<GroupBucket> remainingBuckets = new ArrayList<GroupBucket>(); |
| 247 | remainingBuckets.add(updatedGroupBuckets.buckets().get(2)); |
| 248 | remainingBuckets.add(updatedGroupBuckets.buckets().get(3)); |
| 249 | GroupBuckets remainingGroupBuckets = new GroupBuckets(remainingBuckets); |
| 250 | InternalGroupStoreDelegate removeGroupDescDelegate = |
| 251 | new InternalGroupStoreDelegate(removeKey, |
| 252 | remainingGroupBuckets, |
| 253 | GroupEvent.Type.GROUP_UPDATE_REQUESTED); |
| 254 | simpleGroupStore.setDelegate(removeGroupDescDelegate); |
| 255 | GroupDescription removeGroupDesc = new DefaultGroupDescription( |
| 256 | D1, |
| 257 | Group.Type.SELECT, |
| 258 | toRemoveGroupBuckets, |
| 259 | removeKey, |
| 260 | appId); |
| 261 | simpleGroupStore.updateGroupDescription(D1, |
| 262 | addKey, |
| 263 | UpdateType.REMOVE, |
| 264 | removeGroupDesc); |
| 265 | simpleGroupStore.unsetDelegate(removeGroupDescDelegate); |
| 266 | |
| 267 | /* Testing getGroup operation */ |
| 268 | Group existingGroup = simpleGroupStore.getGroup(D1, removeKey); |
| 269 | checkStoreGroupDelegate.verifyGroupId(existingGroup.id()); |
| 270 | |
| 271 | /* Testing addOrUpdateGroupEntry operation from southbound */ |
| 272 | InternalGroupStoreDelegate updateGroupEntryDelegate = |
| 273 | new InternalGroupStoreDelegate(removeKey, |
| 274 | remainingGroupBuckets, |
| 275 | GroupEvent.Type.GROUP_UPDATED); |
| 276 | simpleGroupStore.setDelegate(updateGroupEntryDelegate); |
| 277 | simpleGroupStore.addOrUpdateGroupEntry(existingGroup); |
| 278 | simpleGroupStore.unsetDelegate(updateGroupEntryDelegate); |
| 279 | |
| 280 | /* Testing deleteGroupDescription operation from northbound */ |
| 281 | InternalGroupStoreDelegate deleteGroupDescDelegate = |
| 282 | new InternalGroupStoreDelegate(removeKey, |
| 283 | remainingGroupBuckets, |
| 284 | GroupEvent.Type.GROUP_REMOVE_REQUESTED); |
| 285 | simpleGroupStore.setDelegate(deleteGroupDescDelegate); |
| 286 | simpleGroupStore.deleteGroupDescription(D1, removeKey); |
| 287 | simpleGroupStore.unsetDelegate(deleteGroupDescDelegate); |
| 288 | |
| 289 | /* Testing removeGroupEntry operation from southbound */ |
| 290 | InternalGroupStoreDelegate removeGroupEntryDelegate = |
| 291 | new InternalGroupStoreDelegate(removeKey, |
| 292 | remainingGroupBuckets, |
| 293 | GroupEvent.Type.GROUP_REMOVED); |
| 294 | simpleGroupStore.setDelegate(removeGroupEntryDelegate); |
| 295 | simpleGroupStore.removeGroupEntry(existingGroup); |
| 296 | |
| 297 | /* Testing getGroup operation */ |
| 298 | existingGroup = simpleGroupStore.getGroup(D1, removeKey); |
| 299 | assertEquals(null, existingGroup); |
| 300 | Iterable<Group> existingGroups = simpleGroupStore.getGroups(D1); |
| 301 | groupCount = 0; |
| 302 | for (Group tmp:existingGroups) { |
| 303 | /* To avoid warning */ |
| 304 | assertEquals(null, tmp); |
| 305 | groupCount++; |
| 306 | } |
| 307 | assertEquals(0, groupCount); |
| 308 | assertEquals(0, simpleGroupStore.getGroupCount(D1)); |
| 309 | |
| 310 | simpleGroupStore.unsetDelegate(removeGroupEntryDelegate); |
| 311 | |
| 312 | } |
| 313 | } |
| 314 | |