Improved multi Proxy DNSLocator based
[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 import org.onap.aaf.auth.batch.Batch;
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.org.OrganizationException;
36 import org.onap.aaf.cadi.util.CSV;
37 import org.onap.aaf.cadi.util.CSV.Writer;
38 import org.onap.aaf.misc.env.APIException;
39 import org.onap.aaf.misc.env.Env;
40 import org.onap.aaf.misc.env.TimeTaken;
41 import org.onap.aaf.misc.env.util.Chrono;
42 import org.onap.aaf.misc.env.util.Split;
43
44 import com.datastax.driver.core.ResultSet;
45 import com.datastax.driver.core.Row;
46 import com.datastax.driver.core.SimpleStatement;
47 import com.datastax.driver.core.Statement;
48
49
50 public class ApprovedRpt extends Batch {
51     
52     private static final String APPR_RPT = "ApprovedRpt";
53     private static final String CSV = ".csv";
54     private static final String INFO = "info";
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             Map<String,Boolean> checked = new TreeMap<String, Boolean>();
91             
92             final AuthzTrans transNoAvg = trans.env().newTransNoAvg();
93 //            ResultSet results;
94 //            Statement stmt = new SimpleStatement( "select dateof(id), approver, status, user, type, memo from authz.approved;" );
95 //            results = session.execute(stmt);
96 //            Iterator<Row> iter = results.iterator();
97 //            Row row;
98             /*
99              *             while (iter.hasNext()) {
100                 ++totalLoaded;
101                 row = iter.next();
102                 d = row.getTimestamp(0);
103                 if(d.after(begin)) {
104                     approvedW.row("aprvd",
105                             Chrono.dateOnlyStamp(d),
106                             row.getString(1),
107                             row.getString(2),
108                             row.getString(3),
109                             row.getString(4),
110                             row.getString(5)
111                     );
112                 }
113             }
114
115              */
116             int totalLoaded = 0;
117             Date d;
118             GregorianCalendar gc = new GregorianCalendar();
119             gc.add(GregorianCalendar.MONTH, -2);
120             Date begin = gc.getTime();
121             approvedW.comment("date, approver, status, user, role, memo");
122             
123             historyR.visit(row -> {
124                 String s = row.get(7);
125                 if(s.equals(yr_mon)) {
126                     String target = row.get(5);
127                     if("user_role".equals(target)) {
128                         String action = row.get(1);
129                         switch(action) {
130                             case "create":
131                                 write("created",row);
132                                 break;
133                             case "update":
134                                 write("approved",row);
135                                 break;
136                             case "delete":
137                                 write("denied",row);
138                                 break;
139                         }
140                     }
141                 }
142             });
143             
144         } catch (Exception e) {
145             trans.info().log(e);
146         }
147     }
148     
149     private void write(String a_or_d, List<String> row) {
150         String[] target = Split.splitTrim('|', row.get(4));
151         
152         if(target.length>1) {
153             UUID id = UUID.fromString(row.get(0));
154             Date date = Chrono.uuidToDate(id);
155             String status;
156             String memo;
157             String approver = row.get(6);
158             if("batch:JobChange".equals(approver)) {
159                 status = "reduced";
160                 memo = "existing role membership reduced to invoke reapproval";
161             } else {
162                 status = a_or_d;
163                 memo = row.get(2);
164             }
165             if(!approver.equals(target[0])) {
166                 approvedW.row(
167                     Chrono.niceDateStamp(date),
168                     approver,
169                     status,
170                     target[0],
171                     target[1],
172                     memo
173                 );
174             }
175         }
176
177         
178     }
179
180 }