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