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