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.helpers;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
29 import java.util.TreeMap;
31 import org.onap.aaf.misc.env.Env;
32 import org.onap.aaf.misc.env.TimeTaken;
33 import org.onap.aaf.misc.env.Trans;
35 import com.datastax.driver.core.ResultSet;
36 import com.datastax.driver.core.Row;
37 import com.datastax.driver.core.Session;
38 import com.datastax.driver.core.SimpleStatement;
39 import com.datastax.driver.core.Statement;
41 public class Role implements Comparable<Role> {
42 public static final TreeMap<Role,Set<String>> data = new TreeMap<>();
43 public static final TreeMap<String,Role> keys = new TreeMap<>();
44 public static final TreeMap<String,Role> byName = new TreeMap<>();
45 private static List<Role> deleteRoles = new ArrayList<>();
47 public final String ns, name, description;
48 private String full, encode;
49 public final Set<String> perms;
51 public Role(String full) {
52 ns = name = description = "";
54 perms = new HashSet<>();
57 public Role(String ns, String name, String description,Set<String> perms) {
60 this.description = description;
66 public String encode() {
68 encode = ns + '|' + name;
73 public String fullName() {
75 full = ns + '.' + name;
80 public static void load(Trans trans, Session session ) {
81 load(trans,session,"select ns, name, description, perms from authz.role;");
84 public static void loadOneNS(Trans trans, Session session, String ns ) {
85 load(trans,session,"select ns, name, description, perms from authz.role WHERE ns='" + ns + "';");
88 private static void load(Trans trans, Session session, String query) {
89 trans.info().log( "query: " + query );
90 TimeTaken tt = trans.start("Read Roles", Env.REMOTE);
94 Statement stmt = new SimpleStatement( query );
95 results = session.execute(stmt);
101 Iterator<Row> iter = results.iterator();
103 tt = trans.start("Load Roles", Env.SUB);
105 while (iter.hasNext()) {
107 Role rk =new Role(row.getString(0),row.getString(1), row.getString(2),row.getSet(3,String.class));
108 keys.put(rk.encode(), rk);
109 data.put(rk,rk.perms);
110 byName.put(rk.fullName(), rk);
116 trans.info().log("Found",data.size(),"roles");
120 public static long count(Trans trans, Session session) {
121 String query = "select count(*) from authz.role LIMIT 1000000;";
122 trans.info().log( "query: " + query );
123 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
126 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
127 results = session.execute(stmt);
128 return results.one().getLong(0);
134 public String toString() {
139 * @see java.lang.Object#hashCode()
142 public int hashCode() {
143 return encode().hashCode();
147 * @see java.lang.Object#equals(java.lang.Object)
150 public boolean equals(Object obj) {
151 return encode().equals(obj);
155 public int compareTo(Role o) {
156 return encode().compareTo(o.encode());
159 public static String fullName(String role) {
160 return role.replace('|', '.');
163 public static void stageRemove(Role r) {
167 public static void executeRemove() {
168 for (Role p : deleteRoles) {
169 keys.remove(p.encode);