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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.asdc.etsi.pkg.processor;
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;
38 * @author Waqas Ikram (waqas.ikram@est.tech)
42 public class SdcResourceProvider {
43 private final static Logger LOGGER = LoggerFactory.getLogger(SdcResourceProvider.class);
45 private static final String SERVICE_NAME = "SO-SDC-CONTROLLER";
47 private final HttpRestServiceProvider httpRestServiceProvider;
49 private final SdcBasicHttpConfigurationProvider sdcBasicHttpConfigurationProvider;
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;
59 public Optional<byte[]> getVnfResource(final String resourceId) {
60 LOGGER.debug("Will get resource from SDC using resource id: {}", resourceId);
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);
69 if (responseEntity.getStatusCode().is2xxSuccessful()) {
70 if (responseEntity.hasBody()) {
71 return Optional.of(responseEntity.getBody());
73 LOGGER.error("Received response without body");
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();
83 private String getSdcResourceEndPoint(final String resourceId) {
84 return sdcBasicHttpConfigurationProvider.getEndPoint() + "/sdc/v1/catalog/resources/" + resourceId
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);