a61619eae685721f2c4139505b4b402f421ea7a6
[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.SortedMap;
32 import java.util.TreeMap;
33
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;
38
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;
44
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<>();
50
51     public RoleDAO.Data rdd;
52     private String full;
53     private String encode;
54
55     public Role(String full) {
56         rdd = new RoleDAO.Data();
57         rdd.ns = "";
58         rdd.name = "";
59         rdd.description = "";
60         rdd.perms = new HashSet<>();
61         this.full = full;
62     }
63
64     public Role(String ns, String name, String description,Set<String> perms) {
65            rdd = new RoleDAO.Data();
66         rdd.ns = ns;
67         rdd.name = name;
68         rdd.description = description;
69         rdd.perms = perms;
70         this.full = null;
71         this.encode = null;
72     }
73
74     public String encode() {
75         if (encode==null) {
76             encode = rdd.ns + '|' + rdd.name;
77         }
78         return encode;
79     }
80
81     public String fullName() {
82         if (full==null) {
83             full = rdd.ns + '.' + rdd.name;
84         }
85         return full;
86     }
87
88     public static void load(Trans trans, Session session ) {
89         load(trans,session,"select ns, name, description, perms from authz.role;");
90     }
91
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 + "';");
94     }
95
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);
99
100         ResultSet results;
101         try {
102             Statement stmt = new SimpleStatement( query );
103             results = session.execute(stmt);
104         } finally {
105             tt.done();
106         }
107
108         try {
109             Iterator<Row> iter = results.iterator();
110             Row row;
111             tt = trans.start("Load Roles", Env.SUB);
112             try {
113                 while (iter.hasNext()) {
114                     row = iter.next();
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);
119                 }
120             } finally {
121                 tt.done();
122             }
123         } finally {
124             trans.info().log("Found",data.size(),"roles");
125         }
126     }
127
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);
132         ResultSet results;
133         try {
134             Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
135             results = session.execute(stmt);
136             return results.one().getLong(0);
137         } finally {
138             tt.done();
139         }
140     }
141
142     public String toString() {
143         return encode();
144     }
145
146     /* (non-Javadoc)
147      * @see java.lang.Object#hashCode()
148      */
149     @Override
150     public int hashCode() {
151         return encode().hashCode();
152     }
153
154     /* (non-Javadoc)
155      * @see java.lang.Object#equals(java.lang.Object)
156      */
157     @Override
158     public boolean equals(Object obj) {
159         return encode().equals(obj);
160     }
161
162     @Override
163     public int compareTo(Role o) {
164         return encode().compareTo(o.encode());
165     }
166
167     public static String fullName(String role) {
168         return role.replace('|', '.');
169     }
170
171     public static void stageRemove(Role r) {
172         deleteRoles.add(r);
173     }
174
175     public static void executeRemove() {
176         for (Role p : deleteRoles) {
177             keys.remove(p.encode);
178             data.remove(p);
179         }
180         deleteRoles.clear();
181     }
182
183     public static void clear() {
184         data.clear();
185         keys.clear();
186         byName.clear();
187         deleteRoles.clear();
188     }
189
190 }