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 {
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.
*
* @param device device to make the request to
......
......@@ -48,6 +48,16 @@
<artifactId>jersey-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-osgi</artifactId>
<version>4.4.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
......
......@@ -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;
......@@ -167,6 +168,31 @@ public class RestSBControllerImpl implements RestSBController {
}
@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);
ClientResponse response = null;
......@@ -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;
}
}
}
......
/*
* 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
......@@ -25,6 +25,8 @@
<bundle>mvn:com.sun.jersey/jersey-client/1.19</bundle>
<bundle>mvn:commons-io/commons-io/2.4</bundle>
<bundle>mvn:org.apache.httpcomponents/httpclient-osgi/4.5.1</bundle>
<bundle>mvn:org.apache.httpcomponents/httpcore-osgi/4.4.4</bundle>
</feature>
</features>
......