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.reports;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.util.Date;
27 import java.util.GregorianCalendar;
28 import java.util.HashMap;
31 import org.onap.aaf.auth.batch.Batch;
32 import org.onap.aaf.auth.batch.helpers.Cred;
33 import org.onap.aaf.auth.batch.helpers.Cred.Instance;
34 import org.onap.aaf.auth.batch.helpers.UserRole;
35 import org.onap.aaf.auth.dao.cass.CredDAO;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.auth.org.OrganizationException;
38 import org.onap.aaf.cadi.util.CSV;
39 import org.onap.aaf.cadi.util.CSV.Writer;
40 import org.onap.aaf.misc.env.APIException;
41 import org.onap.aaf.misc.env.Env;
42 import org.onap.aaf.misc.env.TimeTaken;
43 import org.onap.aaf.misc.env.util.Chrono;
45 public class PrepExtend extends Batch {
47 public static final String PREP_EXTEND = "PrepExtend";
48 private static final String CSV = ".csv";
49 private static final String INFO = "info";
52 * Create a list of Creds and UserRoles to extend
53 * Note: Certificates cannot be renewed in this way.
55 * Arguments From (0 = today, -2 = 2 weeks back) and To (weeks from today)
58 * @throws APIException
60 * @throws OrganizationException
62 public PrepExtend(AuthzTrans trans) throws APIException, IOException, OrganizationException {
64 trans.info().log("Starting Connection Process");
66 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
68 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
70 session = cluster.connect();
80 protected void run(AuthzTrans trans) {
81 GregorianCalendar gc = new GregorianCalendar();
82 Date now = gc.getTime();
87 for(int i=0; i< args().length;++i) {
90 if(args().length>i+1) {
91 ifrom = Integer.parseInt(args()[i++ +1]);
95 if(args().length>i+1) {
96 ito = Integer.parseInt(args()[i++ +1]);
102 System.err.println("Invalid -from param");
106 if(ito<=0 || ito>24 || ifrom>ito) {
107 System.err.println("Invalid -to param");
111 // Make sure to is Zero based from today.
116 gc.add(GregorianCalendar.WEEK_OF_MONTH, ifrom);
117 Date from = gc.getTime();
119 gc.add(GregorianCalendar.WEEK_OF_MONTH, ito /* with From calculated in */);
120 Date to = gc.getTime();
123 File file = new File(logDir(), PREP_EXTEND + Chrono.dateOnlyStamp(now) + CSV);
124 final CSV puntCSV = new CSV(env.access(),file);
125 final Writer cw = puntCSV.writer();
126 cw.row(INFO,PREP_EXTEND,Chrono.dateOnlyStamp(now),0);
129 trans.info().log("Process UserRoles for Extending");
131 Run through User Roles.
132 If match Date Range, write out to appropriate file.
134 UserRole.load(trans, session, UserRole.v2_0_11, ur -> {
135 if(from.before(ur.expires()) && to.after(ur.expires())) {
136 ur.row(cw,UserRole.UR);
140 trans.info().log("Process BasicAuth for Extending");
141 TimeTaken tt0 = trans.start("Load Credentials", Env.REMOTE);
143 // Load only Valid Basic Auth
144 Cred.load(trans, session, CredDAO.BASIC_AUTH_SHA256);
152 If match Date Range, write out to appropriate file.
154 Map<Integer,Instance> imap = new HashMap<>();
156 for(Cred cred : Cred.data.values()) {
158 for(Instance i : cred.instances) {
159 if(from.before(i.expires) && to.after(i.expires)) {
160 prev = imap.get(i.other);
161 // Only do LATEST instance of same cred (accounts for previously extended creds)
162 if(prev==null || prev.expires.before(i.expires)) {
167 for(Instance i: imap.values()) {
174 } catch (FileNotFoundException e) {
179 protected void _close(AuthzTrans trans) {