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