2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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;
25 import java.io.IOException;
26 import org.onap.dmaap.mr.client.MRBatchingPublisher;
27 import org.oransc.policyagent.clients.AsyncRestClient;
28 import org.oransc.policyagent.configuration.ApplicationConfig;
29 import org.oransc.policyagent.dmaap.DmaapRequestMessage.Operation;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.http.HttpStatus;
33 import reactor.core.publisher.Mono;
35 public class DmaapMessageHandler {
37 private static final Logger logger = LoggerFactory.getLogger(DmaapMessageHandler.class);
39 private static Gson gson = new GsonBuilder() //
43 private final MRBatchingPublisher dmaapClient;
44 private final AsyncRestClient agentClient;
46 public DmaapMessageHandler(MRBatchingPublisher dmaapClient, ApplicationConfig applicationConfig,
47 AsyncRestClient agentClient) {
48 this.agentClient = agentClient;
49 this.dmaapClient = dmaapClient;
52 public void handleDmaapMsg(String msg) {
54 this.createTask(msg) //
55 .subscribe(x -> logger.debug("handleDmaapMsg: " + x), //
56 throwable -> logger.warn("handleDmaapMsg failure ", throwable), //
57 () -> logger.debug("handleDmaapMsg complete"));
58 } catch (Exception e) {
59 logger.warn("Received unparsable message from DMAAP: {}", msg);
63 Mono<String> createTask(String msg) {
65 DmaapRequestMessage dmaapRequestMessage = gson.fromJson(msg, ImmutableDmaapRequestMessage.class);
67 return this.invokePolicyAgent(dmaapRequestMessage) //
68 .onErrorResume(t -> handleAgentCallError(t, dmaapRequestMessage)) //
69 .flatMap(response -> sendDmaapResponse(response, dmaapRequestMessage, HttpStatus.OK));
71 } catch (Exception e) {
72 logger.warn("Received unparsable message from DMAAP: {}", msg);
77 private Mono<String> handleAgentCallError(Throwable t, DmaapRequestMessage dmaapRequestMessage) {
78 logger.debug("Agent call failed: " + t.getMessage());
79 return sendDmaapResponse(t.toString(), dmaapRequestMessage, HttpStatus.NOT_FOUND) //
80 .flatMap(s -> Mono.empty());
83 private Mono<String> invokePolicyAgent(DmaapRequestMessage dmaapRequestMessage) {
84 DmaapRequestMessage.Operation operation = dmaapRequestMessage.operation();
85 Mono<String> result = null;
86 String uri = dmaapRequestMessage.url();
87 if (operation == Operation.DELETE) {
88 result = agentClient.delete(uri);
89 } else if (operation == Operation.GET) {
90 result = agentClient.get(uri);
91 } else if (operation == Operation.PUT) {
92 result = agentClient.put(uri, dmaapRequestMessage.payload());
93 } else if (operation == Operation.POST) {
94 result = agentClient.post(uri, dmaapRequestMessage.payload());
96 return Mono.error(new Exception("Not implemented operation: " + operation));
101 private Mono<String> sendDmaapResponse(String response, DmaapRequestMessage dmaapRequestMessage,
103 return getDmaapResponseMessage(dmaapRequestMessage, response, status) //
104 .flatMap(body -> sendToDmaap(body)) //
105 .onErrorResume(t -> handleResponseCallError(t, dmaapRequestMessage));
108 private Mono<String> sendToDmaap(String body) {
110 logger.debug("sendToDmaap: {} ", body);
111 dmaapClient.send(body);
112 dmaapClient.sendBatchWithResponse();
113 return Mono.just("OK");
114 } catch (IOException e) {
115 return Mono.error(e);
119 private Mono<String> handleResponseCallError(Throwable t, DmaapRequestMessage dmaapRequestMessage) {
120 logger.debug("Failed to respond: " + t.getMessage());
124 private Mono<String> getDmaapResponseMessage(DmaapRequestMessage dmaapRequestMessage, String response,
126 DmaapResponseMessage dmaapResponseMessage = ImmutableDmaapResponseMessage.builder() //
127 .status(status.toString()) //
128 .message(response) //
130 .correlationId(dmaapRequestMessage.correlationId()) //
131 .originatorId(dmaapRequestMessage.originatorId()) //
132 .requestId(dmaapRequestMessage.requestId()) //
133 .timestamp(dmaapRequestMessage.timestamp()) //
135 String str = gson.toJson(dmaapResponseMessage);
137 return Mono.just(str);