summaryrefslogtreecommitdiffstats
path: root/authz-batch/src/main/java/com/att/authz/helpers/Cred.java
blob: 39691df98f7e3bbfcf34a7167438f5defa4a2bc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
 *******************************************************************************/
package com.att.authz.helpers;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import com.att.inno.env.Env;
import com.att.inno.env.TimeTaken;
import com.att.inno.env.Trans;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;

public class Cred  {
    public static final TreeMap<String,Cred> data = new TreeMap<String,Cred>();

	public final String id;
	public final List<Instance> instances;
	
	public Cred(String id) {
		this.id = id;
		instances = new ArrayList<Instance>();
	}
	
	public static class Instance {
		public final int type;
		public final Date expires;
		public final Integer other;
		
		public Instance(int type, Date expires, Integer other) {
			this.type = type;
			this.expires = expires;
			this.other = other;
		}
	}
	
	public Date last(final int type) {
		Date last = null;
		for(Instance i : instances) {
			if(i.type==type && (last==null || i.expires.after(last))) {
				last = i.expires;
			}
		}
		return last;
	}

	
	public Set<Integer> types() {
		Set<Integer> types = new HashSet<Integer>();
		for(Instance i : instances) {
			types.add(i.type);
		}
		return types;
	}

	public static void load(Trans trans, Session session ) {
		load(trans, session,"select id, type, expires, other from authz.cred;");
		
	}

	public static void loadOneNS(Trans trans, Session session, String ns ) {
		load(trans, session,"select id, type, expires, other from authz.cred WHERE ns='" + ns + "';");
	}

	private static void load(Trans trans, Session session, String query) {

        trans.info().log( "query: " + query );
        TimeTaken tt = trans.start("Read Creds", Env.REMOTE);
       
        ResultSet results;
		try {
	        Statement stmt = new SimpleStatement( query );
	        results = session.execute(stmt);
        } finally {
        	tt.done();
        }
		int count = 0;
        try {
	        Iterator<Row> iter = results.iterator();
	        Row row;
	        tt = trans.start("Load Roles", Env.SUB);
	        try {
		        while(iter.hasNext()) {
		        	++count;
		        	row = iter.next();
		        	String id = row.getString(0);
		        	Cred cred = data.get(id);
		        	if(cred==null) {
		        		cred = new Cred(id);
		        		data.put(id, cred);
		        	}
		        	cred.instances.add(new Instance(row.getInt(1), row.getDate(2), row.getInt(3)));
		        }
	        } finally {
	        	tt.done();
	        }
        } finally {
        	trans.info().log("Found",count,"creds");
        }


	}
	public String toString() {
		StringBuilder sb = new StringBuilder(id);
		sb.append('[');
		for(Instance i : instances) {
			sb.append('{');
			sb.append(i.type);
			sb.append(",\"");
			sb.append(i.expires);
			sb.append("\"}");
		}
		sb.append(']');
		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return id.hashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		return id.equals(obj);
	}

}