2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
24 package org.onap.aaf.auth.batch.helpers;
26 import java.util.Iterator;
28 import java.util.TreeMap;
30 import org.onap.aaf.misc.env.Env;
31 import org.onap.aaf.misc.env.TimeTaken;
32 import org.onap.aaf.misc.env.Trans;
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;
40 public class NS implements Comparable<NS> {
41 public static final Map<String,NS> data = new TreeMap<>();
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;
49 public static Creator<NS> v2_0_11 = new Creator<NS> () {
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));
56 public String select() {
57 return "SELECT name, description, parent, type, scope FROM authz.ns ";
61 public NS(String name, String description, String parent, int type, int scope) {
63 this.description = description;
69 public static void load(Trans trans, Session session, Creator<NS> creator) {
71 "select name, description, parent, type, scope from authz.ns;"
75 public static void loadOne(Trans trans, Session session, Creator<NS> creator, String ns) {
77 ("select name, description, parent, type, scope from authz.ns WHERE name='"+ns+"';")
82 private static void load(Trans trans, Session session, String query, Creator<NS> creator) {
83 trans.info().log( "query: " + query );
87 tt = trans.start("Read Namespaces", Env.REMOTE);
89 Statement stmt = new SimpleStatement( query );
90 results = session.execute(stmt);
97 Iterator<Row> iter = results.iterator();
99 tt = trans.start("Load Namespaces", Env.SUB);
101 while (iter.hasNext()) {
103 NS ns = creator.create(row);
104 data.put(ns.name,ns);
110 trans.info().log("Found",data.size(),"Namespaces");
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);
121 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
122 results = session.execute(stmt);
123 return results.one().getLong(0);
129 public String toString() {
134 * @see java.lang.Object#hashCode()
137 public int hashCode() {
138 return name.hashCode();
142 * @see java.lang.Object#equals(java.lang.Object)
145 public boolean equals(Object obj) {
146 return name.equals(obj);
150 public int compareTo(NS o) {
151 return name.compareTo(o.name);
154 public static class NSSplit {
157 public NSSplit(String s, int dot) {
158 ns = s.substring(0,dot);
159 other = s.substring(dot+1);
162 public static NSSplit deriveParent(String dotted) {
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);