Approval Batch, prep better JUnit
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / reports / NsRoleUserReport.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.Map;
28 import java.util.Map.Entry;
29 import java.util.TreeMap;
30
31 import org.onap.aaf.auth.batch.Batch;
32 import org.onap.aaf.auth.batch.helpers.NS;
33 import org.onap.aaf.auth.batch.helpers.Role;
34 import org.onap.aaf.auth.batch.helpers.UserRole;
35 import org.onap.aaf.auth.batch.helpers.Visitor;
36 import org.onap.aaf.auth.env.AuthzTrans;
37 import org.onap.aaf.auth.org.OrganizationException;
38 import org.onap.aaf.cadi.util.CSV;
39 import org.onap.aaf.cadi.util.CSV.Writer;
40 import org.onap.aaf.misc.env.APIException;
41 import org.onap.aaf.misc.env.Env;
42 import org.onap.aaf.misc.env.TimeTaken;
43 import org.onap.aaf.misc.env.util.Chrono;
44
45
46 public class NsRoleUserReport extends Batch {
47     
48         private static final String REPORT = NsRoleUserReport.class.getSimpleName();
49         private static final String CSV = ".csv";
50         private Date now;
51         private Writer report;
52         private Map<String,Map<String,Integer>> theMap;
53         
54         public NsRoleUserReport(AuthzTrans trans) throws APIException, IOException, OrganizationException {
55         super(trans.env());
56         trans.info().log("Starting Connection Process");
57         
58         TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
59         try {
60             TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
61             try {
62                 session = cluster.connect();
63             } finally {
64                 tt.done();
65             }
66             
67             // Create Intermediate Output 
68             now = new Date();
69             String sdate = Chrono.dateOnlyStamp(now);
70                 File file = new File(logDir(),REPORT + sdate +CSV);
71             CSV csv = new CSV(env.access(),file);
72             report = csv.writer(false);
73             
74             theMap = new TreeMap<>();
75
76             NS.load(trans, session, NS.v2_0_11);
77             Role.load(trans, session);
78         } finally {
79             tt0.done();
80         }
81     }
82
83     @Override
84     protected void run(AuthzTrans trans) {
85                 try {
86                         trans.info().log("Create Report on Roles by NS");
87                         
88                         final AuthzTrans transNoAvg = trans.env().newTransNoAvg();
89                         UserRole.load(transNoAvg, session, UserRole.v2_0_11, ur -> {
90                                 if(ur.expires().after(now)) {
91                                         Map<String, Integer> roleCount = theMap.get(ur.ns());
92                                         Integer count;
93                                         if(roleCount==null) {
94                                                 roleCount = new TreeMap<>();
95                                                 theMap.put(ur.ns(),roleCount);
96                                                 count = 0;
97                                         } else {
98                                                 count = roleCount.get(ur.rname());
99                                                 if(count == null) {
100                                                         count = 0;
101                                                 }
102                                         }
103                                         roleCount.put(ur.rname(), count+1);
104                                 }
105                         });
106                         
107                         for(Entry<String, Map<String, Integer>> ns_es : theMap.entrySet()) {
108                                 for(Entry<String, Integer> r_es : ns_es.getValue().entrySet()) {
109                                         report.row(ns_es.getKey(),r_es.getKey(),r_es.getValue());
110                                 }
111                         }
112
113
114                 } finally {
115                 }
116         }
117  
118         @Override
119     protected void _close(AuthzTrans trans) {
120         session.close();
121         report.close();
122     }
123
124 }