AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / 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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.helpers;
23
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.TreeMap;
30
31 import org.onap.aaf.misc.env.Env;
32 import org.onap.aaf.misc.env.TimeTaken;
33 import org.onap.aaf.misc.env.Trans;
34
35 import com.datastax.driver.core.ResultSet;
36 import com.datastax.driver.core.Row;
37 import com.datastax.driver.core.Session;
38 import com.datastax.driver.core.SimpleStatement;
39 import com.datastax.driver.core.Statement;
40
41 public class Role implements Comparable<Role> {
42     public static final TreeMap<Role,Set<String>> data = new TreeMap<Role,Set<String>>();
43     public static final TreeMap<String,Role> keys = new TreeMap<String,Role>();
44     public static final TreeMap<String,Role> byName = new TreeMap<String,Role>();
45         private static List<Role> deleteRoles = new ArrayList<Role>();
46
47         public final String ns, name, description;
48         private String full, encode;
49         public final Set<String> perms;
50         
51         public Role(String full) {
52                 ns = name = description = "";
53                 this.full = full;
54                 perms = new HashSet<String>();
55         }
56         
57         public Role(String ns, String name, String description,Set<String> perms) {
58                 this.ns = ns;
59                 this.name = name;
60                 this.description = description;
61                 this.full = null;
62                 this.encode = null;
63                 this.perms = perms;
64         }
65         
66         public String encode() {
67                 if(encode==null) {
68                         encode = ns + '|' + name;
69                 } 
70                 return encode;
71         }
72
73         public String fullName() {
74                 if(full==null) {
75                         full = ns + '.' + name;
76                 } 
77                 return full;
78         }
79
80         public static void load(Trans trans, Session session ) {
81                 load(trans,session,"select ns, name, description, perms from authz.role;");
82         }
83
84         public static void loadOneNS(Trans trans, Session session, String ns ) {
85                 load(trans,session,"select ns, name, description, perms from authz.role WHERE ns='" + ns + "';");
86         }
87
88         private static void load(Trans trans, Session session, String query) {
89         trans.info().log( "query: " + query );
90         TimeTaken tt = trans.start("Read Roles", Env.REMOTE);
91        
92         ResultSet results;
93                 try {
94                 Statement stmt = new SimpleStatement( query );
95                 results = session.execute(stmt);
96         } finally {
97                 tt.done();
98         }
99
100         try {
101                 Iterator<Row> iter = results.iterator();
102                 Row row;
103                 tt = trans.start("Load Roles", Env.SUB);
104                 try {
105                         while(iter.hasNext()) {
106                                 row = iter.next();
107                                 Role rk =new Role(row.getString(0),row.getString(1), row.getString(2),row.getSet(3,String.class));
108                                 keys.put(rk.encode(), rk);
109                                 data.put(rk,rk.perms);
110                                 byName.put(rk.fullName(), rk);
111                         }
112                 } finally {
113                         tt.done();
114                 }
115         } finally {
116                 trans.info().log("Found",data.size(),"roles");
117         }
118         }
119         
120         public static long count(Trans trans, Session session) {
121                 String query = "select count(*) from authz.role LIMIT 1000000;";
122         trans.info().log( "query: " + query );
123         TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
124         ResultSet results;
125         try {
126                 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
127                 results = session.execute(stmt);
128                 return results.one().getLong(0);
129         } finally {
130                 tt.done();
131         }
132         }
133
134         public String toString() {
135                 return encode();
136         }
137
138         /* (non-Javadoc)
139          * @see java.lang.Object#hashCode()
140          */
141         @Override
142         public int hashCode() {
143                 return encode().hashCode();
144         }
145
146         /* (non-Javadoc)
147          * @see java.lang.Object#equals(java.lang.Object)
148          */
149         @Override
150         public boolean equals(Object obj) {
151                 return encode().equals(obj);
152         }
153
154         @Override
155         public int compareTo(Role o) {
156                 return encode().compareTo(o.encode());
157         }
158
159         public static String fullName(String role) {
160                 return role.replace('|', '.');
161         }
162         
163         public static void stageRemove(Role r) {
164                 deleteRoles.add(r);
165         }
166         
167         public static void executeRemove() {
168                 for(Role p : deleteRoles) {
169                         keys.remove(p.encode);
170                         data.remove(p);
171                 }
172                 deleteRoles.clear();
173         }
174
175 }