REST protocol: changing returns for HTTP methods from boolean to int (HTTP standard codes)

Change-Id: I8bbdf1e61cc9f5983e03329327b7ae756372c5fe
diff --git a/drivers/ciena/BUCK b/drivers/ciena/BUCK
index 5eba907..704e96e 100644
--- a/drivers/ciena/BUCK
+++ b/drivers/ciena/BUCK
@@ -5,6 +5,7 @@
     '//drivers/utilities:onos-drivers-utilities',
     '//protocols/rest/api:onos-protocols-rest-api',
     '//apps/optical-model:onos-apps-optical-model',
+    '//lib:javax.ws.rs-api',
 ]
 
 TEST_DEPS = [
diff --git a/protocols/rest/api/src/main/java/org/onosproject/protocol/http/HttpSBController.java b/protocols/rest/api/src/main/java/org/onosproject/protocol/http/HttpSBController.java
index 1694d2a..f5835dc 100644
--- a/protocols/rest/api/src/main/java/org/onosproject/protocol/http/HttpSBController.java
+++ b/protocols/rest/api/src/main/java/org/onosproject/protocol/http/HttpSBController.java
@@ -16,13 +16,15 @@
 
 package org.onosproject.protocol.http;
 
+import java.io.InputStream;
+import java.util.Map;
+
+import javax.ws.rs.core.MediaType;
+
 import org.onlab.packet.IpAddress;
 import org.onosproject.net.DeviceId;
 import org.onosproject.protocol.rest.RestSBDevice;
 
-import java.io.InputStream;
-import java.util.Map;
-
 /**
  * Abstraction of an HTTP controller. Serves as a one stop shop for obtaining
  * HTTP southbound devices and (un)register listeners.
@@ -47,7 +49,7 @@
     /**
      * Returns a device by Ip and Port.
      *
-     * @param ip   device ip
+     * @param ip device ip
      * @param port device port
      * @return RestSBDevice rest device
      */
@@ -70,70 +72,154 @@
     /**
      * Does a HTTP POST 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 device device to make the request to
+     * @param request url of the request
+     * @param payload payload of the request as an InputStream
      * @param mediaType type of content in the payload i.e. application/json
      * @return true if operation returned 200, 201, 202, false otherwise
+     *
+     * @deprecated in Kingfisher (1.10.0)
      */
+    @Deprecated
     boolean post(DeviceId device, String request, InputStream payload, String mediaType);
 
     /**
      * Does a HTTP POST request with specified parameters to the device.
      *
-     * @param <T>           post return type
-     * @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     type of content in the payload i.e. application/json
+     * @param <T> post return type
+     * @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 type of content in the payload i.e. application/json
      * @param responseClass the type of response object we are interested in,
-     *                      such as String, InputStream.
+     *            such as String, InputStream.
      * @return Object of type requested via responseClass.
      */
-    <T> T post(DeviceId device, String request, InputStream payload,
-               String mediaType, Class<T> responseClass);
+    @Deprecated
+    <T> T post(DeviceId device, String request, InputStream payload, String mediaType, Class<T> responseClass);
 
     /**
      * Does a HTTP PUT request with specified parameters to the device.
      *
-     * @param device    device to make the request to
-     * @param request   resource path of the request
-     * @param payload   payload of the request as an InputStream
+     * @param device device to make the request to
+     * @param request resource path of the request
+     * @param payload payload of the request as an InputStream
      * @param mediaType type of content in the payload i.e. application/json
      * @return true if operation returned 200, 201, 202, false otherwise
+     *
+     * @deprecated in Kingfisher (1.10.0)
      */
+    @Deprecated
     boolean put(DeviceId device, String request, InputStream payload, String mediaType);
 
     /**
+     *
      * Does a HTTP GET request with specified parameters to the device.
      *
-     * @param device    device to make the request to
-     * @param request   url of the request
+     * @param device device to make the request to
+     * @param request url of the request
      * @param mediaType format to retrieve the content in
      * @return an inputstream of data from the reply.
      */
+    @Deprecated
     InputStream get(DeviceId device, String request, String mediaType);
 
     /**
      * Does a HTTP 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 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
+     *
+     * @deprecated in Kingfisher (1.10.0)
      */
+    @Deprecated
     boolean patch(DeviceId device, String request, InputStream payload, String mediaType);
 
     /**
      * Does a HTTP DELETE 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 device device to make the request to
+     * @param request url of the request
+     * @param payload payload of the request as an InputStream
      * @param mediaType type of content in the payload i.e. application/json
      * @return true if operation returned 200 false otherwise
+     *
+     * @deprecated in Kingfisher (1.10.0)
      */
+    @Deprecated
     boolean delete(DeviceId device, String request, InputStream payload, String mediaType);
 
+    /**
+     * Does a HTTP POST 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 type of content in the payload i.e. application/json
+     * @return status Commonly used status codes defined by HTTP
+     */
+    int post(DeviceId device, String request, InputStream payload, MediaType mediaType);
+
+    /**
+     * Does a HTTP PUT request with specified parameters to the device.
+     *
+     * @param device device to make the request to
+     * @param request resource path of the request
+     * @param payload payload of the request as an InputStream
+     * @param mediaType type of content in the payload i.e. application/json
+     * @return status Commonly used status codes defined by HTTP
+     */
+    int put(DeviceId device, String request, InputStream payload, MediaType mediaType);
+
+    /**
+     * Does a HTTP 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 status Commonly used status codes defined by HTTP
+     */
+    int patch(DeviceId device, String request, InputStream payload, MediaType mediaType);
+
+    /**
+     * Does a HTTP DELETE 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 type of content in the payload i.e. application/json
+     * @return status Commonly used status codes defined by HTTP
+     */
+    int delete(DeviceId device, String request, InputStream payload, MediaType mediaType);
+
+    /**
+    *
+    * Does a HTTP GET request with specified parameters to the device.
+    *
+    * @param device device to make the request to
+    * @param request url of the request
+    * @param mediaType format to retrieve the content in
+    * @return an inputstream of data from the reply.
+    */
+    InputStream get(DeviceId device, String request, MediaType mediaType);
+
+    /**
+     * Does a HTTP POST request with specified parameters to the device.
+     *
+     * @param <T> post return type
+     * @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 type of content in the payload i.e. application/json
+     * @param responseClass the type of response object we are interested in,
+     *            such as String, InputStream.
+     * @return Object of type requested via responseClass.
+     */
+     <T> T post(DeviceId device, String request, InputStream payload, MediaType mediaType, Class<T> responseClass);
+
+
 }
