[cps] Fix getResourceDataForPassthroughOperational endpoint
[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  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.impl.client;
23
24 import lombok.AllArgsConstructor;
25 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties;
26 import org.springframework.http.HttpEntity;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.MediaType;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.stereotype.Component;
31 import org.springframework.web.client.RestTemplate;
32
33 @Component
34 @AllArgsConstructor
35 public class DmiRestClient {
36
37     private RestTemplate restTemplate;
38     private DmiProperties dmiProperties;
39
40
41     /**
42      * Sends POST operation to DMI with json body containing module references.
43      * @param dmiResourceUrl dmi resource url
44      * @param jsonData json data body
45      * @return response entity of type String
46      */
47     public ResponseEntity<Object> postOperationWithJsonData(final String dmiResourceUrl,
48                                                             final String jsonData) {
49         final var httpEntity = new HttpEntity<>(jsonData, configureHttpHeaders(new HttpHeaders()));
50         return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Object.class);
51     }
52
53     private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) {
54         httpHeaders.setBasicAuth(dmiProperties.getAuthUsername(), dmiProperties.getAuthPassword());
55         httpHeaders.setContentType(MediaType.APPLICATION_JSON);
56         return httpHeaders;
57     }
58 }