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;
41 public SubscriptionManager(
42 @Qualifier(SSL_BASED_CONFIGURABLE_REST_TEMPLATE) final RestTemplate restTemplate) {
43 this.restTemplate = restTemplate;
47 * Send subscription request to the vnfm adapter
49 * @param pkgmSubscriptionRequest The subscription request to send
52 public InlineResponse201 createSubscription(final PkgmSubscriptionRequest pkgmSubscriptionRequest) {
53 final byte[] encodedAuth = getBasicAuth(username, password);
54 final String authHeader = "Basic " + new String(encodedAuth);
55 final String uri = Constant.VNFM_ADAPTER_SUBSCRIPTION_ENDPOINT;
56 final HttpHeaders headers = new HttpHeaders();
57 headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
58 headers.add(HttpHeaders.AUTHORIZATION, authHeader);
59 headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
60 final HttpEntity<?> request = new HttpEntity<>(pkgmSubscriptionRequest, headers);
61 LOGGER.info("Creating Subscription using request: {}", pkgmSubscriptionRequest);
62 return restTemplate.exchange(uri, HttpMethod.POST, request, InlineResponse201.class).getBody();
65 private byte[] getBasicAuth(final String username, final String password) {
66 final String auth = username + ":" + password;
67 return Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));