179707ab66ebefa43dda9a2694c15aad1744c42e
[cps/ncmp-dmi-plugin.git] / src / main / java / org / onap / cps / ncmp / dmi / service / client / SdncRestconfClient.java
1 /*
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
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.dmi.service.client;
22
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;
30
31 @Component
32 public class SdncRestconfClient {
33
34     private SdncProperties sdncProperties;
35     private RestTemplate restTemplate;
36
37     public SdncRestconfClient(final SdncProperties sdncProperties, final RestTemplate restTemplate) {
38         this.sdncProperties = sdncProperties;
39         this.restTemplate = restTemplate;
40     }
41
42     /**
43      * restconf get operation on sdnc.
44      *
45      * @param getResourceUrl sdnc get url
46      * @return the response entity
47      */
48     public ResponseEntity<String> getOperation(final String getResourceUrl) {
49         return getOperation(getResourceUrl, new HttpHeaders());
50     }
51
52     /**
53      * Overloaded restconf get operation on sdnc with http headers.
54      *
55      * @param getResourceUrl sdnc get url
56      * @param httpHeaders    http headers
57      * @return the response entity
58      */
59     public ResponseEntity<String> getOperation(final String getResourceUrl, final HttpHeaders httpHeaders) {
60         return  httpOperationWithJsonData(HttpMethod.GET, getResourceUrl, null, httpHeaders);
61     }
62
63     /**
64      * restconf http operations on sdnc.
65      *
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
71      */
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);
82         } else {
83             httpEntity = new HttpEntity<>(jsonData, httpHeaders);
84         }
85         return restTemplate.exchange(sdncRestconfUrl, httpMethod, httpEntity, String.class);
86     }
87 }