Merge "Fixed sonar issue in ListByPerm.java"
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / update / Remove.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.batch.update;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.List;
27
28 import org.onap.aaf.auth.batch.Batch;
29 import org.onap.aaf.auth.batch.BatchPrincipal;
30 import org.onap.aaf.auth.batch.actions.CacheTouch;
31 import org.onap.aaf.auth.batch.helpers.CQLBatch;
32 import org.onap.aaf.auth.batch.helpers.Cred;
33 import org.onap.aaf.auth.batch.helpers.UserRole;
34 import org.onap.aaf.auth.batch.helpers.X509;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.org.OrganizationException;
37 import org.onap.aaf.cadi.CadiException;
38 import org.onap.aaf.cadi.client.Holder;
39 import org.onap.aaf.cadi.util.CSV;
40 import org.onap.aaf.misc.env.APIException;
41 import org.onap.aaf.misc.env.Env;
42 import org.onap.aaf.misc.env.TimeTaken;
43 import org.onap.aaf.misc.env.util.Chrono;
44
45 public class Remove extends Batch {
46     private final AuthzTrans noAvg;
47         private CacheTouch cacheTouch;
48         private CQLBatch cqlBatch;
49
50     public Remove(AuthzTrans trans) throws APIException, IOException, OrganizationException {
51         super(trans.env());
52         trans.info().log("Starting Connection Process");
53         
54         noAvg = env.newTransNoAvg();
55         noAvg.setUser(new BatchPrincipal("batch:RemoveExpired"));
56
57         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
58         try {
59                 cacheTouch = new CacheTouch(trans, cluster, dryRun);
60             TimeTaken tt2 = trans.start("Connect to Cluster", Env.REMOTE);
61             try {
62                 session = cacheTouch.getSession(trans);
63             } finally {
64                 tt2.done();
65             }
66             cqlBatch = new CQLBatch(session); 
67             
68
69         } finally {
70             tt0.done();
71         }
72     }
73
74     @Override
75     protected void run(AuthzTrans trans) {
76         final int maxBatch = 50;
77
78         // Create Intermediate Output 
79         File logDir = new File(logDir());
80         
81         File expired = new File(logDir,"Delete"+Chrono.dateOnlyStamp()+".csv");
82         CSV expiredCSV = new CSV(expired);
83         try {
84                 final StringBuilder sb = cqlBatch.begin();
85             final Holder<Integer> hi = new Holder<Integer>(0);
86                         expiredCSV.visit(new CSV.Visitor() {
87                                 @Override
88                                 public void visit(List<String> row) throws IOException, CadiException {
89                                         int i = hi.get();
90                                         if(i>=maxBatch) {
91                                                 cqlBatch.execute(dryRun);
92                                                 hi.set(0);
93                                                 cqlBatch.begin();
94                                                 i=0;
95                                         }
96                                         switch(row.get(0)) {
97                                                 case "ur":
98                                                         hi.set(++i);
99                                                         UserRole.row(sb,row);
100                                                         break;
101                                                 case "cred":
102                                                         hi.set(++i);
103                                                         Cred.row(sb,row);
104                                                 break;
105                                                 case "x509":
106                                                         hi.set(++i);
107                                                         X509.row(sb,row);
108                                                         break;
109                                         }
110                                 }
111                         });
112                         cqlBatch.execute(dryRun);
113                 } catch (IOException | CadiException e) {
114                         e.printStackTrace();
115                 }
116     }
117     
118     @Override
119     protected void _close(AuthzTrans trans) {
120         session.close();
121         cacheTouch.close(trans);
122     }
123
124 }