Upgrade to latest oparent
[aaf/authz.git] / authz-batch / src / main / java / com / att / authz / helpers / Perm.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.helpers;
5
6 import java.util.Iterator;
7 import java.util.Set;
8 import java.util.TreeMap;
9
10 import org.onap.aaf.inno.env.Env;
11 import org.onap.aaf.inno.env.TimeTaken;
12 import org.onap.aaf.inno.env.Trans;
13 import com.datastax.driver.core.ResultSet;
14 import com.datastax.driver.core.Row;
15 import com.datastax.driver.core.Session;
16 import com.datastax.driver.core.SimpleStatement;
17 import com.datastax.driver.core.Statement;
18
19 public class Perm implements Comparable<Perm> {
20     public static final TreeMap<Perm,Set<String>> data = new TreeMap<Perm,Set<String>>();
21     public static final TreeMap<String,Perm> keys = new TreeMap<String,Perm>();
22
23         public final String ns, type, instance, action,description;
24         private String fullType = null, fullPerm = null, encode = null;
25         public final Set<String> roles;
26         
27         public String encode() {
28                 if(encode == null) {
29                         encode = ns + '|' + type + '|' + instance + '|' + action;
30                 }
31                 return encode;
32         }
33         
34         public String fullType() {
35                 if(fullType==null) {
36                         fullType = ns + '.' + type;
37                 }
38                 return fullType;
39         }
40         
41         public String fullPerm() {
42                 if(fullPerm==null) {
43                         fullPerm = ns + '.' + type  + '|' + instance + '|' + action;
44                 }
45                 return fullPerm;
46         }
47         
48         public Perm(String ns, String type, String instance, String action, String description, Set<String> roles) {
49                 this.ns = ns;
50                 this.type = type;
51                 this.instance = instance;
52                 this.action = action;
53                 this.description = description;
54                 // 2.0.11
55 //              this.full = encode();//ns+'.'+type+'|'+instance+'|'+action;
56                 this.roles = roles;
57         }
58
59         public static void load(Trans trans, Session session) {
60         load(trans, session, "select ns, type, instance, action, description, roles from authz.perm;");
61         }
62         
63         public static void loadOneNS(Trans trans, Session session, String ns) {
64         load(trans, session, "select ns, type, instance, action, description, roles from authz.perm WHERE ns='" + ns + "';");
65         
66         }
67
68         private static void load(Trans trans, Session session, String query) {
69         //
70         trans.info().log( "query: " + query );
71         TimeTaken tt = trans.start("Read Perms", Env.REMOTE);
72         ResultSet results;
73                 try {
74                 Statement stmt = new SimpleStatement( query );
75                 results = session.execute(stmt);
76         } finally {
77                 tt.done();
78         }
79
80         try {
81                 Iterator<Row> iter = results.iterator();
82                 Row row;
83                 tt = trans.start("Load Perms", Env.SUB);
84                 try {
85                         while(iter.hasNext()) {
86                                 row = iter.next();
87                                 Perm pk = new Perm(row.getString(0),row.getString(1),row.getString(2),row.getString(3), row.getString(4), row.getSet(5,String.class));
88                                 keys.put(pk.encode(), pk);
89                                 data.put(pk,pk.roles);
90                         }
91                 } finally {
92                         tt.done();
93                 }
94         } finally {
95                 trans.info().log("Found",data.size(),"perms");
96         }
97         }
98
99         public String toString() {
100                 return encode();
101         }
102
103         /* (non-Javadoc)
104          * @see java.lang.Object#hashCode()
105          */
106         @Override
107         public int hashCode() {
108                 return encode().hashCode();
109         }
110
111         /* (non-Javadoc)
112          * @see java.lang.Object#equals(java.lang.Object)
113          */
114         @Override
115         public boolean equals(Object obj) {
116                 return encode().equals(obj);
117         }
118
119         @Override
120         public int compareTo(Perm o) {
121                 return encode().compareTo(o.encode());
122         }
123
124 }