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