Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [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.vtnweb.resources; |
| 17 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 19 | import com.eclipsesource.json.JsonObject; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
| 24 | import org.onlab.osgi.TestServiceDirectory; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.rest.BaseResource; |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 27 | import org.onosproject.codec.CodecService; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 28 | import org.onosproject.vtnrsc.FlowClassifier; |
| 29 | import org.onosproject.vtnrsc.FlowClassifierId; |
| 30 | import org.onosproject.vtnrsc.TenantId; |
| 31 | import org.onosproject.vtnrsc.VirtualPortId; |
| 32 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 33 | import org.onosproject.vtnweb.web.SfcCodecContext; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 34 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 35 | import javax.ws.rs.NotFoundException; |
| 36 | import javax.ws.rs.client.Entity; |
| 37 | import javax.ws.rs.client.WebTarget; |
| 38 | import javax.ws.rs.core.MediaType; |
| 39 | import javax.ws.rs.core.Response; |
| 40 | import java.io.InputStream; |
| 41 | import java.net.HttpURLConnection; |
| 42 | import java.util.HashSet; |
| 43 | import java.util.Objects; |
| 44 | import java.util.Set; |
| 45 | |
| 46 | import static org.easymock.EasyMock.anyObject; |
| 47 | import static org.easymock.EasyMock.createMock; |
| 48 | import static org.easymock.EasyMock.expect; |
| 49 | import static org.easymock.EasyMock.replay; |
| 50 | import static org.hamcrest.Matchers.containsString; |
| 51 | import static org.hamcrest.Matchers.is; |
| 52 | import static org.hamcrest.Matchers.notNullValue; |
| 53 | import static org.junit.Assert.assertThat; |
| 54 | import static org.junit.Assert.fail; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 55 | /** |
| 56 | * Unit tests for flow classifier REST APIs. |
| 57 | */ |
| 58 | public class FlowClassifierResourceTest extends VtnResourceTest { |
| 59 | |
| 60 | final FlowClassifierService flowClassifierService = createMock(FlowClassifierService.class); |
| 61 | |
| 62 | FlowClassifierId flowClassifierId1 = FlowClassifierId.of("4a334cd4-fe9c-4fae-af4b-321c5e2eb051"); |
| 63 | TenantId tenantId1 = TenantId.tenantId("1814726e2d22407b8ca76db5e567dcf1"); |
| 64 | VirtualPortId srcPortId1 = VirtualPortId.portId("dace4513-24fc-4fae-af4b-321c5e2eb3d1"); |
| 65 | VirtualPortId dstPortId1 = VirtualPortId.portId("aef3478a-4a56-2a6e-cd3a-9dee4e2ec345"); |
| 66 | |
| 67 | final MockFlowClassifier flowClassifier1 = new MockFlowClassifier(flowClassifierId1, tenantId1, "flowClassifier1", |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 68 | "Mock flow classifier", "IPv4", "IP", 10000, |
| 69 | 1001, 1500, 5001, 6000, |
| 70 | IpPrefix.valueOf("1.1.1.1/16"), |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 71 | IpPrefix.valueOf("22.12.34.45/16"), |
| 72 | srcPortId1, dstPortId1); |
| 73 | |
| 74 | /** |
| 75 | * Mock class for a flow classifier. |
| 76 | */ |
| 77 | private static class MockFlowClassifier implements FlowClassifier { |
| 78 | |
| 79 | private final FlowClassifierId flowClassifierId; |
| 80 | private final TenantId tenantId; |
| 81 | private final String name; |
| 82 | private final String description; |
| 83 | private final String etherType; |
| 84 | private final String protocol; |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 85 | private final int priority; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 86 | private final int minSrcPortRange; |
| 87 | private final int maxSrcPortRange; |
| 88 | private final int minDstPortRange; |
| 89 | private final int maxDstPortRange; |
| 90 | private final IpPrefix srcIpPrefix; |
| 91 | private final IpPrefix dstIpPrefix; |
| 92 | private final VirtualPortId srcPort; |
| 93 | private final VirtualPortId dstPort; |
| 94 | |
| 95 | public MockFlowClassifier(FlowClassifierId flowClassifierId, TenantId tenantId, String name, |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 96 | String description, String etherType, String protocol, int priority, |
| 97 | int minSrcPortRange, int maxSrcPortRange, int minDstPortRange, int maxDstPortRange, |
| 98 | IpPrefix srcIpPrefix, IpPrefix dstIpPrefix, VirtualPortId srcPort, |
| 99 | VirtualPortId dstPort) { |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 100 | this.flowClassifierId = flowClassifierId; |
| 101 | this.tenantId = tenantId; |
| 102 | this.name = name; |
| 103 | this.description = description; |
| 104 | this.etherType = etherType; |
| 105 | this.protocol = protocol; |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 106 | this.priority = priority; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 107 | this.minSrcPortRange = minSrcPortRange; |
| 108 | this.maxSrcPortRange = maxSrcPortRange; |
| 109 | this.minDstPortRange = minDstPortRange; |
| 110 | this.maxDstPortRange = maxDstPortRange; |
| 111 | this.srcIpPrefix = srcIpPrefix; |
| 112 | this.dstIpPrefix = dstIpPrefix; |
| 113 | this.srcPort = srcPort; |
| 114 | this.dstPort = dstPort; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | @Override |
| 119 | public FlowClassifierId flowClassifierId() { |
| 120 | return flowClassifierId; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public TenantId tenantId() { |
| 125 | return tenantId; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String name() { |
| 130 | return name; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public String description() { |
| 135 | return description; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public String etherType() { |
| 140 | return etherType; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public String protocol() { |
| 145 | return protocol; |
| 146 | } |
| 147 | |
| 148 | @Override |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 149 | public int priority() { |
| 150 | return priority; |
| 151 | } |
| 152 | |
| 153 | @Override |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 154 | public int minSrcPortRange() { |
| 155 | return minSrcPortRange; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public int maxSrcPortRange() { |
| 160 | return maxSrcPortRange; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public int minDstPortRange() { |
| 165 | return minDstPortRange; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public int maxDstPortRange() { |
| 170 | return maxDstPortRange; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public IpPrefix srcIpPrefix() { |
| 175 | return srcIpPrefix; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public IpPrefix dstIpPrefix() { |
| 180 | return dstIpPrefix; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public VirtualPortId srcPort() { |
| 185 | return srcPort; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public VirtualPortId dstPort() { |
| 190 | return dstPort; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public boolean exactMatch(FlowClassifier flowClassifier) { |
| 195 | return this.equals(flowClassifier) && |
| 196 | Objects.equals(this.flowClassifierId, flowClassifier.flowClassifierId()) && |
| 197 | Objects.equals(this.tenantId, flowClassifier.tenantId()); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Sets up the global values for all the tests. |
| 203 | */ |
| 204 | @Before |
| 205 | public void setUpTest() { |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 206 | SfcCodecContext context = new SfcCodecContext(); |
| 207 | |
| 208 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 209 | .add(FlowClassifierService.class, flowClassifierService) |
| 210 | .add(CodecService.class, context.codecManager()); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 211 | BaseResource.setServiceDirectory(testDirectory); |
| 212 | |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Cleans up. |
| 217 | */ |
| 218 | @After |
| 219 | public void tearDownTest() { |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Tests the result of the rest api GET when there are no flow classifiers. |
| 224 | */ |
| 225 | @Test |
| 226 | public void testFlowClassifiersEmpty() { |
| 227 | |
| 228 | expect(flowClassifierService.getFlowClassifiers()).andReturn(null).anyTimes(); |
| 229 | replay(flowClassifierService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 230 | final WebTarget wt = target(); |
| 231 | final String response = wt.path("flow_classifiers").request().get(String.class); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 232 | assertThat(response, is("{\"flow_classifiers\":[]}")); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Tests the result of a rest api GET for flow classifier id. |
| 237 | */ |
| 238 | @Test |
| 239 | public void testGetFlowClassifierId() { |
| 240 | |
| 241 | final Set<FlowClassifier> flowClassifiers = new HashSet<>(); |
| 242 | flowClassifiers.add(flowClassifier1); |
| 243 | |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 244 | expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 245 | expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes(); |
| 246 | replay(flowClassifierService); |
| 247 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 248 | final WebTarget wt = target(); |
| 249 | final String response = wt.path("flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051") |
| 250 | .request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 251 | final JsonObject result = Json.parse(response).asObject(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 252 | assertThat(result, notNullValue()); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Tests that a fetch of a non-existent flow classifier object throws an exception. |
| 257 | */ |
| 258 | @Test |
| 259 | public void testBadGet() { |
| 260 | expect(flowClassifierService.getFlowClassifier(anyObject())) |
| 261 | .andReturn(null).anyTimes(); |
| 262 | replay(flowClassifierService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 263 | WebTarget wt = target(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 264 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 265 | wt.path("flow_classifiers/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae") |
| 266 | .request().get(String.class); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 267 | fail("Fetch of non-existent flow classifier did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 268 | } catch (NotFoundException ex) { |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 269 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 270 | containsString("HTTP 404 Not Found")); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Tests creating a flow classifier with POST. |
| 276 | */ |
| 277 | @Test |
| 278 | public void testPost() { |
| 279 | |
| 280 | expect(flowClassifierService.createFlowClassifier(anyObject())) |
| 281 | .andReturn(true).anyTimes(); |
| 282 | replay(flowClassifierService); |
| 283 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 284 | WebTarget wt = target(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 285 | InputStream jsonStream = FlowClassifierResourceTest.class.getResourceAsStream("post-FlowClassifier.json"); |
| 286 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 287 | Response response = wt.path("flow_classifiers") |
| 288 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 289 | .post(Entity.json(jsonStream)); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 290 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Tests deleting a flow classifier. |
| 295 | */ |
| 296 | @Test |
| 297 | public void testDelete() { |
| 298 | expect(flowClassifierService.removeFlowClassifier(anyObject())) |
| 299 | .andReturn(true).anyTimes(); |
| 300 | replay(flowClassifierService); |
| 301 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 302 | WebTarget wt = target(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 303 | |
| 304 | String location = "flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051"; |
| 305 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 306 | Response deleteResponse = wt.path(location) |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 307 | .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE) |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 308 | .delete(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 309 | assertThat(deleteResponse.getStatus(), |
| 310 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 311 | } |
| 312 | } |