Remove unused exceptions from dashboard backend
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / dmaap / DmaapMessageHandler.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent.dmaap;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import java.io.IOException;
27
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;
36
37 public class DmaapMessageHandler {
38
39     private static final Logger logger = LoggerFactory.getLogger(DmaapMessageHandler.class);
40
41     private static Gson gson = new GsonBuilder() //
42         .serializeNulls() //
43         .create(); //
44
45     private final MRBatchingPublisher dmaapClient;
46     private final AsyncRestClient agentClient;
47
48     public DmaapMessageHandler(MRBatchingPublisher dmaapClient, ApplicationConfig applicationConfig,
49         AsyncRestClient agentClient) {
50         this.agentClient = agentClient;
51         this.dmaapClient = dmaapClient;
52     }
53
54     public void handleDmaapMsg(String msg) {
55         this.createTask(msg) //
56             .subscribe(x -> logger.debug("handleDmaapMsg: " + x), //
57                 throwable -> logger.warn("handleDmaapMsg failure ", throwable), //
58                 () -> logger.debug("handleDmaapMsg complete"));
59     }
60
61     Mono<String> createTask(String msg) {
62         try {
63             DmaapRequestMessage dmaapRequestMessage = gson.fromJson(msg, ImmutableDmaapRequestMessage.class);
64
65             return this.invokePolicyAgent(dmaapRequestMessage) //
66                 .onErrorResume(t -> handleAgentCallError(t, dmaapRequestMessage)) //
67                 .flatMap(response -> sendDmaapResponse(response, dmaapRequestMessage, HttpStatus.OK));
68
69         } catch (Exception e) {
70             logger.warn("Received unparsable message from DMAAP: {}", msg);
71             return Mono.error(e);
72         }
73     }
74
75     private Mono<String> handleAgentCallError(Throwable t, DmaapRequestMessage dmaapRequestMessage) {
76         logger.debug("Agent call failed: " + t.getMessage());
77         return sendDmaapResponse(t.toString(), dmaapRequestMessage, HttpStatus.NOT_FOUND) //
78             .flatMap(s -> Mono.empty());
79     }
80
81     private Mono<String> invokePolicyAgent(DmaapRequestMessage dmaapRequestMessage) {
82         DmaapRequestMessage.Operation operation = dmaapRequestMessage.operation();
83         Mono<String> result = null;
84         String uri = dmaapRequestMessage.url();
85         if (operation == Operation.DELETE) {
86             result = agentClient.delete(uri);
87         } else if (operation == Operation.GET) {
88             result = agentClient.get(uri);
89         } else if (operation == Operation.PUT) {
90             result = agentClient.put(uri, dmaapRequestMessage.payload());
91         } else if (operation == Operation.POST) {
92             result = agentClient.post(uri, dmaapRequestMessage.payload());
93         } else {
94             return Mono.error(new Exception("Not implemented operation: " + operation));
95         }
96         return result;
97     }
98
99     private Mono<String> sendDmaapResponse(String response, DmaapRequestMessage dmaapRequestMessage,
100         HttpStatus status) {
101         return getDmaapResponseMessage(dmaapRequestMessage, response, status) //
102             .flatMap(body -> sendToDmaap(body)) //
103             .onErrorResume(t -> handleResponseCallError(t, dmaapRequestMessage));
104     }
105
106     private Mono<String> sendToDmaap(String body) {
107         try {
108             logger.debug("sendToDmaap: {} ", body);
109             dmaapClient.send(body);
110             dmaapClient.sendBatchWithResponse();
111             return Mono.just("OK");
112         } catch (IOException e) {
113             return Mono.error(e);
114         }
115     }
116
117     private Mono<String> handleResponseCallError(Throwable t, DmaapRequestMessage dmaapRequestMessage) {
118         logger.debug("Failed to respond: " + t.getMessage());
119         return Mono.empty();
120     }
121
122     private Mono<String> getDmaapResponseMessage(DmaapRequestMessage dmaapRequestMessage, String response,
123         HttpStatus status) {
124         DmaapResponseMessage dmaapResponseMessage = ImmutableDmaapResponseMessage.builder() //
125             .status(status.toString()) //
126             .message(response) //
127             .type("response") //
128             .correlationId(dmaapRequestMessage.correlationId()) //
129             .originatorId(dmaapRequestMessage.originatorId()) //
130             .requestId(dmaapRequestMessage.requestId()) //
131             .timestamp(dmaapRequestMessage.timestamp()) //
132             .build();
133         String str = gson.toJson(dmaapResponseMessage);
134
135         return Mono.just(str);
136
137     }
138
139 }