blob: da9d6d884d8c8784561be10446db726923fb4dd4 [file] [log] [blame]
Bharat saraswalf38e2262015-11-18 22:57:05 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Phaneendra Mandad66dca42015-12-01 20:24:10 +053026import org.onosproject.codec.CodecService;
Bharat saraswalf38e2262015-11-18 22:57:05 +053027import org.onosproject.vtnrsc.FlowClassifier;
28import org.onosproject.vtnrsc.FlowClassifierId;
29import org.onosproject.vtnrsc.TenantId;
30import org.onosproject.vtnrsc.VirtualPortId;
31import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
Phaneendra Mandad66dca42015-12-01 20:24:10 +053032import org.onosproject.vtnweb.web.SfcCodecContext;
Bharat saraswalf38e2262015-11-18 22:57:05 +053033
Jian Li9d616492016-03-09 10:52:49 -080034import javax.ws.rs.NotFoundException;
35import javax.ws.rs.client.Entity;
36import javax.ws.rs.client.WebTarget;
37import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39import java.io.InputStream;
40import java.net.HttpURLConnection;
41import java.util.HashSet;
42import java.util.Objects;
43import java.util.Set;
44
45import static org.easymock.EasyMock.anyObject;
46import static org.easymock.EasyMock.createMock;
47import static org.easymock.EasyMock.expect;
48import static org.easymock.EasyMock.replay;
49import static org.hamcrest.Matchers.containsString;
50import static org.hamcrest.Matchers.is;
51import static org.hamcrest.Matchers.notNullValue;
52import static org.junit.Assert.assertThat;
53import static org.junit.Assert.fail;
Bharat saraswalf38e2262015-11-18 22:57:05 +053054/**
55 * Unit tests for flow classifier REST APIs.
56 */
57public class FlowClassifierResourceTest extends VtnResourceTest {
58
59 final FlowClassifierService flowClassifierService = createMock(FlowClassifierService.class);
60
61 FlowClassifierId flowClassifierId1 = FlowClassifierId.of("4a334cd4-fe9c-4fae-af4b-321c5e2eb051");
62 TenantId tenantId1 = TenantId.tenantId("1814726e2d22407b8ca76db5e567dcf1");
63 VirtualPortId srcPortId1 = VirtualPortId.portId("dace4513-24fc-4fae-af4b-321c5e2eb3d1");
64 VirtualPortId dstPortId1 = VirtualPortId.portId("aef3478a-4a56-2a6e-cd3a-9dee4e2ec345");
65
66 final MockFlowClassifier flowClassifier1 = new MockFlowClassifier(flowClassifierId1, tenantId1, "flowClassifier1",
Phaneendra Manda299877f2016-04-13 23:28:03 +053067 "Mock flow classifier", "IPv4", "IP", 10000,
68 1001, 1500, 5001, 6000,
69 IpPrefix.valueOf("1.1.1.1/16"),
Bharat saraswalf38e2262015-11-18 22:57:05 +053070 IpPrefix.valueOf("22.12.34.45/16"),
71 srcPortId1, dstPortId1);
72
73 /**
74 * Mock class for a flow classifier.
75 */
76 private static class MockFlowClassifier implements FlowClassifier {
77
78 private final FlowClassifierId flowClassifierId;
79 private final TenantId tenantId;
80 private final String name;
81 private final String description;
82 private final String etherType;
83 private final String protocol;
Phaneendra Manda299877f2016-04-13 23:28:03 +053084 private final int priority;
Bharat saraswalf38e2262015-11-18 22:57:05 +053085 private final int minSrcPortRange;
86 private final int maxSrcPortRange;
87 private final int minDstPortRange;
88 private final int maxDstPortRange;
89 private final IpPrefix srcIpPrefix;
90 private final IpPrefix dstIpPrefix;
91 private final VirtualPortId srcPort;
92 private final VirtualPortId dstPort;
93
94 public MockFlowClassifier(FlowClassifierId flowClassifierId, TenantId tenantId, String name,
Phaneendra Manda299877f2016-04-13 23:28:03 +053095 String description, String etherType, String protocol, int priority,
96 int minSrcPortRange, int maxSrcPortRange, int minDstPortRange, int maxDstPortRange,
97 IpPrefix srcIpPrefix, IpPrefix dstIpPrefix, VirtualPortId srcPort,
98 VirtualPortId dstPort) {
Bharat saraswalf38e2262015-11-18 22:57:05 +053099 this.flowClassifierId = flowClassifierId;
100 this.tenantId = tenantId;
101 this.name = name;
102 this.description = description;
103 this.etherType = etherType;
104 this.protocol = protocol;
Phaneendra Manda299877f2016-04-13 23:28:03 +0530105 this.priority = priority;
Bharat saraswalf38e2262015-11-18 22:57:05 +0530106 this.minSrcPortRange = minSrcPortRange;
107 this.maxSrcPortRange = maxSrcPortRange;
108 this.minDstPortRange = minDstPortRange;
109 this.maxDstPortRange = maxDstPortRange;
110 this.srcIpPrefix = srcIpPrefix;
111 this.dstIpPrefix = dstIpPrefix;
112 this.srcPort = srcPort;
113 this.dstPort = dstPort;
114 }
115
116
117 @Override
118 public FlowClassifierId flowClassifierId() {
119 return flowClassifierId;
120 }
121
122 @Override
123 public TenantId tenantId() {
124 return tenantId;
125 }
126
127 @Override
128 public String name() {
129 return name;
130 }
131
132 @Override
133 public String description() {
134 return description;
135 }
136
137 @Override
138 public String etherType() {
139 return etherType;
140 }
141
142 @Override
143 public String protocol() {
144 return protocol;
145 }
146
147 @Override
Phaneendra Manda299877f2016-04-13 23:28:03 +0530148 public int priority() {
149 return priority;
150 }
151
152 @Override
Bharat saraswalf38e2262015-11-18 22:57:05 +0530153 public int minSrcPortRange() {
154 return minSrcPortRange;
155 }
156
157 @Override
158 public int maxSrcPortRange() {
159 return maxSrcPortRange;
160 }
161
162 @Override
163 public int minDstPortRange() {
164 return minDstPortRange;
165 }
166
167 @Override
168 public int maxDstPortRange() {
169 return maxDstPortRange;
170 }
171
172 @Override
173 public IpPrefix srcIpPrefix() {
174 return srcIpPrefix;
175 }
176
177 @Override
178 public IpPrefix dstIpPrefix() {
179 return dstIpPrefix;
180 }
181
182 @Override
183 public VirtualPortId srcPort() {
184 return srcPort;
185 }
186
187 @Override
188 public VirtualPortId dstPort() {
189 return dstPort;
190 }
191
192 @Override
193 public boolean exactMatch(FlowClassifier flowClassifier) {
194 return this.equals(flowClassifier) &&
195 Objects.equals(this.flowClassifierId, flowClassifier.flowClassifierId()) &&
196 Objects.equals(this.tenantId, flowClassifier.tenantId());
197 }
198 }
199
200 /**
201 * Sets up the global values for all the tests.
202 */
203 @Before
204 public void setUpTest() {
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530205 SfcCodecContext context = new SfcCodecContext();
206
207 ServiceDirectory testDirectory = new TestServiceDirectory()
208 .add(FlowClassifierService.class, flowClassifierService)
209 .add(CodecService.class, context.codecManager());
Ray Milkey094a1352018-01-22 14:03:54 -0800210 setServiceDirectory(testDirectory);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530211
212 }
213
214 /**
215 * Cleans up.
216 */
217 @After
218 public void tearDownTest() {
219 }
220
221 /**
222 * Tests the result of the rest api GET when there are no flow classifiers.
223 */
224 @Test
225 public void testFlowClassifiersEmpty() {
226
227 expect(flowClassifierService.getFlowClassifiers()).andReturn(null).anyTimes();
228 replay(flowClassifierService);
Jian Li9d616492016-03-09 10:52:49 -0800229 final WebTarget wt = target();
230 final String response = wt.path("flow_classifiers").request().get(String.class);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530231 assertThat(response, is("{\"flow_classifiers\":[]}"));
232 }
233
234 /**
235 * Tests the result of a rest api GET for flow classifier id.
236 */
237 @Test
238 public void testGetFlowClassifierId() {
239
240 final Set<FlowClassifier> flowClassifiers = new HashSet<>();
241 flowClassifiers.add(flowClassifier1);
242
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530243 expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530244 expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes();
245 replay(flowClassifierService);
246
Jian Li9d616492016-03-09 10:52:49 -0800247 final WebTarget wt = target();
248 final String response = wt.path("flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051")
249 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800250 final JsonObject result = Json.parse(response).asObject();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530251 assertThat(result, notNullValue());
252 }
253
254 /**
255 * Tests that a fetch of a non-existent flow classifier object throws an exception.
256 */
257 @Test
258 public void testBadGet() {
259 expect(flowClassifierService.getFlowClassifier(anyObject()))
260 .andReturn(null).anyTimes();
261 replay(flowClassifierService);
Jian Li9d616492016-03-09 10:52:49 -0800262 WebTarget wt = target();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530263 try {
Jian Li9d616492016-03-09 10:52:49 -0800264 wt.path("flow_classifiers/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
265 .request().get(String.class);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530266 fail("Fetch of non-existent flow classifier did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800267 } catch (NotFoundException ex) {
Bharat saraswalf38e2262015-11-18 22:57:05 +0530268 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800269 containsString("HTTP 404 Not Found"));
Bharat saraswalf38e2262015-11-18 22:57:05 +0530270 }
271 }
272
273 /**
274 * Tests creating a flow classifier with POST.
275 */
276 @Test
277 public void testPost() {
278
279 expect(flowClassifierService.createFlowClassifier(anyObject()))
280 .andReturn(true).anyTimes();
281 replay(flowClassifierService);
282
Jian Li9d616492016-03-09 10:52:49 -0800283 WebTarget wt = target();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530284 InputStream jsonStream = FlowClassifierResourceTest.class.getResourceAsStream("post-FlowClassifier.json");
285
Jian Li9d616492016-03-09 10:52:49 -0800286 Response response = wt.path("flow_classifiers")
287 .request(MediaType.APPLICATION_JSON_TYPE)
288 .post(Entity.json(jsonStream));
Bharat saraswalf38e2262015-11-18 22:57:05 +0530289 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
290 }
291
292 /**
293 * Tests deleting a flow classifier.
294 */
295 @Test
296 public void testDelete() {
297 expect(flowClassifierService.removeFlowClassifier(anyObject()))
298 .andReturn(true).anyTimes();
299 replay(flowClassifierService);
300
Jian Li9d616492016-03-09 10:52:49 -0800301 WebTarget wt = target();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530302
303 String location = "flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051";
304
Jian Li9d616492016-03-09 10:52:49 -0800305 Response deleteResponse = wt.path(location)
Ray Milkey7c251822016-04-06 17:38:25 -0700306 .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE)
Jian Li9d616492016-03-09 10:52:49 -0800307 .delete();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530308 assertThat(deleteResponse.getStatus(),
309 is(HttpURLConnection.HTTP_NO_CONTENT));
310 }
311}