1 package org.onap.so.svnfm.simulator.services;
3 import static org.onap.so.svnfm.simulator.config.SslBasedRestTemplateConfiguration.SSL_BASED_CONFIGURABLE_REST_TEMPLATE;
5 import java.nio.charset.StandardCharsets;
6 import javax.ws.rs.core.MediaType;
7 import org.apache.commons.codec.binary.Base64;
8 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse201;
9 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest;
10 import org.onap.so.svnfm.simulator.constants.Constant;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.beans.factory.annotation.Qualifier;
15 import org.springframework.beans.factory.annotation.Value;
16 import org.springframework.http.HttpEntity;
17 import org.springframework.http.HttpHeaders;
18 import org.springframework.http.HttpMethod;
19 import org.springframework.stereotype.Service;
20 import org.springframework.web.client.RestTemplate;
24 * @author Eoin Hanan (eoin.hanan@est.tech)
25 * @author Gareth Roper (gareth.roper@est.tech)
28 public class SubscriptionManager {
30 private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionManager.class);
32 private final RestTemplate restTemplate;
34 @Value("${vnfm-adapter.auth.name}")
35 private String username;
37 @Value("${vnfm-adapter.auth.password}")
38 private String password;
40 @Value("${vnfm-adapter.base.endpoint:http://so-vnfm-adapter.onap:9092}")
41 private String baseEndpoint;
44 public SubscriptionManager(
45 @Qualifier(SSL_BASED_CONFIGURABLE_REST_TEMPLATE) final RestTemplate restTemplate) {
46 this.restTemplate = restTemplate;
50 * Send subscription request to the vnfm adapter
52 * @param pkgmSubscriptionRequest The subscription request to send
55 public InlineResponse201 createSubscription(final PkgmSubscriptionRequest pkgmSubscriptionRequest) {
56 final byte[] encodedAuth = getBasicAuth(username, password);
57 final String authHeader = "Basic " + new String(encodedAuth);
58 final String uri = baseEndpoint + Constant.VNFM_ADAPTER_SUBSCRIPTION_ENDPOINT;
59 final HttpHeaders headers = new HttpHeaders();
60 headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
61 headers.add(HttpHeaders.AUTHORIZATION, authHeader);
62 headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
63 final HttpEntity<?> request = new HttpEntity<>(pkgmSubscriptionRequest, headers);
64 LOGGER.info("Creating Subscription using request: {}", pkgmSubscriptionRequest);
65 return restTemplate.exchange(uri, HttpMethod.POST, request, InlineResponse201.class).getBody();
68 private byte[] getBasicAuth(final String username, final String password) {
69 final String auth = username + ":" + password;
70 return Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));