2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.dg.common.impl;
25 import org.apache.commons.lang3.StringUtils;
26 import org.openecomp.appc.adapter.message.EventSender;
27 import org.openecomp.appc.adapter.message.MessageDestination;
28 import org.openecomp.appc.adapter.message.event.EventHeader;
29 import org.openecomp.appc.adapter.message.event.EventMessage;
30 import org.openecomp.appc.adapter.message.event.EventStatus;
31 import org.openecomp.appc.dg.common.DCAEReporterPlugin;
32 import org.openecomp.appc.exceptions.APPCException;
33 import org.openecomp.sdnc.sli.SvcLogicContext;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
40 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
42 private EventSender eventSender;
44 public DCAEReporterPluginImpl() {
45 BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
46 ServiceReference sref = bctx.getServiceReference(EventSender.class);
47 eventSender = (EventSender) bctx.getService(sref);
51 public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
52 String errorDescription,apiVersion,eventId ;
54 Integer errorCode = readErrorCode(params,ctx);
55 errorDescription = params.get(Constants.EVENT_MESSAGE);
57 if(StringUtils.isEmpty(errorDescription)) {
58 reportLegacy(params , ctx);
60 apiVersion = ctx.getAttribute("input.common-header.api-ver");
61 eventId = ctx.getAttribute("input.common-header.request-id");
63 EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
64 String eventWriteTopic = params.get("event-topic-name");
65 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
66 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
68 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
73 private Integer readErrorCode(Map<String, String> params, SvcLogicContext ctx) {
74 Integer errorReportCode = 501;
75 String errorCodeStr = params.get(Constants.DG_ERROR_CODE);
76 errorCodeStr = StringUtils.isEmpty(errorCodeStr)?
77 ctx.getAttribute(Constants.DG_ERROR_CODE):errorCodeStr;
79 errorReportCode = Integer.parseInt(errorCodeStr);
81 catch (NumberFormatException e){
84 return errorReportCode;
88 public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
89 Integer successReportCode = 500;
90 String successDescription, apiVersion, eventId;
91 successDescription = params.get(Constants.EVENT_MESSAGE);
93 if(StringUtils.isEmpty(successDescription)) {
94 successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
97 apiVersion = ctx.getAttribute("input.common-header.api-ver");
98 eventId = ctx.getAttribute("input.common-header.request-id");
100 if (null == successDescription) {
101 successDescription = "Success";
103 EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(successReportCode, successDescription));
104 String eventWriteTopic = params.get("event-topic-name");
105 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
106 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
108 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
112 private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
113 String errorDescription,apiVersion,eventId ;
115 Integer errorCode = readErrorCode(params,ctx);
116 errorDescription = getErrorDescriptionAndAddToCtx(params,ctx);
118 apiVersion = ctx.getAttribute("input.common-header.api-ver");
119 eventId = ctx.getAttribute("input.common-header.request-id");
121 EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
122 String eventWriteTopic = params.get("event-topic-name");
123 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
124 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
126 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
130 private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
131 String errorDescription;
132 errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
133 if(StringUtils.isEmpty(errorDescription)) {
134 errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
136 if(StringUtils.isEmpty(errorDescription)) {
137 errorDescription = "Unknown";
139 addToContextIfNotContains(errorDescription,ctx);
140 return errorDescription;
143 private void addToContextIfNotContains(String errorDescription, SvcLogicContext ctx) {
144 String errorDescriptionFromCtx;
145 errorDescriptionFromCtx = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
146 if(StringUtils.isEmpty(errorDescriptionFromCtx)){
147 errorDescriptionFromCtx = ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE);
149 if(StringUtils.isEmpty(errorDescriptionFromCtx)){
150 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescription);
151 }else if (!errorDescriptionFromCtx.contains(errorDescription)){
152 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescriptionFromCtx+ " | "+ errorDescription);