Merge "Some improved traces"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / dmaap / DmaapMessageHandler.java
index 6d1603c..19d1564 100644 (file)
@@ -17,6 +17,7 @@
  * limitations under the License.
  * ========================LICENSE_END===================================
  */
+
 package org.oransc.policyagent.dmaap;
 
 import com.google.gson.Gson;
@@ -34,6 +35,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
+import org.springframework.web.reactive.function.client.WebClientException;
 import org.springframework.web.reactive.function.client.WebClientResponseException;
 import reactor.core.publisher.Mono;
 
@@ -56,10 +58,12 @@ public class DmaapMessageHandler {
     }
 
     public void handleDmaapMsg(String msg) {
-        this.createTask(msg) //
-            .subscribe(message -> logger.debug("handleDmaapMsg: {}", message), //
-                throwable -> logger.warn("handleDmaapMsg failure ", throwable), //
-                () -> logger.debug("handleDmaapMsg complete"));
+        try {
+            String result = this.createTask(msg).block();
+            logger.debug("handleDmaapMsg: {}", result);
+        } catch (Exception throwable) {
+            logger.warn("handleDmaapMsg failure {}", throwable.getMessage());
+        }
     }
 
     Mono<String> createTask(String msg) {
@@ -70,15 +74,15 @@ public class DmaapMessageHandler {
                 .flatMap(
                     response -> sendDmaapResponse(response.getBody(), dmaapRequestMessage, response.getStatusCode()));
         } catch (Exception e) {
-            logger.warn("Received unparsable message from DMAAP: {}", msg);
-            return Mono.error(e); // Cannot make any response
+            String errorMsg = "Received unparsable message from DMAAP: \"" + msg + "\", reason: " + e.getMessage();
+            return Mono.error(new ServiceException(errorMsg)); // Cannot make any response
         }
     }
 
     private Mono<ResponseEntity<String>> handleAgentCallError(Throwable t, String originalMessage,
         DmaapRequestMessage dmaapRequestMessage) {
         logger.debug("Agent call failed: {}", t.getMessage());
-        HttpStatus status = HttpStatus.NOT_FOUND;
+        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
         String errorMessage = t.getMessage();
         if (t instanceof WebClientResponseException) {
             WebClientResponseException exception = (WebClientResponseException) t;
@@ -87,7 +91,8 @@ public class DmaapMessageHandler {
         } else if (t instanceof ServiceException) {
             status = HttpStatus.BAD_REQUEST;
             errorMessage = prepareBadOperationErrorMessage(t, originalMessage);
-
+        } else if (!(t instanceof WebClientException)) {
+            logger.warn("Unexpected exception ", t);
         }
         return sendDmaapResponse(errorMessage, dmaapRequestMessage, status) //
             .flatMap(notUsed -> Mono.empty());
@@ -116,7 +121,6 @@ public class DmaapMessageHandler {
         } else {
             return Mono.error(new ServiceException("Not implemented operation: " + operation));
         }
-
     }
 
     private String payload(DmaapRequestMessage message) {
@@ -156,12 +160,12 @@ public class DmaapMessageHandler {
         HttpStatus status) {
         DmaapResponseMessage dmaapResponseMessage = ImmutableDmaapResponseMessage.builder() //
             .status(status.toString()) //
-            .message(response) //
+            .message(response == null ? "" : response) //
             .type("response") //
-            .correlationId(dmaapRequestMessage.correlationId()) //
-            .originatorId(dmaapRequestMessage.originatorId()) //
-            .requestId(dmaapRequestMessage.requestId()) //
-            .timestamp(dmaapRequestMessage.timestamp()) //
+            .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);