blob: b21a80f1042599bb5ee44da9d3efff13eb053cd1 [file] [log] [blame]
Bharat saraswalf38e2262015-11-18 22:57:05 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bharat saraswalf38e2262015-11-18 22:57:05 +05303 *
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.vtnweb.resources;
17
Jian Li80cfe452016-01-14 16:04:58 -080018import com.eclipsesource.json.Json;
Jian Li9d616492016-03-09 10:52:49 -080019import com.eclipsesource.json.JsonObject;
Bharat saraswalf38e2262015-11-18 22:57:05 +053020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.osgi.ServiceDirectory;
24import org.onlab.osgi.TestServiceDirectory;
25import org.onlab.packet.IpPrefix;
26import org.onlab.rest.BaseResource;
Phaneendra Mandad66dca42015-12-01 20:24:10 +053027import org.onosproject.codec.CodecService;
Bharat saraswalf38e2262015-11-18 22:57:05 +053028import org.onosproject.vtnrsc.FlowClassifier;
29import org.onosproject.vtnrsc.FlowClassifierId;
30import org.onosproject.vtnrsc.TenantId;
31import org.onosproject.vtnrsc.VirtualPortId;
32import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
Phaneendra Mandad66dca42015-12-01 20:24:10 +053033import org.onosproject.vtnweb.web.SfcCodecContext;
Bharat saraswalf38e2262015-11-18 22:57:05 +053034
Jian Li9d616492016-03-09 10:52:49 -080035import javax.ws.rs.NotFoundException;
36import javax.ws.rs.client.Entity;
37import javax.ws.rs.client.WebTarget;
38import javax.ws.rs.core.MediaType;
39import javax.ws.rs.core.Response;
40import java.io.InputStream;
41import java.net.HttpURLConnection;
42import java.util.HashSet;
43import java.util.Objects;
44import java.util.Set;
45
46import static org.easymock.EasyMock.anyObject;
47import static org.easymock.EasyMock.createMock;
48import static org.easymock.EasyMock.expect;
49import static org.easymock.EasyMock.replay;
50import static org.hamcrest.Matchers.containsString;
51import static org.hamcrest.Matchers.is;
52import static org.hamcrest.Matchers.notNullValue;
53import static org.junit.Assert.assertThat;
54import static org.junit.Assert.fail;
Bharat saraswalf38e2262015-11-18 22:57:05 +053055/**
56 * Unit tests for flow classifier REST APIs.
57 */
58public 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 Manda299877f2016-04-13 23:28:03 +053068 "Mock flow classifier", "IPv4", "IP", 10000,
69 1001, 1500, 5001, 6000,
70 IpPrefix.valueOf("1.1.1.1/16"),
Bharat saraswalf38e2262015-11-18 22:57:05 +053071 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 Manda299877f2016-04-13 23:28:03 +053085 private final int priority;
Bharat saraswalf38e2262015-11-18 22:57:05 +053086 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 Manda299877f2016-04-13 23:28:03 +053096 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 saraswalf38e2262015-11-18 22:57:05 +0530100 this.flowClassifierId = flowClassifierId;
101 this.tenantId = tenantId;
102 this.name = name;
103 this.description = description;
104 this.etherType = etherType;
105 this.protocol = protocol;
Phaneendra Manda299877f2016-04-13 23:28:03 +0530106 this.priority = priority;
Bharat saraswalf38e2262015-11-18 22:57:05 +0530107 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 Manda299877f2016-04-13 23:28:03 +0530149 public int priority() {
150 return priority;
151 }
152
153 @Override
Bharat saraswalf38e2262015-11-18 22:57:05 +0530154 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 Mandad66dca42015-12-01 20:24:10 +0530206 SfcCodecContext context = new SfcCodecContext();
207
208 ServiceDirectory testDirectory = new TestServiceDirectory()
209 .add(FlowClassifierService.class, flowClassifierService)
210 .add(CodecService.class, context.codecManager());
Bharat saraswalf38e2262015-11-18 22:57:05 +0530211 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 Li9d616492016-03-09 10:52:49 -0800230 final WebTarget wt = target();
231 final String response = wt.path("flow_classifiers").request().get(String.class);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530232 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 Sfac02262015-11-20 19:13:22 +0530244 expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530245 expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes();
246 replay(flowClassifierService);
247
Jian Li9d616492016-03-09 10:52:49 -0800248 final WebTarget wt = target();
249 final String response = wt.path("flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051")
250 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800251 final JsonObject result = Json.parse(response).asObject();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530252 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 Li9d616492016-03-09 10:52:49 -0800263 WebTarget wt = target();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530264 try {
Jian Li9d616492016-03-09 10:52:49 -0800265 wt.path("flow_classifiers/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
266 .request().get(String.class);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530267 fail("Fetch of non-existent flow classifier did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800268 } catch (NotFoundException ex) {
Bharat saraswalf38e2262015-11-18 22:57:05 +0530269 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800270 containsString("HTTP 404 Not Found"));
Bharat saraswalf38e2262015-11-18 22:57:05 +0530271 }
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 Li9d616492016-03-09 10:52:49 -0800284 WebTarget wt = target();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530285 InputStream jsonStream = FlowClassifierResourceTest.class.getResourceAsStream("post-FlowClassifier.json");
286
Jian Li9d616492016-03-09 10:52:49 -0800287 Response response = wt.path("flow_classifiers")
288 .request(MediaType.APPLICATION_JSON_TYPE)
289 .post(Entity.json(jsonStream));
Bharat saraswalf38e2262015-11-18 22:57:05 +0530290 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 Li9d616492016-03-09 10:52:49 -0800302 WebTarget wt = target();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530303
304 String location = "flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051";
305
Jian Li9d616492016-03-09 10:52:49 -0800306 Response deleteResponse = wt.path(location)
Ray Milkey7c251822016-04-06 17:38:25 -0700307 .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE)
Jian Li9d616492016-03-09 10:52:49 -0800308 .delete();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530309 assertThat(deleteResponse.getStatus(),
310 is(HttpURLConnection.HTTP_NO_CONTENT));
311 }
312}