Fix dg-common bundle error
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / onap / appc / dg / common / impl / DCAEReporterPluginImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.dg.common.impl;
25
26 import java.util.Map;
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.appc.srvcomm.messaging.event.EventSenderInterface;
29 import org.onap.appc.srvcomm.messaging.MessageDestination;
30 import org.onap.appc.srvcomm.messaging.event.EventHeader;
31 import org.onap.appc.srvcomm.messaging.event.EventMessage;
32 import org.onap.appc.srvcomm.messaging.event.EventStatus;
33 import org.onap.appc.dg.common.DCAEReporterPlugin;
34 import org.onap.appc.exceptions.APPCException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36
37 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
38
39     private static final String ATTR_API_VERSION = "input.common-header.api-ver";
40     private static final String ATTR_REQUEST_ID = "input.common-header.request-id";
41     private static final String PARAM_EVENT_TOPIC_NAME = "event-topic-name";
42     private EventSenderInterface eventSender;
43
44     public DCAEReporterPluginImpl() {
45         // do nothing
46     }
47
48     /**
49      * Injected by blueprint
50      *
51      * @param eventSender to be set
52      */
53     public void setEventSender(EventSenderInterface eventSender) {
54         this.eventSender = eventSender;
55     }
56
57     @Override
58     public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
59         String errorDescription;
60         String apiVersion;
61         String eventId;
62
63         Integer errorCode = readErrorCode(params, ctx);
64         errorDescription = params.get(Constants.EVENT_MESSAGE);
65
66         if (StringUtils.isEmpty(errorDescription)) {
67             reportLegacy(params, ctx);
68         } else {
69             apiVersion = ctx.getAttribute(ATTR_API_VERSION);
70             eventId = ctx.getAttribute(ATTR_REQUEST_ID);
71
72             EventMessage eventMessage = new EventMessage(new EventHeader(
73                 (new java.util.Date()).toString(), apiVersion, eventId),
74                 new EventStatus(errorCode, errorDescription));
75             String eventWriteTopic = params.get(PARAM_EVENT_TOPIC_NAME);
76             if (!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic != null) {
77                 eventSender.sendEvent(MessageDestination.DCAE, eventMessage, eventWriteTopic);
78             } else {
79                 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
80             }
81         }
82     }
83
84     private Integer readErrorCode(Map<String, String> params, SvcLogicContext ctx) {
85         Integer errorReportCode = 501;
86         String errorCodeStr = params.get(Constants.DG_ERROR_CODE);
87         errorCodeStr = StringUtils.isEmpty(errorCodeStr) ?
88             ctx.getAttribute(Constants.DG_ERROR_CODE) : errorCodeStr;
89         try {
90             errorReportCode = Integer.parseInt(errorCodeStr);
91         } catch (NumberFormatException e) {
92             // Ignore Exception
93         }
94         return errorReportCode;
95     }
96
97     @Override
98     public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
99         Integer successReportCode = 500;
100         String successDescription;
101         String apiVersion;
102         String eventId;
103
104         successDescription = params.get(Constants.EVENT_MESSAGE);
105
106         if (StringUtils.isEmpty(successDescription)) {
107             successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
108         }
109
110         apiVersion = ctx.getAttribute(ATTR_API_VERSION);
111         eventId = ctx.getAttribute(ATTR_REQUEST_ID);
112
113         if (null == successDescription) {
114             successDescription = "Success";
115         }
116         EventMessage eventMessage = new EventMessage(new EventHeader(
117             (new java.util.Date()).toString(), apiVersion, eventId),
118             new EventStatus(successReportCode, successDescription));
119         String eventWriteTopic = params.get(PARAM_EVENT_TOPIC_NAME);
120         if (!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic != null) {
121             eventSender.sendEvent(MessageDestination.DCAE, eventMessage, eventWriteTopic);
122         } else {
123             eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
124         }
125     }
126
127     private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
128         String errorDescription;
129         String apiVersion;
130         String eventId;
131
132         Integer errorCode = readErrorCode(params, ctx);
133         errorDescription = getErrorDescriptionAndAddToCtx(params, ctx);
134
135         apiVersion = ctx.getAttribute(ATTR_API_VERSION);
136         eventId = ctx.getAttribute(ATTR_REQUEST_ID);
137
138         EventMessage eventMessage = new EventMessage(new EventHeader(
139             (new java.util.Date()).toString(), apiVersion, eventId),
140             new EventStatus(errorCode, errorDescription));
141         String eventWriteTopic = params.get(PARAM_EVENT_TOPIC_NAME);
142         if (!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic != null) {
143             eventSender.sendEvent(MessageDestination.DCAE, eventMessage, eventWriteTopic);
144         } else {
145             eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
146         }
147     }
148
149     private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
150         String errorDescription;
151         errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
152
153         if (StringUtils.isEmpty(errorDescription)) {
154             errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
155         }
156         if (StringUtils.isEmpty(errorDescription)) {
157             errorDescription = "Unknown";
158         }
159         addToContextIfNotContains(errorDescription, ctx);
160         return errorDescription;
161     }
162
163     private void addToContextIfNotContains(String errorDescription, SvcLogicContext ctx) {
164         String errorDescriptionFromCtx;
165         errorDescriptionFromCtx = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
166         if (StringUtils.isEmpty(errorDescriptionFromCtx)) {
167             errorDescriptionFromCtx = ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE);
168         }
169         if (StringUtils.isEmpty(errorDescriptionFromCtx)) {
170             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescription);
171         } else if (!errorDescriptionFromCtx.contains(errorDescription)) {
172             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescriptionFromCtx + " | " + errorDescription);
173         }
174     }
175 }