Merge "Rework the datasource access"
[clamp.git] / src / main / java / org / onap / clamp / clds / service / LogServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.service;
25
26 import com.att.ajsc.camunda.core.AttCamundaHistoryEvent;
27 import com.att.ajsc.camunda.core.AttCamundaService;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.google.gson.Gson;
31
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Properties;
36
37 import javax.mail.Message;
38 import javax.mail.MessagingException;
39 import javax.mail.Session;
40 import javax.mail.Transport;
41 import javax.mail.internet.InternetAddress;
42 import javax.mail.internet.MimeMessage;
43 import javax.ws.rs.core.Context;
44
45 import org.apache.commons.mail.Email;
46 import org.apache.commons.mail.SimpleEmail;
47 import org.apache.cxf.jaxrs.ext.MessageContext;
48 import org.camunda.bpm.engine.HistoryService;
49 import org.camunda.bpm.engine.RuntimeService;
50 import org.camunda.bpm.engine.history.HistoricActivityInstance;
51 import org.camunda.bpm.engine.impl.history.event.HistoricActivityInstanceEventEntity;
52 import org.camunda.bpm.engine.runtime.ProcessInstance;
53 import org.onap.clamp.clds.common.LogMessages;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.mail.MailException;
56 import org.springframework.mail.SimpleMailMessage;
57 import org.springframework.mail.javamail.JavaMailSenderImpl;
58 import org.springframework.stereotype.Service;
59
60 @Service
61 public class LogServiceImpl implements LogService {
62     protected static final EELFLogger logger      = EELFManager.getInstance().getLogger(LogServiceImpl.class);
63     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
64
65     @Autowired
66     private RuntimeService            runtimeService;
67
68     @Autowired
69     private HistoryService            historyService;
70
71     @Context
72     private MessageContext            context;
73
74     public void setRuntimeService(RuntimeService runtimeService) {
75         this.runtimeService = runtimeService;
76     }
77
78     public LogServiceImpl() {
79         // needed for instantiation
80     }
81
82     @Override
83     public String logMessage(String logMessageText, String javamail, String springmail, String commonsmail) {
84         logger.info("Value of contexxt : " + context);
85         String convId = null;
86         if (context != null) {
87             convId = context.getHttpServletRequest().getHeader("X-CSI-ConversationId");
88             if (convId == null) {
89                 convId = (String) context.getHttpServletRequest().getAttribute("X-CSI-ConversationId");
90             }
91             context.getHttpServletRequest().setAttribute("CALL_TYPE", "Testing");
92             AttCamundaService.setHttpRequest(context.getHttpServletRequest());
93         }
94         // input variables to example camunda process
95         Map<String, Object> variables = new HashMap<>();
96         variables.put("logMessageText", logMessageText);
97         if (convId != null) {
98             variables.put("conversationId", convId);
99         }
100
101         // BEGIN - added for send mail testing
102         // also added the following to the method signature: ,
103         // @QueryParam("javamail") String javamail, @QueryParam("springmail")
104         // String springmail, @QueryParam("commonsmail") String commonsmail
105         // if javamail parameter provided, assume it contains an email address.
106         // use Java Mail to send an email from that address, to that address
107         if (javamail != null && javamail.length() > 0) {
108             variables.put("javamail", javamail);
109             try {
110                 Properties props = new Properties();
111                 props.put("mail.smtp.host", "smtp.sbc.com"); // eMail.setHostName
112                 Session session = Session.getInstance(props);
113                 MimeMessage msg = new MimeMessage(session);
114
115                 msg.setFrom(new InternetAddress(javamail)); // eMail.setFrom
116
117                 InternetAddress[] fromAddresses = { new InternetAddress(javamail) };
118                 msg.setReplyTo(fromAddresses); // eMail.addReplyTo
119                 msg.setSubject("test message using javax.mail"); // eMail.setSubject
120                 msg.setText(logMessageText); // eMail.setMsg
121
122                 msg.addRecipient(Message.RecipientType.TO, new InternetAddress(javamail)); // eMail.addTo
123                 Transport.send(msg);
124             } catch (MessagingException e) {
125                 logger.error(LogMessages.LOGSERVICE_EMAIL_ERROR, e);
126             }
127         }
128
129         // if springmail parameter provided, assume it contains an email
130         // address.
131         // use Spring Mail to send an email from that address, to that address
132         if (springmail != null && springmail.length() > 0) {
133             variables.put("springmail", springmail);
134             JavaMailSenderImpl sender = new JavaMailSenderImpl();
135             SimpleMailMessage smsg = new SimpleMailMessage();
136
137             try {
138                 sender.setHost("smtp.sbc.com"); // eMail.setHostName
139                 smsg.setFrom(springmail); // eMail.setFrom
140                 smsg.setReplyTo(springmail); // eMail.addReplyTo
141                 smsg.setSubject("test message using spring mail"); // eMail.setSubject
142                 smsg.setText(logMessageText); // eMail.setMsg
143                 smsg.setTo(springmail); // eMail.addTo
144                 sender.send(smsg);
145             } catch (MailException e) {
146                 logger.error(LogMessages.LOGSERVICE_EMAIL_ERROR, e);
147             }
148         }
149
150         // if commonsmail parameter provided, assume it contains an email
151         // address.
152         // use Apache Commons Mail to send an email from that address, to that
153         // address
154         if (commonsmail != null && commonsmail.length() > 0) {
155             variables.put("commonsmail", commonsmail);
156             Email email = new SimpleEmail();
157             try {
158                 email.setHostName("smtp.sbc.com");
159                 email.setFrom(commonsmail);
160                 email.addReplyTo(commonsmail);
161                 email.setSubject("test message using commons mail");
162                 email.setMsg(logMessageText);
163                 email.addTo(commonsmail);
164                 java.net.URL classUrl = this.getClass().getResource("com.sun.mail.util.TraceInputStream");
165                 if (classUrl != null) {
166                     logger.info(LogMessages.LOGSERVICE_EMAIL_CLASS, classUrl.getFile());
167                 } else {
168                     logger.info(LogMessages.LOGSERVICE_EMAIL_CLASS_NULL);
169                 }
170                 email.send();
171             } catch (Exception e) {
172                 logger.error(LogMessages.LOGSERVICE_EMAIL_ERROR, e);
173             }
174         }
175         // END - added for send mail testing
176
177         // execute example camunda process, log-message-wf
178         ProcessInstance pi = runtimeService.startProcessInstanceByKey("log-message-wf", variables);
179         AttCamundaService.setHttpRequest(null);
180         // return text message of what was done
181         return "Started processDefinitionId=" + pi.getProcessDefinitionId() + ", processInstanceId="
182                 + pi.getProcessInstanceId() + ", to log message: " + logMessageText;
183     }
184
185     @Override
186     public String postLogMessage(String histEventList) {
187         String message = "no logs Created";
188         logger.info("value of history events:" + histEventList);
189         Gson gson = new Gson();
190         AttCamundaHistoryEvent attCamundaHistoryEvent = gson.fromJson(histEventList, AttCamundaHistoryEvent.class);
191         if (attCamundaHistoryEvent != null && attCamundaHistoryEvent.getProcInstId() != null) {
192             logger.info(LogMessages.PROCESS_INSTANCE_ID, attCamundaHistoryEvent.getProcInstId());
193             if (context != null && context.getHttpServletRequest() != null
194                     && context.getHttpServletRequest().getAttribute("PERFORMANCE_TRACKER_BEAN") != null) {
195                 context.getHttpServletRequest().setAttribute("CALL_TYPE", "Testing");
196                 List<HistoricActivityInstance> histActInstList = historyService.createHistoricActivityInstanceQuery()
197                         .processInstanceId(attCamundaHistoryEvent.getProcInstId()).list();
198
199                 if (histActInstList != null && histActInstList.size() > 0) {
200                     for (HistoricActivityInstance currHistoricActivityInstance : histActInstList) {
201                         if (currHistoricActivityInstance != null
202                                 && currHistoricActivityInstance.getActivityName() != null
203                                 && currHistoricActivityInstance.getStartTime() != null
204                                 && currHistoricActivityInstance.getEndTime() != null) {
205                             logger.info("value of serviceTrack:" + currHistoricActivityInstance);
206                             message = "Log Entry Created";
207                             logger.info(message);
208                         }
209                     }
210                 }
211                 if (attCamundaHistoryEvent.getHistoryEventList() != null
212                         && attCamundaHistoryEvent.getHistoryEventList().size() > 0) {
213                     List<HistoricActivityInstanceEventEntity> historyEventList = attCamundaHistoryEvent
214                             .getHistoryEventList();
215                     for (HistoricActivityInstanceEventEntity actiEvent : historyEventList) {
216                         // resolve null pointer exception if
217                         // actiEvent.getActivityName()
218                         message = "Log Entry Created";
219                     }
220                 }
221             }
222         }
223         return message;
224     }
225
226     @Override
227     public String createLogMessage(String startTime, String endTime, String serviceName) {
228         String message = "no logs Created";
229
230         if (context != null && context.getHttpServletRequest() != null
231                 && context.getHttpServletRequest().getAttribute("PERFORMANCE_TRACKER_BEAN") != null) {
232             context.getHttpServletRequest().setAttribute("X-CSI-ClientApp", "AJSC-CSI~sdsds");
233             /*
234              * PerformanceTrackingBean trackingBean =(PerformanceTrackingBean)
235              * context.getHttpServletRequest().getAttribute(
236              * "PERFORMANCE_TRACKER_BEAN");
237              * PerformanceTracking.addInvokeServiceTrack(trackingBean,
238              * serviceName, Long.valueOf(startTime), Long.valueOf(endTime),
239              * "Completed", 500, 1000) ;
240              */
241             message = "Log Entry Created";
242         }
243         // return text message of what was done
244         return message;
245     }
246
247     @Override
248     public String createLogMessageUsingHistory(String procInstId, String histEventList) {
249         String message = "no logs Created";
250         logger.info("value of history events:" + histEventList);
251         logger.info("value of events:" + histEventList + ":" + histEventList);
252         if (context != null && context.getHttpServletRequest() != null
253                 && context.getHttpServletRequest().getAttribute("PERFORMANCE_TRACKER_BEAN") != null) {
254             context.getHttpServletRequest().setAttribute("CALL_TYPE", "Testing");
255             List<HistoricActivityInstance> histActInstList = historyService.createHistoricActivityInstanceQuery()
256                     .processInstanceId(procInstId).list();
257
258             if (histActInstList != null && histActInstList.size() > 0) {
259                 for (HistoricActivityInstance currHistoricActivityInstance : histActInstList) {
260                     if (currHistoricActivityInstance != null && currHistoricActivityInstance.getActivityName() != null
261                             && currHistoricActivityInstance.getStartTime() != null
262                             && currHistoricActivityInstance.getEndTime() != null) {
263                         logger.info("value of serviceTrack:" + currHistoricActivityInstance);
264                         message = "Log Entry Created";
265                         logger.info(message);
266                     }
267                 }
268             }
269         }
270         return message;
271     }
272
273     @Override
274     public String CreateHistLog(String procInstId) {
275         String message = "no logs Created";
276         if (context != null && context.getHttpServletRequest() != null
277                 && context.getHttpServletRequest().getAttribute("PERFORMANCE_TRACKER_BEAN") != null) {
278             List<HistoricActivityInstance> histActInstList = historyService.createHistoricActivityInstanceQuery()
279                     .processInstanceId(procInstId).list();
280
281             if (histActInstList != null && histActInstList.size() > 0) {
282                 for (HistoricActivityInstance currHistoricActivityInstance : histActInstList) {
283                     if (currHistoricActivityInstance != null && currHistoricActivityInstance.getActivityName() != null
284                             && currHistoricActivityInstance.getStartTime() != null
285                             && currHistoricActivityInstance.getEndTime() != null) {
286                         logger.info("value of serviceTrack:" + currHistoricActivityInstance);
287                         context.getHttpServletRequest().setAttribute("X-CSI-ClientApp", "AJSC-CSI~sdsds");
288                         message = "Log Entry Created";
289                     }
290                 }
291             }
292         }
293         return message;
294     }
295 }