AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / helpers / Future.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.helpers;
23
24 import java.nio.ByteBuffer;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.TreeMap;
30 import java.util.UUID;
31
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;
39
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;
45
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>>();
49         
50         public final FutureDAO.Data fdd;
51         public final String role; // derived
52         private final static CacheChange<Future> cache = new CacheChange<Future>(); 
53         
54         
55         public final UUID id() {
56                 return fdd.id;
57         }
58         
59         public final String memo() {
60                 return fdd.memo;
61         }
62         
63         public final String target() {
64                 return fdd.target;
65         }
66         
67         public final Date start() {
68                 return fdd.start;
69         }
70         
71         public final Date expires() {
72                 return fdd.expires;
73         }
74
75         
76         public Future(UUID id, String memo, String target, Date start, Date expires, ByteBuffer construct) {
77                 fdd = new FutureDAO.Data();
78                 fdd.id = id;
79                 fdd.memo = memo;
80                 fdd.target = target;
81                 fdd.start = start;
82                 fdd.expires = expires;
83                 fdd.construct = construct;
84                 role = Approval.roleFromMemo(memo);
85         }
86
87         public static void load(Trans trans, Session session, Creator<Future> creator) {
88                 trans.info().log( "query: " + creator.select() );
89                 ResultSet results;
90                 TimeTaken tt = trans.start("Load Futures", Env.REMOTE);
91                 try {
92                 Statement stmt = new SimpleStatement(creator.select());
93                 results = session.execute(stmt);
94                 } finally {
95                         tt.done();
96                 }
97                 
98                 int count = 0;
99                 tt = trans.start("Process Futures", Env.SUB);
100                 try {
101                 for(Row row : results.all()) {
102                         ++count;
103                         Future f = creator.create(row);
104                         data.put(f.fdd.id,f);
105                         if(f.role!=null) {
106                                 List<Future> lf = byRole.get(f.role);
107                                 if(lf==null) {
108                                         byRole.put(f.role,lf = new ArrayList<Future>());
109                                 }
110                                 lf.add(f);
111                         }
112                 }
113                 } finally {
114                         tt.done();
115                         trans.info().log("Found",count,"Futures");
116                 }
117         }
118         
119         public static Creator<Future> v2_0_17 = new Creator<Future>() {
120                 @Override
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);
124                 }
125
126                 @Override
127                 public String select() {
128                         return "select id,memo,target,start,expires from authz.future";
129                 }
130         };
131
132         public static Creator<Future> withConstruct = new Creator<Future>() {
133                 @Override
134                 public String select() {
135                         return "select id,memo,target,start,expires,construct from authz.future";
136                 }
137                 
138                 @Override
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));
142                 }
143
144         };
145
146         public Result<Void> delayedDelete(AuthzTrans trans, FutureDAO fd, boolean dryRun, String text) {
147                 Result<Void> rv;
148                 if(dryRun) {
149                         trans.info().log(text,"- Would Delete: ",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
150                         rv = Result.ok();
151                 } else {
152                         rv = fd.delete(trans, fdd, true); // need to read for undelete
153                         if(rv.isOK()) {
154                                 trans.info().log(text, "- Deleted:",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
155                                 cache.delayedDelete(this);
156                         } else {
157                                 if(rv.status!=6) {
158                                         trans.info().log(text,"- Failed to Delete Future", fdd.id);
159                                 }
160                         }
161                 }
162                 return rv;
163         }
164         
165         /* (non-Javadoc)
166          * @see org.onap.aaf.auth.helpers.CacheChange.Data#resetLocalData()
167          */
168         @Override
169         public void expunge() {
170                 data.remove(fdd.id);
171                 if(role!=null) {
172                         List<Future> lf = byRole.get(role);
173                         if(lf!=null) {
174                                 lf.remove(this);
175                         }
176                 }
177         }
178
179         @Override
180         public int compareTo(Future o) {
181                 if(o==null) {
182                         return -1;
183                 }
184                 return fdd.id.compareTo(o.fdd.id);
185         }
186
187         public static void resetLocalData() {
188                 cache.resetLocalData();
189         }
190         
191         public static int sizeForDeletion() {
192                 return cache.cacheSize();
193         }
194
195         public static boolean pendingDelete(Future f) {
196                 return cache.contains(f);
197         }
198
199
200 }