Merge "Added STD sim 2.0.0 tests"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / dmaap / DmaapMessageHandler.java
index 713d483..226b54e 100644 (file)
@@ -2,7 +2,7 @@
  * ========================LICENSE_START=================================
  * O-RAN-SC
  * %%
- * Copyright (C) 2019 Nordix Foundation
+ * Copyright (C) 2020 Nordix Foundation
  * %%
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.oransc.policyagent.dmaap;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import java.io.IOException;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonObject;
+
 import java.util.Optional;
-import java.util.Properties;
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.json.JSONTokener;
+
 import org.oransc.policyagent.clients.AsyncRestClient;
-import org.oransc.policyagent.configuration.ApplicationConfig;
-import org.oransc.policyagent.controllers.PolicyController;
-import org.oransc.policyagent.model.DmaapRequestMessage;
-import org.oransc.policyagent.model.DmaapResponseMessage;
+import org.oransc.policyagent.dmaap.DmaapRequestMessage.Operation;
+import org.oransc.policyagent.exceptions.ServiceException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
-import org.springframework.scheduling.annotation.Async;
-import org.springframework.stereotype.Component;
+import org.springframework.web.reactive.function.client.WebClientException;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
+import reactor.core.publisher.Mono;
 
-@Component
+/**
+ * The class handles incoming requests from DMAAP.
+ * <p>
+ * That means: invoke a REST call towards this services and to send back a response though DMAAP
+ */
 public class DmaapMessageHandler {
-
     private static final Logger logger = LoggerFactory.getLogger(DmaapMessageHandler.class);
+    private static Gson gson = new GsonBuilder() //
+        .create(); //
+    private final AsyncRestClient dmaapClient;
+    private final AsyncRestClient agentClient;
 
-    @Autowired
-    private ObjectMapper mapper;
-    @Autowired
-    private PolicyController policyController;
-    private AsyncRestClient restClient;
-    private ApplicationConfig applicationConfig;
-    private String topic = "";
-
-    @Autowired
-    public DmaapMessageHandler(ApplicationConfig applicationConfig) {
-        this.applicationConfig = applicationConfig;
+    public DmaapMessageHandler(AsyncRestClient dmaapClient, AsyncRestClient agentClient) {
+        this.agentClient = agentClient;
+        this.dmaapClient = dmaapClient;
     }
 
-    // The publish properties is corrupted. It contains the subscribe property values.
-    @Async("threadPoolTaskExecutor")
     public void handleDmaapMsg(String msg) {
-        init();
-        DmaapRequestMessage dmaapRequestMessage = null;
-        Optional<String> dmaapResponse = null;
-        // Process the message
-        /**
-         * Sample Request Message from DMAAP { "type": "request", "correlationId":
-         * "c09ac7d1-de62-0016-2000-e63701125557-201", "target": "policy-agent", "timestamp": "2019-05-14T11:44:51.36Z",
-         * "apiVersion": "1.0", "originatorId": "849e6c6b420", "requestId": "23343221", "operation": "getPolicySchemas",
-         * "payload": "{\"ricName\":\"ric1\"}" }
-         *
-         * --------------------------------------------------------------------------------------------------------------
-         * Sample Response Message to DMAAP {type=response, correlationId=c09ac7d1-de62-0016-2000-e63701125557-201,
-         * timestamp=null, originatorId=849e6c6b420, requestId=23343221, status=200 OK, message=[]}
-         * -------------------------------------------------------------------------------------------------------------
-         */
         try {
-            dmaapRequestMessage = mapper.readValue(msg, DmaapRequestMessage.class);
-            // Call the Controller
-            logger.debug("Invoke the Policy Agent Controller");
-            dmaapResponse = invokeController(dmaapRequestMessage);
-            // Post the Response message to the DMAAP bus
-            logger.debug("DMAAP Response Message to Client- {}", dmaapResponse);
-            if (dmaapResponse.isPresent()) {
-                restClient.post("A1-POLICY-AGENT-WRITE", dmaapResponse.get()).block(); //
-            }
-        } catch (IOException e) {
-            logger.error("Exception occured during message processing", e);
+            String result = this.createTask(msg).block();
+            logger.debug("handleDmaapMsg: {}", result);
+        } catch (Exception throwable) {
+            logger.warn("handleDmaapMsg failure {}", throwable.getMessage());
         }
     }
 
-    private Optional<String> invokeController(DmaapRequestMessage dmaapRequestMessage) {
-        String formattedString = "";
-        String ricName;
-        String instance;
-        String jsonBody;
-        logger.debug("Payload from the Message - {}", dmaapRequestMessage.getPayload());
+    Mono<String> createTask(String msg) {
         try {
-            formattedString = new JSONTokener(dmaapRequestMessage.getPayload()).nextValue().toString();
-            logger.debug("Removed the Escape charater in payload- {}", formattedString);
-        } catch (JSONException e) {
-            logger.error("Exception occurred during formating Payload- {}", dmaapRequestMessage.getPayload());
+            DmaapRequestMessage dmaapRequestMessage = gson.fromJson(msg, ImmutableDmaapRequestMessage.class);
+            return this.invokePolicyAgent(dmaapRequestMessage) //
+                .onErrorResume(t -> handleAgentCallError(t, dmaapRequestMessage)) //
+                .flatMap(
+                    response -> sendDmaapResponse(response.getBody(), dmaapRequestMessage, response.getStatusCode()));
+        } catch (Exception e) {
+            String errorMsg = "Received unparsable message from DMAAP: \"" + msg + "\", reason: " + e.getMessage();
+            return Mono.error(new ServiceException(errorMsg)); // Cannot make any response
         }
-        JSONObject jsonObject = new JSONObject(formattedString);
-        switch (dmaapRequestMessage.getOperation()) {
-            case "getPolicySchemas":
-                ricName = (String) jsonObject.get("ricName");
-                logger.debug("Received the request for getPolicySchemas with Ric Name- {}", ricName);
-                return getDmaapResponseMessage(dmaapRequestMessage, policyController.getPolicySchemas(ricName));
-            case "getPolicySchema":
-                String policyTypeId = (String) jsonObject.get("id");
-                logger.debug("Received the request for getPolicySchema with Policy Type Id- {}", policyTypeId);
-                return getDmaapResponseMessage(dmaapRequestMessage, policyController.getPolicySchema(policyTypeId));
-            case "getPolicyTypes":
-                ricName = (String) jsonObject.get("ricName");
-                logger.debug("Received the request for getPolicyTypes with Ric Name- {}", ricName);
-                return getDmaapResponseMessage(dmaapRequestMessage, policyController.getPolicyTypes(ricName));
-            case "getPolicy":
-                instance = (String) jsonObject.get("instance");
-                logger.debug("Received the request for getPolicy with Instance- {}", instance);
-                return getDmaapResponseMessage(dmaapRequestMessage, policyController.getPolicy(instance));
-            case "deletePolicy":
-                instance = (String) jsonObject.get("instance");
-                logger.debug("Received the request for deletePolicy with Instance- {}", instance);
-                return getDmaapResponseMessage(dmaapRequestMessage, policyController.deletePolicy(instance).block());
-            case "putPolicy":
-                String type = (String) jsonObject.get("type");
-                String putPolicyInstance = (String) jsonObject.get("instance");
-                String putPolicyRic = (String) jsonObject.get("ric");
-                String service = (String) jsonObject.get("service");
-                jsonBody = (String) jsonObject.get("jsonBody");
-                return getDmaapResponseMessage(dmaapRequestMessage,
-                        policyController.putPolicy(type, putPolicyInstance, putPolicyRic, service, jsonBody).block());
-            case "getPolicies":
-                String getPolicyType = (String) jsonObject.get("type");
-                instance = (String) jsonObject.get("instance");
-                String getPolicyRic = (String) jsonObject.get("ric");
-                String getPolicyService = (String) jsonObject.get("service");
-                jsonBody = (String) jsonObject.get("jsonBody");
-                return getDmaapResponseMessage(dmaapRequestMessage, policyController
-                        .putPolicy(getPolicyType, instance, getPolicyRic, getPolicyService, jsonBody).block());
-            default:
-                break;
+    }
+
+    private Mono<ResponseEntity<String>> handleAgentCallError(Throwable error,
+        DmaapRequestMessage dmaapRequestMessage) {
+        logger.debug("Agent call failed: {}", error.getMessage());
+        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
+        String errorMessage = error.getMessage();
+        if (error instanceof WebClientResponseException) {
+            WebClientResponseException exception = (WebClientResponseException) error;
+            status = exception.getStatusCode();
+            errorMessage = exception.getResponseBodyAsString();
+        } else if (error instanceof ServiceException) {
+            status = HttpStatus.BAD_REQUEST;
+            errorMessage = error.getMessage();
+        } else if (!(error instanceof WebClientException)) {
+            logger.warn("Unexpected exception ", error);
         }
-        return Optional.empty();
+        return sendDmaapResponse(errorMessage, dmaapRequestMessage, status) //
+            .flatMap(notUsed -> Mono.empty());
     }
 
-    private Optional<String> getDmaapResponseMessage(DmaapRequestMessage dmaapRequestMessage,
-            ResponseEntity<?> policySchemas) {
-        DmaapResponseMessage dmaapResponseMessage = DmaapResponseMessage.builder()
-                .status(policySchemas.getStatusCode().toString()).message(policySchemas.getBody().toString())
-                .type("response").correlationId(dmaapRequestMessage.getCorrelationId())
-                .originatorId(dmaapRequestMessage.getOriginatorId()).requestId(dmaapRequestMessage.getRequestId())
-                .build();
-        try {
-            return Optional.of(mapper.writeValueAsString(dmaapResponseMessage));
-        } catch (JsonProcessingException e) {
-            logger.error("Exception occured during getDmaapResponseMessage", e);
+    private Mono<ResponseEntity<String>> invokePolicyAgent(DmaapRequestMessage dmaapRequestMessage) {
+        DmaapRequestMessage.Operation operation = dmaapRequestMessage.operation();
+        String uri = dmaapRequestMessage.url();
+
+        if (operation == Operation.DELETE) {
+            return agentClient.deleteForEntity(uri);
+        } else if (operation == Operation.GET) {
+            return agentClient.getForEntity(uri);
+        } else if (operation == Operation.PUT) {
+            return agentClient.putForEntity(uri, payload(dmaapRequestMessage));
+        } else if (operation == Operation.POST) {
+            return agentClient.postForEntity(uri, payload(dmaapRequestMessage));
+        } else {
+            return Mono.error(new ServiceException("Not implemented operation: " + operation));
         }
-        return Optional.empty();
     }
 
-    // @PostConstruct
-    // The application properties value is always NULL for the first time
-    // Need to fix this
-    public void init() {
-        logger.debug("Reading DMAAP Publisher bus details from Application Config");
-        Properties dmaapPublisherConfig = applicationConfig.getDmaapPublisherConfig();
-        String host = (String) dmaapPublisherConfig.get("ServiceName");
-        topic = dmaapPublisherConfig.getProperty("topic");
-        System.out.println("\"Read the topic ---------->" + topic);
-        logger.debug("Read the topic & Service Name - {} , {}", host, topic);
-        this.restClient = new AsyncRestClient("http://" + host + "/"); // get this value from application config
+    private String payload(DmaapRequestMessage message) {
+        Optional<JsonObject> payload = message.payload();
+        if (payload.isPresent()) {
+            return gson.toJson(payload.get());
+        } else {
+            logger.warn("Expected payload in message from DMAAP: {}", message);
+            return "";
+        }
+    }
+
+    private Mono<String> sendDmaapResponse(String response, DmaapRequestMessage dmaapRequestMessage,
+        HttpStatus status) {
+        return createDmaapResponseMessage(dmaapRequestMessage, response, status) //
+            .flatMap(this::sendToDmaap) //
+            .onErrorResume(this::handleResponseCallError);
+    }
+
+    private Mono<String> sendToDmaap(String body) {
+        logger.debug("sendToDmaap: {} ", body);
+        return dmaapClient.post("", "[" + body + "]");
+    }
+
+    private Mono<String> handleResponseCallError(Throwable t) {
+        logger.debug("Failed to send response to DMaaP: {}", t.getMessage());
+        return Mono.empty();
+    }
+
+    private Mono<String> createDmaapResponseMessage(DmaapRequestMessage dmaapRequestMessage, String response,
+        HttpStatus status) {
+        DmaapResponseMessage dmaapResponseMessage = ImmutableDmaapResponseMessage.builder() //
+            .status(status.toString()) //
+            .message(response == null ? "" : response) //
+            .type("response") //
+            .correlationId(dmaapRequestMessage.correlationId() == null ? "" : dmaapRequestMessage.correlationId()) //
+            .originatorId(dmaapRequestMessage.originatorId() == null ? "" : dmaapRequestMessage.originatorId()) //
+            .requestId(dmaapRequestMessage.requestId() == null ? "" : dmaapRequestMessage.requestId()) //
+            .timestamp(dmaapRequestMessage.timestamp() == null ? "" : dmaapRequestMessage.timestamp()) //
+            .build();
+        String str = gson.toJson(dmaapResponseMessage);
+        return Mono.just(str);
 
     }
 }