summaryrefslogtreecommitdiffstats
path: root/authz-batch/src/main/java/com/att/authz/helpers/Future.java
blob: d658535c428bdeea9e73a6af5cdab0654017088e (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
/*******************************************************************************
 * 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.List;
import java.util.TreeMap;
import java.util.UUID;

import org.onap.aaf.inno.env.Env;
import org.onap.aaf.inno.env.TimeTaken;
import org.onap.aaf.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 Future {
	public static final List<Future> data = new ArrayList<Future>();
	public static final TreeMap<String,List<Future>> byMemo = new TreeMap<String,List<Future>>();
	
	public final UUID id;
	public final String memo, target;
	public final Date start, expires;
	public Future(UUID id, String memo, String target, Date start, Date expires) {
		this.id = id;
		this.memo = memo;
		this.target = target;
		this.start = start;
		this.expires = expires;
	}

	public static void load(Trans trans, Session session, Creator<Future> creator) {
		trans.info().log( "query: " + creator.select() );
		ResultSet results;
		TimeTaken tt = trans.start("Load Futures", Env.REMOTE);
		try {
	        Statement stmt = new SimpleStatement(creator.select());
	        results = session.execute(stmt);
		} finally {
			tt.done();
		}
		
		int count = 0;
		tt = trans.start("Process Futures", Env.SUB);
		try {
        	for(Row row : results.all()) {
        		++count;
        		Future f = creator.create(row);
        		data.add(f);
        		
        		List<Future> lf = byMemo.get(f.memo);
        		if(lf == null) {
        			lf = new ArrayList<Future>();
        			byMemo.put(f.memo, lf);
        		}
        		lf.add(f);
        		
        	}
		} finally {
			trans.info().log("Found",count,"Futures");
		}
	}
	
	public static Creator<Future> v2_0_15 = new Creator<Future>() {
		@Override
		public Future create(Row row) {
			return new Future(row.getUUID(0),row.getString(1),row.getString(2),
					row.getDate(3),row.getDate(4));
		}

		@Override
		public String select() {
			return "select id,memo,target,start,expires from authz.future";
		}
	};
	
	public static void delete(List<Future> fl) {
		if(fl==null || fl.isEmpty()) {
			return;
		}
		for(Future f : fl) {
			data.remove(f);
		}
		// Faster to start over, then look for entries.
		byMemo.clear();
		for(Future f : data) {
			List<Future> lf = byMemo.get(f.memo);
			if(lf == null) {
				lf = new ArrayList<Future>();
				byMemo.put(f.memo, lf);
			}
			lf.add(f);
		}
	}
}