0cd0aba9c368516a7dea9acec50dbf1877ba3ae0
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / etsi / pkg / processor / SdcResourceProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Ericsson. All rights reserved.
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.asdc.etsi.pkg.processor;
21
22 import static org.onap.so.asdc.etsi.pkg.processor.HttpRestServiceProviderConfiguration.SDC_HTTP_REST_SERVICE_PROVIDER_BEAN;
23 import static org.springframework.http.HttpHeaders.ACCEPT;
24 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
25 import java.security.GeneralSecurityException;
26 import java.util.Optional;
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.HttpHeaders;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Service;
35
36
37 /**
38  * @author Waqas Ikram (waqas.ikram@est.tech)
39  *
40  */
41 @Service
42 public class SdcResourceProvider {
43     private final static Logger LOGGER = LoggerFactory.getLogger(SdcResourceProvider.class);
44
45     private static final String SERVICE_NAME = "SO-SDC-CONTROLLER";
46
47     private final HttpRestServiceProvider httpRestServiceProvider;
48
49     private final SdcBasicHttpConfigurationProvider sdcBasicHttpConfigurationProvider;
50
51     @Autowired
52     public SdcResourceProvider(
53             @Qualifier(SDC_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpRestServiceProvider,
54             final SdcBasicHttpConfigurationProvider sdcBasicHttpConfigurationProvider) {
55         this.httpRestServiceProvider = httpRestServiceProvider;
56         this.sdcBasicHttpConfigurationProvider = sdcBasicHttpConfigurationProvider;
57     }
58
59     public Optional<byte[]> getVnfResource(final String resourceId) {
60         LOGGER.debug("Will get resource from SDC using resource id: {}", resourceId);
61         try {
62             final HttpHeaders headers = getHttpHeaders();
63             headers.add(ACCEPT, APPLICATION_OCTET_STREAM_VALUE);
64             final String url = getSdcResourceEndPoint(resourceId);
65             LOGGER.debug("will invoke url: {} to get resource ", url);
66             final ResponseEntity<byte[]> responseEntity =
67                     httpRestServiceProvider.getHttpResponse(url, headers, byte[].class);
68
69             if (responseEntity.getStatusCode().is2xxSuccessful()) {
70                 if (responseEntity.hasBody()) {
71                     return Optional.of(responseEntity.getBody());
72                 }
73                 LOGGER.error("Received response without body");
74             }
75             LOGGER.error("Unexpected Status code received : {}", responseEntity.getStatusCode());
76             return Optional.empty();
77         } catch (final Exception exception) {
78             LOGGER.error("Unable to get {} resource from SDC", resourceId, exception);
79             return Optional.empty();
80         }
81     }
82
83     private String getSdcResourceEndPoint(final String resourceId) {
84         return sdcBasicHttpConfigurationProvider.getEndPoint() + "/sdc/v1/catalog/resources/" + resourceId
85                 + "/toscaModel";
86     }
87
88
89     private HttpHeaders getHttpHeaders() throws GeneralSecurityException {
90         final HttpHeaders headers = new HttpHeaders();
91         headers.add(HttpHeaders.AUTHORIZATION, sdcBasicHttpConfigurationProvider.getBasicAuthorization());
92         headers.add("X-ECOMP-InstanceID", SERVICE_NAME);
93         headers.add("X-FromAppId", SERVICE_NAME);
94         return headers;
95     }
96 }