Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / admin / PolicyNotificationMail.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP 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.openecomp.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
34 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
35 import org.openecomp.policy.common.logging.flexlogger.Logger;
36 import org.openecomp.policy.controller.PolicyController;
37 import org.openecomp.policy.rest.dao.CommonClassDao;
38 import org.openecomp.policy.rest.jpa.PolicyVersion;
39 import org.openecomp.policy.rest.jpa.WatchPolicyNotificationTable;
40 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
41 import org.springframework.beans.factory.annotation.Configurable;
42 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.mail.javamail.JavaMailSenderImpl;
45 import org.springframework.mail.javamail.MimeMessageHelper;
46
47 @Configurable
48 public class PolicyNotificationMail{
49         private static Logger LOGGER    = FlexLogger.getLogger(PolicyNotificationMail.class);
50         
51         @Bean
52         public JavaMailSenderImpl javaMailSenderImpl(){
53                 JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
54                 mailSender.setHost(PolicyController.smtpHost);
55                 mailSender.setPort(Integer.parseInt(PolicyController.smtpPort));
56                 mailSender.setUsername(PolicyController.smtpUsername);
57                 mailSender.setPassword(PolicyController.smtpPassword);
58                 Properties prop = mailSender.getJavaMailProperties();
59                 prop.put("mail.transport.protocol", "smtp");
60                 prop.put("mail.smtp.auth", "true");
61                 prop.put("mail.smtp.starttls.enable", "true");
62                 prop.put("mail.debug", "true");
63                 return mailSender;
64         }
65
66         public void sendMail(PolicyVersion entityItem, String policyName, String mode, CommonClassDao policyNotificationDao) throws MessagingException {  
67                 String from = PolicyController.smtpUsername;
68                 String to = "";
69                 String subject = "";
70                 String message = "";
71                 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
72                 Date date = new Date();
73                 if("EditPolicy".equalsIgnoreCase(mode)){
74                         subject = "Policy has been Updated : "+entityItem.getPolicyName();
75                         message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Updated" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
76                                          + '\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)";
77                 }
78                 if("Rename".equalsIgnoreCase(mode)){
79                         subject = "Policy has been Renamed : "+entityItem.getPolicyName();
80                         message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Renamed" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
81                                          + '\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)";
82                 }
83                 if("DeleteAll".equalsIgnoreCase(mode)){
84                         subject = "Policy has been Deleted : "+entityItem.getPolicyName();
85                         message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Deleted with All Versions" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n'  
86                                          + '\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)";
87                 }
88                 if("DeleteOne".equalsIgnoreCase(mode)){
89                         subject = "Policy has been Deleted : "+entityItem.getPolicyName();
90                         message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Deleted" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n'  +"Policy Version : " +entityItem.getActiveVersion()
91                                          + '\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)";
92                 }
93                 if("DeleteScope".equalsIgnoreCase(mode)){
94                         subject = "Scope has been Deleted : "+entityItem.getPolicyName();
95                         message = "The Scope Which you are watching in  " + PolicyController.smtpApplicationName + " has been Deleted" + '\n'  + '\n'  + '\n'+ "Scope + Scope Name  : "  + policyName + '\n'  
96                                          + '\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)";
97                 }
98                 if("SwitchVersion".equalsIgnoreCase(mode)){
99                         subject = "Policy has been SwitchedVersion : "+entityItem.getPolicyName();
100                         message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been SwitchedVersion" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
101                                          + '\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)";
102                 }
103                 if("Move".equalsIgnoreCase(mode)){
104                         subject = "Policy has been Moved to Other Scope : "+entityItem.getPolicyName();
105                         message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Moved to Other Scope" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n' + "Active Version  : " +entityItem.getActiveVersion() 
106                                          + '\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)";
107                 }
108                 String policyFileName = entityItem.getPolicyName();
109                 String checkPolicyName = policyFileName;
110                 if(policyFileName.contains("/")){
111                         policyFileName = policyFileName.substring(0, policyFileName.indexOf("/"));
112                         policyFileName = policyFileName.replace("/", File.separator);
113                 }
114                 if(policyFileName.contains("\\")){
115                         policyFileName = policyFileName.substring(0, policyFileName.indexOf("\\"));
116                         policyFileName = policyFileName.replace("\\", "\\\\");
117                 }
118                 
119                 String query = "from WatchPolicyNotificationTable where policyName like'" +policyFileName+"%'";
120                 boolean sendFlag = false;
121                 List<Object> watchList = policyNotificationDao.getDataByQuery(query);
122                 if(watchList != null){
123                         if(watchList.size() > 0){
124                                 for(Object watch : watchList){
125                                         WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
126                                         String watchPolicyName = list.getPolicyName();
127                                         if(watchPolicyName.contains("Config_")){
128                                                 if(watchPolicyName.equals(checkPolicyName)){
129                                                         sendFlag = true;
130                                                 }
131                                         }else if(watchPolicyName.contains("Action_")){
132                                                 if(watchPolicyName.equals(checkPolicyName)){
133                                                         sendFlag = true;
134                                                 }
135                                         }else if(watchPolicyName.contains("Decision_")){
136                                                 if(watchPolicyName.equals(checkPolicyName)){
137                                                         sendFlag = true;
138                                                 }
139                                         }else{
140                                                 sendFlag = true;
141                                         }
142                                         if(sendFlag){
143                                                 AnnotationConfigApplicationContext ctx = null;
144                                                 try {
145                                                         to = list.getLoginIds()+"@"+PolicyController.smtpEmailExtension;
146                                                         to = to.trim();
147                                                         ctx = new AnnotationConfigApplicationContext();
148                                                         ctx.register(PolicyNotificationMail.class);
149                                                         ctx.refresh();
150                                                         JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
151                                                         MimeMessage mimeMessage = mailSender.createMimeMessage();
152                                                         MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
153                                                         mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
154                                                         mailMsg.setTo(to);
155                                                         mailMsg.setSubject(subject);
156                                                         mailMsg.setText(message);
157                                                         mailSender.send(mimeMessage);
158                                                 } catch (Exception e) {
159                                                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);
160                                                 }finally{
161                                                         ctx.close();
162                                                 }
163                                         }
164                                 }
165                         }
166                 }
167         }
168 }