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.Iterator;
26 import java.util.List;
28 import java.util.TreeMap;
30 import org.onap.aaf.misc.env.Env;
31 import org.onap.aaf.misc.env.TimeTaken;
32 import org.onap.aaf.misc.env.Trans;
34 import com.datastax.driver.core.ResultSet;
35 import com.datastax.driver.core.Row;
36 import com.datastax.driver.core.Session;
37 import com.datastax.driver.core.SimpleStatement;
38 import com.datastax.driver.core.Statement;
40 public class Perm implements Comparable<Perm> {
41 public static final TreeMap<Perm,Set<String>> data = new TreeMap<>();
42 public static final TreeMap<String,Perm> keys = new TreeMap<>();
43 private static List<Perm> deletePerms = new ArrayList<>();
45 public final String ns, type, instance, action,description;
46 private String fullType = null, fullPerm = null, encode = null;
47 public final Set<String> roles;
49 public String encode() {
51 encode = ns + '|' + type + '|' + instance + '|' + action;
56 public String fullType() {
58 fullType = ns + '.' + type;
63 public String fullPerm() {
65 fullPerm = ns + '.' + type + '|' + instance + '|' + action;
70 public Perm(String ns, String type, String instance, String action, String description, Set<String> roles) {
73 this.instance = instance;
75 this.description = description;
77 // this.full = encode();//ns+'.'+type+'|'+instance+'|'+action;
81 public static void load(Trans trans, Session session) {
82 load(trans, session, "select ns, type, instance, action, description, roles from authz.perm;");
85 public static void loadOneNS(Trans trans, Session session, String ns) {
86 load(trans, session, "select ns, type, instance, action, description, roles from authz.perm WHERE ns='" + ns + "';");
90 private static void load(Trans trans, Session session, String query) {
92 trans.info().log( "query: " + query );
93 TimeTaken tt = trans.start("Read Perms", Env.REMOTE);
96 Statement stmt = new SimpleStatement( query );
97 results = session.execute(stmt);
103 Iterator<Row> iter = results.iterator();
105 tt = trans.start("Load Perms", Env.SUB);
107 while (iter.hasNext()) {
110 row.getString(0),row.getString(1),row.getString(2),
111 row.getString(3), row.getString(4), row.getSet(5,String.class));
112 keys.put(pk.encode(), pk);
113 data.put(pk,pk.roles);
119 trans.info().log("Found",data.size(),"perms");
123 public static long count(Trans trans, Session session) {
124 String query = "select count(*) from authz.perm LIMIT 1000000;";
125 trans.info().log( "query: " + query );
126 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
129 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
130 results = session.execute(stmt);
131 return results.one().getLong(0);
137 public String toString() {
142 * @see java.lang.Object#hashCode()
145 public int hashCode() {
146 return encode().hashCode();
150 * @see java.lang.Object#equals(java.lang.Object)
153 public boolean equals(Object obj) {
154 return encode().equals(obj);
158 public int compareTo(Perm o) {
159 return encode().compareTo(o.encode());
162 public static void stageRemove(Perm p) {
166 public static void executeRemove() {
167 for (Perm p : deletePerms) {
168 keys.remove(p.encode);