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