2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.dg.common.impl;
24 import org.apache.commons.lang3.StringUtils;
25 import org.openecomp.appc.adapter.message.EventSender;
26 import org.openecomp.appc.adapter.message.MessageDestination;
27 import org.openecomp.appc.adapter.message.event.EventHeader;
28 import org.openecomp.appc.adapter.message.event.EventMessage;
29 import org.openecomp.appc.adapter.message.event.EventStatus;
30 import org.openecomp.appc.dg.common.DCAEReporterPlugin;
31 import org.openecomp.appc.exceptions.APPCException;
32 import org.openecomp.sdnc.sli.SvcLogicContext;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.FrameworkUtil;
35 import org.osgi.framework.ServiceReference;
39 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
41 private EventSender eventSender;
43 public DCAEReporterPluginImpl() {
44 BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
45 ServiceReference sref = bctx.getServiceReference(EventSender.class);
46 eventSender = (EventSender) bctx.getService(sref);
50 public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
51 String errorDescription,apiVersion,eventId ;
53 Integer errorCode = readErrorCode(params,ctx);
54 errorDescription = params.get(Constants.EVENT_MESSAGE);
56 if(StringUtils.isEmpty(errorDescription)) {
57 reportLegacy(params , ctx);
59 apiVersion = ctx.getAttribute("input.common-header.api-ver");
60 eventId = ctx.getAttribute("input.common-header.request-id");
62 EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
63 String eventWriteTopic = params.get("event-topic-name");
64 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
65 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
67 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
72 private Integer readErrorCode(Map<String, String> params, SvcLogicContext ctx) {
73 Integer errorReportCode = 501;
74 String errorCodeStr = params.get(Constants.DG_ERROR_CODE);
75 errorCodeStr = StringUtils.isEmpty(errorCodeStr)?
76 ctx.getAttribute(Constants.DG_ERROR_CODE):errorCodeStr;
78 errorReportCode = Integer.parseInt(errorCodeStr);
80 catch (NumberFormatException e){
83 return errorReportCode;
87 public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
88 Integer successReportCode = 500;
89 String successDescription, apiVersion, eventId;
90 successDescription = params.get(Constants.EVENT_MESSAGE);
92 if(StringUtils.isEmpty(successDescription)) {
93 successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
96 apiVersion = ctx.getAttribute("input.common-header.api-ver");
97 eventId = ctx.getAttribute("input.common-header.request-id");
99 if (null == successDescription) {
100 successDescription = "Success";
102 EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(successReportCode, successDescription));
103 String eventWriteTopic = params.get("event-topic-name");
104 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
105 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
107 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
111 private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
112 String errorDescription,apiVersion,eventId ;
114 Integer errorCode = readErrorCode(params,ctx);
115 errorDescription = getErrorDescriptionAndAddToCtx(params,ctx);
117 apiVersion = ctx.getAttribute("input.common-header.api-ver");
118 eventId = ctx.getAttribute("input.common-header.request-id");
120 EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
121 String eventWriteTopic = params.get("event-topic-name");
122 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
123 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
125 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
129 private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
130 String errorDescription;
131 errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
132 if(StringUtils.isEmpty(errorDescription)) {
133 errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
135 if(StringUtils.isEmpty(errorDescription)) {
136 errorDescription = "Unknown";
138 addToContextIfNotContains(errorDescription,ctx);
139 return errorDescription;
142 private void addToContextIfNotContains(String errorDescription, SvcLogicContext ctx) {
143 String errorDescriptionFromCtx;
144 errorDescriptionFromCtx = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
145 if(StringUtils.isEmpty(errorDescriptionFromCtx)){
146 errorDescriptionFromCtx = ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE);
148 if(StringUtils.isEmpty(errorDescriptionFromCtx)){
149 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescription);
150 }else if (!errorDescriptionFromCtx.contains(errorDescription)){
151 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescriptionFromCtx+ " | "+ errorDescription);