2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.dg.common.impl;
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;
37 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
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;
44 public DCAEReporterPluginImpl() {
49 * Injected by blueprint
51 * @param eventSender to be set
53 public void setEventSender(EventSenderInterface eventSender) {
54 this.eventSender = eventSender;
58 public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
59 String errorDescription;
63 Integer errorCode = readErrorCode(params, ctx);
64 errorDescription = params.get(Constants.EVENT_MESSAGE);
66 if (StringUtils.isEmpty(errorDescription)) {
67 reportLegacy(params, ctx);
69 apiVersion = ctx.getAttribute(ATTR_API_VERSION);
70 eventId = ctx.getAttribute(ATTR_REQUEST_ID);
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);
79 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
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;
90 errorReportCode = Integer.parseInt(errorCodeStr);
91 } catch (NumberFormatException e) {
94 return errorReportCode;
98 public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
99 Integer successReportCode = 500;
100 String successDescription;
104 successDescription = params.get(Constants.EVENT_MESSAGE);
106 if (StringUtils.isEmpty(successDescription)) {
107 successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
110 apiVersion = ctx.getAttribute(ATTR_API_VERSION);
111 eventId = ctx.getAttribute(ATTR_REQUEST_ID);
113 if (null == successDescription) {
114 successDescription = "Success";
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);
123 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
127 private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
128 String errorDescription;
132 Integer errorCode = readErrorCode(params, ctx);
133 errorDescription = getErrorDescriptionAndAddToCtx(params, ctx);
135 apiVersion = ctx.getAttribute(ATTR_API_VERSION);
136 eventId = ctx.getAttribute(ATTR_REQUEST_ID);
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);
145 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
149 private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
150 String errorDescription;
151 errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
153 if (StringUtils.isEmpty(errorDescription)) {
154 errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
156 if (StringUtils.isEmpty(errorDescription)) {
157 errorDescription = "Unknown";
159 addToContextIfNotContains(errorDescription, ctx);
160 return errorDescription;
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);
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);