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