Second part of onap rename
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / onap / appc / dg / common / impl / DCAEReporterPluginImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.common.impl;
26
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;
36
37 import java.util.Map;
38
39 public class DCAEReporterPluginImpl implements DCAEReporterPlugin {
40
41     private EventSender eventSender;
42
43     public DCAEReporterPluginImpl() {
44         // do nothing
45     }
46
47     /**
48      * Injected by blueprint
49      *
50      * @param eventSender to be set
51      */
52     public void setEventSender(EventSender eventSender) {
53         this.eventSender = eventSender;
54     }
55
56     @Override
57     public void report(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
58         String errorDescription,apiVersion,eventId ;
59
60         Integer errorCode = readErrorCode(params,ctx);
61         errorDescription = params.get(Constants.EVENT_MESSAGE);
62
63         if(StringUtils.isEmpty(errorDescription)) {
64             reportLegacy(params , ctx);
65         }else{
66         apiVersion = ctx.getAttribute("input.common-header.api-ver");
67         eventId = ctx.getAttribute("input.common-header.request-id");
68
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);
75             }else {
76                 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
77             }
78         }
79     }
80
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;
86         try{
87             errorReportCode =  Integer.parseInt(errorCodeStr);
88         }
89         catch (NumberFormatException e){
90             // Ignore Exception
91         }
92         return errorReportCode;
93     }
94
95     @Override
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);
100
101         if(StringUtils.isEmpty(successDescription)) {
102             successDescription = params.get(Constants.DG_OUTPUT_STATUS_MESSAGE);
103         }
104
105         apiVersion = ctx.getAttribute("input.common-header.api-ver");
106         eventId = ctx.getAttribute("input.common-header.request-id");
107
108         if (null == successDescription) {
109             successDescription = "Success";
110         }
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);
117         }else {
118             eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
119         }
120     }
121
122     private void reportLegacy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
123         String errorDescription,apiVersion,eventId ;
124
125         Integer errorCode = readErrorCode(params,ctx);
126         errorDescription = getErrorDescriptionAndAddToCtx(params,ctx);
127
128         apiVersion = ctx.getAttribute("input.common-header.api-ver");
129         eventId = ctx.getAttribute("input.common-header.request-id");
130
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);
137         }else {
138             eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
139         }
140     }
141
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);
147         }
148         if(StringUtils.isEmpty(errorDescription)) {
149             errorDescription = "Unknown";
150         }
151         addToContextIfNotContains(errorDescription,ctx);
152         return errorDescription;
153     }
154
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);
160         }
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);
165         }
166     }
167
168 }