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 | */ |
Jian Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame^] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 22 | import org.hamcrest.Description; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 23 | import org.hamcrest.Matchers; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 24 | import org.hamcrest.TypeSafeMatcher; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.onlab.osgi.ServiceDirectory; |
| 29 | import org.onlab.osgi.TestServiceDirectory; |
| 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onlab.rest.BaseResource; |
| 32 | import org.onosproject.codec.CodecService; |
| 33 | import org.onosproject.codec.impl.CodecManager; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 34 | import org.onosproject.codec.impl.FlowRuleCodec; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 35 | import org.onosproject.core.CoreService; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 36 | import org.onosproject.core.DefaultGroupId; |
| 37 | import org.onosproject.core.GroupId; |
| 38 | import org.onosproject.net.DefaultDevice; |
| 39 | import org.onosproject.net.Device; |
| 40 | import org.onosproject.net.DeviceId; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 41 | import org.onosproject.net.IndexedLambda; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 42 | import org.onosproject.net.NetTestTools; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 43 | import org.onosproject.net.device.DeviceService; |
| 44 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 45 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 46 | import org.onosproject.net.flow.FlowEntry; |
| 47 | import org.onosproject.net.flow.FlowId; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flow.FlowRule; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.FlowRuleExtPayLoad; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.FlowRuleService; |
| 51 | import org.onosproject.net.flow.TrafficSelector; |
| 52 | import org.onosproject.net.flow.TrafficTreatment; |
| 53 | import org.onosproject.net.flow.criteria.Criterion; |
| 54 | import org.onosproject.net.flow.instructions.Instruction; |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 55 | import org.onosproject.net.flow.instructions.Instructions; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 56 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 57 | import javax.ws.rs.NotFoundException; |
| 58 | import javax.ws.rs.client.Entity; |
| 59 | import javax.ws.rs.client.WebTarget; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 60 | import javax.ws.rs.core.MediaType; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 61 | import javax.ws.rs.core.Response; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 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 | |
| 256 | /** |
| 257 | * Sets up the global values for all the tests. |
| 258 | */ |
| 259 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 260 | public void setUpTest() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 261 | // Mock device service |
| 262 | expect(mockDeviceService.getDevice(deviceId1)) |
| 263 | .andReturn(device1); |
| 264 | expect(mockDeviceService.getDevice(deviceId2)) |
| 265 | .andReturn(device2); |
| 266 | expect(mockDeviceService.getDevices()) |
| 267 | .andReturn(ImmutableSet.of(device1, device2)); |
| 268 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 269 | // Mock Core Service |
| 270 | expect(mockCoreService.getAppId(anyShort())) |
| 271 | .andReturn(NetTestTools.APP_ID).anyTimes(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 272 | expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID)) |
| 273 | .andReturn(APP_ID).anyTimes(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 274 | replay(mockCoreService); |
| 275 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 276 | // Register the services needed for the test |
| 277 | final CodecManager codecService = new CodecManager(); |
| 278 | codecService.activate(); |
| 279 | ServiceDirectory testDirectory = |
| 280 | new TestServiceDirectory() |
| 281 | .add(FlowRuleService.class, mockFlowService) |
| 282 | .add(DeviceService.class, mockDeviceService) |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 283 | .add(CodecService.class, codecService) |
| 284 | .add(CoreService.class, mockCoreService); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 285 | |
| 286 | BaseResource.setServiceDirectory(testDirectory); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Cleans up and verifies the mocks. |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 291 | */ |
| 292 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 293 | public void tearDownTest() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 294 | verify(mockFlowService); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 295 | verify(mockCoreService); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Hamcrest matcher to check that a flow representation in JSON matches |
| 300 | * the actual flow entry. |
| 301 | */ |
| 302 | public static class FlowJsonMatcher extends TypeSafeMatcher<JsonObject> { |
| 303 | private final FlowEntry flow; |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 304 | private final String expectedAppId; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 305 | private String reason = ""; |
| 306 | |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 307 | public FlowJsonMatcher(FlowEntry flowValue, String expectedAppIdValue) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 308 | flow = flowValue; |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 309 | expectedAppId = expectedAppIdValue; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public boolean matchesSafely(JsonObject jsonFlow) { |
| 314 | // check id |
| 315 | final String jsonId = jsonFlow.get("id").asString(); |
| 316 | final String flowId = Long.toString(flow.id().value()); |
| 317 | if (!jsonId.equals(flowId)) { |
| 318 | reason = "id " + flow.id().toString(); |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | // check application id |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 323 | final String jsonAppId = jsonFlow.get("appId").asString(); |
| 324 | if (!jsonAppId.equals(expectedAppId)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 325 | reason = "appId " + Short.toString(flow.appId()); |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | // check device id |
| 330 | final String jsonDeviceId = jsonFlow.get("deviceId").asString(); |
| 331 | if (!jsonDeviceId.equals(flow.deviceId().toString())) { |
| 332 | reason = "deviceId " + flow.deviceId(); |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | // check treatment and instructions array |
| 337 | if (flow.treatment() != null) { |
| 338 | final JsonObject jsonTreatment = jsonFlow.get("treatment").asObject(); |
| 339 | final JsonArray jsonInstructions = jsonTreatment.get("instructions").asArray(); |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 340 | if (flow.treatment().immediate().size() != jsonInstructions.size()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 341 | reason = "instructions array size of " + |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 342 | Integer.toString(flow.treatment().immediate().size()); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 343 | return false; |
| 344 | } |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 345 | for (final Instruction instruction : flow.treatment().immediate()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 346 | boolean instructionFound = false; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 347 | for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 348 | final String jsonType = |
| 349 | jsonInstructions.get(instructionIndex) |
| 350 | .asObject().get("type").asString(); |
| 351 | final String instructionType = instruction.type().name(); |
| 352 | if (jsonType.equals(instructionType)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 353 | instructionFound = true; |
| 354 | } |
| 355 | } |
| 356 | if (!instructionFound) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 357 | reason = "instruction " + instruction.toString(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 358 | return false; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // check selector and criteria array |
| 364 | if (flow.selector() != null) { |
| 365 | final JsonObject jsonTreatment = jsonFlow.get("selector").asObject(); |
| 366 | final JsonArray jsonCriteria = jsonTreatment.get("criteria").asArray(); |
| 367 | if (flow.selector().criteria().size() != jsonCriteria.size()) { |
| 368 | reason = "criteria array size of " + |
| 369 | Integer.toString(flow.selector().criteria().size()); |
| 370 | return false; |
| 371 | } |
| 372 | for (final Criterion criterion : flow.selector().criteria()) { |
| 373 | boolean criterionFound = false; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 374 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 375 | for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 376 | final String jsonType = |
| 377 | jsonCriteria.get(criterionIndex) |
| 378 | .asObject().get("type").asString(); |
| 379 | final String criterionType = criterion.type().name(); |
| 380 | if (jsonType.equals(criterionType)) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 381 | criterionFound = true; |
| 382 | } |
| 383 | } |
| 384 | if (!criterionFound) { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 385 | reason = "criterion " + criterion.toString(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public void describeTo(Description description) { |
| 396 | description.appendText(reason); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Factory to allocate a flow matcher. |
| 402 | * |
| 403 | * @param flow flow object we are looking for |
| 404 | * @return matcher |
| 405 | */ |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 406 | private static FlowJsonMatcher matchesFlow(FlowEntry flow, String expectedAppName) { |
| 407 | return new FlowJsonMatcher(flow, expectedAppName); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Hamcrest matcher to check that a flow is represented properly in a JSON |
| 412 | * array of flows. |
| 413 | */ |
| 414 | public static class FlowJsonArrayMatcher extends TypeSafeMatcher<JsonArray> { |
| 415 | private final FlowEntry flow; |
| 416 | private String reason = ""; |
| 417 | |
| 418 | public FlowJsonArrayMatcher(FlowEntry flowValue) { |
| 419 | flow = flowValue; |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public boolean matchesSafely(JsonArray json) { |
| 424 | boolean flowFound = false; |
| 425 | |
| 426 | for (int jsonFlowIndex = 0; jsonFlowIndex < json.size(); |
| 427 | jsonFlowIndex++) { |
| 428 | |
| 429 | final JsonObject jsonFlow = json.get(jsonFlowIndex).asObject(); |
| 430 | |
| 431 | final String flowId = Long.toString(flow.id().value()); |
| 432 | final String jsonFlowId = jsonFlow.get("id").asString(); |
| 433 | if (jsonFlowId.equals(flowId)) { |
| 434 | flowFound = true; |
| 435 | |
| 436 | // We found the correct flow, check attribute values |
Ray Milkey | be539db | 2015-09-04 11:00:43 -0700 | [diff] [blame] | 437 | assertThat(jsonFlow, matchesFlow(flow, APP_ID.name())); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | if (!flowFound) { |
| 441 | reason = "Flow with id " + flow.id().toString() + " not found"; |
| 442 | return false; |
| 443 | } else { |
| 444 | return true; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | @Override |
| 449 | public void describeTo(Description description) { |
| 450 | description.appendText(reason); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Factory to allocate a flow array matcher. |
| 456 | * |
| 457 | * @param flow flow object we are looking for |
| 458 | * @return matcher |
| 459 | */ |
| 460 | private static FlowJsonArrayMatcher hasFlow(FlowEntry flow) { |
| 461 | return new FlowJsonArrayMatcher(flow); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Tests the result of the rest api GET when there are no flows. |
| 466 | */ |
| 467 | @Test |
| 468 | public void testFlowsEmptyArray() { |
| 469 | expect(mockFlowService.getFlowEntries(deviceId1)) |
| 470 | .andReturn(null).anyTimes(); |
| 471 | expect(mockFlowService.getFlowEntries(deviceId2)) |
| 472 | .andReturn(null).anyTimes(); |
| 473 | replay(mockFlowService); |
| 474 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 475 | final WebTarget wt = target(); |
| 476 | final String response = wt.path("flows").request().get(String.class); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 477 | assertThat(response, is("{\"flows\":[]}")); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Tests the result of the rest api GET when there are active flows. |
| 482 | */ |
| 483 | @Test |
| 484 | public void testFlowsPopulatedArray() { |
| 485 | setupMockFlows(); |
| 486 | replay(mockFlowService); |
| 487 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 488 | final WebTarget wt = target(); |
| 489 | final String response = wt.path("flows").request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 490 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 491 | assertThat(result, notNullValue()); |
| 492 | |
| 493 | assertThat(result.names(), hasSize(1)); |
| 494 | assertThat(result.names().get(0), is("flows")); |
| 495 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 496 | assertThat(jsonFlows, notNullValue()); |
| 497 | assertThat(jsonFlows, hasFlow(flow1)); |
| 498 | assertThat(jsonFlows, hasFlow(flow2)); |
| 499 | assertThat(jsonFlows, hasFlow(flow3)); |
| 500 | assertThat(jsonFlows, hasFlow(flow4)); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Tests the result of a rest api GET for a device. |
| 505 | */ |
| 506 | @Test |
| 507 | public void testFlowsSingleDevice() { |
| 508 | setupMockFlows(); |
| 509 | final Set<FlowEntry> flows = new HashSet<>(); |
| 510 | flows.add(flow5); |
| 511 | flows.add(flow6); |
| 512 | expect(mockFlowService.getFlowEntries(anyObject())) |
| 513 | .andReturn(flows).anyTimes(); |
| 514 | replay(mockFlowService); |
| 515 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 516 | final WebTarget wt = target(); |
| 517 | final String response = wt.path("flows/" + deviceId3).request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 518 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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, hasFlow(flow6)); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Tests the result of a rest api GET for a device. |
| 531 | */ |
| 532 | @Test |
| 533 | public void testFlowsSingleDeviceWithFlowId() { |
| 534 | setupMockFlows(); |
| 535 | final Set<FlowEntry> flows = new HashSet<>(); |
| 536 | flows.add(flow5); |
| 537 | flows.add(flow6); |
| 538 | expect(mockFlowService.getFlowEntries(anyObject())) |
| 539 | .andReturn(flows).anyTimes(); |
| 540 | replay(mockFlowService); |
| 541 | replay(mockDeviceService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 542 | final WebTarget wt = target(); |
| 543 | final String response = wt.path("flows/" + deviceId3 + "/" |
| 544 | + Long.toString(flow5.id().value())).request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 545 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 546 | assertThat(result, notNullValue()); |
| 547 | |
| 548 | assertThat(result.names(), hasSize(1)); |
| 549 | assertThat(result.names().get(0), is("flows")); |
| 550 | final JsonArray jsonFlows = result.get("flows").asArray(); |
| 551 | assertThat(jsonFlows, notNullValue()); |
| 552 | assertThat(jsonFlows, hasFlow(flow5)); |
| 553 | assertThat(jsonFlows, not(hasFlow(flow6))); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Tests that a fetch of a non-existent device object throws an exception. |
| 558 | */ |
| 559 | @Test |
| 560 | public void testBadGet() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 561 | expect(mockFlowService.getFlowEntries(anyObject())) |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 562 | .andReturn(null).anyTimes(); |
| 563 | replay(mockFlowService); |
| 564 | replay(mockDeviceService); |
| 565 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 566 | WebTarget wt = target(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 567 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 568 | wt.path("flows/0").request().get(String.class); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 569 | fail("Fetch of non-existent device did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 570 | } catch (NotFoundException ex) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 571 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 572 | containsString("HTTP 404 Not Found")); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 573 | } |
| 574 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 575 | |
| 576 | /** |
| 577 | * Tests creating a flow with POST. |
| 578 | */ |
| 579 | @Test |
| 580 | public void testPost() { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 581 | mockFlowService.applyFlowRules(anyObject()); |
| 582 | expectLastCall(); |
| 583 | replay(mockFlowService); |
| 584 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 585 | WebTarget wt = target(); |
Phaneendra Manda | 2be2f88 | 2015-11-11 15:23:40 +0530 | [diff] [blame] | 586 | InputStream jsonStream = FlowsResourceTest.class |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 587 | .getResourceAsStream("post-flow.json"); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 588 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 589 | Response response = wt.path("flows/of:0000000000000001") |
| 590 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 591 | .post(Entity.json(jsonStream)); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 592 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED)); |
| 593 | String location = response.getLocation().getPath(); |
| 594 | assertThat(location, Matchers.startsWith("/flows/of:0000000000000001/")); |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Tests deleting a flow. |
| 599 | */ |
| 600 | @Test |
| 601 | public void testDelete() { |
| 602 | setupMockFlows(); |
| 603 | mockFlowService.removeFlowRules(anyObject()); |
| 604 | expectLastCall(); |
| 605 | replay(mockFlowService); |
| 606 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 607 | WebTarget wt = target(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 608 | |
| 609 | String location = "/flows/1/155"; |
| 610 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 611 | Response deleteResponse = wt.path(location) |
| 612 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 613 | .delete(); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 614 | assertThat(deleteResponse.getStatus(), |
| 615 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 616 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 617 | } |