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.update;
24 import java.io.BufferedReader;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.text.ParseException;
29 import java.util.HashMap;
31 import java.util.Map.Entry;
33 import org.onap.aaf.auth.batch.Batch;
34 import org.onap.aaf.auth.batch.BatchPrincipal;
35 import org.onap.aaf.auth.batch.actions.Action;
36 import org.onap.aaf.auth.batch.actions.ActionDAO;
37 import org.onap.aaf.auth.batch.actions.CacheTouch;
38 import org.onap.aaf.auth.batch.actions.URDelete;
39 import org.onap.aaf.auth.batch.helpers.UserRole;
40 import org.onap.aaf.auth.env.AuthzTrans;
41 import org.onap.aaf.auth.org.OrganizationException;
42 import org.onap.aaf.cadi.util.Split;
43 import org.onap.aaf.misc.env.APIException;
44 import org.onap.aaf.misc.env.Env;
45 import org.onap.aaf.misc.env.TimeTaken;
46 import org.onap.aaf.misc.env.util.Chrono;
48 public class ExpiringP2 extends Batch {
49 private final URDelete urDelete;
50 private final CacheTouch cacheTouch;
51 private final AuthzTrans noAvg;
52 private final BufferedReader urDeleteF;
54 public ExpiringP2(AuthzTrans trans) throws APIException, IOException, OrganizationException {
56 trans.info().log("Starting Connection Process");
58 noAvg = env.newTransNoAvg();
59 noAvg.setUser(new BatchPrincipal("batch:ExpiringP2"));
61 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
63 urDelete = new URDelete(trans, cluster,isDryRun());
64 TimeTaken tt2 = trans.start("Connect to Cluster", Env.REMOTE);
66 session = urDelete.getSession(trans);
70 cacheTouch = new CacheTouch(trans,urDelete);
72 File data_dir = new File(env.getProperty("aaf_data_dir"));
73 if (!data_dir.exists() || !data_dir.canWrite() || !data_dir.canRead()) {
74 throw new IOException("Cannot read/write to Data Directory "+ data_dir.getCanonicalPath() + ": EXITING!!!");
76 urDeleteF = new BufferedReader(new FileReader(new File(data_dir,"UserRoleDeletes.dat")));
84 protected void run(AuthzTrans trans) {
85 deleteURs(noAvg, urDeleteF, urDelete, cacheTouch);
88 public static void deleteURs(AuthzTrans trans, BufferedReader urDeleteF, URDelete urDelete, CacheTouch cacheTouch) {
92 Map<String,Count> tally = new HashMap<>();
95 while ((line=urDeleteF.readLine())!=null) {
96 if (line.startsWith("#")) {
97 Count cnt = tally.get(line);
99 tally.put(line, cnt=new Count());
104 String[] l = Split.splitTrim('|', line);
106 // Note: following default order from "COPY TO"
107 ur = new UserRole(l[0],l[1],l[3],l[4],Chrono.iso8601Fmt.parse(l[2]));
108 urDelete.exec(trans, ur, prev);
110 } catch (ParseException e) {
111 trans.error().log(e);
116 System.out.println("Tallies of UserRole Deletions");
117 for (Entry<String, Count> es : tally.entrySet()) {
118 System.out.printf(" %6d\t%20s\n", es.getValue().cnt,es.getKey());
121 if (cacheTouch!=null && count>0) {
122 cacheTouch.exec(trans, "user_roles", "Removing UserRoles");
125 } catch (IOException e) {
126 trans.error().log(e);
130 private static class Count {
133 public /*synchonized*/ void inc() {
137 public String toString() {
138 return Integer.toString(cnt);
143 protected void _close(AuthzTrans trans) {
144 trans.info().log("End",this.getClass().getSimpleName(),"processing" );
145 for (Action<?,?,?> action : new Action<?,?,?>[] {urDelete,cacheTouch}) {
146 if (action instanceof ActionDAO) {
147 ((ActionDAO<?,?,?>)action).close(trans);
153 } catch (IOException e) {
154 trans.error().log(e);