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 {
65 final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageContentUrl(vnfPkgId);
66 final String vnfRequestName = "getVnfPackageContent";
67 return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
71 public Optional<byte[]> getVnfPackageArtifact(final String vnfPkgId, final String artifactPath) {
73 final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(
74 etsiCatalogUrlProvider.getVnfPackageArtifactUrl(vnfPkgId, artifactPath), byte[].class);
75 logger.info("getVnfPackageArtifact Request to ETSI Catalog Manager Status Code: {}",
76 response.getStatusCodeValue());
77 if (response.getStatusCode() == HttpStatus.OK) {
78 return Optional.ofNullable(response.getBody());
80 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
81 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
82 throw new VnfPkgNotFoundException("No Vnf Package Artifact found with vnfPkgId: \"" + vnfPkgId
83 + "\" and artifactPath: \"" + artifactPath + "\".");
84 } catch (final RestProcessingException restProcessingException) {
85 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
86 restProcessingException);
87 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
88 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
89 + "due to the attribute: onboardingState not being set to ONBOARDED.");
92 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
96 public Optional<InlineResponse2001[]> getVnfPackages() {
98 final ResponseEntity<VnfPkgInfo[]> response =
99 httpServiceProvider.getHttpResponse(etsiCatalogUrlProvider.getVnfPackagesUrl(), VnfPkgInfo[].class);
100 logger.info("getVnfPackages Request to ETSI Catalog Manager Status Code: {}",
101 response.getStatusCodeValue());
102 if (response.getStatusCode() == HttpStatus.OK) {
103 if (response.hasBody()) {
104 final VnfPkgInfo[] vnfPackages = response.getBody();
105 final InlineResponse2001[] responses = new InlineResponse2001[vnfPackages.length];
106 for (int index = 0; index < vnfPackages.length; index++) {
107 if (conversionService.canConvert(vnfPackages[index].getClass(), InlineResponse2001.class)) {
108 final InlineResponse2001 inlineResponse2001 =
109 conversionService.convert(vnfPackages[index], InlineResponse2001.class);
110 if (inlineResponse2001 != null) {
111 responses[index] = inlineResponse2001;
114 logger.error("Unable to find Converter for response class: {}", vnfPackages[index].getClass());
116 return Optional.ofNullable(responses);
118 logger.error("Received response without body ...");
120 logger.error("Unexpected status code received {}", response.getStatusCode());
121 return Optional.empty();
122 } catch (final InvalidRestRequestException invalidRestRequestException) {
123 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
124 throw new VnfPkgBadRequestException("Error: Bad Request Received");
125 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
126 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
127 throw new VnfPkgNotFoundException("No Vnf Packages found");
128 } catch (final RestProcessingException restProcessingException) {
129 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
130 restProcessingException);
131 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
136 public Optional<InlineResponse2001> getVnfPackage(final String vnfPkgId) {
138 final ResponseEntity<VnfPkgInfo> response = httpServiceProvider
139 .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class);
140 logger.info("getVnfPackage Request for vnfPkgId {} to ETSI Catalog Manager Status Code: {}", vnfPkgId,
141 response.getStatusCodeValue());
142 if (response.getStatusCode() == HttpStatus.OK) {
143 if (response.hasBody()) {
144 final VnfPkgInfo vnfPkgInfo = response.getBody();
145 if (conversionService.canConvert(vnfPkgInfo.getClass(), InlineResponse2001.class)) {
146 return Optional.ofNullable(conversionService.convert(vnfPkgInfo, InlineResponse2001.class));
148 logger.error("Unable to find Converter for response class: {}", vnfPkgInfo.getClass());
150 logger.error("Received response without body ....");
152 return Optional.empty();
153 } catch (final InvalidRestRequestException invalidRestRequestException) {
154 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
155 throw new VnfPkgBadRequestException("Error: Bad Request Received");
156 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
157 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
158 throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
159 } catch (final RestProcessingException restProcessingException) {
160 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
161 restProcessingException);
162 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
167 public Optional<byte[]> getVnfPackageVnfd(final String vnfPkgId) {
168 final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageVnfdUrl(vnfPkgId);
169 final String vnfRequestName = "getVnfPackageVnfd";
170 return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
173 private Optional<byte[]> requestVnfElement(final String vnfPkgId, final String vnfRequestUrl,
174 final String vnfRequestName) {
176 final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(vnfRequestUrl, byte[].class);
177 logger.info("{} Request to ETSI Catalog Manager Status Code: {}", vnfRequestName,
178 response.getStatusCodeValue());
179 if (response.getStatusCode() == HttpStatus.OK) {
180 return Optional.ofNullable(response.getBody());
182 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
183 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
184 throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
185 } catch (final RestProcessingException restProcessingException) {
186 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
187 restProcessingException);
188 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
189 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
190 + "due to the attribute: onboardingState not being set to ONBOARDED.");
193 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");