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 java.util.Locale;
26 import lombok.RequiredArgsConstructor;
27 import lombok.extern.slf4j.Slf4j;
28 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties;
29 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException;
30 import org.onap.cps.ncmp.api.impl.operations.OperationType;
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;
40 @RequiredArgsConstructor
42 public class DmiRestClient {
44 private static final String HEALTH_CHECK_URL_EXTENSION = "/actuator/health";
45 private static final String NOT_SPECIFIED = "";
46 private final RestTemplate restTemplate;
47 private final DmiProperties dmiProperties;
50 * Sends POST operation to DMI with json body containing module references.
52 * @param dmiResourceUrl dmi resource url
53 * @param requestBodyAsJsonString json data body
54 * @param operationType the type of operation being executed (for error reporting only)
55 * @param authorization contents of Authorization header, or null if not present
56 * @return response entity of type String
58 public ResponseEntity<Object> postOperationWithJsonData(final String dmiResourceUrl,
59 final String requestBodyAsJsonString,
60 final OperationType operationType,
61 final String authorization) {
62 final var httpEntity = new HttpEntity<>(requestBodyAsJsonString, configureHttpHeaders(new HttpHeaders(),
65 return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Object.class);
66 } catch (final HttpStatusCodeException httpStatusCodeException) {
67 final String exceptionMessage = "Unable to " + operationType.toString() + " resource data.";
68 throw new HttpClientRequestException(exceptionMessage, httpStatusCodeException.getResponseBodyAsString(),
69 httpStatusCodeException.getStatusCode().value());
74 * Get DMI plugin health status.
76 * @param dmiPluginBaseUrl the base URL of the dmi-plugin
77 * @return plugin health status ("UP" is all OK, "" (not-specified) in case of any exception)
79 public String getDmiHealthStatus(final String dmiPluginBaseUrl) {
80 final HttpEntity<Object> httpHeaders = new HttpEntity<>(configureHttpHeaders(new HttpHeaders(), null));
82 final JsonNode responseHealthStatus =
83 restTemplate.getForObject(dmiPluginBaseUrl + HEALTH_CHECK_URL_EXTENSION,
84 JsonNode.class, httpHeaders);
85 return responseHealthStatus == null ? NOT_SPECIFIED :
86 responseHealthStatus.get("status").asText();
87 } catch (final Exception e) {
88 log.warn("Failed to retrieve health status from {}. Error Message: {}", dmiPluginBaseUrl, e.getMessage());
93 private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders, final String authorization) {
94 if (dmiProperties.isDmiBasicAuthEnabled()) {
95 httpHeaders.setBasicAuth(dmiProperties.getAuthUsername(), dmiProperties.getAuthPassword());
96 } else if (authorization != null && authorization.toLowerCase(Locale.getDefault()).startsWith("bearer ")) {
97 httpHeaders.add(HttpHeaders.AUTHORIZATION, authorization);
99 httpHeaders.setContentType(MediaType.APPLICATION_JSON);