2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2018 IBM.
8 * ===========================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END====================================================
24 package org.onap.aaf.auth.batch.helpers;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.List;
31 import java.util.TreeMap;
33 import org.onap.aaf.misc.env.Env;
34 import org.onap.aaf.misc.env.TimeTaken;
35 import org.onap.aaf.misc.env.Trans;
37 import com.datastax.driver.core.ResultSet;
38 import com.datastax.driver.core.Row;
39 import com.datastax.driver.core.Session;
40 import com.datastax.driver.core.SimpleStatement;
41 import com.datastax.driver.core.Statement;
43 public class Role implements Comparable<Role> {
44 public static final TreeMap<Role,Set<String>> data = new TreeMap<>();
45 public static final TreeMap<String,Role> keys = new TreeMap<>();
46 public static final TreeMap<String,Role> byName = new TreeMap<>();
47 private static List<Role> deleteRoles = new ArrayList<>();
49 public final String ns;
50 public final String name;
51 public final String description;
53 private String encode;
54 public final Set<String> perms;
56 public Role(String full) {
57 ns = name = description = "";
59 perms = new HashSet<>();
62 public Role(String ns, String name, String description,Set<String> perms) {
65 this.description = description;
71 public String encode() {
73 encode = ns + '|' + name;
78 public String fullName() {
80 full = ns + '.' + name;
85 public static void load(Trans trans, Session session ) {
86 load(trans,session,"select ns, name, description, perms from authz.role;");
89 public static void loadOneNS(Trans trans, Session session, String ns ) {
90 load(trans,session,"select ns, name, description, perms from authz.role WHERE ns='" + ns + "';");
93 private static void load(Trans trans, Session session, String query) {
94 trans.info().log( "query: " + query );
95 TimeTaken tt = trans.start("Read Roles", Env.REMOTE);
99 Statement stmt = new SimpleStatement( query );
100 results = session.execute(stmt);
106 Iterator<Row> iter = results.iterator();
108 tt = trans.start("Load Roles", Env.SUB);
110 while (iter.hasNext()) {
112 Role rk =new Role(row.getString(0),row.getString(1), row.getString(2),row.getSet(3,String.class));
113 keys.put(rk.encode(), rk);
114 data.put(rk,rk.perms);
115 byName.put(rk.fullName(), rk);
121 trans.info().log("Found",data.size(),"roles");
125 public static long count(Trans trans, Session session) {
126 String query = "select count(*) from authz.role LIMIT 1000000;";
127 trans.info().log( "query: " + query );
128 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
131 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
132 results = session.execute(stmt);
133 return results.one().getLong(0);
139 public String toString() {
144 * @see java.lang.Object#hashCode()
147 public int hashCode() {
148 return encode().hashCode();
152 * @see java.lang.Object#equals(java.lang.Object)
155 public boolean equals(Object obj) {
156 return encode().equals(obj);
160 public int compareTo(Role o) {
161 return encode().compareTo(o.encode());
164 public static String fullName(String role) {
165 return role.replace('|', '.');
168 public static void stageRemove(Role r) {
172 public static void executeRemove() {
173 for (Role p : deleteRoles) {
174 keys.remove(p.encode);