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
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.message.EventSender;
26 import org.openecomp.appc.adapter.message.MessageDestination;
27 import org.openecomp.appc.adapter.message.event.EventHeader;
28 import org.openecomp.appc.adapter.message.event.EventMessage;
29 import org.openecomp.appc.adapter.message.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         String errorDescription,apiVersion,eventId ;
52
53         Integer errorCode = readErrorCode(params,ctx);
54         errorDescription = params.get(Constants.EVENT_MESSAGE);
55
56         if(StringUtils.isEmpty(errorDescription)) {
57             reportLegacy(params , ctx);
58         }else{
59         apiVersion = ctx.getAttribute("input.common-header.api-ver");
60         eventId = ctx.getAttribute("input.common-header.request-id");
61
62             EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
63             String eventWriteTopic = params.get("event-topic-name");
64             if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
65                 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
66             }else {
67                 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
68             }
69         }
70     }
71
72     private Integer readErrorCode(Map<String, String> params, SvcLogicContext ctx) {
73         Integer errorReportCode = 501;
74         String errorCodeStr = params.get(Constants.DG_ERROR_CODE);
75         errorCodeStr = StringUtils.isEmpty(errorCodeStr)?
76                 ctx.getAttribute(Constants.DG_ERROR_CODE):errorCodeStr;
77         try{
78             errorReportCode =  Integer.parseInt(errorCodeStr);
79         }
80         catch (NumberFormatException e){
81             // Ignore Exception
82         }
83         return errorReportCode;
84     }
85
86     @Override
87     public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
88         Integer successReportCode = 500;
89         String successDescription, apiVersion, eventId;
90         successDescription = params.get(Constants.EVENT_MESSAGE);
91
92         if(StringUtils.isEmpty(successDescription)) {
93             successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
94         }
95
96         apiVersion = ctx.getAttribute("input.common-header.api-ver");
97         eventId = ctx.getAttribute("input.common-header.request-id");
98
99         if (null == successDescription) {
100             successDescription = "Success";
101         }
102         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(successReportCode, successDescription));
103         String eventWriteTopic = params.get("event-topic-name");
104         if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
105             eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
106         }else {
107             eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
108         }
109     }
110
111     private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
112         String errorDescription,apiVersion,eventId ;
113
114         Integer errorCode = readErrorCode(params,ctx);
115         errorDescription = getErrorDescriptionAndAddToCtx(params,ctx);
116
117         apiVersion = ctx.getAttribute("input.common-header.api-ver");
118         eventId = ctx.getAttribute("input.common-header.request-id");
119
120         EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
121         String eventWriteTopic = params.get("event-topic-name");
122         if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
123             eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
124         }else {
125             eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
126         }
127     }
128
129     private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
130         String errorDescription;
131         errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
132         if(StringUtils.isEmpty(errorDescription)) {
133             errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
134         }
135         if(StringUtils.isEmpty(errorDescription)) {
136             errorDescription = "Unknown";
137         }
138         addToContextIfNotContains(errorDescription,ctx);
139         return errorDescription;
140     }
141
142     private void addToContextIfNotContains(String errorDescription, SvcLogicContext ctx) {
143         String errorDescriptionFromCtx;
144         errorDescriptionFromCtx = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
145         if(StringUtils.isEmpty(errorDescriptionFromCtx)){
146             errorDescriptionFromCtx = ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE);
147         }
148         if(StringUtils.isEmpty(errorDescriptionFromCtx)){
149             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescription);
150         }else if  (!errorDescriptionFromCtx.contains(errorDescription)){
151             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescriptionFromCtx+ " | "+ errorDescription);
152         }
153     }
154
155 }