2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 package org.onap.aaf.auth.batch.update;
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;
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;
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;
59 public Extend(AuthzTrans trans) throws APIException, IOException, OrganizationException {
61 trans.info().log("Starting Connection Process");
63 noAvg = env.newTransNoAvg();
64 noAvg.setUser(new BatchPrincipal("Extend"));
66 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
68 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
69 credDAO = new CredDAO(trans, cluster, CassAccess.KEYSPACE);
71 session = credDAO.getSession(trans);
75 cqlBatch = new CQLBatch(noAvg.info(),session);
80 gcType = GregorianCalendar.WEEK_OF_YEAR;
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]);
94 if(weeks<1 || weeks > 24) {
95 throw new APIException("Invalid --weeks");
99 // Create Intermediate Output
100 File logDir = logDir();
101 extFiles = new ArrayList<>();
103 extFiles.add(new File(logDir,PrepExtend.PREP_EXTEND+Chrono.dateOnlyStamp()+".csv"));
105 for(String fn : cmd) {
106 extFiles.add(new File(logDir, fn));
110 // Load Cred. We don't follow Visitor, because we have to gather up everything into Identity Anyway
111 // to find the last one.
115 protected void run(AuthzTrans trans) {
116 final int maxBatch = 50;
118 // Setup Date boundaries
119 final Holder<GregorianCalendar> hgc = new Holder<>(new GregorianCalendar());
120 final GregorianCalendar now = new GregorianCalendar();
122 ///////////////////////////
123 trans.info().log("Bulk Extend Expiring User-Roles and Creds");
125 final Holder<List<String>> info = new Holder<>(null);
126 final Holder<StringBuilder> hsb = new Holder<>(null);
128 for(File f : extFiles) {
129 CSV csv = new CSV(env.access(),f);
131 csv.visit(new CSV.Visitor() {
132 final Holder<Integer> hi = new Holder<>(0);
135 public void visit(List<String> row) throws IOException, CadiException {
136 GregorianCalendar gc;
138 StringBuilder sb = hsb.get();
140 hsb.set(sb=cqlBatch.begin());
149 gc.setTime(new Date(Long.parseLong(row.get(6))));
151 gc.setTime(now.getTime());
153 gc.add(gcType, extendBy);
154 UserRole.batchExtend(sb,row,gc.getTime());
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) {
164 if(row.get(4).equals(prev=Chrono.dateOnlyStamp(cd.expires))) {
166 gc.setTime(new Date(Long.parseLong(row.get(5))));
168 gc.setTime(now.getTime());
170 gc.add(gcType, extendBy);
171 cd.expires = gc.getTime();
173 noAvg.info().printf("Would extend %s, %d - %s to %s",cd.id,cd.type,prev, Chrono.dateOnlyStamp(cd.expires));
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));
187 if(i%maxBatch==0 && sb!=null) {
188 cqlBatch.execute(dryRun);
194 } catch (IOException | CadiException e) {
199 // Cleanup, if required.
200 cqlBatch.execute(dryRun);
205 protected void _close(AuthzTrans trans) {
206 trans.info().log("End " + this.getClass().getSimpleName() + " processing" );
207 credDAO.close(trans);