Merge "Sonar Fix: List.java"
[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.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.TreeMap;
37
38 import org.onap.aaf.auth.batch.reports.Notify;
39 import org.onap.aaf.auth.env.AuthzTrans;
40 import org.onap.aaf.cadi.Access;
41 import org.onap.aaf.misc.env.APIException;
42
43 public abstract class NotifyBody {
44         private static final Map<String,NotifyBody> bodyMap = new HashMap<>();
45
46         protected Map<String,List<List<String>>> rows;
47         private final String name;
48         private final String type;
49         private String date;
50         private int escalation;
51         
52         public NotifyBody(final String type, final String name) {
53                 rows = new TreeMap<>();
54                 this.name = name;
55                 this.type = type;
56                 date="";
57                 escalation = 1;
58         }
59         
60         public void store(List<String> row) {
61                 if(!row.isEmpty()) {
62                         if("info".equals(row.get(0))) {
63                                 if(row.size()>2) {
64                                         date = row.get(2);
65                                 }
66                                 if(row.size()>3) {
67                                         escalation = Integer.parseInt(row.get(3));
68                                 }
69                                 return;
70                         } else if(type.equals(row.get(0))) {
71                                 String user = user(row);
72                                 if(user!=null) {
73                                         List<List<String>> lss = rows.get(user); 
74                                         if(lss == null) {
75                                                 lss = new ArrayList<>();
76                                                 rows.put(user,lss);
77                                         }
78                                         lss.add(row);
79                                 }
80                         }
81                 }
82         }
83
84         public String name() {
85                 return name;
86         }
87         
88         public String date() {
89                 return date;
90         }
91         public int escalation() {
92                 return escalation;
93         }
94         
95         public Set<String> users() {
96                 return rows.keySet();
97         }
98         
99         /**
100          * ID must be set from Row for Email lookup
101          * 
102          * @param trans
103          * @param n
104          * @param id
105          * @param row
106          * @return
107          */
108         public abstract String body(AuthzTrans trans, Notify n, String id);
109         
110         /**
111          * Return "null" if user not found in row... Code will handle.
112          * @param row
113          * @return
114          */
115         protected abstract String user(List<String> row);
116         
117         /**
118          * Get Notify Body based on key of
119          * type|name
120          */
121         public static NotifyBody get(String key) {
122                 return bodyMap.get(key);
123         }
124         
125         /**
126          * Return set of loaded NotifyBodies
127          * 
128          */
129         public static Collection<NotifyBody> getAll() {
130                 return bodyMap.values();
131         }
132         
133         /**
134          * @param propAccess 
135          * @throws URISyntaxException 
136          * 
137          */
138         public static void load(Access access) throws APIException, IOException {
139                 // class load available NotifyBodies
140                 ClassLoader cl = Thread.currentThread().getContextClassLoader();
141                 Package pkg = NotifyBody.class.getPackage();
142                 String path = pkg.getName().replace('.', '/');
143 //              Enumeration<URL> urls = cl.getResources(path);
144 //              while(urls.hasMoreElements()) {
145 //                      URL url = urls.nextElement();
146                 URL url = cl.getResource(path);
147                 if(url == null) {
148                         throw new APIException("Cannot load resources from " + path);
149                 }
150                 System.out.println(url);
151                 File dir;
152                 try {
153                         dir = new File(url.toURI());
154                 } catch (URISyntaxException e) {
155                         throw new APIException(e);
156                 }
157                 if(dir.exists()) {
158                         String[] files = dir.list();
159                         if(files!=null) {
160                                 for(String sf : files) {
161                                         int dot = sf.indexOf('.');
162                                         if(dot>=0) {
163                                                 String cls = pkg.getName()+'.'+sf.substring(0,dot);
164                                                 try {
165                                                         Class<?> c = cl.loadClass(cls);
166                                                         if(c!=null) {
167                                                                 if(!Modifier.isAbstract(c.getModifiers())) {
168                                                                         Constructor<?> cst = c.getConstructor(Access.class);
169                                                                         NotifyBody nb = (NotifyBody)cst.newInstance(access);
170                                                                         if(nb!=null) {
171                                                                                 bodyMap.put("info|"+nb.name, nb);
172                                                                                 bodyMap.put(nb.type+'|'+nb.name, nb);
173                                                                         }
174                                                                 }
175                                                         }
176                                                 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
177                                                         e.printStackTrace();
178                                                 }
179                                         }
180                                 }
181                         }
182                 }
183 //              }
184         }
185 }