5e69661dd305eebd68f03b033d06bf83263b172d
[so/adapters/so-cnf-adapter.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.cnfm.lcm.bpmn.flows.extclients.sdc;
21
22 import static org.onap.so.cnfm.lcm.bpmn.flows.extclients.sdc.SdcHttpRestServiceProviderConfiguration.SDC_HTTP_REST_SERVICE_PROVIDER_BEAN;
23 import java.util.Optional;
24 import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.SdcPackageRequestFailureException;
25 import org.onap.so.rest.service.HttpRestServiceProvider;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.beans.factory.annotation.Qualifier;
30 import org.springframework.http.HttpHeaders;
31 import org.springframework.http.MediaType;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.stereotype.Service;
34
35 /**
36  *
37  * @author Waqas Ikram (waqas.ikram@est.tech)
38  *
39  */
40 @Service
41 public class SdcPackageProviderImpl implements SdcPackageProvider {
42     private static final Logger logger = LoggerFactory.getLogger(SdcPackageProviderImpl.class);
43     private final SdcClientConfigurationProvider sdcClientConfigurationProvider;
44     private final HttpRestServiceProvider httpServiceProvider;
45     private static final String SERVICE_NAME = "SO-CNFM";
46
47     @Autowired
48     public SdcPackageProviderImpl(final SdcClientConfigurationProvider sdcClientConfigurationProvider,
49             @Qualifier(SDC_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
50         this.sdcClientConfigurationProvider = sdcClientConfigurationProvider;
51         this.httpServiceProvider = httpServiceProvider;
52     }
53
54     @Override
55     public Optional<byte[]> getSdcResourcePackage(final String packageId) {
56         try {
57             final HttpHeaders headers = new HttpHeaders();
58             headers.add(HttpHeaders.AUTHORIZATION, sdcClientConfigurationProvider.getBasicAuth());
59             headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_VALUE);
60             headers.add("X-ECOMP-InstanceID", SERVICE_NAME);
61             headers.add("X-FromAppId", SERVICE_NAME);
62
63             logger.info("Will retrieve resource package with id: {} from SDC", packageId);
64             final String url = sdcClientConfigurationProvider.getSdcPackageUrl(packageId);
65
66             final ResponseEntity<byte[]> response = httpServiceProvider.getHttpResponse(url, headers, byte[].class);
67             if (response.getStatusCode().is2xxSuccessful()) {
68                 if (response.hasBody()) {
69                     return Optional.of(response.getBody());
70                 }
71                 logger.error("Received response without body ...");
72             }
73             return Optional.empty();
74         } catch (final Exception restProcessingException) {
75             final String message = "Caught exception while getting resource package content for: " + packageId;
76             throw new SdcPackageRequestFailureException(message, restProcessingException);
77         }
78     }
79
80 }