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;
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.appc.adapter.message.EventSender;
29 import org.onap.appc.adapter.message.MessageDestination;
30 import org.onap.appc.adapter.message.event.EventHeader;
31 import org.onap.appc.adapter.message.event.EventMessage;
32 import org.onap.appc.adapter.message.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;
39 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
41 private EventSender eventSender;
43 public DCAEReporterPluginImpl() {
48 * Injected by blueprint
50 * @param eventSender to be set
52 public void setEventSender(EventSender eventSender) {
53 this.eventSender = eventSender;
57 public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
58 String errorDescription,apiVersion,eventId ;
60 Integer errorCode = readErrorCode(params,ctx);
61 errorDescription = params.get(Constants.EVENT_MESSAGE);
63 if(StringUtils.isEmpty(errorDescription)) {
64 reportLegacy(params , ctx);
66 apiVersion = ctx.getAttribute("input.common-header.api-ver");
67 eventId = ctx.getAttribute("input.common-header.request-id");
69 EventMessage eventMessage = new EventMessage(new EventHeader(
70 (new java.util.Date()).toString(), apiVersion, eventId),
71 new EventStatus(errorCode, errorDescription));
72 String eventWriteTopic = params.get("event-topic-name");
73 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
74 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
76 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
81 private Integer readErrorCode(Map<String, String> params, SvcLogicContext ctx) {
82 Integer errorReportCode = 501;
83 String errorCodeStr = params.get(Constants.DG_ERROR_CODE);
84 errorCodeStr = StringUtils.isEmpty(errorCodeStr)?
85 ctx.getAttribute(Constants.DG_ERROR_CODE):errorCodeStr;
87 errorReportCode = Integer.parseInt(errorCodeStr);
89 catch (NumberFormatException e){
92 return errorReportCode;
96 public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
97 Integer successReportCode = 500;
98 String successDescription, apiVersion, eventId;
99 successDescription = params.get(Constants.EVENT_MESSAGE);
101 if(StringUtils.isEmpty(successDescription)) {
102 successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
105 apiVersion = ctx.getAttribute("input.common-header.api-ver");
106 eventId = ctx.getAttribute("input.common-header.request-id");
108 if (null == successDescription) {
109 successDescription = "Success";
111 EventMessage eventMessage = new EventMessage(new EventHeader(
112 (new java.util.Date()).toString(), apiVersion, eventId),
113 new EventStatus(successReportCode, successDescription));
114 String eventWriteTopic = params.get("event-topic-name");
115 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
116 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
118 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
122 private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
123 String errorDescription,apiVersion,eventId ;
125 Integer errorCode = readErrorCode(params,ctx);
126 errorDescription = getErrorDescriptionAndAddToCtx(params,ctx);
128 apiVersion = ctx.getAttribute("input.common-header.api-ver");
129 eventId = ctx.getAttribute("input.common-header.request-id");
131 EventMessage eventMessage = new EventMessage(new EventHeader(
132 (new java.util.Date()).toString(), apiVersion, eventId),
133 new EventStatus(errorCode, errorDescription));
134 String eventWriteTopic = params.get("event-topic-name");
135 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
136 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
138 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
142 private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
143 String errorDescription;
144 errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
145 if(StringUtils.isEmpty(errorDescription)) {
146 errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
148 if(StringUtils.isEmpty(errorDescription)) {
149 errorDescription = "Unknown";
151 addToContextIfNotContains(errorDescription,ctx);
152 return errorDescription;
155 private void addToContextIfNotContains(String errorDescription, SvcLogicContext ctx) {
156 String errorDescriptionFromCtx;
157 errorDescriptionFromCtx = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
158 if(StringUtils.isEmpty(errorDescriptionFromCtx)){
159 errorDescriptionFromCtx = ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE);
161 if(StringUtils.isEmpty(errorDescriptionFromCtx)){
162 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescription);
163 }else if (!errorDescriptionFromCtx.contains(errorDescription)){
164 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescriptionFromCtx+ " | "+ errorDescription);