Merge "Check for existing VNF in VNFM"
authorByung-Woo Jun <byung-woo.jun@est.tech>
Tue, 2 Apr 2019 15:13:04 +0000 (15:13 +0000)
committerGerrit Code Review <gerrit@onap.org>
Tue, 2 Apr 2019 15:13:04 +0000 (15:13 +0000)
1  2 
common/src/main/java/org/onap/so/rest/service/HttpRestServiceProvider.java
common/src/main/java/org/onap/so/rest/service/HttpRestServiceProviderImpl.java

@@@ -20,9 -20,8 +20,8 @@@
  
  package org.onap.so.rest.service;
  
- import org.springframework.http.ResponseEntity;
  import com.google.common.base.Optional;
+ import org.springframework.http.ResponseEntity;
  
  /**
   * @author waqas.ikram@est.tech
@@@ -31,51 -30,62 +30,71 @@@ public interface HttpRestServiceProvide
  
      /**
       * Execute the HTTP GET to the given URI template
-      * 
+      *
       * @param url the URL
       * @param clazz the type of the return value
       * @return Returns the body of this entity.
       */
-     public <T> Optional<T> get(final String url, final Class<T> clazz);
+     <T> Optional<T> get(final String url, final Class<T> clazz);
  
      /**
       * Execute the HTTP GET to the given URI template
-      * 
+      *
       * @param url the URL
       * @param clazz the type of the return value
       * @return Returns the {@link ResponseEntity}.
       */
-     public <T> ResponseEntity<T> getHttpResponse(final String url, final Class<T> clazz);
+     <T> ResponseEntity<T> getHttpResponse(final String url, final Class<T> clazz);
  
  
      /**
       * Execute the HTTP POST to the given URI template
-      * 
+      *
       * @param object the entity (i.e. body) to write to the request
       * @param url the URL
       * @param clazz the type of the return value
       * @return Returns the body of this entity.
       */
-     public <T> Optional<T> post(final Object object, final String url, final Class<T> clazz);
+     <T> Optional<T> post(final Object object, final String url, final Class<T> clazz);
  
      /**
       * Execute the HTTP POST to the given URI template
-      * 
+      *
+      * @param object the entity (i.e. body) to write to the request
+      * @param url the URL
+      * @param clazz the type of the return value
+      * @return Returns the {@link ResponseEntity}.
+      */
+     <T> ResponseEntity<T> postHttpRequest(final Object object, final String url, final Class<T> clazz);
+     /**
+      * Execute the HTTP PUT to the given URI template
+      *
+      * @param object the entity (i.e. body) to write to the request
+      * @param url the URL
+      * @param clazz the type of the return value
+      * @return Returns the body of this entity.
+      */
+     <T> Optional<T> put(final Object object, final String url, final Class<T> clazz);
+     /**
+      * Execute the HTTP PUT to the given URI template
+      *
       * @param object the entity (i.e. body) to write to the request
       * @param url the URL
       * @param clazz the type of the return value
       * @return Returns the {@link ResponseEntity}.
       */
-     public <T> ResponseEntity<T> postHttpRequest(final Object object, final String url, final Class<T> clazz);
+     <T> ResponseEntity<T> putHttpRequest(final Object object, final String url, final Class<T> clazz);
  
 +  /**
 +   * Execute the HTTP DELETE to the given URI template
 +   *
 +   * @param url the URL
 +   * @param clazz the type of the return value
 +   * @return Returns the {@link ResponseEntity}.
 +   */
 +  public <T> ResponseEntity<T> deleteHttpRequest(final String url, final Class<T> clazz);
 +
  
  }
@@@ -20,6 -20,7 +20,7 @@@
  
  package org.onap.so.rest.service;
  
+ import com.google.common.base.Optional;
  import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
  import org.onap.so.configuration.rest.HttpHeadersProvider;
  import org.onap.so.rest.exceptions.InvalidRestRequestException;
@@@ -35,11 -36,9 +36,9 @@@ import org.springframework.web.client.H
  import org.springframework.web.client.RestClientException;
  import org.springframework.web.client.RestTemplate;
  
