2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.batch.helpers;
24 import java.io.PrintStream;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.SortedMap;
30 import java.util.TreeMap;
32 import org.onap.aaf.auth.batch.actions.URDelete;
33 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
34 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.cadi.util.CSV;
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;
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;
48 public class UserRole implements Cloneable, CacheChange.Data {
50 private static final String SEPARATOR = "\",\"";
53 private static final String LOG_FMT = "%s UserRole - %s: %s-%s (%s, %s) expiring %s";
54 private static final String REPLAY_FMT = "%s|%s|%s|%s|%s\n";
55 private static final String DELETE_FMT = "# %s\n"+ REPLAY_FMT;
57 private static final List<UserRole> data = new ArrayList<>();
58 private static final SortedMap<String,List<UserRole>> byUser = new TreeMap<>();
59 private static final SortedMap<String,List<UserRole>> byRole = new TreeMap<>();
60 private static final CacheChange<UserRole> cache = new CacheChange<>();
61 private static PrintStream urDelete = System.out;
62 private static PrintStream urRecover = System.err;
63 private static int totalLoaded;
67 public static final Creator<UserRole> v2_0_11 = new Creator<UserRole>() {
69 public UserRole create(Row row) {
70 return new UserRole(row.getString(0), row.getString(1), row.getString(2),row.getString(3),row.getTimestamp(4));
74 public String select() {
75 return "select user,role,ns,rname,expires from authz.user_role";
79 public UserRole(String user, String ns, String rname, Date expires) {
80 urdd = new UserRoleDAO.Data();
82 urdd.role = ns + '.' + rname;
85 urdd.expires = expires;
88 public UserRole(String user, String role, String ns, String rname, Date expires) {
89 urdd = new UserRoleDAO.Data();
94 urdd.expires = expires;
97 public static List<UserRole> getData() {
101 public static SortedMap<String, List<UserRole>> getByUser() {
105 public static SortedMap<String, List<UserRole>> getByRole() {
109 public static void load(Trans trans, Session session, Creator<UserRole> creator) {
110 load(trans,session,creator,null,new DataLoadVisitor());
113 public static void load(Trans trans, Session session, Creator<UserRole> creator, Visitor<UserRole> visitor ) {
114 load(trans,session,creator,null,visitor);
117 public static void loadOneRole(Trans trans, Session session, Creator<UserRole> creator, String role, Visitor<UserRole> visitor) {
118 load(trans,session,creator,"role='" + role +"' ALLOW FILTERING;",visitor);
121 public static void loadOneUser(Trans trans, Session session, Creator<UserRole> creator, String user, Visitor<UserRole> visitor ) {
122 load(trans,session,creator,"role='"+ user +"';",visitor);
125 private static void load(Trans trans, Session session, Creator<UserRole> creator, String where, Visitor<UserRole> visitor) {
126 String query = creator.query(where);
127 trans.info().log( "query: " + query );
128 TimeTaken tt = trans.start("Read UserRoles", Env.REMOTE);
132 Statement stmt = new SimpleStatement( query );
133 results = session.execute(stmt);
138 tt = trans.start("Load UserRole", Env.SUB);
140 iterateResults(creator, results.iterator(), visitor);
145 trans.info().log("Loaded",totalLoaded,"UserRoles");
149 private static void iterateResults(Creator<UserRole> creator, Iterator<Row> iter, Visitor<UserRole> visit ) {
151 while (iter.hasNext()) {
154 UserRole ur = creator.create(row);
159 public static class DataLoadVisitor implements Visitor<UserRole> {
161 public void visit(UserRole ur) {
164 List<UserRole> lur = byUser.get(ur.urdd.user);
166 lur = new ArrayList<>();
167 byUser.put(ur.urdd.user, lur);
171 lur = byRole.get(ur.urdd.role);
173 lur = new ArrayList<>();
174 byRole.put(ur.urdd.role, lur);
180 public int totalLoaded() {
184 public int deleted() {
189 public void expunge() {
192 List<UserRole> lur = byUser.get(urdd.user);
197 lur = byRole.get(urdd.role);
203 public static void setDeleteStream(PrintStream ds) {
207 public static void setRecoverStream(PrintStream ds) {
211 public static long count(Trans trans, Session session) {
212 String query = "select count(*) from authz.user_role LIMIT 1000000;";
213 trans.info().log( "query: " + query );
214 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
217 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
218 results = session.execute(stmt);
219 return results.one().getLong(0);
225 public UserRoleDAO.Data urdd() {
229 public String user() {
233 public String role() {
241 public String rname() {
245 public Date expires() {
249 public void expires(Date time) {
253 public String toString() {
254 return "\"" + urdd.user + SEPARATOR + urdd.role + SEPARATOR + urdd.ns + SEPARATOR + urdd.rname + SEPARATOR
255 + Chrono.dateOnlyStamp(urdd.expires);
258 public static UserRole get(String u, String r) {
259 List<UserRole> lur = byUser.get(u);
261 for (UserRole ur : lur) {
263 if (ur.urdd.role.equals(r)) {
271 // SAFETY - DO NOT DELETE USER ROLES DIRECTLY FROM BATCH FILES!!!
272 // We write to a file, and validate. If the size is iffy, we email Support
273 public void delayDelete(AuthzTrans trans, String text, boolean dryRun) {
274 String dt = Chrono.dateTime(urdd.expires);
276 trans.info().printf(LOG_FMT,text,"Would Delete",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
278 trans.info().printf(LOG_FMT,text,"Staged Deletion",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
280 urDelete.printf(DELETE_FMT,text,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
281 urRecover.printf(REPLAY_FMT,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
283 cache.delayedDelete(this);
289 * Calls expunge() for all deleteCached entries
291 public static void resetLocalData() {
292 cache.resetLocalData();
295 public static int sizeForDeletion() {
296 return cache.cacheSize();
299 public static boolean pendingDelete(UserRole ur) {
300 return cache.contains(ur);
303 public static void actuateDeletionNow(AuthzTrans trans, URDelete directDel) {
304 for (UserRole ur : cache.getRemoved()) {
305 directDel.exec(trans, ur, "Actuating UserRole Deletion");
307 cache.getRemoved().clear();
308 cache.resetLocalData();
311 public void row(final CSV.Writer csvw) {
312 csvw.row("ur",user(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime());
315 public static void batchDelete(StringBuilder sb, List<String> row) {
316 sb.append("DELETE from authz.user_role WHERE user='");
317 sb.append(row.get(1));
318 sb.append("' AND role='");
319 sb.append(row.get(2));
321 sb.append(row.get(3));
325 public static void batchExtend(StringBuilder sb, List<String> row, String newDate ) {
326 sb.append("UPDATE authz.user_role SET expires='");
328 sb.append("' WHERE user='");
329 sb.append(row.get(1));
330 sb.append("' AND role='");
331 sb.append(row.get(2));
333 sb.append(row.get(3));
337 public static String histMemo(String fmt, List<String> row) {
338 return String.format(fmt, row.get(1),row.get(2)+'.'+row.get(3), row.get(4));
341 public static String histSubject(List<String> row) {
342 return row.get(1) + '|' + row.get(2)+'.'+row.get(3);