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.FileInputStream;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.lang.reflect.Constructor;
28 import java.lang.reflect.InvocationTargetException;
29 import java.util.ArrayList;
30 import java.util.HashSet;
31 import java.util.List;
34 import org.onap.aaf.auth.batch.Batch;
35 import org.onap.aaf.auth.batch.reports.bodies.NotifyBody;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.auth.org.Mailer;
38 import org.onap.aaf.auth.org.Organization.Identity;
39 import org.onap.aaf.auth.org.OrganizationException;
40 import org.onap.aaf.cadi.Access;
41 import org.onap.aaf.cadi.CadiException;
42 import org.onap.aaf.cadi.client.Holder;
43 import org.onap.aaf.cadi.util.CSV;
44 import org.onap.aaf.misc.env.APIException;
46 public class Notify extends Batch {
47 private final Mailer mailer;
48 private final String mailFrom;
49 private final String header;
50 private final String footer;
51 private List<File> notifyFile;
53 public Notify(AuthzTrans trans) throws APIException, IOException, OrganizationException {
55 String mailerCls = env.getProperty("MAILER");
56 mailFrom = env.getProperty("MAIL_FROM");
57 String header_html = env.getProperty("HEADER_HTML");
58 String footer_html = env.getProperty("FOOTER_HTML");
59 if(mailerCls==null || mailFrom==null || header_html==null || footer_html==null) {
60 throw new APIException("Notify requires MAILER, MAILER_FROM, HEADER_HTML and FOOTER_HTML properties");
63 Class<?> mailc = Class.forName(mailerCls);
64 Constructor<?> mailcst = mailc.getConstructor(Access.class);
65 mailer = (Mailer)mailcst.newInstance(env.access());
66 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
67 throw new APIException("Unable to construct " + mailerCls,e);
71 StringBuilder sb = new StringBuilder();
72 BufferedReader br = new BufferedReader(new FileReader(header_html));
74 while((line=br.readLine())!=null) {
78 header = sb.toString();
83 br = new BufferedReader(new FileReader(footer_html));
85 while((line=br.readLine())!=null) {
89 footer = sb.toString();
94 // Class Load possible data
95 NotifyBody.load(env.access());
97 // Create Intermediate Output
98 File logDir = new File(logDir());
99 notifyFile = new ArrayList<>();
100 if(args().length>0) {
101 for(int i=0;i<args().length;++i) {
102 notifyFile.add(new File(logDir, args()[i]));
108 protected void run(AuthzTrans trans) {
109 List<String> toList = new ArrayList<>();
110 List<String> ccList = new ArrayList<>();
111 AuthzTrans noAvg = trans.env().newTransNoAvg();
112 String subject = "Test Notify";
113 boolean urgent = false;
117 final Notify notify = this;
118 final Holder<List<String>> info = new Holder<>(null);
119 final Set<String> errorSet = new HashSet<>();
122 for(File f : notifyFile) {
123 CSV csv = new CSV(f);
125 csv.visit(new CSV.Visitor() {
127 public void visit(List<String> row) throws IOException, CadiException {
128 if("info".equals(row.get(0))) {
131 if(info.get()==null) {
132 throw new CadiException("First line of Feed MUST contain 'info' record");
134 String key = row.get(0)+'|'+info.get().get(1);
135 NotifyBody body = NotifyBody.get(key);
137 errorSet.add("No NotifyBody defined for " + key);
143 } catch (IOException | CadiException e) {
147 // now create Notification
148 for(NotifyBody nb : NotifyBody.getAll()) {
149 for(String id : nb.users()) {
153 String bodyS = nb.body(noAvg, notify, id);
154 Identity identity = trans.org().getIdentity(noAvg, id);
155 if(!identity.isPerson()) {
156 identity = identity.responsibleTo();
158 for(int i=1;i<nb.escalation();++i) {
159 if(identity != null) {
161 toList.add(identity.email());
163 identity=identity.responsibleTo();
164 ccList.add(identity.email());
169 mailer.sendEmail(noAvg, dryRun, mailFrom, toList, ccList, subject,
170 String.format(header,"2.1.9",Identity.mixedCase(identity.firstName()))+
173 } catch (OrganizationException e) {
174 trans.error().log(e);
181 for(String s : errorSet) {
182 trans.audit().log(s);
188 protected void _close(AuthzTrans trans) {