2 * ============LICENSE_START====================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.batch.update;
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;
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.client.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;
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;
60 public Extend(AuthzTrans trans) throws APIException, IOException, OrganizationException {
62 trans.info().log("Starting Connection Process");
64 noAvg = env.newTransNoAvg();
65 noAvg.setUser(new BatchPrincipal("Extend"));
67 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
69 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
70 credDAO = new CredDAO(trans, cluster, CassAccess.KEYSPACE);
72 session = credDAO.getSession(trans);
76 cqlBatch = new CQLBatch(noAvg.info(),session);
81 gcType = GregorianCalendar.WEEK_OF_YEAR;
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]);
95 if(weeks<1 || weeks > 24) {
96 throw new APIException("Invalid --weeks");
100 // Create Intermediate Output
101 File logDir = logDir();
102 extFiles = new ArrayList<>();
104 extFiles.add(new File(logDir,PrepExtend.PREP_EXTEND+Chrono.dateOnlyStamp()+".csv"));
106 for(String fn : cmd) {
107 extFiles.add(new File(logDir, fn));
111 // Load Cred. We don't follow Visitor, because we have to gather up everything into Identity Anyway
112 // to find the last one.
116 protected void run(AuthzTrans trans) {
117 final int maxBatch = 50;
119 // Setup Date boundaries
120 final Holder<GregorianCalendar> hgc = new Holder<>(new GregorianCalendar());
121 final GregorianCalendar now = new GregorianCalendar();
123 ///////////////////////////
124 trans.info().log("Bulk Extend Expiring User-Roles and Creds");
126 final Holder<List<String>> info = new Holder<>(null);
127 final Holder<StringBuilder> hsb = new Holder<>(null);
129 for(File f : extFiles) {
130 CSV csv = new CSV(env.access(),f);
132 csv.visit(new CSV.Visitor() {
133 final Holder<Integer> hi = new Holder<>(0);
136 public void visit(List<String> row) throws IOException, CadiException {
137 GregorianCalendar gc;
139 StringBuilder sb = hsb.get();
151 gc.setTime(new Date(Long.parseLong(row.get(6))));
153 gc.setTime(now.getTime());
155 gc.add(gcType, extendBy);
156 UserRole.batchExtend(sb,row,gc.getTime());
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) {
166 prev=Chrono.dateOnlyStamp(cd.expires);
167 if(row.get(4).equals(prev)){
169 gc.setTime(new Date(Long.parseLong(row.get(5))));
171 gc.setTime(now.getTime());
173 gc.add(gcType, extendBy);
174 cd.expires = gc.getTime();
176 noAvg.info().printf("Would extend %s, %d - %s to %s",cd.id,cd.type,prev, Chrono.dateOnlyStamp(cd.expires));
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));
190 if(i%maxBatch==0 && sb!=null) {
191 cqlBatch.execute(dryRun);
198 } catch (IOException | CadiException e) {
203 // Cleanup, if required.
204 cqlBatch.execute(dryRun);
209 protected void _close(AuthzTrans trans) {
210 trans.info().log("End " + this.getClass().getSimpleName() + " processing" );
211 credDAO.close(trans);