Sonar Fixes: Auth Batch Helpers
[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     protected static final Map<UUID,Future> data = new TreeMap<>();
53     protected 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 final 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 final 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         if ("user_role".equals(target)) {
97             UserRoleDAO.Data urdd = new UserRoleDAO.Data();
98             try {
99                 urdd.reconstitute(construct);
100                 fdd.target_key = urdd.user + '|' + urdd.role;
101                 fdd.target_date = urdd.expires;
102                 role = urdd.role;
103             } catch (IOException e) {
104                 e.printStackTrace(System.err);
105             }
106         }
107         this.role = role;
108     }
109
110     public final UUID id() {
111         return fdd.id;
112     }
113
114     public final String memo() {
115         return fdd.memo;
116     }
117
118     public final String target() {
119         return fdd.target;
120     }
121
122     public final Date start() {
123         return fdd.start;
124     }
125
126     public final Date expires() {
127         return fdd.expires;
128     }
129
130     public static void load(Trans trans, Session session, Creator<Future> creator) {
131         load(trans,session,creator, f -> {
132             data.put(f.fdd.id,f);
133             if (f.role==null) {
134                 return;
135             }
136             List<Future> lf = byRole.computeIfAbsent(f.role, k -> new ArrayList<>());
137             lf.add(f);
138         });
139     }
140
141
142     public static void load(Trans trans, Session session, Creator<Future> creator, Visitor<Future> visitor) {
143         trans.info().log( "query: " + creator.select() );
144         ResultSet results;
145         TimeTaken tt = trans.start("Load Futures", Env.REMOTE);
146         try {
147             Statement stmt = new SimpleStatement(creator.select());
148             results = session.execute(stmt);
149         } finally {
150             tt.done();
151         }
152
153         int count = 0;
154         tt = trans.start("Process Futures", Env.SUB);
155         try {
156             for (Row row : results.all()) {
157                 ++count;
158                 visitor.visit(creator.create(row));
159             }
160         } finally {
161             tt.done();
162             trans.info().log("Found",count,"Futures");
163         }
164     }
165
166     public Result<Void> delayedDelete(AuthzTrans trans, FutureDAO fd, boolean dryRun, String text) {
167         Result<Void> rv;
168         if (dryRun) {
169             trans.info().log(text,"- Would Delete: ",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
170             rv = Result.ok();
171         } else {
172             rv = fd.delete(trans, fdd, true); // need to read for undelete
173             if (rv.isOK()) {
174                 trans.info().log(text, "- Deleted:",fdd.id,fdd.memo,"expiring on",Chrono.dateOnlyStamp(fdd.expires));
175                 cache.delayedDelete(this);
176             } else {
177                 if (rv.status!=6) {
178                     trans.info().log(text,"- Failed to Delete Future", fdd.id);
179                 }
180             }
181         }
182         return rv;
183     }
184
185     /* (non-Javadoc)
186      * @see org.onap.aaf.auth.helpers.CacheChange.Data#resetLocalData()
187      */
188     @Override
189     public void expunge() {
190         data.remove(fdd.id);
191         if (role!=null) {
192             List<Future> lf = byRole.get(role);
193             if (lf!=null) {
194                 lf.remove(this);
195             }
196         }
197     }
198
199     @Override
200     public int compareTo(Future o) {
201         if (o==null) {
202             return -1;
203         }
204         return fdd.id.compareTo(o.fdd.id);
205     }
206
207     public static void resetLocalData() {
208         cache.resetLocalData();
209     }
210
211     public static int sizeForDeletion() {
212         return cache.cacheSize();
213     }
214
215     public static boolean pendingDelete(Future f) {
216         return cache.contains(f);
217     }
218
219     public static void row(CSV.Writer cw, Future f) {
220         cw.row("future",f.fdd.id,f.fdd.target,f.fdd.expires,f.role,f.fdd.memo);
221     }
222
223
224     public static void deleteByIDBatch(StringBuilder sb, String id) {
225         sb.append("DELETE from authz.future where id=");
226         sb.append(id);
227         sb.append(";\n");
228     }
229
230 }