Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / History.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright (C) 2018 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.batch.helpers;
25
26 import java.nio.ByteBuffer;
27 import java.util.Iterator;
28 import java.util.UUID;
29
30 import org.onap.aaf.misc.env.Env;
31 import org.onap.aaf.misc.env.TimeTaken;
32 import org.onap.aaf.misc.env.Trans;
33
34 import com.datastax.driver.core.ResultSet;
35 import com.datastax.driver.core.Row;
36 import com.datastax.driver.core.Session;
37 import com.datastax.driver.core.SimpleStatement;
38 import com.datastax.driver.core.Statement;
39
40 public class History  {
41     public final UUID id;
42     public final String action;
43     public final String memo;
44     public final String reconstruct;
45     public final String subject;
46     public final String target;
47     public final String user;
48     public final int yrMon;
49
50     public static Creator<History> sansConstruct = new Creator<History> () {
51         @Override
52         public History create(Row row) {
53             return new History(
54                     row.getUUID(0),
55                     row.getString(1),
56                     row.getString(2),
57                     row.getString(3),
58                     row.getString(4),
59                     row.getString(5),
60                     row.getInt(6));
61         }
62
63         @Override
64         public String select() {
65             return "SELECT id, action, memo, subject, target, user, yr_mon from authz.history LIMIT 10000000 ";
66         }
67     };
68
69     public static Creator<History> avecConstruct = new Creator<History> () {
70         private final StringBuilder sb = new StringBuilder();
71
72         @Override
73         public History create(Row row) {
74             ByteBuffer bb = row.getBytes(3);
75             sb.setLength(0);
76
77             if (bb!=null && bb.hasRemaining()) {
78                 sb.append("0x");
79                 while (bb.hasRemaining()) {
80                     sb.append(String.format("%02x",bb.get()));
81                 }
82                 bb.flip();
83             }
84             return new History(
85                     row.getUUID(0),
86                     row.getString(1),
87                     row.getString(2),
88                     sb.toString(),
89                     row.getString(4),
90                     row.getString(5),
91                     row.getString(6),
92                     row.getInt(7));
93         }
94
95         @Override
96         public String select() {
97             return "SELECT id, action, memo, reconstruct, subject, target, user, yr_mon from authz.history LIMIT 10000000 ";
98         }
99     };
100     
101     public History(UUID id, String action, String memo, String subject, String target, String user, int yrMon) {
102         this.id = id;
103         this.action = action;
104         this.memo = memo;
105         this.reconstruct = null;
106         this.subject = subject;
107         this.target = target;
108         this.user = user;
109         this.yrMon = yrMon;
110     }
111     
112     public History(UUID id, String action, String memo, String reconstruct, String subject, String target, String user, int yrMon) {
113         this.id = id;
114         this.action = action;
115         this.memo = memo;
116         this.reconstruct = reconstruct;
117         this.subject = subject;
118         this.target = target;
119         this.user = user;
120         this.yrMon = yrMon;
121     }
122
123     public static void load(Trans trans, Session session, Creator<History> creator, Loader<History> loader) {
124         trans.info().log( "query: " + creator.select() );
125         TimeTaken tt = trans.start("Read History", Env.REMOTE);
126        
127         ResultSet results;
128         try {
129             Statement stmt = new SimpleStatement( creator.select() ).setReadTimeoutMillis(240000);
130             results = session.execute(stmt);
131         } finally {
132             tt.done();
133         }
134         int count = 0;
135         try {
136             Iterator<Row> iter = results.iterator();
137             Row row;
138             tt = trans.start("Load History", Env.SUB);
139             try {
140                 while (iter.hasNext()) {
141                     ++count;
142                     row = iter.next();
143                     loader.exec(creator.create(row));
144                 }
145             } finally {
146                 tt.done();
147             }
148         } finally {
149             trans.info().log("Found",count,"histories");
150         }
151     }
152     
153     public String toString() {
154         return String.format("%s %d %s, %s, %s, %s, %s", 
155                 id.toString(),
156                 yrMon,
157                 user,
158                 target,
159                 action,
160                 subject,
161                 memo);
162     }
163
164     /* (non-Javadoc)
165      * @see java.lang.Object#hashCode()
166      */
167     @Override
168     public int hashCode() {
169         return id.hashCode();
170     }
171
172     /* (non-Javadoc)
173      * @see java.lang.Object#equals(java.lang.Object)
174      */
175     @Override
176     public boolean equals(Object obj) {
177         return id.equals(obj);
178     }
179 }