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.SortedMap;
32 import java.util.TreeMap;
34 import org.onap.aaf.auth.dao.cass.RoleDAO;
35 import org.onap.aaf.misc.env.Env;
36 import org.onap.aaf.misc.env.TimeTaken;
37 import org.onap.aaf.misc.env.Trans;
39 import com.datastax.driver.core.ResultSet;
40 import com.datastax.driver.core.Row;
41 import com.datastax.driver.core.Session;
42 import com.datastax.driver.core.SimpleStatement;
43 import com.datastax.driver.core.Statement;
45 public class Role implements Comparable<Role> {
46 public static final SortedMap<Role,Set<String>> data = new TreeMap<>();
47 public static final SortedMap<String,Role> keys = new TreeMap<>();
48 public static final SortedMap<String,Role> byName = new TreeMap<>();
49 private static List<Role> deleteRoles = new ArrayList<>();
51 public RoleDAO.Data rdd;
53 private String encode;
55 public Role(String full) {
56 rdd = new RoleDAO.Data();
60 rdd.perms = new HashSet<>();
64 public Role(String ns, String name, String description,Set<String> perms) {
65 rdd = new RoleDAO.Data();
68 rdd.description = description;
74 public String encode() {
76 encode = rdd.ns + '|' + rdd.name;
81 public String fullName() {
83 full = rdd.ns + '.' + rdd.name;
88 public static void load(Trans trans, Session session ) {
89 load(trans,session,"select ns, name, description, perms from authz.role;");
92 public static void loadOneNS(Trans trans, Session session, String ns ) {
93 load(trans,session,"select ns, name, description, perms from authz.role WHERE ns='" + ns + "';");
96 private static void load(Trans trans, Session session, String query) {
97 trans.info().log( "query: " + query );
98 TimeTaken tt = trans.start("Read Roles", Env.REMOTE);
102 Statement stmt = new SimpleStatement( query );
103 results = session.execute(stmt);
109 Iterator<Row> iter = results.iterator();
111 tt = trans.start("Load Roles", Env.SUB);
113 while (iter.hasNext()) {
115 Role rk =new Role(row.getString(0),row.getString(1), row.getString(2),row.getSet(3,String.class));
116 keys.put(rk.encode(), rk);
117 data.put(rk,rk.rdd.perms);
118 byName.put(rk.fullName(), rk);
124 trans.info().log("Found",data.size(),"roles");
128 public static long count(Trans trans, Session session) {
129 String query = "select count(*) from authz.role LIMIT 1000000;";
130 trans.info().log( "query: " + query );
131 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
134 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
135 results = session.execute(stmt);
136 return results.one().getLong(0);
142 public String toString() {
147 * @see java.lang.Object#hashCode()
150 public int hashCode() {
151 return encode().hashCode();
155 * @see java.lang.Object#equals(java.lang.Object)
158 public boolean equals(Object obj) {
159 return encode().equals(obj);
163 public int compareTo(Role o) {
164 return encode().compareTo(o.encode());
167 public static String fullName(String role) {
168 return role.replace('|', '.');
171 public static void stageRemove(Role r) {
175 public static void executeRemove() {
176 for (Role p : deleteRoles) {
177 keys.remove(p.encode);
183 public static void clear() {