AT&T 2.0.19 Code drop, stage 3
[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<Perm,Set<String>>();
42     public static final TreeMap<String,Perm> keys = new TreeMap<String,Perm>();
43         private static List<Perm> deletePerms = new ArrayList<Perm>();
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(row.getString(0),row.getString(1),row.getString(2),row.getString(3), row.getString(4), row.getSet(5,String.class));
110                                 keys.put(pk.encode(), pk);
111                                 data.put(pk,pk.roles);
112                         }
113                 } finally {
114                         tt.done();
115                 }
116         } finally {
117                 trans.info().log("Found",data.size(),"perms");
118         }
119         }
120
121         public static long count(Trans trans, Session session) {
122                 String query = "select count(*) from authz.perm LIMIT 1000000;";
123         trans.info().log( "query: " + query );
124         TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
125         ResultSet results;
126         try {
127                 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
128                 results = session.execute(stmt);
129                 return results.one().getLong(0);
130         } finally {
131                 tt.done();
132         }
133         }
134
135         public String toString() {
136                 return encode();
137         }
138
139         /* (non-Javadoc)
140          * @see java.lang.Object#hashCode()
141          */
142         @Override
143         public int hashCode() {
144                 return encode().hashCode();
145         }
146
147         /* (non-Javadoc)
148          * @see java.lang.Object#equals(java.lang.Object)
149          */
150         @Override
151         public boolean equals(Object obj) {
152                 return encode().equals(obj);
153         }
154
155         @Override
156         public int compareTo(Perm o) {
157                 return encode().compareTo(o.encode());
158         }
159
160         public static void stageRemove(Perm p) {
161                 deletePerms.add(p);
162         }
163         
164         public static void executeRemove() {
165                 for(Perm p : deletePerms) {
166                         keys.remove(p.encode);
167                         data.remove(p);
168                 }
169                 deletePerms.clear();
170         }
171
172 }