Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / Approval.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  *  Modifications Copyright (C) 2019 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.batch.helpers;
25
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.TreeMap;
32 import java.util.UUID;
33
34 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.layer.Result;
37 import org.onap.aaf.cadi.util.CSV;
38 import org.onap.aaf.misc.env.TimeTaken;
39 import org.onap.aaf.misc.env.Trans;
40
41 import com.datastax.driver.core.ResultSet;
42 import com.datastax.driver.core.Row;
43 import com.datastax.driver.core.Session;
44 import com.datastax.driver.core.SimpleStatement;
45 import com.datastax.driver.core.Statement;
46
47 public class Approval implements CacheChange.Data  {
48     public static final String ADD_USER_TO_ROLE = "Add User [";
49     public static final String RE_APPROVAL_IN_ROLE = "Extend access of User [";
50     public static final String RE_VALIDATE_ADMIN = "Revalidate as Admin of AAF Namespace [";
51     public static final String RE_VALIDATE_OWNER = "Revalidate as Owner of AAF Namespace [";
52
53     public static TreeMap<String,List<Approval>> byApprover = new TreeMap<>();
54     public static TreeMap<String,List<Approval>> byUser = new TreeMap<>();
55     public static TreeMap<UUID,List<Approval>> byTicket = new TreeMap<>();
56     public static List<Approval> list = new LinkedList<>();
57     private static final CacheChange<Approval> cache = new CacheChange<>();
58     
59     public final ApprovalDAO.Data add;
60     private String role;
61
62     public static Creator<Approval> v2_0_17 = new Creator<Approval>() {
63         @Override
64         public Approval create(Row row) {
65             return new Approval(row.getUUID(0), row.getUUID(1), row.getString(2),
66                     row.getString(3),row.getString(4),row.getString(5),row.getString(6),row.getString(7),
67                     row.getLong(8)/1000);
68         }
69
70         @Override
71         public String select() {
72             return "select id,ticket,approver,user,memo,operation,status,type,WRITETIME(status) from authz.approval";
73         }
74     };
75
76     public static Visitor<Approval> FullLoad = new Visitor<Approval>() {
77         @Override
78         public void visit(Approval app) {
79             List<Approval> ln;
80             list.add(app);
81
82             String person = app.getApprover();
83             if (person!=null) {
84                 ln = byApprover.get(person);
85                 if (ln==null) {
86                     ln = new ArrayList<>();
87                     byApprover.put(app.getApprover(), ln);
88                 }
89                 ln.add(app);
90             }
91
92             person = app.getUser();
93             if (person!=null) {
94                 ln = byUser.get(person);
95                 if (ln==null) {
96                     ln = new ArrayList<>();
97                     byUser.put(app.getUser(), ln);
98                 }
99                 ln.add(app);
100             }
101             UUID ticket = app.getTicket();
102             if (ticket!=null) {
103                 ln = byTicket.get(ticket);
104                 if (ln==null) {
105                     ln = new ArrayList<>();
106                     byTicket.put(app.getTicket(), ln);
107                 }
108                 ln.add(app);
109             }
110         }
111     };
112     
113     public Approval(UUID id, UUID ticket, String approver,// Date last_notified, 
114             String user, String memo, String operation, String status, String type, long updated) {
115         add = new ApprovalDAO.Data();
116         add.id = id;
117         add.ticket = ticket;
118         add.approver = approver;
119         add.user = user;
120         add.memo = memo;
121         add.operation = operation;
122         add.status = status;
123         add.type = type;
124         add.updated = new Date(updated);
125         role = roleFromMemo(memo);
126     }
127     
128     public static String roleFromMemo(String memo) {
129         if (memo==null) {
130             return null;
131         }
132         int first = memo.indexOf('[');
133         if (first>=0) {
134             int second = memo.indexOf(']', ++first);
135             if (second>=0) {
136                 String role = memo.substring(first, second);
137                 if (memo.startsWith(RE_VALIDATE_ADMIN)) {
138                     return role + ".admin";
139                 } else if (memo.startsWith(RE_VALIDATE_OWNER)) {
140                     return role + ".owner";
141                 } else {
142                     first = memo.indexOf('[',second);
143                     if(first>=0) {
144                         second = memo.indexOf(']', ++first);
145                         if(second>=0 && (memo.startsWith(RE_APPROVAL_IN_ROLE) ||
146                                 memo.startsWith(ADD_USER_TO_ROLE))) {
147                                 return  memo.substring(first, second);
148                         }
149                     }
150                 }
151             }
152         }
153         return null;
154     }
155
156     public static int load(Trans trans, Session session, Creator<Approval> creator, Visitor<Approval> visitor) {
157         int count = 0;
158         try {
159             count+=call(trans,session,creator.query(null), creator, visitor);
160         } finally {
161             trans.info().log("Found",count,"Approval Records");
162         }
163         return count;
164     }
165     
166     public static int load(Trans trans, Session session, Creator<Approval> creator ) {
167         int count = 0;
168         try {
169             count+=call(trans,session,creator.query(null), creator, FullLoad);
170         } finally {
171             trans.info().log("Found",count,"Approval Records");
172         }
173         return count;
174     }
175     
176     public static int loadUsers(Trans trans, Session session, Set<String> users, Visitor<Approval> visitor) {
177         int total = 0;
178         for(String user : users) {
179             total+=call(trans,session,String.format("%s WHERE user='%s';",v2_0_17.select(), user),v2_0_17,visitor);
180         }
181         return total;
182     }
183     
184     public static void row(CSV.RowSetter crs, Approval app) {
185         crs.row("approval",app.add.id,app.add.ticket,app.add.user,app.role,app.add.memo);
186     }
187
188     private static int call(Trans trans, Session session, String query, Creator<Approval> creator, Visitor<Approval> visitor) {
189         TimeTaken tt = trans.start("DB Query", Trans.REMOTE);
190         ResultSet results;
191         try {
192             Statement stmt = new SimpleStatement( query );
193             results = session.execute(stmt);
194             int count = 0;
195             for (Row row : results.all()) {
196                 ++count;
197                 visitor.visit(creator.create(row));
198             }
199             return count;
200         } finally {
201             tt.done();
202         }
203     }
204     
205     @Override
206     public void expunge() {
207         List<Approval> la = byApprover.get(getApprover());
208         if (la!=null) {
209             la.remove(this);
210         }
211         
212         la = byUser.get(getUser());
213         if (la!=null) {
214             la.remove(this);
215         }
216         UUID ticket = this.add==null?null:this.add.ticket;
217         if (ticket!=null) {
218             la = byTicket.get(this.add.ticket);
219             if (la!=null) {
220                 la.remove(this);
221             }
222         }
223     }
224
225     public static void clear() {
226         byApprover.clear();
227         byUser.clear();
228         byTicket.clear();
229         list.clear();
230         cache.resetLocalData();
231     }
232
233     /**
234      * @return the status
235      */
236     public String getStatus() {
237         return add.status;
238     }
239     /**
240      * @param status the status to set
241      */
242     public void setStatus(String status) {
243         add.status = status;
244     }
245     /**
246      * @return the id
247      */
248     public UUID getId() {
249         return add.id;
250     }
251     /**
252      * @return the ticket
253      */
254     public UUID getTicket() {
255         return add.ticket;
256     }
257     /**
258      * @return the approver
259      */
260     public String getApprover() {
261         return add.approver;
262     }
263     /**
264      * @return the user
265      */
266     public String getUser() {
267         return add.user;
268     }
269     /**
270      * @return the memo
271      */
272     public String getMemo() {
273         return add.memo;
274     }
275     /**
276      * @return the operation
277      */
278     public String getOperation() {
279         return add.operation;
280     }
281     /**
282      * @return the type
283      */
284     public String getType() {
285         return add.type;
286     }
287     public void lapsed() {
288         add.ticket=null;
289         add.status="lapsed";
290     }
291     
292     public String getRole() {
293         return role;
294     }
295     
296     public String toString() {
297         return getUser() + ' ' + getMemo();
298     }
299
300     public void delayDelete(AuthzTrans trans, ApprovalDAO ad, boolean dryRun, String text) {
301         if (dryRun) {
302             trans.info().log(text,"- Would Delete: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
303         } else {
304             Result<Void> rv = ad.delete(trans, add, false);
305             if (rv.isOK()) {
306                 trans.info().log(text,"- Deleted: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
307                 cache.delayedDelete(this);
308             } else {
309                 trans.info().log(text,"- Failed to Delete Approval",getId());
310             }
311         }
312     }
313     
314
315     public static void resetLocalData() {
316         cache.resetLocalData();
317     }
318     
319     public static int sizeForDeletion() {
320         return cache.cacheSize();
321     }
322
323     public static void delayDelete(AuthzTrans noAvg, ApprovalDAO apprDAO, boolean dryRun, List<Approval> list, String text) {
324         if (list!=null) {
325             for (Approval a : list) {
326                 a.delayDelete(noAvg, apprDAO, dryRun,text);
327             }
328         }
329     }
330
331     public static boolean pendingDelete(Approval a) {
332         return cache.contains(a);
333     }
334
335     public static void deleteByIDBatch(StringBuilder sb, String id) {
336         sb.append("DELETE from authz.approval where id=");
337         sb.append(id);
338         sb.append(";\n");
339     }
340
341 }