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<UUID,Future>();
48 public static final Map<String,List<Future>> byRole = new TreeMap<String,List<Future>>();
50 public final FutureDAO.Data fdd;
51 public final String role; // derived
52 private final static CacheChange<Future> cache = new CacheChange<Future>();
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);
106 List<Future> lf = byRole.get(f.role);
108 byRole.put(f.role,lf = new ArrayList<Future>());
115 trans.info().log("Found",count,"Futures");
119 public static Creator<Future> v2_0_17 = new Creator<Future>() {
121 public Future create(Row row) {
122 return new Future(row.getUUID(0),row.getString(1),row.getString(2),
123 row.getTimestamp(3),row.getTimestamp(4), null);
127 public String select() {
128 return "select id,memo,target,start,expires from authz.future";
132 public static Creator<Future> withConstruct = new Creator<Future>() {
134 public String select() {
135 return "select id,memo,target,start,expires,construct from authz.future";
139 public Future create(Row row) {
140 return new Future(row.getUUID(0),row.getString(1),row.getString(2),
141 row.getTimestamp(3),row.getTimestamp(4), row.getBytes(5));
146 public Result<Void> delayedDelete(AuthzTrans trans, FutureDAO fd, boolean dryRun, String text) {
149 trans.info().log(text,"- Would Delete: ",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
152 rv = fd.delete(trans, fdd, true); // need to read for undelete
154 trans.info().log(text, "- Deleted:",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
155 cache.delayedDelete(this);
158 trans.info().log(text,"- Failed to Delete Future", fdd.id);
166 * @see org.onap.aaf.auth.helpers.CacheChange.Data#resetLocalData()
169 public void expunge() {
172 List<Future> lf = byRole.get(role);
180 public int compareTo(Future o) {
184 return fdd.id.compareTo(o.fdd.id);
187 public static void resetLocalData() {
188 cache.resetLocalData();
191 public static int sizeForDeletion() {
192 return cache.cacheSize();
195 public static boolean pendingDelete(Future f) {
196 return cache.contains(f);