2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2018 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.nio.ByteBuffer;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.List;
31 import java.util.TreeMap;
32 import java.util.UUID;
34 import org.onap.aaf.auth.dao.cass.FutureDAO;
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.Env;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.env.Trans;
41 import org.onap.aaf.misc.env.util.Chrono;
43 import com.datastax.driver.core.ResultSet;
44 import com.datastax.driver.core.Row;
45 import com.datastax.driver.core.Session;
46 import com.datastax.driver.core.SimpleStatement;
47 import com.datastax.driver.core.Statement;
49 public class Future implements CacheChange.Data, Comparable<Future> {
50 public static final Map<UUID,Future> data = new TreeMap<>();
51 public static final Map<String,List<Future>> byRole = new TreeMap<>();
53 public final FutureDAO.Data fdd;
54 public final String role; // derived
55 private static final CacheChange<Future> cache = new CacheChange<>();
57 public static Creator<Future> v2_0_17 = new Creator<Future>() {
59 public Future create(Row row) {
60 return new Future(row.getUUID(0),row.getString(1),row.getString(2),
61 row.getTimestamp(3),row.getTimestamp(4), null);
65 public String select() {
66 return "select id,memo,target,start,expires from authz.future";
70 public static Creator<Future> withConstruct = new Creator<Future>() {
72 public String select() {
73 return "select id,memo,target,start,expires,construct from authz.future";
77 public Future create(Row row) {
78 return new Future(row.getUUID(0),row.getString(1),row.getString(2),
79 row.getTimestamp(3),row.getTimestamp(4), row.getBytes(5));
85 public Future(UUID id, String memo, String target, Date start, Date expires, ByteBuffer construct) {
86 fdd = new FutureDAO.Data();
91 fdd.expires = expires;
92 fdd.construct = construct;
93 role = Approval.roleFromMemo(memo);
96 public final UUID id() {
100 public final String memo() {
104 public final String target() {
108 public final Date start() {
112 public final Date expires() {
116 public static void load(Trans trans, Session session, Creator<Future> creator) {
117 load(trans,session,creator, new Visitor<Future>() {
119 public void visit(Future f) {
120 data.put(f.fdd.id,f);
124 List<Future> lf = byRole.get(f.role);
126 lf = new ArrayList<>();
127 byRole.put(f.role,lf);
135 public static void load(Trans trans, Session session, Creator<Future> creator, Visitor<Future> visitor) {
136 trans.info().log( "query: " + creator.select() );
138 TimeTaken tt = trans.start("Load Futures", Env.REMOTE);
140 Statement stmt = new SimpleStatement(creator.select());
141 results = session.execute(stmt);
147 tt = trans.start("Process Futures", Env.SUB);
149 for (Row row : results.all()) {
151 visitor.visit(creator.create(row));
155 trans.info().log("Found",count,"Futures");
159 public Result<Void> delayedDelete(AuthzTrans trans, FutureDAO fd, boolean dryRun, String text) {
162 trans.info().log(text,"- Would Delete: ",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
165 rv = fd.delete(trans, fdd, true); // need to read for undelete
167 trans.info().log(text, "- Deleted:",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
168 cache.delayedDelete(this);
171 trans.info().log(text,"- Failed to Delete Future", fdd.id);
179 * @see org.onap.aaf.auth.helpers.CacheChange.Data#resetLocalData()
182 public void expunge() {
185 List<Future> lf = byRole.get(role);
193 public int compareTo(Future o) {
197 return fdd.id.compareTo(o.fdd.id);
200 public static void resetLocalData() {
201 cache.resetLocalData();
204 public static int sizeForDeletion() {
205 return cache.cacheSize();
208 public static boolean pendingDelete(Future f) {
209 return cache.contains(f);
212 public static void row(CSV.Writer cw, Future f) {
213 cw.row("future",f.fdd.id,f.fdd.target,f.fdd.expires,f.role,f.fdd.memo);
217 public static void deleteByIDBatch(StringBuilder sb, String id) {
218 sb.append("DELETE from authz.future where id=");