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