2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright © 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.Iterator;
28 import java.util.List;
30 import java.util.TreeMap;
32 import org.onap.aaf.misc.env.Env;
33 import org.onap.aaf.misc.env.TimeTaken;
34 import org.onap.aaf.misc.env.Trans;
36 import com.datastax.driver.core.ResultSet;
37 import com.datastax.driver.core.Row;
38 import com.datastax.driver.core.Session;
39 import com.datastax.driver.core.SimpleStatement;
40 import com.datastax.driver.core.Statement;
42 public class Perm implements Comparable<Perm> {
43 public static final TreeMap<Perm,Set<String>> data = new TreeMap<>();
44 public static final TreeMap<String,Perm> keys = new TreeMap<>();
45 private static List<Perm> deletePerms = new ArrayList<>();
47 public final String ns;
48 public final String type;
49 public final String instance;
50 public final String action;
51 public final String description;
52 private String fullType = null;
53 private String fullPerm = null;
54 private String encode = null;
55 public final Set<String> roles;
57 public Perm(String ns, String type, String instance, String action, String description, Set<String> roles) {
60 this.instance = instance;
62 this.description = description;
66 public String encode() {
68 encode = ns + '|' + type + '|' + instance + '|' + action;
73 public String fullType() {
75 fullType = ns + '.' + type;
80 public String fullPerm() {
82 fullPerm = ns + '.' + type + '|' + instance + '|' + action;
87 public static void load(Trans trans, Session session) {
88 load(trans, session, "select ns, type, instance, action, description, roles from authz.perm;");
91 public static void loadOneNS(Trans trans, Session session, String ns) {
92 load(trans, session, "select ns, type, instance, action, description, roles from authz.perm WHERE ns='" + ns + "';");
96 private static void load(Trans trans, Session session, String query) {
98 trans.info().log( "query: " + query );
99 TimeTaken tt = trans.start("Read Perms", Env.REMOTE);
102 Statement stmt = new SimpleStatement( query );
103 results = session.execute(stmt);
109 Iterator<Row> iter = results.iterator();
111 tt = trans.start("Load Perms", Env.SUB);
113 while (iter.hasNext()) {
116 row.getString(0),row.getString(1),row.getString(2),
117 row.getString(3), row.getString(4), row.getSet(5,String.class));
118 keys.put(pk.encode(), pk);
119 data.put(pk,pk.roles);
125 trans.info().log("Found",data.size(),"perms");
129 public static long count(Trans trans, Session session) {
130 String query = "select count(*) from authz.perm LIMIT 1000000;";
131 trans.info().log( "query: " + query );
132 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
135 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
136 results = session.execute(stmt);
137 return results.one().getLong(0);
143 public String toString() {
148 * @see java.lang.Object#hashCode()
151 public int hashCode() {
152 return encode().hashCode();
156 * @see java.lang.Object#equals(java.lang.Object)
159 public boolean equals(Object obj) {
160 return encode().equals(obj);
164 public int compareTo(Perm o) {
165 return encode().compareTo(o.encode());
168 public static void stageRemove(Perm p) {
172 public static void executeRemove() {
173 for (Perm p : deletePerms) {
174 keys.remove(p.encode);