Mass whitespace changes (Style Warnings)
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / reports / ExpiringNext.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
22 package org.onap.aaf.auth.reports;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.GregorianCalendar;
29 import java.util.List;
30
31 import org.onap.aaf.auth.Batch;
32 import org.onap.aaf.auth.dao.cass.CredDAO;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.helpers.Cred;
35 import org.onap.aaf.auth.helpers.UserRole;
36 import org.onap.aaf.auth.helpers.Cred.Instance;
37 import org.onap.aaf.auth.org.OrganizationException;
38 import org.onap.aaf.misc.env.APIException;
39 import org.onap.aaf.misc.env.Env;
40 import org.onap.aaf.misc.env.TimeTaken;
41 import org.onap.aaf.misc.env.util.Chrono;
42
43 public class ExpiringNext extends Batch {
44     
45     public ExpiringNext(AuthzTrans trans) throws APIException, IOException, OrganizationException {
46         super(trans.env());
47         trans.info().log("Starting Connection Process");
48         
49         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
50         try {
51             TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
52             try {
53                 session = cluster.connect();
54             } finally {
55                 tt.done();
56             }
57
58             UserRole.load(trans, session, UserRole.v2_0_11);
59             Cred.load(trans, session);
60         } finally {
61             tt0.done();
62         }
63     }
64
65     @Override
66     protected void run(AuthzTrans trans) {
67         GregorianCalendar gc = new GregorianCalendar();
68         Date now = gc.getTime();
69         gc.add(GregorianCalendar.WEEK_OF_MONTH, 2);
70         Date twoWeeks = gc.getTime();
71         // Set time way off
72         gc.set(GregorianCalendar.YEAR, 3000);
73         Date earliestUR = gc.getTime();
74         Date earliestCred = gc.getTime();
75         // Run for Roles
76         List<String> expiring = new ArrayList<>();
77         
78         trans.info().log("Checking for Expired UserRoles");
79         for (UserRole ur : UserRole.getData()) {
80             if (ur.expires().after(now)) {
81                 if (ur.expires().before(twoWeeks)) {
82                     expiring.add(Chrono.dateOnlyStamp(ur.expires()) + ":\t" + ur.user() + '\t' + ur.role());
83                 }
84                 if (ur.expires().before(earliestUR)) {
85                     earliestUR = ur.expires();
86                 }
87             }
88         }
89
90         if (expiring.size()>0) {
91             Collections.sort(expiring,Collections.reverseOrder());
92             for (String s : expiring) {
93                 System.err.print('\t');
94                 System.err.println(s);
95             }
96             trans.info().printf("Earliest Expiring UR is %s\n\n", Chrono.dateOnlyStamp(earliestUR));
97         } else {
98             trans.info().printf("No Expiring UserRoles within 2 weeks");
99         }
100         
101         expiring.clear();
102         
103         trans.info().log("Checking for Expired Credentials");
104         for ( Cred creds : Cred.data.values()) {
105             Instance lastInstance=null;
106             for (Instance inst : creds.instances) {
107                 if (inst.type==CredDAO.BASIC_AUTH || inst.type==CredDAO.BASIC_AUTH_SHA256) {
108                     if (lastInstance == null || inst.expires.after(lastInstance.expires)) {
109                         lastInstance = inst;
110                     }
111                 }
112             }
113             if (lastInstance!=null) {
114                 if (lastInstance.expires.after(now)) {
115                     if (lastInstance.expires.before(twoWeeks)) {
116                         expiring.add(Chrono.dateOnlyStamp(lastInstance.expires) + ": \t" + creds.id);
117                     }
118                 }
119                 if (lastInstance.expires.before(earliestCred)) {
120                     earliestCred = lastInstance.expires;
121                 }
122             }
123         }
124         
125         if (expiring.size()>0) {
126             Collections.sort(expiring,Collections.reverseOrder());
127             for (String s : expiring) {
128                 System.err.print('\t');
129                 System.err.println(s);
130             }
131             trans.info().printf("Earliest Expiring Cred is %s\n\n", Chrono.dateOnlyStamp(earliestCred));
132         } else {
133             trans.info().printf("No Expiring Creds within 2 weeks");
134         }
135
136     }
137     
138     @Override
139     protected void _close(AuthzTrans trans) {
140         session.close();
141     }
142
143 }