1fccfda7815dab8ed683d1cdd4cd12936ee88b55
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / admin / PolicyNotificationMail.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * Modifications Copyright (C) 2019 Bell Canada
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  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.admin;
24
25 import java.io.File;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.List;
31 import java.util.Properties;
32
33 import javax.mail.internet.InternetAddress;
34 import javax.mail.internet.MimeMessage;
35 import javax.script.SimpleBindings;
36
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.controller.PolicyController;
40 import org.onap.policy.rest.dao.CommonClassDao;
41 import org.onap.policy.rest.jpa.PolicyVersion;
42 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
43 import org.onap.policy.xacml.api.XACMLErrorConstants;
44 import org.springframework.beans.factory.annotation.Configurable;
45 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
46 import org.springframework.context.annotation.Bean;
47 import org.springframework.mail.javamail.JavaMailSenderImpl;
48 import org.springframework.mail.javamail.MimeMessageHelper;
49
50 /**
51  * Send policy notification mail depending on the mode for every policy being watched
52  */
53 @Configurable
54 public class PolicyNotificationMail{
55     private static final String POLICY_WATCHING_MESSAGE = "The Policy Which you are watching in  ";
56     private static final String EMAIL_MESSAGE_POSTSCRIPT = "Policy Notification System  (please don't respond to this email)";
57     private static final String ACTIVE_VERSION = "Active Version  : ";
58     private static final String SCOPE_POLICY_NAME = "Scope + Policy Name  : ";
59     private static final String DELETED_TIME = "Deleted Time  : ";
60     private static final String DELETED_BY = "Deleted By : ";
61     private static Logger policyLogger  = FlexLogger.getLogger(PolicyNotificationMail.class);
62
63     @Bean
64     public JavaMailSenderImpl javaMailSenderImpl(){
65         JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
66         mailSender.setHost(PolicyController.getSmtpHost());
67         mailSender.setPort(Integer.parseInt(PolicyController.getSmtpPort()));
68         mailSender.setUsername(PolicyController.getSmtpUsername());
69         mailSender.setPassword(PolicyController.getSmtpPassword());
70         Properties prop = mailSender.getJavaMailProperties();
71         prop.put("mail.transport.protocol", "smtp");
72         prop.put("mail.smtp.auth", "true");
73         prop.put("mail.smtp.starttls.enable", "true");
74         prop.put("mail.debug", "true");
75         return mailSender;
76     }
77
78     /**
79      * Depending on the mode of operation on the policy, compose the subject and message.
80      * Invoke another internal method to actual send the mail. If the watch list is empty , then
81      * this method returns without sending notification mail
82      * @param entityItem Database item from which policy name could be extracted
83      * @param policyName Name of the policy for which notification is to be sent
84      * @param mode kind of operation done on the policy
85      * @param policyNotificationDao database access object for policy
86      */
87     public void sendMail(PolicyVersion entityItem, String policyName, String mode, CommonClassDao policyNotificationDao) {
88
89         String subject = "";
90         String message = "";
91         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
92         Date date = new Date();
93         if("EditPolicy".equalsIgnoreCase(mode)){
94             subject = "Policy has been Updated : "+entityItem.getPolicyName();
95             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Updated" + '\n'  + '\n'  + '\n'+ SCOPE_POLICY_NAME + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
96                      + '\n'  + '\n' + "Modified By : " +entityItem.getModifiedBy() + '\n' + "Modified Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
97         }
98         if("Rename".equalsIgnoreCase(mode)){
99             subject = "Policy has been Renamed : "+entityItem.getPolicyName();
100             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Renamed" + '\n'  + '\n'  + '\n'+ SCOPE_POLICY_NAME + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
101                      + '\n'  + '\n' + "Renamed By : " +entityItem.getModifiedBy() + '\n' + "Renamed Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
102         }
103         if("DeleteAll".equalsIgnoreCase(mode)){
104             subject = "Policy has been Deleted : "+entityItem.getPolicyName();
105             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Deleted with All Versions" + '\n'  + '\n'  + '\n'+ SCOPE_POLICY_NAME + policyName + '\n'
106                      + '\n'  + '\n' + DELETED_BY +entityItem.getModifiedBy() + '\n' + DELETED_TIME +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
107         }
108         if("DeleteOne".equalsIgnoreCase(mode)){
109             subject = "Policy has been Deleted : "+entityItem.getPolicyName();
110             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n'  + '\n'  + '\n'+ SCOPE_POLICY_NAME + policyName + '\n'  +"Policy Version : " +entityItem.getActiveVersion()
111                      + '\n'  + '\n' + DELETED_BY +entityItem.getModifiedBy() + '\n' + DELETED_TIME +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
112         }
113         if("DeleteScope".equalsIgnoreCase(mode)){
114             subject = "Scope has been Deleted : "+entityItem.getPolicyName();
115             message = "The Scope Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n'  + '\n'  + '\n'+ "Scope + Scope Name  : "  + policyName + '\n'
116                      + '\n'  + '\n' + DELETED_BY +entityItem.getModifiedBy() + '\n' + DELETED_TIME +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
117         }
118         if("SwitchVersion".equalsIgnoreCase(mode)){
119             subject = "Policy has been SwitchedVersion : "+entityItem.getPolicyName();
120             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been SwitchedVersion" + '\n'  + '\n'  + '\n'+ SCOPE_POLICY_NAME + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
121                      + '\n'  + '\n' + "Switched By : " +entityItem.getModifiedBy() + '\n' + "Switched Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
122         }
123         if("Move".equalsIgnoreCase(mode)){
124             subject = "Policy has been Moved to Other Scope : "+entityItem.getPolicyName();
125             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Moved to Other Scope" + '\n'  + '\n'  + '\n'+ SCOPE_POLICY_NAME + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
126                      + '\n'  + '\n' + "Moved By : " +entityItem.getModifiedBy() + '\n' + "Moved Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
127         }
128         String checkPolicyName = findCheckPolicyName(policyName);
129
130         String policyFileName = findPolicyFileName(entityItem);
131         String query = "from WatchPolicyNotificationTable where policyName like:policyFileName";
132         List<Object> watchList = findWatchList(policyNotificationDao, policyFileName, query);
133         if (!watchList.isEmpty()) {
134             composeAndSendMail(mode, policyNotificationDao, subject, message, checkPolicyName, watchList);
135         }
136     }
137
138     private List<Object> findWatchList(CommonClassDao policyNotificationDao, String policyFileName, String query) {
139         SimpleBindings params = new SimpleBindings();
140         params.put("policyFileName", policyFileName);
141         List<Object> watchList;
142         if (PolicyController.isjUnit()) {
143             watchList = policyNotificationDao.getDataByQuery(query, null);
144         } else {
145             watchList = policyNotificationDao.getDataByQuery(query, params);
146         }
147
148         if (watchList == null || watchList.isEmpty()) {
149             policyLogger
150                 .debug("List of policy being watched is either null or empty, hence return without sending mail");
151             watchList = new ArrayList<>();
152         }
153         return watchList;
154     }
155
156     private String findPolicyFileName(PolicyVersion entityItem) {
157         String policyFileName = entityItem.getPolicyName();
158         if(policyFileName.contains("/")){
159             policyFileName = policyFileName.substring(0, policyFileName.indexOf('/'));
160             policyFileName = policyFileName.replace("/", File.separator);
161         }
162         if(policyFileName.contains("\\")){
163             policyFileName = policyFileName.substring(0, policyFileName.indexOf('\\'));
164             policyFileName = policyFileName.replace("\\", "\\\\");
165         }
166
167         policyFileName += "%";
168         return policyFileName;
169     }
170
171     private String findCheckPolicyName(String policyName) {
172         String checkPolicyName = policyName;
173         if(checkPolicyName.endsWith(".xml") || checkPolicyName.contains(".")){
174             checkPolicyName = checkPolicyName.substring(0, checkPolicyName.indexOf('.'));
175         }
176         return checkPolicyName;
177     }
178
179     /**
180      * For every policy being watched and when the policy name is one of the Config_, Action_ or Decision_,
181      * send the notification
182      * @param mode
183      * @param policyNotificationDao
184      * @param subject
185      * @param message
186      * @param checkPolicyName
187      * @param watchList
188      */
189     private void composeAndSendMail(String mode, CommonClassDao policyNotificationDao, String subject, String message, String checkPolicyName, List<Object> watchList) {
190         String from = PolicyController.getSmtpUsername();
191         String to;
192         for(Object watch : watchList){
193             WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
194             String watchPolicyName = list.getPolicyName();
195             //this conditino check for specific stringin policy name being watched and
196             //also if the policy being checked is different from the watched ones,
197             //then there is no need to send mail, hence continue with next policy in the loop
198             if((watchPolicyName.contains("Config_") || watchPolicyName.contains("Action_") || watchPolicyName.contains("Decision_"))
199                     && !watchPolicyName.equals(checkPolicyName)){
200                 continue;
201             }
202             try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
203                 to = list.getLoginIds()+"@"+PolicyController.getSmtpEmailExtension();
204                 to = to.trim();
205                 ctx.register(PolicyNotificationMail.class);
206                 ctx.refresh();
207                 JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
208                 MimeMessage mimeMessage = mailSender.createMimeMessage();
209                 MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
210                 mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
211                 mailMsg.setTo(to);
212                 mailMsg.setSubject(subject);
213                 mailMsg.setText(message);
214                 mailSender.send(mimeMessage);
215                 if("Rename".equalsIgnoreCase(mode) || mode.contains("Delete") || mode.contains("Move")){
216                     policyNotificationDao.delete(watch);
217                 }
218             } catch (Exception e) {
219                 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);
220             }
221
222         }
223     }
224 }