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