2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.batch.helpers;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.TreeMap;
29 import java.util.UUID;
31 import org.onap.aaf.auth.dao.cass.ApprovalDAO;
32 import org.onap.aaf.auth.env.AuthzTrans;
33 import org.onap.aaf.auth.layer.Result;
34 import org.onap.aaf.cadi.util.CSV;
35 import org.onap.aaf.misc.env.Env;
36 import org.onap.aaf.misc.env.TimeTaken;
37 import org.onap.aaf.misc.env.Trans;
39 import com.datastax.driver.core.ResultSet;
40 import com.datastax.driver.core.Row;
41 import com.datastax.driver.core.Session;
42 import com.datastax.driver.core.SimpleStatement;
43 import com.datastax.driver.core.Statement;
45 public class Approval implements CacheChange.Data {
46 public static final String RE_APPROVAL_IN_ROLE = "Re-Approval in Role '";
47 public static final String RE_VALIDATE_ADMIN = "Re-Validate as Administrator for AAF Namespace '";
48 public static final String RE_VALIDATE_OWNER = "Re-Validate Ownership for AAF Namespace '";
50 public static TreeMap<String,List<Approval>> byApprover = new TreeMap<>();
51 public static TreeMap<String,List<Approval>> byUser = new TreeMap<>();
52 public static TreeMap<UUID,List<Approval>> byTicket = new TreeMap<>();
53 private final static CacheChange<Approval> cache = new CacheChange<>();
55 public final ApprovalDAO.Data add;
58 public Approval(UUID id, UUID ticket, String approver, Date last_notified,
59 String user, String memo, String operation, String status, String type, long updated) {
60 add = new ApprovalDAO.Data();
63 add.approver = approver;
64 add.last_notified = last_notified;
67 add.operation = operation;
70 add.updated = new Date(updated);
71 role = roleFromMemo(memo);
74 public static String roleFromMemo(String memo) {
78 int first = memo.indexOf('\'');
80 int second = memo.indexOf('\'', ++first);
82 String role = memo.substring(first, second);
83 if (memo.startsWith(RE_VALIDATE_ADMIN)) {
84 return role + ".admin";
85 } else if (memo.startsWith(RE_VALIDATE_OWNER)) {
86 return role + ".owner";
87 } else if (memo.startsWith(RE_APPROVAL_IN_ROLE)) {
95 public static void load(Trans trans, Session session, Creator<Approval> creator, Visitor<Approval> visitor) {
96 trans.info().log( "query: " + creator.select() );
97 TimeTaken tt = trans.start("Read Approval", Env.REMOTE);
101 Statement stmt = new SimpleStatement( creator.select() );
102 results = session.execute(stmt);
109 Iterator<Row> iter = results.iterator();
111 tt = trans.start("Load X509s", Env.SUB);
113 while (iter.hasNext()) {
116 visitor.visit(creator.create(row));
122 trans.info().log("Found",count,"X509 Certificates");
126 public static void row(CSV.Writer cw, Approval app) {
127 cw.row("approval",app.add.id,app.add.ticket,app.add.user,app.role,app.add.memo);
130 public static void load(Trans trans, Session session, Creator<Approval> creator ) {
131 trans.info().log( "query: " + creator.select() );
132 TimeTaken tt = trans.start("Load Notify", Env.REMOTE);
136 Statement stmt = new SimpleStatement(creator.select());
137 results = session.execute(stmt);
142 tt = trans.start("Process Notify", Env.SUB);
146 for (Row row : results.all()) {
149 Approval app = creator.create(row);
150 String person = app.getApprover();
152 ln = byApprover.get(person);
154 ln = new ArrayList<>();
155 byApprover.put(app.getApprover(), ln);
161 person = app.getUser();
163 ln = byUser.get(person);
165 ln = new ArrayList<>();
166 byUser.put(app.getUser(), ln);
170 UUID ticket = app.getTicket();
172 ln = byTicket.get(ticket);
174 ln = new ArrayList<>();
175 byTicket.put(app.getTicket(), ln);
185 trans.info().log("Found",count,"Approval Records");
190 public void expunge() {
191 List<Approval> la = byApprover.get(getApprover());
196 la = byUser.get(getUser());
200 UUID ticket = this.add==null?null:this.add.ticket;
202 la = byTicket.get(this.add.ticket);
209 public void update(AuthzTrans trans, ApprovalDAO apprDAO, boolean dryRun) {
211 trans.info().printf("Would update Approval %s, %s, last_notified %s",add.id,add.status,add.last_notified);
213 trans.info().printf("Update Approval %s, %s, last_notified %s",add.id,add.status,add.last_notified);
214 apprDAO.update(trans, add);
218 public static Creator<Approval> v2_0_17 = new Creator<Approval>() {
220 public Approval create(Row row) {
221 return new Approval(row.getUUID(0), row.getUUID(1), row.getString(2), row.getTimestamp(3),
222 row.getString(4),row.getString(5),row.getString(6),row.getString(7),row.getString(8)
223 ,row.getLong(9)/1000);
227 public String select() {
228 return "select id,ticket,approver,last_notified,user,memo,operation,status,type,WRITETIME(status) from authz.approval";
233 * @return the lastNotified
235 public Date getLast_notified() {
236 return add.last_notified;
239 * @param lastNotified the lastNotified to set
241 public void setLastNotified(Date last_notified) {
242 add.last_notified = last_notified;
247 public String getStatus() {
251 * @param status the status to set
253 public void setStatus(String status) {
259 public UUID getId() {
265 public UUID getTicket() {
269 * @return the approver
271 public String getApprover() {
277 public String getUser() {
283 public String getMemo() {
287 * @return the operation
289 public String getOperation() {
290 return add.operation;
295 public String getType() {
298 public void lapsed() {
303 public String getRole() {
307 public String toString() {
308 return getUser() + ' ' + getMemo();
311 public void delayDelete(AuthzTrans trans, ApprovalDAO ad, boolean dryRun, String text) {
313 trans.info().log(text,"- Would Delete: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
315 Result<Void> rv = ad.delete(trans, add, false);
317 trans.info().log(text,"- Deleted: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
318 cache.delayedDelete(this);
320 trans.info().log(text,"- Failed to Delete Approval",getId());
326 public static void resetLocalData() {
327 cache.resetLocalData();
330 public static int sizeForDeletion() {
331 return cache.cacheSize();
334 public static void delayDelete(AuthzTrans noAvg, ApprovalDAO apprDAO, boolean dryRun, List<Approval> list, String text) {
336 for (Approval a : list) {
337 a.delayDelete(noAvg, apprDAO, dryRun,text);
342 public static boolean pendingDelete(Approval a) {
343 return cache.contains(a);
346 public static void deleteByIDBatch(StringBuilder sb, String id) {
347 sb.append("DELETE from authz.approval where id=");