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