6424465de3144352672e6cd96998b3e11d714f64
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.admin;
22
23 import java.io.File;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.Properties;
29
30 import javax.mail.MessagingException;
31 import javax.mail.internet.InternetAddress;
32 import javax.mail.internet.MimeMessage;
33 import javax.script.SimpleBindings;
34
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.controller.PolicyController;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.PolicyVersion;
40 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
41 import org.onap.policy.xacml.api.XACMLErrorConstants;
42 import org.springframework.beans.factory.annotation.Configurable;
43 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
44 import org.springframework.context.annotation.Bean;
45 import org.springframework.mail.javamail.JavaMailSenderImpl;
46 import org.springframework.mail.javamail.MimeMessageHelper;
47
48 @Configurable
49 public class PolicyNotificationMail{
50         private static Logger policyLogger      = FlexLogger.getLogger(PolicyNotificationMail.class);
51         
52         @Bean
53         public JavaMailSenderImpl javaMailSenderImpl(){
54                 JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
55                 mailSender.setHost(PolicyController.getSmtpHost());
56                 mailSender.setPort(Integer.parseInt(PolicyController.getSmtpPort()));
57                 mailSender.setUsername(PolicyController.getSmtpUsername());
58                 mailSender.setPassword(PolicyController.getSmtpPassword());
59                 Properties prop = mailSender.getJavaMailProperties();
60                 prop.put("mail.transport.protocol", "smtp");
61                 prop.put("mail.smtp.auth", "true");
62                 prop.put("mail.smtp.starttls.enable", "true");
63                 prop.put("mail.debug", "true");
64                 return mailSender;
65         }
66
67         public void sendMail(PolicyVersion entityItem, String policyName, String mode, CommonClassDao policyNotificationDao) throws MessagingException {  
68                 String from = PolicyController.getSmtpUsername();
69                 String to;
70                 String subject = "";
71                 String message = "";
72                 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
73                 Date date = new Date();
74                 if("EditPolicy".equalsIgnoreCase(mode)){
75                         subject = "Policy has been Updated : "+entityItem.getPolicyName();
76                         message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Updated" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
77                                          + '\n'  + '\n' + "Modified By : " +entityItem.getModifiedBy() + '\n' + "Modified Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
78                 }
79                 if("Rename".equalsIgnoreCase(mode)){
80                         subject = "Policy has been Renamed : "+entityItem.getPolicyName();
81                         message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Renamed" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
82                                          + '\n'  + '\n' + "Renamed By : " +entityItem.getModifiedBy() + '\n' + "Renamed Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
83                 }
84                 if("DeleteAll".equalsIgnoreCase(mode)){
85                         subject = "Policy has been Deleted : "+entityItem.getPolicyName();
86                         message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Deleted with All Versions" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n'  
87                                          + '\n'  + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
88                 }
89                 if("DeleteOne".equalsIgnoreCase(mode)){
90                         subject = "Policy has been Deleted : "+entityItem.getPolicyName();
91                         message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n'  +"Policy Version : " +entityItem.getActiveVersion()
92                                          + '\n'  + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
93                 }
94                 if("DeleteScope".equalsIgnoreCase(mode)){
95                         subject = "Scope has been Deleted : "+entityItem.getPolicyName();
96                         message = "The Scope Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n'  + '\n'  + '\n'+ "Scope + Scope Name  : "  + policyName + '\n'  
97                                          + '\n'  + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
98                 }
99                 if("SwitchVersion".equalsIgnoreCase(mode)){
100                         subject = "Policy has been SwitchedVersion : "+entityItem.getPolicyName();
101                         message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been SwitchedVersion" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
102                                          + '\n'  + '\n' + "Switched By : " +entityItem.getModifiedBy() + '\n' + "Switched Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
103                 }
104                 if("Move".equalsIgnoreCase(mode)){
105                         subject = "Policy has been Moved to Other Scope : "+entityItem.getPolicyName();
106                         message = "The Policy Which you are watching in  " + PolicyController.getSmtpApplicationName() + " has been Moved to Other Scope" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
107                                          + '\n'  + '\n' + "Moved By : " +entityItem.getModifiedBy() + '\n' + "Moved Time  : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + "Policy Notification System  (please don't respond to this email)";
108                 }
109                 String policyFileName = entityItem.getPolicyName();
110                 String checkPolicyName = policyName;
111                 if(checkPolicyName.endsWith(".xml") || checkPolicyName.contains(".")){
112                         checkPolicyName = checkPolicyName.substring(0, checkPolicyName.indexOf('.'));
113                 }
114                 if(policyFileName.contains("/")){
115                         policyFileName = policyFileName.substring(0, policyFileName.indexOf('/'));
116                         policyFileName = policyFileName.replace("/", File.separator);
117                 }
118                 if(policyFileName.contains("\\")){
119                         policyFileName = policyFileName.substring(0, policyFileName.indexOf('\\'));
120                         policyFileName = policyFileName.replace("\\", "\\\\");
121                 }
122                 
123                 policyFileName += "%";
124                 String query = "from WatchPolicyNotificationTable where policyName like:policyFileName";
125                 boolean sendFlag = false;
126                 SimpleBindings params = new SimpleBindings();
127                 params.put("policyFileName", policyFileName);
128                 List<Object> watchList;
129                 if(PolicyController.isjUnit()){
130                         watchList = policyNotificationDao.getDataByQuery(query, null);
131                 }else{
132                         watchList = policyNotificationDao.getDataByQuery(query, params);
133                 }
134                 if(watchList != null && !watchList.isEmpty()){
135                         for(Object watch : watchList){
136                                 WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
137                                 String watchPolicyName = list.getPolicyName();
138                                 if(watchPolicyName.contains("Config_") || watchPolicyName.contains("Action_") || watchPolicyName.contains("Decision_")){
139                                         if(watchPolicyName.equals(checkPolicyName)){
140                                                 sendFlag = true;
141                                         }else{
142                                                 sendFlag = false;
143                                         }
144                                 }
145                                 if(sendFlag){
146                                         try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
147                                                 to = list.getLoginIds()+"@"+PolicyController.getSmtpEmailExtension();
148                                                 to = to.trim();
149                                                 ctx.register(PolicyNotificationMail.class);
150                                                 ctx.refresh();
151                                                 JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
152                                                 MimeMessage mimeMessage = mailSender.createMimeMessage();
153                                                 MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
154                                                 mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
155                                                 mailMsg.setTo(to);
156                                                 mailMsg.setSubject(subject);
157                                                 mailMsg.setText(message);
158                                                 mailSender.send(mimeMessage);
159                                                 if("Rename".equalsIgnoreCase(mode) || mode.contains("Delete") || mode.contains("Move")){
160                                                         policyNotificationDao.delete(watch);
161                                                 }
162                                         } catch (Exception e) {
163                                                 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);
164                                         }
165                                 }
166                         }
167                 }
168         }
169 }