Merge "Fix sonar issues in Mapper1_0"
[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<>();
48         public static final Map<String,List<Future>> byRole = new TreeMap<>();
49         
50         public final FutureDAO.Data fdd;
51         public final String role; // derived
52         private static final CacheChange<Future> cache = new CacheChange<>();
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                                 continue;
107                         }
108                         List<Future> lf = byRole.get(f.role);
109                         if(lf==null) {
110                                         lf = new ArrayList<>();
111                                 byRole.put(f.role,lf);
112                         }
113                         lf.add(f);
114
115                 }
116                 } finally {
117                         tt.done();
118                         trans.info().log("Found",count,"Futures");
119                 }
120         }
121         
122         public static Creator<Future> v2_0_17 = new Creator<Future>() {
123                 @Override
124                 public Future create(Row row) {
125                         return new Future(row.getUUID(0),row.getString(1),row.getString(2),
126                                         row.getTimestamp(3),row.getTimestamp(4), null);
127                 }
128
129                 @Override
130                 public String select() {
131                         return "select id,memo,target,start,expires from authz.future";
132                 }
133         };
134
135         public static Creator<Future> withConstruct = new Creator<Future>() {
136                 @Override
137                 public String select() {
138                         return "select id,memo,target,start,expires,construct from authz.future";
139                 }
140                 
141                 @Override
142                 public Future create(Row row) {
143                         return new Future(row.getUUID(0),row.getString(1),row.getString(2),
144                                         row.getTimestamp(3),row.getTimestamp(4), row.getBytes(5));
145                 }
146
147         };
148
149         public Result<Void> delayedDelete(AuthzTrans trans, FutureDAO fd, boolean dryRun, String text) {
150                 Result<Void> rv;
151                 if(dryRun) {
152                         trans.info().log(text,"- Would Delete: ",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
153                         rv = Result.ok();
154                 } else {
155                         rv = fd.delete(trans, fdd, true); // need to read for undelete
156                         if(rv.isOK()) {
157                                 trans.info().log(text, "- Deleted:",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
158                                 cache.delayedDelete(this);
159                         } else {
160                                 if(rv.status!=6) {
161                                         trans.info().log(text,"- Failed to Delete Future", fdd.id);
162                                 }
163                         }
164                 }
165                 return rv;
166         }
167         
168         /* (non-Javadoc)
169          * @see org.onap.aaf.auth.helpers.CacheChange.Data#resetLocalData()
170          */
171         @Override
172         public void expunge() {
173                 data.remove(fdd.id);
174                 if(role!=null) {
175                         List<Future> lf = byRole.get(role);
176                         if(lf!=null) {
177                                 lf.remove(this);
178                         }
179                 }
180         }
181
182         @Override
183         public int compareTo(Future o) {
184                 if(o==null) {
185                         return -1;
186                 }
187                 return fdd.id.compareTo(o.fdd.id);
188         }
189
190         public static void resetLocalData() {
191                 cache.resetLocalData();
192         }
193         
194         public static int sizeForDeletion() {
195                 return cache.cacheSize();
196         }
197
198         public static boolean pendingDelete(Future f) {
199                 return cache.contains(f);
200         }
201
202
203 }