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.EtsiCatalogManagerRequestFailureException;
27 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgBadRequestException;
28 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgConflictException;
29 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgNotFoundException;
30 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
31 import org.onap.so.rest.exceptions.InvalidRestRequestException;
32 import org.onap.so.rest.exceptions.RestProcessingException;
33 import org.onap.so.rest.service.HttpRestServiceProvider;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Qualifier;
38 import org.springframework.core.convert.ConversionService;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.stereotype.Service;
44 * Provides the implementations of the REST Requests to the ETSI Catalog Manager.
46 * @author gareth.roper@est.tech
49 public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvider {
50 private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogServiceProviderImpl.class);
52 @Qualifier("etsiCatalogServiceProvider")
53 private final HttpRestServiceProvider httpServiceProvider;
54 private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
55 private final ConversionService conversionService;
58 public EtsiCatalogServiceProviderImpl(final EtsiCatalogUrlProvider etsiCatalogUrlProvider,
59 final HttpRestServiceProvider httpServiceProvider, final ConversionService conversionService) {
60 this.etsiCatalogUrlProvider = etsiCatalogUrlProvider;
61 this.httpServiceProvider = httpServiceProvider;
62 this.conversionService = conversionService;
66 public Optional<byte[]> getVnfPackageContent(final String vnfPkgId)
67 throws EtsiCatalogManagerRequestFailureException {
69 final ResponseEntity<byte[]> response = httpServiceProvider
70 .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageContentUrl(vnfPkgId), byte[].class);
71 logger.info("getVnfPackageContent Request to ETSI Catalog Manager Status Code: {}",
72 response.getStatusCodeValue());
73 if (response.getStatusCode() == HttpStatus.OK) {
74 return Optional.ofNullable(response.getBody());
76 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
77 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
78 throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
79 } catch (final RestProcessingException restProcessingException) {
80 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
81 restProcessingException);
82 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
83 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
84 + "due to the attribute: onboardingState not being set to ONBOARDED.");
87 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
91 public Optional<InlineResponse2001[]> getVnfPackages() {
93 final ResponseEntity<VnfPkgInfo[]> response =
94 httpServiceProvider.getHttpResponse(etsiCatalogUrlProvider.getVnfPackagesUrl(), VnfPkgInfo[].class);
95 logger.info("getVnfPackages Request to ETSI Catalog Manager Status Code: {}",
96 response.getStatusCodeValue());
97 if (response.getStatusCode() == HttpStatus.OK) {
98 if (response.hasBody()) {
99 final VnfPkgInfo[] vnfPackages = response.getBody();
100 final InlineResponse2001[] responses = new InlineResponse2001[vnfPackages.length];
101 for (int index = 0; index < vnfPackages.length; index++) {
102 if (conversionService.canConvert(vnfPackages[index].getClass(), InlineResponse2001.class)) {
103 final InlineResponse2001 inlineResponse2001 =
104 conversionService.convert(vnfPackages[index], InlineResponse2001.class);
105 if (inlineResponse2001 != null) {
106 responses[index] = inlineResponse2001;
109 logger.error("Unable to find Converter for response class: {}", vnfPackages[index].getClass());
111 return Optional.ofNullable(responses);
113 logger.error("Received response without body ...");
115 logger.error("Unexpected status code received {}", response.getStatusCode());
116 return Optional.empty();
117 } catch (final InvalidRestRequestException invalidRestRequestException) {
118 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
119 throw new VnfPkgBadRequestException("Error: Bad Request Received");
120 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
121 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
122 throw new VnfPkgNotFoundException("No Vnf Packages found");
123 } catch (final RestProcessingException restProcessingException) {
124 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
125 restProcessingException);
126 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
131 public Optional<InlineResponse2001> getVnfPackage(final String vnfPkgId) {
133 final ResponseEntity<VnfPkgInfo> response = httpServiceProvider
134 .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class);
135 logger.info("getVnfPackage Request for vnfPkgId {} to ETSI Catalog Manager Status Code: {}", vnfPkgId,
136 response.getStatusCodeValue());
137 if (response.getStatusCode() == HttpStatus.OK) {
138 if (response.hasBody()) {
139 final VnfPkgInfo vnfPkgInfo = response.getBody();
140 if (conversionService.canConvert(vnfPkgInfo.getClass(), InlineResponse2001.class)) {
141 return Optional.ofNullable(conversionService.convert(vnfPkgInfo, InlineResponse2001.class));
143 logger.error("Unable to find Converter for response class: {}", vnfPkgInfo.getClass());
145 logger.error("Received response without body ....");
147 return Optional.empty();
148 } catch (final InvalidRestRequestException invalidRestRequestException) {
149 logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
150 throw new VnfPkgBadRequestException("Error: Bad Request Received");
151 } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
152 logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
153 throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
154 } catch (final RestProcessingException restProcessingException) {
155 logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
156 restProcessingException);
157 throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");