Upgrade to latest oparent
[aaf/authz.git] / authz-batch / src / main / java / com / att / authz / helpers / NS.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.helpers;
5
6 import java.util.Iterator;
7 import java.util.Map;
8 import java.util.TreeMap;
9
10 import org.onap.aaf.inno.env.Env;
11 import org.onap.aaf.inno.env.TimeTaken;
12 import org.onap.aaf.inno.env.Trans;
13 import com.datastax.driver.core.ResultSet;
14 import com.datastax.driver.core.Row;
15 import com.datastax.driver.core.Session;
16 import com.datastax.driver.core.SimpleStatement;
17 import com.datastax.driver.core.Statement;
18
19 public class NS implements Comparable<NS> {
20         public final static Map<String,NS> data = new TreeMap<String,NS>();
21
22         public final String name, description, parent;
23         public final int scope,type;
24
25         public NS(String name, String description, String parent, int type, int scope) {
26                 this.name = name;
27                 this.description = description;
28                 this.parent = parent;
29                 this.scope = scope;
30                 this.type = type;
31         }
32         
33         public static void load(Trans trans, Session session, Creator<NS> creator) {
34                 load(trans,session,
35                                 "select name, description, parent, type, scope from authz.ns;"
36                                 ,creator);
37         }
38         
39         public static void loadOne(Trans trans, Session session, Creator<NS> creator, String ns) {
40             load(trans,session,
41                                 ("select name, description, parent, type, scope from authz.ns WHERE name='"+ns+"';")
42                                 ,creator
43                                 );
44         }
45
46         private static void load(Trans trans, Session session, String query, Creator<NS> creator) {
47         trans.info().log( "query: " + query );
48         ResultSet results;
49         TimeTaken tt;
50
51         tt = trans.start("Read Namespaces", Env.REMOTE);
52         try {
53                 Statement stmt = new SimpleStatement( query );
54                 results = session.execute(stmt);
55         } finally {
56                 tt.done();
57         }
58         
59
60         try {
61                 Iterator<Row> iter = results.iterator();
62                 Row row;
63                 tt = trans.start("Load Namespaces", Env.SUB);
64                 try {
65                         while(iter.hasNext()) {
66                                 row = iter.next();
67                                 NS ns = creator.create(row);
68                                 data.put(ns.name,ns);
69                         }
70                 } finally {
71                         tt.done();
72                 }
73         } finally {
74                 trans.info().log("Found",data.size(),"Namespaces");
75         }
76
77         }
78
79         public String toString() {
80                 return name;
81         }
82
83         /* (non-Javadoc)
84          * @see java.lang.Object#hashCode()
85          */
86         @Override
87         public int hashCode() {
88                 return name.hashCode();
89         }
90
91         /* (non-Javadoc)
92          * @see java.lang.Object#equals(java.lang.Object)
93          */
94         @Override
95         public boolean equals(Object obj) {
96                 return name.equals(obj);
97         }
98
99         @Override
100         public int compareTo(NS o) {
101                 return name.compareTo(o.name);
102         }
103         
104         public static class NSSplit {
105                 public String ns;
106                 public String other;
107                 public NSSplit(String s, int dot) {
108                         ns = s.substring(0,dot);
109                         other = s.substring(dot+1);
110                 }
111         }
112         public static NSSplit deriveParent(String dotted) {
113                 if(dotted==null)return null;
114                 for(int idx = dotted.lastIndexOf('.');idx>=0; idx=dotted.lastIndexOf('.',idx-1)) {
115                         if(data.get(dotted.substring(0, idx))!=null) {
116                                 return new NSSplit(dotted,idx);
117                         }
118                 }
119                 return null;
120         }
121         
122         public static Creator<NS> v2_0_11 = new Creator<NS> () {
123                 @Override
124                 public NS create(Row row) {
125                         return new NS(row.getString(0),row.getString(1), row.getString(2),row.getInt(3),row.getInt(4));
126                 }
127                 
128                 @Override
129                 public String select() {
130                         return "SELECT name, description, parent, type, scope FROM authz.ns ";
131                 }
132         };
133
134 }