Batch work and client
[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.UserRole;
40 import org.onap.aaf.auth.batch.helpers.X509;
41 import org.onap.aaf.auth.dao.CassAccess;
42 import org.onap.aaf.auth.dao.cass.CertDAO;
43 import org.onap.aaf.auth.dao.cass.CredDAO;
44 import org.onap.aaf.auth.dao.cass.HistoryDAO;
45 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
46 import org.onap.aaf.auth.env.AuthzTrans;
47 import org.onap.aaf.auth.org.OrganizationException;
48 import org.onap.aaf.cadi.CadiException;
49 import org.onap.aaf.cadi.client.Holder;
50 import org.onap.aaf.cadi.util.CSV;
51 import org.onap.aaf.misc.env.APIException;
52 import org.onap.aaf.misc.env.Env;
53 import org.onap.aaf.misc.env.TimeTaken;
54 import org.onap.aaf.misc.env.util.Chrono;
55
56 public class Remove extends Batch {
57         private final AuthzTrans noAvg;
58         private HistoryDAO historyDAO;
59         private CQLBatch cqlBatch;
60
61         public Remove(AuthzTrans trans) throws APIException, IOException, OrganizationException {
62                 super(trans.env());
63                 trans.info().log("Starting Connection Process");
64
65                 noAvg = env.newTransNoAvg();
66                 noAvg.setUser(new BatchPrincipal("Remove"));
67
68                 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
69                 try {
70                         historyDAO = new HistoryDAO(trans, cluster, CassAccess.KEYSPACE);
71                         TimeTaken tt2 = trans.start("Connect to Cluster", Env.REMOTE);
72                         try {
73                                 session = historyDAO.getSession(trans);
74                         } finally {
75                                 tt2.done();
76                         }
77                         cqlBatch = new CQLBatch(noAvg.info(),session); 
78
79
80                 } finally {
81                         tt0.done();
82                 }
83         }
84
85         @Override
86         protected void run(AuthzTrans trans) {
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                         final CQLBatchLoop cbl = new CQLBatchLoop(cqlBatch,50,dryRun);
119                         for(File f : remove) {
120                                 trans.info().log("Processing ",f.getAbsolutePath(),"for Deletions");
121                                 if(f.exists()) {
122                                         CSV removeCSV = new CSV(env.access(),f);
123                                         try {
124                                                 removeCSV.visit( row -> {
125                                                         cbl.preLoop();
126                                                         switch(row.get(0)) {
127                                                                 case "info":
128                                                                         switch(row.get(1)) {
129                                                                                 case "Delete":
130                                                                                         memoFmt.set("%s expired from %s on %s");
131                                                                                         break;
132                                                                                 case "NotInOrgDelete":
133                                                                                         memoFmt.set("Identity %s was removed from %s on %s");
134                                                                                         break;
135                                                                         }
136                                                                         break;
137                                                                 case "ur":
138                                                                         if(!ur.get()) {
139                                                                                 ur.set(true);
140                                                                         }
141                                                                         UserRole.batchDelete(cbl.inc(),row);
142                                                                         hdd.target=UserRoleDAO.TABLE; 
143                                                                         hdd.subject=UserRole.histSubject(row);
144                                                                         hdd.memo=UserRole.histMemo(memoFmt.get(), row);
145                                                                         historyDAO.createBatch(cbl.inc(), hdd);
146                                                                         break;
147                                                                 case "cred":
148                                                                         if(!cred.get()) {
149                                                                                 cred.set(true);
150                                                                         }
151                                                                         Cred.batchDelete(cbl.inc(),row);
152                                                                         hdd.target=CredDAO.TABLE; 
153                                                                         hdd.subject=Cred.histSubject(row);
154                                                                         hdd.memo=Cred.histMemo(memoFmt.get(), orgName,row);
155                                                                         historyDAO.createBatch(cbl.inc(), hdd);
156                                                                         break;
157                                                                 case "x509":
158                                                                         if(!x509.get()) {
159                                                                                 x509.set(true);
160                                                                         }
161                                                                         X509.row(cbl.inc(),row);
162                                                                         hdd.target=CertDAO.TABLE; 
163                                                                         hdd.subject=X509.histSubject(row);
164                                                                         hdd.memo=X509.histMemo(memoFmt.get(),row);
165                                                                         historyDAO.createBatch(cbl.inc(), hdd);
166                                                                         break;
167                                                                 case "future":
168                                                                         // Not cached
169                                                                         Future.deleteByIDBatch(cbl.inc(),row.get(1));
170                                                                         break;
171                                                                 case "approval":
172                                                                         // Not cached
173                                                                         Approval.deleteByIDBatch(cbl.inc(),row.get(1));
174                                                                         break;
175                                                         }
176                                                 });
177                                                 cbl.flush();
178                                         } catch (IOException | CadiException e) {
179                                                 e.printStackTrace();
180                                         }
181                                 } else {
182                                         trans.error().log("File",f.getAbsolutePath(),"does not exist.");
183                                 }
184                         }
185                 } finally {
186                         if(ur.get()) {
187                                 cqlBatch.touch(UserRoleDAO.TABLE, 0, UserRoleDAO.CACHE_SEG, dryRun);
188                         }
189                         if(cred.get()) {
190                                 cqlBatch.touch(CredDAO.TABLE, 0, CredDAO.CACHE_SEG, dryRun);
191                         }
192                         if(x509.get()) {
193                                 cqlBatch.touch(CertDAO.TABLE, 0, CertDAO.CACHE_SEG, dryRun);
194                         }
195                 }
196         }
197
198         @Override
199         protected void _close(AuthzTrans trans) {
200                 session.close();
201         }
202
203 }