Approval Batch, prep better JUnit
[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.nio.ByteBuffer;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.List;
31
32 import org.onap.aaf.auth.batch.Batch;
33 import org.onap.aaf.auth.batch.BatchPrincipal;
34 import org.onap.aaf.auth.batch.helpers.Approval;
35 import org.onap.aaf.auth.batch.helpers.CQLBatch;
36 import org.onap.aaf.auth.batch.helpers.Cred;
37 import org.onap.aaf.auth.batch.helpers.Future;
38 import org.onap.aaf.auth.batch.helpers.UserRole;
39 import org.onap.aaf.auth.batch.helpers.X509;
40 import org.onap.aaf.auth.dao.CassAccess;
41 import org.onap.aaf.auth.dao.cass.CertDAO;
42 import org.onap.aaf.auth.dao.cass.CredDAO;
43 import org.onap.aaf.auth.dao.cass.HistoryDAO;
44 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
45 import org.onap.aaf.auth.env.AuthzTrans;
46 import org.onap.aaf.auth.org.OrganizationException;
47 import org.onap.aaf.cadi.CadiException;
48 import org.onap.aaf.cadi.client.Holder;
49 import org.onap.aaf.cadi.util.CSV;
50 import org.onap.aaf.misc.env.APIException;
51 import org.onap.aaf.misc.env.Env;
52 import org.onap.aaf.misc.env.TimeTaken;
53 import org.onap.aaf.misc.env.util.Chrono;
54
55 public class Remove extends Batch {
56     private final AuthzTrans noAvg;
57     private HistoryDAO historyDAO;
58         private CQLBatch cqlBatch;
59
60     public Remove(AuthzTrans trans) throws APIException, IOException, OrganizationException {
61         super(trans.env());
62         trans.info().log("Starting Connection Process");
63         
64         noAvg = env.newTransNoAvg();
65         noAvg.setUser(new BatchPrincipal("Remove"));
66
67         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
68         try {
69                 historyDAO = new HistoryDAO(trans, cluster, CassAccess.KEYSPACE);
70             TimeTaken tt2 = trans.start("Connect to Cluster", Env.REMOTE);
71             try {
72                 session = historyDAO.getSession(trans);
73             } finally {
74                 tt2.done();
75             }
76             cqlBatch = new CQLBatch(noAvg.info(),session); 
77             
78
79         } finally {
80             tt0.done();
81         }
82     }
83
84     @Override
85     protected void run(AuthzTrans trans) {
86         final int maxBatch = 25;
87
88         // Create Intermediate Output 
89         File logDir = logDir();
90         
91         List<File> remove = new ArrayList<>();
92         if(args().length>0) {
93                 for(int i=0;i<args().length;++i) {
94                         remove.add(new File(logDir, args()[i]));
95                 }
96         } else {
97                 remove.add(new File(logDir,"Delete"+Chrono.dateOnlyStamp()+".csv"));
98         }
99         
100         for(File f : remove) {
101                 trans.init().log("Processing File:",f.getAbsolutePath());
102         }
103         
104         final Holder<Boolean> ur = new Holder<>(false);
105         final Holder<Boolean> cred = new Holder<>(false);
106         final Holder<Boolean> x509 = new Holder<>(false);
107         final Holder<String> memoFmt = new Holder<String>("");
108         final HistoryDAO.Data hdd = new HistoryDAO.Data();
109         final String orgName = trans.org().getName();
110         
111         hdd.action="delete";
112         hdd.reconstruct = ByteBuffer.allocate(0);
113         hdd.user = noAvg.user();
114         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
115         hdd.yr_mon = Integer.parseInt(sdf.format(new Date()));
116         
117         try { 
118                 for(File f : remove) {
119                         trans.info().log("Processing ",f.getAbsolutePath(),"for Deletions");
120                         if(f.exists()) {
121                                 CSV removeCSV = new CSV(env.access(),f);
122                                         
123                                 try {
124                                         final StringBuilder sb = cqlBatch.begin();
125                                     final Holder<Integer> hi = new Holder<Integer>(0);
126                                                 removeCSV.visit(new CSV.Visitor() {
127                                                         @Override
128                                                         public void visit(List<String> row) throws IOException, CadiException {
129                                                                 int i = hi.get();
130                                                                 if(i>=maxBatch) {
131                                                                         cqlBatch.execute(dryRun);
132                                                                         hi.set(0);
133                                                                         cqlBatch.begin();
134                                                                         i=0;
135                                                                 }
136                                                                 switch(row.get(0)) {
137                                                                         case "info":
138                                                                                 switch(row.get(1)) {
139                                                                                         case "Delete":
140                                                                                                 memoFmt.set("%s expired from %s on %s");
141                                                                                                 break;
142                                                                                         case "NotInOrgDelete":
143                                                                                                 memoFmt.set("Identity %s was removed from %s on %s");
144                                                                                                 break;
145
146                                                                                 }
147                                                                                 break;
148                                                                         case "ur":
149                                                                                 if(!ur.get()) {
150                                                                                         ur.set(true);
151                                                                                 }
152                                                                                 hi.set(++i);
153                                                                                 UserRole.batchDelete(sb,row);
154                                                                                 hdd.target=UserRoleDAO.TABLE; 
155                                                                                 hdd.subject=UserRole.histSubject(row);
156                                                                                 hdd.memo=UserRole.histMemo(memoFmt.get(), row);
157                                                                                 historyDAO.createBatch(sb, hdd);
158                                                                                 break;
159                                                                         case "cred":
160                                                                                 if(!cred.get()) {
161                                                                                         cred.set(true);
162                                                                                 }
163                                                                                 hi.set(++i);
164                                                                                 Cred.batchDelete(sb,row);
165                                                                                 hdd.target=CredDAO.TABLE; 
166                                                                                 hdd.subject=Cred.histSubject(row);
167                                                                                 hdd.memo=Cred.histMemo(memoFmt.get(), orgName,row);
168                                                                                 historyDAO.createBatch(sb, hdd);
169                                                                         break;
170                                                                         case "x509":
171                                                                                 if(!x509.get()) {
172                                                                                         x509.set(true);
173                                                                                 }
174                                                                                 hi.set(++i);
175                                                                                 X509.row(sb,row);
176                                                                                 hdd.target=CertDAO.TABLE; 
177                                                                                 hdd.subject=X509.histSubject(row);
178                                                                                 hdd.memo=X509.histMemo(memoFmt.get(),row);
179                                                                                 historyDAO.createBatch(sb, hdd);
180                                                                                 break;
181                                                                         case "future":
182                                                                                 // Not cached
183                                                                                 hi.set(++i);
184                                                                                 Future.deleteByIDBatch(sb,row.get(1));
185                                                                                 break;
186                                                                         case "approval":
187                                                                                 // Not cached
188                                                                                 hi.set(++i);
189                                                                                 Approval.deleteByIDBatch(sb,row.get(1));
190                                                                                 break;
191                                                                 }
192                                                         }
193                                                 });
194                                                 cqlBatch.execute(dryRun);
195                                         } catch (IOException | CadiException e) {
196                                                 e.printStackTrace();
197                                         }
198                         } else {
199                                 trans.error().log("File",f.getAbsolutePath(),"does not exist.");
200                         }
201                 }
202         } finally {
203                 if(ur.get()) {
204                         cqlBatch.touch(UserRoleDAO.TABLE, 0, UserRoleDAO.CACHE_SEG, dryRun);
205                 }
206                 if(cred.get()) {
207                         cqlBatch.touch(CredDAO.TABLE, 0, CredDAO.CACHE_SEG, dryRun);
208                 }
209                 if(x509.get()) {
210                         cqlBatch.touch(CertDAO.TABLE, 0, CertDAO.CACHE_SEG, dryRun);
211                 }
212         }
213     }
214     
215     @Override
216     protected void _close(AuthzTrans trans) {
217         session.close();
218     }
219
220 }