blob: 673e088eb82113b842debbcff7d50811db1e549e [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",
68 "Mock flow classifier", "IPv4", "IP", 1001, 1500,
69 5001, 6000, IpPrefix.valueOf("1.1.1.1/16"),
70 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;
84 private final int minSrcPortRange;
85 private final int maxSrcPortRange;
86 private final int minDstPortRange;
87 private final int maxDstPortRange;
88 private final IpPrefix srcIpPrefix;
89 private final IpPrefix dstIpPrefix;
90 private final VirtualPortId srcPort;
91 private final VirtualPortId dstPort;
92
93 public MockFlowClassifier(FlowClassifierId flowClassifierId, TenantId tenantId, String name,
94 String description, String etherType, String protocol, int minSrcPortRange,
95 int maxSrcPortRange, int minDstPortRange, int maxDstPortRange, IpPrefix srcIpPrefix,
96 IpPrefix dstIpPrefix, VirtualPortId srcPort, VirtualPortId dstPort) {
97 this.flowClassifierId = flowClassifierId;
98 this.tenantId = tenantId;
99 this.name = name;
100 this.description = description;
101 this.etherType = etherType;
102 this.protocol = protocol;
103 this.minSrcPortRange = minSrcPortRange;
104 this.maxSrcPortRange = maxSrcPortRange;
105 this.minDstPortRange = minDstPortRange;
106 this.maxDstPortRange = maxDstPortRange;
107 this.srcIpPrefix = srcIpPrefix;
108 this.dstIpPrefix = dstIpPrefix;
109 this.srcPort = srcPort;
110 this.dstPort = dstPort;
111 }
112
113
114 @Override
115 public FlowClassifierId flowClassifierId() {
116 return flowClassifierId;
117 }
118
119 @Override
120 public TenantId tenantId() {
121 return tenantId;
122 }
123
124 @Override
125 public String name() {
126 return name;
127 }
128
129 @Override
130 public String description() {
131 return description;
132 }
133
134 @Override
135 public String etherType() {
136 return etherType;
137 }
138
139 @Override
140 public String protocol() {
141 return protocol;
142 }
143
144 @Override
145 public int minSrcPortRange() {
146 return minSrcPortRange;
147 }
148
149 @Override
150 public int maxSrcPortRange() {
151 return maxSrcPortRange;
152 }
153
154 @Override
155 public int minDstPortRange() {
156 return minDstPortRange;
157 }
158
159 @Override
160 public int maxDstPortRange() {
161 return maxDstPortRange;
162 }
163
164 @Override
165 public IpPrefix srcIpPrefix() {
166 return srcIpPrefix;
167 }
168
169 @Override
170 public IpPrefix dstIpPrefix() {
171 return dstIpPrefix;
172 }
173
174 @Override
175 public VirtualPortId srcPort() {
176 return srcPort;
177 }
178
179 @Override
180 public VirtualPortId dstPort() {
181 return dstPort;
182 }
183
184 @Override
185 public boolean exactMatch(FlowClassifier flowClassifier) {
186 return this.equals(flowClassifier) &&
187 Objects.equals(this.flowClassifierId, flowClassifier.flowClassifierId()) &&
188 Objects.equals(this.tenantId, flowClassifier.tenantId());
189 }
190 }
191
192 /**
193 * Sets up the global values for all the tests.
194 */
195 @Before
196 public void setUpTest() {
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530197 SfcCodecContext context = new SfcCodecContext();
198
199 ServiceDirectory testDirectory = new TestServiceDirectory()
200 .add(FlowClassifierService.class, flowClassifierService)
201 .add(CodecService.class, context.codecManager());
Bharat saraswalf38e2262015-11-18 22:57:05 +0530202 BaseResource.setServiceDirectory(testDirectory);
203
204 }
205
206 /**
207 * Cleans up.
208 */
209 @After
210 public void tearDownTest() {
211 }
212
213 /**
214 * Tests the result of the rest api GET when there are no flow classifiers.
215 */
216 @Test
217 public void testFlowClassifiersEmpty() {
218
219 expect(flowClassifierService.getFlowClassifiers()).andReturn(null).anyTimes();
220 replay(flowClassifierService);
Jian Li9d616492016-03-09 10:52:49 -0800221 final WebTarget wt = target();
222 final String response = wt.path("flow_classifiers").request().get(String.class);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530223 assertThat(response, is("{\"flow_classifiers\":[]}"));
224 }
225
226 /**
227 * Tests the result of a rest api GET for flow classifier id.
228 */
229 @Test
230 public void testGetFlowClassifierId() {
231
232 final Set<FlowClassifier> flowClassifiers = new HashSet<>();
233 flowClassifiers.add(flowClassifier1);
234
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530235 expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530236 expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes();
237 replay(flowClassifierService);
238
Jian Li9d616492016-03-09 10:52:49 -0800239 final WebTarget wt = target();
240 final String response = wt.path("flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051")
241 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800242 final JsonObject result = Json.parse(response).asObject();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530243 assertThat(result, notNullValue());
244 }
245
246 /**
247 * Tests that a fetch of a non-existent flow classifier object throws an exception.
248 */
249 @Test
250 public void testBadGet() {
251 expect(flowClassifierService.getFlowClassifier(anyObject()))
252 .andReturn(null).anyTimes();
253 replay(flowClassifierService);
Jian Li9d616492016-03-09 10:52:49 -0800254 WebTarget wt = target();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530255 try {
Jian Li9d616492016-03-09 10:52:49 -0800256 wt.path("flow_classifiers/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
257 .request().get(String.class);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530258 fail("Fetch of non-existent flow classifier did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800259 } catch (NotFoundException ex) {
Bharat saraswalf38e2262015-11-18 22:57:05 +0530260 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800261 containsString("HTTP 404 Not Found"));
Bharat saraswalf38e2262015-11-18 22:57:05 +0530262 }
263 }
264
265 /**
266 * Tests creating a flow classifier with POST.
267 */
268 @Test
269 public void testPost() {
270
271 expect(flowClassifierService.createFlowClassifier(anyObject()))
272 .andReturn(true).anyTimes();
273 replay(flowClassifierService);
274
Jian Li9d616492016-03-09 10:52:49 -0800275 WebTarget wt = target();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530276 InputStream jsonStream = FlowClassifierResourceTest.class.getResourceAsStream("post-FlowClassifier.json");
277
Jian Li9d616492016-03-09 10:52:49 -0800278 Response response = wt.path("flow_classifiers")
279 .request(MediaType.APPLICATION_JSON_TYPE)
280 .post(Entity.json(jsonStream));
Bharat saraswalf38e2262015-11-18 22:57:05 +0530281 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
282 }
283
284 /**
285 * Tests deleting a flow classifier.
286 */
287 @Test
288 public void testDelete() {
289 expect(flowClassifierService.removeFlowClassifier(anyObject()))
290 .andReturn(true).anyTimes();
291 replay(flowClassifierService);
292
Jian Li9d616492016-03-09 10:52:49 -0800293 WebTarget wt = target();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530294
295 String location = "flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051";
296
Jian Li9d616492016-03-09 10:52:49 -0800297 Response deleteResponse = wt.path(location)
Ray Milkey7c251822016-04-06 17:38:25 -0700298 .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE)
Jian Li9d616492016-03-09 10:52:49 -0800299 .delete();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530300 assertThat(deleteResponse.getStatus(),
301 is(HttpURLConnection.HTTP_NO_CONTENT));
302 }
303}