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