ONOS-3795 PATCH method in RestSbController and basic unit tests
Change-Id: I35dc31ab03fc72c11523b2c60f4455d7446a5364
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 b659d2f..0bc3545 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.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 @@
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 @@
Client client;
@Activate
- public void activate(ComponentContext context) {
+ public void activate() {
client = Client.create();
log.info("Started");
}
@@ -87,10 +88,10 @@
@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 @@
}
@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 @@
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 0000000..ab01cac
--- /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