blob: 277e1ca8187fe96f140944fb2d853ae8f4b74904 [file] [log] [blame]
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -08001/*
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 */
16package org.onosproject.store.trivial.impl;
17
18import static org.junit.Assert.assertEquals;
19import static org.onosproject.net.DeviceId.deviceId;
20
21import java.util.ArrayList;
22import java.util.List;
23
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.packet.MacAddress;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.DefaultApplicationId;
30import org.onosproject.core.GroupId;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.group.DefaultGroupBucket;
36import org.onosproject.net.group.DefaultGroupDescription;
37import org.onosproject.net.group.Group;
38import org.onosproject.net.group.GroupBucket;
39import org.onosproject.net.group.GroupBuckets;
40import org.onosproject.net.group.GroupDescription;
41import org.onosproject.net.group.GroupEvent;
42import org.onosproject.net.group.GroupKey;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080043import org.onosproject.net.group.GroupStore.UpdateType;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080044import org.onosproject.net.group.GroupStoreDelegate;
45
46import com.google.common.collect.Iterables;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080047
48/**
49 * Test of the simple DeviceStore implementation.
50 */
51public class SimpleGroupStoreTest {
52
53 private SimpleGroupStore simpleGroupStore;
54
55 public static final DeviceId D1 = deviceId("of:1");
56
57 @Before
58 public void setUp() throws Exception {
59 simpleGroupStore = new SimpleGroupStore();
60 simpleGroupStore.activate();
61 }
62
63 @After
64 public void tearDown() throws Exception {
65 simpleGroupStore.deactivate();
66 }
67
68 public class TestGroupKey implements GroupKey {
69 private String groupId;
70
71 public TestGroupKey(String id) {
72 this.groupId = id;
73 }
74
75 public String id() {
76 return this.groupId;
77 }
78
79 @Override
80 public int hashCode() {
81 return groupId.hashCode();
82 }
83
84 @Override
85 public boolean equals(Object obj) {
86 if (obj instanceof TestGroupKey) {
87 return this.groupId.equals(((TestGroupKey) obj).id());
88 }
89 return false;
90 }
91 }
92
93 private class InternalGroupStoreDelegate
94 implements GroupStoreDelegate {
95 private GroupId createdGroupId = null;
96 private GroupKey createdGroupKey;
97 private GroupBuckets createdBuckets;
98 private GroupEvent.Type expectedEvent;
99
100 public InternalGroupStoreDelegate(GroupKey key,
101 GroupBuckets buckets,
102 GroupEvent.Type expectedEvent) {
103 this.createdBuckets = buckets;
104 this.createdGroupKey = key;
105 this.expectedEvent = expectedEvent;
106 }
107 @Override
108 public void notify(GroupEvent event) {
109 assertEquals(expectedEvent, event.type());
110 assertEquals(Group.Type.SELECT, event.subject().type());
111 assertEquals(D1, event.subject().deviceId());
112 assertEquals(createdGroupKey, event.subject().appCookie());
113 assertEquals(createdBuckets.buckets(), event.subject().buckets().buckets());
114 if (expectedEvent == GroupEvent.Type.GROUP_ADD_REQUESTED) {
115 createdGroupId = event.subject().id();
116 assertEquals(Group.GroupState.PENDING_ADD,
117 event.subject().state());
118 } else if (expectedEvent == GroupEvent.Type.GROUP_ADDED) {
119 createdGroupId = event.subject().id();
120 assertEquals(Group.GroupState.ADDED,
121 event.subject().state());
122 } else if (expectedEvent == GroupEvent.Type.GROUP_UPDATE_REQUESTED) {
123 assertEquals(Group.GroupState.PENDING_UPDATE,
124 event.subject().state());
125 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVE_REQUESTED) {
126 assertEquals(Group.GroupState.PENDING_DELETE,
127 event.subject().state());
128 } else if (expectedEvent == GroupEvent.Type.GROUP_REMOVED) {
129 createdGroupId = event.subject().id();
130 assertEquals(Group.GroupState.PENDING_DELETE,
131 event.subject().state());
132 }
133 }
134
135 public void verifyGroupId(GroupId id) {
136 assertEquals(createdGroupId, id);
137 }
138 }
139
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800140 /**
141 * Tests group store operations. The following operations are tested:
142 * a)Tests device group audit completion status change
143 * b)Tests storeGroup operation
144 * c)Tests getGroupCount operation
145 * d)Tests getGroup operation
146 * e)Tests getGroups operation
147 * f)Tests addOrUpdateGroupEntry operation from southbound
148 * g)Tests updateGroupDescription for ADD operation from northbound
149 * h)Tests updateGroupDescription for REMOVE operation from northbound
150 * i)Tests deleteGroupDescription operation from northbound
151 * j)Tests removeGroupEntry operation from southbound
152 */
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800153 @Test
154 public void testGroupStoreOperations() {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800155 // Set the Device AUDIT completed in the store
156 simpleGroupStore.deviceInitialAuditCompleted(D1);
157
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800158 ApplicationId appId =
159 new DefaultApplicationId(2, "org.groupstore.test");
160 TestGroupKey key = new TestGroupKey("group1");
161 PortNumber[] ports = {PortNumber.portNumber(31),
162 PortNumber.portNumber(32)};
163 List<PortNumber> outPorts = new ArrayList<PortNumber>();
164 outPorts.add(ports[0]);
165 outPorts.add(ports[1]);
166
167 List<GroupBucket> buckets = new ArrayList<GroupBucket>();
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()
174 .setMpls(106);
175 buckets.add(DefaultGroupBucket.createSelectGroupBucket(
176 tBuilder.build()));
177 }
178 GroupBuckets groupBuckets = new GroupBuckets(buckets);
179 GroupDescription groupDesc = new DefaultGroupDescription(
180 D1,
181 Group.Type.SELECT,
182 groupBuckets,
183 key,
184 appId);
185 InternalGroupStoreDelegate checkStoreGroupDelegate =
186 new InternalGroupStoreDelegate(key,
187 groupBuckets,
188 GroupEvent.Type.GROUP_ADD_REQUESTED);
189 simpleGroupStore.setDelegate(checkStoreGroupDelegate);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800190 // Testing storeGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800191 simpleGroupStore.storeGroupDescription(groupDesc);
192
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800193 // Testing getGroupCount operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800194 assertEquals(1, simpleGroupStore.getGroupCount(D1));
195
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800196 // Testing getGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800197 Group createdGroup = simpleGroupStore.getGroup(D1, key);
198 checkStoreGroupDelegate.verifyGroupId(createdGroup.id());
199
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800200 // Testing getGroups operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800201 Iterable<Group> createdGroups = simpleGroupStore.getGroups(D1);
202 int groupCount = 0;
203 for (Group group:createdGroups) {
204 checkStoreGroupDelegate.verifyGroupId(group.id());
205 groupCount++;
206 }
207 assertEquals(1, groupCount);
208 simpleGroupStore.unsetDelegate(checkStoreGroupDelegate);
209
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800210 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800211 InternalGroupStoreDelegate addGroupEntryDelegate =
212 new InternalGroupStoreDelegate(key,
213 groupBuckets,
214 GroupEvent.Type.GROUP_ADDED);
215 simpleGroupStore.setDelegate(addGroupEntryDelegate);
216 simpleGroupStore.addOrUpdateGroupEntry(createdGroup);
217 simpleGroupStore.unsetDelegate(addGroupEntryDelegate);
218
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800219 // Testing updateGroupDescription for ADD operation from northbound
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800220 TestGroupKey addKey = new TestGroupKey("group1AddBuckets");
221 PortNumber[] newNeighborPorts = {PortNumber.portNumber(41),
222 PortNumber.portNumber(42)};
223 List<PortNumber> newOutPorts = new ArrayList<PortNumber>();
224 newOutPorts.add(newNeighborPorts[0]);
225 newOutPorts.add(newNeighborPorts[1]);
226
227 List<GroupBucket> toAddBuckets = new ArrayList<GroupBucket>();
228 for (PortNumber portNumber: newOutPorts) {
229 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
230 tBuilder.setOutput(portNumber)
231 .setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
232 .setEthSrc(MacAddress.valueOf("00:00:00:00:00:01"))
233 .pushMpls()
234 .setMpls(106);
235 toAddBuckets.add(DefaultGroupBucket.createSelectGroupBucket(
236 tBuilder.build()));
237 }
238 GroupBuckets toAddGroupBuckets = new GroupBuckets(toAddBuckets);
239 buckets.addAll(toAddBuckets);
240 GroupBuckets updatedGroupBuckets = new GroupBuckets(buckets);
241 InternalGroupStoreDelegate updateGroupDescDelegate =
242 new InternalGroupStoreDelegate(addKey,
243 updatedGroupBuckets,
244 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
245 simpleGroupStore.setDelegate(updateGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800246 simpleGroupStore.updateGroupDescription(D1,
247 key,
248 UpdateType.ADD,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800249 toAddGroupBuckets,
250 addKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800251 simpleGroupStore.unsetDelegate(updateGroupDescDelegate);
252
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800253 // Testing updateGroupDescription for REMOVE operation from northbound
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800254 TestGroupKey removeKey = new TestGroupKey("group1RemoveBuckets");
255 List<GroupBucket> toRemoveBuckets = new ArrayList<GroupBucket>();
256 toRemoveBuckets.add(updatedGroupBuckets.buckets().get(0));
257 toRemoveBuckets.add(updatedGroupBuckets.buckets().get(1));
258 GroupBuckets toRemoveGroupBuckets = new GroupBuckets(toRemoveBuckets);
259 List<GroupBucket> remainingBuckets = new ArrayList<GroupBucket>();
260 remainingBuckets.add(updatedGroupBuckets.buckets().get(2));
261 remainingBuckets.add(updatedGroupBuckets.buckets().get(3));
262 GroupBuckets remainingGroupBuckets = new GroupBuckets(remainingBuckets);
263 InternalGroupStoreDelegate removeGroupDescDelegate =
264 new InternalGroupStoreDelegate(removeKey,
265 remainingGroupBuckets,
266 GroupEvent.Type.GROUP_UPDATE_REQUESTED);
267 simpleGroupStore.setDelegate(removeGroupDescDelegate);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800268 simpleGroupStore.updateGroupDescription(D1,
269 addKey,
270 UpdateType.REMOVE,
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800271 toRemoveGroupBuckets,
272 removeKey);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800273 simpleGroupStore.unsetDelegate(removeGroupDescDelegate);
274
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800275 // Testing getGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800276 Group existingGroup = simpleGroupStore.getGroup(D1, removeKey);
277 checkStoreGroupDelegate.verifyGroupId(existingGroup.id());
278
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800279 // Testing addOrUpdateGroupEntry operation from southbound
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800280 InternalGroupStoreDelegate updateGroupEntryDelegate =
281 new InternalGroupStoreDelegate(removeKey,
282 remainingGroupBuckets,
283 GroupEvent.Type.GROUP_UPDATED);
284 simpleGroupStore.setDelegate(updateGroupEntryDelegate);
285 simpleGroupStore.addOrUpdateGroupEntry(existingGroup);
286 simpleGroupStore.unsetDelegate(updateGroupEntryDelegate);
287
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800288 // Testing deleteGroupDescription operation from northbound
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800289 InternalGroupStoreDelegate deleteGroupDescDelegate =
290 new InternalGroupStoreDelegate(removeKey,
291 remainingGroupBuckets,
292 GroupEvent.Type.GROUP_REMOVE_REQUESTED);
293 simpleGroupStore.setDelegate(deleteGroupDescDelegate);
294 simpleGroupStore.deleteGroupDescription(D1, removeKey);
295 simpleGroupStore.unsetDelegate(deleteGroupDescDelegate);
296
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800297 // Testing removeGroupEntry operation from southbound
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800298 InternalGroupStoreDelegate removeGroupEntryDelegate =
299 new InternalGroupStoreDelegate(removeKey,
300 remainingGroupBuckets,
301 GroupEvent.Type.GROUP_REMOVED);
302 simpleGroupStore.setDelegate(removeGroupEntryDelegate);
303 simpleGroupStore.removeGroupEntry(existingGroup);
304
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800305 // Testing getGroup operation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800306 existingGroup = simpleGroupStore.getGroup(D1, removeKey);
307 assertEquals(null, existingGroup);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800308 assertEquals(0, Iterables.size(simpleGroupStore.getGroups(D1)));
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800309 assertEquals(0, simpleGroupStore.getGroupCount(D1));
310
311 simpleGroupStore.unsetDelegate(removeGroupEntryDelegate);
312
313 }
314}
315