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.helpers;
24 import java.nio.ByteBuffer;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.List;
29 import java.util.TreeMap;
30 import java.util.UUID;
32 import org.onap.aaf.auth.dao.cass.FutureDAO;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.layer.Result;
35 import org.onap.aaf.misc.env.Env;
36 import org.onap.aaf.misc.env.TimeTaken;
37 import org.onap.aaf.misc.env.Trans;
38 import org.onap.aaf.misc.env.util.Chrono;
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 Future implements CacheChange.Data, Comparable<Future> {
47 public static final Map<UUID,Future> data = new TreeMap<>();
48 public static final Map<String,List<Future>> byRole = new TreeMap<>();
50 public final FutureDAO.Data fdd;
51 public final String role; // derived
52 private static final CacheChange<Future> cache = new CacheChange<>();
55 public final UUID id() {
59 public final String memo() {
63 public final String target() {
67 public final Date start() {
71 public final Date expires() {
76 public Future(UUID id, String memo, String target, Date start, Date expires, ByteBuffer construct) {
77 fdd = new FutureDAO.Data();
82 fdd.expires = expires;
83 fdd.construct = construct;
84 role = Approval.roleFromMemo(memo);
87 public static void load(Trans trans, Session session, Creator<Future> creator) {
88 trans.info().log( "query: " + creator.select() );
90 TimeTaken tt = trans.start("Load Futures", Env.REMOTE);
92 Statement stmt = new SimpleStatement(creator.select());
93 results = session.execute(stmt);
99 tt = trans.start("Process Futures", Env.SUB);
101 for(Row row : results.all()) {
103 Future f = creator.create(row);
104 data.put(f.fdd.id,f);
108 List<Future> lf = byRole.get(f.role);
110 lf = new ArrayList<>();
111 byRole.put(f.role,lf);
118 trans.info().log("Found",count,"Futures");
122 public static Creator<Future> v2_0_17 = new Creator<Future>() {
124 public Future create(Row row) {
125 return new Future(row.getUUID(0),row.getString(1),row.getString(2),
126 row.getTimestamp(3),row.getTimestamp(4), null);
130 public String select() {
131 return "select id,memo,target,start,expires from authz.future";
135 public static Creator<Future> withConstruct = new Creator<Future>() {
137 public String select() {
138 return "select id,memo,target,start,expires,construct from authz.future";
142 public Future create(Row row) {
143 return new Future(row.getUUID(0),row.getString(1),row.getString(2),
144 row.getTimestamp(3),row.getTimestamp(4), row.getBytes(5));
149 public Result<Void> delayedDelete(AuthzTrans trans, FutureDAO fd, boolean dryRun, String text) {
152 trans.info().log(text,"- Would Delete: ",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
155 rv = fd.delete(trans, fdd, true); // need to read for undelete
157 trans.info().log(text, "- Deleted:",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
158 cache.delayedDelete(this);
161 trans.info().log(text,"- Failed to Delete Future", fdd.id);
169 * @see org.onap.aaf.auth.helpers.CacheChange.Data#resetLocalData()
172 public void expunge() {
175 List<Future> lf = byRole.get(role);
183 public int compareTo(Future o) {
187 return fdd.id.compareTo(o.fdd.id);
190 public static void resetLocalData() {
191 cache.resetLocalData();
194 public static int sizeForDeletion() {
195 return cache.cacheSize();
198 public static boolean pendingDelete(Future f) {
199 return cache.contains(f);