Approval Batch, prep better JUnit
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / reports / Notify.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */package org.onap.aaf.auth.batch.reports;
21
22 import java.io.BufferedReader;
23 import java.io.File;
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;
32 import java.util.Set;
33
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;
45
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;
52
53         public Notify(AuthzTrans trans) throws APIException, IOException, OrganizationException {
54                 super(trans.env());
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");
61                 }
62                 try {
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);
68                 }
69                 
70                 String line;
71                 StringBuilder sb = new StringBuilder();
72                 BufferedReader br = new BufferedReader(new FileReader(header_html));
73                 try {
74                         while((line=br.readLine())!=null) {
75                                 sb.append(line);
76                                 sb.append('\n');
77                         }
78                         header = sb.toString();
79                 } finally {
80                         br.close();
81                 }
82                 
83                 br = new BufferedReader(new FileReader(footer_html));
84                 try {
85                         while((line=br.readLine())!=null) {
86                                 sb.append(line);
87                                 sb.append('\n');
88                         }
89                         footer = sb.toString();
90                 } finally {
91                         br.close();
92                 }
93         
94                 // Class Load possible data
95                 NotifyBody.load(env.access());
96                 
97         // Create Intermediate Output 
98         File logDir = 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]));
103                 }
104         }
105         }
106
107         @Override
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;
114                 
115
116                 
117                 final Notify notify = this;
118                 final Holder<List<String>> info = new Holder<>(null);
119                 final Set<String> errorSet = new HashSet<>();
120                 
121                 try {
122                         for(File f : notifyFile) {
123                                 CSV csv = new CSV(env.access(),f);
124                                 try {
125                                         csv.visit(new CSV.Visitor() {
126                                                 @Override
127                                                 public void visit(List<String> row) throws IOException, CadiException {
128                                                         if("info".equals(row.get(0))) {
129                                                                 info.set(row);
130                                                         }
131                                                         if(info.get()==null) {
132                                                                 throw new CadiException("First line of Feed MUST contain 'info' record");
133                                                         }
134                                                         String key = row.get(0)+'|'+info.get().get(1);
135                                                         NotifyBody body = NotifyBody.get(key);
136                                                         if(body==null) {
137                                                                 errorSet.add("No NotifyBody defined for " + key);
138                                                         } else {
139                                                                 body.store(row);
140                                                         }
141                                                 }
142                                         });
143                                 } catch (IOException | CadiException e) {
144                                         e.printStackTrace();
145                                 }
146                                 
147                                 // now create Notification
148                                 for(NotifyBody nb : NotifyBody.getAll()) {
149                                         for(String id : nb.users()) {
150                                                 toList.clear();
151                                                 ccList.clear();
152                                                 try {
153                                                         String bodyS = nb.body(noAvg, notify, id);
154                                                         Identity identity = trans.org().getIdentity(noAvg, id);
155                                                         if(!identity.isPerson()) {
156                                                                 identity = identity.responsibleTo();
157                                                         }
158                                                         for(int i=1;i<nb.escalation();++i) {
159                                                                 if(identity != null) {
160                                                                         if(i==1) {
161                                                                                 toList.add(identity.email());
162                                                                         } else {
163                                                                                 identity=identity.responsibleTo();
164                                                                                 ccList.add(identity.email());
165                                                                         }
166                                                                 }
167                                                         }
168                                                                 
169                                                         mailer.sendEmail(noAvg, dryRun, mailFrom, toList, ccList, subject, 
170                                                                         String.format(header,"2.1.9",Identity.mixedCase(identity.firstName()))+
171                                                                         bodyS +
172                                                                         footer, urgent);
173                                                 } catch (OrganizationException e) {
174                                                         trans.error().log(e);
175                                                 }
176                                         }
177                                 }
178
179                         }
180                 } finally {
181                         for(String s : errorSet) {
182                                 trans.audit().log(s);
183                         }
184                 }
185         }
186         
187         @Override
188         protected void _close(AuthzTrans trans) {
189         }
190
191 }