Batch work and client
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / update / Approvals.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.batch.update;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.text.ParseException;
27 import java.util.ArrayList;
28 import java.util.GregorianCalendar;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.TreeMap;
32
33 import org.onap.aaf.auth.batch.Batch;
34 import org.onap.aaf.auth.batch.BatchPrincipal;
35 import org.onap.aaf.auth.batch.approvalsets.ApprovalSet;
36 import org.onap.aaf.auth.batch.approvalsets.Pending;
37 import org.onap.aaf.auth.batch.approvalsets.URApprovalSet;
38 import org.onap.aaf.auth.batch.helpers.BatchDataView;
39 import org.onap.aaf.auth.batch.helpers.NS;
40 import org.onap.aaf.auth.batch.helpers.Role;
41 import org.onap.aaf.auth.batch.helpers.UserRole;
42 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
43 import org.onap.aaf.auth.env.AuthzTrans;
44 import org.onap.aaf.auth.layer.Result;
45 import org.onap.aaf.auth.org.OrganizationException;
46 import org.onap.aaf.cadi.CadiException;
47 import org.onap.aaf.cadi.client.Holder;
48 import org.onap.aaf.cadi.util.CSV;
49 import org.onap.aaf.misc.env.APIException;
50 import org.onap.aaf.misc.env.TimeTaken;
51 import org.onap.aaf.misc.env.Trans;
52 import org.onap.aaf.misc.env.util.Chrono;
53
54 public class Approvals extends Batch {
55     private final AuthzTrans noAvg;
56         private BatchDataView dataview;
57         private List<CSV> csvList;
58         private GregorianCalendar now;
59
60     public Approvals(AuthzTrans trans) throws APIException, IOException, OrganizationException {
61         super(trans.env());
62         
63         noAvg = env.newTransNoAvg();
64         noAvg.setUser(new BatchPrincipal("batch:Approvals"));
65         session = cluster.connect();
66         dataview = new BatchDataView(noAvg,session,dryRun);
67         NS.load(trans, session, NS.v2_0_11);
68         Role.load(trans, session);
69         UserRole.load(trans, session, UserRole.v2_0_11);
70
71         now = new GregorianCalendar();
72         
73         csvList = new ArrayList<>();
74         File f;
75         if(args().length>0) {
76                 for(int i=0;i<args().length;++i) {
77                         f = new File(logDir(), args()[i]);
78                         if(f.exists()) {
79                                 csvList.add(new CSV(env.access(),f).processAll());
80                         } else {
81                         trans.error().printf("CSV File %s does not exist",f.getAbsolutePath());
82                         }
83                 }
84         } else {
85                 f = new File(logDir(), "Approvals"+Chrono.dateOnlyStamp()+".csv");
86                 if(f.exists()) {
87                         csvList.add(new CSV(env.access(),f).processAll());
88                         } else {
89                         trans.error().printf("CSV File %s does not exist",f.getAbsolutePath());
90                         }
91         }
92         
93         
94     }
95
96     @Override
97     protected void run(AuthzTrans trans) {
98         Map<String,Pending> mpending = new TreeMap<>();
99                 Holder<Integer> count = new Holder<>(0);
100         for(CSV approveCSV : csvList) {
101                 TimeTaken tt = trans.start("Load Analyzed Reminders",Trans.SUB,approveCSV.name());
102                 try {
103                                 approveCSV.visit(row -> {
104                                         switch(row.get(0)) {
105                                                 case Pending.REMIND:
106                                                         try {
107                                                                 Pending p = new Pending(row);
108                                                                 Pending mp = mpending.get(row.get(1));
109                                                                 if(mp==null) {
110                                                                         mpending.put(row.get(1), p);
111                                                                 } else {
112                                                                         mp.inc(p); // FYI, unlikely
113                                                                 }
114                                                                 count.set(count.get()+1);
115                                                         } catch (ParseException e) {
116                                                                 trans.error().log(e);
117                                                         } 
118                                                 break;
119                                         }
120                                 });
121                         } catch (IOException | CadiException e) {
122                                 e.printStackTrace();
123                                 // .... but continue with next row
124                 } finally {
125                         tt.done();
126                 }
127         }
128         trans.info().printf("Processed %d Reminder Rows", count.get());
129
130         count.set(0);
131         for(CSV approveCSV : csvList) {
132                 TimeTaken tt = trans.start("Processing %s's UserRoles",Trans.SUB,approveCSV.name());
133                 try {
134                                 approveCSV.visit(row -> {
135                                         switch(row.get(0)) {
136                                                 case UserRole.APPROVE_UR:
137                                                         UserRoleDAO.Data urdd = UserRole.row(row);
138                                                         // Create an Approval
139                                                         ApprovalSet uras = new URApprovalSet(noAvg, now, dataview, () -> {
140                                                                 return urdd;
141                                                         });
142                                                         Result<Void> rw = uras.write(noAvg);
143                                                         if(rw.isOK()) {
144                                                                 Pending p = new Pending();
145                                                                 Pending mp = mpending.get(urdd.user);
146                                                                 if(mp==null) {
147                                                                         mpending.put(urdd.user, p);
148                                                                 } else {
149                                                                         mp.inc(p);
150                                                                 }
151                                                                 count.set(count.get()+1);
152                                                         } else {
153                                                                 trans.error().log(rw.errorString());
154                                                         }
155                                                         break;
156                                         }
157                                 });
158                                 dataview.flush();
159                         } catch (IOException | CadiException e) {
160                                 e.printStackTrace();
161                                 // .... but continue with next row
162                 } finally {
163                         tt.done();
164                 }
165             trans.info().printf("Processed %d UserRoles", count.get());
166
167             count.set(0);
168                 tt = trans.start("Notify for Pending", Trans.SUB);
169                 try {
170                         
171                 } finally {
172                         tt.done();
173                 }
174             trans.info().printf("Created %d Notifications", count.get());
175             }
176     }
177     
178     @Override
179     protected void _close(AuthzTrans trans) {
180         if(session!=null) {
181                 session.close();
182                 session = null;
183         }
184     }
185 }