2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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 lombok.AllArgsConstructor;
25 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration.DmiProperties;
26 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException;
27 import org.onap.cps.ncmp.api.impl.operations.DmiRequestBody;
28 import org.springframework.http.HttpEntity;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.MediaType;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.stereotype.Component;
33 import org.springframework.web.client.HttpStatusCodeException;
34 import org.springframework.web.client.RestTemplate;
38 public class DmiRestClient {
40 private RestTemplate restTemplate;
41 private DmiProperties dmiProperties;
44 * Sends POST operation to DMI with json body containing module references.
45 * @param dmiResourceUrl dmi resource url
46 * @param jsonData json data body
47 * @param operation the type of operation being executed (for error reporting only)
48 * @return response entity of type String
50 public ResponseEntity<Object> postOperationWithJsonData(final String dmiResourceUrl,
51 final String jsonData,
52 final DmiRequestBody.OperationEnum operation) {
53 final var httpEntity = new HttpEntity<>(jsonData, configureHttpHeaders(new HttpHeaders()));
55 return restTemplate.postForEntity(dmiResourceUrl, httpEntity, Object.class);
56 } catch (final HttpStatusCodeException httpStatusCodeException) {
57 final String exceptionMessage = "Unable to " + operation.toString() + " resource data.";
58 throw new HttpClientRequestException(exceptionMessage, httpStatusCodeException.getResponseBodyAsString(),
59 httpStatusCodeException.getRawStatusCode());
63 private HttpHeaders configureHttpHeaders(final HttpHeaders httpHeaders) {
64 httpHeaders.setBasicAuth(dmiProperties.getAuthUsername(), dmiProperties.getAuthPassword());
65 httpHeaders.setContentType(MediaType.APPLICATION_JSON);