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;
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;
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;
58 public class Remove extends Batch {
59 private final AuthzTrans noAvg;
60 private HistoryDAO historyDAO;
61 private CQLBatch cqlBatch;
63 public Remove(AuthzTrans trans) throws APIException, IOException, OrganizationException {
65 trans.info().log("Starting Connection Process");
67 noAvg = env.newTransNoAvg();
68 noAvg.setUser(new BatchPrincipal("Remove"));
70 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
72 historyDAO = new HistoryDAO(trans, cluster, CassAccess.KEYSPACE);
73 TimeTaken tt2 = trans.start("Connect to Cluster", Env.REMOTE);
75 session = historyDAO.getSession(trans);
79 cqlBatch = new CQLBatch(noAvg.info(),session);
86 protected void run(AuthzTrans trans) {
88 // Create Intermediate Output
89 File logDir = logDir();
91 List<File> remove = new ArrayList<>();
93 for(int i=0;i<args().length;++i) {
94 remove.add(new File(logDir, args()[i]));
97 final String ending = Chrono.dateOnlyStamp()+".csv";
98 for (File file : logDir.listFiles(f -> {
99 String name = f.getName();
100 return name.endsWith(ending) && (
101 name.startsWith("Delete") || name.startsWith("Clean."));
107 for(File f : remove) {
108 trans.init().log("Processing File:",f.getAbsolutePath());
111 final Holder<Boolean> ur = new Holder<>(false);
112 final Holder<Boolean> cred = new Holder<>(false);
113 final Holder<Boolean> x509 = new Holder<>(false);
114 final Holder<String> memoFmt = new Holder<>("");
115 final HistoryDAO.Data hdd = new HistoryDAO.Data();
116 final String orgName = trans.org().getName();
119 hdd.reconstruct = ByteBuffer.allocate(0);
120 hdd.user = noAvg.user();
121 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
122 hdd.yr_mon = Integer.parseInt(sdf.format(new Date()));
125 final CQLBatchLoop cbl = new CQLBatchLoop(cqlBatch,50,dryRun);
126 for(File f : remove) {
127 trans.info().log("Processing ",f.getAbsolutePath(),"for Deletions");
129 CSV removeCSV = new CSV(env.access(),f);
131 removeCSV.visit( row -> {
136 memoFmt.set("%s expired from %s on %s");
138 case "NotInOrgDelete":
139 memoFmt.set("Identity %s was removed from %s on %s");
147 //TODO If deleted because Role is no longer there, double check...
149 UserRole.batchDelete(cbl.inc(),row);
150 hdd.target=UserRoleDAO.TABLE;
151 hdd.subject=UserRole.histSubject(row);
152 hdd.memo=UserRole.histMemo(memoFmt.get(), row);
153 historyDAO.createBatch(cbl.inc(), hdd);
159 Cred.batchDelete(cbl.inc(),row);
160 hdd.target=CredDAO.TABLE;
161 hdd.subject=Cred.histSubject(row);
162 hdd.memo=Cred.histMemo(memoFmt.get(), orgName,row);
163 historyDAO.createBatch(cbl.inc(), hdd);
169 X509.batchDelete(cbl.inc(),row);
171 hdd.subject=X509.histSubject(row);
172 hdd.memo=X509.histMemo(memoFmt.get(),row);
173 historyDAO.createBatch(cbl.inc(), hdd);
177 Future.deleteByIDBatch(cbl.inc(),row.get(1));
181 Approval.deleteByIDBatch(cbl.inc(),row.get(1));
184 LastNotified.delete(cbl.inc(),row);
189 } catch (IOException | CadiException e) {
193 trans.error().log("File",f.getAbsolutePath(),"does not exist.");
197 TimeTaken tt = trans.start("Touch UR,Cred and Cert Caches",Trans.REMOTE);
200 cqlBatch.touch(UserRoleDAO.TABLE, 0, UserRoleDAO.CACHE_SEG, dryRun);
203 cqlBatch.touch(CredDAO.TABLE, 0, CredDAO.CACHE_SEG, dryRun);
206 cqlBatch.touch(CertDAO.TABLE, 0, CertDAO.CACHE_SEG, dryRun);
215 protected void _close(AuthzTrans trans) {