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.List;
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;
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;
57 public Extend(AuthzTrans trans) throws APIException, IOException, OrganizationException {
59 trans.info().log("Starting Connection Process");
61 noAvg = env.newTransNoAvg();
62 noAvg.setUser(new BatchPrincipal("Extend"));
64 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
66 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
67 credDAO = new CredDAO(trans, cluster, CassAccess.KEYSPACE);
69 session = credDAO.getSession(trans);
73 cqlBatch = new CQLBatch(noAvg.info(),session);
78 gcType = GregorianCalendar.WEEK_OF_YEAR;
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]);
90 if(weeks<1 || weeks > 24) {
91 throw new APIException("Invalid --weeks");
95 // Create Intermediate Output
96 File logDir = logDir();
97 extFiles = new ArrayList<>();
99 for(int i=0;i<args().length;++i) {
100 extFiles.add(new File(logDir, args()[i]));
103 extFiles.add(new File(logDir,PrepExtend.PREP_EXTEND+Chrono.dateOnlyStamp()+".csv"));
106 // Load Cred. We don't follow Visitor, because we have to gather up everything into Identity Anyway
107 // to find the last one.
111 protected void run(AuthzTrans trans) {
112 final int maxBatch = 50;
114 // Setup Date boundaries
115 final Holder<GregorianCalendar> hgc = new Holder<>(new GregorianCalendar());
116 final GregorianCalendar now = new GregorianCalendar();
118 ///////////////////////////
119 trans.info().log("Bulk Extend Expiring User-Roles and Creds");
121 final Holder<List<String>> info = new Holder<>(null);
122 final Holder<StringBuilder> hsb = new Holder<>(null);
124 for(File f : extFiles) {
125 CSV csv = new CSV(env.access(),f);
127 csv.visit(new CSV.Visitor() {
128 final Holder<Integer> hi = new Holder<>(0);
131 public void visit(List<String> row) throws IOException, CadiException {
132 GregorianCalendar gc;
134 StringBuilder sb = hsb.get();
136 hsb.set(sb=cqlBatch.begin());
145 gc.setTime(new Date(Long.parseLong(row.get(5))));
147 gc.setTime(now.getTime());
149 gc.add(gcType, extendBy);
150 UserRole.batchExtend(sb,row,Chrono.dateTime(gc));
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) {
160 if(row.get(4).equals(prev=Chrono.dateOnlyStamp(cd.expires))) {
162 gc.setTime(new Date(Long.parseLong(row.get(5))));
164 gc.setTime(now.getTime());
166 gc.add(gcType, extendBy);
167 cd.expires = gc.getTime();
169 noAvg.info().printf("Would extend %s, %d - %s to %s",cd.id,cd.type,prev, Chrono.dateOnlyStamp(cd.expires));
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));
183 if(i%maxBatch==0 && sb!=null) {
184 cqlBatch.execute(dryRun);
190 } catch (IOException | CadiException e) {
195 // Cleanup, if required.
196 cqlBatch.execute(dryRun);
201 protected void _close(AuthzTrans trans) {
202 trans.info().log("End " + this.getClass().getSimpleName() + " processing" );
203 credDAO.close(trans);