[Policy-17] Removed the sql scripts from sdk app
[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 policyLogger      = FlexLogger.getLogger(PolicyNotificationMail.class);
50         
51         @Bean
52         public JavaMailSenderImpl javaMailSenderImpl(){
53                 JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
54                 mailSender.setHost(PolicyController.getSmtpHost());
55                 mailSender.setPort(Integer.parseInt(PolicyController.getSmtpPort()));
56                 mailSender.setUsername(PolicyController.getSmtpUsername());
57                 mailSender.setPassword(PolicyController.getSmtpPassword());
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.getSmtpUsername();
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.getSmtpApplicationName() + " 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.getSmtpApplicationName() + " 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.getSmtpApplicationName() + " 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.getSmtpApplicationName() + " 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.getSmtpApplicationName() + " 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.getSmtpApplicationName() + " 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.getSmtpApplicationName() + " 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.isEmpty()){
124                                 for(Object watch : watchList){
125                                         WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
126                                         String watchPolicyName = list.getPolicyName();
127                                         if(watchPolicyName.contains("Config_") || watchPolicyName.contains("Action_") || watchPolicyName.contains("Decision_")){
128                                                 if(watchPolicyName.equals(checkPolicyName)){
129                                                         sendFlag = true;
130                                                 }else{
131                                                         sendFlag = false;
132                                                 }
133                                         }
134                                         if(sendFlag){
135                                                 AnnotationConfigApplicationContext ctx = null;
136                                                 try {
137                                                         to = list.getLoginIds()+"@"+PolicyController.getSmtpApplicationName();
138                                                         to = to.trim();
139                                                         ctx = new AnnotationConfigApplicationContext();
140                                                         ctx.register(PolicyNotificationMail.class);
141                                                         ctx.refresh();
142                                                         JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
143                                                         MimeMessage mimeMessage = mailSender.createMimeMessage();
144                                                         MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
145                                                         mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
146                                                         mailMsg.setTo(to);
147                                                         mailMsg.setSubject(subject);
148                                                         mailMsg.setText(message);
149                                                         mailSender.send(mimeMessage);
150                                                 } catch (Exception e) {
151                                                         policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);
152                                                 }finally{
153                                                         if(ctx != null){
154                                                                 ctx.close();
155                                                         }
156                                                 }
157                                         }
158                                 }
159                         }
160                 }
161         }
162 }