Andrea Campanella
Committed by Ray Milkey

ONOS-3795 PATCH method in RestSbController and basic unit tests

Change-Id: I35dc31ab03fc72c11523b2c60f4455d7446a5364
...@@ -99,6 +99,17 @@ public interface RestSBController { ...@@ -99,6 +99,17 @@ public interface RestSBController {
99 InputStream get(DeviceId device, String request, String mediaType); 99 InputStream get(DeviceId device, String request, String mediaType);
100 100
101 /** 101 /**
102 + * Does a REST PATCH request with specified parameters to the device.
103 + *
104 + * @param device device to make the request to
105 + * @param request url of the request
106 + * @param payload payload of the request as an InputStream
107 + * @param mediaType format to retrieve the content in
108 + * @return true if operation returned 200, 201, 202, false otherwise
109 + */
110 + boolean patch(DeviceId device, String request, InputStream payload, String mediaType);
111 +
112 + /**
102 * Does a REST DELETE request with specified parameters to the device. 113 * Does a REST DELETE request with specified parameters to the device.
103 * 114 *
104 * @param device device to make the request to 115 * @param device device to make the request to
......
...@@ -48,6 +48,16 @@ ...@@ -48,6 +48,16 @@
48 <artifactId>jersey-client</artifactId> 48 <artifactId>jersey-client</artifactId>
49 </dependency> 49 </dependency>
50 <dependency> 50 <dependency>
51 + <groupId>org.apache.httpcomponents</groupId>
52 + <artifactId>httpclient-osgi</artifactId>
53 + <version>4.5.1</version>
54 + </dependency>
55 + <dependency>
56 + <groupId>org.apache.httpcomponents</groupId>
57 + <artifactId>httpcore-osgi</artifactId>
58 + <version>4.4.4</version>
59 + </dependency>
60 + <dependency>
51 <groupId>commons-io</groupId> 61 <groupId>commons-io</groupId>
52 <artifactId>commons-io</artifactId> 62 <artifactId>commons-io</artifactId>
53 <version>2.4</version> 63 <version>2.4</version>
......
...@@ -24,11 +24,13 @@ import org.apache.felix.scr.annotations.Activate; ...@@ -24,11 +24,13 @@ import org.apache.felix.scr.annotations.Activate;
24 import org.apache.felix.scr.annotations.Component; 24 import org.apache.felix.scr.annotations.Component;
25 import org.apache.felix.scr.annotations.Deactivate; 25 import org.apache.felix.scr.annotations.Deactivate;
26 import org.apache.felix.scr.annotations.Service; 26 import org.apache.felix.scr.annotations.Service;
27 +import org.apache.http.client.methods.HttpPatch;
28 +import org.apache.http.entity.StringEntity;
29 +import org.apache.http.impl.client.HttpClients;
27 import org.onlab.packet.IpAddress; 30 import org.onlab.packet.IpAddress;
28 import org.onosproject.net.DeviceId; 31 import org.onosproject.net.DeviceId;
29 import org.onosproject.protocol.rest.RestSBController; 32 import org.onosproject.protocol.rest.RestSBController;
30 import org.onosproject.protocol.rest.RestSBDevice; 33 import org.onosproject.protocol.rest.RestSBDevice;
31 -import org.osgi.service.component.ComponentContext;
32 import org.slf4j.Logger; 34 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory; 35 import org.slf4j.LoggerFactory;
34 36
...@@ -50,7 +52,6 @@ public class RestSBControllerImpl implements RestSBController { ...@@ -50,7 +52,6 @@ public class RestSBControllerImpl implements RestSBController {
50 52
51 private static final Logger log = 53 private static final Logger log =
52 LoggerFactory.getLogger(RestSBControllerImpl.class); 54 LoggerFactory.getLogger(RestSBControllerImpl.class);
53 - private static final String APPLICATION = "application/";
54 private static final String XML = "xml"; 55 private static final String XML = "xml";
55 private static final String JSON = "json"; 56 private static final String JSON = "json";
56 private static final String DOUBLESLASH = "//"; 57 private static final String DOUBLESLASH = "//";
...@@ -64,7 +65,7 @@ public class RestSBControllerImpl implements RestSBController { ...@@ -64,7 +65,7 @@ public class RestSBControllerImpl implements RestSBController {
64 Client client; 65 Client client;
65 66
66 @Activate 67 @Activate
67 - public void activate(ComponentContext context) { 68 + public void activate() {
68 client = Client.create(); 69 client = Client.create();
69 log.info("Started"); 70 log.info("Started");
70 } 71 }
...@@ -87,10 +88,10 @@ public class RestSBControllerImpl implements RestSBController { ...@@ -87,10 +88,10 @@ public class RestSBControllerImpl implements RestSBController {
87 88
88 @Override 89 @Override
89 public RestSBDevice getDevice(IpAddress ip, int port) { 90 public RestSBDevice getDevice(IpAddress ip, int port) {
90 - for (DeviceId info : deviceMap.keySet()) { 91 + for (RestSBDevice device : deviceMap.values()) {
91 - if (IpAddress.valueOf(info.uri().getHost()).equals(ip) && 92 + if (device.ip().equals(ip) &&
92 - info.uri().getPort() == port) { 93 + device.port() == port) {
93 - return deviceMap.get(info); 94 + return device;
94 } 95 }
95 } 96 }
96 return null; 97 return null;
...@@ -167,6 +168,31 @@ public class RestSBControllerImpl implements RestSBController { ...@@ -167,6 +168,31 @@ public class RestSBControllerImpl implements RestSBController {
167 } 168 }
168 169
169 @Override 170 @Override
171 + public boolean patch(DeviceId device, String request, InputStream payload, String mediaType) {
172 + String url = deviceMap.get(device).protocol() + COLON +
173 + DOUBLESLASH +
174 + deviceMap.get(device).ip().toString() +
175 + COLON + deviceMap.get(device).port() +
176 + SLASH + request;
177 + try {
178 + HttpPatch httprequest = new HttpPatch(url);
179 + if (payload != null) {
180 + StringEntity input = new StringEntity(IOUtils.toString(payload, StandardCharsets.UTF_8));
181 + input.setContentType(mediaType);
182 + httprequest.setEntity(input);
183 + }
184 + int responseStatusCode = HttpClients.createDefault().execute(httprequest)
185 + .getStatusLine()
186 + .getStatusCode();
187 + return checkStatusCode(responseStatusCode);
188 + } catch (IOException e) {
189 + log.error("Cannot do PATCH {} request on device {} because can't read payload",
190 + request, device);
191 + }
192 + return false;
193 + }
194 +
195 + @Override
170 public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) { 196 public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) {
171 WebResource webResource = getWebResource(device, request); 197 WebResource webResource = getWebResource(device, request);
172 ClientResponse response = null; 198 ClientResponse response = null;
...@@ -195,17 +221,21 @@ public class RestSBControllerImpl implements RestSBController { ...@@ -195,17 +221,21 @@ public class RestSBControllerImpl implements RestSBController {
195 221
196 private boolean checkReply(ClientResponse response) { 222 private boolean checkReply(ClientResponse response) {
197 if (response != null) { 223 if (response != null) {
198 - if (response.getStatus() == STATUS_OK || 224 + return checkStatusCode(response.getStatus());
199 - response.getStatus() == STATUS_CREATED ||
200 - response.getStatus() == STATUS_ACCEPTED) {
201 - return true;
202 - } else {
203 - log.error("Failed request: HTTP error code : "
204 - + response.getStatus());
205 - return false;
206 - }
207 } 225 }
208 log.error("Null reply from device"); 226 log.error("Null reply from device");
209 return false; 227 return false;
210 } 228 }
229 +
230 + private boolean checkStatusCode(int statusCode) {
231 + if (statusCode == STATUS_OK ||
232 + statusCode == STATUS_CREATED ||
233 + statusCode == STATUS_ACCEPTED) {
234 + return true;
235 + } else {
236 + log.error("Failed request: HTTP error code : "
237 + + statusCode);
238 + return false;
239 + }
240 + }
211 } 241 }
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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 +
17 +package org.onosproject.protocol.rest.ctl;
18 +
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +import org.onlab.packet.IpAddress;
22 +import org.onosproject.protocol.rest.DefaultRestSBDevice;
23 +import org.onosproject.protocol.rest.RestSBDevice;
24 +
25 +import static org.junit.Assert.*;
26 +
27 +/**
28 + * Basic testing for RestSBController.
29 + */
30 +public class RestSBControllerImplTest {
31 +
32 + RestSBControllerImpl controller;
33 +
34 + RestSBDevice device1;
35 + RestSBDevice device2;
36 +
37 +
38 + @Before
39 + public void setUp() {
40 + controller = new RestSBControllerImpl();
41 + controller.activate();
42 + device1 = new DefaultRestSBDevice(IpAddress.valueOf("127.0.0.1"), 8080, "foo", "bar", "http", true);
43 + device2 = new DefaultRestSBDevice(IpAddress.valueOf("127.0.0.2"), 8080, "foo1", "bar2", "http", true);
44 + controller.addDevice(device1);
45 + }
46 +
47 + @Test
48 + public void basics() {
49 + assertTrue("Device1 non added", controller.getDevices().containsValue(device1));
50 + assertEquals("Device1 added but with wrong key", controller.getDevices()
51 + .get(device1.deviceId()), device1);
52 + assertEquals("Incorrect Get Device by ID", controller.getDevice(device1.deviceId()), device1);
53 + assertEquals("Incorrect Get Device by IP, Port", controller.getDevice(device1.ip(), device1.port()), device1);
54 + controller.addDevice(device2);
55 + assertTrue("Device2 non added", controller.getDevices().containsValue(device2));
56 + controller.removeDevice(device2);
57 + assertFalse("Device2 not removed", controller.getDevices().containsValue(device2));
58 + }
59 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
25 25
26 <bundle>mvn:com.sun.jersey/jersey-client/1.19</bundle> 26 <bundle>mvn:com.sun.jersey/jersey-client/1.19</bundle>
27 <bundle>mvn:commons-io/commons-io/2.4</bundle> 27 <bundle>mvn:commons-io/commons-io/2.4</bundle>
28 + <bundle>mvn:org.apache.httpcomponents/httpclient-osgi/4.5.1</bundle>
29 + <bundle>mvn:org.apache.httpcomponents/httpcore-osgi/4.4.4</bundle>
28 </feature> 30 </feature>
29 </features> 31 </features>
30 32
......