blob: ab4cf9db431346786695dddb79bbf8fdde2e8bfe [file] [log] [blame]
Jian Liecb3c0f2015-12-15 10:07:49 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Liecb3c0f2015-12-15 10:07:49 -08003 *
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
Jian Li8ae91202016-03-24 14:36:16 -070017package org.onosproject.rest.resources;
Jian Liecb3c0f2015-12-15 10:07:49 -080018
Jian Li80cfe452016-01-14 16:04:58 -080019import com.eclipsesource.json.Json;
Jian Liecb3c0f2015-12-15 10:07:49 -080020import com.eclipsesource.json.JsonArray;
21import com.eclipsesource.json.JsonObject;
22import com.google.common.collect.ImmutableSet;
Jian Liecb3c0f2015-12-15 10:07:49 -080023import org.hamcrest.Description;
Jian Li9d616492016-03-09 10:52:49 -080024import org.hamcrest.Matchers;
Jian Liecb3c0f2015-12-15 10:07:49 -080025import org.hamcrest.TypeSafeMatcher;
26import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
29import org.onlab.osgi.ServiceDirectory;
30import org.onlab.osgi.TestServiceDirectory;
Jian Liecb3c0f2015-12-15 10:07:49 -080031import org.onosproject.codec.CodecService;
32import org.onosproject.codec.impl.CodecManager;
33import org.onosproject.codec.impl.GroupCodec;
34import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
36import org.onosproject.core.DefaultApplicationId;
Jian Liecb3c0f2015-12-15 10:07:49 -080037import org.onosproject.core.GroupId;
38import org.onosproject.net.DefaultDevice;
39import org.onosproject.net.Device;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.NetTestTools;
42import org.onosproject.net.device.DeviceService;
43import org.onosproject.net.group.DefaultGroupKey;
44import org.onosproject.net.group.Group;
45import org.onosproject.net.group.GroupBucket;
46import org.onosproject.net.group.GroupBuckets;
47import org.onosproject.net.group.GroupDescription;
48import org.onosproject.net.group.GroupKey;
49import org.onosproject.net.group.GroupService;
50
Jian Li9d616492016-03-09 10:52:49 -080051import javax.ws.rs.client.Entity;
52import javax.ws.rs.client.WebTarget;
Jian Liecb3c0f2015-12-15 10:07:49 -080053import javax.ws.rs.core.MediaType;
Jian Li9d616492016-03-09 10:52:49 -080054import javax.ws.rs.core.Response;
Jian Liecb3c0f2015-12-15 10:07:49 -080055import java.io.InputStream;
56import java.net.HttpURLConnection;
57import java.util.ArrayList;
58import java.util.HashMap;
59import java.util.HashSet;
60import java.util.List;
61import java.util.Set;
62
63import static org.easymock.EasyMock.anyObject;
64import static org.easymock.EasyMock.anyShort;
65import static org.easymock.EasyMock.createMock;
66import static org.easymock.EasyMock.expect;
67import static org.easymock.EasyMock.expectLastCall;
68import static org.easymock.EasyMock.replay;
69import static org.easymock.EasyMock.verify;
70import static org.hamcrest.Matchers.hasSize;
71import static org.hamcrest.Matchers.is;
72import static org.hamcrest.Matchers.notNullValue;
Jian Lia7f86ce2015-12-20 13:42:10 -080073import static org.junit.Assert.assertEquals;
Jian Li9d616492016-03-09 10:52:49 -080074import static org.junit.Assert.assertThat;
Jian Liecb3c0f2015-12-15 10:07:49 -080075import static org.onosproject.net.NetTestTools.APP_ID;
76
77/**
78 * Unit tests for Groups REST APIs.
79 */
80public class GroupsResourceTest extends ResourceTest {
81 final GroupService mockGroupService = createMock(GroupService.class);
82 CoreService mockCoreService = createMock(CoreService.class);
83 final DeviceService mockDeviceService = createMock(DeviceService.class);
84
85 final HashMap<DeviceId, Set<Group>> groups = new HashMap<>();
86
87
88 final DeviceId deviceId1 = DeviceId.deviceId("1");
89 final DeviceId deviceId2 = DeviceId.deviceId("2");
90 final DeviceId deviceId3 = DeviceId.deviceId("3");
91 final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER,
92 "", "", "", "", null);
93 final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER,
94 "", "", "", "", null);
95
Varun Sharma1853b3f2016-07-18 14:37:13 +053096 final MockGroup group1 = new MockGroup(deviceId1, 1, "0x111", 1);
97 final MockGroup group2 = new MockGroup(deviceId1, 2, "0x222", 2);
Jian Liecb3c0f2015-12-15 10:07:49 -080098
Varun Sharma1853b3f2016-07-18 14:37:13 +053099 final MockGroup group3 = new MockGroup(deviceId2, 3, "0x333", 3);
100 final MockGroup group4 = new MockGroup(deviceId2, 4, "0x444", 4);
Jian Liecb3c0f2015-12-15 10:07:49 -0800101
Varun Sharma1853b3f2016-07-18 14:37:13 +0530102 final MockGroup group5 = new MockGroup(deviceId3, 5, "0x555", 5);
103 final MockGroup group6 = new MockGroup(deviceId3, 6, "0x666", 6);
Jian Liecb3c0f2015-12-15 10:07:49 -0800104
105 /**
106 * Mock class for a group.
107 */
108 private static class MockGroup implements Group {
109
110 final DeviceId deviceId;
111 final ApplicationId appId;
112 final GroupKey appCookie;
113 final long baseValue;
114 final List<GroupBucket> bucketList;
115 GroupBuckets buckets;
116
117 public MockGroup(DeviceId deviceId, int appId, String appCookie, int id) {
118 this.deviceId = deviceId;
119 this.appId = new DefaultApplicationId(appId, String.valueOf(appId));
120 this.appCookie = new DefaultGroupKey(appCookie.getBytes());
rohitc2e77362017-03-27 20:07:25 +0530121 this.baseValue = id * 100L;
Jian Liecb3c0f2015-12-15 10:07:49 -0800122 this.bucketList = new ArrayList<>();
123 this.buckets = new GroupBuckets(bucketList);
124 }
125
126 @Override
127 public GroupId id() {
Yi Tsengfa394de2017-02-01 11:26:40 -0800128 return new GroupId((int) baseValue + 55);
Jian Liecb3c0f2015-12-15 10:07:49 -0800129 }
130
131 @Override
132 public GroupState state() {
133 return GroupState.ADDED;
134 }
135
136 @Override
137 public long life() {
138 return baseValue + 11;
139 }
140
141 @Override
142 public long packets() {
143 return baseValue + 22;
144 }
145
146 @Override
147 public long bytes() {
148 return baseValue + 33;
149 }
150
151 @Override
152 public long referenceCount() {
153 return baseValue + 44;
154 }
155
156 @Override
alshabibb0285992016-03-28 23:30:37 -0700157 public int age() {
158 return 0;
159 }
160
161 @Override
Jian Liecb3c0f2015-12-15 10:07:49 -0800162 public Type type() {
163 return GroupDescription.Type.ALL;
164 }
165
166 @Override
167 public DeviceId deviceId() {
168 return this.deviceId;
169 }
170
171 @Override
172 public ApplicationId appId() {
173 return this.appId;
174 }
175
176 @Override
177 public GroupKey appCookie() {
178 return this.appCookie;
179 }
180
181 @Override
182 public Integer givenGroupId() {
183 return (int) baseValue + 55;
184 }
185
186 @Override
187 public GroupBuckets buckets() {
188 return this.buckets;
189 }
190 }
191
192 /**
193 * Populates some groups used as testing data.
194 */
195 private void setupMockGroups() {
196 final Set<Group> groups1 = new HashSet<>();
197 groups1.add(group1);
198 groups1.add(group2);
199
200 final Set<Group> groups2 = new HashSet<>();
201 groups2.add(group3);
202 groups2.add(group4);
203
204 groups.put(deviceId1, groups1);
205 groups.put(deviceId2, groups2);
206
207 expect(mockGroupService.getGroups(deviceId1))
208 .andReturn(groups.get(deviceId1)).anyTimes();
209 expect(mockGroupService.getGroups(deviceId2))
210 .andReturn(groups.get(deviceId2)).anyTimes();
211 }
212
213 /**
214 * Sets up the global values for all the tests.
215 */
216 @Before
217 public void setUpTest() {
218 // Mock device service
219 expect(mockDeviceService.getDevice(deviceId1))
220 .andReturn(device1);
221 expect(mockDeviceService.getDevice(deviceId2))
222 .andReturn(device2);
223 expect(mockDeviceService.getDevices())
224 .andReturn(ImmutableSet.of(device1, device2));
225
226 // Mock Core Service
227 expect(mockCoreService.getAppId(anyShort()))
228 .andReturn(NetTestTools.APP_ID).anyTimes();
229 expect(mockCoreService.registerApplication(GroupCodec.REST_APP_ID))
230 .andReturn(APP_ID).anyTimes();
231 replay(mockCoreService);
232
233 // Register the services needed for the test
234 final CodecManager codecService = new CodecManager();
235 codecService.activate();
236 ServiceDirectory testDirectory =
237 new TestServiceDirectory()
238 .add(GroupService.class, mockGroupService)
239 .add(DeviceService.class, mockDeviceService)
240 .add(CodecService.class, codecService)
241 .add(CoreService.class, mockCoreService);
242
Ray Milkey094a1352018-01-22 14:03:54 -0800243 setServiceDirectory(testDirectory);
Jian Liecb3c0f2015-12-15 10:07:49 -0800244 }
245
246 /**
247 * Cleans up and verifies the mocks.
248 */
249 @After
250 public void tearDownTest() {
251 verify(mockGroupService);
252 verify(mockCoreService);
253 }
254
255 /**
256 * Hamcrest matcher to check that a group representation in JSON matches
257 * the actual group.
258 */
259 public static class GroupJsonMatcher extends TypeSafeMatcher<JsonObject> {
260 private final Group group;
261 private final String expectedAppId;
262 private String reason = "";
263
264 public GroupJsonMatcher(Group groupValue, String expectedAppIdValue) {
265 group = groupValue;
266 expectedAppId = expectedAppIdValue;
267 }
268
269 @Override
270 public boolean matchesSafely(JsonObject jsonGroup) {
271 // check id
272 final String jsonId = jsonGroup.get("id").asString();
Prince Pereira3ff504c2016-08-30 14:23:43 +0530273 final String groupId = group.id().id().toString();
Jian Liecb3c0f2015-12-15 10:07:49 -0800274 if (!jsonId.equals(groupId)) {
Prince Pereira3ff504c2016-08-30 14:23:43 +0530275 reason = "id " + group.id().id().toString();
Jian Liecb3c0f2015-12-15 10:07:49 -0800276 return false;
277 }
278
279 // check application id
280 final String jsonAppId = jsonGroup.get("appId").asString();
varunsha34b30602016-08-18 10:31:18 -0700281 final String appId = group.appId().name();
Jian Liecb3c0f2015-12-15 10:07:49 -0800282 if (!jsonAppId.equals(appId)) {
varunsha34b30602016-08-18 10:31:18 -0700283 reason = "appId " + group.appId().name();
Jian Liecb3c0f2015-12-15 10:07:49 -0800284 return false;
285 }
286
287 // check device id
288 final String jsonDeviceId = jsonGroup.get("deviceId").asString();
289 if (!jsonDeviceId.equals(group.deviceId().toString())) {
290 reason = "deviceId " + group.deviceId();
291 return false;
292 }
293
294 // check bucket array
295 if (group.buckets().buckets() != null) {
296 final JsonArray jsonBuckets = jsonGroup.get("buckets").asArray();
297 if (group.buckets().buckets().size() != jsonBuckets.size()) {
298 reason = "buckets array size of " +
299 Integer.toString(group.buckets().buckets().size());
300 return false;
301 }
302 for (final GroupBucket groupBucket : group.buckets().buckets()) {
303 boolean groupBucketFound = false;
304 for (int groupBucketIndex = 0; groupBucketIndex < jsonBuckets.size(); groupBucketIndex++) {
305 final String jsonType = jsonBuckets.get(groupBucketIndex).asObject().get("type").asString();
306 final String bucketType = groupBucket.type().name();
307 if (jsonType.equals(bucketType)) {
308 groupBucketFound = true;
309 }
310 }
311 if (!groupBucketFound) {
312 reason = "group bucket " + groupBucket.toString();
313 return false;
314 }
315 }
316 }
317
318 return true;
319 }
320
321 @Override
322 public void describeTo(Description description) {
323 description.appendText(reason);
324 }
325 }
326
327 /**
328 * Factory to allocate a group matcher.
329 *
330 * @param group group object we are looking for
331 * @return matcher
332 */
333 private static GroupJsonMatcher matchesGroup(Group group, String expectedAppName) {
334 return new GroupJsonMatcher(group, expectedAppName);
335 }
336
337 /**
338 * Hamcrest matcher to check that a group is represented properly in a JSON
339 * array of flows.
340 */
341 public static class GroupJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
342 private final Group group;
343 private String reason = "";
344
345 public GroupJsonArrayMatcher(Group groupValue) {
346 group = groupValue;
347 }
348
349 @Override
350 public boolean matchesSafely(JsonArray json) {
351 boolean groupFound = false;
352 for (int jsonGroupIndex = 0; jsonGroupIndex < json.size();
353 jsonGroupIndex++) {
354
355 final JsonObject jsonGroup = json.get(jsonGroupIndex).asObject();
356
Prince Pereira3ff504c2016-08-30 14:23:43 +0530357 final String groupId = group.id().id().toString();
Jian Liecb3c0f2015-12-15 10:07:49 -0800358 final String jsonGroupId = jsonGroup.get("id").asString();
359 if (jsonGroupId.equals(groupId)) {
360 groupFound = true;
361
362 // We found the correct group, check attribute values
363 assertThat(jsonGroup, matchesGroup(group, APP_ID.name()));
364 }
365 }
366 if (!groupFound) {
Prince Pereira3ff504c2016-08-30 14:23:43 +0530367 reason = "Group with id " + group.id().id().toString() + " not found";
Jian Liecb3c0f2015-12-15 10:07:49 -0800368 return false;
369 } else {
370 return true;
371 }
372 }
373
374 @Override
375 public void describeTo(Description description) {
376 description.appendText(reason);
377 }
378 }
379
380 /**
381 * Factory to allocate a group array matcher.
382 *
383 * @param group group object we are looking for
384 * @return matcher
385 */
386 private static GroupJsonArrayMatcher hasGroup(Group group) {
387 return new GroupJsonArrayMatcher(group);
388 }
389
390 /**
391 * Tests the result of the rest api GET when there are no groups.
392 */
393 @Test
394 public void testGroupsEmptyArray() {
395 expect(mockGroupService.getGroups(deviceId1)).andReturn(null).anyTimes();
396 expect(mockGroupService.getGroups(deviceId2)).andReturn(null).anyTimes();
397 replay(mockGroupService);
398 replay(mockDeviceService);
Jian Li9d616492016-03-09 10:52:49 -0800399 final WebTarget wt = target();
400 final String response = wt.path("groups").request().get(String.class);
Jian Liecb3c0f2015-12-15 10:07:49 -0800401 assertThat(response, is("{\"groups\":[]}"));
402 }
403
404 /**
405 * Tests the result of the rest api GET when there are active groups.
406 */
407 @Test
408 public void testGroupsPopulatedArray() {
409 setupMockGroups();
410 replay(mockGroupService);
411 replay(mockDeviceService);
Jian Li9d616492016-03-09 10:52:49 -0800412 final WebTarget wt = target();
413 final String response = wt.path("groups").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800414 final JsonObject result = Json.parse(response).asObject();
Jian Liecb3c0f2015-12-15 10:07:49 -0800415 assertThat(result, notNullValue());
416
417 assertThat(result.names(), hasSize(1));
418 assertThat(result.names().get(0), is("groups"));
419 final JsonArray jsonGroups = result.get("groups").asArray();
420 assertThat(jsonGroups, notNullValue());
421 assertThat(jsonGroups, hasGroup(group1));
422 assertThat(jsonGroups, hasGroup(group2));
423 assertThat(jsonGroups, hasGroup(group3));
424 assertThat(jsonGroups, hasGroup(group4));
425 }
426
427 /**
428 * Tests the result of a rest api GET for a device.
429 */
430 @Test
431 public void testGroupsSingleDevice() {
432 setupMockGroups();
433 final Set<Group> groups = new HashSet<>();
434 groups.add(group5);
435 groups.add(group6);
436 expect(mockGroupService.getGroups(anyObject()))
437 .andReturn(groups).anyTimes();
438 replay(mockGroupService);
439 replay(mockDeviceService);
Jian Li9d616492016-03-09 10:52:49 -0800440 final WebTarget wt = target();
441 final String response = wt.path("groups/" + deviceId3).request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800442 final JsonObject result = Json.parse(response).asObject();
Jian Liecb3c0f2015-12-15 10:07:49 -0800443 assertThat(result, notNullValue());
444
445 assertThat(result.names(), hasSize(1));
446 assertThat(result.names().get(0), is("groups"));
Jian Li2e02fab2016-02-25 15:45:59 +0900447 final JsonArray jsonGroups = result.get("groups").asArray();
448 assertThat(jsonGroups, notNullValue());
449 assertThat(jsonGroups, hasGroup(group5));
450 assertThat(jsonGroups, hasGroup(group6));
Jian Liecb3c0f2015-12-15 10:07:49 -0800451 }
452
453 /**
Jian Lia7f86ce2015-12-20 13:42:10 -0800454 * Test the result of a rest api GET with specifying device id and appcookie.
455 */
456 @Test
457 public void testGroupByDeviceIdAndAppCookie() {
458 setupMockGroups();
459 expect(mockGroupService.getGroup(anyObject(), anyObject()))
460 .andReturn(group5).anyTimes();
461 replay(mockGroupService);
Jian Li9d616492016-03-09 10:52:49 -0800462 final WebTarget wt = target();
Varun Sharma1853b3f2016-07-18 14:37:13 +0530463 final String response = wt.path("groups/" + deviceId3 + "/" + "0x111")
Jian Li9d616492016-03-09 10:52:49 -0800464 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800465 final JsonObject result = Json.parse(response).asObject();
Jian Lia7f86ce2015-12-20 13:42:10 -0800466 assertThat(result, notNullValue());
467
468 assertThat(result.names(), hasSize(1));
469 assertThat(result.names().get(0), is("groups"));
470 final JsonArray jsonFlows = result.get("groups").asArray();
471 assertThat(jsonFlows, notNullValue());
472 assertThat(jsonFlows, hasGroup(group5));
473 }
474
475 /**
476 * Test whether the REST API returns 404 if no entry has been found.
477 */
478 @Test
479 public void testGroupByDeviceIdAndAppCookieNull() {
480 setupMockGroups();
481 expect(mockGroupService.getGroup(anyObject(), anyObject()))
482 .andReturn(null).anyTimes();
483 replay(mockGroupService);
Jian Li9d616492016-03-09 10:52:49 -0800484 final WebTarget wt = target();
Varun Sharma1853b3f2016-07-18 14:37:13 +0530485 final Response response = wt.path("groups/" + deviceId3 + "/" + "0x222").request().get();
Jian Lia7f86ce2015-12-20 13:42:10 -0800486
487 assertEquals(404, response.getStatus());
488 }
489
490 /**
Jian Liecb3c0f2015-12-15 10:07:49 -0800491 * Tests creating a group with POST.
492 */
493 @Test
494 public void testPost() {
495 mockGroupService.addGroup(anyObject());
496 expectLastCall();
497 replay(mockGroupService);
498
Jian Li9d616492016-03-09 10:52:49 -0800499 WebTarget wt = target();
Jian Liecb3c0f2015-12-15 10:07:49 -0800500 InputStream jsonStream = GroupsResourceTest.class
501 .getResourceAsStream("post-group.json");
502
Jian Li9d616492016-03-09 10:52:49 -0800503 Response response = wt.path("groups/of:0000000000000001")
504 .request(MediaType.APPLICATION_JSON_TYPE)
505 .post(Entity.json(jsonStream));
Jian Liecb3c0f2015-12-15 10:07:49 -0800506 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
Jian Li9d616492016-03-09 10:52:49 -0800507 String location = response.getLocation().getPath();
508 assertThat(location, Matchers.startsWith("/groups/of:0000000000000001/"));
Jian Liecb3c0f2015-12-15 10:07:49 -0800509 }
510
511 /**
512 * Tests deleting a group.
513 */
514 @Test
515 public void testDelete() {
516 setupMockGroups();
517 mockGroupService.removeGroup(anyObject(), anyObject(), anyObject());
518 expectLastCall();
519 replay(mockGroupService);
520
Jian Li9d616492016-03-09 10:52:49 -0800521 WebTarget wt = target();
Jian Liecb3c0f2015-12-15 10:07:49 -0800522
Varun Sharma1853b3f2016-07-18 14:37:13 +0530523 String location = "/groups/1/0x111";
Jian Liecb3c0f2015-12-15 10:07:49 -0800524
Jian Li9d616492016-03-09 10:52:49 -0800525 Response deleteResponse = wt.path(location)
526 .request(MediaType.APPLICATION_JSON_TYPE)
527 .delete();
Jian Liecb3c0f2015-12-15 10:07:49 -0800528 assertThat(deleteResponse.getStatus(),
529 is(HttpURLConnection.HTTP_NO_CONTENT));
530 }
531}