Batch work and client
[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  * 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
22 package org.onap.aaf.auth.batch.helpers;
23
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.Iterator;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.TreeMap;
30 import java.util.UUID;
31
32 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.layer.Result;
35 import org.onap.aaf.cadi.util.CSV;
36 import org.onap.aaf.misc.env.Env;
37 import org.onap.aaf.misc.env.TimeTaken;
38 import org.onap.aaf.misc.env.Trans;
39
40 import com.datastax.driver.core.ResultSet;
41 import com.datastax.driver.core.Row;
42 import com.datastax.driver.core.Session;
43 import com.datastax.driver.core.SimpleStatement;
44 import com.datastax.driver.core.Statement;
45
46 public class Approval implements CacheChange.Data  {
47     public static final String RE_APPROVAL_IN_ROLE = "Re-Approval in Role '";
48     public static final String RE_VALIDATE_ADMIN = "Re-Validate as Administrator for AAF Namespace '";
49     public static final String RE_VALIDATE_OWNER = "Re-Validate Ownership for AAF Namespace '";
50
51     public static TreeMap<String,List<Approval>> byApprover = new TreeMap<>();
52     public static TreeMap<String,List<Approval>> byUser = new TreeMap<>();
53     public static TreeMap<UUID,List<Approval>> byTicket = new TreeMap<>();
54     public static List<Approval> list = new LinkedList<>();
55     private final static CacheChange<Approval> cache = new CacheChange<>(); 
56     
57     public final ApprovalDAO.Data add;
58     private String role;
59     
60     public Approval(UUID id, UUID ticket, String approver, Date last_notified, 
61             String user, String memo, String operation, String status, String type, long updated) {
62         add = new ApprovalDAO.Data();
63         add.id = id;
64         add.ticket = ticket;
65         add.approver = approver;
66         add.last_notified = last_notified;
67         add.user = user;
68         add.memo = memo;
69         add.operation = operation;
70         add.status = status;
71         add.type = type;
72         add.updated = new Date(updated);
73         role = roleFromMemo(memo);
74     }
75     
76     public static String roleFromMemo(String memo) {
77         if (memo==null) {
78             return null;
79         }
80         int first = memo.indexOf('\'');
81         if (first>=0) {
82             int second = memo.indexOf('\'', ++first);
83             if (second>=0) {
84                 String role = memo.substring(first, second);
85                 if (memo.startsWith(RE_VALIDATE_ADMIN)) {
86                     return role + ".admin";
87                 } else if (memo.startsWith(RE_VALIDATE_OWNER)) {
88                     return role + ".owner";
89                 } else if (memo.startsWith(RE_APPROVAL_IN_ROLE)) {
90                     return role;
91                 }
92             }
93         }
94         return null;
95     }
96
97     public static void load(Trans trans, Session session, Creator<Approval> creator, Visitor<Approval> visitor) {
98         trans.info().log( "query: " + creator.select() );
99         TimeTaken tt = trans.start("Read Approval", Env.REMOTE);
100        
101         ResultSet results;
102         try {
103             Statement stmt = new SimpleStatement( creator.select() );
104             results = session.execute(stmt);
105         } finally {
106             tt.done();
107         }
108
109         int count = 0;
110         try {
111             Iterator<Row> iter = results.iterator();
112             Row row;
113             tt = trans.start("Load X509s", Env.SUB);
114             try {
115                 while (iter.hasNext()) {
116                         ++count;
117                     row = iter.next();
118                     visitor.visit(creator.create(row));
119                 }
120             } finally {
121                 tt.done();
122             }
123         } finally {
124             trans.info().log("Found",count,"X509 Certificates");
125         }
126     }
127     
128         public static void row(CSV.Writer cw, Approval app) {
129                 cw.row("approval",app.add.id,app.add.ticket,app.add.user,app.role,app.add.memo);
130         }
131
132
133     public static void load(Trans trans, Session session, Creator<Approval> creator ) {
134         trans.info().log( "query: " + creator.select() );
135         TimeTaken tt = trans.start("Load Notify", Env.REMOTE);
136        
137         ResultSet results;
138         try {
139             Statement stmt = new SimpleStatement(creator.select());
140             results = session.execute(stmt);
141         } finally {
142             tt.done();
143         }
144         int count = 0;
145         tt = trans.start("Process Notify", Env.SUB);
146
147         try {
148                 List<Approval> ln;
149                 for (Row row : results.all()) {
150                     ++count;
151                     try {
152                             Approval app = creator.create(row);
153                             list.add(app);
154                             
155                             String person = app.getApprover();
156                             if (person!=null) {
157                             ln = byApprover.get(person);
158                                 if (ln==null) {
159                                     ln = new ArrayList<>();
160                                     byApprover.put(app.getApprover(), ln);
161                                 }
162                                 ln.add(app);
163                             }
164                             
165                             
166                         person = app.getUser();
167                             if (person!=null) {
168                                 ln = byUser.get(person);
169                                 if (ln==null) {
170                                     ln = new ArrayList<>();
171                                     byUser.put(app.getUser(), ln);
172                                 }
173                                 ln.add(app);
174                             }
175                             UUID ticket = app.getTicket();
176                             if (ticket!=null) {
177                                 ln = byTicket.get(ticket);
178                                 if (ln==null) {
179                                     ln = new ArrayList<>();
180                                     byTicket.put(app.getTicket(), ln);
181                                 }
182                             ln.add(app);
183                             }
184                     } finally {
185                         tt.done();
186                     }
187                 }
188         } finally {
189             tt.done();
190             trans.info().log("Found",count,"Approval Records");
191         }
192     }
193     
194     @Override
195     public void expunge() {
196         List<Approval> la = byApprover.get(getApprover());
197         if (la!=null) {
198             la.remove(this);
199         }
200         
201         la = byUser.get(getUser());
202         if (la!=null) {
203             la.remove(this);
204         }
205         UUID ticket = this.add==null?null:this.add.ticket;
206         if (ticket!=null) {
207             la = byTicket.get(this.add.ticket);
208             if (la!=null) {
209                 la.remove(this);
210             }
211         }
212     }
213
214     public void update(AuthzTrans trans, ApprovalDAO apprDAO, boolean dryRun) {
215         if (dryRun) {
216             trans.info().printf("Would update Approval %s, %s, last_notified %s",add.id,add.status,add.last_notified);
217         } else {
218             trans.info().printf("Update Approval %s, %s, last_notified %s",add.id,add.status,add.last_notified);
219             apprDAO.update(trans, add);
220         }
221     }
222
223     public static Creator<Approval> v2_0_17 = new Creator<Approval>() {
224         @Override
225         public Approval create(Row row) {
226             return new Approval(row.getUUID(0), row.getUUID(1), row.getString(2), row.getTimestamp(3),
227                     row.getString(4),row.getString(5),row.getString(6),row.getString(7),row.getString(8)
228                     ,row.getLong(9)/1000);
229         }
230
231         @Override
232         public String select() {
233             return "select id,ticket,approver,last_notified,user,memo,operation,status,type,WRITETIME(status) from authz.approval";
234         }
235     };
236
237     /**
238      * @return the lastNotified
239      */
240     public Date getLast_notified() {
241         return add.last_notified;
242     }
243     /**
244      * @param lastNotified the lastNotified to set
245      */
246     public void setLastNotified(Date last_notified) {
247         add.last_notified = last_notified;
248     }
249     /**
250      * @return the status
251      */
252     public String getStatus() {
253         return add.status;
254     }
255     /**
256      * @param status the status to set
257      */
258     public void setStatus(String status) {
259         add.status = status;
260     }
261     /**
262      * @return the id
263      */
264     public UUID getId() {
265         return add.id;
266     }
267     /**
268      * @return the ticket
269      */
270     public UUID getTicket() {
271         return add.ticket;
272     }
273     /**
274      * @return the approver
275      */
276     public String getApprover() {
277         return add.approver;
278     }
279     /**
280      * @return the user
281      */
282     public String getUser() {
283         return add.user;
284     }
285     /**
286      * @return the memo
287      */
288     public String getMemo() {
289         return add.memo;
290     }
291     /**
292      * @return the operation
293      */
294     public String getOperation() {
295         return add.operation;
296     }
297     /**
298      * @return the type
299      */
300     public String getType() {
301         return add.type;
302     }
303     public void lapsed() {
304         add.ticket=null;
305         add.status="lapsed";
306     }
307     
308     public String getRole() {
309         return role;
310     }
311     
312     public String toString() {
313         return getUser() + ' ' + getMemo();
314     }
315
316     public void delayDelete(AuthzTrans trans, ApprovalDAO ad, boolean dryRun, String text) {
317         if (dryRun) {
318             trans.info().log(text,"- Would Delete: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
319         } else {
320             Result<Void> rv = ad.delete(trans, add, false);
321             if (rv.isOK()) {
322                 trans.info().log(text,"- Deleted: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
323                 cache.delayedDelete(this);
324             } else {
325                 trans.info().log(text,"- Failed to Delete Approval",getId());
326             }
327         }
328     }
329     
330
331     public static void resetLocalData() {
332         cache.resetLocalData();
333     }
334     
335     public static int sizeForDeletion() {
336         return cache.cacheSize();
337     }
338
339     public static void delayDelete(AuthzTrans noAvg, ApprovalDAO apprDAO, boolean dryRun, List<Approval> list, String text) {
340         if (list!=null) {
341             for (Approval a : list) {
342                 a.delayDelete(noAvg, apprDAO, dryRun,text);
343             }
344         }
345     }
346
347     public static boolean pendingDelete(Approval a) {
348         return cache.contains(a);
349     }
350
351         public static void deleteByIDBatch(StringBuilder sb, String id) {
352                 sb.append("DELETE from authz.approval where id=");
353                 sb.append(id);
354                 sb.append(";\n");
355         }
356
357 }