blob: 32eb8573e38f6a0ca9c44c0e8a7b1b4e70232647 [file] [log] [blame]
Jovana Vuletafe32db7d2017-05-01 12:18:00 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jovana Vuletafe32db7d2017-05-01 12:18:00 +02003 *
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
17package org.onosproject.ofagent.rest;
18
19import com.eclipsesource.json.Json;
20import com.eclipsesource.json.JsonObject;
21import com.google.common.collect.Sets;
22import org.glassfish.jersey.server.ResourceConfig;
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020023import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onlab.osgi.ServiceDirectory;
27import org.onlab.osgi.TestServiceDirectory;
28import org.onlab.packet.IpAddress;
29import org.onlab.packet.TpPort;
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020030import org.onosproject.incubator.net.virtual.NetworkId;
Jovana Vuletac884b692017-11-28 16:52:35 +010031import org.onosproject.incubator.net.virtual.TenantId;
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020032import org.onosproject.ofagent.api.OFAgent;
33import org.onosproject.ofagent.api.OFAgentAdminService;
34import org.onosproject.ofagent.api.OFAgentService;
35import org.onosproject.ofagent.api.OFController;
36import org.onosproject.ofagent.impl.DefaultOFAgent;
37import org.onosproject.ofagent.impl.DefaultOFController;
Jian Li242ce522017-05-23 11:41:41 +090038import org.onosproject.rest.resources.ResourceTest;
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020039
40import javax.ws.rs.client.Entity;
41import javax.ws.rs.client.WebTarget;
42import javax.ws.rs.core.MediaType;
43import javax.ws.rs.core.Response;
44import java.io.IOException;
45import java.io.InputStream;
46import java.net.HttpURLConnection;
47import java.util.Set;
48
Jian Li242ce522017-05-23 11:41:41 +090049import static org.easymock.EasyMock.anyObject;
50import static org.easymock.EasyMock.createMock;
51import static org.easymock.EasyMock.eq;
52import static org.easymock.EasyMock.expect;
53import static org.easymock.EasyMock.expectLastCall;
54import static org.easymock.EasyMock.replay;
55import static org.easymock.EasyMock.verify;
56import static org.hamcrest.Matchers.containsString;
57import static org.hamcrest.Matchers.hasSize;
58import static org.hamcrest.Matchers.is;
59import static org.hamcrest.Matchers.notNullValue;
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020060import static org.junit.Assert.assertNotNull;
61import static org.junit.Assert.assertThat;
62import static org.onosproject.ofagent.api.OFAgent.State.STOPPED;
63
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020064/**
65 * Test class for OFAgent application REST resource.
66 */
Jian Li242ce522017-05-23 11:41:41 +090067public class OFAgentWebResourceTest extends ResourceTest {
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020068
69 private static final Set<OFController> CONTROLLER_SET_1 = Sets.newHashSet(
70 DefaultOFController.of(
71 IpAddress.valueOf("147.91.1.4"),
72 TpPort.tpPort(6633)));
73
74 private static final Set<OFController> CONTROLLER_SET_2 = Sets.newHashSet(
75 DefaultOFController.of(
76 IpAddress.valueOf("147.91.4.25"),
77 TpPort.tpPort(6633)),
78 DefaultOFController.of(
79 IpAddress.valueOf("147.91.4.27"),
80 TpPort.tpPort(6653)));
81
82 private static final Set<OFController> CONTROLLER_SET = Sets.newHashSet(
83 DefaultOFController.of(
84 IpAddress.valueOf("147.91.2.11"),
85 TpPort.tpPort(6633)),
86 DefaultOFController.of(
87 IpAddress.valueOf("147.91.2.9"),
88 TpPort.tpPort(6633)),
89 DefaultOFController.of(
90 IpAddress.valueOf("147.91.2.17"),
91 TpPort.tpPort(6653)));
92
93 private static final NetworkId NETWORK_1 = NetworkId.networkId(1);
94 private static final NetworkId NETWORK_2 = NetworkId.networkId(2);
95 private static final NetworkId NETWORK = NetworkId.networkId(3);
96
Jovana Vuletac884b692017-11-28 16:52:35 +010097
98 private static final TenantId TENANT_1 = TenantId.tenantId("Tenant_1");
99 private static final TenantId TENANT_2 = TenantId.tenantId("Tenant_2");
100 private static final TenantId TENANT = TenantId.tenantId("Tenant");
101
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200102 private static final OFAgent OF_AGENT = DefaultOFAgent.builder()
103 .networkId(NETWORK)
Jovana Vuletac884b692017-11-28 16:52:35 +0100104 .tenantId(TENANT)
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200105 .controllers(CONTROLLER_SET)
106 .state(STOPPED)
107 .build();
108
109 private Set<OFAgent> agents = Sets.newHashSet(DefaultOFAgent.builder()
110 .networkId(NETWORK_1)
Jovana Vuletac884b692017-11-28 16:52:35 +0100111 .tenantId(TENANT_1)
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200112 .controllers(CONTROLLER_SET_1)
113 .state(STOPPED)
114 .build(),
115 DefaultOFAgent.builder()
116 .networkId(NETWORK_2)
Jovana Vuletac884b692017-11-28 16:52:35 +0100117 .tenantId(TENANT_2)
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200118 .controllers(CONTROLLER_SET_2)
119 .state(STOPPED)
120 .build(),
121 OF_AGENT);
122
123 private Set<OFAgent> empty = Sets.newHashSet();
124
125 private final OFAgentAdminService mockOFAgentAdminService = createMock(OFAgentAdminService.class);
126 private final OFAgentService mockOFAgentService = createMock(OFAgentService.class);
127
128 /**
129 * Constructs OFAgent Web application test instance.
130 */
131 public OFAgentWebResourceTest() {
132 super(ResourceConfig.forApplicationClass(OFAgentWebApplication.class));
133 }
134
135 /**
136 * Sets up the global values for all the tests.
137 */
138 @Before
139 public void setUpMocks() {
140 ServiceDirectory testDirectory = new TestServiceDirectory()
141 .add(OFAgentAdminService.class, mockOFAgentAdminService)
142 .add(OFAgentService.class, mockOFAgentService);
Ray Milkey094a1352018-01-22 14:03:54 -0800143 setServiceDirectory(testDirectory);
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200144 }
145
146 /**
147 * Cleans up.
148 */
149 @After
150 public void tearDownMocks() {
151 }
152
153 /**
154 * Tests the result of the rest api GET when there are OFAgents.
155 *
Jian Li242ce522017-05-23 11:41:41 +0900156 * @throws IOException IO exception
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200157 */
158 @Test
159 public void testNonEmptyOFAgentSet() throws IOException {
160 expect(mockOFAgentService.agents()).andReturn(agents).anyTimes();
161 replay(mockOFAgentService);
162
163 final WebTarget wt = target();
164 assertNotNull("WebTarget is null", wt);
165 assertNotNull("WebTarget request is null", wt.request());
166 final String response = wt.path("service/ofagents").request().get(String.class);
167 final JsonObject result = Json.parse(response).asObject();
168 assertThat(result, notNullValue());
169 assertThat(result.names(), hasSize(1));
170 assertThat(result.names().get(0), is("ofAgents"));
171
172 mockOFAgentService.agents().forEach(ofAgent -> {
173
174 String expectedJsonStringNetworkId = "\"networkId\":\"" + ofAgent.networkId().id() + "\"";
175 assertThat(response, containsString(expectedJsonStringNetworkId));
176
Jovana Vuletac884b692017-11-28 16:52:35 +0100177 String expectedJsonStringTenantId = "\"tenantId\":\"" + ofAgent.tenantId().id() + "\"";
178 assertThat(response, containsString(expectedJsonStringTenantId));
179
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200180 String expectedJsonStringState = "\"state\":\"" + ofAgent.state() + "\"";
181 assertThat(response, containsString(expectedJsonStringState));
182
183 ofAgent.controllers().forEach(ofController -> {
184 String expectedJsonStringIP = "\"ip\":\"" + ofController.ip() + "\"";
185 assertThat(response, containsString(expectedJsonStringIP));
186
187 String expectedJsonStringPort = "\"port\":\"" + ofController.port() + "\"";
188 assertThat(response, containsString(expectedJsonStringPort));
189 });
190 });
191
192 verify(mockOFAgentService);
193 }
194
195 /**
196 * Tests the result of the rest api GET when there are no OFAgents.
197 *
Jian Li242ce522017-05-23 11:41:41 +0900198 * @throws IOException IO exception
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200199 */
200 @Test
201 public void testEmptyOFAgentSet() throws IOException {
202 expect(mockOFAgentService.agents()).andReturn(empty).anyTimes();
203 replay(mockOFAgentService);
204
205 final WebTarget wt = target();
206 assertNotNull("WebTarget is null", wt);
207 assertNotNull("WebTarget request is null", wt.request());
208 final String response = wt.path("service/ofagents").request().get(String.class);
209 final JsonObject result = Json.parse(response).asObject();
210 assertThat(result, notNullValue());
211 assertThat(result.names(), hasSize(1));
212 assertThat(response, is("{\"ofAgents\":[]}"));
213
214 verify(mockOFAgentService);
215 }
216
217 /**
218 * Tests the result of the rest api GET for OFAgent.
219 *
Jian Li242ce522017-05-23 11:41:41 +0900220 * @throws IOException IO exception
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200221 */
222 @Test
223 public void testOFAgent() throws IOException {
224 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(OF_AGENT).anyTimes();
225 replay(mockOFAgentService);
226
227 final WebTarget wt = target();
228 assertNotNull("WebTarget is null", wt);
229 assertNotNull("WebTarget request is null", wt.request());
230 final Response response = wt.path("service/ofagent/" + NETWORK).request().get();
231 final JsonObject result = Json.parse(response.readEntity(String.class)).asObject();
232 assertThat(result, notNullValue());
Jovana Vuletac884b692017-11-28 16:52:35 +0100233 assertThat(result.names(), hasSize(4));
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200234 assertThat(result.get("networkId").asString(), is(NETWORK.id().toString()));
Jovana Vuletac884b692017-11-28 16:52:35 +0100235 assertThat(result.get("tenantId").asString(), is(TENANT.id()));
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200236 assertThat(result.get("state").asString(), is(STOPPED.toString()));
237
238 verify(mockOFAgentService);
239 }
240
241
242 /**
243 * Tests the result of the rest api GET for non-existent OFAgent.
244 *
Jian Li242ce522017-05-23 11:41:41 +0900245 * @throws IOException IO exception
Jovana Vuletafe32db7d2017-05-01 12:18:00 +0200246 */
247 @Test
248 public void testNonExistentOFAgent() throws IOException {
249 expect(mockOFAgentService.agent(anyObject())).andReturn(null).anyTimes();
250 replay(mockOFAgentService);
251
252 final WebTarget wt = target();
253 assertNotNull("WebTarget is null", wt);
254 assertNotNull("WebTarget request is null", wt.request());
255 final Response response = wt.path("service/ofagent/" + NETWORK_1).request().get();
256 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
257
258 verify(mockOFAgentService);
259 }
260
261
262 /**
263 * Tests creating an OFAgent with POST.
264 */
265 @Test
266 public void testOFAgentCreate() {
267 mockOFAgentAdminService.createAgent(anyObject());
268 expectLastCall().anyTimes();
269 replay(mockOFAgentAdminService);
270
271
272 InputStream jsonStream = OFAgentWebResourceTest.class
273 .getResourceAsStream("post-ofagent-create.json");
274 assertNotNull("post-ofagent-create.json is null", jsonStream);
275 WebTarget wt = target();
276
277 Response response = wt.path("service/ofagent-create")
278 .request(MediaType.APPLICATION_JSON_TYPE)
279 .post(Entity.json(jsonStream));
280 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
281
282 verify(mockOFAgentAdminService);
283 }
284
285 /**
286 * Tests creating an OFAgent with bad POST request.
287 */
288 @Test
289 public void testOFAgentCreateBadRequest() {
290 InputStream jsonStream = OFAgentWebResourceTest.class
291 .getResourceAsStream("post-bad-request.json");
292 assertNotNull("post-bad-request.json is null", jsonStream);
293 WebTarget wt = target();
294
295 Response response = wt.path("service/ofagent-create")
296 .request(MediaType.APPLICATION_JSON_TYPE)
297 .post(Entity.json(jsonStream));
298 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_INTERNAL_ERROR));
299 }
300
301 /**
302 * Tests updating an OFAgent with PUT.
303 */
304 @Test
305 public void testOFAgentUpdate() {
306 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(OF_AGENT).anyTimes();
307 replay(mockOFAgentService);
308
309 mockOFAgentAdminService.updateAgent(anyObject());
310 expectLastCall().anyTimes();
311 replay(mockOFAgentAdminService);
312
313 InputStream jsonStream = OFAgentWebResourceTest.class
314 .getResourceAsStream("put-ofagent-update.json");
315 assertNotNull("put-ofagent-update.json is null", jsonStream);
316 WebTarget wt = target();
317 Response response = wt.path("service/ofagent-update")
318 .request(MediaType.APPLICATION_JSON_TYPE)
319 .put(Entity.json(jsonStream));
320 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
321 assertThat(response.readEntity(String.class), containsString("OFAgent updated"));
322
323 verify(mockOFAgentService);
324 verify(mockOFAgentAdminService);
325
326 }
327
328 /**
329 * Tests non-existent OFAgent updating with PUT.
330 */
331 @Test
332 public void testNonExistentOFAgentUpdate() {
333 expect(mockOFAgentService.agent(anyObject())).andReturn(null).anyTimes();
334 replay(mockOFAgentService);
335
336 InputStream jsonStream = OFAgentWebResourceTest.class
337 .getResourceAsStream("put-non-existent-ofagent-update.json");
338 assertNotNull("put-non-existent-ofagent-update.json is null", jsonStream);
339 WebTarget wt = target();
340 Response response = wt.path("service/ofagent-update")
341 .request(MediaType.APPLICATION_JSON_TYPE)
342 .put(Entity.json(jsonStream));
343 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
344
345 verify(mockOFAgentService);
346
347 }
348
349 /**
350 * Tests OFAgent updating with bad PUT request.
351 */
352 @Test
353 public void testOFAgentUpdateBadRequest() {
354 expect(mockOFAgentService.agent(anyObject())).andReturn(null).anyTimes();
355 replay(mockOFAgentService);
356
357 InputStream jsonStream = OFAgentWebResourceTest.class
358 .getResourceAsStream("put-bad-request.json");
359 assertNotNull("put-bad-request.json is null", jsonStream);
360 WebTarget wt = target();
361 Response response = wt.path("service/ofagent-update")
362 .request(MediaType.APPLICATION_JSON_TYPE)
363 .put(Entity.json(jsonStream));
364 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_INTERNAL_ERROR));
365
366 verify(mockOFAgentService);
367 }
368
369 /**
370 * Tests starting an OFAgent with POST.
371 */
372 @Test
373 public void testOFAgentStart() {
374 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(OF_AGENT).anyTimes();
375 replay(mockOFAgentService);
376
377 mockOFAgentAdminService.startAgent(anyObject());
378 expectLastCall().anyTimes();
379 replay(mockOFAgentAdminService);
380
381 InputStream jsonStream = OFAgentWebResourceTest.class
382 .getResourceAsStream("post-ofagent-start.json");
383 assertNotNull("post-ofagent-create.json is null", jsonStream);
384 WebTarget wt = target();
385
386 Response response = wt.path("service/ofagent-start")
387 .request(MediaType.APPLICATION_JSON_TYPE)
388 .post(Entity.json(jsonStream));
389 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
390// assertThat(response.readEntity(String.class), containsString("OFAgent started"));
391 assertThat(response.readEntity(String.class), is("OFAgent started"));
392
393 verify(mockOFAgentService);
394 verify(mockOFAgentAdminService);
395 }
396
397 /**
398 * Tests non-existent OFAgent starting with POST.
399 */
400 @Test
401 public void testNonExistentOFAgentStart() {
402 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(null).anyTimes();
403 replay(mockOFAgentService);
404
405 InputStream jsonStream = OFAgentWebResourceTest.class
406 .getResourceAsStream("post-non-existent-ofagent-start.json");
407 assertNotNull("post-non-existent-ofagent-start.json is null", jsonStream);
408 WebTarget wt = target();
409
410 Response response = wt.path("service/ofagent-start")
411 .request(MediaType.APPLICATION_JSON_TYPE)
412 .post(Entity.json(jsonStream));
413 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
414
415 verify(mockOFAgentService);
416 }
417
418 /**
419 * Tests OFAgent starting with bad POST request.
420 */
421 @Test
422 public void testOFAgentStartBadRequest() {
423
424 InputStream jsonStream = OFAgentWebResourceTest.class
425 .getResourceAsStream("post-bad-request.json");
426 assertNotNull("post-bad-request.json is null", jsonStream);
427 WebTarget wt = target();
428
429 Response response = wt.path("service/ofagent-start")
430 .request(MediaType.APPLICATION_JSON_TYPE)
431 .post(Entity.json(jsonStream));
432 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_BAD_REQUEST));
433
434 }
435
436 /**
437 * Tests stopping an OFAgent with POST.
438 */
439 @Test
440 public void testOFAgentStop() {
441 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(OF_AGENT).anyTimes();
442 replay(mockOFAgentService);
443
444 mockOFAgentAdminService.stopAgent(anyObject());
445 expectLastCall().anyTimes();
446 replay(mockOFAgentAdminService);
447
448 InputStream jsonStream = OFAgentWebResourceTest.class
449 .getResourceAsStream("post-ofagent-stop.json");
450 assertNotNull("post-ofagent-stop.json is null", jsonStream);
451 WebTarget wt = target();
452 Response response = wt.path("service/ofagent-stop")
453 .request(MediaType.APPLICATION_JSON_TYPE)
454 .post(Entity.json(jsonStream));
455 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT));
456
457 verify(mockOFAgentService);
458 verify(mockOFAgentAdminService);
459 }
460
461 /**
462 * Tests stopping non-existent OFAgent with POST.
463 */
464 @Test
465 public void testNonExistentOFAgentStop() {
466 expect(mockOFAgentService.agent(NETWORK)).andReturn(null).anyTimes();
467 replay(mockOFAgentService);
468
469 InputStream jsonStream = OFAgentWebResourceTest.class
470 .getResourceAsStream("post-non-existent-ofagent-stop.json");
471 assertNotNull("post-non-existent-ofagent-stop.json is null", jsonStream);
472 WebTarget wt = target();
473
474 Response response = wt.path("service/ofagent-stop")
475 .request(MediaType.APPLICATION_JSON_TYPE)
476 .post(Entity.json(jsonStream));
477 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
478
479 verify(mockOFAgentService);
480 }
481
482 /**
483 * Tests stopping FAgent with bad POST request.
484 */
485 @Test
486 public void testOFAgentStopBadRequest() {
487 InputStream jsonStream = OFAgentWebResourceTest.class
488 .getResourceAsStream("post-bad-request.json");
489 assertNotNull("post-bad-request.json is null", jsonStream);
490 WebTarget wt = target();
491
492 Response response = wt.path("service/ofagent-stop")
493 .request(MediaType.APPLICATION_JSON_TYPE)
494 .post(Entity.json(jsonStream));
495 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_INTERNAL_ERROR));
496 }
497
498
499 /**
500 * Tests deleting an OFAgent with DELETE.
501 */
502 @Test
503 public void testOFAgentRemove() {
504 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(OF_AGENT).anyTimes();
505 replay(mockOFAgentService);
506
507 expect(mockOFAgentAdminService.removeAgent(NETWORK)).andReturn(OF_AGENT).anyTimes();
508 replay(mockOFAgentAdminService);
509
510 WebTarget wt = target();
511 Response response = wt.path("service/ofagent-remove/" + NETWORK.toString())
512 .request(MediaType.APPLICATION_JSON_TYPE)
513 .delete();
514 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
515 final JsonObject result = Json.parse(response.readEntity(String.class)).asObject();
516 assertThat(result.get("networkId").asString(), is(NETWORK.id().toString()));
517 assertThat(result.get("state").asString(), is(STOPPED.toString()));
518
519 verify(mockOFAgentService);
520 verify(mockOFAgentAdminService);
521 }
522
523 /**
524 * Tests deleting a non-existent OFAgent with DELETE.
525 */
526 @Test
527 public void testNonExistentOFAgentRemove() {
528 expect(mockOFAgentService.agent(eq(NETWORK))).andReturn(null).anyTimes();
529 replay(mockOFAgentService);
530
531 expect(mockOFAgentAdminService.removeAgent(NETWORK)).andReturn(null).anyTimes();
532 replay(mockOFAgentAdminService);
533
534 WebTarget wt = target();
535 Response response = wt.path("service/ofagent-remove/" + NETWORK.toString())
536 .request(MediaType.APPLICATION_JSON_TYPE)
537 .delete();
538 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_BAD_REQUEST));
539 assertThat(response.readEntity(String.class), containsString("OFAgent not found"));
540
541 verify(mockOFAgentService);
542 verify(mockOFAgentAdminService);
543 }
544}