Implementing Create NS
[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.rest.service.HttpRestServiceProvider;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Qualifier;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.stereotype.Service;
34
35 /**
36  * @author Waqas Ikram (waqas.ikram@est.tech)
37  *
38  */
39 @Service
40 public class EtsiCatalogPackageManagementServiceProviderImpl implements EtsiCatalogPackageManagementServiceProvider {
41
42     private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogPackageManagementServiceProviderImpl.class);
43
44     private final HttpRestServiceProvider httpServiceProvider;
45     private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
46
47     @Autowired
48     public EtsiCatalogPackageManagementServiceProviderImpl(final EtsiCatalogUrlProvider etsiCatalogUrlProvider,
49             @Qualifier(ETSI_CATALOG_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
50         this.etsiCatalogUrlProvider = etsiCatalogUrlProvider;
51         this.httpServiceProvider = httpServiceProvider;
52     }
53
54     @Override
55     public Optional<NsdInfo> getNSPackageModel(final String nsdId) {
56         try {
57             final ResponseEntity<NsdInfo> response =
58                     httpServiceProvider.getHttpResponse(etsiCatalogUrlProvider.getNsPackageUrl(nsdId), NsdInfo.class);
59             if (response.getStatusCode().is2xxSuccessful()) {
60                 return Optional.ofNullable(response.getBody());
61             }
62             return Optional.empty();
63         } catch (final Exception restProcessingException) {
64             logger.error("Caught exception while getting NS package model for: {}", nsdId, restProcessingException);
65             throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.",
66                     restProcessingException);
67         }
68     }
69
70     @Override
71     public Optional<VnfPkgInfo> getVnfPkgInfo(final String vnfPkgId) {
72         try {
73             final ResponseEntity<VnfPkgInfo> response = httpServiceProvider
74                     .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageUrl(vnfPkgId), VnfPkgInfo.class);
75             if (response.getStatusCode().is2xxSuccessful()) {
76                 return Optional.ofNullable(response.getBody());
77             }
78             return Optional.empty();
79         } catch (final Exception restProcessingException) {
80             logger.error("Caught exception while getting VNF package model for: {}", vnfPkgId, restProcessingException);
81             throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.",
82                     restProcessingException);
83         }
84     }
85
86 }