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