2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.batch.reports;
25 import java.io.IOException;
26 import java.util.Date;
28 import java.util.Map.Entry;
29 import java.util.TreeMap;
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;
46 public class NsRoleUserReport extends Batch {
48 private static final String REPORT = NsRoleUserReport.class.getSimpleName();
49 private static final String CSV = ".csv";
51 private Writer report;
52 private Map<String,Map<String,Integer>> theMap;
54 public NsRoleUserReport(AuthzTrans trans) throws APIException, IOException, OrganizationException {
56 trans.info().log("Starting Connection Process");
58 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
60 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
62 session = cluster.connect();
67 // Create Intermediate Output
69 String sdate = Chrono.dateOnlyStamp(now);
70 File file = new File(logDir(),REPORT + sdate +CSV);
71 CSV csv = new CSV(file);
72 report = csv.writer(false);
74 theMap = new TreeMap<>();
76 NS.load(trans, session, NS.v2_0_11);
77 Role.load(trans, session);
84 protected void run(AuthzTrans trans) {
86 trans.info().log("Create Report on Roles by NS");
88 final AuthzTrans transNoAvg = trans.env().newTransNoAvg();
89 UserRole.load(transNoAvg, session, UserRole.v2_0_11, new Visitor<UserRole>() {
91 public void visit(UserRole ur) {
92 if(ur.expires().after(now)) {
93 Map<String, Integer> roleCount = theMap.get(ur.ns());
96 roleCount = new TreeMap<>();
97 theMap.put(ur.ns(),roleCount);
100 count = roleCount.get(ur.rname());
105 roleCount.put(ur.rname(), count+1);
110 for(Entry<String, Map<String, Integer>> ns_es : theMap.entrySet()) {
111 for(Entry<String, Integer> r_es : ns_es.getValue().entrySet()) {
112 report.row(ns_es.getKey(),r_es.getKey(),r_es.getValue());
122 protected void _close(AuthzTrans trans) {