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