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.auth.dao.cass.RoleDAO;
34 import org.onap.aaf.misc.env.Env;
35 import org.onap.aaf.misc.env.TimeTaken;
36 import org.onap.aaf.misc.env.Trans;
38 import com.datastax.driver.core.ResultSet;
39 import com.datastax.driver.core.Row;
40 import com.datastax.driver.core.Session;
41 import com.datastax.driver.core.SimpleStatement;
42 import com.datastax.driver.core.Statement;
44 public class Role implements Comparable<Role> {
45 public static final TreeMap<Role,Set<String>> data = new TreeMap<>();
46 public static final TreeMap<String,Role> keys = new TreeMap<>();
47 public static final TreeMap<String,Role> byName = new TreeMap<>();
48 private static List<Role> deleteRoles = new ArrayList<>();
50 public RoleDAO.Data rdd;
52 private String encode;
54 public Role(String full) {
55 rdd = new RoleDAO.Data();
59 rdd.perms = new HashSet<>();
63 public Role(String ns, String name, String description,Set<String> perms) {
64 rdd = new RoleDAO.Data();
67 rdd.description = description;
73 public String encode() {
75 encode = rdd.ns + '|' + rdd.name;
80 public String fullName() {
82 full = rdd.ns + '.' + rdd.name;
87 public static void load(Trans trans, Session session ) {
88 load(trans,session,"select ns, name, description, perms from authz.role;");
91 public static void loadOneNS(Trans trans, Session session, String ns ) {
92 load(trans,session,"select ns, name, description, perms from authz.role WHERE ns='" + ns + "';");
95 private static void load(Trans trans, Session session, String query) {
96 trans.info().log( "query: " + query );
97 TimeTaken tt = trans.start("Read Roles", Env.REMOTE);
101 Statement stmt = new SimpleStatement( query );
102 results = session.execute(stmt);
108 Iterator<Row> iter = results.iterator();
110 tt = trans.start("Load Roles", Env.SUB);
112 while (iter.hasNext()) {
114 Role rk =new Role(row.getString(0),row.getString(1), row.getString(2),row.getSet(3,String.class));
115 keys.put(rk.encode(), rk);
116 data.put(rk,rk.rdd.perms);
117 byName.put(rk.fullName(), rk);
123 trans.info().log("Found",data.size(),"roles");
127 public static long count(Trans trans, Session session) {
128 String query = "select count(*) from authz.role LIMIT 1000000;";
129 trans.info().log( "query: " + query );
130 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
133 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
134 results = session.execute(stmt);
135 return results.one().getLong(0);
141 public String toString() {
146 * @see java.lang.Object#hashCode()
149 public int hashCode() {
150 return encode().hashCode();
154 * @see java.lang.Object#equals(java.lang.Object)
157 public boolean equals(Object obj) {
158 return encode().equals(obj);
162 public int compareTo(Role o) {
163 return encode().compareTo(o.encode());
166 public static String fullName(String role) {
167 return role.replace('|', '.');
170 public static void stageRemove(Role r) {
174 public static void executeRemove() {
175 for (Role p : deleteRoles) {
176 keys.remove(p.encode);
182 public static void clear() {