Moving all files to root directory
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / openecomp / appc / dg / common / impl / DCAEReporterPluginImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.dg.common.impl;
23
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;
36
37 import java.util.Map;
38
39 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
40
41     private EventSender eventSender;
42
43     public DCAEReporterPluginImpl() {
44         BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
45         ServiceReference sref = bctx.getServiceReference(EventSender.class);
46         eventSender = (EventSender) bctx.getService(sref);
47     }
48
49     @Override
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);
55         if(!bwcMode){
56             apiVersion = ctx.getAttribute("input.common-header.api-ver");
57             eventId = ctx.getAttribute("input.common-header.request-id");
58         }else {
59             apiVersion = ctx.getAttribute(Constants.API_VERSION_FIELD_NAME);
60             eventId = ctx.getAttribute(Constants.REQ_ID_FIELD_NAME);
61         }
62
63         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorReportCode, errorDescription));
64         eventSender.sendEvent(DmaapDestination.DCAE, eventMessage);
65     }
66
67     private String getErrorDescriptionAndAddToCtx(boolean bwcMode, Map<String, String> params, SvcLogicContext ctx) {
68         String errorDescription;
69         if(!bwcMode) {
70             errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
71             if(StringUtils.isEmpty(errorDescription)) {
72                 errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
73             }else {
74                 addToContextIfNotContains(bwcMode,errorDescription,ctx);
75             }
76         }else{
77             errorDescription = params.get(Constants.DG_ERROR_FIELD_NAME);
78             if(StringUtils.isEmpty(errorDescription)) {
79                 errorDescription = ctx.getAttribute("org.openecomp.appc.dg.error");
80             }else {
81                 addToContextIfNotContains(bwcMode, errorDescription,ctx);
82             }
83         }
84
85         if(StringUtils.isEmpty(errorDescription)) {
86             errorDescription = "Unknown";
87         }
88         return errorDescription;
89     }
90
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);
100             }
101         }
102     }
103
104
105     @Override
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);
113
114         if (null == successDescription) {
115             successDescription = "Success";
116         }
117         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(successReportCode, successDescription));
118         eventSender.sendEvent(DmaapDestination.DCAE, eventMessage);
119     }
120
121 }