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.auth.dao.cass.NsDAO;
31 import org.onap.aaf.cadi.util.CSV;
32 import org.onap.aaf.misc.env.Env;
33 import org.onap.aaf.misc.env.TimeTaken;
34 import org.onap.aaf.misc.env.Trans;
35 import org.onap.aaf.misc.env.util.Chrono;
37 import com.datastax.driver.core.ResultSet;
38 import com.datastax.driver.core.Row;
39 import com.datastax.driver.core.Session;
40 import com.datastax.driver.core.SimpleStatement;
41 import com.datastax.driver.core.Statement;
43 public class NS implements Comparable<NS> {
44 public static final Map<String,NS> data = new TreeMap<>();
46 public NsDAO.Data ndd;
48 public static Creator<NS> v2_0_11 = new Creator<NS> () {
50 public NS create(Row row) {
51 return new NS(row.getString(0),row.getString(1), row.getString(2),row.getInt(3),row.getInt(4));
55 public String select() {
56 return "SELECT name, description, parent, type, scope FROM authz.ns ";
60 public NS(String name, String description, String parent, int type, int scope) {
61 ndd = new NsDAO.Data();
63 ndd.description = description;
69 public static void load(Trans trans, Session session, Creator<NS> creator) {
71 "select name, description, parent, type, scope from authz.ns;"
73 , v -> data.put(v.ndd.name,v)
77 public static void loadOne(Trans trans, Session session, Creator<NS> creator, String ns) {
79 ("select name, description, parent, type, scope from authz.ns WHERE name='"+ns+"';")
81 , v -> data.put(v.ndd.name,v)
85 public static void load(Trans trans, Session session, Creator<NS> creator, Visitor<NS> visitor) {
86 load(trans,session,creator.query(null),creator, visitor);
89 public void row(final CSV.Writer csvw, String tag) {
90 csvw.row(tag,ndd.name,ndd.type,ndd.parent);
94 private static void load(Trans trans, Session session, String query, Creator<NS> creator, Visitor<NS> visitor) {
95 trans.info().log( "query: " + query );
99 tt = trans.start("Read Namespaces", Env.REMOTE);
101 Statement stmt = new SimpleStatement( query );
102 results = session.execute(stmt);
109 Iterator<Row> iter = results.iterator();
111 tt = trans.start("Load Namespaces", Env.SUB);
113 while (iter.hasNext()) {
115 NS ns = creator.create(row);
122 trans.info().log("Found",data.size(),"Namespaces");
127 public static long count(Trans trans, Session session) {
128 String query = "select count(*) from authz.ns LIMIT 1000000;";
129 trans.info().log( "query: " + query );
130 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
133 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
134 results = session.execute(stmt);
135 return results.one().getLong(0);
141 public String toString() {
146 * @see java.lang.Object#hashCode()
149 public int hashCode() {
150 return ndd.name.hashCode();
154 * @see java.lang.Object#equals(java.lang.Object)
157 public boolean equals(Object obj) {
158 return ndd.name.equals(obj);
162 public int compareTo(NS o) {
163 return ndd.name.compareTo(o.ndd.name);
166 public static class NSSplit {
169 public NSSplit(String s, int dot) {
170 ns = s.substring(0,dot);
171 other = s.substring(dot+1);
174 public static NSSplit deriveParent(String dotted) {
178 for (int idx = dotted.lastIndexOf('.');idx>=0; idx=dotted.lastIndexOf('.',idx-1)) {
179 if (data.get(dotted.substring(0, idx))!=null) {
180 return new NSSplit(dotted,idx);