Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / reports / PrepExtend.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.reports;
22
23 import java.io.File;
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;
29 import java.util.Map;
30
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;
44
45 public class PrepExtend extends Batch {
46
47     public static final String PREP_EXTEND = "PrepExtend";
48     private static final String CSV = ".csv";
49     private static final String INFO = "info";
50
51     /**
52      * Create a list of Creds and UserRoles to extend
53      * Note: Certificates cannot be renewed in this way.
54      * 
55      * Arguments From (0 = today, -2 = 2 weeks back) and To (weeks from today)
56      * 
57      * @param trans
58      * @throws APIException
59      * @throws IOException
60      * @throws OrganizationException
61      */
62     public PrepExtend(AuthzTrans trans) throws APIException, IOException, OrganizationException {
63         super(trans.env());
64         trans.info().log("Starting Connection Process");
65
66         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
67         try {
68             TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
69             try {
70                 session = cluster.connect();
71             } finally {
72                 tt.done();
73             }
74         } finally {
75             tt0.done();
76         }
77     }
78
79     @Override
80     protected void run(AuthzTrans trans) {
81         GregorianCalendar gc = new GregorianCalendar();
82         Date now = gc.getTime();
83         
84         int ifrom = 0;
85         int ito = 4;
86         
87         for(int i=0; i< args().length;++i) {
88             switch(args()[i]) {
89                 case "-from":
90                     if(args().length>i+1) {
91                         ifrom = Integer.parseInt(args()[i++ +1]); 
92                     }
93                     break;
94                 case "-to":
95                     if(args().length>i+1) {
96                         ito = Integer.parseInt(args()[i++ +1]);
97                     }
98                     break;
99             }
100         }
101         if(ifrom < -4) {
102             System.err.println("Invalid -from param");
103             return;
104         }
105         
106         if(ito<=0 || ito>24 || ifrom>ito) {
107             System.err.println("Invalid -to param");
108             return;
109         }
110         
111         // Make sure to is Zero based from today.
112         if(ifrom<0) {
113             ito+= ifrom*-1;
114         }
115         
116         gc.add(GregorianCalendar.WEEK_OF_MONTH, ifrom);
117         Date from = gc.getTime();
118         
119         gc.add(GregorianCalendar.WEEK_OF_MONTH, ito /* with From calculated in */);
120         Date to = gc.getTime();
121         
122         try {
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);
127
128             try {
129                 trans.info().log("Process UserRoles for Extending");
130                 /**
131                    Run through User Roles.  
132                    If match Date Range, write out to appropriate file.
133                 */
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);
137                     }
138                 });
139                 
140                 trans.info().log("Process BasicAuth for Extending");
141                 TimeTaken tt0 = trans.start("Load Credentials", Env.REMOTE);
142                 try {
143                     // Load only Valid Basic Auth
144                     Cred.load(trans, session, CredDAO.BASIC_AUTH_SHA256);
145                 } finally {
146                     tt0.done();
147                 }
148
149
150                 /**
151                    Run through Creds.  
152                    If match Date Range, write out to appropriate file.
153                 */
154                 Map<Integer,Instance> imap = new HashMap<>();
155                 Instance prev;
156                 for(Cred cred : Cred.data.values()) {
157                     imap.clear();
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)) {
163                                 imap.put(i.other,i);
164                             }
165                         }
166                     };
167                     for(Instance i: imap.values()) {
168                         cred.row(cw,i);
169                     }
170                 }
171             } finally {
172                 cw.close();
173             }
174         } catch (FileNotFoundException e) {
175             e.printStackTrace();
176         }
177     }
178     @Override
179     protected void _close(AuthzTrans trans) {
180         session.close();
181     }
182
183
184 }