From ce279eeafba29abb28ef1be39e1cc5d28ea049b0 Mon Sep 17 00:00:00 2001 From: Andrea Campanella Date: Mon, 25 Jan 2016 10:21:45 -0800 Subject: [PATCH] ONOS-3795 PATCH method in RestSbController and basic unit tests Change-Id: I35dc31ab03fc72c11523b2c60f4455d7446a5364 --- .../protocol/rest/RestSBController.java | 11 ++++ protocols/rest/ctl/pom.xml | 10 +++ .../rest/ctl/RestSBControllerImpl.java | 62 ++++++++++++++----- .../rest/ctl/RestSBControllerImplTest.java | 59 ++++++++++++++++++ providers/rest/app/features.xml | 2 + 5 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 protocols/rest/ctl/src/test/java/org/onosproject/protocol/rest/ctl/RestSBControllerImplTest.java diff --git a/protocols/rest/api/src/main/java/org/onosproject/protocol/rest/RestSBController.java b/protocols/rest/api/src/main/java/org/onosproject/protocol/rest/RestSBController.java index 28d5617c9d..bfc0f9ee83 100644 --- a/protocols/rest/api/src/main/java/org/onosproject/protocol/rest/RestSBController.java +++ b/protocols/rest/api/src/main/java/org/onosproject/protocol/rest/RestSBController.java @@ -98,6 +98,17 @@ public interface RestSBController { */ InputStream get(DeviceId device, String request, String mediaType); + /** + * Does a REST PATCH request with specified parameters to the device. + * + * @param device device to make the request to + * @param request url of the request + * @param payload payload of the request as an InputStream + * @param mediaType format to retrieve the content in + * @return true if operation returned 200, 201, 202, false otherwise + */ + boolean patch(DeviceId device, String request, InputStream payload, String mediaType); + /** * Does a REST DELETE request with specified parameters to the device. * diff --git a/protocols/rest/ctl/pom.xml b/protocols/rest/ctl/pom.xml index bce280e39f..d417fff1e7 100644 --- a/protocols/rest/ctl/pom.xml +++ b/protocols/rest/ctl/pom.xml @@ -47,6 +47,16 @@ com.sun.jersey jersey-client + + org.apache.httpcomponents + httpclient-osgi + 4.5.1 + + + org.apache.httpcomponents + httpcore-osgi + 4.4.4 + commons-io commons-io diff --git a/protocols/rest/ctl/src/main/java/org/onosproject/protocol/rest/ctl/RestSBControllerImpl.java b/protocols/rest/ctl/src/main/java/org/onosproject/protocol/rest/ctl/RestSBControllerImpl.java index b659d2f9df..0bc35457e9 100644 --- a/protocols/rest/ctl/src/main/java/org/onosproject/protocol/rest/ctl/RestSBControllerImpl.java +++ b/protocols/rest/ctl/src/main/java/org/onosproject/protocol/rest/ctl/RestSBControllerImpl.java @@ -24,11 +24,13 @@ import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Deactivate; import org.apache.felix.scr.annotations.Service; +import org.apache.http.client.methods.HttpPatch; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.HttpClients; import org.onlab.packet.IpAddress; import org.onosproject.net.DeviceId; import org.onosproject.protocol.rest.RestSBController; import org.onosproject.protocol.rest.RestSBDevice; -import org.osgi.service.component.ComponentContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,7 +52,6 @@ public class RestSBControllerImpl implements RestSBController { private static final Logger log = LoggerFactory.getLogger(RestSBControllerImpl.class); - private static final String APPLICATION = "application/"; private static final String XML = "xml"; private static final String JSON = "json"; private static final String DOUBLESLASH = "//"; @@ -64,7 +65,7 @@ public class RestSBControllerImpl implements RestSBController { Client client; @Activate - public void activate(ComponentContext context) { + public void activate() { client = Client.create(); log.info("Started"); } @@ -87,10 +88,10 @@ public class RestSBControllerImpl implements RestSBController { @Override public RestSBDevice getDevice(IpAddress ip, int port) { - for (DeviceId info : deviceMap.keySet()) { - if (IpAddress.valueOf(info.uri().getHost()).equals(ip) && - info.uri().getPort() == port) { - return deviceMap.get(info); + for (RestSBDevice device : deviceMap.values()) { + if (device.ip().equals(ip) && + device.port() == port) { + return device; } } return null; @@ -166,6 +167,31 @@ public class RestSBControllerImpl implements RestSBController { .getBytes(StandardCharsets.UTF_8)); } + @Override + public boolean patch(DeviceId device, String request, InputStream payload, String mediaType) { + String url = deviceMap.get(device).protocol() + COLON + + DOUBLESLASH + + deviceMap.get(device).ip().toString() + + COLON + deviceMap.get(device).port() + + SLASH + request; + try { + HttpPatch httprequest = new HttpPatch(url); + if (payload != null) { + StringEntity input = new StringEntity(IOUtils.toString(payload, StandardCharsets.UTF_8)); + input.setContentType(mediaType); + httprequest.setEntity(input); + } + int responseStatusCode = HttpClients.createDefault().execute(httprequest) + .getStatusLine() + .getStatusCode(); + return checkStatusCode(responseStatusCode); + } catch (IOException e) { + log.error("Cannot do PATCH {} request on device {} because can't read payload", + request, device); + } + return false; + } + @Override public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) { WebResource webResource = getWebResource(device, request); @@ -195,17 +221,21 @@ public class RestSBControllerImpl implements RestSBController { private boolean checkReply(ClientResponse response) { if (response != null) { - if (response.getStatus() == STATUS_OK || - response.getStatus() == STATUS_CREATED || - response.getStatus() == STATUS_ACCEPTED) { - return true; - } else { - log.error("Failed request: HTTP error code : " - + response.getStatus()); - return false; - } + return checkStatusCode(response.getStatus()); } log.error("Null reply from device"); return false; } + + private boolean checkStatusCode(int statusCode) { + if (statusCode == STATUS_OK || + statusCode == STATUS_CREATED || + statusCode == STATUS_ACCEPTED) { + return true; + } else { + log.error("Failed request: HTTP error code : " + + statusCode); + return false; + } + } } diff --git a/protocols/rest/ctl/src/test/java/org/onosproject/protocol/rest/ctl/RestSBControllerImplTest.java b/protocols/rest/ctl/src/test/java/org/onosproject/protocol/rest/ctl/RestSBControllerImplTest.java new file mode 100644 index 0000000000..ab01cac779 --- /dev/null +++ b/protocols/rest/ctl/src/test/java/org/onosproject/protocol/rest/ctl/RestSBControllerImplTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onosproject.protocol.rest.ctl; + +import org.junit.Before; +import org.junit.Test; +import org.onlab.packet.IpAddress; +import org.onosproject.protocol.rest.DefaultRestSBDevice; +import org.onosproject.protocol.rest.RestSBDevice; + +import static org.junit.Assert.*; + +/** + * Basic testing for RestSBController. + */ +public class RestSBControllerImplTest { + + RestSBControllerImpl controller; + + RestSBDevice device1; + RestSBDevice device2; + + + @Before + public void setUp() { + controller = new RestSBControllerImpl(); + controller.activate(); + device1 = new DefaultRestSBDevice(IpAddress.valueOf("127.0.0.1"), 8080, "foo", "bar", "http", true); + device2 = new DefaultRestSBDevice(IpAddress.valueOf("127.0.0.2"), 8080, "foo1", "bar2", "http", true); + controller.addDevice(device1); + } + + @Test + public void basics() { + assertTrue("Device1 non added", controller.getDevices().containsValue(device1)); + assertEquals("Device1 added but with wrong key", controller.getDevices() + .get(device1.deviceId()), device1); + assertEquals("Incorrect Get Device by ID", controller.getDevice(device1.deviceId()), device1); + assertEquals("Incorrect Get Device by IP, Port", controller.getDevice(device1.ip(), device1.port()), device1); + controller.addDevice(device2); + assertTrue("Device2 non added", controller.getDevices().containsValue(device2)); + controller.removeDevice(device2); + assertFalse("Device2 not removed", controller.getDevices().containsValue(device2)); + } +} \ No newline at end of file diff --git a/providers/rest/app/features.xml b/providers/rest/app/features.xml index 168ae55a19..db9f5eeded 100644 --- a/providers/rest/app/features.xml +++ b/providers/rest/app/features.xml @@ -25,6 +25,8 @@ mvn:com.sun.jersey/jersey-client/1.19 mvn:commons-io/commons-io/2.4 + mvn:org.apache.httpcomponents/httpclient-osgi/4.5.1 + mvn:org.apache.httpcomponents/httpcore-osgi/4.4.4