Allow separate registration of DMIDataPlugin and DmiModelPugin
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / client / DmiRestClient.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.client;
22
23 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties;
24 import org.springframework.http.HttpEntity;
25 import org.springframework.http.HttpHeaders;
26 import org.springframework.http.HttpMethod;
27 import org.springframework.http.MediaType;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.stereotype.Component;
30 import org.springframework.web.client.RestTemplate;
31
32 @Component
33 public class DmiRestClient {
34
35     private RestTemplate restTemplate;
36     private DmiProperties dmiProperties;
37
38     /**
39      * Constructor injection for DmiRestClient objects.
40      *
41      * @param restTemplate the rest template
42      * @param dmiProperties the DMI properties
43      */
44     public DmiRestClient(final RestTemplate restTemplate, final DmiProperties dmiProperties) {
45         this.restTemplate = restTemplate;
46         this.dmiProperties = dmiProperties;
47     }
48
49     /**
50      * Sends a PUT operation to DMI with JSON payload.
51      *
52      * @param dmiResourceUrl the DMI resource URL
53      * @param jsonData the JSON payload
54      * @param headers the HTTP headers
55      * @return response entity of type Object
56      */
57     public ResponseEntity<Object> putOperationWithJsonData(final String dmiResourceUrl,
58                                                             final String jsonData, final HttpHeaders headers) {
59         //TODO Toine Siebelink, should we use POST operation below instead (and return a String-Entity!)
60         final var httpEntity = new HttpEntity<>(jsonData, configureHttpHeaders(headers));
61         return restTemplate.exchange(dmiResourceUrl, HttpMethod.PUT, httpEntity, Object.class);
62     }
63
64     /**
65      * Sends POST operation to DMI with json body containing module references.
66      * @param dmiResourceUrl dmi resource url
67      * @param jsonData json data body
68      * @param httpHeaders http headers
69      * @return response entity of type String
70      */
71     public ResponseEntity<String> postOperationWithJsonData(final String dmiResourceUrl,
72                                                             final String jsonData,
73                                                             final HttpHeaders httpHeaders) {
74         final var httpEntity = new HttpEntity<>(jsonData, configureHttpHeaders(httpHeaders));
75         return restTemplate.postForEntity(dmiResourceUrl, httpEntity, String.class);
76     }
77
78     private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) {
79         httpHeaders.setBasicAuth(dmiProperties.getAuthUsername(), dmiProperties.getAuthPassword());
80         httpHeaders.setContentType(MediaType.APPLICATION_JSON);
81         return httpHeaders;
82     }
83
84     /**
85      * Sends POST operation to DMI.
86      * @param dmiResourceUrl dmi resource url
87      * @param httpHeaders http headers
88      * @return response entity of type String
89      */
90     public ResponseEntity<String> postOperation(final String dmiResourceUrl, final HttpHeaders httpHeaders) {
91         final var httpEntity = new HttpEntity<>(configureHttpHeaders(httpHeaders));
92         return restTemplate.exchange(dmiResourceUrl, HttpMethod.POST, httpEntity, String.class);
93     }
94 }