Remove Tabs, per Jococo
[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  * ===========================================================================
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 package org.onap.aaf.auth.batch.update;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.GregorianCalendar;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
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.UserRole;
36 import org.onap.aaf.auth.batch.reports.PrepExtend;
37 import org.onap.aaf.auth.dao.CassAccess;
38 import org.onap.aaf.auth.dao.cass.CredDAO;
39 import org.onap.aaf.auth.dao.cass.CredDAO.Data;
40 import org.onap.aaf.auth.env.AuthzTrans;
41 import org.onap.aaf.auth.layer.Result;
42 import org.onap.aaf.auth.org.OrganizationException;
43 import org.onap.aaf.cadi.CadiException;
44 import org.onap.aaf.cadi.client.Holder;
45 import org.onap.aaf.cadi.util.CSV;
46 import org.onap.aaf.misc.env.APIException;
47 import org.onap.aaf.misc.env.Env;
48 import org.onap.aaf.misc.env.TimeTaken;
49 import org.onap.aaf.misc.env.util.Chrono;
50
51 public class Extend extends Batch {
52     private final CQLBatch cqlBatch;
53     private final CredDAO credDAO;
54     private final AuthzTrans noAvg;
55     private List<File> extFiles;
56     private final int extendBy;
57     private int gcType;
58     
59     public Extend(AuthzTrans trans) throws APIException, IOException, OrganizationException {
60         super(trans.env());
61         trans.info().log("Starting Connection Process");
62         
63         noAvg = env.newTransNoAvg();
64         noAvg.setUser(new BatchPrincipal("Extend"));
65
66         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
67         try {
68             TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
69             credDAO = new CredDAO(trans, cluster, CassAccess.KEYSPACE);
70             try {
71                 session = credDAO.getSession(trans);
72             } finally {
73                 tt.done();
74             }
75             cqlBatch = new CQLBatch(noAvg.info(),session); 
76         } finally {
77             tt0.done();
78         }
79
80         gcType = GregorianCalendar.WEEK_OF_YEAR;
81         int weeks = 4;
82         
83         Set<String> cmd = new HashSet<>();
84         for(int i=0; i< args().length;++i) {
85             if("-weeks".equals(args()[i])) {
86                 if(args().length>i+1) {
87                     weeks = Integer.parseInt(args()[++i]);
88                 }
89             } else {
90                 cmd.add(args()[i]);
91             }
92         }
93         
94         if(weeks<1 || weeks > 24) {
95             throw new APIException("Invalid --weeks");
96         }
97         extendBy = weeks;
98
99         // Create Intermediate Output 
100         File logDir = logDir();
101         extFiles = new ArrayList<>();
102         if(cmd.isEmpty()) {
103             extFiles.add(new File(logDir,PrepExtend.PREP_EXTEND+Chrono.dateOnlyStamp()+".csv"));
104         } else {
105             for(String fn : cmd) {
106                 extFiles.add(new File(logDir, fn));
107             }
108         }
109         
110         // Load Cred.  We don't follow Visitor, because we have to gather up everything into Identity Anyway
111         // to find the last one.
112     }
113
114     @Override
115     protected void run(AuthzTrans trans) {
116         final int maxBatch = 50;
117
118         // Setup Date boundaries
119         final Holder<GregorianCalendar> hgc = new Holder<>(new GregorianCalendar());
120         final GregorianCalendar now = new GregorianCalendar();
121
122         ///////////////////////////
123         trans.info().log("Bulk Extend Expiring User-Roles and Creds");
124
125         final Holder<List<String>> info = new Holder<>(null);
126         final Holder<StringBuilder> hsb = new Holder<>(null);
127
128         for(File f : extFiles) {
129             CSV csv = new CSV(env.access(),f);
130             try {
131                 csv.visit(new CSV.Visitor() {
132                     final Holder<Integer> hi = new Holder<>(0); 
133
134                     @Override
135                     public void visit(List<String> row) throws IOException, CadiException {
136                         GregorianCalendar gc;
137                         int i = hi.get();
138                         StringBuilder sb = hsb.get();
139                         if(sb==null) {
140                             hsb.set(sb=cqlBatch.begin());
141                         }
142                         switch(row.get(0)) {
143                             case "info":
144                                 info.set(row);
145                                 break;
146                             case "ur":
147                                 hi.set(++i);
148                                 gc = hgc.get();
149                                 gc.setTime(new Date(Long.parseLong(row.get(6))));
150                                 if(gc.before(now)) {
151                                     gc.setTime(now.getTime());
152                                 }
153                                 gc.add(gcType, extendBy);
154                                 UserRole.batchExtend(sb,row,gc.getTime());
155                                 break;
156                             case "cred":
157                                 int ctype = Integer.parseInt(row.get(3));
158                                 if(ctype == CredDAO.BASIC_AUTH_SHA256 || ctype == CredDAO.BASIC_AUTH) {
159                                     Result<List<Data>> result = credDAO.readID(noAvg, row.get(1));
160                                     if(result.isOKhasData()) {
161                                         for(CredDAO.Data cd : result.value) {
162                                             if(cd.type == CredDAO.BASIC_AUTH_SHA256 || cd.type == CredDAO.BASIC_AUTH) {
163                                                 String prev;
164                                                 if(row.get(4).equals(prev=Chrono.dateOnlyStamp(cd.expires))) {
165                                                     gc = hgc.get();
166                                                     gc.setTime(new Date(Long.parseLong(row.get(5))));
167                                                     if(gc.before(now)) {
168                                                         gc.setTime(now.getTime());
169                                                     }
170                                                     gc.add(gcType, extendBy);
171                                                     cd.expires = gc.getTime();
172                                                     if(dryRun) {
173                                                         noAvg.info().printf("Would extend %s, %d - %s to %s",cd.id,cd.type,prev, Chrono.dateOnlyStamp(cd.expires));
174                                                     } else {
175                                                         Result<Void> r = credDAO.update(noAvg, cd, true);
176                                                         noAvg.info().printf("%s %s, %d - %s to %s",
177                                                                 r.isOK()?"Extended":"Failed to Extend",
178                                                                 cd.id,cd.type,prev, Chrono.dateOnlyStamp(cd.expires));
179                                                     }
180                                                 }
181                                             }
182                                         }
183                                     }
184                                 }
185                                 break;
186                         }
187                         if(i%maxBatch==0 && sb!=null) {
188                             cqlBatch.execute(dryRun);
189                             hi.set(1);
190                             hsb.set(sb=null);
191                         }
192                     }
193                 });
194             } catch (IOException | CadiException e) {
195                 e.printStackTrace();
196             }
197         }
198         
199         // Cleanup, if required.
200         cqlBatch.execute(dryRun);
201
202     }
203     
204     @Override
205     protected void _close(AuthzTrans trans) {
206         trans.info().log("End " + this.getClass().getSimpleName() + " processing" );
207         credDAO.close(trans);
208         session.close();
209     }
210
211 }