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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl.client;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import lombok.RequiredArgsConstructor;
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.springframework.http.HttpEntity;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.MediaType;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Component;
35 import org.springframework.web.client.HttpStatusCodeException;
36 import org.springframework.web.client.RestTemplate;
39 @RequiredArgsConstructor
41 public class DmiRestClient {
43 private static final String HEALTH_CHECK_URL_EXTENSION = "/actuator/health";
44 private static final String EMPTY_STRING = "";
45 private final RestTemplate restTemplate;
46 private final DmiProperties dmiProperties;
49 * Sends POST operation to DMI with json body containing module references.
51 * @param dmiResourceUrl dmi resource url
52 * @param requestBodyAsJsonString json data body
53 * @param operationType the type of operation being executed (for error reporting only)
54 * @return response entity of type String
56 public ResponseEntity<Object> postOperationWithJsonData(final String dmiResourceUrl,
57 final String requestBodyAsJsonString,
58 final OperationType operationType) {
59 final var httpEntity = new HttpEntity<>(requestBodyAsJsonString, configureHttpHeaders(new HttpHeaders()));
61 return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Object.class);
62 } catch (final HttpStatusCodeException httpStatusCodeException) {
63 final String exceptionMessage = "Unable to " + operationType.toString() + " resource data.";
64 throw new HttpClientRequestException(exceptionMessage, httpStatusCodeException.getResponseBodyAsString(),
65 httpStatusCodeException.getStatusCode().value());
70 * Get DMI plugin health status.
72 * @param dmiPluginBaseUrl the base URL of the dmi-plugin
73 * @return plugin health status ("UP" is all OK, EMPTY_STRING in case of any exception)
75 public String getDmiHealthStatus(final String dmiPluginBaseUrl) {
76 final HttpEntity<Object> httpHeaders = new HttpEntity<>(configureHttpHeaders(new HttpHeaders()));
78 final JsonNode responseHealthStatus =
79 restTemplate.getForObject(dmiPluginBaseUrl + HEALTH_CHECK_URL_EXTENSION,
80 JsonNode.class, httpHeaders);
81 return responseHealthStatus == null ? EMPTY_STRING :
82 responseHealthStatus.get("status").asText();
83 } catch (final Exception e) {
84 log.warn("Failed to retrieve health status from {}. Error Message: {}", dmiPluginBaseUrl, e.getMessage());
89 private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) {
90 if (dmiProperties.isDmiBasicAuthEnabled()) {
91 httpHeaders.setBasicAuth(dmiProperties.getAuthUsername(), dmiProperties.getAuthPassword());
93 httpHeaders.setContentType(MediaType.APPLICATION_JSON);