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