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.LinkedList;
27 import java.util.List;
29 import java.util.TreeMap;
30 import java.util.UUID;
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.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 ADD_USER_TO_ROLE = "Add User [";
47 public static final String RE_APPROVAL_IN_ROLE = "Extend access of User [";
48 public static final String RE_VALIDATE_ADMIN = "Revalidate as Admin of AAF Namespace [";
49 public static final String RE_VALIDATE_OWNER = "Revalidate as Owner of AAF Namespace [";
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<>();
57 public final ApprovalDAO.Data add;
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();
65 add.approver = approver;
66 // add.last_notified = last_notified;
69 add.operation = operation;
72 add.updated = new Date(updated);
73 role = roleFromMemo(memo);
76 public static String roleFromMemo(String memo) {
80 int first = memo.indexOf('[');
82 int second = memo.indexOf(']', ++first);
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";
90 first = memo.indexOf('[',second);
92 second = memo.indexOf(']', ++first);
94 if(memo.startsWith(RE_APPROVAL_IN_ROLE) ||
95 memo.startsWith(ADD_USER_TO_ROLE)) {
96 return memo.substring(first, second);
106 public static int load(Trans trans, Session session, Creator<Approval> creator, Visitor<Approval> visitor) {
109 count+=call(trans,session,creator.query(null), creator, visitor);
111 trans.info().log("Found",count,"Approval Records");
116 public static int load(Trans trans, Session session, Creator<Approval> creator ) {
119 count+=call(trans,session,creator.query(null), creator, FullLoad);
121 trans.info().log("Found",count,"Approval Records");
126 public static int loadUsers(Trans trans, Session session, Set<String> users, Visitor<Approval> visitor) {
128 for(String user : users) {
129 total+=call(trans,session,String.format("%s WHERE user='%s';",v2_0_17.select(), user),v2_0_17,visitor);
134 public static void row(CSV.RowSetter crs, Approval app) {
135 crs.row("approval",app.add.id,app.add.ticket,app.add.user,app.role,app.add.memo);
138 private static int call(Trans trans, Session session, String query, Creator<Approval> creator, Visitor<Approval> visitor) {
139 TimeTaken tt = trans.start("DB Query", Trans.REMOTE);
142 Statement stmt = new SimpleStatement( query );
143 results = session.execute(stmt);
145 for (Row row : results.all()) {
147 visitor.visit(creator.create(row));
156 public void expunge() {
157 List<Approval> la = byApprover.get(getApprover());
162 la = byUser.get(getUser());
166 UUID ticket = this.add==null?null:this.add.ticket;
168 la = byTicket.get(this.add.ticket);
175 public static void clear() {
180 cache.resetLocalData();
183 public static Creator<Approval> v2_0_17 = new Creator<Approval>() {
185 public Approval create(Row row) {
186 return new Approval(row.getUUID(0), row.getUUID(1), row.getString(2),
187 row.getString(3),row.getString(4),row.getString(5),row.getString(6),row.getString(7),
188 row.getLong(8)/1000);
192 public String select() {
193 return "select id,ticket,approver,user,memo,operation,status,type,WRITETIME(status) from authz.approval";
197 public static Visitor<Approval> FullLoad = new Visitor<Approval>() {
199 public void visit(Approval app) {
203 String person = app.getApprover();
205 ln = byApprover.get(person);
207 ln = new ArrayList<>();
208 byApprover.put(app.getApprover(), ln);
213 person = app.getUser();
215 ln = byUser.get(person);
217 ln = new ArrayList<>();
218 byUser.put(app.getUser(), ln);
222 UUID ticket = app.getTicket();
224 ln = byTicket.get(ticket);
226 ln = new ArrayList<>();
227 byTicket.put(app.getTicket(), ln);
237 public String getStatus() {
241 * @param status the status to set
243 public void setStatus(String status) {
249 public UUID getId() {
255 public UUID getTicket() {
259 * @return the approver
261 public String getApprover() {
267 public String getUser() {
273 public String getMemo() {
277 * @return the operation
279 public String getOperation() {
280 return add.operation;
285 public String getType() {
288 public void lapsed() {
293 public String getRole() {
297 public String toString() {
298 return getUser() + ' ' + getMemo();
301 public void delayDelete(AuthzTrans trans, ApprovalDAO ad, boolean dryRun, String text) {
303 trans.info().log(text,"- Would Delete: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
305 Result<Void> rv = ad.delete(trans, add, false);
307 trans.info().log(text,"- Deleted: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
308 cache.delayedDelete(this);
310 trans.info().log(text,"- Failed to Delete Approval",getId());
316 public static void resetLocalData() {
317 cache.resetLocalData();
320 public static int sizeForDeletion() {
321 return cache.cacheSize();
324 public static void delayDelete(AuthzTrans noAvg, ApprovalDAO apprDAO, boolean dryRun, List<Approval> list, String text) {
326 for (Approval a : list) {
327 a.delayDelete(noAvg, apprDAO, dryRun,text);
332 public static boolean pendingDelete(Approval a) {
333 return cache.contains(a);
336 public static void deleteByIDBatch(StringBuilder sb, String id) {
337 sb.append("DELETE from authz.approval where id=");