aboutsummaryrefslogtreecommitdiffstats
path: root/ecomp-sdk-app/src/main/java/org/openecomp/policy/admin/PolicyNotificationMail.java
blob: 186adfa291eecd4e308f52cc4a30402b016a5a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*-
 * ============LICENSE_START=======================================================
 * ECOMP Policy Engine
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.policy.admin;

/*
 * 
 * 
 * 
 * */
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.openecomp.policy.controller.PolicyController;
import org.openecomp.policy.dao.WatchPolicyNotificationDao;
import org.openecomp.policy.rest.jpa.PolicyVersion;
import org.openecomp.policy.rest.jpa.WatchPolicyNotificationTable;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import org.openecomp.policy.xacml.api.XACMLErrorConstants;

import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
import org.openecomp.policy.common.logging.flexlogger.Logger;

@Configurable
public class PolicyNotificationMail{
	private static Logger logger	= FlexLogger.getLogger(PolicyNotificationMail.class);
	
	@Bean
	public JavaMailSenderImpl javaMailSenderImpl(){
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		mailSender.setHost(PolicyController.smtpHost);
		mailSender.setPort(Integer.parseInt(PolicyController.smtpPort));
		mailSender.setUsername(PolicyController.smtpUsername);
		mailSender.setPassword(PolicyController.smtpPassword);
		Properties prop = mailSender.getJavaMailProperties();
		prop.put("mail.transport.protocol", "smtp");
		prop.put("mail.smtp.auth", "true");
		prop.put("mail.smtp.starttls.enable", "true");
		prop.put("mail.debug", "true");
		return mailSender;
	}

	@SuppressWarnings("resource")
	public void sendMail(PolicyVersion entityItem, String policyName, String mode, WatchPolicyNotificationDao policyNotificationDao) throws MessagingException {  
		String from = PolicyController.smtpUsername;
		String to = "";
		String subject = "";
		String message = "";
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		if(mode.equalsIgnoreCase("EditPolicy")){
			subject = "Policy has been Updated : "+entityItem.getPolicyName();
			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() 
					 + '\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)";
		}
		if(mode.equalsIgnoreCase("Rename")){
			subject = "Policy has been Renamed : "+entityItem.getPolicyName();
			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() 
					 + '\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)";
		}
		if(mode.equalsIgnoreCase("DeleteAll")){
			subject = "Policy has been Deleted : "+entityItem.getPolicyName();
			message = "The Policy Which you are watching in  " + PolicyController.smtpApplicationName + " has been Deleted with All Versions" + '\n'  + '\n'  + '\n'+ "Scope + Policy Name  : "  + policyName + '\n'  
					 + '\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)";
		}
		if(mode.equalsIgnoreCase("DeleteOne")){
			subject = "Policy has been Deleted : "+entityItem.getPolicyName();
			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()
					 + '\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)";
		}
		if(mode.equalsIgnoreCase("DeleteScope")){
			subject = "Scope has been Deleted : "+entityItem.getPolicyName();
			message = "The Scope Which you are watching in  " + PolicyController.smtpApplicationName + " has been Deleted" + '\n'  + '\n'  + '\n'+ "Scope + Scope Name  : "  + policyName + '\n'  
					 + '\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)";
		}
		if(mode.equalsIgnoreCase("SwitchVersion")){
			subject = "Policy has been SwitchedVersion : "+entityItem.getPolicyName();
			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() 
					 + '\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)";
		}
		if(mode.equalsIgnoreCase("Move")){
			subject = "Policy has been Moved to Other Scope : "+entityItem.getPolicyName();
			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() 
					 + '\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)";
		}
		String policyFileName = entityItem.getPolicyName();
		List<WatchPolicyNotificationTable> watchList = policyNotificationDao.getListDataByPolicyName(policyFileName);
		if(watchList.size() > 0){
			for(WatchPolicyNotificationTable list : watchList){
				to = list.getLoginIds()+"@"+PolicyController.smtpEmailExtension;
				to = to.trim();
				AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
				ctx.register(PolicyNotificationMail.class);
				ctx.refresh();
				JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
				MimeMessage mimeMessage = mailSender.createMimeMessage();
				MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
				try {
					mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
				} catch (UnsupportedEncodingException e) {
					logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception Occured in Policy Notification" +e);
				}
				mailMsg.setTo(to);
				mailMsg.setSubject(subject);
				mailMsg.setText(message);
				mailSender.send(mimeMessage);
			}
		}
	}
}