2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2020 Nordix Foundation. All rights reserved.
6 * ======================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.dmaap;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonObject;
27 import java.lang.invoke.MethodHandles;
29 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
30 import org.onap.ccsdk.oran.a1policymanagementservice.dmaap.DmaapRequestMessage.Operation;
31 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.reactive.function.client.WebClientException;
37 import org.springframework.web.reactive.function.client.WebClientResponseException;
38 import reactor.core.publisher.Mono;
41 * The class handles incoming requests from DMAAP.
43 * That means: invoke a REST call towards this services and to send back a
44 * response though DMAAP
46 public class DmaapMessageHandler {
47 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
48 private static Gson gson = new GsonBuilder().create();
49 private final AsyncRestClient dmaapClient;
50 private final AsyncRestClient pmsClient;
52 public DmaapMessageHandler(AsyncRestClient dmaapClient, AsyncRestClient pmsClient) {
53 this.pmsClient = pmsClient;
54 this.dmaapClient = dmaapClient;
57 public Mono<String> handleDmaapMsg(DmaapRequestMessage dmaapRequestMessage) {
58 return this.invokePolicyManagementService(dmaapRequestMessage) //
59 .onErrorResume(t -> handlePolicyManagementServiceCallError(t, dmaapRequestMessage)) //
60 .flatMap(response -> sendDmaapResponse(response.getBody(), dmaapRequestMessage,
61 response.getStatusCode()))
62 .doOnError(t -> logger.warn("Failed to handle DMAAP message : {}", t.getMessage()))//
63 .onErrorResume(t -> Mono.empty());
66 private Mono<ResponseEntity<String>> handlePolicyManagementServiceCallError(Throwable error,
67 DmaapRequestMessage dmaapRequestMessage) {
68 logger.debug("Policy Management Service call failed: {}", error.getMessage());
69 HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
70 String errorMessage = error.getMessage();
71 if (error instanceof WebClientResponseException) {
72 WebClientResponseException exception = (WebClientResponseException) error;
73 status = exception.getStatusCode();
74 errorMessage = exception.getResponseBodyAsString();
75 } else if (error instanceof ServiceException) {
76 status = HttpStatus.BAD_REQUEST;
77 errorMessage = error.getMessage();
78 } else if (!(error instanceof WebClientException)) {
79 logger.warn("Unexpected exception ", error);
81 return sendDmaapResponse(errorMessage, dmaapRequestMessage, status) //
82 .flatMap(notUsed -> Mono.empty());
85 public Mono<String> sendDmaapResponse(String response, DmaapRequestMessage dmaapRequestMessage, HttpStatus status) {
86 return createDmaapResponseMessage(dmaapRequestMessage, response, status) //
87 .flatMap(this::sendToDmaap) //
88 .onErrorResume(this::handleResponseCallError);
91 private Mono<ResponseEntity<String>> invokePolicyManagementService(DmaapRequestMessage dmaapRequestMessage) {
92 DmaapRequestMessage.Operation operation = dmaapRequestMessage.getOperation();
93 String uri = dmaapRequestMessage.getUrl();
95 if (operation == Operation.DELETE) {
96 return pmsClient.deleteForEntity(uri);
97 } else if (operation == Operation.GET) {
98 return pmsClient.getForEntity(uri);
99 } else if (operation == Operation.PUT) {
100 return pmsClient.putForEntity(uri, payload(dmaapRequestMessage));
101 } else if (operation == Operation.POST) {
102 return pmsClient.postForEntity(uri, payload(dmaapRequestMessage));
104 return Mono.error(new ServiceException("Not implemented operation: " + operation));
108 private String payload(DmaapRequestMessage message) {
109 JsonObject payload = message.getPayload();
110 if (payload != null) {
111 return gson.toJson(payload);
113 logger.warn("Expected payload in message from DMAAP: {}", message);
118 private Mono<String> sendToDmaap(String body) {
119 logger.debug("sendToDmaap: {} ", body);
120 return dmaapClient.post("", "[" + body + "]");
123 private Mono<String> handleResponseCallError(Throwable t) {
124 logger.warn("Failed to send response to DMaaP: {}", t.getMessage());
128 private Mono<String> createDmaapResponseMessage(DmaapRequestMessage dmaapRequestMessage, String response,
130 DmaapResponseMessage dmaapResponseMessage = DmaapResponseMessage.builder() //
131 .status(status.toString()) //
132 .message(response == null ? "" : response) //
135 dmaapRequestMessage.getCorrelationId() == null ? "" : dmaapRequestMessage.getCorrelationId()) //
137 dmaapRequestMessage.getOriginatorId() == null ? "" : dmaapRequestMessage.getOriginatorId()) //
138 .requestId(dmaapRequestMessage.getRequestId() == null ? "" : dmaapRequestMessage.getRequestId()) //
139 .timestamp(dmaapRequestMessage.getTimestamp() == null ? "" : dmaapRequestMessage.getTimestamp()) //
141 String str = gson.toJson(dmaapResponseMessage);
142 return Mono.just(str);