Upgrade to latest oparent
[aaf/authz.git] / authz-batch / src / main / java / com / att / authz / helpers / Cred.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.ArrayList;
7 import java.util.Date;
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Set;
12 import java.util.TreeMap;
13
14 import org.onap.aaf.inno.env.Env;
15 import org.onap.aaf.inno.env.TimeTaken;
16 import org.onap.aaf.inno.env.Trans;
17 import com.datastax.driver.core.ResultSet;
18 import com.datastax.driver.core.Row;
19 import com.datastax.driver.core.Session;
20 import com.datastax.driver.core.SimpleStatement;
21 import com.datastax.driver.core.Statement;
22
23 public class Cred  {
24     public static final TreeMap<String,Cred> data = new TreeMap<String,Cred>();
25
26         public final String id;
27         public final List<Instance> instances;
28         
29         public Cred(String id) {
30                 this.id = id;
31                 instances = new ArrayList<Instance>();
32         }
33         
34         public static class Instance {
35                 public final int type;
36                 public final Date expires;
37                 public final Integer other;
38                 
39                 public Instance(int type, Date expires, Integer other) {
40                         this.type = type;
41                         this.expires = expires;
42                         this.other = other;
43                 }
44         }
45         
46         public Date last(final int type) {
47                 Date last = null;
48                 for(Instance i : instances) {
49                         if(i.type==type && (last==null || i.expires.after(last))) {
50                                 last = i.expires;
51                         }
52                 }
53                 return last;
54         }
55
56         
57         public Set<Integer> types() {
58                 Set<Integer> types = new HashSet<Integer>();
59                 for(Instance i : instances) {
60                         types.add(i.type);
61                 }
62                 return types;
63         }
64
65         public static void load(Trans trans, Session session ) {
66                 load(trans, session,"select id, type, expires, other from authz.cred;");
67                 
68         }
69
70         public static void loadOneNS(Trans trans, Session session, String ns ) {
71                 load(trans, session,"select id, type, expires, other from authz.cred WHERE ns='" + ns + "';");
72         }
73
74         private static void load(Trans trans, Session session, String query) {
75
76         trans.info().log( "query: " + query );
77         TimeTaken tt = trans.start("Read Creds", Env.REMOTE);
78        
79         ResultSet results;
80                 try {
81                 Statement stmt = new SimpleStatement( query );
82                 results = session.execute(stmt);
83         } finally {
84                 tt.done();
85         }
86                 int count = 0;
87         try {
88                 Iterator<Row> iter = results.iterator();
89                 Row row;
90                 tt = trans.start("Load Roles", Env.SUB);
91                 try {
92                         while(iter.hasNext()) {
93                                 ++count;
94                                 row = iter.next();
95                                 String id = row.getString(0);
96                                 Cred cred = data.get(id);
97                                 if(cred==null) {
98                                         cred = new Cred(id);
99                                         data.put(id, cred);
100                                 }
101                                 cred.instances.add(new Instance(row.getInt(1), row.getDate(2), row.getInt(3)));
102                         }
103                 } finally {
104                         tt.done();
105                 }
106         } finally {
107                 trans.info().log("Found",count,"creds");
108         }
109
110
111         }
112         public String toString() {
113                 StringBuilder sb = new StringBuilder(id);
114                 sb.append('[');
115                 for(Instance i : instances) {
116                         sb.append('{');
117                         sb.append(i.type);
118                         sb.append(",\"");
119                         sb.append(i.expires);
120                         sb.append("\"}");
121                 }
122                 sb.append(']');
123                 return sb.toString();
124         }
125
126         /* (non-Javadoc)
127          * @see java.lang.Object#hashCode()
128          */
129         @Override
130         public int hashCode() {
131                 return id.hashCode();
132         }
133
134         /* (non-Javadoc)
135          * @see java.lang.Object#equals(java.lang.Object)
136          */
137         @Override
138         public boolean equals(Object obj) {
139                 return id.equals(obj);
140         }
141
142 }