summaryrefslogtreecommitdiffstats
path: root/dcaedt_tools/src/main/java/utilities/DcaeRestClient.java
blob: ee6cdc7681a0cc7c0458350bb73121b97767ee9a (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package utilities;

import json.Credential;
import json.Environment;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest;
import org.onap.sdc.dcae.composition.restmodels.canvas.DcaeComponentCatalog;
import org.onap.sdc.dcae.composition.restmodels.sdc.Resource;
import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component("dcaerestclient")
public class DcaeRestClient implements IDcaeRestClient {

    private static final String GET_RESOURCES_BY_CATEGORY = "/getResourcesByCategory";
    private static final String CREATE_VFCMT = "/createVFCMT";
    private static final String ELEMENTS = "/elements";
	private static final String CATALOG = "/catalog";


    private static final String ECOMP_INSTANCE_ID_HEADER = "X-ECOMP-InstanceID";
    private static final String USER_ID_HEADER = "USER_ID";


    private String uri;
    private RestTemplate client;
    private Credential credential;

    public DcaeRestClient(Credential credential) {
        this.credential = credential;
    }

    @Override
    public String getUserId() {
        return credential.getUsername();
    }

    @PostConstruct
    @Override
    public void init(Environment environment) {
        credential = environment.getCredential();
        CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultHeaders(defaultHeaders(credential)).build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        client = new RestTemplate(requestFactory);
        uri = String.format("%s:%s%s", environment.getDcaeBeHost(), environment.getDcaeBePort(), environment.getApiPath());
    }

    private List<BasicHeader> defaultHeaders(Credential credential) {
        List<BasicHeader> headers = new ArrayList<>();
        headers.add(new BasicHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE));
        headers.add(new BasicHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_VALUE));
        headers.add(new BasicHeader(ECOMP_INSTANCE_ID_HEADER, credential.getUsername()));
        return headers;
    }

    @Override
    public List<ResourceDetailed> getAllVfcmts() {
        String url = buildRequestPath(GET_RESOURCES_BY_CATEGORY);
        return Arrays.asList(client.getForObject(url, ResourceDetailed[].class));
    }

    @Override
    public List<ResourceDetailed> getAllBaseVfcmts() {
        String url = buildRequestPath("/getResourcesByMonitoringTemplateCategory");
        return Arrays.asList(client.getForObject(url, ResourceDetailed[].class));
    }

    @Override
    public ResourceDetailed createResource(CreateVFCMTRequest resource) {
        String url = buildRequestPath(CREATE_VFCMT);
        return client.postForObject(url, new HttpEntity<>(resource, postResourceHeaders(credential.getUsername())), ResourceDetailed.class);
    }

    @Override
    public ResourceDetailed checkoutVfcmt(String vfcmtUuid) {
        String url = buildRequestPath(String.format("/checkout/vfcmt/%s", vfcmtUuid));
        ResponseEntity<ResourceDetailed> resourceDetailedResponse = client.exchange(url, HttpMethod.PUT, new HttpEntity(postResourceHeaders(credential.getUsername())), ResourceDetailed.class);

        return resourceDetailedResponse.getBody();
    }

    @Override
    public ResourceDetailed checkinVfcmt(String vfcmtUuid) {
        String url = buildRequestPath(String.format("/checkin/vfcmt/%s", vfcmtUuid));
        ResponseEntity<ResourceDetailed> resourceDetailedResponse = client.exchange(url, HttpMethod.PUT, new HttpEntity(postResourceHeaders(credential.getUsername())), ResourceDetailed.class);

        return resourceDetailedResponse.getBody();
    }

	@Override
	public Map<String, List<Resource>> getDcaeCatalog() {
		String url = buildRequestPath(CATALOG);
		DcaeComponentCatalog catalog = client.getForObject(url, DcaeComponentCatalog.class);
		return catalog.getElements().stream().collect(Collectors.toMap(DcaeComponentCatalog.SubCategoryFolder::getName, DcaeComponentCatalog.SubCategoryFolder::getItems));
	}


    @Override
    public String getItemModel(String elementId) {
        String url = buildRequestPath("/"+ elementId +"/model");
        return client.getForObject(url, String.class);
    }
    @Override
    public String getItemType(String elementId, String type) {
        String url = buildRequestPath("/"+ elementId +"/type/"+ type +"/");
        return client.getForObject(url, String.class);
    }

    @Override
    public String saveComposition(String componentId, String body) {
        String url = buildRequestPath("/saveComposition/" + componentId);
        ResponseEntity<String> resourceDetailedResponse = client.exchange(url, HttpMethod.POST, new HttpEntity<>(body, postResourceHeaders(credential.getUsername())), String.class);

        return resourceDetailedResponse.getBody();
    }

    @Override
    public String certifyVfcmt(String vfcmtUuid) {
        String url = buildRequestPath(String.format("/certify/vfcmt/%s", vfcmtUuid));
        ResponseEntity<String> resourceDetailedResponse = client.exchange(url, HttpMethod.PUT, new HttpEntity(postResourceHeaders(credential.getUsername())), String.class);

        return resourceDetailedResponse.getBody();
    }

    private HttpHeaders postResourceHeaders(String userId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.add(USER_ID_HEADER, userId);
        return headers;
    }

    private String buildRequestPath(String... args){
        return uri + Stream.of(args).collect(Collectors.joining());
    }

    @Override
    public void updateResource(ResourceDetailed vfcmt) {
        // Do nothing
    }
}