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;
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.configuration.ApplicationConfig;
31 import org.oransc.policyagent.dmaap.DmaapRequestMessage.Operation;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.http.HttpStatus;
35 import reactor.core.publisher.Mono;
37 public class DmaapMessageHandler {
39 private static final Logger logger = LoggerFactory.getLogger(DmaapMessageHandler.class);
41 private static Gson gson = new GsonBuilder() //
45 private final MRBatchingPublisher dmaapClient;
46 private final AsyncRestClient agentClient;
48 public DmaapMessageHandler(MRBatchingPublisher dmaapClient, ApplicationConfig applicationConfig,
49 AsyncRestClient agentClient) {
50 this.agentClient = agentClient;
51 this.dmaapClient = dmaapClient;
54 public void handleDmaapMsg(String msg) {
56 this.createTask(msg) //
57 .subscribe(x -> logger.debug("handleDmaapMsg: " + x), //
58 throwable -> logger.warn("handleDmaapMsg failure ", throwable), //
59 () -> logger.debug("handleDmaapMsg complete"));
60 } catch (Exception e) {
61 logger.warn("Received unparsable message from DMAAP: {}", msg);
65 Mono<String> createTask(String msg) {
67 DmaapRequestMessage dmaapRequestMessage = gson.fromJson(msg, ImmutableDmaapRequestMessage.class);
69 return this.invokePolicyAgent(dmaapRequestMessage) //
70 .onErrorResume(t -> handleAgentCallError(t, dmaapRequestMessage)) //
71 .flatMap(response -> sendDmaapResponse(response, dmaapRequestMessage, HttpStatus.OK));
73 } catch (Exception e) {
74 logger.warn("Received unparsable message from DMAAP: {}", msg);
79 private Mono<String> handleAgentCallError(Throwable t, DmaapRequestMessage dmaapRequestMessage) {
80 logger.debug("Agent call failed: " + t.getMessage());
81 return sendDmaapResponse(t.toString(), dmaapRequestMessage, HttpStatus.NOT_FOUND) //
82 .flatMap(s -> Mono.empty());
85 private Mono<String> invokePolicyAgent(DmaapRequestMessage dmaapRequestMessage) {
86 DmaapRequestMessage.Operation operation = dmaapRequestMessage.operation();
87 Mono<String> result = null;
88 String uri = dmaapRequestMessage.url();
89 if (operation == Operation.DELETE) {
90 result = agentClient.delete(uri);
91 } else if (operation == Operation.GET) {
92 result = agentClient.get(uri);
93 } else if (operation == Operation.PUT) {
94 result = agentClient.put(uri, dmaapRequestMessage.payload());
95 } else if (operation == Operation.POST) {
96 result = agentClient.post(uri, dmaapRequestMessage.payload());
98 return Mono.error(new Exception("Not implemented operation: " + operation));
103 private Mono<String> sendDmaapResponse(String response, DmaapRequestMessage dmaapRequestMessage,
105 return getDmaapResponseMessage(dmaapRequestMessage, response, status) //
106 .flatMap(body -> sendToDmaap(body)) //
107 .onErrorResume(t -> handleResponseCallError(t, dmaapRequestMessage));
110 private Mono<String> sendToDmaap(String body) {
112 dmaapClient.send(body);
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);