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;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.InvocationTargetException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 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);
70 FileInputStream fis = new FileInputStream(header_html);
72 byte[] content = new byte[(int)fis.getChannel().size()];
74 header = new String(content);
79 fis = new FileInputStream(footer_html);
81 byte[] content = new byte[(int)fis.getChannel().size()];
83 footer = new String(content);
88 // Class Load possible data
89 NotifyBody.load(env.access());
91 // Create Intermediate Output
92 File logDir = new File(logDir());
93 notifyFile = new ArrayList<>();
95 for(int i=0;i<args().length;++i) {
96 notifyFile.add(new File(logDir, args()[i]));
102 protected void run(AuthzTrans trans) {
103 List<String> toList = new ArrayList<>();
104 List<String> ccList = new ArrayList<>();
105 AuthzTrans noAvg = trans.env().newTransNoAvg();
106 String subject = "Test Notify";
107 boolean urgent = false;
111 final Notify notify = this;
112 final Holder<List<String>> info = new Holder<>(null);
113 final Set<String> errorSet = new HashSet<>();
116 for(File f : notifyFile) {
117 CSV csv = new CSV(f);
119 csv.visit(new CSV.Visitor() {
121 public void visit(List<String> row) throws IOException, CadiException {
122 if("info".equals(row.get(0))) {
125 if(info.get()==null) {
126 throw new CadiException("First line of Feed MUST contain 'info' record");
128 String key = row.get(0)+'|'+info.get().get(1);
129 NotifyBody body = NotifyBody.get(key);
131 errorSet.add("No NotifyBody defined for " + key);
137 } catch (IOException | CadiException e) {
141 // now create Notification
142 for(NotifyBody nb : NotifyBody.getAll()) {
143 for(String id : nb.users()) {
147 String bodyS = nb.body(noAvg, notify, id);
148 Identity identity = trans.org().getIdentity(noAvg, id);
149 if(!identity.isPerson()) {
150 identity = identity.responsibleTo();
152 for(int i=1;i<nb.escalation();++i) {
153 if(identity != null) {
155 toList.add(identity.email());
157 identity=identity.responsibleTo();
158 ccList.add(identity.email());
163 mailer.sendEmail(noAvg, dryRun, mailFrom, toList, ccList, subject,
164 String.format(header,"2.1.9",Identity.mixedCase(identity.firstName()))+
167 } catch (OrganizationException e) {
168 trans.error().log(e);
175 for(String s : errorSet) {
176 trans.audit().log(s);
182 protected void _close(AuthzTrans trans) {