Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / update / ExpiringP2.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.update;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.text.ParseException;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Map.Entry;
32
33 import org.onap.aaf.auth.Batch;
34 import org.onap.aaf.auth.BatchPrincipal;
35 import org.onap.aaf.auth.actions.Action;
36 import org.onap.aaf.auth.actions.ActionDAO;
37 import org.onap.aaf.auth.actions.CacheTouch;
38 import org.onap.aaf.auth.actions.URDelete;
39 import org.onap.aaf.auth.env.AuthzTrans;
40 import org.onap.aaf.auth.helpers.UserRole;
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;
47
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;
53
54     public ExpiringP2(AuthzTrans trans) throws APIException, IOException, OrganizationException {
55         super(trans.env());
56         trans.info().log("Starting Connection Process");
57         
58         noAvg = env.newTransNoAvg();
59         noAvg.setUser(new BatchPrincipal("batch:ExpiringP2"));
60
61         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
62         try {
63                 urDelete = new URDelete(trans, cluster,isDryRun());
64                 TimeTaken tt2 = trans.start("Connect to Cluster", Env.REMOTE);
65                 try {
66                     session = urDelete.getSession(trans);
67                 } finally {
68                     tt2.done();
69                 }
70             cacheTouch = new CacheTouch(trans,urDelete);
71             
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!!!");
75             }
76             urDeleteF = new BufferedReader(new FileReader(new File(data_dir,"UserRoleDeletes.dat")));
77             
78         } finally {
79             tt0.done();
80         }
81     }
82
83     @Override
84     protected void run(AuthzTrans trans) {
85         deleteURs(noAvg, urDeleteF, urDelete, cacheTouch);
86     }
87     
88     public static void deleteURs(AuthzTrans trans, BufferedReader urDeleteF, URDelete urDelete, CacheTouch cacheTouch) {
89         String line,prev="";
90         try {
91             UserRole ur;
92             Map<String,Count> tally = new HashMap<>();
93             int count=0;
94             try {
95                 while((line=urDeleteF.readLine())!=null) {
96                     if(line.startsWith("#")) {
97                         Count cnt = tally.get(line);
98                         if(cnt==null) {
99                             tally.put(line, cnt=new Count());
100                         }
101                         cnt.inc();
102                         prev = line;
103                     } else {
104                         String[] l = Split.splitTrim('|', line);
105                         try {
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);
109                             ++count;
110                         } catch (ParseException e) {
111                             trans.error().log(e);
112                         }
113                     }
114                 }
115                 
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());
119                 }
120             } finally {
121                 if(cacheTouch!=null && count>0) {
122                         cacheTouch.exec(trans, "user_roles", "Removing UserRoles");
123                 }
124             }
125         } catch (IOException e) {
126             trans.error().log(e);
127         }
128         
129     }
130     private static class Count {
131         private int cnt=0;
132         
133         public /*synchonized*/ void inc() {
134             ++cnt;
135         }
136         
137         public String toString() {
138             return Integer.toString(cnt);
139         }
140     }
141     
142     @Override
143     protected void _close(AuthzTrans trans) {
144         aspr.info("End " + this.getClass().getSimpleName() + " processing" );
145         for(Action<?,?,?> action : new Action<?,?,?>[] {urDelete,cacheTouch}) {
146                 if(action instanceof ActionDAO) {
147                     ((ActionDAO<?,?,?>)action).close(trans);
148                 }
149         }
150         session.close();
151         try {
152             urDeleteF.close();
153         } catch (IOException e) {
154             trans.error().log(e);
155         }
156     }
157
158 }