Improve Batches
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / reports / bodies / NotifyBody.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  */
21 package org.onap.aaf.auth.batch.reports.bodies;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Modifier;
28 import java.net.URISyntaxException;
29 import java.net.URL;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.TreeMap;
38
39 import org.onap.aaf.auth.batch.reports.Notify;
40 import org.onap.aaf.auth.env.AuthzTrans;
41 import org.onap.aaf.cadi.Access;
42 import org.onap.aaf.cadi.config.Config;
43 import org.onap.aaf.misc.env.APIException;
44
45 public abstract class NotifyBody {
46         private static final String DUPL = "<td style=\"text-indent: 4em;\">''</td>";
47         private static final Map<String,NotifyBody> bodyMap = new HashMap<>();
48
49         protected Map<String,List<List<String>>> rows;
50         private final String name;
51         private final String type;
52         protected final String env;
53         private String date;
54         private int escalation;
55         private int count;
56         
57         public NotifyBody(Access access, final String type, final String name) {
58                 rows = new TreeMap<>();
59                 this.name = name;
60                 this.type = type;
61                 date="";
62                 escalation = 1;
63                 count = 0;
64                 env = access.getProperty(Config.AAF_ENV,"DEVL");
65         }
66         
67         public void store(List<String> row) {
68                 if(!row.isEmpty()) {
69                         if("info".equals(row.get(0))) {
70                                 if(row.size()>2) {
71                                         date = row.get(2);
72                                 }
73                                 if(row.size()>3) {
74                                         escalation = Integer.parseInt(row.get(3));
75                                 }
76                                 return;
77                         } else if(type.equals(row.get(0))) {
78                                 String user = user(row);
79                                 if(user!=null) {
80                                         List<List<String>> lss = rows.get(user); 
81                                         if(lss == null) {
82                                                 lss = new ArrayList<>();
83                                                 rows.put(user,lss);
84                                         }
85                                         lss.add(row);
86                                 }
87                         }
88                 }
89         }
90
91         public String name() {
92                 return name;
93         }
94         
95         public String type() {
96                 return type;
97         }
98         
99         public String date() {
100                 return date;
101         }
102         public int escalation() {
103                 return escalation;
104         }
105         
106         public Set<String> users() {
107                 return rows.keySet();
108         }
109         
110         /**
111          * ID must be set from Row for Email lookup
112          * 
113          * @param trans
114          * @param n
115          * @param id
116          * @param row
117          * @return
118          */
119         public abstract boolean body(AuthzTrans trans, StringBuilder sb, int indent, Notify n, String id);
120         
121         /**
122          * Return "null" if user not found in row... Code will handle.
123          * @param row
124          * @return
125          */
126         protected abstract String user(List<String> row);
127         
128         public abstract String subject();
129
130         /**
131          * Get Notify Body based on key of
132          * type|name
133          */
134         public static NotifyBody get(String key) {
135                 return bodyMap.get(key);
136         }
137         
138         /**
139          * Return set of loaded NotifyBodies
140          * 
141          */
142         public static Collection<NotifyBody> getAll() {
143                 // Note: The same Notify Body is entered several times with different keys.
144                 // Therefore, need a Set of Values, not all the Values.
145                 Set<NotifyBody> set = new HashSet<>();
146                 set.addAll(bodyMap.values());
147                 return set;
148         }
149         
150         /**
151          * @param propAccess 
152          * @throws URISyntaxException 
153          * 
154          */
155         public static void load(Access access) throws APIException, IOException {
156                 // class load available NotifyBodies
157                 ClassLoader cl = Thread.currentThread().getContextClassLoader();
158                 Package pkg = NotifyBody.class.getPackage();
159                 String path = pkg.getName().replace('.', '/');
160                 URL url = cl.getResource(path);
161                 if(url == null) {
162                         throw new APIException("Cannot load resources from " + path);
163                 }
164                 File dir;
165                 try {
166                         dir = new File(url.toURI());
167                 } catch (URISyntaxException e) {
168                         throw new APIException(e);
169                 }
170                 if(dir.exists()) {
171                         String[] files = dir.list();
172                         if(files!=null) {
173                                 for(String sf : files) {
174                                         int dot = sf.indexOf('.');
175                                         if(dot>=0) {
176                                                 String cls = pkg.getName()+'.'+sf.substring(0,dot);
177                                                 try {
178                                                         Class<?> c = cl.loadClass(cls);
179                                                         if(c!=null) {
180                                                                 if(!Modifier.isAbstract(c.getModifiers())) {
181                                                                         Constructor<?> cst = c.getConstructor(Access.class);
182                                                                         NotifyBody nb = (NotifyBody)cst.newInstance(access);
183                                                                         if(nb!=null) {
184                                                                                 bodyMap.put("info|"+nb.name, nb);
185                                                                                 bodyMap.put(nb.type+'|'+nb.name, nb);
186                                                                         }
187                                                                 }
188                                                         }
189                                                 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
190                                                         e.printStackTrace();
191                                                 }
192                                         }
193                                 }
194                         }
195                 }
196         }
197
198         protected void print(StringBuilder sb, int indent, Object ... objs) {
199                 for(int i=0;i<indent;++i) {
200                         sb.append(' ');
201                 }
202                 for(Object o : objs) {
203                         sb.append(o.toString());
204                 }
205         }
206                         
207         protected void println(StringBuilder sb, int indent, Object ... objs) {
208                 print(sb,indent,objs);
209                 sb.append('\n');
210         }
211
212         protected void printf(StringBuilder sb, int indent, String fmt, Object ... objs) {
213                 print(sb,indent,String.format(fmt, objs));
214         }
215
216         protected String printCell(StringBuilder sb, int indent, String current, String prev) {
217                 if(current.equals(prev)) {
218                         println(sb,indent,DUPL);
219                 } else {
220                         printCell(sb,indent,current);
221                 }
222                 return current; // use to set prev...
223         }
224         
225         protected void printCell(StringBuilder sb, int indent, String current) {
226                 println(sb,indent,"<td>",current,"</td>");
227         }
228         
229         public synchronized void inc() {
230                 ++count;
231         }
232         
233         public int count() {
234                 return count;
235         }
236 }