Update css file name in conf.py
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / admin / PolicyNotificationMail.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
6  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * Modifications Copyright (C) 2019 Bell Canada
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.admin;
24
25 import java.io.File;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.List;
31 import java.util.Properties;
32
33 import javax.mail.internet.InternetAddress;
34 import javax.mail.internet.MimeMessage;
35 import javax.script.SimpleBindings;
36
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.controller.PolicyController;
40 import org.onap.policy.rest.dao.CommonClassDao;
41 import org.onap.policy.rest.jpa.PolicyVersion;
42 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
43 import org.onap.policy.xacml.api.XACMLErrorConstants;
44 import org.springframework.beans.factory.annotation.Configurable;
45 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
46 import org.springframework.context.annotation.Bean;
47 import org.springframework.mail.javamail.JavaMailSenderImpl;
48 import org.springframework.mail.javamail.MimeMessageHelper;
49
50 /**
51  * Send policy notification mail depending on the mode for every policy being watched.
52  */
53 @Configurable
54 public class PolicyNotificationMail {
55     private static final String POLICY_WATCHING_MESSAGE = "The Policy Which you are watching in  ";
56     private static final String EMAIL_MESSAGE_POSTSCRIPT =
57             "Policy Notification System  (please don't respond to this email)";
58     private static final String ACTIVE_VERSION = "Active Version  : ";
59     private static final String SCOPE_POLICY_NAME = "Scope + Policy Name  : ";
60     private static final String DELETED_TIME = "Deleted Time  : ";
61     private static final String DELETED_BY = "Deleted By : ";
62     private static Logger policyLogger = FlexLogger.getLogger(PolicyNotificationMail.class);
63
64     /**
65      * javaMailSenderImpl.
66      *
67      * @return JavaMailSenderImpl object
68      */
69     @Bean
70     public JavaMailSenderImpl javaMailSenderImpl() {
71         JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
72         mailSender.setHost(PolicyController.getSmtpHost());
73         mailSender.setPort(Integer.parseInt(PolicyController.getSmtpPort()));
74         mailSender.setUsername(PolicyController.getSmtpUsername());
75         mailSender.setPassword(PolicyController.getSmtpPassword());
76         Properties prop = mailSender.getJavaMailProperties();
77         prop.put("mail.transport.protocol", "smtp");
78         prop.put("mail.smtp.auth", "true");
79         prop.put("mail.smtp.starttls.enable", "true");
80         prop.put("mail.debug", "true");
81         return mailSender;
82     }
83
84     /**
85      * Depending on the mode of operation on the policy, compose the subject and message.
86      * Invoke another internal method to actual send the mail. If the watch list is empty , then
87      * this method returns without sending notification mail
88      *
89      * @param entityItem Database item from which policy name could be extracted
90      * @param policyName Name of the policy for which notification is to be sent
91      * @param mode kind of operation done on the policy
92      * @param policyNotificationDao database access object for policy
93      */
94     public void sendMail(PolicyVersion entityItem, String policyName, String mode,
95             CommonClassDao policyNotificationDao) {
96
97         String subject = "";
98         String message = "";
99         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
100         Date date = new Date();
101         if ("EditPolicy".equalsIgnoreCase(mode)) {
102             subject = "Policy has been Updated : " + entityItem.getPolicyName();
103             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Updated" + '\n'
104                     + '\n' + '\n' + SCOPE_POLICY_NAME + policyName + '\n' + ACTIVE_VERSION
105                     + entityItem.getActiveVersion() + '\n' + '\n' + "Modified By : " + entityItem.getModifiedBy() + '\n'
106                     + "Modified Time  : " + dateFormat.format(date) + '\n' + '\n' + '\n' + '\n'
107                     + EMAIL_MESSAGE_POSTSCRIPT;
108         }
109         if ("Rename".equalsIgnoreCase(mode)) {
110             subject = "Policy has been Renamed : " + entityItem.getPolicyName();
111             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Renamed" + '\n'
112                     + '\n' + '\n' + SCOPE_POLICY_NAME + policyName + '\n' + ACTIVE_VERSION
113                     + entityItem.getActiveVersion() + '\n' + '\n' + "Renamed By : " + entityItem.getModifiedBy() + '\n'
114                     + "Renamed Time  : " + dateFormat.format(date) + '\n' + '\n' + '\n' + '\n'
115                     + EMAIL_MESSAGE_POSTSCRIPT;
116         }
117         if ("DeleteAll".equalsIgnoreCase(mode)) {
118             subject = "Policy has been Deleted : " + entityItem.getPolicyName();
119             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName()
120                     + " has been Deleted with All Versions" + '\n' + '\n' + '\n' + SCOPE_POLICY_NAME + policyName + '\n'
121                     + '\n' + '\n' + DELETED_BY + entityItem.getModifiedBy() + '\n' + DELETED_TIME
122                     + dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
123         }
124         if ("DeleteOne".equalsIgnoreCase(mode)) {
125             subject = "Policy has been Deleted : " + entityItem.getPolicyName();
126             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been Deleted" + '\n'
127                     + '\n' + '\n' + SCOPE_POLICY_NAME + policyName + '\n' + "Policy Version : "
128                     + entityItem.getActiveVersion() + '\n' + '\n' + DELETED_BY + entityItem.getModifiedBy() + '\n'
129                     + DELETED_TIME + dateFormat.format(date) + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
130         }
131         if ("DeleteScope".equalsIgnoreCase(mode)) {
132             subject = "Scope has been Deleted : " + entityItem.getPolicyName();
133             message = "The Scope Which you are watching in  " + PolicyController.getSmtpApplicationName()
134                     + " has been Deleted" + '\n' + '\n' + '\n' + "Scope + Scope Name  : " + policyName + '\n' + '\n'
135                     + '\n' + DELETED_BY + entityItem.getModifiedBy() + '\n' + DELETED_TIME + dateFormat.format(date)
136                     + '\n' + '\n' + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
137         }
138         if ("SwitchVersion".equalsIgnoreCase(mode)) {
139             subject = "Policy has been SwitchedVersion : " + entityItem.getPolicyName();
140             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName() + " has been SwitchedVersion"
141                     + '\n' + '\n' + '\n' + SCOPE_POLICY_NAME + policyName + '\n' + ACTIVE_VERSION
142                     + entityItem.getActiveVersion() + '\n' + '\n' + "Switched By : " + entityItem.getModifiedBy() + '\n'
143                     + "Switched Time  : " + dateFormat.format(date) + '\n' + '\n' + '\n' + '\n'
144                     + EMAIL_MESSAGE_POSTSCRIPT;
145         }
146         if ("Move".equalsIgnoreCase(mode)) {
147             subject = "Policy has been Moved to Other Scope : " + entityItem.getPolicyName();
148             message = POLICY_WATCHING_MESSAGE + PolicyController.getSmtpApplicationName()
149                     + " has been Moved to Other Scope" + '\n' + '\n' + '\n' + SCOPE_POLICY_NAME + policyName + '\n'
150                     + ACTIVE_VERSION + entityItem.getActiveVersion() + '\n' + '\n' + "Moved By : "
151                     + entityItem.getModifiedBy() + '\n' + "Moved Time  : " + dateFormat.format(date) + '\n' + '\n'
152                     + '\n' + '\n' + EMAIL_MESSAGE_POSTSCRIPT;
153         }
154         String checkPolicyName = findCheckPolicyName(policyName);
155
156         String policyFileName = findPolicyFileName(entityItem);
157         String query = "from WatchPolicyNotificationTable where policyName like:policyFileName";
158         List<Object> watchList = findWatchList(policyNotificationDao, policyFileName, query);
159         if (!watchList.isEmpty()) {
160             composeAndSendMail(mode, policyNotificationDao, subject, message, checkPolicyName, watchList);
161         }
162     }
163
164     private List<Object> findWatchList(CommonClassDao policyNotificationDao, String policyFileName, String query) {
165         SimpleBindings params = new SimpleBindings();
166         params.put("policyFileName", policyFileName);
167         List<Object> watchList;
168         if (PolicyController.isjUnit()) {
169             watchList = policyNotificationDao.getDataByQuery(query, null);
170         } else {
171             watchList = policyNotificationDao.getDataByQuery(query, params);
172         }
173
174         if (watchList == null || watchList.isEmpty()) {
175             policyLogger
176                     .debug("List of policy being watched is either null or empty, hence return without sending mail");
177             watchList = new ArrayList<>();
178         }
179         return watchList;
180     }
181
182     private String findPolicyFileName(PolicyVersion entityItem) {
183         String policyFileName = entityItem.getPolicyName();
184         if (policyFileName.contains("/")) {
185             policyFileName = policyFileName.substring(0, policyFileName.indexOf('/'));
186             policyFileName = policyFileName.replace("/", File.separator);
187         }
188         if (policyFileName.contains("\\")) {
189             policyFileName = policyFileName.substring(0, policyFileName.indexOf('\\'));
190             policyFileName = policyFileName.replace("\\", "\\\\");
191         }
192
193         policyFileName += "%";
194         return policyFileName;
195     }
196
197     private String findCheckPolicyName(String policyName) {
198         String checkPolicyName = policyName;
199         if (checkPolicyName.endsWith(".xml") || checkPolicyName.contains(".")) {
200             checkPolicyName = checkPolicyName.substring(0, checkPolicyName.indexOf('.'));
201         }
202         return checkPolicyName;
203     }
204
205     /**
206      * For every policy being watched and when the policy name is one of the Config_, Action_ or Decision_,
207      * send the notification.
208      *
209      * @param mode String for the mode
210      * @param policyNotificationDao CommonClassDao
211      * @param subject String subject
212      * @param message String message
213      * @param checkPolicyName String check policy name
214      * @param watchList List of watch objects
215      */
216     private void composeAndSendMail(String mode, CommonClassDao policyNotificationDao, String subject, String message,
217             String checkPolicyName, List<Object> watchList) {
218         String from = PolicyController.getSmtpUsername();
219         String to;
220         for (Object watch : watchList) {
221             WatchPolicyNotificationTable list = (WatchPolicyNotificationTable) watch;
222             String watchPolicyName = list.getPolicyName();
223             // this condition check for specific stringin policy name being watched and
224             // also if the policy being checked is different from the watched ones,
225             // then there is no need to send mail, hence continue with next policy in the loop
226             if ((watchPolicyName.contains("Config_") || watchPolicyName.contains("Action_")
227                     || watchPolicyName.contains("Decision_")) && !watchPolicyName.equals(checkPolicyName)) {
228                 continue;
229             }
230             try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
231                 to = list.getLoginIds() + "@" + PolicyController.getSmtpEmailExtension();
232                 to = to.trim();
233                 ctx.register(PolicyNotificationMail.class);
234                 ctx.refresh();
235                 JavaMailSenderImpl mailSender = ctx.getBean(JavaMailSenderImpl.class);
236                 MimeMessage mimeMessage = mailSender.createMimeMessage();
237                 MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
238                 mailMsg.setFrom(new InternetAddress(from, "Policy Notification System"));
239                 mailMsg.setTo(to);
240                 mailMsg.setSubject(subject);
241                 mailMsg.setText(message);
242                 mailSender.send(mimeMessage);
243                 if ("Rename".equalsIgnoreCase(mode) || mode.contains("Delete") || mode.contains("Move")) {
244                     policyNotificationDao.delete(watch);
245                 }
246             } catch (Exception e) {
247                 policyLogger
248                         .error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Exception Occured in Policy Notification" + e);
249             }
250
251         }
252     }
253 }