Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.rest; |
| 17 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 19 | import com.eclipsesource.json.JsonArray; |
| 20 | import com.eclipsesource.json.JsonObject; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.sun.jersey.api.client.ClientResponse; |
| 23 | import com.sun.jersey.api.client.UniformInterfaceException; |
| 24 | import com.sun.jersey.api.client.WebResource; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 25 | import org.hamcrest.Description; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 26 | import org.hamcrest.Matchers; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 27 | import org.hamcrest.TypeSafeMatcher; |
| 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.onlab.osgi.ServiceDirectory; |
| 32 | import org.onlab.osgi.TestServiceDirectory; |
| 33 | import org.onlab.packet.MacAddress; |
| 34 | import org.onlab.rest.BaseResource; |
| 35 | import org.onosproject.codec.CodecService; |
| 36 | import org.onosproject.codec.impl.CodecManager; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 37 | import org.onosproject.codec.impl.FlowRuleCodec; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 38 | import org.onosproject.core.CoreService; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 39 | import org.onosproject.core.DefaultGroupId; |
| 40 | import org.onosproject.core.GroupId; |
| 41 | import org.onosproject.net.DefaultDevice; |
| 42 | import org.onosproject.net.Device; |
| 43 | import org.onosproject.net.DeviceId; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 44 | import org.onosproject.net.IndexedLambda; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 45 | import org.onosproject.net.NetTestTools; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 46 | import org.onosproject.net.device.DeviceService; |
| 47 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 48 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 49 | import org.onosproject.net.flow.FlowEntry; |
| 50 | import org.onosproject.net.flow.FlowId; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flow.FlowRule; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 52 | import org.onosproject.net.flow.FlowRuleExtPayLoad; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 53 | import org.onosproject.net.flow.FlowRuleService; |
| 54 | import org.onosproject.net.flow.TrafficSelector; |
| 55 | import org.onosproject.net.flow.TrafficTreatment; |
| 56 | import org.onosproject.net.flow.criteria.Criterion; |
| 57 | import org.onosproject.net.flow.instructions.Instruction; |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 58 | import org.onosproject.net.flow.instructions.Instructions; |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 59 | import org.onosproject.rest.resources.CoreWebApplication; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 60 | |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 61 | import javax.ws.rs.core.MediaType; |
| 62 | import java.io.InputStream; |
| 63 | import java.net.HttpURLConnection; |
| 64 | import java.util.HashMap; |
| 65 | import java.util.HashSet; |
| 66 | import java.util.Set; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 67 | |
| 68 | import static org.easymock.EasyMock.anyObject; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 69 | import static org.easymock.EasyMock.anyShort; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 70 | import static org.easymock.EasyMock.createMock; |
| 71 | import static org.easymock.EasyMock.expect; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 72 | import static org.easymock.EasyMock.expectLastCall; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 73 | import static org.easymock.EasyMock.replay; |
| 74 | import static org.easymock.EasyMock.verify; |
| 75 | import static org.hamcrest.Matchers.containsString; |
| 76 | import static org.hamcrest.Matchers.hasSize; |
| 77 | import static org.hamcrest.Matchers.is; |
| 78 | import static org.hamcrest.Matchers.not; |
| 79 | import static org.hamcrest.Matchers.notNullValue; |
| 80 | import static org.junit.Assert.assertThat; |
| 81 | import static org.junit.Assert.fail; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 82 | import static org.onosproject.net.NetTestTools.APP_ID; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Unit tests for Flows REST APIs. |
| 86 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 87 | public class FlowsResourceTest extends ResourceTest { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 88 | final FlowRuleService mockFlowService = createMock(FlowRuleService.class); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 89 | CoreService mockCoreService = createMock(CoreService.class); |
| 90 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 91 | final HashMap<DeviceId, Set<FlowEntry>> rules = new HashMap<>(); |
| 92 | |
| 93 | final DeviceService mockDeviceService = createMock(DeviceService.class); |
| 94 | |
| 95 | final DeviceId deviceId1 = DeviceId.deviceId("1"); |
| 96 | final DeviceId deviceId2 = DeviceId.deviceId("2"); |
| 97 | final DeviceId deviceId3 = DeviceId.deviceId("3"); |
| 98 | final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER, |
| 99 | "", "", "", "", null); |
| 100 | final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER, |
| 101 | "", "", "", "", null); |
| 102 | |
| 103 | final MockFlowEntry flow1 = new MockFlowEntry(deviceId1, 1); |
| 104 | final MockFlowEntry flow2 = new MockFlowEntry(deviceId1, 2); |
| 105 | |
| 106 | final MockFlowEntry flow3 = new MockFlowEntry(deviceId2, 3); |
| 107 | final MockFlowEntry flow4 = new MockFlowEntry(deviceId2, 4); |
| 108 | |
| 109 | final MockFlowEntry flow5 = new MockFlowEntry(deviceId2, 5); |
| 110 | final MockFlowEntry flow6 = new MockFlowEntry(deviceId2, 6); |
| 111 | |
| 112 | /** |
| 113 | * Mock class for a flow entry. |
| 114 | */ |
| 115 | private static class MockFlowEntry implements FlowEntry { |
| 116 | final DeviceId deviceId; |
| 117 | final long baseValue; |
| 118 | TrafficTreatment treatment; |
| 119 | TrafficSelector selector; |
| 120 | |
| 121 | public MockFlowEntry(DeviceId deviceId, long id) { |
| 122 | this.deviceId = deviceId; |
| 123 | this.baseValue = id * 100; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public FlowEntryState state() { |
| 128 | return FlowEntryState.ADDED; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public long life() { |
| 133 | return baseValue + 11; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public long packets() { |
| 138 | return baseValue + 22; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public long bytes() { |
| 143 | return baseValue + 33; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public long lastSeen() { |
| 148 | return baseValue + 44; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public int errType() { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public int errCode() { |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public FlowId id() { |
| 163 | final long id = baseValue + 55; |
| 164 | return FlowId.valueOf(id); |
| 165 | } |
| 166 | |
| 167 | @Override |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 168 | public GroupId groupId() { |
| 169 | return new DefaultGroupId(3); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 173 | public short appId() { |
| 174 | return 2; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public int priority() { |
| 179 | return (int) (baseValue + 66); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public DeviceId deviceId() { |
| 184 | return deviceId; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public TrafficSelector selector() { |
| 189 | return selector; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public TrafficTreatment treatment() { |
| 194 | return treatment; |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public int timeout() { |
| 199 | return (int) (baseValue + 77); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public boolean isPermanent() { |
| 204 | return false; |
| 205 | } |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 206 | |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 207 | @Override |
alshabib | db77407 | 2015-04-20 13:13:51 -0700 | [diff] [blame] | 208 | public int tableId() { |
| 209 | return 0; |
| 210 | } |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 211 | |
| 212 | @Override |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 213 | public boolean exactMatch(FlowRule rule) { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | @Override |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 218 | public FlowRuleExtPayLoad payLoad() { |
| 219 | return null; |
| 220 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 223 | /** |
| 224 | * Populates some flows used as testing data. |
| 225 | */ |
| 226 | private void setupMockFlows() { |
| 227 | flow2.treatment = DefaultTrafficTreatment.builder() |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 228 | .add(Instructions.modL0Lambda(new IndexedLambda((short) 4))) |
| 229 | .add(Instructions.modL0Lambda(new IndexedLambda((short) 5))) |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 230 | .setEthDst(MacAddress.BROADCAST) |
| 231 | .build(); |
| 232 | flow2.selector = DefaultTrafficSelector.builder() |
| 233 | .matchEthType((short) 3) |
| 234 | .matchIPProtocol((byte) 9) |
| 235 | .build(); |
| 236 | flow4.treatment = DefaultTrafficTreatment.builder() |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 237 | .add(Instructions.modL0Lambda(new IndexedLambda((short) 6))) |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 238 | .build(); |
| 239 | final Set<FlowEntry> flows1 = new HashSet<>(); |
| 240 | flows1.add(flow1); |
| 241 | flows1.add(flow2); |
| 242 | |
| 243 | final Set<FlowEntry> flows2 = new HashSet<>(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 244 | flows2.add(flow3); |
| 245 | flows2.add(flow4); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 246 | |
| 247 | rules.put(deviceId1, flows1); |
| 248 | rules.put(deviceId2, flows2); |
| 249 | |
| 250 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 251 | .andReturn(rules.get(deviceId1)).anyTimes(); |
| 252 | expect(mockFlowService.getFlowEntries(deviceId2)) |
| 253 | .andReturn(rules.get(deviceId2)).anyTimes(); |
| 254 | } |
| 255 | |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 256 | public FlowsResourceTest() { |
| 257 | super(CoreWebApplication.class); |
| 258 | } |
| 259 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 260 | /** |
| 261 | * Sets up the global values for all the tests. |
| 262 | */ |
| 263 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 264 | public void setUpTest() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 265 | // Mock device service |
| 266 | expect(mockDeviceService.getDevice(deviceId1)) |
| 267 | .andReturn(device1); |
| 268 | expect(mockDeviceService.getDevice(deviceId2)) |
| 269 | .andReturn(device2); |
| 270 | expect(mockDeviceService.getDevices()) |
| 271 | .andReturn(ImmutableSet.of(device1, device2)); |
| 272 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 273 | // Mock Core Service |
| 274 | expect(mockCoreService.getAppId(anyShort())) |
| 275 | .andReturn(NetTestTools.APP_ID).anyTimes(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 276 | expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID)) |
| 277 | .andReturn(APP_ID).anyTimes(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 278 | replay(mockCoreService); |
| 279 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 280 | // Register the services needed for the test |
| 281 | final CodecManager codecService = new CodecManager(); |
| 282 | codecService.activate(); |
| 283 | ServiceDirectory testDirectory = |
| 284 | new TestServiceDirectory() |
| 285 | .add(FlowRuleService.class, mockFlowService) |
| 286 | .add(DeviceService.class, mockDeviceService) |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 287 | .add(CodecService.class, codecService) |
| 288 | .add(CoreService.class, mockCoreService); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 289 | |
| 290 | BaseResource.setServiceDirectory(testDirectory); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Cleans up and verifies the mocks. |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 295 | */ |
| 296 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 297 | public void tearDownTest() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 298 | verify(mockFlowService); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 299 | verify(mockCoreService); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Hamcrest matcher to check that a flow representation in JSON matches |
| 304 | * the actual flow entry. |
| 305 | */ |
| 306 | public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 307 | private final FlowEntry flow; |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 308 | private final String expectedAppId; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 309 | private String reason = ""; |
| 310 | |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 311 | public FlowJsonMatcher(FlowEntry flowValue, String expectedAppIdValue) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 312 | flow = flowValue; |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 313 | expectedAppId = expectedAppIdValue; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public boolean matchesSafely(JsonObject jsonFlow) { |
| 318 | // check id |
| 319 | final String jsonId = jsonFlow.get("id").asString(); |
| 320 | final String flowId = Long.toString(flow.id().value()); |
| 321 | if (!jsonId.equals(flowId)) { |
| 322 | reason = "id " + flow.id().toString(); |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | // check application id |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 327 | final String jsonAppId = jsonFlow.get("appId").asString(); |
| 328 | if (!jsonAppId.equals(expectedAppId)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 329 | reason = "appId " + Short.toString(flow.appId()); |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | // check device id |
| 334 | final String jsonDeviceId = jsonFlow.get("deviceId").asString(); |
| 335 | if (!jsonDeviceId.equals(flow.deviceId().toString())) { |
| 336 | reason = "deviceId " + flow.deviceId(); |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | // check treatment and instructions array |
| 341 | if (flow.treatment() != null) { |
| 342 | final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject(); |
| 343 | final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray(); |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 344 | if (flow.treatment().immediate().size() != jsonInstructions.size()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 345 | reason = "instructions array size of " + |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 346 | Integer.toString(flow.treatment().immediate().size()); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 347 | return false; |
| 348 | } |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 349 | for (final Instruction instruction : flow.treatment().immediate()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 350 | boolean instructionFound = false; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 351 | for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 352 | final String jsonType = |
| 353 | jsonInstructions.get(instructionIndex) |
| 354 | .asObject().get("type").asString(); |
| 355 | final String instructionType = instruction.type().name(); |
| 356 | if (jsonType.equals(instructionType)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 357 | instructionFound = true; |
| 358 | } |
| 359 | } |
| 360 | if (!instructionFound) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 361 | reason = "instruction " + instruction.toString(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // check selector and criteria array |
| 368 | if (flow.selector() != null) { |
| 369 | final JsonObject jsonTreatment = jsonFlow.get("selector").asObject(); |
| 370 | final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray(); |
| 371 | if (flow.selector().criteria().size() != jsonCriteria.size()) { |
| 372 | reason = "criteria array size of " + |
| 373 | Integer.toString(flow.selector().criteria().size()); |
| 374 | return false; |
| 375 | } |
| 376 | for (final Criterion criterion : flow.selector().criteria()) { |
| 377 | boolean criterionFound = false; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 378 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 379 | for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 380 | final String jsonType = |
| 381 | jsonCriteria.get(criterionIndex) |
| 382 | .asObject().get("type").asString(); |
| 383 | final String criterionType = criterion.type().name(); |
| 384 | if (jsonType.equals(criterionType)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 385 | criterionFound = true; |
| 386 | } |
| 387 | } |
| 388 | if (!criterionFound) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 389 | reason = "criterion " + criterion.toString(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public void describeTo(Description description) { |
| 400 | description.appendText(reason); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Factory to allocate a flow matcher. |
| 406 | * |
| 407 | * @param flow flow object we are looking for |
| 408 | * @return matcher |
| 409 | */ |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 410 | private static FlowJsonMatcher matchesFlow(FlowEntry flow, String expectedAppName) { |
| 411 | return new FlowJsonMatcher(flow, expectedAppName); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Hamcrest matcher to check that a flow is represented properly in a JSON |
| 416 | * array of flows. |
| 417 | */ |
| 418 | public static class FlowJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { |
| 419 | private final FlowEntry flow; |
| 420 | private String reason = ""; |
| 421 | |
| 422 | public FlowJsonArrayMatcher(FlowEntry flowValue) { |
| 423 | flow = flowValue; |
| 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public boolean matchesSafely(JsonArray json) { |
| 428 | boolean flowFound = false; |
| 429 | |
| 430 | for (int jsonFlowIndex = 0; jsonFlowIndex < json.size(); |
| 431 | jsonFlowIndex++) { |
| 432 | |
| 433 | final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject(); |
| 434 | |
| 435 | final String flowId = Long.toString(flow.id().value()); |
| 436 | final String jsonFlowId = jsonFlow.get("id").asString(); |
| 437 | if (jsonFlowId.equals(flowId)) { |
| 438 | flowFound = true; |
| 439 | |
| 440 | // We found the correct flow, check attribute values |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 441 | assertThat(jsonFlow, matchesFlow(flow, APP_ID.name())); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | if (!flowFound) { |
| 445 | reason = "Flow with id " + flow.id().toString() + " not found"; |
| 446 | return false; |
| 447 | } else { |
| 448 | return true; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public void describeTo(Description description) { |
| 454 | description.appendText(reason); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Factory to allocate a flow array matcher. |
| 460 | * |
| 461 | * @param flow flow object we are looking for |
| 462 | * @return matcher |
| 463 | */ |
| 464 | private static FlowJsonArrayMatcher hasFlow(FlowEntry flow) { |
| 465 | return new FlowJsonArrayMatcher(flow); |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Tests the result of the rest api GET when there are no flows. |
| 470 | */ |
| 471 | @Test |
| 472 | public void testFlowsEmptyArray() { |
| 473 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 474 | .andReturn(null).anyTimes(); |
| 475 | expect(mockFlowService.getFlowEntries(deviceId2)) |
| 476 | .andReturn(null).anyTimes(); |
| 477 | replay(mockFlowService); |
| 478 | replay(mockDeviceService); |
| 479 | final WebResource rs = resource(); |
| 480 | final String response = rs.path("flows").get(String.class); |
| 481 | assertThat(response, is("{\"flows\":[]}")); |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Tests the result of the rest api GET when there are active flows. |
| 486 | */ |
| 487 | @Test |
| 488 | public void testFlowsPopulatedArray() { |
| 489 | setupMockFlows(); |
| 490 | replay(mockFlowService); |
| 491 | replay(mockDeviceService); |
| 492 | final WebResource rs = resource(); |
| 493 | final String response = rs.path("flows").get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 494 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 495 | assertThat(result, notNullValue()); |
| 496 | |
| 497 | assertThat(result.names(), hasSize(1)); |
| 498 | assertThat(result.names().get(0), is("flows")); |
| 499 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 500 | assertThat(jsonFlows, notNullValue()); |
| 501 | assertThat(jsonFlows, hasFlow(flow1)); |
| 502 | assertThat(jsonFlows, hasFlow(flow2)); |
| 503 | assertThat(jsonFlows, hasFlow(flow3)); |
| 504 | assertThat(jsonFlows, hasFlow(flow4)); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Tests the result of a rest api GET for a device. |
| 509 | */ |
| 510 | @Test |
| 511 | public void testFlowsSingleDevice() { |
| 512 | setupMockFlows(); |
| 513 | final Set<FlowEntry> flows = new HashSet<>(); |
| 514 | flows.add(flow5); |
| 515 | flows.add(flow6); |
| 516 | expect(mockFlowService.getFlowEntries(anyObject())) |
| 517 | .andReturn(flows).anyTimes(); |
| 518 | replay(mockFlowService); |
| 519 | replay(mockDeviceService); |
| 520 | final WebResource rs = resource(); |
| 521 | final String response = rs.path("flows/" + deviceId3).get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 522 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 523 | assertThat(result, notNullValue()); |
| 524 | |
| 525 | assertThat(result.names(), hasSize(1)); |
| 526 | assertThat(result.names().get(0), is("flows")); |
| 527 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 528 | assertThat(jsonFlows, notNullValue()); |
| 529 | assertThat(jsonFlows, hasFlow(flow5)); |
| 530 | assertThat(jsonFlows, hasFlow(flow6)); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Tests the result of a rest api GET for a device. |
| 535 | */ |
| 536 | @Test |
| 537 | public void testFlowsSingleDeviceWithFlowId() { |
| 538 | setupMockFlows(); |
| 539 | final Set<FlowEntry> flows = new HashSet<>(); |
| 540 | flows.add(flow5); |
| 541 | flows.add(flow6); |
| 542 | expect(mockFlowService.getFlowEntries(anyObject())) |
| 543 | .andReturn(flows).anyTimes(); |
| 544 | replay(mockFlowService); |
| 545 | replay(mockDeviceService); |
| 546 | final WebResource rs = resource(); |
| 547 | final String response = rs.path("flows/" + deviceId3 + "/" |
| 548 | + Long.toString(flow5.id().value())).get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 549 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 550 | assertThat(result, notNullValue()); |
| 551 | |
| 552 | assertThat(result.names(), hasSize(1)); |
| 553 | assertThat(result.names().get(0), is("flows")); |
| 554 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 555 | assertThat(jsonFlows, notNullValue()); |
| 556 | assertThat(jsonFlows, hasFlow(flow5)); |
| 557 | assertThat(jsonFlows, not(hasFlow(flow6))); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Tests that a fetch of a non-existent device object throws an exception. |
| 562 | */ |
| 563 | @Test |
| 564 | public void testBadGet() { |
| 565 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 566 | .andReturn(null).anyTimes(); |
| 567 | expect(mockFlowService.getFlowEntries(deviceId2)) |
| 568 | .andReturn(null).anyTimes(); |
| 569 | replay(mockFlowService); |
| 570 | replay(mockDeviceService); |
| 571 | |
| 572 | WebResource rs = resource(); |
| 573 | try { |
| 574 | rs.path("flows/0").get(String.class); |
| 575 | fail("Fetch of non-existent device did not throw an exception"); |
| 576 | } catch (UniformInterfaceException ex) { |
| 577 | assertThat(ex.getMessage(), |
| 578 | containsString("returned a response status of")); |
| 579 | } |
| 580 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 581 | |
| 582 | /** |
| 583 | * Tests creating a flow with POST. |
| 584 | */ |
| 585 | @Test |
| 586 | public void testPost() { |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 587 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 588 | |
| 589 | mockFlowService.applyFlowRules(anyObject()); |
| 590 | expectLastCall(); |
| 591 | replay(mockFlowService); |
| 592 | |
| 593 | WebResource rs = resource(); |
Phaneendra Manda | 2be2f88 | 2015-11-11 15:23:40 +0530 | [diff] [blame] | 594 | InputStream jsonStream = FlowsResourceTest.class |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 595 | .getResourceAsStream("post-flow.json"); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 596 | |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 597 | ClientResponse response = rs.path("flows/of:0000000000000001") |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 598 | .type(MediaType.APPLICATION_JSON_TYPE) |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 599 | .post(ClientResponse.class, jsonStream); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 600 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
| 601 | String location = response.getLocation().getPath(); |
| 602 | assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/")); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Tests deleting a flow. |
| 607 | */ |
| 608 | @Test |
| 609 | public void testDelete() { |
| 610 | setupMockFlows(); |
| 611 | mockFlowService.removeFlowRules(anyObject()); |
| 612 | expectLastCall(); |
| 613 | replay(mockFlowService); |
| 614 | |
| 615 | WebResource rs = resource(); |
| 616 | |
| 617 | String location = "/flows/1/155"; |
| 618 | |
| 619 | ClientResponse deleteResponse = rs.path(location) |
| 620 | .type(MediaType.APPLICATION_JSON_TYPE) |
| 621 | .delete(ClientResponse.class); |
| 622 | assertThat(deleteResponse.getStatus(), |
| 623 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 624 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 625 | } |