2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent.dmaap;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import java.io.IOException;
28 import org.onap.dmaap.mr.client.MRBatchingPublisher;
29 import org.oransc.policyagent.clients.AsyncRestClient;
30 import org.oransc.policyagent.dmaap.DmaapRequestMessage.Operation;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.http.HttpStatus;
34 import reactor.core.publisher.Mono;
36 public class DmaapMessageHandler {
38 private static final Logger logger = LoggerFactory.getLogger(DmaapMessageHandler.class);
40 private static Gson gson = new GsonBuilder() //
43 private final MRBatchingPublisher dmaapClient;
44 private final AsyncRestClient agentClient;
46 public DmaapMessageHandler(MRBatchingPublisher dmaapClient, AsyncRestClient agentClient) {
47 this.agentClient = agentClient;
48 this.dmaapClient = dmaapClient;
51 public void handleDmaapMsg(String msg) {
52 this.createTask(msg) //
53 .subscribe(message -> logger.debug("handleDmaapMsg: {}", message), //
54 throwable -> logger.warn("handleDmaapMsg failure ", throwable), //
55 () -> logger.debug("handleDmaapMsg complete"));
58 Mono<String> createTask(String msg) {
60 DmaapRequestMessage dmaapRequestMessage = gson.fromJson(msg, ImmutableDmaapRequestMessage.class);
62 return this.invokePolicyAgent(dmaapRequestMessage) //
63 .onErrorResume(t -> handleAgentCallError(t, dmaapRequestMessage)) //
64 .flatMap(response -> sendDmaapResponse(response, dmaapRequestMessage, HttpStatus.OK));
66 } catch (Exception e) {
67 logger.warn("Received unparsable message from DMAAP: {}", msg);
72 private Mono<String> handleAgentCallError(Throwable t, DmaapRequestMessage dmaapRequestMessage) {
73 logger.debug("Agent call failed: {}", t.getMessage());
74 return sendDmaapResponse(t.toString(), dmaapRequestMessage, HttpStatus.NOT_FOUND) //
75 .flatMap(notUsed -> Mono.empty());
78 private Mono<String> invokePolicyAgent(DmaapRequestMessage dmaapRequestMessage) {
79 DmaapRequestMessage.Operation operation = dmaapRequestMessage.operation();
80 Mono<String> result = null;
81 String uri = dmaapRequestMessage.url();
82 if (operation == Operation.DELETE) {
83 result = agentClient.delete(uri);
84 } else if (operation == Operation.GET) {
85 result = agentClient.get(uri);
86 } else if (operation == Operation.PUT) {
87 result = agentClient.put(uri, payload(dmaapRequestMessage));
88 } else if (operation == Operation.POST) {
89 result = agentClient.post(uri, payload(dmaapRequestMessage));
91 return Mono.error(new Exception("Not implemented operation: " + operation));
96 private String payload(DmaapRequestMessage message) {
97 if (message.payload().isPresent()) {
98 return gson.toJson(message.payload().get());
100 logger.warn("Expected payload in message from DMAAP: {}", message);
105 private Mono<String> sendDmaapResponse(String response, DmaapRequestMessage dmaapRequestMessage,
107 return getDmaapResponseMessage(dmaapRequestMessage, response, status) //
108 .flatMap(this::sendToDmaap) //
109 .onErrorResume(this::handleResponseCallError);
112 private Mono<String> sendToDmaap(String body) {
114 logger.debug("sendToDmaap: {} ", body);
115 dmaapClient.send(body);
116 dmaapClient.sendBatchWithResponse();
117 return Mono.just("OK");
118 } catch (IOException e) {
119 return Mono.error(e);
123 private Mono<String> handleResponseCallError(Throwable t) {
124 logger.debug("Failed to respond: {}", t.getMessage());
128 private Mono<String> getDmaapResponseMessage(DmaapRequestMessage dmaapRequestMessage, String response,
130 DmaapResponseMessage dmaapResponseMessage = ImmutableDmaapResponseMessage.builder() //
131 .status(status.toString()) //
132 .message(response) //
134 .correlationId(dmaapRequestMessage.correlationId()) //
135 .originatorId(dmaapRequestMessage.originatorId()) //
136 .requestId(dmaapRequestMessage.requestId()) //
137 .timestamp(dmaapRequestMessage.timestamp()) //
139 String str = gson.toJson(dmaapResponseMessage);
141 return Mono.just(str);