Update DCAE Startup Info
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / reports / ApprovedRpt.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 IBM.
7  * ===========================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END====================================================
20  *
21  */
22
23 package org.onap.aaf.auth.batch.reports;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.Date;
28 import java.util.GregorianCalendar;
29 import java.util.List;
30 import java.util.UUID;
31
32 import org.onap.aaf.auth.batch.Batch;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.org.OrganizationException;
35 import org.onap.aaf.cadi.util.CSV;
36 import org.onap.aaf.cadi.util.CSV.Writer;
37 import org.onap.aaf.misc.env.APIException;
38 import org.onap.aaf.misc.env.Env;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.env.util.Chrono;
41 import org.onap.aaf.misc.env.util.Split;
42
43
44 public class ApprovedRpt extends Batch {
45
46     private static final String APPR_RPT = "ApprovedRpt";
47     private static final String CSV = ".csv";
48     private Date now;
49     private Writer approvedW;
50     private CSV historyR;
51     private static String yr_mon;
52
53     public ApprovedRpt(AuthzTrans trans) throws APIException, IOException, OrganizationException {
54         super(trans.env());
55         trans.info().log("Starting Connection Process");
56
57         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
58         try {
59             now = new Date();
60             String sdate = Chrono.dateOnlyStamp(now);
61             File file = new File(logDir(),APPR_RPT + sdate +CSV);
62             CSV csv = new CSV(env.access(),file);
63             approvedW = csv.writer(false);
64
65             historyR = new CSV(env.access(),args()[1]).setDelimiter('|');
66
67             yr_mon = args()[0];
68         } finally {
69             tt0.done();
70         }
71     }
72
73     @Override
74     protected void run(AuthzTrans trans) {
75         try {
76 //            ResultSet results;
77 //            Statement stmt = new SimpleStatement( "select dateof(id), approver, status, user, type, memo from authz.approved;" );
78 //            results = session.execute(stmt);
79 //            Iterator<Row> iter = results.iterator();
80 //            Row row;
81             /*
82              *             while (iter.hasNext()) {
83                 ++totalLoaded;
84                 row = iter.next();
85                 d = row.getTimestamp(0);
86                 if(d.after(begin)) {
87                     approvedW.row("aprvd",
88                             Chrono.dateOnlyStamp(d),
89                             row.getString(1),
90                             row.getString(2),
91                             row.getString(3),
92                             row.getString(4),
93                             row.getString(5)
94                     );
95                 }
96             }
97              */
98             GregorianCalendar gc = new GregorianCalendar();
99             gc.add(GregorianCalendar.MONTH, -2);
100             approvedW.comment("date, approver, status, user, role, memo");
101             historyR.visit(row -> {
102                 String s = row.get(7);
103                 if(s.equals(yr_mon)) {
104                     String target = row.get(5);
105                     if("user_role".equals(target)) {
106                         String action = row.get(1);
107                         switch(action) {
108                             case "create":
109                                 write("created",row);
110                                 break;
111                             case "update":
112                                 write("approved",row);
113                                 break;
114                             case "delete":
115                                 write("denied",row);
116                                 break;
117                         }
118                     }
119                 }
120             });
121
122         } catch (Exception e) {
123             trans.info().log(e);
124         }
125     }
126
127     private void write(String a_or_d, List<String> row) {
128         String[] target = Split.splitTrim('|', row.get(4));
129
130         if(target.length>1) {
131             UUID id = UUID.fromString(row.get(0));
132             Date date = Chrono.uuidToDate(id);
133             String status;
134             String memo;
135             String approver = row.get(6);
136             if("batch:JobChange".equals(approver)) {
137                 status = "reduced";
138                 memo = "existing role membership reduced to invoke reapproval";
139             } else {
140                 status = a_or_d;
141                 memo = row.get(2);
142             }
143             if(!approver.equals(target[0])) {
144                 approvedW.row(
145                     Chrono.niceDateStamp(date),
146                     approver,
147                     status,
148                     target[0],
149                     target[1],
150                     memo
151                 );
152             }
153         }
154
155
156     }
157
158 }