Create Helm based Certificates for Clients
[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.RowSetter crs, Approval app) {
129                 crs.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 static void clear() {
215         byApprover.clear();
216         byUser.clear();
217         byTicket.clear();
218         list.clear();
219         cache.resetLocalData();
220     }
221 //    public void update(AuthzTrans trans, ApprovalDAO apprDAO, boolean dryRun) {
222 //        if (dryRun) {
223 //            trans.info().printf("Would update Approval %s, %s, last_notified %s",add.id,add.status,add.last_notified);
224 //        } else {
225 //            trans.info().printf("Update Approval %s, %s, last_notified %s",add.id,add.status,add.last_notified);
226 //            apprDAO.update(trans, add);
227 //        }
228 //    }
229
230     public static Creator<Approval> v2_0_17 = new Creator<Approval>() {
231         @Override
232         public Approval create(Row row) {
233             return new Approval(row.getUUID(0), row.getUUID(1), row.getString(2),
234                     row.getString(3),row.getString(4),row.getString(5),row.getString(6),row.getString(7),
235                     row.getLong(8)/1000);
236         }
237
238         @Override
239         public String select() {
240             return "select id,ticket,approver,user,memo,operation,status,type,WRITETIME(status) from authz.approval";
241         }
242     };
243
244 //    /**
245 //     * @return the lastNotified
246 //     */
247 //    public Date getLast_notified() {
248 //        return add.last_notified;
249 //    }
250 //    /**
251 //     * @param lastNotified the lastNotified to set
252 //     */
253 //    public void setLastNotified(Date last_notified) {
254 //        add.last_notified = last_notified;
255 //    }
256     /**
257      * @return the status
258      */
259     public String getStatus() {
260         return add.status;
261     }
262     /**
263      * @param status the status to set
264      */
265     public void setStatus(String status) {
266         add.status = status;
267     }
268     /**
269      * @return the id
270      */
271     public UUID getId() {
272         return add.id;
273     }
274     /**
275      * @return the ticket
276      */
277     public UUID getTicket() {
278         return add.ticket;
279     }
280     /**
281      * @return the approver
282      */
283     public String getApprover() {
284         return add.approver;
285     }
286     /**
287      * @return the user
288      */
289     public String getUser() {
290         return add.user;
291     }
292     /**
293      * @return the memo
294      */
295     public String getMemo() {
296         return add.memo;
297     }
298     /**
299      * @return the operation
300      */
301     public String getOperation() {
302         return add.operation;
303     }
304     /**
305      * @return the type
306      */
307     public String getType() {
308         return add.type;
309     }
310     public void lapsed() {
311         add.ticket=null;
312         add.status="lapsed";
313     }
314     
315     public String getRole() {
316         return role;
317     }
318     
319     public String toString() {
320         return getUser() + ' ' + getMemo();
321     }
322
323     public void delayDelete(AuthzTrans trans, ApprovalDAO ad, boolean dryRun, String text) {
324         if (dryRun) {
325             trans.info().log(text,"- Would Delete: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
326         } else {
327             Result<Void> rv = ad.delete(trans, add, false);
328             if (rv.isOK()) {
329                 trans.info().log(text,"- Deleted: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
330                 cache.delayedDelete(this);
331             } else {
332                 trans.info().log(text,"- Failed to Delete Approval",getId());
333             }
334         }
335     }
336     
337
338     public static void resetLocalData() {
339         cache.resetLocalData();
340     }
341     
342     public static int sizeForDeletion() {
343         return cache.cacheSize();
344     }
345
346     public static void delayDelete(AuthzTrans noAvg, ApprovalDAO apprDAO, boolean dryRun, List<Approval> list, String text) {
347         if (list!=null) {
348             for (Approval a : list) {
349                 a.delayDelete(noAvg, apprDAO, dryRun,text);
350             }
351         }
352     }
353
354     public static boolean pendingDelete(Approval a) {
355         return cache.contains(a);
356     }
357
358         public static void deleteByIDBatch(StringBuilder sb, String id) {
359                 sb.append("DELETE from authz.approval where id=");
360                 sb.append(id);
361                 sb.append(";\n");
362         }
363
364 }