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;
36 import com.datastax.driver.core.ResultSet;
37 import com.datastax.driver.core.Row;
38 import com.datastax.driver.core.Session;
39 import com.datastax.driver.core.SimpleStatement;
40 import com.datastax.driver.core.Statement;
42 public class NS implements Comparable<NS> {
43 public static final Map<String,NS> data = new TreeMap<>();
45 public NsDAO.Data ndd;
47 public static Creator<NS> v2_0_11 = new Creator<NS> () {
49 public NS create(Row row) {
50 return new NS(row.getString(0),row.getString(1), row.getString(2),row.getInt(3),row.getInt(4));
54 public String select() {
55 return "SELECT name, description, parent, type, scope FROM authz.ns ";
59 public NS(String name, String description, String parent, int type, int scope) {
60 ndd = new NsDAO.Data();
62 ndd.description = description;
68 public static void load(Trans trans, Session session, Creator<NS> creator) {
70 "select name, description, parent, type, scope from authz.ns;"
72 , v -> data.put(v.ndd.name,v)
76 public static void loadOne(Trans trans, Session session, Creator<NS> creator, String ns) {
78 ("select name, description, parent, type, scope from authz.ns WHERE name='" + ns + "';")
80 , v -> data.put(v.ndd.name,v)
84 public static void load(Trans trans, Session session, Creator<NS> creator, Visitor<NS> visitor) {
85 load(trans,session,creator.query(null),creator, visitor);
88 public void row(final CSV.Writer csvw, String tag) {
89 csvw.row(tag,ndd.name,ndd.type,ndd.parent);
93 private static void load(Trans trans, Session session, String query, Creator<NS> creator, Visitor<NS> visitor) {
94 trans.info().log( "query: " + query );
98 tt = trans.start("Read Namespaces", Env.REMOTE);
100 Statement stmt = new SimpleStatement( query );
101 results = session.execute(stmt);
108 Iterator<Row> iter = results.iterator();
110 tt = trans.start("Load Namespaces", Env.SUB);
112 while (iter.hasNext()) {
114 NS ns = creator.create(row);
121 trans.info().log("Found",data.size(),"Namespaces");
126 public static long count(Trans trans, Session session) {
127 String query = "select count(*) from authz.ns LIMIT 1000000;";
128 trans.info().log( "query: " + query );
129 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
132 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
133 results = session.execute(stmt);
134 return results.one().getLong(0);
140 public String toString() {
145 * @see java.lang.Object#hashCode()
148 public int hashCode() {
149 return ndd.name.hashCode();
153 * @see java.lang.Object#equals(java.lang.Object)
156 public boolean equals(Object obj) {
157 return ndd.name.equals(obj);
161 public int compareTo(NS o) {
162 return ndd.name.compareTo(o.ndd.name);
165 public static class NSSplit {
168 public NSSplit(String s, int dot) {
169 ns = s.substring(0,dot);
170 other = s.substring(dot + 1);
173 public static NSSplit deriveParent(String dotted) {
177 for (int idx = dotted.lastIndexOf('.');idx >= 0; idx = dotted.lastIndexOf('.',idx - 1)) {
178 if (data.get(dotted.substring(0, idx)) != null) {
179 return new NSSplit(dotted,idx);