Add a MassMail Batch Program
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / update / Extend.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 IBM.
7  * ===========================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END====================================================
20  *
21  */
22 package org.onap.aaf.auth.batch.update;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.GregorianCalendar;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Set;
32
33 import org.onap.aaf.auth.batch.Batch;
34 import org.onap.aaf.auth.batch.BatchPrincipal;
35 import org.onap.aaf.auth.batch.helpers.CQLBatch;
36 import org.onap.aaf.auth.batch.helpers.UserRole;
37 import org.onap.aaf.auth.batch.reports.PrepExtend;
38 import org.onap.aaf.auth.dao.CassAccess;
39 import org.onap.aaf.auth.dao.cass.CredDAO;
40 import org.onap.aaf.auth.dao.cass.CredDAO.Data;
41 import org.onap.aaf.auth.env.AuthzTrans;
42 import org.onap.aaf.auth.layer.Result;
43 import org.onap.aaf.auth.org.OrganizationException;
44 import org.onap.aaf.cadi.CadiException;
45 import org.onap.aaf.cadi.util.Holder;
46 import org.onap.aaf.cadi.util.CSV;
47 import org.onap.aaf.misc.env.APIException;
48 import org.onap.aaf.misc.env.Env;
49 import org.onap.aaf.misc.env.TimeTaken;
50 import org.onap.aaf.misc.env.util.Chrono;
51
52 public class Extend extends Batch {
53     private final CQLBatch cqlBatch;
54     private final CredDAO credDAO;
55     private final AuthzTrans noAvg;
56     private List<File> extFiles;
57     private final int extendBy;
58     private int gcType;
59
60     public Extend(AuthzTrans trans) throws APIException, IOException, OrganizationException {
61         super(trans.env());
62         trans.info().log("Starting Connection Process");
63
64         noAvg = env.newTransNoAvg();
65         noAvg.setUser(new BatchPrincipal("Extend"));
66
67         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
68         try {
69             TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
70             credDAO = new CredDAO(trans, cluster, CassAccess.KEYSPACE);
71             try {
72                 session = credDAO.getSession(trans);
73             } finally {
74                 tt.done();
75             }
76             cqlBatch = new CQLBatch(noAvg.info(),session);
77         } finally {
78             tt0.done();
79         }
80
81         gcType = GregorianCalendar.WEEK_OF_YEAR;
82         int weeks = 4;
83
84         Set<String> cmd = new HashSet<>();
85         for(int i=0; i< args().length;++i) {
86             if("-weeks".equals(args()[i])) {
87                 if(args().length>i+1) {
88                     weeks = Integer.parseInt(args()[++i]);
89                 }
90             } else {
91                 cmd.add(args()[i]);
92             }
93         }
94
95         if(weeks<1 || weeks > 24) {
96             throw new APIException("Invalid --weeks");
97         }
98         extendBy = weeks;
99
100         // Create Intermediate Output
101         File logDir = logDir();
102         extFiles = new ArrayList<>();
103         if(cmd.isEmpty()) {
104             extFiles.add(new File(logDir,PrepExtend.PREP_EXTEND+Chrono.dateOnlyStamp()+".csv"));
105         } else {
106             for(String fn : cmd) {
107                 extFiles.add(new File(logDir, fn));
108             }
109         }
110
111         // Load Cred.  We don't follow Visitor, because we have to gather up everything into Identity Anyway
112         // to find the last one.
113     }
114
115     @Override
116     protected void run(AuthzTrans trans) {
117         final int maxBatch = 50;
118
119         // Setup Date boundaries
120         final Holder<GregorianCalendar> hgc = new Holder<>(new GregorianCalendar());
121         final GregorianCalendar now = new GregorianCalendar();
122
123         ///////////////////////////
124         trans.info().log("Bulk Extend Expiring User-Roles and Creds");
125
126         final Holder<List<String>> info = new Holder<>(null);
127         final Holder<StringBuilder> hsb = new Holder<>(null);
128
129         for(File f : extFiles) {
130             CSV csv = new CSV(env.access(),f);
131             try {
132                 csv.visit(new CSV.Visitor() {
133                     final Holder<Integer> hi = new Holder<>(0);
134
135                     @Override
136                     public void visit(List<String> row) throws IOException, CadiException {
137                         GregorianCalendar gc;
138                         int i = hi.get();
139                         StringBuilder sb = hsb.get();
140                         if(sb==null) {
141                             sb=cqlBatch.begin();
142                             hsb.set(sb);
143                         }
144                         switch(row.get(0)) {
145                             case "info":
146                                 info.set(row);
147                                 break;
148                             case "ur":
149                                 hi.set(++i);
150                                 gc = hgc.get();
151                                 gc.setTime(new Date(Long.parseLong(row.get(6))));
152                                 if(gc.before(now)) {
153                                     gc.setTime(now.getTime());
154                                 }
155                                 gc.add(gcType, extendBy);
156                                 UserRole.batchExtend(sb,row,gc.getTime());
157                                 break;
158                             case "cred":
159                                 int ctype = Integer.parseInt(row.get(3));
160                                 if(ctype == CredDAO.BASIC_AUTH_SHA256 || ctype == CredDAO.BASIC_AUTH) {
161                                     Result<List<Data>> result = credDAO.readID(noAvg, row.get(1));
162                                     if(result.isOKhasData()) {
163                                         for(CredDAO.Data cd : result.value) {
164                                             if(cd.type == CredDAO.BASIC_AUTH_SHA256 || cd.type == CredDAO.BASIC_AUTH) {
165                                                 String prev;
166                                                 prev=Chrono.dateOnlyStamp(cd.expires);
167                                                 if(row.get(4).equals(prev)){
168                                                     gc = hgc.get();
169                                                     gc.setTime(new Date(Long.parseLong(row.get(5))));
170                                                     if(gc.before(now)) {
171                                                         gc.setTime(now.getTime());
172                                                     }
173                                                     gc.add(gcType, extendBy);
174                                                     cd.expires = gc.getTime();
175                                                     if(dryRun) {
176                                                         noAvg.info().printf("Would extend %s, %d - %s to %s",cd.id,cd.type,prev, Chrono.dateOnlyStamp(cd.expires));
177                                                     } else {
178                                                         Result<Void> r = credDAO.update(noAvg, cd, true);
179                                                         noAvg.info().printf("%s %s, %d - %s to %s",
180                                                                 r.isOK()?"Extended":"Failed to Extend",
181                                                                 cd.id,cd.type,prev, Chrono.dateOnlyStamp(cd.expires));
182                                                     }
183                                                 }
184                                             }
185                                         }
186                                     }
187                                 }
188                                 break;
189                         }
190                         if(i%maxBatch==0 && sb!=null) {
191                             cqlBatch.execute(dryRun);
192                             hi.set(1);
193                             sb=null;
194                             hsb.set(sb);
195                         }
196                     }
197                 });
198             } catch (IOException | CadiException e) {
199                 e.printStackTrace();
200             }
201         }
202
203         // Cleanup, if required.
204         cqlBatch.execute(dryRun);
205
206     }
207
208     @Override
209     protected void _close(AuthzTrans trans) {
210         trans.info().log("End " + this.getClass().getSimpleName() + " processing" );
211         credDAO.close(trans);
212         session.close();
213     }
214
215 }