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.misc.env.Env;
32 import org.onap.aaf.misc.env.TimeTaken;
33 import org.onap.aaf.misc.env.Trans;
35 import com.datastax.driver.core.ResultSet;
36 import com.datastax.driver.core.Row;
37 import com.datastax.driver.core.Session;
38 import com.datastax.driver.core.SimpleStatement;
39 import com.datastax.driver.core.Statement;
41 public class NS implements Comparable<NS> {
42 public static final Map<String,NS> data = new TreeMap<>();
44 public NsDAO.Data ndd;
46 public static Creator<NS> v2_0_11 = new Creator<NS> () {
48 public NS create(Row row) {
49 return new NS(row.getString(0),row.getString(1), row.getString(2),row.getInt(3),row.getInt(4));
53 public String select() {
54 return "SELECT name, description, parent, type, scope FROM authz.ns ";
58 public NS(String name, String description, String parent, int type, int scope) {
59 ndd = new NsDAO.Data();
61 ndd.description = description;
67 public static void load(Trans trans, Session session, Creator<NS> creator) {
69 "select name, description, parent, type, scope from authz.ns;"
73 public static void loadOne(Trans trans, Session session, Creator<NS> creator, String ns) {
75 ("select name, description, parent, type, scope from authz.ns WHERE name='"+ns+"';")
80 private static void load(Trans trans, Session session, String query, Creator<NS> creator) {
81 trans.info().log( "query: " + query );
85 tt = trans.start("Read Namespaces", Env.REMOTE);
87 Statement stmt = new SimpleStatement( query );
88 results = session.execute(stmt);
95 Iterator<Row> iter = results.iterator();
97 tt = trans.start("Load Namespaces", Env.SUB);
99 while (iter.hasNext()) {
101 NS ns = creator.create(row);
102 data.put(ns.ndd.name,ns);
108 trans.info().log("Found",data.size(),"Namespaces");
113 public static long count(Trans trans, Session session) {
114 String query = "select count(*) from authz.ns LIMIT 1000000;";
115 trans.info().log( "query: " + query );
116 TimeTaken tt = trans.start("Count Namespaces", Env.REMOTE);
119 Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
120 results = session.execute(stmt);
121 return results.one().getLong(0);
127 public String toString() {
132 * @see java.lang.Object#hashCode()
135 public int hashCode() {
136 return ndd.name.hashCode();
140 * @see java.lang.Object#equals(java.lang.Object)
143 public boolean equals(Object obj) {
144 return ndd.name.equals(obj);
148 public int compareTo(NS o) {
149 return ndd.name.compareTo(o.ndd.name);
152 public static class NSSplit {
155 public NSSplit(String s, int dot) {
156 ns = s.substring(0,dot);
157 other = s.substring(dot+1);
160 public static NSSplit deriveParent(String dotted) {
164 for (int idx = dotted.lastIndexOf('.');idx>=0; idx=dotted.lastIndexOf('.',idx-1)) {
165 if (data.get(dotted.substring(0, idx))!=null) {
166 return new NSSplit(dotted,idx);