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