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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.client;
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;
33 public class DmiRestClient {
35 private RestTemplate restTemplate;
36 private DmiProperties dmiProperties;
38 public DmiRestClient(final RestTemplate restTemplate, final DmiProperties dmiProperties) {
39 this.restTemplate = restTemplate;
40 this.dmiProperties = dmiProperties;
43 public ResponseEntity<Object> putOperationWithJsonData(final String dmiResourceUrl,
44 final String jsonData, final HttpHeaders headers) {
45 final var httpEntity = new HttpEntity<>(jsonData, configureHttpHeaders(headers));
46 return restTemplate.exchange(dmiResourceUrl, HttpMethod.PUT, httpEntity, Object.class);
49 public ResponseEntity<Void> postOperationWithJsonData(final String dmiResourceUrl,
50 final String jsonData, final HttpHeaders headers) {
51 final var httpEntity = new HttpEntity<>(jsonData, configureHttpHeaders(headers));
52 return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Void.class);
55 private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) {
56 httpHeaders.setBasicAuth(dmiProperties.getAuthUsername(), dmiProperties.getAuthPassword());
57 httpHeaders.setContentType(MediaType.APPLICATION_JSON);
61 public ResponseEntity<String> postOperation(final String dmiResourceUrl, final HttpHeaders httpHeaders) {
62 final var httpEntity = new HttpEntity<>(configureHttpHeaders(httpHeaders));
63 return restTemplate.exchange(dmiResourceUrl, HttpMethod.POST, httpEntity, String.class);