6a8310c2079482543866e36d0870b901f2a97b0a
[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-2023 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 com.fasterxml.jackson.databind.JsonNode;
25 import lombok.AllArgsConstructor;
26 import lombok.extern.slf4j.Slf4j;
27 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties;
28 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException;
29 import org.onap.cps.ncmp.api.impl.operations.OperationType;
30 import org.onap.cps.ncmp.api.impl.trustlevel.dmiavailability.DmiPluginStatus;
31 import org.springframework.http.HttpEntity;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Component;
36 import org.springframework.web.client.HttpStatusCodeException;
37 import org.springframework.web.client.RestTemplate;
38
39 @Component
40 @AllArgsConstructor
41 @Slf4j
42 public class DmiRestClient {
43
44     private RestTemplate restTemplate;
45     private DmiProperties dmiProperties;
46
47     /**
48      * Sends POST operation to DMI with json body containing module references.
49      * @param dmiResourceUrl dmi resource url
50      * @param requestBodyAsJsonString json data body
51      * @param operationType the type of operation being executed (for error reporting only)
52      * @return response entity of type String
53      */
54     public ResponseEntity<Object> postOperationWithJsonData(final String dmiResourceUrl,
55                                                             final String requestBodyAsJsonString,
56                                                             final OperationType operationType) {
57         final var httpEntity = new HttpEntity<>(requestBodyAsJsonString, configureHttpHeaders(new HttpHeaders()));
58         try {
59             return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Object.class);
60         } catch (final HttpStatusCodeException httpStatusCodeException) {
61             final String exceptionMessage = "Unable to " + operationType.toString() + " resource data.";
62             throw new HttpClientRequestException(exceptionMessage, httpStatusCodeException.getResponseBodyAsString(),
63                     httpStatusCodeException.getStatusCode().value());
64         }
65     }
66
67     /**
68      * Sends GET operation to DMI plugin's health check URL.
69      *
70      * @param       dmiPluginBaseUrl the base URL of the dmi-plugin
71      * @return      DmiPluginStatus as UP or DOWN
72      */
73     public DmiPluginStatus getDmiPluginStatus(final String dmiPluginBaseUrl) {
74         try {
75             final HttpEntity<Object> httpHeaders = new HttpEntity<>(configureHttpHeaders(new HttpHeaders()));
76             final JsonNode dmiPluginHealthStatus = restTemplate.getForObject(dmiPluginBaseUrl + "/manage/health",
77                     JsonNode.class, httpHeaders);
78             if (dmiPluginHealthStatus != null) {
79                 if (dmiPluginHealthStatus.get("status").asText().equals("UP")) {
80                     return DmiPluginStatus.UP;
81                 }
82             }
83         } catch (final Exception exception) {
84             log.warn("Could not send request for health check since {}", exception.getMessage());
85         }
86         return DmiPluginStatus.DOWN;
87     }
88
89     private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) {
90         if (dmiProperties.isDmiBasicAuthEnabled()) {
91             httpHeaders.setBasicAuth(dmiProperties.getAuthUsername(), dmiProperties.getAuthPassword());
92         }
93         httpHeaders.setContentType(MediaType.APPLICATION_JSON);
94         return httpHeaders;
95     }
96 }