aboutsummaryrefslogtreecommitdiffstats
path: root/plans/so/integration-etsi-testing/so-simulators/vnfm-simulator/vnfm-service/src/main/java/org/onap/so/svnfm/simulator/services/SubscriptionManager.java
blob: d886da9b8bcae12ab755300088ad131845646756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package org.onap.so.svnfm.simulator.services;

import static org.onap.so.svnfm.simulator.config.SslBasedRestTemplateConfiguration.SSL_BASED_CONFIGURABLE_REST_TEMPLATE;

import java.nio.charset.StandardCharsets;
import javax.ws.rs.core.MediaType;
import org.apache.commons.codec.binary.Base64;
import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse201;
import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest;
import org.onap.so.svnfm.simulator.constants.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author Eoin Hanan (eoin.hanan@est.tech)
 * @author Gareth Roper (gareth.roper@est.tech)
 */
@Service
public class SubscriptionManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionManager.class);

    private final RestTemplate restTemplate;

    @Value("${vnfm-adapter.auth.name}")
    private String username;

    @Value("${vnfm-adapter.auth.password}")
    private String password;

    @Value("${vnfm-adapter.base.endpoint:http://so-vnfm-adapter.onap:9092}")
    private String baseEndpoint;

    @Autowired
    public SubscriptionManager(
            @Qualifier(SSL_BASED_CONFIGURABLE_REST_TEMPLATE) final RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * Send subscription request to the vnfm adapter
     *
     * @param pkgmSubscriptionRequest The subscription request to send
     * @return
     */
    public InlineResponse201 createSubscription(final PkgmSubscriptionRequest pkgmSubscriptionRequest) {
        final byte[] encodedAuth = getBasicAuth(username, password);
        final String authHeader = "Basic " + new String(encodedAuth);
        final String uri = baseEndpoint + Constant.VNFM_ADAPTER_SUBSCRIPTION_ENDPOINT;
        final HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
        headers.add(HttpHeaders.AUTHORIZATION, authHeader);
        headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
        final HttpEntity<?> request = new HttpEntity<>(pkgmSubscriptionRequest, headers);
        LOGGER.info("Creating Subscription using request: {}", pkgmSubscriptionRequest);
        return restTemplate.exchange(uri, HttpMethod.POST, request, InlineResponse201.class).getBody();
    }

    private byte[] getBasicAuth(final String username, final String password) {
        final String auth = username + ":" + password;
        return Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
    }

}