2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.batch.helpers;
24 import java.util.Iterator;
26 import java.util.TreeMap;
28 import org.onap.aaf.misc.env.Env;
29 import org.onap.aaf.misc.env.TimeTaken;
30 import org.onap.aaf.misc.env.Trans;
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;
38 public class NS implements Comparable<NS> {
39 public final static Map<String,NS> data = new TreeMap<>();
41 public final String name, description, parent;
42 public final int scope,type;
44 public NS(String name, String description, String parent, int type, int scope) {
46 this.description = description;
52 public static void load(Trans trans, Session session, Creator<NS> creator) {
54 "select name, description, parent, type, scope from authz.ns;"
58 public static void loadOne(Trans trans, Session session, Creator<NS> creator, String ns) {
60 ("select name, description, parent, type, scope from authz.ns WHERE name='"+ns+"';")
65 private static void load(Trans trans, Session session, String query, Creator<NS> creator) {
66 trans.info().log( "query: " + query );
70 tt = trans.start("Read Namespaces", Env.REMOTE);
72 Statement stmt = new SimpleStatement( query );
73 results = session.execute(stmt);
80 Iterator<Row> iter = results.iterator();
82 tt = trans.start("Load Namespaces", Env.SUB);
84 while (iter.hasNext()) {
86 NS ns = creator.create(row);
93 trans.info().log("Found",data.size(),"Namespaces");
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);
104 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
105 results = session.execute(stmt);
106 return results.one().getLong(0);
112 public String toString() {
117 * @see java.lang.Object#hashCode()
120 public int hashCode() {
121 return name.hashCode();
125 * @see java.lang.Object#equals(java.lang.Object)
128 public boolean equals(Object obj) {
129 return name.equals(obj);
133 public int compareTo(NS o) {
134 return name.compareTo(o.name);
137 public static class NSSplit {
140 public NSSplit(String s, int dot) {
141 ns = s.substring(0,dot);
142 other = s.substring(dot+1);
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);
155 public static Creator<NS> v2_0_11 = new Creator<NS> () {
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));
162 public String select() {
163 return "SELECT name, description, parent, type, scope FROM authz.ns ";