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.VnfPkgInfo;
25 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001;
26 import org.onap.so.adapters.vnfmadapter.rest.exceptions.*;
27 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
28 import org.onap.so.rest.exceptions.InvalidRestRequestException;
29 import org.onap.so.rest.exceptions.RestProcessingException;
30 import org.onap.so.rest.service.HttpRestServiceProvider;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Qualifier;
35 import org.springframework.core.convert.ConversionService;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Service;
41 * Provides the implementations of the REST Requests to the ETSI Catalog Manager.
43 * @author gareth.roper@est.tech
46 public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvider {
47 private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogServiceProviderImpl.class);
49 @Qualifier("etsiCatalogServiceProvider")
50 private final HttpRestServiceProvider httpServiceProvider;
51 private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
52 private final ConversionService conversionService;
55 public EtsiCatalogServiceProviderImpl(final EtsiCatalogUrlProvider etsiCatalogUrlProvider,
56 final HttpRestServiceProvider httpServiceProvider, final ConversionService conversionService) {
57 this.etsiCatalogUrlProvider = etsiCatalogUrlProvider;
58 this.httpServiceProvider = httpServiceProvider;
59 this.conversionService = conversionService;
63 public Optional<byte[]> getVnfPackageContent(final String vnfPkgId)
64 throws EtsiCatalogManagerRequestFailureException {
66 final ResponseEntity<byte[]> response = httpServiceProvider
67 .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageContentUrl(vnfPkgId), byte[].class);
68 logger.info("getVnfPackageContent Request to ETSI Catalog Manager Status Code: {}",
69 response.getStatusCodeValue());
70 if (response.getStatusCode() == HttpStatus.OK) {
71 return Optional.ofNullable(response.getBody());
73 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
74 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
75 throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
76 } catch (final RestProcessingException restProcessingException) {
77 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
78 restProcessingException);
79 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
80 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
81 + "due to the attribute: onboardingState not being set to ONBOARDED.");
84 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
88 public Optional<byte[]> getVnfPackageArtifact(final String vnfPkgId, final String artifactPath) {
90 final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(
91 etsiCatalogUrlProvider.getVnfPackageArtifactUrl(vnfPkgId, artifactPath), byte[].class);
92 logger.info("getVnfPackageArtifact Request to ETSI Catalog Manager Status Code: {}",
93 response.getStatusCodeValue());
94 if (response.getStatusCode() == HttpStatus.OK) {
95 return Optional.ofNullable(response.getBody());
97 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
98 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
99 throw new VnfPkgNotFoundException("No Vnf Package Artifact found with vnfPkgId: \"" + vnfPkgId
100 + "\" and artifactPath: \"" + artifactPath + "\".");
101 } catch (final RestProcessingException restProcessingException) {
102 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
103 restProcessingException);
104 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
105 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
106 + "due to the attribute: onboardingState not being set to ONBOARDED.");
109 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
113 public Optional<InlineResponse2001[]> getVnfPackages() {
115 final ResponseEntity<VnfPkgInfo[]> response =
116 httpServiceProvider.getHttpResponse(etsiCatalogUrlProvider.getVnfPackagesUrl(), VnfPkgInfo[].class);
117 logger.info("getVnfPackages Request to ETSI Catalog Manager Status Code: {}",
118 response.getStatusCodeValue());
119 if (response.getStatusCode() == HttpStatus.OK) {
120 if (response.hasBody()) {
121 final VnfPkgInfo[] vnfPackages = response.getBody();
122 final InlineResponse2001[] responses = new InlineResponse2001[vnfPackages.length];
123 for (int index = 0; index < vnfPackages.length; index++) {
124 if (conversionService.canConvert(vnfPackages[index].getClass(), InlineResponse2001.class)) {
125 final InlineResponse2001 inlineResponse2001 =
126 conversionService.convert(vnfPackages[index], InlineResponse2001.class);
127 if (inlineResponse2001 != null) {
128 responses[index] = inlineResponse2001;
131 logger.error("Unable to find Converter for response class: {}", vnfPackages[index].getClass());
133 return Optional.ofNullable(responses);
135 logger.error("Received response without body ...");
137 logger.error("Unexpected status code received {}", response.getStatusCode());
138 return Optional.empty();
139 } catch (final InvalidRestRequestException invalidRestRequestException) {
140 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
141 throw new VnfPkgBadRequestException("Error: Bad Request Received");
142 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
143 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
144 throw new VnfPkgNotFoundException("No Vnf Packages found");
145 } catch (final RestProcessingException restProcessingException) {
146 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
147 restProcessingException);
148 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
153 public Optional<InlineResponse2001> getVnfPackage(final String vnfPkgId) {
155 final ResponseEntity<VnfPkgInfo> response = httpServiceProvider
156 .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class);
157 logger.info("getVnfPackage Request for vnfPkgId {} to ETSI Catalog Manager Status Code: {}", vnfPkgId,
158 response.getStatusCodeValue());
159 if (response.getStatusCode() == HttpStatus.OK) {
160 if (response.hasBody()) {
161 final VnfPkgInfo vnfPkgInfo = response.getBody();
162 if (conversionService.canConvert(vnfPkgInfo.getClass(), InlineResponse2001.class)) {
163 return Optional.ofNullable(conversionService.convert(vnfPkgInfo, InlineResponse2001.class));
165 logger.error("Unable to find Converter for response class: {}", vnfPkgInfo.getClass());
167 logger.error("Received response without body ....");
169 return Optional.empty();
170 } catch (final InvalidRestRequestException invalidRestRequestException) {
171 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
172 throw new VnfPkgBadRequestException("Error: Bad Request Received");
173 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
174 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
175 throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
176 } catch (final RestProcessingException restProcessingException) {
177 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
178 restProcessingException);
179 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");