Merge "Removing so-monitoring module"
[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             final String message = "Caught exception while getting NS package model for: " + nsdId;
70             throw new EtsiCatalogManagerRequestFailureException(message, restProcessingException);
71         }
72     }
73
74     @Override
75     public Optional<VnfPkgInfo> getVnfPkgInfo(final String vnfPkgId) {
76         try {
77             final ResponseEntity<VnfPkgInfo> response = httpServiceProvider
78                     .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class);
79             if (response.getStatusCode().is2xxSuccessful()) {
80                 return Optional.ofNullable(response.getBody());
81             }
82             return Optional.empty();
83         } catch (final Exception restProcessingException) {
84             final String message = "Caught exception while getting VNF package model for: " + vnfPkgId;
85             throw new EtsiCatalogManagerRequestFailureException(message, restProcessingException);
86         }
87     }
88
89     @Override
90     public Optional<NetworkServiceDescriptor> getNetworkServiceDescriptor(final String nsdId) {
91         try {
92             final ResponseEntity<byte[]> response = httpServiceProvider
93                     .getHttpResponse(etsiCatalogUrlProvider.getNsPackageContentUrl(nsdId), byte[].class);
94             if (response.getStatusCode().is2xxSuccessful()) {
95                 if (response.hasBody()) {
96                     return networkServiceDescriptorParser.parse(response.getBody());
97                 }
98                 logger.error("Received response without body ...");
99             }
100             return Optional.empty();
101         } catch (final Exception restProcessingException) {
102             final String message = "Caught exception while getting NS package content for: " + nsdId;
103             throw new EtsiCatalogManagerRequestFailureException(message, restProcessingException);
104         }
105     }
106
107 }