- import com.google.common.base.Optional;
  /**
   * A Service to perform HTTP requests
-  * 
+  *
   * @author waqas.ikram@est.tech
   */
  public class HttpRestServiceProviderImpl implements HttpRestServiceProvider {
      @Override
      public <T> Optional<T> get(final String url, final Class<T> clazz) {
          final ResponseEntity<T> response = getHttpResponse(url, clazz);
-         if (!response.getStatusCode().equals(HttpStatus.OK)) {
-             final String message =
-                     "Unable to invoke HTTP GET using URL: " + url + ", Response Code: " + response.getStatusCode();
-             LOGGER.error(message);
-             return Optional.absent();
-         }
-         if (response.hasBody()) {
-             return Optional.of(response.getBody());
-         }
-         return Optional.absent();
+         return createOptional(response, url, HttpMethod.GET);
      }
  
  
      @Override
      public <T> ResponseEntity<T> getHttpResponse(final String url, final Class<T> clazz) {
-         LOGGER.trace("Will invoke HTTP GET using URL: {}", url);
-         try {
-             final HttpEntity<?> request = new HttpEntity<>(getHttpHeaders());
-             return restTemplate.exchange(url, HttpMethod.GET, request, clazz);
-         } catch (final HttpClientErrorException httpClientErrorException) {
-             final String message = "Unable to invoke HTTP GET using url: " + url + ", Response: "
-                     + httpClientErrorException.getRawStatusCode();
-             LOGGER.error(message, httpClientErrorException);
-             final int rawStatusCode = httpClientErrorException.getRawStatusCode();
-             if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
-                 throw new InvalidRestRequestException("No result found for given url: " + url);
-             }
-             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url);
-         } catch (final RestClientException restClientException) {
-             LOGGER.error("Unable to invoke HTTP GET using url: {}", url, restClientException);
-             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url, restClientException);
-         }
+         final HttpEntity<?> request = new HttpEntity<>(getHttpHeaders());
+         return invokeHttpRequest(request, HttpMethod.GET, url, clazz);
      }
  
      @Override
      public <T> Optional<T> post(final Object object, final String url, final Class<T> clazz) {
          final ResponseEntity<T> response = postHttpRequest(object, url, clazz);
+         return createOptional(response, url, HttpMethod.POST);
+     }
+     @Override
+     public <T> ResponseEntity<T> postHttpRequest(final Object object, final String url, final Class<T> clazz) {
+         final HttpEntity<?> request = new HttpEntity<>(object, getHttpHeaders());
+         return invokeHttpRequest(request, HttpMethod.POST, url, clazz);
+     }
+     @Override
+     public <T> Optional<T> put(final Object object, final String url, final Class<T> clazz) {
+         final ResponseEntity<T> response = putHttpRequest(object, url, clazz);
+         return createOptional(response, url, HttpMethod.PUT);
+     }
+     @Override
+     public <T> ResponseEntity<T> putHttpRequest(final Object object, final String url, final Class<T> clazz) {
+         final HttpEntity<?> request = new HttpEntity<>(object, getHttpHeaders());
+         return invokeHttpRequest(request, HttpMethod.PUT, url, clazz);
+     }
+     private <T> Optional<T> createOptional(final ResponseEntity<T> response, final String url,
+             final HttpMethod httpMethod) {
          if (!response.getStatusCode().equals(HttpStatus.OK)) {
-             final String message =
-                     "Unable to invoke HTTP GET using URL: " + url + ", Response Code: " + response.getStatusCode();
+             final String message = "Unable to invoke HTTP " + httpMethod + " using URL: " + url + ", Response Code: "
+                     + response.getStatusCode();
              LOGGER.error(message);
              return Optional.absent();
          }
          return Optional.absent();
      }
  
-     @Override
-     public <T> ResponseEntity<T> postHttpRequest(final Object object, final String url, final Class<T> clazz) {
+     private <T> ResponseEntity<T> invokeHttpRequest(final HttpEntity<?> request, final HttpMethod httpMethod,
+             final String url, final Class<T> clazz) {
+         LOGGER.trace("Will invoke HTTP {} using URL: {}", httpMethod, url);
          try {
-             final HttpEntity<?> request = new HttpEntity<>(object, getHttpHeaders());
-             return restTemplate.exchange(url, HttpMethod.POST, request, clazz);
+             return restTemplate.exchange(url, httpMethod, request, clazz);
  
          } catch (final HttpClientErrorException httpClientErrorException) {
-             final String message = "Unable to invoke HTTP POST using url: " + url + ", Response: "
+             final String message = "Unable to invoke HTTP " + httpMethod + " using url: " + url + ", Response: "
                      + httpClientErrorException.getRawStatusCode();
              LOGGER.error(message, httpClientErrorException);
              final int rawStatusCode = httpClientErrorException.getRawStatusCode();
              if (rawStatusCode == HttpStatus.BAD_REQUEST.value() || rawStatusCode == HttpStatus.NOT_FOUND.value()) {
                  throw new InvalidRestRequestException("No result found for given url: " + url);
              }
-             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url);
+             throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url);
  
          } catch (final RestClientException restClientException) {
              LOGGER.error("Unable to invoke HTTP POST using url: {}", url, restClientException);
-             throw new RestProcessingException("Unable to invoke HTTP GET using URL: " + url, restClientException);
+             throw new RestProcessingException("Unable to invoke HTTP " + httpMethod + " using URL: " + url,
+                     restClientException);
          }
      }
  
 +  @Override
 +  public <T> ResponseEntity<T> deleteHttpRequest(final String url, final Class<T> clazz) {
 +    try {
 +      final HttpEntity<?> request = new HttpEntity<>(getHttpHeaders());
 +      return restTemplate.exchange(url, HttpMethod.DELETE, request, clazz);
 +
 +    } catch (final RestClientException restClientException) {
 +      LOGGER.error("Unable to invoke HTTP DELETE using url: " + url, restClientException);
 +      throw new InvalidRestRequestException("Unable to invoke HTTP DELETE using URL: " + url, restClientException);
 +    }
 +  }
 +
      private HttpHeaders getHttpHeaders() {
          return httpHeadersProvider.getHttpHeaders();
      }