2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dg.common.impl;
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;
38 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
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;
45 public DCAEReporterPluginImpl() {
50 * Injected by blueprint
52 * @param eventSender to be set
54 public void setEventSender(EventSender eventSender) {
55 this.eventSender = eventSender;
59 public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
60 String errorDescription;
64 Integer errorCode = readErrorCode(params, ctx);
65 errorDescription = params.get(Constants.EVENT_MESSAGE);
67 if (StringUtils.isEmpty(errorDescription)) {
68 reportLegacy(params, ctx);
70 apiVersion = ctx.getAttribute(ATTR_API_VERSION);
71 eventId = ctx.getAttribute(ATTR_REQUEST_ID);
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);
80 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
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;
91 errorReportCode = Integer.parseInt(errorCodeStr);
92 } catch (NumberFormatException e) {
95 return errorReportCode;
99 public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
100 Integer successReportCode = 500;
101 String successDescription;
105 successDescription = params.get(Constants.EVENT_MESSAGE);
107 if (StringUtils.isEmpty(successDescription)) {
108 successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
111 apiVersion = ctx.getAttribute(ATTR_API_VERSION);
112 eventId = ctx.getAttribute(ATTR_REQUEST_ID);
114 if (null == successDescription) {
115 successDescription = "Success";
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);
124 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
128 private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
129 String errorDescription;
133 Integer errorCode = readErrorCode(params, ctx);
134 errorDescription = getErrorDescriptionAndAddToCtx(params, ctx);
136 apiVersion = ctx.getAttribute(ATTR_API_VERSION);
137 eventId = ctx.getAttribute(ATTR_REQUEST_ID);
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);
146 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
150 private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
151 String errorDescription;
152 errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
154 if (StringUtils.isEmpty(errorDescription)) {
155 errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
157 if (StringUtils.isEmpty(errorDescription)) {
158 errorDescription = "Unknown";
160 addToContextIfNotContains(errorDescription, ctx);
161 return errorDescription;
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);
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);