2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.openecomp.appc.dg.common.impl;
 
  24 import org.apache.commons.lang3.StringUtils;
 
  25 import org.openecomp.appc.adapter.dmaap.EventSender;
 
  26 import org.openecomp.appc.adapter.dmaap.DmaapDestination;
 
  27 import org.openecomp.appc.adapter.dmaap.event.EventHeader;
 
  28 import org.openecomp.appc.adapter.dmaap.event.EventMessage;
 
  29 import org.openecomp.appc.adapter.dmaap.event.EventStatus;
 
  30 import org.openecomp.appc.dg.common.DCAEReporterPlugin;
 
  31 import org.openecomp.appc.exceptions.APPCException;
 
  32 import org.openecomp.sdnc.sli.SvcLogicContext;
 
  33 import org.osgi.framework.BundleContext;
 
  34 import org.osgi.framework.FrameworkUtil;
 
  35 import org.osgi.framework.ServiceReference;
 
  39 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
 
  41     private EventSender eventSender;
 
  43     public DCAEReporterPluginImpl() {
 
  44         BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
 
  45         ServiceReference sref = bctx.getServiceReference(EventSender.class);
 
  46         eventSender = (EventSender) bctx.getService(sref);
 
  50     public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
 
  51         Integer errorReportCode = 501;
 
  52         boolean bwcMode = Boolean.parseBoolean(ctx.getAttribute("isBwcMode"));
 
  53         String errorDescription,apiVersion,eventId ;
 
  54         errorDescription = getErrorDescriptionAndAddToCtx(bwcMode,params,ctx);
 
  56             apiVersion = ctx.getAttribute("input.common-header.api-ver");
 
  57             eventId = ctx.getAttribute("input.common-header.request-id");
 
  59             apiVersion = ctx.getAttribute(Constants.API_VERSION_FIELD_NAME);
 
  60             eventId = ctx.getAttribute(Constants.REQ_ID_FIELD_NAME);
 
  63         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorReportCode, errorDescription));
 
  64         eventSender.sendEvent(DmaapDestination.DCAE, eventMessage);
 
  67     private String getErrorDescriptionAndAddToCtx(boolean bwcMode, Map<String, String> params, SvcLogicContext ctx) {
 
  68         String errorDescription;
 
  70             errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
 
  71             if(StringUtils.isEmpty(errorDescription)) {
 
  72                 errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
 
  74                 addToContextIfNotContains(bwcMode,errorDescription,ctx);
 
  77             errorDescription = params.get(Constants.DG_ERROR_FIELD_NAME);
 
  78             if(StringUtils.isEmpty(errorDescription)) {
 
  79                 errorDescription = ctx.getAttribute("org.openecomp.appc.dg.error");
 
  81                 addToContextIfNotContains(bwcMode, errorDescription,ctx);
 
  85         if(StringUtils.isEmpty(errorDescription)) {
 
  86             errorDescription = "Unknown";
 
  88         return errorDescription;
 
  91     private void addToContextIfNotContains(boolean bwcMode, String errorDescription, SvcLogicContext ctx) {
 
  92         String errorDescriptionFromCtx;
 
  93         if(!StringUtils.isEmpty(errorDescription)) {
 
  94             String outputStatusMessageProperty = bwcMode ? "org.openecomp.appc.dg.error" : Constants.DG_OUTPUT_STATUS_MESSAGE;
 
  95             errorDescriptionFromCtx = ctx.getAttribute(outputStatusMessageProperty);
 
  96             if(StringUtils.isEmpty(errorDescriptionFromCtx)){
 
  97                 ctx.setAttribute(outputStatusMessageProperty, errorDescription);
 
  98             }else if  (!errorDescriptionFromCtx.contains(errorDescription)){
 
  99                 ctx.setAttribute(outputStatusMessageProperty, errorDescriptionFromCtx+ " | "+ errorDescription);
 
 106     public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
 
 107         Integer successReportCode = 500;
 
 108         String successDescription, apiVersion, eventId;
 
 109         successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
 
 110         apiVersion = ctx.getAttribute("input.common-header.api-ver");
 
 111         eventId = ctx.getAttribute("input.common-header.request-id");
 
 112         ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, successDescription);
 
 114         if (null == successDescription) {
 
 115             successDescription = "Success";
 
 117         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(successReportCode, successDescription));
 
 118         eventSender.sendEvent(DmaapDestination.DCAE, eventMessage);