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