Clean LOCAL Dir based on VERSION
[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.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;
36
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;
42
43 public class    NS implements Comparable<NS> {
44     public static final Map<String,NS> data = new TreeMap<>();
45
46     public NsDAO.Data ndd;
47
48     public static Creator<NS> v2_0_11 = new Creator<NS> () {
49         @Override
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));
52         }
53
54         @Override
55         public String select() {
56             return "SELECT name, description, parent, type, scope FROM authz.ns ";
57         }
58     };
59
60     public NS(String name, String description, String parent, int type, int scope) {
61         ndd = new NsDAO.Data();
62         ndd.name = name;
63         ndd.description = description;
64         ndd.parent = parent;
65         ndd.type = type;
66         // ndd.attrib = 
67     }
68     
69     public static void load(Trans trans, Session session, Creator<NS> creator) {
70         load(trans,session,
71                 "select name, description, parent, type, scope from authz.ns;"
72                 ,creator
73                 , v -> data.put(v.ndd.name,v)
74                         );
75     }
76     
77     public static void loadOne(Trans trans, Session session, Creator<NS> creator, String ns) {
78         load(trans,session,
79                 ("select name, description, parent, type, scope from authz.ns WHERE name='"+ns+"';")
80                 ,creator
81                 , v -> data.put(v.ndd.name,v)
82                 );
83     }
84
85     public static void load(Trans trans, Session session, Creator<NS> creator, Visitor<NS> visitor) {
86          load(trans,session,creator.query(null),creator, visitor);
87     }
88     
89     public void row(final CSV.Writer csvw, String tag) {
90         csvw.row(tag,ndd.name,ndd.type,ndd.parent);
91     }
92
93
94     private static void load(Trans trans, Session session, String query, Creator<NS> creator, Visitor<NS> visitor) {
95         trans.info().log( "query: " + query );
96         ResultSet results;
97         TimeTaken tt;
98
99         tt = trans.start("Read Namespaces", Env.REMOTE);
100         try {
101             Statement stmt = new SimpleStatement( query );
102             results = session.execute(stmt);
103         } finally {
104             tt.done();
105         }
106         
107
108         try {
109             Iterator<Row> iter = results.iterator();
110             Row row;
111             tt = trans.start("Load Namespaces", Env.SUB);
112             try {
113                 while (iter.hasNext()) {
114                     row = iter.next();
115                     NS ns = creator.create(row);
116                     visitor.visit(ns);
117                 }
118             } finally {
119                 tt.done();
120             }
121         } finally {
122             trans.info().log("Found",data.size(),"Namespaces");
123         }
124
125     }
126
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);
131         ResultSet results;
132         try {
133             Statement stmt = new SimpleStatement(query).setReadTimeoutMillis(12000);
134             results = session.execute(stmt);
135             return results.one().getLong(0);
136         } finally {
137             tt.done();
138         }
139     }
140         
141     public String toString() {
142         return ndd.name;
143     }
144
145     /* (non-Javadoc)
146      * @see java.lang.Object#hashCode()
147      */
148     @Override
149     public int hashCode() {
150         return ndd.name.hashCode();
151     }
152
153     /* (non-Javadoc)
154      * @see java.lang.Object#equals(java.lang.Object)
155      */
156     @Override
157     public boolean equals(Object obj) {
158         return ndd.name.equals(obj);
159     }
160
161     @Override
162     public int compareTo(NS o) {
163         return ndd.name.compareTo(o.ndd.name);
164     }
165     
166     public static class NSSplit {
167         public String ns;
168         public String other;
169         public NSSplit(String s, int dot) {
170             ns = s.substring(0,dot);
171             other = s.substring(dot+1);
172         }
173     }
174     public static NSSplit deriveParent(String dotted) {
175         if (dotted==null) {
176             return null;
177         }
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);
181             }
182         }
183         return null;
184     }
185
186 }