2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.admin;
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;
30 import javax.mail.MessagingException;
31 import javax.mail.internet.InternetAddress;
32 import javax.mail.internet.MimeMessage;
33 import javax.script.SimpleBindings;
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;
49 * Send policy notification mail depending on the mode for every policy being watched
52 public class PolicyNotificationMail{
53 private static final String POLICY_WATCHING_MESSAGE = "The Policy Which you are watching in ";
54 private static final String EMAIL_MESSAGE_POSTSCRIPT = "Policy Notification System (please don't respond to this email)";
55 private static final String ACTIVE_VERSION = "Active Version : ";
56 private static Logger policyLogger = FlexLogger.getLogger(PolicyNotificationMail.class);
59 public JavaMailSenderImpl javaMailSenderImpl(){
60 JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
61 mailSender.setHost(PolicyController.getSmtpHost());
62 mailSender.setPort(Integer.parseInt(PolicyController.getSmtpPort()));
63 mailSender.setUsername(PolicyController.getSmtpUsername());
64 mailSender.setPassword(PolicyController.getSmtpPassword());
65 Properties prop = mailSender.getJavaMailProperties();
66 prop.put("mail.transport.protocol", "smtp");
67 prop.put("mail.smtp.auth", "true");
68 prop.put("mail.smtp.starttls.enable", "true");
69 prop.put("mail.debug", "true");
74 * Depending on the mode of operation on the policy, compose the subject and message.
75 * Invoke another internal method to actual send the mail. If the watch list is empty , then
76 * this method returns without sending notification mail
77 * @param entityItem Database item from which policy name could be extracted
78 * @param policyName Name of the policy for which notification is to be sent
79 * @param mode kind of operation done on the policy
80 * @param policyNotificationDao database access object for policy
81 * @throws MessagingException
83 public void sendMail(PolicyVersion entityItem, String policyName, String mode, CommonClassDao policyNotificationDao) throws MessagingException {
87 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
88 Date date = new Date();
89 if("EditPolicy".equalsIgnoreCase(mode)){
90 subject = "Policy has been Updated : "+entityItem.getPolicyName();
91 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Updated" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
92 + '\n' + '\n' + "Modified By : " +entityItem.getModifiedBy() + '\n' + "Modified Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
94 if("Rename".equalsIgnoreCase(mode)){
95 subject = "Policy has been Renamed : "+entityItem.getPolicyName();
96 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Renamed" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
97 + '\n' + '\n' + "Renamed By : " +entityItem.getModifiedBy() + '\n' + "Renamed Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
99 if("DeleteAll".equalsIgnoreCase(mode)){
100 subject = "Policy has been Deleted : "+entityItem.getPolicyName();
101 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Deleted with All Versions" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n'
102 + '\n' + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
104 if("DeleteOne".equalsIgnoreCase(mode)){
105 subject = "Policy has been Deleted : "+entityItem.getPolicyName();
106 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' +"Policy Version : " +entityItem.getActiveVersion()
107 + '\n' + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
109 if("DeleteScope".equalsIgnoreCase(mode)){
110 subject = "Scope has been Deleted : "+entityItem.getPolicyName();
111 message = "The Scope Which you are watching in " + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n' + '\n' + '\n'+ "Scope + Scope Name : " + policyName + '\n'
112 + '\n' + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
114 if("SwitchVersion".equalsIgnoreCase(mode)){
115 subject = "Policy has been SwitchedVersion : "+entityItem.getPolicyName();
116 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been SwitchedVersion" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
117 + '\n' + '\n' + "Switched By : " +entityItem.getModifiedBy() + '\n' + "Switched Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
119 if("Move".equalsIgnoreCase(mode)){
120 subject = "Policy has been Moved to Other Scope : "+entityItem.getPolicyName();
121 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Moved to Other Scope" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
122 + '\n' + '\n' + "Moved By : " +entityItem.getModifiedBy() + '\n' + "Moved Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
124 String policyFileName = entityItem.getPolicyName();
125 String checkPolicyName = policyName;
126 if(checkPolicyName.endsWith(".xml") || checkPolicyName.contains(".")){
127 checkPolicyName = checkPolicyName.substring(0, checkPolicyName.indexOf('.'));
129 if(policyFileName.contains("/")){
130 policyFileName = policyFileName.substring(0, policyFileName.indexOf('/'));
131 policyFileName = policyFileName.replace("/", File.separator);
133 if(policyFileName.contains("\\")){
134 policyFileName = policyFileName.substring(0, policyFileName.indexOf('\\'));
135 policyFileName = policyFileName.replace("\\", "\\\\");
138 policyFileName += "%";
139 String query = "from WatchPolicyNotificationTable where policyName like:policyFileName";
141 SimpleBindings params = new SimpleBindings();
142 params.put("policyFileName", policyFileName);
143 List<Object> watchList;
144 if(PolicyController.isjUnit()){
145 watchList = policyNotificationDao.getDataByQuery(query, null);
147 watchList = policyNotificationDao.getDataByQuery(query, params);
150 if(watchList == null || watchList.isEmpty()) {
151 policyLogger.debug("List of policy being watched is either null or empty, hence return without sending mail");
155 composeAndSendMail(mode, policyNotificationDao, subject, message, checkPolicyName, watchList);
159 * For every policy being watched and when the policy name is one of the Config_, Action_ or Decision_,
160 * send the notification
162 * @param policyNotificationDao
165 * @param checkPolicyName
168 private void composeAndSendMail(String mode, CommonClassDao policyNotificationDao, String subject, String message, String checkPolicyName, List<Object> watchList) {
169 String from = PolicyController.getSmtpUsername();
171 for(Object watch : watchList){
172 WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
173 String watchPolicyName = list.getPolicyName();
174 //this conditino check for specific stringin policy name being watched and
175 //also if the policy being checked is different from the watched ones,
176 //then there is no need to send mail, hence continue with next policy in the loop
177 if((watchPolicyName.contains("Config_") || watchPolicyName.contains("Action_") || watchPolicyName.contains("Decision_"))
178 && !watchPolicyName.equals(checkPolicyName)){
181 try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
182 to = list.getLoginIds()+"@"+PolicyController.getSmtpEmailExtension();
184 ctx.register(PolicyNotificationMail.class);
186 JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
187 MimeMessage mimeMessage = mailSender.createMimeMessage();
188 MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
189 mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
191 mailMsg.setSubject(subject);
192 mailMsg.setText(message);
193 mailSender.send(mimeMessage);
194 if("Rename".equalsIgnoreCase(mode) || mode.contains("Delete") || mode.contains("Move")){
195 policyNotificationDao.delete(watch);
197 } catch (Exception e) {
198 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);