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.LinkedList;
28 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.Env;
37 import org.onap.aaf.misc.env.TimeTaken;
38 import org.onap.aaf.misc.env.Trans;
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;
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 '";
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";
89 } else if (memo.startsWith(RE_APPROVAL_IN_ROLE)) {
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);
103 Statement stmt = new SimpleStatement( creator.select() );
104 results = session.execute(stmt);
111 Iterator<Row> iter = results.iterator();
113 tt = trans.start("Load X509s", Env.SUB);
115 while (iter.hasNext()) {
118 visitor.visit(creator.create(row));
124 trans.info().log("Found",count,"X509 Certificates");
128 public static void row(CSV.Writer cw, Approval app) {
129 cw.row("approval",app.add.id,app.add.ticket,app.add.user,app.role,app.add.memo);
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);
139 Statement stmt = new SimpleStatement(creator.select());
140 results = session.execute(stmt);
145 tt = trans.start("Process Notify", Env.SUB);
149 for (Row row : results.all()) {
152 Approval app = creator.create(row);
155 String person = app.getApprover();
157 ln = byApprover.get(person);
159 ln = new ArrayList<>();
160 byApprover.put(app.getApprover(), ln);
166 person = app.getUser();
168 ln = byUser.get(person);
170 ln = new ArrayList<>();
171 byUser.put(app.getUser(), ln);
175 UUID ticket = app.getTicket();
177 ln = byTicket.get(ticket);
179 ln = new ArrayList<>();
180 byTicket.put(app.getTicket(), ln);
190 trans.info().log("Found",count,"Approval Records");
195 public void expunge() {
196 List<Approval> la = byApprover.get(getApprover());
201 la = byUser.get(getUser());
205 UUID ticket = this.add==null?null:this.add.ticket;
207 la = byTicket.get(this.add.ticket);
214 public void update(AuthzTrans trans, ApprovalDAO apprDAO, boolean dryRun) {
216 trans.info().printf("Would update Approval %s, %s, last_notified %s",add.id,add.status,add.last_notified);
218 trans.info().printf("Update Approval %s, %s, last_notified %s",add.id,add.status,add.last_notified);
219 apprDAO.update(trans, add);
223 public static Creator<Approval> v2_0_17 = new Creator<Approval>() {
225 public Approval create(Row row) {
226 return new Approval(row.getUUID(0), row.getUUID(1), row.getString(2), row.getTimestamp(3),
227 row.getString(4),row.getString(5),row.getString(6),row.getString(7),row.getString(8)
228 ,row.getLong(9)/1000);
232 public String select() {
233 return "select id,ticket,approver,last_notified,user,memo,operation,status,type,WRITETIME(status) from authz.approval";
238 * @return the lastNotified
240 public Date getLast_notified() {
241 return add.last_notified;
244 * @param lastNotified the lastNotified to set
246 public void setLastNotified(Date last_notified) {
247 add.last_notified = last_notified;
252 public String getStatus() {
256 * @param status the status to set
258 public void setStatus(String status) {
264 public UUID getId() {
270 public UUID getTicket() {
274 * @return the approver
276 public String getApprover() {
282 public String getUser() {
288 public String getMemo() {
292 * @return the operation
294 public String getOperation() {
295 return add.operation;
300 public String getType() {
303 public void lapsed() {
308 public String getRole() {
312 public String toString() {
313 return getUser() + ' ' + getMemo();
316 public void delayDelete(AuthzTrans trans, ApprovalDAO ad, boolean dryRun, String text) {
318 trans.info().log(text,"- Would Delete: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
320 Result<Void> rv = ad.delete(trans, add, false);
322 trans.info().log(text,"- Deleted: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
323 cache.delayedDelete(this);
325 trans.info().log(text,"- Failed to Delete Approval",getId());
331 public static void resetLocalData() {
332 cache.resetLocalData();
335 public static int sizeForDeletion() {
336 return cache.cacheSize();
339 public static void delayDelete(AuthzTrans noAvg, ApprovalDAO apprDAO, boolean dryRun, List<Approval> list, String text) {
341 for (Approval a : list) {
342 a.delayDelete(noAvg, apprDAO, dryRun,text);
347 public static boolean pendingDelete(Approval a) {
348 return cache.contains(a);
351 public static void deleteByIDBatch(StringBuilder sb, String id) {
352 sb.append("DELETE from authz.approval where id=");