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;
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
36 import org.onap.policy.controller.PolicyController;
37 import org.onap.policy.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.PolicyVersion;
39 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
40 import org.onap.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;
48 public class PolicyNotificationMail{
49 private static Logger policyLogger = FlexLogger.getLogger(PolicyNotificationMail.class);
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");
66 public void sendMail(PolicyVersion entityItem, String policyName, String mode, CommonClassDao policyNotificationDao) throws MessagingException {
67 String from = PolicyController.getSmtpUsername();
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)";
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)";
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)";
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)";
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)";
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)";
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)";
108 String policyFileName = entityItem.getPolicyName();
109 String checkPolicyName = policyName;
110 if(policyFileName.contains("/")){
111 policyFileName = policyFileName.substring(0, policyFileName.indexOf("/"));
112 policyFileName = policyFileName.replace("/", File.separator);
114 if(policyFileName.contains("\\")){
115 policyFileName = policyFileName.substring(0, policyFileName.indexOf("\\"));
116 policyFileName = policyFileName.replace("\\", "\\\\");
119 String query = "from WatchPolicyNotificationTable where policyName like'" +policyFileName+"%'";
120 boolean sendFlag = false;
121 List<Object> watchList = policyNotificationDao.getDataByQuery(query);
122 if(watchList != null && !watchList.isEmpty()){
123 for(Object watch : watchList){
124 WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
125 String watchPolicyName = list.getPolicyName();
126 if(watchPolicyName.contains("Config_") || watchPolicyName.contains("Action_") || watchPolicyName.contains("Decision_")){
127 if(watchPolicyName.equals(checkPolicyName)){
134 AnnotationConfigApplicationContext ctx = null;
136 to = list.getLoginIds()+"@"+PolicyController.getSmtpEmailExtension();
138 ctx = new AnnotationConfigApplicationContext();
139 ctx.register(PolicyNotificationMail.class);
141 JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
142 MimeMessage mimeMessage = mailSender.createMimeMessage();
143 MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
144 mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
146 mailMsg.setSubject(subject);
147 mailMsg.setText(message);
148 mailSender.send(mimeMessage);
149 if(mode.equalsIgnoreCase("Rename") || mode.contains("Delete") || mode.contains("Move")){
150 policyNotificationDao.delete(watch);
152 } catch (Exception e) {
153 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);