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====================================================
22 package org.onap.aaf.auth.batch.update;
25 import java.io.IOException;
26 import java.text.ParseException;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.GregorianCalendar;
30 import java.util.List;
32 import java.util.Map.Entry;
33 import java.util.TreeMap;
35 import org.onap.aaf.auth.batch.Batch;
36 import org.onap.aaf.auth.batch.BatchPrincipal;
37 import org.onap.aaf.auth.batch.approvalsets.ApprovalSet;
38 import org.onap.aaf.auth.batch.approvalsets.Pending;
39 import org.onap.aaf.auth.batch.approvalsets.URApprovalSet;
40 import org.onap.aaf.auth.batch.helpers.BatchDataView;
41 import org.onap.aaf.auth.batch.helpers.NS;
42 import org.onap.aaf.auth.batch.helpers.Notification;
43 import org.onap.aaf.auth.batch.helpers.Role;
44 import org.onap.aaf.auth.batch.helpers.UserRole;
45 import org.onap.aaf.auth.batch.reports.bodies.NotifyPendingApprBody;
46 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
47 import org.onap.aaf.auth.env.AuthzTrans;
48 import org.onap.aaf.auth.layer.Result;
49 import org.onap.aaf.auth.org.OrganizationException;
50 import org.onap.aaf.cadi.CadiException;
51 import org.onap.aaf.cadi.client.Holder;
52 import org.onap.aaf.cadi.util.CSV;
53 import org.onap.aaf.misc.env.APIException;
54 import org.onap.aaf.misc.env.TimeTaken;
55 import org.onap.aaf.misc.env.Trans;
56 import org.onap.aaf.misc.env.util.Chrono;
58 public class Approvals extends Batch {
59 private final AuthzTrans noAvg;
60 private BatchDataView dataview;
61 private List<CSV> csvList;
62 private GregorianCalendar now;
65 public Approvals(AuthzTrans trans) throws APIException, IOException, OrganizationException {
68 noAvg = env.newTransNoAvg();
69 noAvg.setUser(new BatchPrincipal("batch:Approvals"));
70 session = cluster.connect();
71 dataview = new BatchDataView(noAvg,session,dryRun);
72 NS.load(trans, session, NS.v2_0_11);
73 Role.load(trans, session);
74 UserRole.load(trans, session, UserRole.v2_0_11);
76 now = new GregorianCalendar();
78 csvList = new ArrayList<>();
81 for(int i=0;i<args().length;++i) {
82 f = new File(logDir(), args()[i]);
84 csvList.add(new CSV(env.access(),f).processAll());
86 trans.error().printf("CSV File %s does not exist",f.getAbsolutePath());
90 f = new File(logDir(), "Approvals"+Chrono.dateOnlyStamp()+".csv");
92 csvList.add(new CSV(env.access(),f).processAll());
94 trans.error().printf("CSV File %s does not exist",f.getAbsolutePath());
101 protected void run(AuthzTrans trans) {
102 Map<String,Pending> mpending = new TreeMap<>();
103 Holder<Integer> count = new Holder<>(0);
104 for(CSV approveCSV : csvList) {
105 TimeTaken tt = trans.start("Load Analyzed Reminders",Trans.SUB,approveCSV.name());
107 approveCSV.visit(row -> {
111 String user = row.get(1);
112 Pending p = new Pending(row);
113 Pending mp = mpending.get(user);
115 mpending.put(user, p);
117 mp.inc(p); // FYI, unlikely
119 count.set(count.get()+1);
120 } catch (ParseException e) {
121 trans.error().log(e);
126 } catch (IOException | CadiException e) {
128 // .... but continue with next row
133 trans.info().printf("Processed %d Reminder Rows", count.get());
136 for(CSV approveCSV : csvList) {
137 TimeTaken tt = trans.start("Processing %s's UserRoles",Trans.SUB,approveCSV.name());
139 approveCSV.visit(row -> {
141 case UserRole.APPROVE_UR:
142 UserRoleDAO.Data urdd = UserRole.row(row);
143 // Create an Approval
144 ApprovalSet uras = new URApprovalSet(noAvg, now, dataview, () -> {
147 Result<Void> rw = uras.write(noAvg);
149 Pending p = new Pending();
150 Pending mp = mpending.get(urdd.user);
152 mpending.put(urdd.user, p);
156 count.set(count.get()+1);
158 trans.error().log(rw.errorString());
164 } catch (IOException | CadiException e) {
166 // .... but continue with next row
170 trans.info().printf("Processed %d UserRoles", count.get());
173 NotifyPendingApprBody npab;
174 GregorianCalendar gc = new GregorianCalendar();
175 gc.add(GregorianCalendar.DAY_OF_MONTH, 7);
176 Date oneWeek = gc.getTime();
179 tt = trans.start("Notify for Pending", Trans.SUB);
181 for(Entry<String, Pending> es : mpending.entrySet()) {
183 Date earliest = p.earliest();
184 if(p.newApprovals() || earliest==null || earliest.before(oneWeek) ) {
185 System.out.println("update");
191 trans.info().printf("Created %d Notifications", count.get());
196 protected void _close(AuthzTrans trans) {