Approval Batch, prep better JUnit
[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.nio.ByteBuffer;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.TreeMap;
32 import java.util.UUID;
33
34 import org.onap.aaf.auth.dao.cass.FutureDAO;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.layer.Result;
37 import org.onap.aaf.cadi.util.CSV;
38 import org.onap.aaf.misc.env.Env;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.env.Trans;
41 import org.onap.aaf.misc.env.util.Chrono;
42
43 import com.datastax.driver.core.ResultSet;
44 import com.datastax.driver.core.Row;
45 import com.datastax.driver.core.Session;
46 import com.datastax.driver.core.SimpleStatement;
47 import com.datastax.driver.core.Statement;
48
49 public class Future implements CacheChange.Data, Comparable<Future> {
50     public static final Map<UUID,Future> data = new TreeMap<>();
51     public static final Map<String,List<Future>> byRole = new TreeMap<>();
52     
53     public final FutureDAO.Data fdd;
54     public final String role; // derived
55     private static final CacheChange<Future> cache = new CacheChange<>();
56
57     public static Creator<Future> v2_0_17 = new Creator<Future>() {
58         @Override
59         public Future create(Row row) {
60             return new Future(row.getUUID(0),row.getString(1),row.getString(2),
61                     row.getTimestamp(3),row.getTimestamp(4), null);
62         }
63
64         @Override
65         public String select() {
66             return "select id,memo,target,start,expires from authz.future";
67         }
68     };
69
70     public static Creator<Future> withConstruct = new Creator<Future>() {
71         @Override
72         public String select() {
73             return "select id,memo,target,start,expires,construct from authz.future";
74         }
75
76         @Override
77         public Future create(Row row) {
78             return new Future(row.getUUID(0),row.getString(1),row.getString(2),
79                     row.getTimestamp(3),row.getTimestamp(4), row.getBytes(5));
80         }
81
82     };
83
84
85     public Future(UUID id, String memo, String target, Date start, Date expires, ByteBuffer construct) {
86         fdd = new FutureDAO.Data();
87         fdd.id = id;
88         fdd.memo = memo;
89         fdd.target = target;
90         fdd.start = start;
91         fdd.expires = expires;
92         fdd.construct = construct;
93         role = Approval.roleFromMemo(memo);
94     }
95     
96     public final UUID id() {
97         return fdd.id;
98     }
99     
100     public final String memo() {
101         return fdd.memo;
102     }
103     
104     public final String target() {
105         return fdd.target;
106     }
107     
108     public final Date start() {
109         return fdd.start;
110     }
111     
112     public final Date expires() {
113         return fdd.expires;
114     }
115     
116     public static void load(Trans trans, Session session, Creator<Future> creator) {
117         load(trans,session,creator, new Visitor<Future>() {
118                         @Override
119                         public void visit(Future f) {
120                             data.put(f.fdd.id,f);
121                             if (f.role==null) {
122                                 return;
123                             }
124                             List<Future> lf = byRole.get(f.role);
125                             if (lf==null) {
126                                 lf = new ArrayList<>();
127                                 byRole.put(f.role,lf);
128                             }
129                             lf.add(f);
130                         }
131                 });
132     }
133
134
135     public static void load(Trans trans, Session session, Creator<Future> creator, Visitor<Future> visitor) {
136         trans.info().log( "query: " + creator.select() );
137         ResultSet results;
138         TimeTaken tt = trans.start("Load Futures", Env.REMOTE);
139         try {
140             Statement stmt = new SimpleStatement(creator.select());
141             results = session.execute(stmt);
142         } finally {
143             tt.done();
144         }
145         
146         int count = 0;
147         tt = trans.start("Process Futures", Env.SUB);
148         try {
149             for (Row row : results.all()) {
150                     ++count;
151                 visitor.visit(creator.create(row));
152             }
153         } finally {
154             tt.done();
155             trans.info().log("Found",count,"Futures");
156         }
157     }
158
159     public Result<Void> delayedDelete(AuthzTrans trans, FutureDAO fd, boolean dryRun, String text) {
160         Result<Void> rv;
161         if (dryRun) {
162             trans.info().log(text,"- Would Delete: ",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
163             rv = Result.ok();
164         } else {
165             rv = fd.delete(trans, fdd, true); // need to read for undelete
166             if (rv.isOK()) {
167                 trans.info().log(text, "- Deleted:",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
168                 cache.delayedDelete(this);
169             } else {
170                 if (rv.status!=6) {
171                     trans.info().log(text,"- Failed to Delete Future", fdd.id);
172                 }
173             }
174         }
175         return rv;
176     }
177     
178     /* (non-Javadoc)
179      * @see org.onap.aaf.auth.helpers.CacheChange.Data#resetLocalData()
180      */
181     @Override
182     public void expunge() {
183         data.remove(fdd.id);
184         if (role!=null) {
185             List<Future> lf = byRole.get(role);
186             if (lf!=null) {
187                 lf.remove(this);
188             }
189         }
190     }
191
192     @Override
193     public int compareTo(Future o) {
194         if (o==null) {
195             return -1;
196         }
197         return fdd.id.compareTo(o.fdd.id);
198     }
199
200     public static void resetLocalData() {
201         cache.resetLocalData();
202     }
203     
204     public static int sizeForDeletion() {
205         return cache.cacheSize();
206     }
207
208     public static boolean pendingDelete(Future f) {
209         return cache.contains(f);
210     }
211     
212         public static void row(CSV.Writer cw, Future f) {
213                 cw.row("future",f.fdd.id,f.fdd.target,f.fdd.expires,f.role,f.fdd.memo);
214         }
215
216
217         public static void deleteByIDBatch(StringBuilder sb, String id) {
218                 sb.append("DELETE from authz.future where id=");
219                 sb.append(id);
220                 sb.append(";\n");
221         }
222
223 }