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