2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 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====================================================
20 */package org.onap.aaf.auth.batch.reports;
22 import java.io.BufferedReader;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.lang.reflect.Constructor;
27 import java.lang.reflect.InvocationTargetException;
28 import java.util.ArrayList;
29 import java.util.HashSet;
30 import java.util.List;
33 import org.onap.aaf.auth.batch.Batch;
34 import org.onap.aaf.auth.batch.reports.bodies.NotifyBody;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.org.Mailer;
37 import org.onap.aaf.auth.org.Organization.Identity;
38 import org.onap.aaf.auth.org.OrganizationException;
39 import org.onap.aaf.cadi.Access;
40 import org.onap.aaf.cadi.CadiException;
41 import org.onap.aaf.cadi.client.Holder;
42 import org.onap.aaf.cadi.util.CSV;
43 import org.onap.aaf.misc.env.APIException;
44 import org.onap.aaf.misc.env.util.Chrono;
46 public class Notify extends Batch {
47 private static final String HTML_CSS = "HTML_CSS";
48 private final Mailer mailer;
49 private final String header;
50 private final String footer;
51 private List<File> notifyFile;
52 public final String guiURL;
53 private int maxEmails;
56 public Notify(AuthzTrans trans) throws APIException, IOException, OrganizationException {
58 String mailerCls = env.getProperty("MAILER");
59 String mailFrom = env.getProperty("MAIL_FROM");
60 String header_html = env.getProperty("HEADER_HTML");
61 String footer_html = env.getProperty("FOOTER_HTML");
62 String maxEmails = env.getProperty("MAX_EMAIL");
63 guiURL = env.getProperty("GUI_URL");
64 this.maxEmails = maxEmails==null?1:Integer.parseInt(maxEmails);
65 if(mailerCls==null || mailFrom==null || guiURL==null || header_html==null || footer_html==null) {
66 throw new APIException("Notify requires MAILER, MAILER_FROM, GUI_URL, HEADER_HTML and FOOTER_HTML properties");
69 Class<?> mailc = Class.forName(mailerCls);
70 Constructor<?> mailcst = mailc.getConstructor(Access.class);
71 mailer = (Mailer)mailcst.newInstance(env.access());
72 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
73 throw new APIException("Unable to construct " + mailerCls,e);
77 StringBuilder sb = new StringBuilder();
78 BufferedReader br = new BufferedReader(new FileReader(header_html));
80 while((line=br.readLine())!=null) {
84 String html_css = env.getProperty(HTML_CSS);
85 int hc = sb.indexOf(HTML_CSS);
86 if(hc!=0 && html_css!=null) {
87 header = sb.replace(hc,hc+HTML_CSS.length(), html_css).toString();
89 header = sb.toString();
97 // Establish index from header
98 int lastTag = header.lastIndexOf('<');
100 int prevCR = header.lastIndexOf('\n',lastTag);
102 indent = lastTag-prevCR;
104 indent = 6; //arbitrary
110 br = new BufferedReader(new FileReader(footer_html));
112 while((line=br.readLine())!=null) {
116 footer = sb.toString();
121 // Class Load possible data
122 NotifyBody.load(env.access());
124 // Create Intermediate Output
125 File logDir = logDir();
126 notifyFile = new ArrayList<>();
127 if(args().length>0) {
128 for(int i=0;i<args().length;++i) {
129 notifyFile.add(new File(logDir, args()[i]));
132 String fmt = "%s"+Chrono.dateOnlyStamp()+".csv";
134 for(NotifyBody nb : NotifyBody.getAll()) {
135 file = new File(logDir,String.format(fmt, nb.name()));
137 trans.info().printf("Processing %s",file.getCanonicalPath());
138 notifyFile.add(file);
140 trans.info().printf("No Files found for %s",nb.name());
147 protected void run(AuthzTrans trans) {
148 List<String> toList = new ArrayList<>();
149 List<String> ccList = new ArrayList<>();
150 AuthzTrans noAvg = trans.env().newTransNoAvg();
151 String subject = "Test Notify";
152 boolean urgent = false;
156 final Notify notify = this;
157 final Holder<List<String>> info = new Holder<>(null);
158 final Set<String> errorSet = new HashSet<>();
162 for(File f : notifyFile) {
163 CSV csv = new CSV(env.access(),f);
165 csv.visit(new CSV.Visitor() {
167 public void visit(List<String> row) throws IOException, CadiException {
168 if("info".equals(row.get(0))) {
171 if(info.get()==null) {
172 throw new CadiException("First line of Feed MUST contain 'info' record");
174 String key = row.get(0)+'|'+info.get().get(1);
175 NotifyBody body = NotifyBody.get(key);
177 errorSet.add("No NotifyBody defined for " + key);
183 } catch (IOException | CadiException e) {
187 // now create Notification
188 for(NotifyBody nb : NotifyBody.getAll()) {
189 for(String id : nb.users()) {
193 Identity identity = trans.org().getIdentity(noAvg, id);
194 if(!identity.isPerson()) {
195 identity = identity.responsibleTo();
197 for(int i=1;i<nb.escalation();++i) {
198 if(identity != null) {
200 toList.add(identity.email());
202 identity=identity.responsibleTo();
203 ccList.add(identity.email());
208 StringBuilder content = new StringBuilder();
209 content.append(String.format(header,version,Identity.mixedCase(identity.firstName())));
211 nb.body(noAvg, content, indent, notify, id);
212 content.append(footer);
214 if(!mailer.sendEmail(noAvg, dryRun, toList, ccList, subject,content.toString(), urgent)) {
215 trans.error().log("Mailer failed to send Mail");
217 if(maxEmails>0 && mailer.count()>=maxEmails) {
220 } catch (OrganizationException e) {
221 trans.error().log(e);
228 for(String s : errorSet) {
229 trans.audit().log(s);
235 protected void _close(AuthzTrans trans) {