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