2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.admin;
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.Properties;
31 import javax.mail.MessagingException;
32 import javax.mail.internet.InternetAddress;
33 import javax.mail.internet.MimeMessage;
34 import javax.script.SimpleBindings;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.controller.PolicyController;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.PolicyVersion;
41 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
42 import org.onap.policy.xacml.api.XACMLErrorConstants;
43 import org.springframework.beans.factory.annotation.Configurable;
44 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.mail.javamail.JavaMailSenderImpl;
47 import org.springframework.mail.javamail.MimeMessageHelper;
50 * Send policy notification mail depending on the mode for every policy being watched
53 public class PolicyNotificationMail{
54 private static final String POLICY_WATCHING_MESSAGE = "The Policy Which you are watching in ";
55 private static final String EMAIL_MESSAGE_POSTSCRIPT = "Policy Notification System (please don't respond to this email)";
56 private static final String ACTIVE_VERSION = "Active Version : ";
57 private static Logger policyLogger = FlexLogger.getLogger(PolicyNotificationMail.class);
60 public JavaMailSenderImpl javaMailSenderImpl(){
61 JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
62 mailSender.setHost(PolicyController.getSmtpHost());
63 mailSender.setPort(Integer.parseInt(PolicyController.getSmtpPort()));
64 mailSender.setUsername(PolicyController.getSmtpUsername());
65 mailSender.setPassword(PolicyController.getSmtpPassword());
66 Properties prop = mailSender.getJavaMailProperties();
67 prop.put("mail.transport.protocol", "smtp");
68 prop.put("mail.smtp.auth", "true");
69 prop.put("mail.smtp.starttls.enable", "true");
70 prop.put("mail.debug", "true");
75 * Depending on the mode of operation on the policy, compose the subject and message.
76 * Invoke another internal method to actual send the mail. If the watch list is empty , then
77 * this method returns without sending notification mail
78 * @param entityItem Database item from which policy name could be extracted
79 * @param policyName Name of the policy for which notification is to be sent
80 * @param mode kind of operation done on the policy
81 * @param policyNotificationDao database access object for policy
82 * @throws MessagingException
84 public void sendMail(PolicyVersion entityItem, String policyName, String mode, CommonClassDao policyNotificationDao) throws MessagingException {
88 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
89 Date date = new Date();
90 if("EditPolicy".equalsIgnoreCase(mode)){
91 subject = "Policy has been Updated : "+entityItem.getPolicyName();
92 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Updated" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
93 + '\n' + '\n' + "Modified By : " +entityItem.getModifiedBy() + '\n' + "Modified Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
95 if("Rename".equalsIgnoreCase(mode)){
96 subject = "Policy has been Renamed : "+entityItem.getPolicyName();
97 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Renamed" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
98 + '\n' + '\n' + "Renamed By : " +entityItem.getModifiedBy() + '\n' + "Renamed Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
100 if("DeleteAll".equalsIgnoreCase(mode)){
101 subject = "Policy has been Deleted : "+entityItem.getPolicyName();
102 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Deleted with All Versions" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n'
103 + '\n' + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
105 if("DeleteOne".equalsIgnoreCase(mode)){
106 subject = "Policy has been Deleted : "+entityItem.getPolicyName();
107 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' +"Policy Version : " +entityItem.getActiveVersion()
108 + '\n' + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
110 if("DeleteScope".equalsIgnoreCase(mode)){
111 subject = "Scope has been Deleted : "+entityItem.getPolicyName();
112 message = "The Scope Which you are watching in " + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n' + '\n' + '\n'+ "Scope + Scope Name : " + policyName + '\n'
113 + '\n' + '\n' + "Deleted By : " +entityItem.getModifiedBy() + '\n' + "Deleted Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
115 if("SwitchVersion".equalsIgnoreCase(mode)){
116 subject = "Policy has been SwitchedVersion : "+entityItem.getPolicyName();
117 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been SwitchedVersion" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
118 + '\n' + '\n' + "Switched By : " +entityItem.getModifiedBy() + '\n' + "Switched Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
120 if("Move".equalsIgnoreCase(mode)){
121 subject = "Policy has been Moved to Other Scope : "+entityItem.getPolicyName();
122 message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Moved to Other Scope" + '\n' + '\n' + '\n'+ "Scope + Policy Name : " + policyName + '\n' + ACTIVE_VERSION +entityItem.getActiveVersion()
123 + '\n' + '\n' + "Moved By : " +entityItem.getModifiedBy() + '\n' + "Moved Time : " +dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
125 String policyFileName = entityItem.getPolicyName();
126 String checkPolicyName = policyName;
127 if(checkPolicyName.endsWith(".xml") || checkPolicyName.contains(".")){
128 checkPolicyName = checkPolicyName.substring(0, checkPolicyName.indexOf('.'));
130 if(policyFileName.contains("/")){
131 policyFileName = policyFileName.substring(0, policyFileName.indexOf('/'));
132 policyFileName = policyFileName.replace("/", File.separator);
134 if(policyFileName.contains("\\")){
135 policyFileName = policyFileName.substring(0, policyFileName.indexOf('\\'));
136 policyFileName = policyFileName.replace("\\", "\\\\");
139 policyFileName += "%";
140 String query = "from WatchPolicyNotificationTable where policyName like:policyFileName";
142 SimpleBindings params = new SimpleBindings();
143 params.put("policyFileName", policyFileName);
144 List<Object> watchList;
145 if(PolicyController.isjUnit()){
146 watchList = policyNotificationDao.getDataByQuery(query, null);
148 watchList = policyNotificationDao.getDataByQuery(query, params);
151 if(watchList == null || watchList.isEmpty()) {
152 policyLogger.debug("List of policy being watched is either null or empty, hence return without sending mail");
156 composeAndSendMail(mode, policyNotificationDao, subject, message, checkPolicyName, watchList);
160 * For every policy being watched and when the policy name is one of the Config_, Action_ or Decision_,
161 * send the notification
163 * @param policyNotificationDao
166 * @param checkPolicyName
169 private void composeAndSendMail(String mode, CommonClassDao policyNotificationDao, String subject, String message, String checkPolicyName, List<Object> watchList) {
170 String from = PolicyController.getSmtpUsername();
172 for(Object watch : watchList){
173 WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
174 String watchPolicyName = list.getPolicyName();
175 //this conditino check for specific stringin policy name being watched and
176 //also if the policy being checked is different from the watched ones,
177 //then there is no need to send mail, hence continue with next policy in the loop
178 if((watchPolicyName.contains("Config_") || watchPolicyName.contains("Action_") || watchPolicyName.contains("Decision_"))
179 && !watchPolicyName.equals(checkPolicyName)){
182 try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
183 to = list.getLoginIds()+"@"+PolicyController.getSmtpEmailExtension();
185 ctx.register(PolicyNotificationMail.class);
187 JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
188 MimeMessage mimeMessage = mailSender.createMimeMessage();
189 MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
190 mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
192 mailMsg.setSubject(subject);
193 mailMsg.setText(message);
194 mailSender.send(mimeMessage);
195 if("Rename".equalsIgnoreCase(mode) || mode.contains("Delete") || mode.contains("Move")){
196 policyNotificationDao.delete(watch);
198 } catch (Exception e) {
199 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);