cdc6e956a93998ef6e9591711399d3f42e758178
[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  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.dg.common.impl;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.openecomp.appc.adapter.message.EventSender;
27 import org.openecomp.appc.adapter.message.MessageDestination;
28 import org.openecomp.appc.adapter.message.event.EventHeader;
29 import org.openecomp.appc.adapter.message.event.EventMessage;
30 import org.openecomp.appc.adapter.message.event.EventStatus;
31 import org.openecomp.appc.dg.common.DCAEReporterPlugin;
32 import org.openecomp.appc.exceptions.APPCException;
33 import org.openecomp.sdnc.sli.SvcLogicContext;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
37
38 import java.util.Map;
39
40 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
41
42     private EventSender eventSender;
43
44     public DCAEReporterPluginImpl() {
45         BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
46         ServiceReference sref = bctx.getServiceReference(EventSender.class);
47         eventSender = (EventSender) bctx.getService(sref);
48     }
49
50     @Override
51     public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
52         String errorDescription,apiVersion,eventId ;
53
54         Integer errorCode = readErrorCode(params,ctx);
55         errorDescription = params.get(Constants.EVENT_MESSAGE);
56
57         if(StringUtils.isEmpty(errorDescription)) {
58             reportLegacy(params , ctx);
59         }else{
60         apiVersion = ctx.getAttribute("input.common-header.api-ver");
61         eventId = ctx.getAttribute("input.common-header.request-id");
62
63             EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
64             String eventWriteTopic = params.get("event-topic-name");
65             if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
66                 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
67             }else {
68                 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
69             }
70         }
71     }
72
73     private Integer readErrorCode(Map<String, String> params, SvcLogicContext ctx) {
74         Integer errorReportCode = 501;
75         String errorCodeStr = params.get(Constants.DG_ERROR_CODE);
76         errorCodeStr = StringUtils.isEmpty(errorCodeStr)?
77                 ctx.getAttribute(Constants.DG_ERROR_CODE):errorCodeStr;
78         try{
79             errorReportCode =  Integer.parseInt(errorCodeStr);
80         }
81         catch (NumberFormatException e){
82             // Ignore Exception
83         }
84         return errorReportCode;
85     }
86
87     @Override
88     public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
89         Integer successReportCode = 500;
90         String successDescription, apiVersion, eventId;
91         successDescription = params.get(Constants.EVENT_MESSAGE);
92
93         if(StringUtils.isEmpty(successDescription)) {
94             successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
95         }
96
97         apiVersion = ctx.getAttribute("input.common-header.api-ver");
98         eventId = ctx.getAttribute("input.common-header.request-id");
99
100         if (null == successDescription) {
101             successDescription = "Success";
102         }
103         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(successReportCode, successDescription));
104         String eventWriteTopic = params.get("event-topic-name");
105         if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
106             eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
107         }else {
108             eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
109         }
110     }
111
112     private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
113         String errorDescription,apiVersion,eventId ;
114
115         Integer errorCode = readErrorCode(params,ctx);
116         errorDescription = getErrorDescriptionAndAddToCtx(params,ctx);
117
118         apiVersion = ctx.getAttribute("input.common-header.api-ver");
119         eventId = ctx.getAttribute("input.common-header.request-id");
120
121         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
122         String eventWriteTopic = params.get("event-topic-name");
123         if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
124             eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
125         }else {
126             eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
127         }
128     }
129
130     private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
131         String errorDescription;
132         errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
133         if(StringUtils.isEmpty(errorDescription)) {
134             errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
135         }
136         if(StringUtils.isEmpty(errorDescription)) {
137             errorDescription = "Unknown";
138         }
139         addToContextIfNotContains(errorDescription,ctx);
140         return errorDescription;
141     }
142
143     private void addToContextIfNotContains(String errorDescription, SvcLogicContext ctx) {
144         String errorDescriptionFromCtx;
145         errorDescriptionFromCtx = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
146         if(StringUtils.isEmpty(errorDescriptionFromCtx)){
147             errorDescriptionFromCtx = ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE);
148         }
149         if(StringUtils.isEmpty(errorDescriptionFromCtx)){
150             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescription);
151         }else if  (!errorDescriptionFromCtx.contains(errorDescription)){
152             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescriptionFromCtx+ " | "+ errorDescription);
153         }
154     }
155
156 }