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