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.etsisol003adapter.pkgm.extclients.etsicatalog;
23 import static org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.EtsiCatalogServiceProviderConfiguration.ETSI_CATALOG_SERVICE_PROVIDER_BEAN;
24 import java.util.Optional;
25 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.NsdmSubscription;
26 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.PkgmSubscription;
27 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.VnfPkgInfo;
28 import org.onap.so.adapters.etsisol003adapter.pkgm.model.InlineResponse2001;
29 import org.onap.so.adapters.etsisol003adapter.pkgm.rest.exceptions.EtsiCatalogManagerBadRequestException;
30 import org.onap.so.adapters.etsisol003adapter.pkgm.rest.exceptions.EtsiCatalogManagerRequestFailureException;
31 import org.onap.so.adapters.etsisol003adapter.pkgm.rest.exceptions.SubscriptionNotFoundException;
32 import org.onap.so.adapters.etsisol003adapter.pkgm.rest.exceptions.VnfPkgBadRequestException;
33 import org.onap.so.adapters.etsisol003adapter.pkgm.rest.exceptions.VnfPkgConflictException;
34 import org.onap.so.adapters.etsisol003adapter.pkgm.rest.exceptions.VnfPkgNotFoundException;
35 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
36 import org.onap.so.rest.exceptions.InvalidRestRequestException;
37 import org.onap.so.rest.exceptions.RestProcessingException;
38 import org.onap.so.rest.service.HttpRestServiceProvider;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.beans.factory.annotation.Qualifier;
43 import org.springframework.core.convert.ConversionService;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.stereotype.Service;
49 * Provides the implementations of the REST Requests to the ETSI Catalog Manager.
51 * @author gareth.roper@est.tech
54 public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvider {
55 private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogServiceProviderImpl.class);
57 private final HttpRestServiceProvider httpServiceProvider;
58 private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
59 private final ConversionService conversionService;
62 public EtsiCatalogServiceProviderImpl(final EtsiCatalogUrlProvider etsiCatalogUrlProvider,
63 @Qualifier(ETSI_CATALOG_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider,
64 final ConversionService conversionService) {
65 this.etsiCatalogUrlProvider = etsiCatalogUrlProvider;
66 this.httpServiceProvider = httpServiceProvider;
67 this.conversionService = conversionService;
71 public Optional<byte[]> getVnfPackageContent(final String vnfPkgId)
72 throws EtsiCatalogManagerRequestFailureException {
73 final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageContentUrl(vnfPkgId);
74 final String vnfRequestName = "getVnfPackageContent";
75 return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
79 public Optional<byte[]> getVnfPackageArtifact(final String vnfPkgId, final String artifactPath) {
81 final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(
82 etsiCatalogUrlProvider.getVnfPackageArtifactUrl(vnfPkgId, artifactPath), byte[].class);
83 logger.info("getVnfPackageArtifact Request to ETSI Catalog Manager Status Code: {}",
84 response.getStatusCodeValue());
85 if (response.getStatusCode().is2xxSuccessful()) {
86 return Optional.ofNullable(response.getBody());
88 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
89 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
90 throw new VnfPkgNotFoundException("No Vnf Package Artifact found with vnfPkgId: \"" + vnfPkgId
91 + "\" and artifactPath: \"" + artifactPath + "\".");
92 } catch (final RestProcessingException restProcessingException) {
93 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
94 restProcessingException);
95 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
96 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
97 + "due to the attribute: onboardingState not being set to ONBOARDED.");
100 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
104 public Optional<InlineResponse2001[]> getVnfPackages() {
106 final ResponseEntity<VnfPkgInfo[]> response =
107 httpServiceProvider.getHttpResponse(etsiCatalogUrlProvider.getVnfPackagesUrl(), VnfPkgInfo[].class);
108 logger.info("getVnfPackages Request to ETSI Catalog Manager Status Code: {}",
109 response.getStatusCodeValue());
110 if (response.getStatusCode().is2xxSuccessful()) {
111 if (response.hasBody()) {
112 final VnfPkgInfo[] vnfPackages = response.getBody();
113 assert (vnfPackages != null);
114 final InlineResponse2001[] responses = new InlineResponse2001[vnfPackages.length];
115 for (int index = 0; index < vnfPackages.length; index++) {
116 if (conversionService.canConvert(vnfPackages[index].getClass(), InlineResponse2001.class)) {
117 final InlineResponse2001 inlineResponse2001 =
118 conversionService.convert(vnfPackages[index], InlineResponse2001.class);
119 if (inlineResponse2001 != null) {
120 responses[index] = inlineResponse2001;
123 logger.error("Unable to find Converter for response class: {}",
124 vnfPackages[index].getClass());
127 return Optional.of(responses);
129 logger.error("Received response without body ...");
131 logger.error("Unexpected status code received {}", response.getStatusCode());
132 return Optional.empty();
133 } catch (final InvalidRestRequestException invalidRestRequestException) {
134 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
135 throw new VnfPkgBadRequestException("Error: Bad Request Received");
136 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
137 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
138 throw new VnfPkgNotFoundException("No Vnf Packages found");
139 } catch (final RestProcessingException restProcessingException) {
140 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
141 restProcessingException);
142 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
147 public Optional<InlineResponse2001> getVnfPackage(final String vnfPkgId) {
149 final ResponseEntity<VnfPkgInfo> response = httpServiceProvider
150 .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class);
151 logger.info("getVnfPackage Request for vnfPkgId {} to ETSI Catalog Manager Status Code: {}", vnfPkgId,
152 response.getStatusCodeValue());
153 if (response.getStatusCode().is2xxSuccessful()) {
154 if (response.hasBody()) {
155 final VnfPkgInfo vnfPkgInfo = response.getBody();
156 if (conversionService.canConvert(vnfPkgInfo.getClass(), InlineResponse2001.class)) {
157 return Optional.ofNullable(conversionService.convert(vnfPkgInfo, InlineResponse2001.class));
159 logger.error("Unable to find Converter for response class: {}", vnfPkgInfo.getClass());
161 logger.error("Received response without body ....");
163 return Optional.empty();
164 } catch (final InvalidRestRequestException invalidRestRequestException) {
165 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
166 throw new VnfPkgBadRequestException("Error: Bad Request Received");
167 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
168 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
169 throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
170 } catch (final RestProcessingException restProcessingException) {
171 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
172 restProcessingException);
173 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
178 public Optional<byte[]> getVnfPackageVnfd(final String vnfPkgId) {
179 final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageVnfdUrl(vnfPkgId);
180 final String vnfRequestName = "getVnfPackageVnfd";
181 return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
185 public Optional<PkgmSubscription> postSubscription(
186 final org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest) {
188 final ResponseEntity<PkgmSubscription> responseEntity =
189 httpServiceProvider.postHttpRequest(etsiCatalogManagerSubscriptionRequest,
190 etsiCatalogUrlProvider.getSubscriptionUrl(), PkgmSubscription.class);
191 if (responseEntity.getStatusCode().is2xxSuccessful()) {
192 if (responseEntity.hasBody()) {
193 return Optional.of(responseEntity.getBody());
195 logger.error("Received response without body on postSubscription");
197 logger.error("Unexpected Status Code Received on postSubscription: {}", responseEntity.getStatusCode());
198 return Optional.empty();
199 } catch (final InvalidRestRequestException invalidRestRequestException) {
200 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
201 throw new EtsiCatalogManagerBadRequestException(
202 "Bad Request Received on postSubscription call to ETSI Catalog Manager.");
203 } catch (final RestProcessingException restProcessingException) {
204 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
205 restProcessingException);
206 throw new EtsiCatalogManagerRequestFailureException(
207 "Internal Server Error Occurred. On postSubscription with StatusCode: "
208 + restProcessingException.getStatusCode());
213 public boolean deleteSubscription(final String subscriptionId) {
215 final ResponseEntity<Void> responseEntity = httpServiceProvider
216 .deleteHttpRequest(etsiCatalogUrlProvider.getSubscriptionUrl() + "/" + subscriptionId, Void.class);
218 if (responseEntity.getStatusCode().is2xxSuccessful()) {
219 logger.info("Subscription with ID: {} has been successfully deleted from the ETSI Catalog Manager",
223 logger.error("Unexpected Status Code Received on deleteSubscription: {}", responseEntity.getStatusCode());
225 } catch (final HttpResouceNotFoundException resouceNotFoundException) {
226 final String message = "Unable to find subscription in ETSI Catalog Manager using id: " + subscriptionId;
227 logger.error(message);
228 throw new SubscriptionNotFoundException(message);
229 } catch (final InvalidRestRequestException invalidRestRequestException) {
230 logger.error("Caught InvalidRestRequestException on deleteSubscription call to ETSI Catalog Manager.",
231 invalidRestRequestException);
232 throw new EtsiCatalogManagerBadRequestException(
233 "Bad Request Received on deleteSubscription call to ETSI Catalog Manager.");
238 public Optional<NsdmSubscription> getSubscription(final String subscriptionId) {
240 final ResponseEntity<NsdmSubscription> responseEntity = httpServiceProvider.getHttpResponse(
241 etsiCatalogUrlProvider.getSubscriptionUrl() + "/" + subscriptionId, NsdmSubscription.class);
243 if (responseEntity.getStatusCode().is2xxSuccessful()) {
244 logger.debug("Found subscription with ID: {} in ETSI Catalog Manager", subscriptionId);
245 return Optional.ofNullable(responseEntity.getBody());
247 logger.error("Unexpected Status Code Received on getting subscription from ETSI Catalog Manager: {}",
248 responseEntity.getStatusCode());
249 } catch (final HttpResouceNotFoundException resouceNotFoundException) {
250 logger.error("Unable to find subscription in ETSI Catalog Manager using id: {}", subscriptionId);
251 return Optional.empty();
252 } catch (final RestProcessingException | InvalidRestRequestException exception) {
253 logger.error("Unable to query ETSI Catalog Manager for subscription using id: {}", subscriptionId,
256 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
259 private Optional<byte[]> requestVnfElement(final String vnfPkgId, final String vnfRequestUrl,
260 final String vnfRequestName) {
262 final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(vnfRequestUrl, byte[].class);
263 logger.info("{} Request to ETSI Catalog Manager Status Code: {}", vnfRequestName,
264 response.getStatusCodeValue());
265 if (response.getStatusCode() == HttpStatus.OK) {
266 return Optional.ofNullable(response.getBody());
268 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
269 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
270 throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
271 } catch (final RestProcessingException restProcessingException) {
272 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
273 restProcessingException);
274 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
275 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
276 + "due to the attribute: onboardingState not being set to ONBOARDED.");
279 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");