Added Junits for POLICY-SDK-APP
[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(policyFileName.contains("/")){
112                         policyFileName = policyFileName.substring(0, policyFileName.indexOf("/"));
113                         policyFileName = policyFileName.replace("/", File.separator);
114                 }
115                 if(policyFileName.contains("\\")){
116                         policyFileName = policyFileName.substring(0, policyFileName.indexOf("\\"));
117                         policyFileName = policyFileName.replace("\\", "\\\\");
118                 }
119                 
120                 policyFileName += "%";
121                 String query = "from WatchPolicyNotificationTable where policyName like:policyFileName";
122                 boolean sendFlag = false;
123                 SimpleBindings params = new SimpleBindings();
124                 params.put("policyFileName", policyFileName);
125                 List<Object> watchList = null;
126                 if(PolicyController.isjUnit()){
127                         watchList = policyNotificationDao.getDataByQuery(query, null);
128                 }else{
129                         watchList = policyNotificationDao.getDataByQuery(query, params);
130                 }
131                 if(watchList != null && !watchList.isEmpty()){
132                         for(Object watch : watchList){
133                                 WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
134                                 String watchPolicyName = list.getPolicyName();
135                                 if(watchPolicyName.contains("Config_") || watchPolicyName.contains("Action_") || watchPolicyName.contains("Decision_")){
136                                         if(watchPolicyName.equals(checkPolicyName)){
137                                                 sendFlag = true;
138                                         }else{
139                                                 sendFlag = false;
140                                         }
141                                 }
142                                 if(sendFlag){
143                                         AnnotationConfigApplicationContext ctx = null;
144                                         try {
145                                                 to = list.getLoginIds()+"@"+PolicyController.getSmtpEmailExtension();
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                                                 if(mode.equalsIgnoreCase("Rename") || mode.contains("Delete") || mode.contains("Move")){
159                                                         policyNotificationDao.delete(watch);
160                                                 }
161                                         } catch (Exception e) {
162                                                 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);
163                                         }finally{
164                                                 if(ctx != null){
165                                                         ctx.close();
166                                                 }
167                                         }
168                                 }
169                         }
170                 }
171         }
172 }