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