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.NsdmSubscription;
25 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription;
26 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPkgInfo;
27 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001;
28 import org.onap.so.adapters.vnfmadapter.rest.exceptions.EtsiCatalogManagerBadRequestException;
29 import org.onap.so.adapters.vnfmadapter.rest.exceptions.EtsiCatalogManagerRequestFailureException;
30 import org.onap.so.adapters.vnfmadapter.rest.exceptions.SubscriptionNotFoundException;
31 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgBadRequestException;
32 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgConflictException;
33 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgNotFoundException;
34 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
35 import org.onap.so.rest.exceptions.InvalidRestRequestException;
36 import org.onap.so.rest.exceptions.RestProcessingException;
37 import org.onap.so.rest.service.HttpRestServiceProvider;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.beans.factory.annotation.Qualifier;
42 import org.springframework.core.convert.ConversionService;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.stereotype.Service;
48 * Provides the implementations of the REST Requests to the ETSI Catalog Manager.
50 * @author gareth.roper@est.tech
53 public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvider {
54 private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogServiceProviderImpl.class);
56 @Qualifier("etsiCatalogServiceProvider")
57 private final HttpRestServiceProvider httpServiceProvider;
58 private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
59 private final ConversionService conversionService;
62 public EtsiCatalogServiceProviderImpl(final EtsiCatalogUrlProvider etsiCatalogUrlProvider,
63 final HttpRestServiceProvider httpServiceProvider, final ConversionService conversionService) {
64 this.etsiCatalogUrlProvider = etsiCatalogUrlProvider;
65 this.httpServiceProvider = httpServiceProvider;
66 this.conversionService = conversionService;
70 public Optional<byte[]> getVnfPackageContent(final String vnfPkgId)
71 throws EtsiCatalogManagerRequestFailureException {
72 final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageContentUrl(vnfPkgId);
73 final String vnfRequestName = "getVnfPackageContent";
74 return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
78 public Optional<byte[]> getVnfPackageArtifact(final String vnfPkgId, final String artifactPath) {
80 final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(
81 etsiCatalogUrlProvider.getVnfPackageArtifactUrl(vnfPkgId, artifactPath), byte[].class);
82 logger.info("getVnfPackageArtifact Request to ETSI Catalog Manager Status Code: {}",
83 response.getStatusCodeValue());
84 if (response.getStatusCode().is2xxSuccessful()) {
85 return Optional.ofNullable(response.getBody());
87 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
88 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
89 throw new VnfPkgNotFoundException("No Vnf Package Artifact found with vnfPkgId: \"" + vnfPkgId
90 + "\" and artifactPath: \"" + artifactPath + "\".");
91 } catch (final RestProcessingException restProcessingException) {
92 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
93 restProcessingException);
94 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
95 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
96 + "due to the attribute: onboardingState not being set to ONBOARDED.");
99 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
103 public Optional<InlineResponse2001[]> getVnfPackages() {
105 final ResponseEntity<VnfPkgInfo[]> response =
106 httpServiceProvider.getHttpResponse(etsiCatalogUrlProvider.getVnfPackagesUrl(), VnfPkgInfo[].class);
107 logger.info("getVnfPackages Request to ETSI Catalog Manager Status Code: {}",
108 response.getStatusCodeValue());
109 if (response.getStatusCode().is2xxSuccessful()) {
110 if (response.hasBody()) {
111 final VnfPkgInfo[] vnfPackages = response.getBody();
112 assert (vnfPackages != null);
113 final InlineResponse2001[] responses = new InlineResponse2001[vnfPackages.length];
114 for (int index = 0; index < vnfPackages.length; index++) {
115 if (conversionService.canConvert(vnfPackages[index].getClass(), InlineResponse2001.class)) {
116 final InlineResponse2001 inlineResponse2001 =
117 conversionService.convert(vnfPackages[index], InlineResponse2001.class);
118 if (inlineResponse2001 != null) {
119 responses[index] = inlineResponse2001;
122 logger.error("Unable to find Converter for response class: {}", vnfPackages[index].getClass());
124 return Optional.of(responses);
126 logger.error("Received response without body ...");
128 logger.error("Unexpected status code received {}", response.getStatusCode());
129 return Optional.empty();
130 } catch (final InvalidRestRequestException invalidRestRequestException) {
131 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
132 throw new VnfPkgBadRequestException("Error: Bad Request Received");
133 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
134 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
135 throw new VnfPkgNotFoundException("No Vnf Packages found");
136 } catch (final RestProcessingException restProcessingException) {
137 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
138 restProcessingException);
139 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
144 public Optional<InlineResponse2001> getVnfPackage(final String vnfPkgId) {
146 final ResponseEntity<VnfPkgInfo> response = httpServiceProvider
147 .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class);
148 logger.info("getVnfPackage Request for vnfPkgId {} to ETSI Catalog Manager Status Code: {}", vnfPkgId,
149 response.getStatusCodeValue());
150 if (response.getStatusCode().is2xxSuccessful()) {
151 if (response.hasBody()) {
152 final VnfPkgInfo vnfPkgInfo = response.getBody();
153 if (conversionService.canConvert(vnfPkgInfo.getClass(), InlineResponse2001.class)) {
154 return Optional.ofNullable(conversionService.convert(vnfPkgInfo, InlineResponse2001.class));
156 logger.error("Unable to find Converter for response class: {}", vnfPkgInfo.getClass());
158 logger.error("Received response without body ....");
160 return Optional.empty();
161 } catch (final InvalidRestRequestException invalidRestRequestException) {
162 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
163 throw new VnfPkgBadRequestException("Error: Bad Request Received");
164 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
165 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
166 throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
167 } catch (final RestProcessingException restProcessingException) {
168 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
169 restProcessingException);
170 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
175 public Optional<byte[]> getVnfPackageVnfd(final String vnfPkgId) {
176 final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageVnfdUrl(vnfPkgId);
177 final String vnfRequestName = "getVnfPackageVnfd";
178 return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
182 public Optional<PkgmSubscription> postSubscription(
183 final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest) {
185 final ResponseEntity<PkgmSubscription> responseEntity =
186 httpServiceProvider.postHttpRequest(etsiCatalogManagerSubscriptionRequest,
187 etsiCatalogUrlProvider.getSubscriptionUrl(), PkgmSubscription.class);
188 if (responseEntity.getStatusCode().is2xxSuccessful()) {
189 if (responseEntity.hasBody()) {
190 return Optional.of(responseEntity.getBody());
192 logger.error("Received response without body on postSubscription");
194 logger.error("Unexpected Status Code Received on postSubscription: {}", responseEntity.getStatusCode());
195 return Optional.empty();
196 } catch (final InvalidRestRequestException invalidRestRequestException) {
197 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
198 throw new EtsiCatalogManagerBadRequestException(
199 "Bad Request Received on postSubscription call to ETSI Catalog Manager.");
200 } catch (final RestProcessingException restProcessingException) {
201 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
202 restProcessingException);
203 throw new EtsiCatalogManagerRequestFailureException(
204 "Internal Server Error Occurred. On postSubscription with StatusCode: "
205 + restProcessingException.getStatusCode());
210 public boolean deleteSubscription(final String subscriptionId) {
212 final ResponseEntity<Void> responseEntity = httpServiceProvider
213 .deleteHttpRequest(etsiCatalogUrlProvider.getSubscriptionUrl() + "/" + subscriptionId, Void.class);
215 if (responseEntity.getStatusCode().is2xxSuccessful()) {
216 logger.info("Subscription with ID: {} has been successfully deleted from the ETSI Catalog Manager",
220 logger.error("Unexpected Status Code Received on deleteSubscription: {}", responseEntity.getStatusCode());
222 } catch (final HttpResouceNotFoundException resouceNotFoundException) {
223 final String message = "Unable to find subscription in ETSI Catalog Manager using id: " + subscriptionId;
224 logger.error(message);
225 throw new SubscriptionNotFoundException(message);
226 } catch (final InvalidRestRequestException invalidRestRequestException) {
227 logger.error("Caught InvalidRestRequestException on deleteSubscription call to ETSI Catalog Manager.",
228 invalidRestRequestException);
229 throw new EtsiCatalogManagerBadRequestException(
230 "Bad Request Received on deleteSubscription call to ETSI Catalog Manager.");
235 public Optional<NsdmSubscription> getSubscription(final String subscriptionId) {
237 final ResponseEntity<NsdmSubscription> responseEntity = httpServiceProvider.getHttpResponse(
238 etsiCatalogUrlProvider.getSubscriptionUrl() + "/" + subscriptionId, NsdmSubscription.class);
240 if (responseEntity.getStatusCode().is2xxSuccessful()) {
241 logger.debug("Found subscription with ID: {} in ETSI Catalog Manager", subscriptionId);
242 return Optional.ofNullable(responseEntity.getBody());
244 logger.error("Unexpected Status Code Received on getting subscription from ETSI Catalog Manager: {}",
245 responseEntity.getStatusCode());
246 } catch (final HttpResouceNotFoundException resouceNotFoundException) {
247 logger.error("Unable to find subscription in ETSI Catalog Manager using id: {}", subscriptionId);
248 return Optional.empty();
249 } catch (final RestProcessingException | InvalidRestRequestException exception) {
250 logger.error("Unable to query ETSI Catalog Manager for subscription using id: {}", subscriptionId,
253 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
256 private Optional<byte[]> requestVnfElement(final String vnfPkgId, final String vnfRequestUrl,
257 final String vnfRequestName) {
259 final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(vnfRequestUrl, byte[].class);
260 logger.info("{} Request to ETSI Catalog Manager Status Code: {}", vnfRequestName,
261 response.getStatusCodeValue());
262 if (response.getStatusCode() == HttpStatus.OK) {
263 return Optional.ofNullable(response.getBody());
265 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
266 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
267 throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
268 } catch (final RestProcessingException restProcessingException) {
269 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
270 restProcessingException);
271 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
272 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
273 + "due to the attribute: onboardingState not being set to ONBOARDED.");
276 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");