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.io.IOException;
27 import java.nio.ByteBuffer;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.List;
32 import java.util.TreeMap;
33 import java.util.UUID;
35 import org.onap.aaf.auth.dao.cass.FutureDAO;
36 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
37 import org.onap.aaf.auth.env.AuthzTrans;
38 import org.onap.aaf.auth.layer.Result;
39 import org.onap.aaf.cadi.util.CSV;
40 import org.onap.aaf.misc.env.Env;
41 import org.onap.aaf.misc.env.TimeTaken;
42 import org.onap.aaf.misc.env.Trans;
43 import org.onap.aaf.misc.env.util.Chrono;
45 import com.datastax.driver.core.ResultSet;
46 import com.datastax.driver.core.Row;
47 import com.datastax.driver.core.Session;
48 import com.datastax.driver.core.SimpleStatement;
49 import com.datastax.driver.core.Statement;
51 public class Future implements CacheChange.Data, Comparable<Future> {
52 public static final Map<UUID,Future> data = new TreeMap<>();
53 public static final Map<String,List<Future>> byRole = new TreeMap<>();
55 public final FutureDAO.Data fdd;
56 public final String role; // derived
57 private static final CacheChange<Future> cache = new CacheChange<>();
59 public static Creator<Future> v2_0_17 = new Creator<Future>() {
61 public Future create(Row row) {
62 return new Future(row.getUUID(0),row.getString(1),row.getString(2),
63 row.getTimestamp(3),row.getTimestamp(4), null);
67 public String select() {
68 return "select id,memo,target,start,expires from authz.future";
72 public static Creator<Future> withConstruct = new Creator<Future>() {
74 public String select() {
75 return "select id,memo,target,start,expires,construct from authz.future";
79 public Future create(Row row) {
80 return new Future(row.getUUID(0),row.getString(1),row.getString(2),
81 row.getTimestamp(3),row.getTimestamp(4), row.getBytes(5));
87 public Future(UUID id, String memo, String target, Date start, Date expires, ByteBuffer construct) {
88 fdd = new FutureDAO.Data();
93 fdd.expires = expires;
94 fdd.construct = construct;
98 UserRoleDAO.Data urdd = new UserRoleDAO.Data();
100 urdd.reconstitute(construct);
101 fdd.target_key = urdd.user + '|' + urdd.role;
102 fdd.target_date=urdd.expires;
104 } catch (IOException e) {
105 e.printStackTrace(System.err);
112 public final UUID id() {
116 public final String memo() {
120 public final String target() {
124 public final Date start() {
128 public final Date expires() {
132 public static void load(Trans trans, Session session, Creator<Future> creator) {
133 load(trans,session,creator, new Visitor<Future>() {
135 public void visit(Future f) {
136 data.put(f.fdd.id,f);
140 List<Future> lf = byRole.get(f.role);
142 lf = new ArrayList<>();
143 byRole.put(f.role,lf);
151 public static void load(Trans trans, Session session, Creator<Future> creator, Visitor<Future> visitor) {
152 trans.info().log( "query: " + creator.select() );
154 TimeTaken tt = trans.start("Load Futures", Env.REMOTE);
156 Statement stmt = new SimpleStatement(creator.select());
157 results = session.execute(stmt);
163 tt = trans.start("Process Futures", Env.SUB);
165 for (Row row : results.all()) {
167 visitor.visit(creator.create(row));
171 trans.info().log("Found",count,"Futures");
175 public Result<Void> delayedDelete(AuthzTrans trans, FutureDAO fd, boolean dryRun, String text) {
178 trans.info().log(text,"- Would Delete: ",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
181 rv = fd.delete(trans, fdd, true); // need to read for undelete
183 trans.info().log(text, "- Deleted:",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
184 cache.delayedDelete(this);
187 trans.info().log(text,"- Failed to Delete Future", fdd.id);
195 * @see org.onap.aaf.auth.helpers.CacheChange.Data#resetLocalData()
198 public void expunge() {
201 List<Future> lf = byRole.get(role);
209 public int compareTo(Future o) {
213 return fdd.id.compareTo(o.fdd.id);
216 public static void resetLocalData() {
217 cache.resetLocalData();
220 public static int sizeForDeletion() {
221 return cache.cacheSize();
224 public static boolean pendingDelete(Future f) {
225 return cache.contains(f);
228 public static void row(CSV.Writer cw, Future f) {
229 cw.row("future",f.fdd.id,f.fdd.target,f.fdd.expires,f.role,f.fdd.memo);
233 public static void deleteByIDBatch(StringBuilder sb, String id) {
234 sb.append("DELETE from authz.future where id=");