2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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.dmi.service.client;
23 import org.onap.cps.ncmp.dmi.config.DmiConfiguration.SdncProperties;
24 import org.springframework.http.HttpEntity;
25 import org.springframework.http.HttpHeaders;
26 import org.springframework.http.HttpMethod;
27 import org.springframework.http.ResponseEntity;
28 import org.springframework.stereotype.Component;
29 import org.springframework.web.client.RestTemplate;
32 public class SdncRestconfClient {
34 private SdncProperties sdncProperties;
35 private RestTemplate restTemplate;
37 public SdncRestconfClient(final SdncProperties sdncProperties, final RestTemplate restTemplate) {
38 this.sdncProperties = sdncProperties;
39 this.restTemplate = restTemplate;
43 * restconf get operation on sdnc.
45 * @param getResourceUrl sdnc get url
46 * @return the response entity
48 public ResponseEntity<String> getOperation(final String getResourceUrl) {
49 return getOperation(getResourceUrl, new HttpHeaders());
53 * Overloaded restconf get operation on sdnc with http headers.
55 * @param getResourceUrl sdnc get url
56 * @param httpHeaders http headers
57 * @return the response entity
59 public ResponseEntity<String> getOperation(final String getResourceUrl, final HttpHeaders httpHeaders) {
60 return httpOperationWithJsonData(HttpMethod.GET, getResourceUrl, null, httpHeaders);
64 * restconf http operations on sdnc.
66 * @param httpMethod HTTP Method
67 * @param resourceUrl sdnc resource url
68 * @param jsonData json data
69 * @param httpHeaders HTTP Headers
70 * @return response entity
72 public ResponseEntity<String> httpOperationWithJsonData(final HttpMethod httpMethod,
73 final String resourceUrl,
74 final String jsonData,
75 final HttpHeaders httpHeaders) {
76 final String sdncBaseUrl = sdncProperties.getBaseUrl();
77 final String sdncRestconfUrl = sdncBaseUrl.concat(resourceUrl);
78 httpHeaders.setBasicAuth(sdncProperties.getAuthUsername(), sdncProperties.getAuthPassword());
79 final HttpEntity<String> httpEntity;
80 if (jsonData == null) {
81 httpEntity = new HttpEntity<>(httpHeaders);
83 httpEntity = new HttpEntity<>(jsonData, httpHeaders);
85 return restTemplate.exchange(sdncRestconfUrl, httpMethod, httpEntity, String.class);