30d084629c0769d1ca9ade89ec4248fc581252d3
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / extclients / etsicatalog / EtsiCatalogServiceProviderImpl.java
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 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;
46
47 /**
48  * Provides the implementations of the REST Requests to the ETSI Catalog Manager.
49  * 
50  * @author gareth.roper@est.tech
51  */
52 @Service
53 public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvider {
54     private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogServiceProviderImpl.class);
55
56     @Qualifier("etsiCatalogServiceProvider")
57     private final HttpRestServiceProvider httpServiceProvider;
58     private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
59     private final ConversionService conversionService;
60
61     @Autowired
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;
67     }
68
69     @Override
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);
75     }
76
77     @Override
78     public Optional<byte[]> getVnfPackageArtifact(final String vnfPkgId, final String artifactPath) {
79         try {
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());
86             }
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.");
97             }
98         }
99         throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
100     }
101
102     @Override
103     public Optional<InlineResponse2001[]> getVnfPackages() {
104         try {
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;
120                             }
121                         }
122                         logger.error("Unable to find Converter for response class: {}", vnfPackages[index].getClass());
123                     }
124                     return Optional.of(responses);
125                 }
126                 logger.error("Received response without body ...");
127             }
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.");
140         }
141     }
142
143     @Override
144     public Optional<InlineResponse2001> getVnfPackage(final String vnfPkgId) {
145         try {
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));
155                     }
156                     logger.error("Unable to find Converter for response class: {}", vnfPkgInfo.getClass());
157                 }
158                 logger.error("Received response without body ....");
159             }
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.");
171         }
172     }
173
174     @Override
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);
179     }
180
181     @Override
182     public Optional<PkgmSubscription> postSubscription(
183             final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest) {
184         try {
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());
191                 }
192                 logger.error("Received response without body on postSubscription");
193             }
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());
206         }
207     }
208
209     @Override
210     public boolean deleteSubscription(final String subscriptionId) {
211         try {
212             final ResponseEntity<Void> responseEntity = httpServiceProvider
213                     .deleteHttpRequest(etsiCatalogUrlProvider.getSubscriptionUrl() + "/" + subscriptionId, Void.class);
214
215             if (responseEntity.getStatusCode().is2xxSuccessful()) {
216                 logger.info("Subscription with ID: {} has been successfully deleted from the ETSI Catalog Manager",
217                         subscriptionId);
218                 return true;
219             }
220             logger.error("Unexpected Status Code Received on deleteSubscription: {}", responseEntity.getStatusCode());
221             return false;
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.");
231         }
232     }
233
234     @Override
235     public Optional<NsdmSubscription> getSubscription(final String subscriptionId) {
236         try {
237             final ResponseEntity<NsdmSubscription> responseEntity = httpServiceProvider.getHttpResponse(
238                     etsiCatalogUrlProvider.getSubscriptionUrl() + "/" + subscriptionId, NsdmSubscription.class);
239
240             if (responseEntity.getStatusCode().is2xxSuccessful()) {
241                 logger.debug("Found subscription with ID: {} in ETSI Catalog Manager", subscriptionId);
242                 return Optional.ofNullable(responseEntity.getBody());
243             }
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,
251                     exception);
252         }
253         throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
254     }
255
256     private Optional<byte[]> requestVnfElement(final String vnfPkgId, final String vnfRequestUrl,
257             final String vnfRequestName) {
258         try {
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());
264             }
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.");
274             }
275         }
276         throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
277     }
278 }