Upgrade to latest oparent
[aaf/authz.git] / authz-batch / src / main / java / com / att / authz / reports / Expiring.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.reports;
5
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.Date;
9 import java.util.GregorianCalendar;
10 import java.util.List;
11
12 import com.att.authz.Batch;
13 import com.att.authz.actions.Action;
14 import com.att.authz.actions.ActionDAO;
15 import com.att.authz.actions.CredDelete;
16 import com.att.authz.actions.CredPrint;
17 import com.att.authz.actions.FADelete;
18 import com.att.authz.actions.FAPrint;
19 import com.att.authz.actions.Key;
20 import com.att.authz.actions.URDelete;
21 import com.att.authz.actions.URFutureApprove;
22 import com.att.authz.actions.URFuturePrint;
23 import com.att.authz.actions.URPrint;
24 import com.att.authz.env.AuthzTrans;
25 import com.att.authz.helpers.Cred;
26 import com.att.authz.helpers.Cred.Instance;
27 import com.att.authz.helpers.Future;
28 import com.att.authz.helpers.Notification;
29 import com.att.authz.helpers.UserRole;
30 import com.att.authz.layer.Result;
31 import com.att.authz.org.Organization.Identity;
32 import com.att.dao.aaf.cass.CredDAO;
33 import org.onap.aaf.inno.env.APIException;
34 import org.onap.aaf.inno.env.Env;
35 import org.onap.aaf.inno.env.TimeTaken;
36
37 public class Expiring extends Batch {
38         
39         private final Action<UserRole,Void> urDelete,urPrint;
40         private final Action<UserRole,List<Identity>> urFutureApprove;
41         private final Action<CredDAO.Data,Void> crDelete,crPrint;
42         private final Action<Future,Void> faDelete;
43 //      private final Email email;
44         private final Key<UserRole> memoKey;
45         
46         public Expiring(AuthzTrans trans) throws APIException, IOException {
47                 super(trans.env());
48             trans.info().log("Starting Connection Process");
49             TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
50             try {
51                         urPrint = new URPrint("Expired:");
52                         crPrint = new CredPrint("Expired:");
53
54                         URFutureApprove ufr = new URFutureApprove(trans,cluster); 
55                         memoKey = ufr;
56                         
57                         if(isDryRun()) {
58                                 urDelete = new URPrint("Would Delete:");
59                                 // While Testing
60 //                              urFutureApprove = ufr;
61                                 urFutureApprove = new URFuturePrint("Would setup Future/Approvals");
62                                 crDelete = new CredPrint("Would Delete:");
63                                 faDelete = new FAPrint("Would Delete:");
64 //                              email = new EmailPrint();
65
66                                 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
67                                 try {
68                                         session = cluster.connect();
69                                 } finally {
70                                         tt.done();
71                                 }
72         
73                         } else {
74                                 TimeTaken tt = trans.start("Connect to Cluster with DAOs", Env.REMOTE);
75                                 try {
76                                         ActionDAO<UserRole,Void> adao;
77                                         urDelete = adao = new URDelete(trans, cluster);
78                                         urFutureApprove = new URFutureApprove(trans,adao);
79                                         faDelete = new FADelete(trans, adao);
80
81                                         crDelete = new CredDelete(trans, adao);
82 //                                      email = new Email();
83                                         TimeTaken tt2 = trans.start("Connect to Cluster", Env.REMOTE);
84                                         try {
85                                                 session = adao.getSession(trans);
86                                         } finally {
87                                                 tt2.done();
88                                         }
89                                 } finally {
90                                         tt.done();
91                                 }
92                         }
93                         
94                         UserRole.load(trans, session, UserRole.v2_0_11);
95                         Cred.load(trans, session);
96                         Notification.load(trans, session, Notification.v2_0_14);
97                         Future.load(trans,session,Future.v2_0_15);
98             } finally {
99                 tt0.done();
100             }
101         }
102
103         @Override
104         protected void run(AuthzTrans trans) {
105                 // Setup Date boundaries
106                 Date now = new Date();
107         GregorianCalendar gc = new GregorianCalendar();
108         gc.setTime(now);
109         gc.add(GregorianCalendar.MONTH, 1);
110         Date future = gc.getTime();
111         gc.setTime(now);
112         gc.add(GregorianCalendar.MONTH, -1);
113         Date tooLate = gc.getTime();
114         int count = 0, deleted=0;
115         
116 //        List<Notification> ln = new ArrayList<Notification>();
117         TimeTaken tt;
118                 
119         // Run for Expired Futures
120         trans.info().log("Checking for Expired Futures");
121         tt = trans.start("Delete old Futures", Env.REMOTE);
122         try {
123                 List<Future> delf = new ArrayList<Future>();
124                 for(Future f : Future.data) {
125                         AuthzTrans localTrans = env.newTransNoAvg();
126                         if(f.expires.before(now)) {
127                                 faDelete.exec(localTrans, f);
128                                 delf.add(f);
129                         }
130                 }
131                 Future.delete(delf);
132         } finally {
133                 tt.done();
134         }
135
136         // Run for Roles
137         trans.info().log("Checking for Expired Roles");
138         try {
139                 for(UserRole ur : UserRole.data) {
140                         AuthzTrans localTrans = env.newTransNoAvg();
141                         if(ur.expires.before(tooLate)) {
142                                 if("owner".equals(ur.rname)) { // don't delete Owners, even if Expired
143                                         urPrint.exec(localTrans,ur);
144                                 } else {
145                                 urDelete.exec(localTrans,ur);
146                                 ++deleted;
147                                 trans.logAuditTrail(trans.info());
148                                 }
149                                 ++count;
150                         } else if(ur.expires.before(future)) {
151                                 List<Future> fbm = Future.byMemo.get(memoKey.key(ur));
152                                 if(fbm==null || fbm.isEmpty()) {
153                                         Result<List<Identity>> rapprovers = urFutureApprove.exec(localTrans, ur);
154                                         if(rapprovers.isOK()) {
155                                                 for(Identity ou : rapprovers.value) {
156 //                                                      Notification n = Notification.addApproval(localTrans,ou);
157 //                                                      if(n.org==null) {
158 //                                                              n.org = getOrgFromID(localTrans, ur.user);
159 //                                                      }
160 //                                                      ln.add(n);
161                                                         urPrint.exec(localTrans,ur);
162                                                         if(isDryRun()) {
163                                                                 trans.logAuditTrail(trans.info());
164                                                         }
165                                                 }
166                                         }
167                                 }
168                                 ++count;
169                         }
170                 }
171                 } finally {
172                 env.info().log("Found",count,"roles expiring before",future);
173                 env.info().log("deleting",deleted,"roles expiring before",tooLate);
174         }
175         
176 //        // Email Approval Notification
177 //              email.subject("AAF Role Expiration Warning (ENV: %s)", batchEnv);
178 //              email.indent("");
179 //        for(Notification n: ln) {
180 //              if(n.org==null) {
181 //                      trans.error().log("No Organization for Notification");
182 //              } else if(n.update(trans, session, isDryRun())) {
183 //                      email.clear();
184 //                      email.addTo(n.user);
185 //                              email.line(n.text(new StringBuilder()).toString());
186 //                              email.exec(trans,n.org);
187 //              }               
188 //        }
189         // Run for Creds
190         trans.info().log("Checking for Expired Credentials");
191         System.out.flush();
192         count = 0;
193         try {
194                 CredDAO.Data crd = new CredDAO.Data();
195                 Date last = null;
196                 for( Cred creds : Cred.data.values()) {
197                         AuthzTrans localTrans = env.newTransNoAvg();
198                                 crd.id = creds.id;
199                         for(int type : creds.types()) {
200                                         crd.type = type;
201                                 for( Instance inst : creds.instances) {
202                                         if(inst.expires.before(tooLate)) {
203                                                 crd.expires = inst.expires;
204                                                 crDelete.exec(localTrans, crd);
205                                         } else if(last==null || inst.expires.after(last)) {
206                                                 last = inst.expires;
207                                         }
208                                 }
209                                 if(last!=null) {
210                                         if(last.before(future)) {
211                                                 crd.expires = last;
212                                                 crPrint.exec(localTrans, crd);
213                                                 ++count;
214                                         }
215                                 }
216                         }
217                 }
218         } finally {
219                 env.info().log("Found",count,"current creds expiring before",future);
220         }
221         
222         }
223         
224         @Override
225         protected void _close(AuthzTrans trans) {
226         aspr.info("End " + this.getClass().getSimpleName() + " processing" );
227         for(Action<?,?> action : new Action<?,?>[] {urDelete,crDelete}) {
228                 if(action instanceof ActionDAO) {
229                         ((ActionDAO<?,?>)action).close(trans);
230                 }
231         }
232         session.close();
233         }
234
235 }