a6c49f0824eceb2d26dccd1bafad8e6c2992b19e
[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.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.TreeMap;
33 import java.util.UUID;
34 import org.onap.aaf.auth.batch.Batch;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.org.OrganizationException;
37 import org.onap.aaf.cadi.util.CSV;
38 import org.onap.aaf.cadi.util.CSV.Writer;
39 import org.onap.aaf.misc.env.APIException;
40 import org.onap.aaf.misc.env.Env;
41 import org.onap.aaf.misc.env.TimeTaken;
42 import org.onap.aaf.misc.env.util.Chrono;
43 import org.onap.aaf.misc.env.util.Split;
44
45 import com.datastax.driver.core.ResultSet;
46 import com.datastax.driver.core.Row;
47 import com.datastax.driver.core.SimpleStatement;
48 import com.datastax.driver.core.Statement;
49
50
51 public class ApprovedRpt extends Batch {
52
53     private static final String APPR_RPT = "ApprovedRpt";
54     private static final String CSV = ".csv";
55     private Date now;
56     private Writer approvedW;
57     private CSV historyR;
58     private static String yr_mon;
59
60     public ApprovedRpt(AuthzTrans trans) throws APIException, IOException, OrganizationException {
61         super(trans.env());
62         trans.info().log("Starting Connection Process");
63
64         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
65         try {
66 //            TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
67 //            try {
68 //                session = cluster.connect();
69 //            } finally {
70 //                tt.done();
71 //            }
72
73             now = new Date();
74             String sdate = Chrono.dateOnlyStamp(now);
75             File file = new File(logDir(),APPR_RPT + sdate +CSV);
76             CSV csv = new CSV(env.access(),file);
77             approvedW = csv.writer(false);
78
79             historyR = new CSV(env.access(),args()[1]).setDelimiter('|');
80
81             yr_mon = args()[0];
82         } finally {
83             tt0.done();
84         }
85     }
86
87     @Override
88     protected void run(AuthzTrans trans) {
89         try {
90 //            ResultSet results;
91 //            Statement stmt = new SimpleStatement( "select dateof(id), approver, status, user, type, memo from authz.approved;" );
92 //            results = session.execute(stmt);
93 //            Iterator<Row> iter = results.iterator();
94 //            Row row;
95             /*
96              *             while (iter.hasNext()) {
97                 ++totalLoaded;
98                 row = iter.next();
99                 d = row.getTimestamp(0);
100                 if(d.after(begin)) {
101                     approvedW.row("aprvd",
102                             Chrono.dateOnlyStamp(d),
103                             row.getString(1),
104                             row.getString(2),
105                             row.getString(3),
106                             row.getString(4),
107                             row.getString(5)
108                     );
109                 }
110             }
111              */
112             GregorianCalendar gc = new GregorianCalendar();
113             gc.add(GregorianCalendar.MONTH, -2);
114             approvedW.comment("date, approver, status, user, role, memo");
115             historyR.visit(row -> {
116                 String s = row.get(7);
117                 if(s.equals(yr_mon)) {
118                     String target = row.get(5);
119                     if("user_role".equals(target)) {
120                         String action = row.get(1);
121                         switch(action) {
122                             case "create":
123                                 write("created",row);
124                                 break;
125                             case "update":
126                                 write("approved",row);
127                                 break;
128                             case "delete":
129                                 write("denied",row);
130                                 break;
131                         }
132                     }
133                 }
134             });
135
136         } catch (Exception e) {
137             trans.info().log(e);
138         }
139     }
140
141     private void write(String a_or_d, List<String> row) {
142         String[] target = Split.splitTrim('|', row.get(4));
143
144         if(target.length>1) {
145             UUID id = UUID.fromString(row.get(0));
146             Date date = Chrono.uuidToDate(id);
147             String status;
148             String memo;
149             String approver = row.get(6);
150             if("batch:JobChange".equals(approver)) {
151                 status = "reduced";
152                 memo = "existing role membership reduced to invoke reapproval";
153             } else {
154                 status = a_or_d;
155                 memo = row.get(2);
156             }
157             if(!approver.equals(target[0])) {
158                 approvedW.row(
159                     Chrono.niceDateStamp(date),
160                     approver,
161                     status,
162                     target[0],
163                     target[1],
164                     memo
165                 );
166             }
167         }
168
169
170     }
171
172 }