diff --git a/protocols/rest/api/src/main/java/org/onosproject/protocol/http/ctl/HttpSBControllerImpl.java b/protocols/rest/api/src/main/java/org/onosproject/protocol/http/ctl/HttpSBControllerImpl.java
index b162b56..cf34a40 100644
--- a/protocols/rest/api/src/main/java/org/onosproject/protocol/http/ctl/HttpSBControllerImpl.java
+++ b/protocols/rest/api/src/main/java/org/onosproject/protocol/http/ctl/HttpSBControllerImpl.java
@@ -16,7 +16,30 @@
 
 package org.onosproject.protocol.http.ctl;
 
-import com.google.common.collect.ImmutableMap;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyManagementException;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+import java.util.Base64;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+
 import org.apache.commons.io.IOUtils;
 import org.apache.http.client.methods.HttpPatch;
 import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
@@ -32,35 +55,14 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.TrustManager;
-import javax.net.ssl.X509TrustManager;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.WebTarget;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-import java.security.KeyManagementException;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
-import java.security.cert.CertificateException;
-import java.security.cert.X509Certificate;
-import java.util.Base64;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
+import com.google.common.collect.ImmutableMap;
 
 /**
  * The implementation of HttpSBController.
  */
 public class HttpSBControllerImpl implements HttpSBController {
 
-    private static final Logger log =
-            LoggerFactory.getLogger(HttpSBControllerImpl.class);
+    private static final Logger log = LoggerFactory.getLogger(HttpSBControllerImpl.class);
     private static final String XML = "xml";
     private static final String JSON = "json";
     protected static final String DOUBLESLASH = "//";
@@ -95,8 +97,7 @@
 
     @Override
     public RestSBDevice getDevice(IpAddress ip, int port) {
-        return deviceMap.values().stream().filter(v -> v.ip().equals(ip)
-                && v.port() == port).findFirst().get();
+        return deviceMap.values().stream().filter(v -> v.ip().equals(ip) && v.port() == port).findFirst().get();
     }
 
     @Override
@@ -124,13 +125,26 @@
 
     @Override
     public boolean post(DeviceId device, String request, InputStream payload, String mediaType) {
-        Response response = getResponse(device, request, payload, mediaType);
-        return checkReply(response);
+        return checkStatusCode(post(device, request, payload, typeOfMediaType(mediaType)));
     }
 
     @Override
-    public <T> T post(DeviceId device, String request, InputStream payload,
-                      String mediaType, Class<T> responseClass) {
+    public int post(DeviceId device, String request, InputStream payload, MediaType mediaType) {
+        Response response = getResponse(device, request, payload, mediaType);
+        if (response == null) {
+            return Status.NO_CONTENT.getStatusCode();
+        }
+        return response.getStatus();
+    }
+
+    @Override
+    public <T> T post(DeviceId device, String request, InputStream payload, String mediaType, Class<T> responseClass) {
+        return post(device, request, payload, typeOfMediaType(mediaType), responseClass);
+    }
+
+    @Override
+    public <T> T post(DeviceId device, String request, InputStream payload, MediaType mediaType,
+                      Class<T> responseClass) {
         Response response = getResponse(device, request, payload, mediaType);
         if (response.hasEntity()) {
             return response.readEntity(responseClass);
@@ -139,65 +153,76 @@
         return null;
     }
 
-    private Response getResponse(DeviceId device, String request, InputStream payload, String mediaType) {
-        String type = typeOfMediaType(mediaType);
+    private Response getResponse(DeviceId device, String request, InputStream payload, MediaType mediaType) {
 
         WebTarget wt = getWebTarget(device, request);
 
         Response response = null;
         if (payload != null) {
             try {
-                response = wt.request(type)
-                        .post(Entity.entity(IOUtils.toString(payload, StandardCharsets.UTF_8), type));
+                response = wt.request(mediaType.getType())
+                        .post(Entity.entity(IOUtils.toString(payload, StandardCharsets.UTF_8), mediaType.getType()));
             } catch (IOException e) {
-                log.error("Cannot do POST {} request on device {} because can't read payload",
-                          request, device);
+                log.error("Cannot do POST {} request on device {} because can't read payload", request, device);
             }
         } else {
-            response = wt.request(type).post(Entity.entity(null, type));
+            response = wt.request(mediaType.getType()).post(Entity.entity(null, mediaType.getType()));
         }
         return response;
     }
 
     @Override
     public boolean put(DeviceId device, String request, InputStream payload, String mediaType) {
-        String type = typeOfMediaType(mediaType);
+        return checkStatusCode(put(device, request, payload, typeOfMediaType(mediaType)));
+    }
+
+    @Override
+    public int put(DeviceId device, String request, InputStream payload, MediaType mediaType) {
 
         WebTarget wt = getWebTarget(device, request);
 
         Response response = null;
         if (payload != null) {
             try {
-                response = wt.request(type)
-                        .put(Entity.entity(IOUtils.toString(payload, StandardCharsets.UTF_8), type));
+                response = wt.request(mediaType.getType()).put(Entity.entity(IOUtils.
+                        toString(payload, StandardCharsets.UTF_8), mediaType.getType()));
             } catch (IOException e) {
-                log.error("Cannot do PUT {} request on device {} because can't read payload",
-                          request, device);
+                log.error("Cannot do PUT {} request on device {} because can't read payload", request, device);
             }
         } else {
-            response = wt.request(type).put(Entity.entity(null, type));
+            response = wt.request(mediaType.getType()).put(Entity.entity(null, mediaType.getType()));
         }
-        return checkReply(response);
+
+        if (response == null) {
+            return Status.NO_CONTENT.getStatusCode();
+        }
+        return response.getStatus();
     }
 
     @Override
     public InputStream get(DeviceId device, String request, String mediaType) {
-        String type = typeOfMediaType(mediaType);
+        return get(device, request, typeOfMediaType(mediaType));
+    }
 
+    @Override
+    public InputStream get(DeviceId device, String request, MediaType mediaType) {
         WebTarget wt = getWebTarget(device, request);
 
-        Response s = wt.request(type).get();
+        Response s = wt.request(mediaType.getType()).get();
 
         if (checkReply(s)) {
-            return new ByteArrayInputStream(s.readEntity((String.class))
-                    .getBytes(StandardCharsets.UTF_8));
+            return new ByteArrayInputStream(s.readEntity((String.class)).getBytes(StandardCharsets.UTF_8));
         }
         return null;
     }
 
     @Override
     public boolean patch(DeviceId device, String request, InputStream payload, String mediaType) {
-        String type = typeOfMediaType(mediaType);
+        return checkStatusCode(patch(device, request, payload, typeOfMediaType(mediaType)));
+    }
+
+    @Override
+    public int patch(DeviceId device, String request, InputStream payload, MediaType mediaType) {
 
         try {
             log.debug("Url request {} ", getUrlString(device, request));
@@ -210,7 +235,7 @@
             }
             if (payload != null) {
                 StringEntity input = new StringEntity(IOUtils.toString(payload, StandardCharsets.UTF_8));
-                input.setContentType(type);
+                input.setContentType(mediaType.getType());
                 httprequest.setEntity(input);
             }
             CloseableHttpClient httpClient;
@@ -219,45 +244,41 @@
             } else {
                 httpClient = HttpClients.createDefault();
             }
-            int responseStatusCode = httpClient
-                    .execute(httprequest)
-                    .getStatusLine()
-                    .getStatusCode();
-            return checkStatusCode(responseStatusCode);
+            return httpClient.execute(httprequest).getStatusLine().getStatusCode();
         } catch (IOException | NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
-            log.error("Cannot do PATCH {} request on device {}",
-                      request, device, e);
+            log.error("Cannot do PATCH {} request on device {}", request, device, e);
         }
-        return false;
+        return Status.BAD_REQUEST.getStatusCode();
     }
 
     @Override
     public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) {
-        String type = typeOfMediaType(mediaType);
+        return checkStatusCode(delete(device, request, payload, typeOfMediaType(mediaType)));
+    }
+
+    @Override
+    public int delete(DeviceId device, String request, InputStream payload, MediaType mediaType) {
 
         WebTarget wt = getWebTarget(device, request);
 
-        // FIXME: do we need to delete an entry by enclosing data in DELETE request?
+        // FIXME: do we need to delete an entry by enclosing data in DELETE
+        // request?
         // wouldn't it be nice to use PUT to implement the similar concept?
-        Response response = wt.request(type).delete();
+        Response response = wt.request(mediaType.getType()).delete();
 
-        return checkReply(response);
+        return response.getStatus();
     }
 
-    private String typeOfMediaType(String mediaType) {
-        String type;
-        switch (mediaType) {
-            case XML:
-                type = MediaType.APPLICATION_XML;
-                break;
-            case JSON:
-                type = MediaType.APPLICATION_JSON;
-                break;
-            default:
-                throw new IllegalArgumentException("Unsupported media type " + mediaType);
+    private MediaType typeOfMediaType(String type) {
+        switch (type) {
+        case XML:
+            return MediaType.APPLICATION_XML_TYPE;
+        case JSON:
+            return MediaType.APPLICATION_JSON_TYPE;
+        default:
+            throw new IllegalArgumentException("Unsupported media type " + type);
 
         }
-        return type;
     }
 
     private void authenticate(Client client, String username, String password) {
@@ -281,32 +302,30 @@
 
     protected String getUrlString(DeviceId device, String request) {
         if (deviceMap.get(device).url() != null) {
-            return deviceMap.get(device).protocol() + COLON + DOUBLESLASH
-                    + deviceMap.get(device).url() + request;
+            return deviceMap.get(device).protocol() + COLON + DOUBLESLASH + deviceMap.get(device).url() + request;
         } else {
-            return deviceMap.get(device).protocol() + COLON +
-                    DOUBLESLASH +
-                    deviceMap.get(device).ip().toString() +
-                    COLON + deviceMap.get(device).port() + request;
+            return deviceMap.get(device).protocol() + COLON + DOUBLESLASH + deviceMap.get(device).ip().toString()
+                    + COLON + deviceMap.get(device).port() + request;
         }
     }
 
     private boolean checkReply(Response response) {
         if (response != null) {
-            return checkStatusCode(response.getStatus());
+            boolean statusCode = checkStatusCode(response.getStatus());
+            if (!statusCode && response.hasEntity()) {
+                log.error("Failed request, HTTP error msg : " + response.readEntity(String.class));
+            }
+            return statusCode;
         }
         log.error("Null reply from device");
         return false;
     }
 
     private boolean checkStatusCode(int statusCode) {
-        if (statusCode == STATUS_OK ||
-                statusCode == STATUS_CREATED ||
-                statusCode == STATUS_ACCEPTED) {
+        if (statusCode == STATUS_OK || statusCode == STATUS_CREATED || statusCode == STATUS_ACCEPTED) {
             return true;
         } else {
-            log.error("Failed request, HTTP error code : "
-                              + statusCode);
+            log.error("Failed request, HTTP error code : " + statusCode);
             return false;
         }
     }
@@ -333,4 +352,5 @@
 
         return ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true).build();
     }
+
 }