96b01f04e4e99425f3d4fa623891e59566253586
[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.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;
42
43 /**
44  * Provides the implementations of the REST Requests to the ETSI Catalog Manager.
45  * 
46  * @author gareth.roper@est.tech
47  */
48 @Service
49 public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvider {
50     private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogServiceProviderImpl.class);
51
52     @Qualifier("etsiCatalogServiceProvider")
53     private final HttpRestServiceProvider httpServiceProvider;
54     private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
55     private final ConversionService conversionService;
56
57     @Autowired
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;
63     }
64
65     @Override
66     public Optional<byte[]> getVnfPackageContent(final String vnfPkgId)
67             throws EtsiCatalogManagerRequestFailureException {
68         try {
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());
75             }
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.");
85             }
86         }
87         throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
88     }
89
90     @Override
91     public Optional<InlineResponse2001[]> getVnfPackages() {
92         try {
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;
107                             }
108                         }
109                         logger.error("Unable to find Converter for response class: {}", vnfPackages[index].getClass());
110                     }
111                     return Optional.ofNullable(responses);
112                 }
113                 logger.error("Received response without body ...");
114             }
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.");
127         }
128     }
129
130     @Override
131     public Optional<InlineResponse2001> getVnfPackage(final String vnfPkgId) {
132         try {
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));
142                     }
143                     logger.error("Unable to find Converter for response class: {}", vnfPkgInfo.getClass());
144                 }
145                 logger.error("Received response without body ....");
146             }
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.");
158         }
159     }
160 }