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 public class PolicyNotificationMail{
50 private static Logger policyLogger = FlexLogger.getLogger(PolicyNotificationMail.class);
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");
67 public void sendMail(PolicyVersion entityItem, String policyName, String mode, CommonClassDao policyNotificationDao) throws MessagingException {
68 String from = PolicyController.getSmtpUsername();
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)";
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)";
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)";
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)";
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)";
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)";
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)";
109 String policyFileName = entityItem.getPolicyName();
110 String checkPolicyName = policyName;
111 if(checkPolicyName.endsWith(".xml") || checkPolicyName.contains(".")){
112 checkPolicyName = checkPolicyName.substring(0, checkPolicyName.indexOf('.'));
114 if(policyFileName.contains("/")){
115 policyFileName = policyFileName.substring(0, policyFileName.indexOf('/'));
116 policyFileName = policyFileName.replace("/", File.separator);
118 if(policyFileName.contains("\\")){
119 policyFileName = policyFileName.substring(0, policyFileName.indexOf('\\'));
120 policyFileName = policyFileName.replace("\\", "\\\\");
123 policyFileName += "%";
124 String query = "from WatchPolicyNotificationTable where policyName like:policyFileName";
125 boolean sendFlag = false;
126 SimpleBindings params = new SimpleBindings();
127 params.put("policyFileName", policyFileName);
128 List<Object> watchList;
129 if(PolicyController.isjUnit()){
130 watchList = policyNotificationDao.getDataByQuery(query, null);
132 watchList = policyNotificationDao.getDataByQuery(query, params);
134 if(watchList != null && !watchList.isEmpty()){
135 for(Object watch : watchList){
136 WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
137 String watchPolicyName = list.getPolicyName();
138 if(watchPolicyName.contains("Config_") || watchPolicyName.contains("Action_") || watchPolicyName.contains("Decision_")){
139 if(watchPolicyName.equals(checkPolicyName)){
146 try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
147 to = list.getLoginIds()+"@"+PolicyController.getSmtpEmailExtension();
149 ctx.register(PolicyNotificationMail.class);
151 JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
152 MimeMessage mimeMessage = mailSender.createMimeMessage();
153 MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
154 mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
156 mailMsg.setSubject(subject);
157 mailMsg.setText(message);
158 mailSender.send(mimeMessage);
159 if("Rename".equalsIgnoreCase(mode) || mode.contains("Delete") || mode.contains("Move")){
160 policyNotificationDao.delete(watch);
162 } catch (Exception e) {
163 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);