Fixing vulnerabilities and code smells
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / extclients / etsicatalog / EtsiCatalogPackageManagementServiceProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.etsicatalog;
21
22 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.etsicatalog.EtsiCatalogServiceProviderConfiguration.ETSI_CATALOG_SERVICE_PROVIDER_BEAN;
23 import java.util.Optional;
24 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.NsdInfo;
25 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.VnfPkgInfo;
26 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.exceptions.EtsiCatalogManagerRequestFailureException;
27 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.NetworkServiceDescriptor;
28 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.parser.NetworkServiceDescriptorParser;
29 import org.onap.so.rest.service.HttpRestServiceProvider;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Qualifier;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Service;
36
37 /**
38  * @author Waqas Ikram (waqas.ikram@est.tech)
39  *
40  */
41 @Service
42 public class EtsiCatalogPackageManagementServiceProviderImpl implements EtsiCatalogPackageManagementServiceProvider {
43
44     private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogPackageManagementServiceProviderImpl.class);
45
46     private final HttpRestServiceProvider httpServiceProvider;
47     private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
48     private final NetworkServiceDescriptorParser networkServiceDescriptorParser;
49
50     @Autowired
51     public EtsiCatalogPackageManagementServiceProviderImpl(final EtsiCatalogUrlProvider etsiCatalogUrlProvider,
52             @Qualifier(ETSI_CATALOG_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider,
53             final NetworkServiceDescriptorParser networkServiceDescriptorParser) {
54         this.etsiCatalogUrlProvider = etsiCatalogUrlProvider;
55         this.httpServiceProvider = httpServiceProvider;
56         this.networkServiceDescriptorParser = networkServiceDescriptorParser;
57     }
58
59     @Override
60     public Optional<NsdInfo> getNSPackageModel(final String nsdId) {
61         try {
62             final ResponseEntity<NsdInfo> response =
63                     httpServiceProvider.getHttpResponse(etsiCatalogUrlProvider.getNsPackageUrl(nsdId), NsdInfo.class);
64             if (response.getStatusCode().is2xxSuccessful()) {
65                 return Optional.ofNullable(response.getBody());
66             }
67             return Optional.empty();
68         } catch (final Exception restProcessingException) {
69             logger.error("Caught exception while getting NS package model for: {}", nsdId, restProcessingException);
70             throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.",
71                     restProcessingException);
72         }
73     }
74
75     @Override
76     public Optional<VnfPkgInfo> getVnfPkgInfo(final String vnfPkgId) {
77         try {
78             final ResponseEntity<VnfPkgInfo> response = httpServiceProvider
79                     .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class);
80             if (response.getStatusCode().is2xxSuccessful()) {
81                 return Optional.ofNullable(response.getBody());
82             }
83             return Optional.empty();
84         } catch (final Exception restProcessingException) {
85             logger.error("Caught exception while getting VNF package model for: {}", vnfPkgId, restProcessingException);
86             throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.",
87                     restProcessingException);
88         }
89     }
90
91     @Override
92     public Optional<NetworkServiceDescriptor> getNetworkServiceDescriptor(final String nsdId) {
93         try {
94             final ResponseEntity<byte[]> response = httpServiceProvider
95                     .getHttpResponse(etsiCatalogUrlProvider.getNsPackageContentUrl(nsdId), byte[].class);
96             if (response.getStatusCode().is2xxSuccessful()) {
97                 if (response.hasBody()) {
98                     return networkServiceDescriptorParser.parse(response.getBody());
99                 }
100                 logger.error("Received response without body ...");
101             }
102             return Optional.empty();
103         } catch (final Exception restProcessingException) {
104             logger.error("Caught exception while getting NS package content for: {}", nsdId, restProcessingException);
105             throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.",
106                     restProcessingException);
107         }
108     }
109
110 }