Upgrade to latest oparent
[aaf/authz.git] / authz-batch / src / main / java / com / att / authz / helpers / Future.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.List;
9 import java.util.TreeMap;
10 import java.util.UUID;
11
12 import org.onap.aaf.inno.env.Env;
13 import org.onap.aaf.inno.env.TimeTaken;
14 import org.onap.aaf.inno.env.Trans;
15 import com.datastax.driver.core.ResultSet;
16 import com.datastax.driver.core.Row;
17 import com.datastax.driver.core.Session;
18 import com.datastax.driver.core.SimpleStatement;
19 import com.datastax.driver.core.Statement;
20
21 public class Future {
22         public static final List<Future> data = new ArrayList<Future>();
23         public static final TreeMap<String,List<Future>> byMemo = new TreeMap<String,List<Future>>();
24         
25         public final UUID id;
26         public final String memo, target;
27         public final Date start, expires;
28         public Future(UUID id, String memo, String target, Date start, Date expires) {
29                 this.id = id;
30                 this.memo = memo;
31                 this.target = target;
32                 this.start = start;
33                 this.expires = expires;
34         }
35
36         public static void load(Trans trans, Session session, Creator<Future> creator) {
37                 trans.info().log( "query: " + creator.select() );
38                 ResultSet results;
39                 TimeTaken tt = trans.start("Load Futures", Env.REMOTE);
40                 try {
41                 Statement stmt = new SimpleStatement(creator.select());
42                 results = session.execute(stmt);
43                 } finally {
44                         tt.done();
45                 }
46                 
47                 int count = 0;
48                 tt = trans.start("Process Futures", Env.SUB);
49                 try {
50                 for(Row row : results.all()) {
51                         ++count;
52                         Future f = creator.create(row);
53                         data.add(f);
54                         
55                         List<Future> lf = byMemo.get(f.memo);
56                         if(lf == null) {
57                                 lf = new ArrayList<Future>();
58                                 byMemo.put(f.memo, lf);
59                         }
60                         lf.add(f);
61                         
62                 }
63                 } finally {
64                         trans.info().log("Found",count,"Futures");
65                 }
66         }
67         
68         public static Creator<Future> v2_0_15 = new Creator<Future>() {
69                 @Override
70                 public Future create(Row row) {
71                         return new Future(row.getUUID(0),row.getString(1),row.getString(2),
72                                         row.getDate(3),row.getDate(4));
73                 }
74
75                 @Override
76                 public String select() {
77                         return "select id,memo,target,start,expires from authz.future";
78                 }
79         };
80         
81         public static void delete(List<Future> fl) {
82                 if(fl==null || fl.isEmpty()) {
83                         return;
84                 }
85                 for(Future f : fl) {
86                         data.remove(f);
87                 }
88                 // Faster to start over, then look for entries.
89                 byMemo.clear();
90                 for(Future f : data) {
91                         List<Future> lf = byMemo.get(f.memo);
92                         if(lf == null) {
93                                 lf = new ArrayList<Future>();
94                                 byMemo.put(f.memo, lf);
95                         }
96                         lf.add(f);
97                 }
98         }
99 }