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