blob: a460385053fb974e0088a13ae94303f44f90b2ad [file] [log] [blame]
Ray Milkey4f5de002014-12-17 19:26:11 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Ray Milkey4f5de002014-12-17 19:26:11 -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 */
16package org.onosproject.rest;
17
18import java.util.HashMap;
19import java.util.HashSet;
20import java.util.Set;
21
22import org.hamcrest.Description;
23import org.hamcrest.TypeSafeMatcher;
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.osgi.ServiceDirectory;
28import org.onlab.osgi.TestServiceDirectory;
29import org.onlab.packet.MacAddress;
30import org.onlab.rest.BaseResource;
31import org.onosproject.codec.CodecService;
32import org.onosproject.codec.impl.CodecManager;
33import org.onosproject.core.DefaultGroupId;
34import org.onosproject.core.GroupId;
35import org.onosproject.net.DefaultDevice;
36import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.FlowEntry;
42import org.onosproject.net.flow.FlowId;
43import org.onosproject.net.flow.FlowRuleService;
44import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
46import org.onosproject.net.flow.criteria.Criterion;
47import org.onosproject.net.flow.instructions.Instruction;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -080048import org.onosproject.net.flow.instructions.Instructions;
Ray Milkey4f5de002014-12-17 19:26:11 -080049import com.eclipsesource.json.JsonArray;
50import com.eclipsesource.json.JsonObject;
Ray Milkey4f5de002014-12-17 19:26:11 -080051import com.google.common.collect.ImmutableSet;
52import com.sun.jersey.api.client.UniformInterfaceException;
53import com.sun.jersey.api.client.WebResource;
Ray Milkey4f5de002014-12-17 19:26:11 -080054
55import static org.easymock.EasyMock.anyObject;
56import static org.easymock.EasyMock.createMock;
57import static org.easymock.EasyMock.expect;
58import static org.easymock.EasyMock.replay;
59import static org.easymock.EasyMock.verify;
60import static org.hamcrest.Matchers.containsString;
61import static org.hamcrest.Matchers.hasSize;
62import static org.hamcrest.Matchers.is;
63import static org.hamcrest.Matchers.not;
64import static org.hamcrest.Matchers.notNullValue;
65import static org.junit.Assert.assertThat;
66import static org.junit.Assert.fail;
67
68/**
69 * Unit tests for Flows REST APIs.
70 */
Ray Milkey9c3d3362015-01-28 10:39:56 -080071public class FlowsResourceTest extends ResourceTest {
Ray Milkey4f5de002014-12-17 19:26:11 -080072 final FlowRuleService mockFlowService = createMock(FlowRuleService.class);
73 final HashMap<DeviceId, Set<FlowEntry>> rules = new HashMap<>();
74
75 final DeviceService mockDeviceService = createMock(DeviceService.class);
76
77 final DeviceId deviceId1 = DeviceId.deviceId("1");
78 final DeviceId deviceId2 = DeviceId.deviceId("2");
79 final DeviceId deviceId3 = DeviceId.deviceId("3");
80 final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER,
81 "", "", "", "", null);
82 final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER,
83 "", "", "", "", null);
84
85 final MockFlowEntry flow1 = new MockFlowEntry(deviceId1, 1);
86 final MockFlowEntry flow2 = new MockFlowEntry(deviceId1, 2);
87
88 final MockFlowEntry flow3 = new MockFlowEntry(deviceId2, 3);
89 final MockFlowEntry flow4 = new MockFlowEntry(deviceId2, 4);
90
91 final MockFlowEntry flow5 = new MockFlowEntry(deviceId2, 5);
92 final MockFlowEntry flow6 = new MockFlowEntry(deviceId2, 6);
93
94 /**
95 * Mock class for a flow entry.
96 */
97 private static class MockFlowEntry implements FlowEntry {
98 final DeviceId deviceId;
99 final long baseValue;
100 TrafficTreatment treatment;
101 TrafficSelector selector;
102
103 public MockFlowEntry(DeviceId deviceId, long id) {
104 this.deviceId = deviceId;
105 this.baseValue = id * 100;
106 }
107
108 @Override
109 public FlowEntryState state() {
110 return FlowEntryState.ADDED;
111 }
112
113 @Override
114 public long life() {
115 return baseValue + 11;
116 }
117
118 @Override
119 public long packets() {
120 return baseValue + 22;
121 }
122
123 @Override
124 public long bytes() {
125 return baseValue + 33;
126 }
127
128 @Override
129 public long lastSeen() {
130 return baseValue + 44;
131 }
132
133 @Override
134 public int errType() {
135 return 0;
136 }
137
138 @Override
139 public int errCode() {
140 return 0;
141 }
142
143 @Override
144 public FlowId id() {
145 final long id = baseValue + 55;
146 return FlowId.valueOf(id);
147 }
148
149 @Override
alshabib08d98982015-04-21 16:25:50 -0700150 public GroupId groupId() {
151 return new DefaultGroupId(3);
Ray Milkey4f5de002014-12-17 19:26:11 -0800152 }
153
154 @Override
alshabib08d98982015-04-21 16:25:50 -0700155 public short appId() {
156 return 2;
Ray Milkey4f5de002014-12-17 19:26:11 -0800157 }
158
159 @Override
160 public int priority() {
161 return (int) (baseValue + 66);
162 }
163
164 @Override
165 public DeviceId deviceId() {
166 return deviceId;
167 }
168
169 @Override
170 public TrafficSelector selector() {
171 return selector;
172 }
173
174 @Override
175 public TrafficTreatment treatment() {
176 return treatment;
177 }
178
179 @Override
180 public int timeout() {
181 return (int) (baseValue + 77);
182 }
183
184 @Override
185 public boolean isPermanent() {
186 return false;
187 }
sangho11c30ac2015-01-22 14:30:55 -0800188
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800189 @Override
alshabibdb774072015-04-20 13:13:51 -0700190 public int tableId() {
191 return 0;
192 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800193 }
194
Ray Milkey4f5de002014-12-17 19:26:11 -0800195 /**
196 * Populates some flows used as testing data.
197 */
198 private void setupMockFlows() {
199 flow2.treatment = DefaultTrafficTreatment.builder()
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800200 .add(Instructions.modL0Lambda((short) 4))
201 .add(Instructions.modL0Lambda((short) 5))
Ray Milkey4f5de002014-12-17 19:26:11 -0800202 .setEthDst(MacAddress.BROADCAST)
203 .build();
204 flow2.selector = DefaultTrafficSelector.builder()
205 .matchEthType((short) 3)
206 .matchIPProtocol((byte) 9)
207 .build();
208 flow4.treatment = DefaultTrafficTreatment.builder()
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800209 .add(Instructions.modL0Lambda((short) 6))
Ray Milkey4f5de002014-12-17 19:26:11 -0800210 .build();
211 final Set<FlowEntry> flows1 = new HashSet<>();
212 flows1.add(flow1);
213 flows1.add(flow2);
214
215 final Set<FlowEntry> flows2 = new HashSet<>();
216 flows1.add(flow3);
217 flows1.add(flow4);
218
219 rules.put(deviceId1, flows1);
220 rules.put(deviceId2, flows2);
221
222 expect(mockFlowService.getFlowEntries(deviceId1))
223 .andReturn(rules.get(deviceId1)).anyTimes();
224 expect(mockFlowService.getFlowEntries(deviceId2))
225 .andReturn(rules.get(deviceId2)).anyTimes();
226 }
227
228 /**
229 * Sets up the global values for all the tests.
230 */
231 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800232 public void setUpTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800233 // Mock device service
234 expect(mockDeviceService.getDevice(deviceId1))
235 .andReturn(device1);
236 expect(mockDeviceService.getDevice(deviceId2))
237 .andReturn(device2);
238 expect(mockDeviceService.getDevices())
239 .andReturn(ImmutableSet.of(device1, device2));
240
241 // Register the services needed for the test
242 final CodecManager codecService = new CodecManager();
243 codecService.activate();
244 ServiceDirectory testDirectory =
245 new TestServiceDirectory()
246 .add(FlowRuleService.class, mockFlowService)
247 .add(DeviceService.class, mockDeviceService)
248 .add(CodecService.class, codecService);
249
250 BaseResource.setServiceDirectory(testDirectory);
251 }
252
253 /**
254 * Cleans up and verifies the mocks.
Ray Milkey4f5de002014-12-17 19:26:11 -0800255 */
256 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800257 public void tearDownTest() {
Ray Milkey4f5de002014-12-17 19:26:11 -0800258 verify(mockFlowService);
259 }
260
261 /**
262 * Hamcrest matcher to check that a flow representation in JSON matches
263 * the actual flow entry.
264 */
265 public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> {
266 private final FlowEntry flow;
267 private String reason = "";
268
269 public FlowJsonMatcher(FlowEntry flowValue) {
270 flow = flowValue;
271 }
272
273 @Override
274 public boolean matchesSafely(JsonObject jsonFlow) {
275 // check id
276 final String jsonId = jsonFlow.get("id").asString();
277 final String flowId = Long.toString(flow.id().value());
278 if (!jsonId.equals(flowId)) {
279 reason = "id " + flow.id().toString();
280 return false;
281 }
282
283 // check application id
284 final int jsonAppId = jsonFlow.get("appId").asInt();
285 if (jsonAppId != flow.appId()) {
286 reason = "appId " + Short.toString(flow.appId());
287 return false;
288 }
289
290 // check device id
291 final String jsonDeviceId = jsonFlow.get("deviceId").asString();
292 if (!jsonDeviceId.equals(flow.deviceId().toString())) {
293 reason = "deviceId " + flow.deviceId();
294 return false;
295 }
296
297 // check treatment and instructions array
298 if (flow.treatment() != null) {
299 final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject();
300 final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray();
Ray Milkey42507352015-03-20 15:16:10 -0700301 if (flow.treatment().immediate().size() != jsonInstructions.size()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800302 reason = "instructions array size of " +
Ray Milkey42507352015-03-20 15:16:10 -0700303 Integer.toString(flow.treatment().immediate().size());
Ray Milkey4f5de002014-12-17 19:26:11 -0800304 return false;
305 }
Ray Milkey42507352015-03-20 15:16:10 -0700306 for (final Instruction instruction : flow.treatment().immediate()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800307 boolean instructionFound = false;
Ray Milkey4f5de002014-12-17 19:26:11 -0800308 for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800309 final String jsonType =
310 jsonInstructions.get(instructionIndex)
311 .asObject().get("type").asString();
312 final String instructionType = instruction.type().name();
313 if (jsonType.equals(instructionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800314 instructionFound = true;
315 }
316 }
317 if (!instructionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800318 reason = "instruction " + instruction.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800319 return false;
320 }
321 }
322 }
323
324 // check selector and criteria array
325 if (flow.selector() != null) {
326 final JsonObject jsonTreatment = jsonFlow.get("selector").asObject();
327 final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray();
328 if (flow.selector().criteria().size() != jsonCriteria.size()) {
329 reason = "criteria array size of " +
330 Integer.toString(flow.selector().criteria().size());
331 return false;
332 }
333 for (final Criterion criterion : flow.selector().criteria()) {
334 boolean criterionFound = false;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800335
Ray Milkey4f5de002014-12-17 19:26:11 -0800336 for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800337 final String jsonType =
338 jsonCriteria.get(criterionIndex)
339 .asObject().get("type").asString();
340 final String criterionType = criterion.type().name();
341 if (jsonType.equals(criterionType)) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800342 criterionFound = true;
343 }
344 }
345 if (!criterionFound) {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800346 reason = "criterion " + criterion.toString();
Ray Milkey4f5de002014-12-17 19:26:11 -0800347 return false;
348 }
349 }
350 }
351
352 return true;
353 }
354
355 @Override
356 public void describeTo(Description description) {
357 description.appendText(reason);
358 }
359 }
360
361 /**
362 * Factory to allocate a flow matcher.
363 *
364 * @param flow flow object we are looking for
365 * @return matcher
366 */
367 private static FlowJsonMatcher matchesFlow(FlowEntry flow) {
368 return new FlowJsonMatcher(flow);
369 }
370
371 /**
372 * Hamcrest matcher to check that a flow is represented properly in a JSON
373 * array of flows.
374 */
375 public static class FlowJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
376 private final FlowEntry flow;
377 private String reason = "";
378
379 public FlowJsonArrayMatcher(FlowEntry flowValue) {
380 flow = flowValue;
381 }
382
383 @Override
384 public boolean matchesSafely(JsonArray json) {
385 boolean flowFound = false;
386
387 for (int jsonFlowIndex = 0; jsonFlowIndex < json.size();
388 jsonFlowIndex++) {
389
390 final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject();
391
392 final String flowId = Long.toString(flow.id().value());
393 final String jsonFlowId = jsonFlow.get("id").asString();
394 if (jsonFlowId.equals(flowId)) {
395 flowFound = true;
396
397 // We found the correct flow, check attribute values
398 assertThat(jsonFlow, matchesFlow(flow));
399 }
400 }
401 if (!flowFound) {
402 reason = "Flow with id " + flow.id().toString() + " not found";
403 return false;
404 } else {
405 return true;
406 }
407 }
408
409 @Override
410 public void describeTo(Description description) {
411 description.appendText(reason);
412 }
413 }
414
415 /**
416 * Factory to allocate a flow array matcher.
417 *
418 * @param flow flow object we are looking for
419 * @return matcher
420 */
421 private static FlowJsonArrayMatcher hasFlow(FlowEntry flow) {
422 return new FlowJsonArrayMatcher(flow);
423 }
424
425 /**
426 * Tests the result of the rest api GET when there are no flows.
427 */
428 @Test
429 public void testFlowsEmptyArray() {
430 expect(mockFlowService.getFlowEntries(deviceId1))
431 .andReturn(null).anyTimes();
432 expect(mockFlowService.getFlowEntries(deviceId2))
433 .andReturn(null).anyTimes();
434 replay(mockFlowService);
435 replay(mockDeviceService);
436 final WebResource rs = resource();
437 final String response = rs.path("flows").get(String.class);
438 assertThat(response, is("{\"flows\":[]}"));
439 }
440
441 /**
442 * Tests the result of the rest api GET when there are active flows.
443 */
444 @Test
445 public void testFlowsPopulatedArray() {
446 setupMockFlows();
447 replay(mockFlowService);
448 replay(mockDeviceService);
449 final WebResource rs = resource();
450 final String response = rs.path("flows").get(String.class);
451 final JsonObject result = JsonObject.readFrom(response);
452 assertThat(result, notNullValue());
453
454 assertThat(result.names(), hasSize(1));
455 assertThat(result.names().get(0), is("flows"));
456 final JsonArray jsonFlows = result.get("flows").asArray();
457 assertThat(jsonFlows, notNullValue());
458 assertThat(jsonFlows, hasFlow(flow1));
459 assertThat(jsonFlows, hasFlow(flow2));
460 assertThat(jsonFlows, hasFlow(flow3));
461 assertThat(jsonFlows, hasFlow(flow4));
462 }
463
464 /**
465 * Tests the result of a rest api GET for a device.
466 */
467 @Test
468 public void testFlowsSingleDevice() {
469 setupMockFlows();
470 final Set<FlowEntry> flows = new HashSet<>();
471 flows.add(flow5);
472 flows.add(flow6);
473 expect(mockFlowService.getFlowEntries(anyObject()))
474 .andReturn(flows).anyTimes();
475 replay(mockFlowService);
476 replay(mockDeviceService);
477 final WebResource rs = resource();
478 final String response = rs.path("flows/" + deviceId3).get(String.class);
479 final JsonObject result = JsonObject.readFrom(response);
480 assertThat(result, notNullValue());
481
482 assertThat(result.names(), hasSize(1));
483 assertThat(result.names().get(0), is("flows"));
484 final JsonArray jsonFlows = result.get("flows").asArray();
485 assertThat(jsonFlows, notNullValue());
486 assertThat(jsonFlows, hasFlow(flow5));
487 assertThat(jsonFlows, hasFlow(flow6));
488 }
489
490 /**
491 * Tests the result of a rest api GET for a device.
492 */
493 @Test
494 public void testFlowsSingleDeviceWithFlowId() {
495 setupMockFlows();
496 final Set<FlowEntry> flows = new HashSet<>();
497 flows.add(flow5);
498 flows.add(flow6);
499 expect(mockFlowService.getFlowEntries(anyObject()))
500 .andReturn(flows).anyTimes();
501 replay(mockFlowService);
502 replay(mockDeviceService);
503 final WebResource rs = resource();
504 final String response = rs.path("flows/" + deviceId3 + "/"
505 + Long.toString(flow5.id().value())).get(String.class);
506 final JsonObject result = JsonObject.readFrom(response);
507 assertThat(result, notNullValue());
508
509 assertThat(result.names(), hasSize(1));
510 assertThat(result.names().get(0), is("flows"));
511 final JsonArray jsonFlows = result.get("flows").asArray();
512 assertThat(jsonFlows, notNullValue());
513 assertThat(jsonFlows, hasFlow(flow5));
514 assertThat(jsonFlows, not(hasFlow(flow6)));
515 }
516
517 /**
518 * Tests that a fetch of a non-existent device object throws an exception.
519 */
520 @Test
521 public void testBadGet() {
522 expect(mockFlowService.getFlowEntries(deviceId1))
523 .andReturn(null).anyTimes();
524 expect(mockFlowService.getFlowEntries(deviceId2))
525 .andReturn(null).anyTimes();
526 replay(mockFlowService);
527 replay(mockDeviceService);
528
529 WebResource rs = resource();
530 try {
531 rs.path("flows/0").get(String.class);
532 fail("Fetch of non-existent device did not throw an exception");
533 } catch (UniformInterfaceException ex) {
534 assertThat(ex.getMessage(),
535 containsString("returned a response status of"));
536 }
537 }
538}