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.openecomp.appc.dg.common.impl;
27 import org.apache.commons.lang3.StringUtils;
28 import org.openecomp.appc.adapter.message.EventSender;
29 import org.openecomp.appc.adapter.message.MessageDestination;
30 import org.openecomp.appc.adapter.message.event.EventHeader;
31 import org.openecomp.appc.adapter.message.event.EventMessage;
32 import org.openecomp.appc.adapter.message.event.EventStatus;
33 import org.openecomp.appc.dg.common.DCAEReporterPlugin;
34 import org.openecomp.appc.exceptions.APPCException;
35 import org.openecomp.sdnc.sli.SvcLogicContext;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.FrameworkUtil;
38 import org.osgi.framework.ServiceReference;
42 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
44 private EventSender eventSender;
46 public DCAEReporterPluginImpl() {
47 BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
48 ServiceReference sref = bctx.getServiceReference(EventSender.class);
49 eventSender = (EventSender) bctx.getService(sref);
53 public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
54 String errorDescription,apiVersion,eventId ;
56 Integer errorCode = readErrorCode(params,ctx);
57 errorDescription = params.get(Constants.EVENT_MESSAGE);
59 if(StringUtils.isEmpty(errorDescription)) {
60 reportLegacy(params , ctx);
62 apiVersion = ctx.getAttribute("input.common-header.api-ver");
63 eventId = ctx.getAttribute("input.common-header.request-id");
65 EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
66 String eventWriteTopic = params.get("event-topic-name");
67 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
68 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
70 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
75 private Integer readErrorCode(Map<String, String> params, SvcLogicContext ctx) {
76 Integer errorReportCode = 501;
77 String errorCodeStr = params.get(Constants.DG_ERROR_CODE);
78 errorCodeStr = StringUtils.isEmpty(errorCodeStr)?
79 ctx.getAttribute(Constants.DG_ERROR_CODE):errorCodeStr;
81 errorReportCode = Integer.parseInt(errorCodeStr);
83 catch (NumberFormatException e){
86 return errorReportCode;
90 public void reportSuccess(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
91 Integer successReportCode = 500;
92 String successDescription, apiVersion, eventId;
93 successDescription = params.get(Constants.EVENT_MESSAGE);
95 if(StringUtils.isEmpty(successDescription)) {
96 successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
99 apiVersion = ctx.getAttribute("input.common-header.api-ver");
100 eventId = ctx.getAttribute("input.common-header.request-id");
102 if (null == successDescription) {
103 successDescription = "Success";
105 EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(successReportCode, successDescription));
106 String eventWriteTopic = params.get("event-topic-name");
107 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
108 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
110 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
114 private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
115 String errorDescription,apiVersion,eventId ;
117 Integer errorCode = readErrorCode(params,ctx);
118 errorDescription = getErrorDescriptionAndAddToCtx(params,ctx);
120 apiVersion = ctx.getAttribute("input.common-header.api-ver");
121 eventId = ctx.getAttribute("input.common-header.request-id");
123 EventMessage eventMessage = new EventMessage(new EventHeader((new java.util.Date()).toString(), apiVersion, eventId), new EventStatus(errorCode, errorDescription));
124 String eventWriteTopic = params.get("event-topic-name");
125 if(!StringUtils.isEmpty(eventWriteTopic) && eventWriteTopic!=null){
126 eventSender.sendEvent(MessageDestination.DCAE, eventMessage,eventWriteTopic);
128 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
132 private String getErrorDescriptionAndAddToCtx(Map<String, String> params, SvcLogicContext ctx) {
133 String errorDescription;
134 errorDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
135 if(StringUtils.isEmpty(errorDescription)) {
136 errorDescription = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
138 if(StringUtils.isEmpty(errorDescription)) {
139 errorDescription = "Unknown";
141 addToContextIfNotContains(errorDescription,ctx);
142 return errorDescription;
145 private void addToContextIfNotContains(String errorDescription, SvcLogicContext ctx) {
146 String errorDescriptionFromCtx;
147 errorDescriptionFromCtx = ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE);
148 if(StringUtils.isEmpty(errorDescriptionFromCtx)){
149 errorDescriptionFromCtx = ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE);
151 if(StringUtils.isEmpty(errorDescriptionFromCtx)){
152 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescription);
153 }else if (!errorDescriptionFromCtx.contains(errorDescription)){
154 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, errorDescriptionFromCtx+ " | "+ errorDescription);