1a48494e1a7752292b14fb7aef15a8ca0e929d72
[so.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.extclients.etsicatalog;
22
23 import java.util.Optional;
24 import javax.swing.text.html.Option;
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.*;
29 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
30 import org.onap.so.rest.exceptions.InvalidRestRequestException;
31 import org.onap.so.rest.exceptions.RestProcessingException;
32 import org.onap.so.rest.service.HttpRestServiceProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Qualifier;
37 import org.springframework.core.convert.ConversionService;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Service;
41
42 /**
43  * Provides the implementations of the REST Requests to the ETSI Catalog Manager.
44  * 
45  * @author gareth.roper@est.tech
46  */
47 @Service
48 public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvider {
49     private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogServiceProviderImpl.class);
50
51     @Qualifier("etsiCatalogServiceProvider")
52     private final HttpRestServiceProvider httpServiceProvider;
53     private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
54     private final ConversionService conversionService;
55
56     @Autowired
57     public EtsiCatalogServiceProviderImpl(final EtsiCatalogUrlProvider etsiCatalogUrlProvider,
58             final HttpRestServiceProvider httpServiceProvider, final ConversionService conversionService) {
59         this.etsiCatalogUrlProvider = etsiCatalogUrlProvider;
60         this.httpServiceProvider = httpServiceProvider;
61         this.conversionService = conversionService;
62     }
63
64     @Override
65     public Optional<byte[]> getVnfPackageContent(final String vnfPkgId)
66             throws EtsiCatalogManagerRequestFailureException {
67         final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageContentUrl(vnfPkgId);
68         final String vnfRequestName = "getVnfPackageContent";
69         return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
70     }
71
72     @Override
73     public Optional<byte[]> getVnfPackageArtifact(final String vnfPkgId, final String artifactPath) {
74         try {
75             final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(
76                     etsiCatalogUrlProvider.getVnfPackageArtifactUrl(vnfPkgId, artifactPath), byte[].class);
77             logger.info("getVnfPackageArtifact Request to ETSI Catalog Manager Status Code: {}",
78                     response.getStatusCodeValue());
79             if (response.getStatusCode() == HttpStatus.OK) {
80                 return Optional.ofNullable(response.getBody());
81             }
82         } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
83             logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
84             throw new VnfPkgNotFoundException("No Vnf Package Artifact found with vnfPkgId: \"" + vnfPkgId
85                     + "\" and artifactPath: \"" + artifactPath + "\".");
86         } catch (final RestProcessingException restProcessingException) {
87             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
88                     restProcessingException);
89             if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
90                 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
91                         + "due to the attribute: onboardingState not being set to ONBOARDED.");
92             }
93         }
94         throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
95     }
96
97     @Override
98     public Optional<InlineResponse2001[]> getVnfPackages() {
99         try {
100             final ResponseEntity<VnfPkgInfo[]> response =
101                     httpServiceProvider.getHttpResponse(etsiCatalogUrlProvider.getVnfPackagesUrl(), VnfPkgInfo[].class);
102             logger.info("getVnfPackages Request to ETSI Catalog Manager Status Code: {}",
103                     response.getStatusCodeValue());
104             if (response.getStatusCode() == HttpStatus.OK) {
105                 if (response.hasBody()) {
106                     final VnfPkgInfo[] vnfPackages = response.getBody();
107                     assert (vnfPackages != null);
108                     final InlineResponse2001[] responses = new InlineResponse2001[vnfPackages.length];
109                     for (int index = 0; index < vnfPackages.length; index++) {
110                         if (conversionService.canConvert(vnfPackages[index].getClass(), InlineResponse2001.class)) {
111                             final InlineResponse2001 inlineResponse2001 =
112                                     conversionService.convert(vnfPackages[index], InlineResponse2001.class);
113                             if (inlineResponse2001 != null) {
114                                 responses[index] = inlineResponse2001;
115                             }
116                         }
117                         logger.error("Unable to find Converter for response class: {}", vnfPackages[index].getClass());
118                     }
119                     return Optional.of(responses);
120                 }
121                 logger.error("Received response without body ...");
122             }
123             logger.error("Unexpected status code received {}", response.getStatusCode());
124             return Optional.empty();
125         } catch (final InvalidRestRequestException invalidRestRequestException) {
126             logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
127             throw new VnfPkgBadRequestException("Error: Bad Request Received");
128         } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
129             logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
130             throw new VnfPkgNotFoundException("No Vnf Packages found");
131         } catch (final RestProcessingException restProcessingException) {
132             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
133                     restProcessingException);
134             throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
135         }
136     }
137
138     @Override
139     public Optional<InlineResponse2001> getVnfPackage(final String vnfPkgId) {
140         try {
141             final ResponseEntity<VnfPkgInfo> response = httpServiceProvider
142                     .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class);
143             logger.info("getVnfPackage Request for vnfPkgId {} to ETSI Catalog Manager Status Code: {}", vnfPkgId,
144                     response.getStatusCodeValue());
145             if (response.getStatusCode() == HttpStatus.OK) {
146                 if (response.hasBody()) {
147                     final VnfPkgInfo vnfPkgInfo = response.getBody();
148                     if (conversionService.canConvert(vnfPkgInfo.getClass(), InlineResponse2001.class)) {
149                         return Optional.ofNullable(conversionService.convert(vnfPkgInfo, InlineResponse2001.class));
150                     }
151                     logger.error("Unable to find Converter for response class: {}", vnfPkgInfo.getClass());
152                 }
153                 logger.error("Received response without body ....");
154             }
155             return Optional.empty();
156         } catch (final InvalidRestRequestException invalidRestRequestException) {
157             logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
158             throw new VnfPkgBadRequestException("Error: Bad Request Received");
159         } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
160             logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
161             throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
162         } catch (final RestProcessingException restProcessingException) {
163             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
164                     restProcessingException);
165             throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
166         }
167     }
168
169     @Override
170     public Optional<byte[]> getVnfPackageVnfd(final String vnfPkgId) {
171         final String vnfRequestUrl = etsiCatalogUrlProvider.getVnfPackageVnfdUrl(vnfPkgId);
172         final String vnfRequestName = "getVnfPackageVnfd";
173         return requestVnfElement(vnfPkgId, vnfRequestUrl, vnfRequestName);
174     }
175
176     @Override
177     public Optional<PkgmSubscription> postSubscription(
178             final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest) {
179         try {
180             final ResponseEntity<PkgmSubscription> responseEntity =
181                     httpServiceProvider.postHttpRequest(etsiCatalogManagerSubscriptionRequest,
182                             etsiCatalogUrlProvider.getSubscriptionUrl(), PkgmSubscription.class);
183             if (responseEntity.getStatusCode() == HttpStatus.OK) {
184                 if (responseEntity.hasBody()) {
185                     return Optional.of(responseEntity.getBody());
186                 }
187                 logger.error("Received response without body on postSubscription");
188             }
189             logger.error("Unexpected Status Code Received on postSubscription: {}", responseEntity.getStatusCode());
190             return Optional.empty();
191         } catch (final InvalidRestRequestException invalidRestRequestException) {
192             logger.error("Caught InvalidRestRequestException", invalidRestRequestException);
193             throw new EtsiCatalogManagerBadRequestException("Bad Request Received on postSubscription call.");
194         } catch (final RestProcessingException restProcessingException) {
195             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
196                     restProcessingException);
197             throw new EtsiCatalogManagerRequestFailureException(
198                     "Internal Server Error Occurred. On postSubscription with StatusCode: "
199                             + restProcessingException.getStatusCode());
200         }
201     }
202
203     private Optional<byte[]> requestVnfElement(final String vnfPkgId, final String vnfRequestUrl,
204             final String vnfRequestName) {
205         try {
206             final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(vnfRequestUrl, byte[].class);
207             logger.info("{} Request to ETSI Catalog Manager Status Code: {}", vnfRequestName,
208                     response.getStatusCodeValue());
209             if (response.getStatusCode() == HttpStatus.OK) {
210                 return Optional.ofNullable(response.getBody());
211             }
212         } catch (final HttpResouceNotFoundException httpResouceNotFoundException) {
213             logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
214             throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
215         } catch (final RestProcessingException restProcessingException) {
216             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
217                     restProcessingException);
218             if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
219                 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
220                         + "due to the attribute: onboardingState not being set to ONBOARDED.");
221             }
222         }
223         throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
224     }
225 }