Merge "Sonar Fix: List.java"
[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.File;
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;
31 import java.util.Map;
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                 FileInputStream fis = new FileInputStream(header_html);
71                 try {
72                         byte[] content = new byte[(int)fis.getChannel().size()];
73                         fis.read(content);
74                         header = new String(content);
75                 } finally {
76                         fis.close();
77                 }
78                 
79                 fis = new FileInputStream(footer_html);
80                 try {
81                         byte[] content = new byte[(int)fis.getChannel().size()];
82                         fis.read(content);
83                         footer = new String(content);
84                 } finally {
85                         fis.close();
86                 }
87         
88                 // Class Load possible data
89                 NotifyBody.load(env.access());
90                 
91         // Create Intermediate Output 
92         File logDir = new File(logDir());
93         notifyFile = new ArrayList<>();
94         if(args().length>0) {
95                 for(int i=0;i<args().length;++i) {
96                         notifyFile.add(new File(logDir, args()[i]));
97                 }
98         }
99         }
100
101         @Override
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;
108                 
109
110                 
111                 final Notify notify = this;
112                 final Holder<List<String>> info = new Holder<>(null);
113                 final Set<String> errorSet = new HashSet<>();
114                 
115                 try {
116                         for(File f : notifyFile) {
117                                 CSV csv = new CSV(f);
118                                 try {
119                                         csv.visit(new CSV.Visitor() {
120                                                 @Override
121                                                 public void visit(List<String> row) throws IOException, CadiException {
122                                                         if("info".equals(row.get(0))) {
123                                                                 info.set(row);
124                                                         }
125                                                         if(info.get()==null) {
126                                                                 throw new CadiException("First line of Feed MUST contain 'info' record");
127                                                         }
128                                                         String key = row.get(0)+'|'+info.get().get(1);
129                                                         NotifyBody body = NotifyBody.get(key);
130                                                         if(body==null) {
131                                                                 errorSet.add("No NotifyBody defined for " + key);
132                                                         } else {
133                                                                 body.store(row);
134                                                         }
135                                                 }
136                                         });
137                                 } catch (IOException | CadiException e) {
138                                         e.printStackTrace();
139                                 }
140                                 
141                                 // now create Notification
142                                 for(NotifyBody nb : NotifyBody.getAll()) {
143                                         for(String id : nb.users()) {
144                                                 toList.clear();
145                                                 ccList.clear();
146                                                 try {
147                                                         String bodyS = nb.body(noAvg, notify, id);
148                                                         Identity identity = trans.org().getIdentity(noAvg, id);
149                                                         if(!identity.isPerson()) {
150                                                                 identity = identity.responsibleTo();
151                                                         }
152                                                         for(int i=1;i<nb.escalation();++i) {
153                                                                 if(identity != null) {
154                                                                         if(i==1) {
155                                                                                 toList.add(identity.email());
156                                                                         } else {
157                                                                                 identity=identity.responsibleTo();
158                                                                                 ccList.add(identity.email());
159                                                                         }
160                                                                 }
161                                                         }
162                                                                 
163                                                         mailer.sendEmail(noAvg, dryRun, mailFrom, toList, ccList, subject, 
164                                                                         String.format(header,"2.1.9",Identity.mixedCase(identity.firstName()))+
165                                                                         bodyS +
166                                                                         footer, urgent);
167                                                 } catch (OrganizationException e) {
168                                                         trans.error().log(e);
169                                                 }
170                                         }
171                                 }
172
173                         }
174                 } finally {
175                         for(String s : errorSet) {
176                                 trans.audit().log(s);
177                         }
178                 }
179         }
180         
181         @Override
182         protected void _close(AuthzTrans trans) {
183         }
184
185 }