2 * ============LICENSE_START====================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
24 package org.onap.aaf.auth.batch.helpers;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.LinkedList;
29 import java.util.List;
31 import java.util.TreeMap;
32 import java.util.UUID;
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;
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;
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 [";
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<>();
59 public final ApprovalDAO.Data add;
62 public static Creator<Approval> v2_0_17 = new Creator<Approval>() {
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),
71 public String select() {
72 return "select id,ticket,approver,user,memo,operation,status,type,WRITETIME(status) from authz.approval";
76 public static Visitor<Approval> FullLoad = new Visitor<Approval>() {
78 public void visit(Approval app) {
82 String person = app.getApprover();
84 ln = byApprover.get(person);
86 ln = new ArrayList<>();
87 byApprover.put(app.getApprover(), ln);
92 person = app.getUser();
94 ln = byUser.get(person);
96 ln = new ArrayList<>();
97 byUser.put(app.getUser(), ln);
101 UUID ticket = app.getTicket();
103 ln = byTicket.get(ticket);
105 ln = new ArrayList<>();
106 byTicket.put(app.getTicket(), ln);
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();
118 add.approver = approver;
121 add.operation = operation;
124 add.updated = new Date(updated);
125 role = roleFromMemo(memo);
128 public static String roleFromMemo(String memo) {
132 int first = memo.indexOf('[');
134 int second = memo.indexOf(']', ++first);
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";
142 first = memo.indexOf('[',second);
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);
156 public static int load(Trans trans, Session session, Creator<Approval> creator, Visitor<Approval> visitor) {
159 count+=call(trans,session,creator.query(null), creator, visitor);
161 trans.info().log("Found",count,"Approval Records");
166 public static int load(Trans trans, Session session, Creator<Approval> creator ) {
169 count+=call(trans,session,creator.query(null), creator, FullLoad);
171 trans.info().log("Found",count,"Approval Records");
176 public static int loadUsers(Trans trans, Session session, Set<String> users, Visitor<Approval> visitor) {
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);
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);
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);
192 Statement stmt = new SimpleStatement( query );
193 results = session.execute(stmt);
195 for (Row row : results.all()) {
197 visitor.visit(creator.create(row));
206 public void expunge() {
207 List<Approval> la = byApprover.get(getApprover());
212 la = byUser.get(getUser());
216 UUID ticket = this.add==null?null:this.add.ticket;
218 la = byTicket.get(this.add.ticket);
225 public static void clear() {
230 cache.resetLocalData();
236 public String getStatus() {
240 * @param status the status to set
242 public void setStatus(String status) {
248 public UUID getId() {
254 public UUID getTicket() {
258 * @return the approver
260 public String getApprover() {
266 public String getUser() {
272 public String getMemo() {
276 * @return the operation
278 public String getOperation() {
279 return add.operation;
284 public String getType() {
287 public void lapsed() {
292 public String getRole() {
296 public String toString() {
297 return getUser() + ' ' + getMemo();
300 public void delayDelete(AuthzTrans trans, ApprovalDAO ad, boolean dryRun, String text) {
302 trans.info().log(text,"- Would Delete: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
304 Result<Void> rv = ad.delete(trans, add, false);
306 trans.info().log(text,"- Deleted: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
307 cache.delayedDelete(this);
309 trans.info().log(text,"- Failed to Delete Approval",getId());
315 public static void resetLocalData() {
316 cache.resetLocalData();
319 public static int sizeForDeletion() {
320 return cache.cacheSize();
323 public static void delayDelete(AuthzTrans noAvg, ApprovalDAO apprDAO, boolean dryRun, List<Approval> list, String text) {
325 for (Approval a : list) {
326 a.delayDelete(noAvg, apprDAO, dryRun,text);
331 public static boolean pendingDelete(Approval a) {
332 return cache.contains(a);
335 public static void deleteByIDBatch(StringBuilder sb, String id) {
336 sb.append("DELETE from authz.approval where id=");