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