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.util.Optional;
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(DmaapMessageHandler.class);
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 void handleDmaapMsg(DmaapRequestMessage msg) {
59 String result = this.createTask(msg).block();
60 logger.debug("handleDmaapMsg: {}", result);
61 } catch (Exception throwable) {
62 logger.warn("handleDmaapMsg failure {}", throwable.getMessage());
66 Mono<String> createTask(DmaapRequestMessage dmaapRequestMessage) {
67 return this.invokePolicyManagementService(dmaapRequestMessage) //
68 .onErrorResume(t -> handlePolicyManagementServiceCallError(t, dmaapRequestMessage)) //
69 .flatMap(response -> sendDmaapResponse(response.getBody(), dmaapRequestMessage,
70 response.getStatusCode()));
73 private Mono<ResponseEntity<String>> handlePolicyManagementServiceCallError(Throwable error,
74 DmaapRequestMessage dmaapRequestMessage) {
75 logger.debug("Policy Management Service call failed: {}", error.getMessage());
76 HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
77 String errorMessage = error.getMessage();
78 if (error instanceof WebClientResponseException) {
79 WebClientResponseException exception = (WebClientResponseException) error;
80 status = exception.getStatusCode();
81 errorMessage = exception.getResponseBodyAsString();
82 } else if (error instanceof ServiceException) {
83 status = HttpStatus.BAD_REQUEST;
84 errorMessage = error.getMessage();
85 } else if (!(error instanceof WebClientException)) {
86 logger.warn("Unexpected exception ", error);
88 return sendDmaapResponse(errorMessage, dmaapRequestMessage, status) //
89 .flatMap(notUsed -> Mono.empty());
92 public Mono<String> sendDmaapResponse(String response, DmaapRequestMessage dmaapRequestMessage, HttpStatus status) {
93 return createDmaapResponseMessage(dmaapRequestMessage, response, status) //
94 .flatMap(this::sendToDmaap) //
95 .onErrorResume(this::handleResponseCallError);
98 private Mono<ResponseEntity<String>> invokePolicyManagementService(DmaapRequestMessage dmaapRequestMessage) {
99 DmaapRequestMessage.Operation operation = dmaapRequestMessage.operation();
100 String uri = dmaapRequestMessage.url();
102 if (operation == Operation.DELETE) {
103 return pmsClient.deleteForEntity(uri);
104 } else if (operation == Operation.GET) {
105 return pmsClient.getForEntity(uri);
106 } else if (operation == Operation.PUT) {
107 return pmsClient.putForEntity(uri, payload(dmaapRequestMessage));
108 } else if (operation == Operation.POST) {
109 return pmsClient.postForEntity(uri, payload(dmaapRequestMessage));
111 return Mono.error(new ServiceException("Not implemented operation: " + operation));
115 private String payload(DmaapRequestMessage message) {
116 Optional<JsonObject> payload = message.payload();
117 if (payload.isPresent()) {
118 return gson.toJson(payload.get());
120 logger.warn("Expected payload in message from DMAAP: {}", message);
125 private Mono<String> sendToDmaap(String body) {
126 logger.debug("sendToDmaap: {} ", body);
127 return dmaapClient.post("", "[" + body + "]");
130 private Mono<String> handleResponseCallError(Throwable t) {
131 logger.warn("Failed to send response to DMaaP: {}", t.getMessage());
135 private Mono<String> createDmaapResponseMessage(DmaapRequestMessage dmaapRequestMessage, String response,
137 DmaapResponseMessage dmaapResponseMessage = ImmutableDmaapResponseMessage.builder() //
138 .status(status.toString()) //
139 .message(response == null ? "" : response) //
141 .correlationId(dmaapRequestMessage.correlationId() == null ? "" : dmaapRequestMessage.correlationId()) //
142 .originatorId(dmaapRequestMessage.originatorId() == null ? "" : dmaapRequestMessage.originatorId()) //
143 .requestId(dmaapRequestMessage.requestId() == null ? "" : dmaapRequestMessage.requestId()) //
144 .timestamp(dmaapRequestMessage.timestamp() == null ? "" : dmaapRequestMessage.timestamp()) //
146 String str = gson.toJson(dmaapResponseMessage);
147 return Mono.just(str);