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.openecomp.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.openecomp.policy.common.logging.flexlogger.FlexLogger;
35 import org.openecomp.policy.common.logging.flexlogger.Logger;
36 import org.openecomp.policy.controller.PolicyController;
37 import org.openecomp.policy.rest.dao.CommonClassDao;
38 import org.openecomp.policy.rest.jpa.PolicyVersion;
39 import org.openecomp.policy.rest.jpa.WatchPolicyNotificationTable;
40 import org.openecomp.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 LOGGER = FlexLogger.getLogger(PolicyNotificationMail.class);
52 public JavaMailSenderImpl javaMailSenderImpl(){
53 JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
54 mailSender.setHost(PolicyController.smtpHost);
55 mailSender.setPort(Integer.parseInt(PolicyController.smtpPort));
56 mailSender.setUsername(PolicyController.smtpUsername);
57 mailSender.setPassword(PolicyController.smtpPassword);
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 @SuppressWarnings("resource")
67 public void sendMail(PolicyVersion entityItem, String policyName, String mode, CommonClassDao policyNotificationDao) throws MessagingException {
68 String from = PolicyController.smtpUsername;
72 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
73 Date date = new Date();
74 if(mode.equalsIgnoreCase("EditPolicy")){
75 subject = "Policy has been Updated : "+entityItem.getPolicyName();
76 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " 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(mode.equalsIgnoreCase("Rename")){
80 subject = "Policy has been Renamed : "+entityItem.getPolicyName();
81 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " 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(mode.equalsIgnoreCase("DeleteAll")){
85 subject = "Policy has been Deleted : "+entityItem.getPolicyName();
86 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " 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(mode.equalsIgnoreCase("DeleteOne")){
90 subject = "Policy has been Deleted : "+entityItem.getPolicyName();
91 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " 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(mode.equalsIgnoreCase("DeleteScope")){
95 subject = "Scope has been Deleted : "+entityItem.getPolicyName();
96 message = "The Scope Which you are watching in " + PolicyController.smtpApplicationName + " 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(mode.equalsIgnoreCase("SwitchVersion")){
100 subject = "Policy has been SwitchedVersion : "+entityItem.getPolicyName();
101 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " 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(mode.equalsIgnoreCase("Move")){
105 subject = "Policy has been Moved to Other Scope : "+entityItem.getPolicyName();
106 message = "The Policy Which you are watching in " + PolicyController.smtpApplicationName + " 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 = policyFileName;
111 if(policyFileName.contains("/")){
112 policyFileName = policyFileName.substring(0, policyFileName.indexOf("/"));
113 policyFileName = policyFileName.replace("/", File.separator);
115 if(policyFileName.contains("\\")){
116 policyFileName = policyFileName.substring(0, policyFileName.indexOf("\\"));
117 policyFileName = policyFileName.replace("\\", "\\\\");
120 String query = "from WatchPolicyNotificationTable where policyName like'" +policyFileName+"%'";
121 boolean sendFlag = false;
122 List<Object> watchList = policyNotificationDao.getDataByQuery(query);
123 if(watchList != null){
124 if(watchList.size() > 0){
125 for(Object watch : watchList){
126 WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
127 String watchPolicyName = list.getPolicyName();
128 if(watchPolicyName.contains("Config_")){
129 if(watchPolicyName.equals(checkPolicyName)){
132 }else if(watchPolicyName.contains("Action_")){
133 if(watchPolicyName.equals(checkPolicyName)){
136 }else if(watchPolicyName.contains("Decision_")){
137 if(watchPolicyName.equals(checkPolicyName)){
144 to = list.getLoginIds()+"@"+PolicyController.smtpEmailExtension;
146 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
147 ctx.register(PolicyNotificationMail.class);
149 JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
150 MimeMessage mimeMessage = mailSender.createMimeMessage();
151 MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
153 mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
154 } catch (Exception e) {
155 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);
158 mailMsg.setSubject(subject);
159 mailMsg.setText(message);
160 mailSender.send(mimeMessage);