6d87dedc06e315fcce580c3885d6975206f0506a
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / Role.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.batch.helpers;
25
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.TreeMap;
32
33 import org.onap.aaf.misc.env.Env;
34 import org.onap.aaf.misc.env.TimeTaken;
35 import org.onap.aaf.misc.env.Trans;
36
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;
42
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<>();
48
49     public final String ns;
50     public final String name;
51     public final String description;
52     private String full;
53     private String encode;
54     public final Set<String> perms;
55     
56     public Role(String full) {
57         ns = name = description = "";
58         this.full = full;
59         perms = new HashSet<>();
60     }
61     
62     public Role(String ns, String name, String description,Set<String> perms) {
63         this.ns = ns;
64         this.name = name;
65         this.description = description;
66         this.full = null;
67         this.encode = null;
68         this.perms = perms;
69     }
70     
71     public String encode() {
72         if (encode==null) {
73             encode = ns + '|' + name;
74         } 
75         return encode;
76     }
77
78     public String fullName() {
79         if (full==null) {
80             full = ns + '.' + name;
81         } 
82         return full;
83     }
84
85     public static void load(Trans trans, Session session ) {
86         load(trans,session,"select ns, name, description, perms from authz.role;");
87     }
88
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 + "';");
91     }
92
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);
96        
97         ResultSet results;
98         try {
99             Statement stmt = new SimpleStatement( query );
100             results = session.execute(stmt);
101         } finally {
102             tt.done();
103         }
104
105         try {
106             Iterator<Row> iter = results.iterator();
107             Row row;
108             tt = trans.start("Load Roles", Env.SUB);
109             try {
110                 while (iter.hasNext()) {
111                     row = iter.next();
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);
116                 }
117             } finally {
118                 tt.done();
119             }
120         } finally {
121             trans.info().log("Found",data.size(),"roles");
122         }
123     }
124     
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);
129         ResultSet results;
130         try {
131             Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
132             results = session.execute(stmt);
133             return results.one().getLong(0);
134         } finally {
135             tt.done();
136         }
137     }
138
139     public String toString() {
140         return encode();
141     }
142
143     /* (non-Javadoc)
144      * @see java.lang.Object#hashCode()
145      */
146     @Override
147     public int hashCode() {
148         return encode().hashCode();
149     }
150
151     /* (non-Javadoc)
152      * @see java.lang.Object#equals(java.lang.Object)
153      */
154     @Override
155     public boolean equals(Object obj) {
156         return encode().equals(obj);
157     }
158
159     @Override
160     public int compareTo(Role o) {
161         return encode().compareTo(o.encode());
162     }
163
164     public static String fullName(String role) {
165         return role.replace('|', '.');
166     }
167     
168     public static void stageRemove(Role r) {
169         deleteRoles.add(r);
170     }
171     
172     public static void executeRemove() {
173         for (Role p : deleteRoles) {
174             keys.remove(p.encode);
175             data.remove(p);
176         }
177         deleteRoles.clear();
178     }
179
180 }