2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2019 Nordix Foundation.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.so.adapters.vnfmadapter.extclients.etsicatalog;
 
  23 import java.util.Optional;
 
  24 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription;
 
  25 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPkgInfo;
 
  26 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001;
 
  27 import org.onap.so.adapters.vnfmadapter.rest.exceptions.EtsiCatalogManagerBadRequestException;
 
  28 import org.onap.so.adapters.vnfmadapter.rest.exceptions.EtsiCatalogManagerRequestFailureException;
 
  29 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgBadRequestException;
 
  30 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgConflictException;
 
  31 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgNotFoundException;
 
  32 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
 
  33 import org.onap.so.rest.exceptions.InvalidRestRequestException;
 
  34 import org.onap.so.rest.exceptions.RestProcessingException;
 
  35 import org.onap.so.rest.service.HttpRestServiceProvider;
 
  36 import org.slf4j.Logger;
 
  37 import org.slf4j.LoggerFactory;
 
  38 import org.springframework.beans.factory.annotation.Autowired;
 
  39 import org.springframework.beans.factory.annotation.Qualifier;
 
  40 import org.springframework.core.convert.ConversionService;
 
  41 import org.springframework.http.HttpStatus;
 
  42 import org.springframework.http.ResponseEntity;
 
  43 import org.springframework.stereotype.Service;
 
  46  * Provides the implementations of the REST Requests to the ETSI Catalog Manager.
 
  48  * @author gareth.roper@est.tech
 
  51 public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvider {
 
  52     private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogServiceProviderImpl.class);
 
  54     @Qualifier("etsiCatalogServiceProvider")
 
  55     private final HttpRestServiceProvider httpServiceProvider;
 
  56     private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
 
  57     private final ConversionService conversionService;
 
  60     public EtsiCatalogServiceProviderImpl(final EtsiCatalogUrlProvider etsiCatalogUrlProvider,
 
  61             final HttpRestServiceProvider httpServiceProvider, final ConversionService conversionService) {
 
  62         this.etsiCatalogUrlProvider = etsiCatalogUrlProvider;
 
  63         this.httpServiceProvider = httpServiceProvider;
 
  64         this.conversionService = conversionService;
 
  68     public Optional<byte[]> getVnfPackageContent(final String vnfPkgId)
 
  69             throws EtsiCatalogManagerRequestFailureException {
 
  70         final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageContentUrl(vnfPkgId);
 
  71         final String vnfRequestName = "getVnfPackageContent";
 
  72         return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
 
  76     public Optional<byte[]> getVnfPackageArtifact(final String vnfPkgId, final String artifactPath) {
 
  78             final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(
 
  79                     etsiCatalogUrlProvider.getVnfPackageArtifactUrl(vnfPkgId, artifactPath), byte[].class);
 
  80             logger.info("getVnfPackageArtifact Request to ETSI Catalog Manager Status Code: {}",
 
  81                     response.getStatusCodeValue());
 
  82             if (response.getStatusCode() == HttpStatus.OK) {
 
  83                 return Optional.ofNullable(response.getBody());
 
  85         } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
 
  86             logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
 
  87             throw new VnfPkgNotFoundException("No Vnf Package Artifact found with vnfPkgId: \"" + vnfPkgId
 
  88                     + "\" and artifactPath: \"" + artifactPath + "\".");
 
  89         } catch (final RestProcessingException restProcessingException) {
 
  90             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
 
  91                     restProcessingException);
 
  92             if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
 
  93                 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
 
  94                         + "due to the attribute: onboardingState not being set to ONBOARDED.");
 
  97         throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
 
 101     public Optional<InlineResponse2001[]> getVnfPackages() {
 
 103             final ResponseEntity<VnfPkgInfo[]> response =
 
 104                     httpServiceProvider.getHttpResponse(etsiCatalogUrlProvider.getVnfPackagesUrl(), VnfPkgInfo[].class);
 
 105             logger.info("getVnfPackages Request to ETSI Catalog Manager Status Code: {}",
 
 106                     response.getStatusCodeValue());
 
 107             if (response.getStatusCode() == HttpStatus.OK) {
 
 108                 if (response.hasBody()) {
 
 109                     final VnfPkgInfo[] vnfPackages = response.getBody();
 
 110                     assert (vnfPackages != null);
 
 111                     final InlineResponse2001[] responses = new InlineResponse2001[vnfPackages.length];
 
 112                     for (int index = 0; index < vnfPackages.length; index++) {
 
 113                         if (conversionService.canConvert(vnfPackages[index].getClass(), InlineResponse2001.class)) {
 
 114                             final InlineResponse2001 inlineResponse2001 =
 
 115                                     conversionService.convert(vnfPackages[index], InlineResponse2001.class);
 
 116                             if (inlineResponse2001 != null) {
 
 117                                 responses[index] = inlineResponse2001;
 
 120                         logger.error("Unable to find Converter for response class: {}", vnfPackages[index].getClass());
 
 122                     return Optional.of(responses);
 
 124                 logger.error("Received response without body ...");
 
 126             logger.error("Unexpected status code received {}", response.getStatusCode());
 
 127             return Optional.empty();
 
 128         } catch (final InvalidRestRequestException invalidRestRequestException) {
 
 129             logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
 
 130             throw new VnfPkgBadRequestException("Error: Bad Request Received");
 
 131         } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
 
 132             logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
 
 133             throw new VnfPkgNotFoundException("No Vnf Packages found");
 
 134         } catch (final RestProcessingException restProcessingException) {
 
 135             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
 
 136                     restProcessingException);
 
 137             throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
 
 142     public Optional<InlineResponse2001> getVnfPackage(final String vnfPkgId) {
 
 144             final ResponseEntity<VnfPkgInfo> response = httpServiceProvider
 
 145                     .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class);
 
 146             logger.info("getVnfPackage Request for vnfPkgId {} to ETSI Catalog Manager Status Code: {}", vnfPkgId,
 
 147                     response.getStatusCodeValue());
 
 148             if (response.getStatusCode() == HttpStatus.OK) {
 
 149                 if (response.hasBody()) {
 
 150                     final VnfPkgInfo vnfPkgInfo = response.getBody();
 
 151                     if (conversionService.canConvert(vnfPkgInfo.getClass(), InlineResponse2001.class)) {
 
 152                         return Optional.ofNullable(conversionService.convert(vnfPkgInfo, InlineResponse2001.class));
 
 154                     logger.error("Unable to find Converter for response class: {}", vnfPkgInfo.getClass());
 
 156                 logger.error("Received response without body ....");
 
 158             return Optional.empty();
 
 159         } catch (final InvalidRestRequestException invalidRestRequestException) {
 
 160             logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
 
 161             throw new VnfPkgBadRequestException("Error: Bad Request Received");
 
 162         } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
 
 163             logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
 
 164             throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
 
 165         } catch (final RestProcessingException restProcessingException) {
 
 166             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
 
 167                     restProcessingException);
 
 168             throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
 
 173     public Optional<byte[]> getVnfPackageVnfd(final String vnfPkgId) {
 
 174         final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageVnfdUrl(vnfPkgId);
 
 175         final String vnfRequestName = "getVnfPackageVnfd";
 
 176         return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
 
 180     public Optional<PkgmSubscription> postSubscription(
 
 181             final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest) {
 
 183             final ResponseEntity<PkgmSubscription> responseEntity =
 
 184                     httpServiceProvider.postHttpRequest(etsiCatalogManagerSubscriptionRequest,
 
 185                             etsiCatalogUrlProvider.getSubscriptionUrl(), PkgmSubscription.class);
 
 186             if (responseEntity.getStatusCode() == HttpStatus.OK) {
 
 187                 if (responseEntity.hasBody()) {
 
 188                     return Optional.of(responseEntity.getBody());
 
 190                 logger.error("Received response without body on postSubscription");
 
 192             logger.error("Unexpected Status Code Received on postSubscription: {}", responseEntity.getStatusCode());
 
 193             return Optional.empty();
 
 194         } catch (final InvalidRestRequestException invalidRestRequestException) {
 
 195             logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
 
 196             throw new EtsiCatalogManagerBadRequestException(
 
 197                     "Bad Request Received on postSubscription call to ETSI Catalog Manager.");
 
 198         } catch (final RestProcessingException restProcessingException) {
 
 199             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
 
 200                     restProcessingException);
 
 201             throw new EtsiCatalogManagerRequestFailureException(
 
 202                     "Internal Server Error Occurred. On postSubscription with StatusCode: "
 
 203                             + restProcessingException.getStatusCode());
 
 207     public Boolean deleteSubscription(final String subscriptionId) {
 
 209             final ResponseEntity<Void> responseEntity = httpServiceProvider
 
 210                     .deleteHttpRequest(etsiCatalogUrlProvider.getSubscriptionUrl() + "/" + subscriptionId, Void.class);
 
 212             if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
 
 213                 logger.info("Subscription with ID: {} has been successfully deleted from the ETSI Catalog Manager",
 
 217             logger.error("Unexpected Status Code Received on deleteSubscription: {}", responseEntity.getStatusCode());
 
 219         } catch (final InvalidRestRequestException invalidRestRequestException) {
 
 220             logger.error("Caught InvalidRestRequestException on deleteSubscription call to ETSI Catalog Manager.",
 
 221                     invalidRestRequestException);
 
 222             throw new EtsiCatalogManagerBadRequestException(
 
 223                     "Bad Request Received on deleteSubscription call to ETSI Catalog Manager.");
 
 229     private Optional<byte[]> requestVnfElement(final String vnfPkgId, final String vnfRequestUrl,
 
 230             final String vnfRequestName) {
 
 232             final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(vnfRequestUrl, byte[].class);
 
 233             logger.info("{} Request to ETSI Catalog Manager Status Code: {}", vnfRequestName,
 
 234                     response.getStatusCodeValue());
 
 235             if (response.getStatusCode() == HttpStatus.OK) {
 
 236                 return Optional.ofNullable(response.getBody());
 
 238         } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
 
 239             logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
 
 240             throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
 
 241         } catch (final RestProcessingException restProcessingException) {
 
 242             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
 
 243                     restProcessingException);
 
 244             if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
 
 245                 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
 
 246                         + "due to the attribute: onboardingState not being set to ONBOARDED.");
 
 249         throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");