Approval Batch, prep better JUnit
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / NS.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.batch.helpers;
25
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.TreeMap;
29
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;
34
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;
40
41 public class    NS implements Comparable<NS> {
42     public static final Map<String,NS> data = new TreeMap<>();
43
44     public NsDAO.Data ndd;
45
46     public static Creator<NS> v2_0_11 = new Creator<NS> () {
47         @Override
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));
50         }
51
52         @Override
53         public String select() {
54             return "SELECT name, description, parent, type, scope FROM authz.ns ";
55         }
56     };
57
58     public NS(String name, String description, String parent, int type, int scope) {
59         ndd = new NsDAO.Data();
60         ndd.name = name;
61         ndd.description = description;
62         ndd.parent = parent;
63         ndd.type = type;
64         // ndd.attrib = 
65     }
66     
67     public static void load(Trans trans, Session session, Creator<NS> creator) {
68         load(trans,session,
69                 "select name, description, parent, type, scope from authz.ns;"
70                 ,creator);
71     }
72     
73     public static void loadOne(Trans trans, Session session, Creator<NS> creator, String ns) {
74         load(trans,session,
75                 ("select name, description, parent, type, scope from authz.ns WHERE name='"+ns+"';")
76                 ,creator
77                 );
78     }
79
80     private static void load(Trans trans, Session session, String query, Creator<NS> creator) {
81         trans.info().log( "query: " + query );
82         ResultSet results;
83         TimeTaken tt;
84
85         tt = trans.start("Read Namespaces", Env.REMOTE);
86         try {
87             Statement stmt = new SimpleStatement( query );
88             results = session.execute(stmt);
89         } finally {
90             tt.done();
91         }
92         
93
94         try {
95             Iterator<Row> iter = results.iterator();
96             Row row;
97             tt = trans.start("Load Namespaces", Env.SUB);
98             try {
99                 while (iter.hasNext()) {
100                     row = iter.next();
101                     NS ns = creator.create(row);
102                     data.put(ns.ndd.name,ns);
103                 }
104             } finally {
105                 tt.done();
106             }
107         } finally {
108             trans.info().log("Found",data.size(),"Namespaces");
109         }
110
111     }
112
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);
117         ResultSet results;
118         try {
119             Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
120             results = session.execute(stmt);
121             return results.one().getLong(0);
122         } finally {
123             tt.done();
124         }
125     }
126         
127     public String toString() {
128         return ndd.name;
129     }
130
131     /* (non-Javadoc)
132      * @see java.lang.Object#hashCode()
133      */
134     @Override
135     public int hashCode() {
136         return ndd.name.hashCode();
137     }
138
139     /* (non-Javadoc)
140      * @see java.lang.Object#equals(java.lang.Object)
141      */
142     @Override
143     public boolean equals(Object obj) {
144         return ndd.name.equals(obj);
145     }
146
147     @Override
148     public int compareTo(NS o) {
149         return ndd.name.compareTo(o.ndd.name);
150     }
151     
152     public static class NSSplit {
153         public String ns;
154         public String other;
155         public NSSplit(String s, int dot) {
156             ns = s.substring(0,dot);
157             other = s.substring(dot+1);
158         }
159     }
160     public static NSSplit deriveParent(String dotted) {
161         if (dotted==null) {
162             return null;
163         }
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);
167             }
168         }
169         return null;
170     }
171
172 }