0f99f8314128ad291c062f51840e3cfee42d20b6
[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  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.dg.common.impl;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.openecomp.appc.adapter.message.EventSender;
29 import org.openecomp.appc.adapter.message.MessageDestination;
30 import org.openecomp.appc.adapter.message.event.EventHeader;
31 import org.openecomp.appc.adapter.message.event.EventMessage;
32 import org.openecomp.appc.adapter.message.event.EventStatus;
33 import org.openecomp.appc.dg.common.DCAEReporterPlugin;
34 import org.openecomp.appc.exceptions.APPCException;
35 import org.openecomp.sdnc.sli.SvcLogicContext;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.FrameworkUtil;
38 import org.osgi.framework.ServiceReference;
39
40 import java.util.Map;
41
42 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
43
44     private EventSender eventSender;
45
46     public DCAEReporterPluginImpl() {
47         BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
48         ServiceReference sref = bctx.getServiceReference(EventSender.class);
49         eventSender = (EventSender) bctx.getService(sref);
50     }
51
52     @Override
53     public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
54         String errorDescription,apiVersion,eventId ;
55
56         Integer errorCode = readErrorCode(params,ctx);
57         errorDescription = params.get(Constants.EVENT_MESSAGE);
58
59         if(StringUtils.isEmpty(errorDescription)) {
60             reportLegacy(params , ctx);
61         }else{
62         apiVersion = ctx.getAttribute("input.common-header.api-ver");
63         eventId = ctx.getAttribute("input.common-header.request-id");
64
65             EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
66             String eventWriteTopic = params.get("event-topic-name");
67             if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
68                 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
69             }else {
70                 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
71             }
72         }
73     }
74
75     private Integer readErrorCode(Map<String, String> params, SvcLogicContext ctx) {
76         Integer errorReportCode = 501;
77         String errorCodeStr = params.get(Constants.DG_ERROR_CODE);
78         errorCodeStr = StringUtils.isEmpty(errorCodeStr)?
79                 ctx.getAttribute(Constants.DG_ERROR_CODE):errorCodeStr;
80         try{
81             errorReportCode =  Integer.parseInt(errorCodeStr);
82         }
83         catch (NumberFormatException e){
84             // Ignore Exception
85         }
86         return errorReportCode;
87     }
88
89     @Override
90     public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
91         Integer successReportCode = 500;
92         String successDescription, apiVersion, eventId;
93         successDescription = params.get(Constants.EVENT_MESSAGE);
94
95         if(StringUtils.isEmpty(successDescription)) {
96             successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
97         }
98
99         apiVersion = ctx.getAttribute("input.common-header.api-ver");
100         eventId = ctx.getAttribute("input.common-header.request-id");
101
102         if (null == successDescription) {
103             successDescription = "Success";
104         }
105         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(successReportCode, successDescription));
106         String eventWriteTopic = params.get("event-topic-name");
107         if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
108             eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
109         }else {
110             eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
111         }
112     }
113
114     private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
115         String errorDescription,apiVersion,eventId ;
116
117         Integer errorCode = readErrorCode(params,ctx);
118         errorDescription = getErrorDescriptionAndAddToCtx(params,ctx);
119
120         apiVersion = ctx.getAttribute("input.common-header.api-ver");
121         eventId = ctx.getAttribute("input.common-header.request-id");
122
123         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
124         String eventWriteTopic = params.get("event-topic-name");
125         if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
126             eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
127         }else {
128             eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
129         }
130     }
131
132     private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
133         String errorDescription;
134         errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
135         if(StringUtils.isEmpty(errorDescription)) {
136             errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
137         }
138         if(StringUtils.isEmpty(errorDescription)) {
139             errorDescription = "Unknown";
140         }
141         addToContextIfNotContains(errorDescription,ctx);
142         return errorDescription;
143     }
144
145     private void addToContextIfNotContains(String errorDescription, SvcLogicContext ctx) {
146         String errorDescriptionFromCtx;
147         errorDescriptionFromCtx = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
148         if(StringUtils.isEmpty(errorDescriptionFromCtx)){
149             errorDescriptionFromCtx = ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE);
150         }
151         if(StringUtils.isEmpty(errorDescriptionFromCtx)){
152             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescription);
153         }else if  (!errorDescriptionFromCtx.contains(errorDescription)){
154             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescriptionFromCtx+ " | "+ errorDescription);
155         }
156     }
157
158 }