Merge of new rebased code
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / openecomp / appc / dg / common / impl / DCAEReporterPluginImpl.java
index 4458de1..8081a6f 100644 (file)
 package org.openecomp.appc.dg.common.impl;
 
 import org.apache.commons.lang3.StringUtils;
-import org.openecomp.appc.adapter.dmaap.EventSender;
-import org.openecomp.appc.adapter.dmaap.DmaapDestination;
-import org.openecomp.appc.adapter.dmaap.event.EventHeader;
-import org.openecomp.appc.adapter.dmaap.event.EventMessage;
-import org.openecomp.appc.adapter.dmaap.event.EventStatus;
+import org.openecomp.appc.adapter.message.EventSender;
+import org.openecomp.appc.adapter.message.MessageDestination;
+import org.openecomp.appc.adapter.message.event.EventHeader;
+import org.openecomp.appc.adapter.message.event.EventMessage;
+import org.openecomp.appc.adapter.message.event.EventStatus;
 import org.openecomp.appc.dg.common.DCAEReporterPlugin;
 import org.openecomp.appc.exceptions.APPCException;
 import org.openecomp.sdnc.sli.SvcLogicContext;
@@ -48,74 +48,108 @@ public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
 
     @Override
     public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
-        Integer errorReportCode = 501;
-        boolean bwcMode = Boolean.parseBoolean(ctx.getAttribute("isBwcMode"));
         String errorDescription,apiVersion,eventId ;
-        errorDescription = getErrorDescriptionAndAddToCtx(bwcMode,params,ctx);
-        if(!bwcMode){
-            apiVersion = ctx.getAttribute("input.common-header.api-ver");
-            eventId = ctx.getAttribute("input.common-header.request-id");
-        }else {
-            apiVersion = ctx.getAttribute(Constants.API_VERSION_FIELD_NAME);
-            eventId = ctx.getAttribute(Constants.REQ_ID_FIELD_NAME);
-        }
 
-        EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorReportCode, errorDescription));
-        eventSender.sendEvent(DmaapDestination.DCAE, eventMessage);
-    }
+        Integer errorCode = readErrorCode(params,ctx);
+        errorDescription = params.get(Constants.EVENT_MESSAGE);
 
-    private String getErrorDescriptionAndAddToCtx(boolean bwcMode, Map<String, String> params, SvcLogicContext ctx) {
-        String errorDescription;
-        if(!bwcMode) {
-            errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
-            if(StringUtils.isEmpty(errorDescription)) {
-                errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
-            }else {
-                addToContextIfNotContains(bwcMode,errorDescription,ctx);
-            }
+        if(StringUtils.isEmpty(errorDescription)) {
+            reportLegacy(params , ctx);
         }else{
-            errorDescription = params.get(Constants.DG_ERROR_FIELD_NAME);
-            if(StringUtils.isEmpty(errorDescription)) {
-                errorDescription = ctx.getAttribute("org.openecomp.appc.dg.error");
+        apiVersion = ctx.getAttribute("input.common-header.api-ver");
+        eventId = ctx.getAttribute("input.common-header.request-id");
+
+            EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
+            String eventWriteTopic = params.get("event-topic-name");
+            if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
+                eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
             }else {
-                addToContextIfNotContains(bwcMode, errorDescription,ctx);
+                eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
             }
         }
-
-        if(StringUtils.isEmpty(errorDescription)) {
-            errorDescription = "Unknown";
-        }
-        return errorDescription;
     }
 
-    private void addToContextIfNotContains(boolean bwcMode, String errorDescription, SvcLogicContext ctx) {
-        String errorDescriptionFromCtx;
-        if(!StringUtils.isEmpty(errorDescription)) {
-            String outputStatusMessageProperty = bwcMode ? "org.openecomp.appc.dg.error" : Constants.DG_OUTPUT_STATUS_MESSAGE;
-            errorDescriptionFromCtx = ctx.getAttribute(outputStatusMessageProperty);
-            if(StringUtils.isEmpty(errorDescriptionFromCtx)){
-                ctx.setAttribute(outputStatusMessageProperty, errorDescription);
-            }else if  (!errorDescriptionFromCtx.contains(errorDescription)){
-                ctx.setAttribute(outputStatusMessageProperty, errorDescriptionFromCtx+ " | "+ errorDescription);
-            }
+    private Integer readErrorCode(Map<String, String> params, SvcLogicContext ctx) {
+        Integer errorReportCode = 501;
+        String errorCodeStr = params.get(Constants.DG_ERROR_CODE);
+        errorCodeStr = StringUtils.isEmpty(errorCodeStr)?
+                ctx.getAttribute(Constants.DG_ERROR_CODE):errorCodeStr;
+        try{
+            errorReportCode =  Integer.parseInt(errorCodeStr);
         }
+        catch (NumberFormatException e){
+            // Ignore Exception
+        }
+        return errorReportCode;
     }
 
-
     @Override
     public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
         Integer successReportCode = 500;
         String successDescription, apiVersion, eventId;
-        successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
+        successDescription = params.get(Constants.EVENT_MESSAGE);
+
+        if(StringUtils.isEmpty(successDescription)) {
+            successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
+        }
+
         apiVersion = ctx.getAttribute("input.common-header.api-ver");
         eventId = ctx.getAttribute("input.common-header.request-id");
-        ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, successDescription);
 
         if (null == successDescription) {
             successDescription = "Success";
         }
         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(successReportCode, successDescription));
-        eventSender.sendEvent(DmaapDestination.DCAE, eventMessage);
+        String eventWriteTopic = params.get("event-topic-name");
+        if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
+            eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
+        }else {
+            eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
+        }
+    }
+
+    private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
+        String errorDescription,apiVersion,eventId ;
+
+        Integer errorCode = readErrorCode(params,ctx);
+        errorDescription = getErrorDescriptionAndAddToCtx(params,ctx);
+
+        apiVersion = ctx.getAttribute("input.common-header.api-ver");
+        eventId = ctx.getAttribute("input.common-header.request-id");
+
+        EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
+        String eventWriteTopic = params.get("event-topic-name");
+        if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
+            eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
+        }else {
+            eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
+        }
+    }
+
+    private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
+        String errorDescription;
+        errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
+        if(StringUtils.isEmpty(errorDescription)) {
+            errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
+        }
+        if(StringUtils.isEmpty(errorDescription)) {
+            errorDescription = "Unknown";
+        }
+        addToContextIfNotContains(errorDescription,ctx);
+        return errorDescription;
+    }
+
+    private void addToContextIfNotContains(String errorDescription, SvcLogicContext ctx) {
+        String errorDescriptionFromCtx;
+        errorDescriptionFromCtx = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
+        if(StringUtils.isEmpty(errorDescriptionFromCtx)){
+            errorDescriptionFromCtx = ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE);
+        }
+        if(StringUtils.isEmpty(errorDescriptionFromCtx)){
+            ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescription);
+        }else if  (!errorDescriptionFromCtx.contains(errorDescription)){
+            ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescriptionFromCtx+ " | "+ errorDescription);
+        }
     }
 
 }