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 EventSender eventSender;
42 public DCAEReporterPluginImpl() {
47 * Injected by blueprint
49 * @param eventSender to be set
51 public void setEventSender(EventSender eventSender) {
52 this.eventSender = eventSender;
56 public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
57 String errorDescription, apiVersion, eventId;
59 Integer errorCode = readErrorCode(params, ctx);
60 errorDescription = params.get(Constants.EVENT_MESSAGE);
62 if (StringUtils.isEmpty(errorDescription)) {
63 reportLegacy(params, ctx);
65 apiVersion = ctx.getAttribute("input.common-header.api-ver");
66 eventId = ctx.getAttribute("input.common-header.request-id");
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);
75 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
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;
86 errorReportCode = Integer.parseInt(errorCodeStr);
87 } catch (NumberFormatException e) {
90 return errorReportCode;
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);
99 if (StringUtils.isEmpty(successDescription)) {
100 successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
103 apiVersion = ctx.getAttribute("input.common-header.api-ver");
104 eventId = ctx.getAttribute("input.common-header.request-id");
106 if (null == successDescription) {
107 successDescription = "Success";
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);
116 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
120 private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
121 String errorDescription, apiVersion, eventId;
123 Integer errorCode = readErrorCode(params, ctx);
124 errorDescription = getErrorDescriptionAndAddToCtx(params, ctx);
126 apiVersion = ctx.getAttribute("input.common-header.api-ver");
127 eventId = ctx.getAttribute("input.common-header.request-id");
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);
136 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
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);
146 if (StringUtils.isEmpty(errorDescription)) {
147 errorDescription = "Unknown";
149 addToContextIfNotContains(errorDescription, ctx);
150 return errorDescription;
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);
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);