Batch Test improvements
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / 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  * 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.batch.helpers;
25
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;
31 import java.util.Map;
32 import java.util.TreeMap;
33 import java.util.UUID;
34
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;
44
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;
50
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<>();
54     
55     public final FutureDAO.Data fdd;
56     public final String role; // derived
57     private static final CacheChange<Future> cache = new CacheChange<>();
58
59     public static Creator<Future> v2_0_17 = new Creator<Future>() {
60         @Override
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);
64         }
65
66         @Override
67         public String select() {
68             return "select id,memo,target,start,expires from authz.future";
69         }
70     };
71
72     public static Creator<Future> withConstruct = new Creator<Future>() {
73         @Override
74         public String select() {
75             return "select id,memo,target,start,expires,construct from authz.future";
76         }
77
78         @Override
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));
82         }
83
84     };
85
86
87     public Future(UUID id, String memo, String target, Date start, Date expires, ByteBuffer construct) {
88         fdd = new FutureDAO.Data();
89         fdd.id = id;
90         fdd.memo = memo;
91         fdd.target = target;
92         fdd.start = start;
93         fdd.expires = expires;
94         fdd.construct = construct;
95         String role = null;
96         switch(target) {
97                 case "user_role":
98                         UserRoleDAO.Data urdd = new UserRoleDAO.Data();
99                         try {
100                                 urdd.reconstitute(construct);
101                                 fdd.target_key = urdd.user + '|' + urdd.role;
102                                 fdd.target_date=urdd.expires;
103                                 role=urdd.role;
104                         } catch (IOException e) {
105                                 e.printStackTrace(System.err);
106                         }
107                         break;
108         }
109         this.role = role;
110     }
111     
112     public final UUID id() {
113         return fdd.id;
114     }
115     
116     public final String memo() {
117         return fdd.memo;
118     }
119     
120     public final String target() {
121         return fdd.target;
122     }
123     
124     public final Date start() {
125         return fdd.start;
126     }
127     
128     public final Date expires() {
129         return fdd.expires;
130     }
131     
132     public static void load(Trans trans, Session session, Creator<Future> creator) {
133         load(trans,session,creator, new Visitor<Future>() {
134                         @Override
135                         public void visit(Future f) {
136                             data.put(f.fdd.id,f);
137                             if (f.role==null) {
138                                 return;
139                             }
140                             List<Future> lf = byRole.get(f.role);
141                             if (lf==null) {
142                                 lf = new ArrayList<>();
143                                 byRole.put(f.role,lf);
144                             }
145                             lf.add(f);
146                         }
147                 });
148     }
149
150
151     public static void load(Trans trans, Session session, Creator<Future> creator, Visitor<Future> visitor) {
152         trans.info().log( "query: " + creator.select() );
153         ResultSet results;
154         TimeTaken tt = trans.start("Load Futures", Env.REMOTE);
155         try {
156             Statement stmt = new SimpleStatement(creator.select());
157             results = session.execute(stmt);
158         } finally {
159             tt.done();
160         }
161         
162         int count = 0;
163         tt = trans.start("Process Futures", Env.SUB);
164         try {
165             for (Row row : results.all()) {
166                     ++count;
167                 visitor.visit(creator.create(row));
168             }
169         } finally {
170             tt.done();
171             trans.info().log("Found",count,"Futures");
172         }
173     }
174
175     public Result<Void> delayedDelete(AuthzTrans trans, FutureDAO fd, boolean dryRun, String text) {
176         Result<Void> rv;
177         if (dryRun) {
178             trans.info().log(text,"- Would Delete: ",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
179             rv = Result.ok();
180         } else {
181             rv = fd.delete(trans, fdd, true); // need to read for undelete
182             if (rv.isOK()) {
183                 trans.info().log(text, "- Deleted:",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
184                 cache.delayedDelete(this);
185             } else {
186                 if (rv.status!=6) {
187                     trans.info().log(text,"- Failed to Delete Future", fdd.id);
188                 }
189             }
190         }
191         return rv;
192     }
193     
194     /* (non-Javadoc)
195      * @see org.onap.aaf.auth.helpers.CacheChange.Data#resetLocalData()
196      */
197     @Override
198     public void expunge() {
199         data.remove(fdd.id);
200         if (role!=null) {
201             List<Future> lf = byRole.get(role);
202             if (lf!=null) {
203                 lf.remove(this);
204             }
205         }
206     }
207
208     @Override
209     public int compareTo(Future o) {
210         if (o==null) {
211             return -1;
212         }
213         return fdd.id.compareTo(o.fdd.id);
214     }
215
216     public static void resetLocalData() {
217         cache.resetLocalData();
218     }
219     
220     public static int sizeForDeletion() {
221         return cache.cacheSize();
222     }
223
224     public static boolean pendingDelete(Future f) {
225         return cache.contains(f);
226     }
227     
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);
230         }
231
232
233         public static void deleteByIDBatch(StringBuilder sb, String id) {
234                 sb.append("DELETE from authz.future where id=");
235                 sb.append(id);
236                 sb.append(";\n");
237         }
238
239 }