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