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.List;
27 import java.util.TreeMap;
28 import java.util.UUID;
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;
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;
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 '";
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<>();
53 public final ApprovalDAO.Data add;
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();
61 add.approver = approver;
62 add.last_notified = last_notified;
65 add.operation = operation;
68 add.updated = new Date(updated);
69 role = roleFromMemo(memo);
72 public static String roleFromMemo(String memo) {
76 int first = memo.indexOf('\'');
78 int second = memo.indexOf('\'', ++first);
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)) {
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);
99 Statement stmt = new SimpleStatement(creator.select());
100 results = session.execute(stmt);
105 tt = trans.start("Process Notify", Env.SUB);
109 for (Row row : results.all()) {
112 Approval app = creator.create(row);
113 String person = app.getApprover();
115 ln = byApprover.get(person);
117 ln = new ArrayList<>();
118 byApprover.put(app.getApprover(), ln);
124 person = app.getUser();
126 ln = byUser.get(person);
128 ln = new ArrayList<>();
129 byUser.put(app.getUser(), ln);
133 UUID ticket = app.getTicket();
135 ln = byTicket.get(ticket);
137 ln = new ArrayList<>();
138 byTicket.put(app.getTicket(), ln);
148 trans.info().log("Found",count,"Approval Records");
153 public void expunge() {
154 List<Approval> la = byApprover.get(getApprover());
159 la = byUser.get(getUser());
163 UUID ticket = this.add==null?null:this.add.ticket;
165 la = byTicket.get(this.add.ticket);
172 public void update(AuthzTrans trans, ApprovalDAO apprDAO, boolean dryRun) {
174 trans.info().printf("Would update Approval %s, %s, last_notified %s",add.id,add.status,add.last_notified);
176 trans.info().printf("Update Approval %s, %s, last_notified %s",add.id,add.status,add.last_notified);
177 apprDAO.update(trans, add);
181 public static Creator<Approval> v2_0_17 = new Creator<Approval>() {
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);
190 public String select() {
191 return "select id,ticket,approver,last_notified,user,memo,operation,status,type,WRITETIME(status) from authz.approval";
196 * @return the lastNotified
198 public Date getLast_notified() {
199 return add.last_notified;
202 * @param lastNotified the lastNotified to set
204 public void setLastNotified(Date last_notified) {
205 add.last_notified = last_notified;
210 public String getStatus() {
214 * @param status the status to set
216 public void setStatus(String status) {
222 public UUID getId() {
228 public UUID getTicket() {
232 * @return the approver
234 public String getApprover() {
240 public String getUser() {
246 public String getMemo() {
250 * @return the operation
252 public String getOperation() {
253 return add.operation;
258 public String getType() {
261 public void lapsed() {
266 public String getRole() {
270 public String toString() {
271 return getUser() + ' ' + getMemo();
274 public void delayDelete(AuthzTrans trans, ApprovalDAO ad, boolean dryRun, String text) {
276 trans.info().log(text,"- Would Delete: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
278 Result<Void> rv = ad.delete(trans, add, false);
280 trans.info().log(text,"- Deleted: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
281 cache.delayedDelete(this);
283 trans.info().log(text,"- Failed to Delete Approval",getId());
289 public static void resetLocalData() {
290 cache.resetLocalData();
293 public static int sizeForDeletion() {
294 return cache.cacheSize();
297 public static void delayDelete(AuthzTrans noAvg, ApprovalDAO apprDAO, boolean dryRun, List<Approval> list, String text) {
299 for (Approval a : list) {
300 a.delayDelete(noAvg, apprDAO, dryRun,text);
305 public static boolean pendingDelete(Approval a) {
306 return cache.contains(a);