Merge "Improve Batches"
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / Notification.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.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.TreeMap;
30
31 import org.onap.aaf.auth.batch.actions.Message;
32 import org.onap.aaf.auth.env.AuthzTrans;
33 import org.onap.aaf.auth.org.Organization;
34 import org.onap.aaf.misc.env.Env;
35 import org.onap.aaf.misc.env.TimeTaken;
36 import org.onap.aaf.misc.env.Trans;
37 import org.onap.aaf.misc.env.util.Chrono;
38
39 import com.datastax.driver.core.ResultSet;
40 import com.datastax.driver.core.Row;
41 import com.datastax.driver.core.Session;
42 import com.datastax.driver.core.SimpleStatement;
43 import com.datastax.driver.core.Statement;
44
45 public class Notification {
46     public enum TYPE {
47         OA("Owner Approval",1),SA("Supervisor Approval",2),CN("Credential Expiration",20);
48         
49         private String desc;
50         private int type;
51     
52         private TYPE(String desc,int type) {
53             this.desc = desc;
54             this.type = type;
55         }
56         
57         public String desc() {
58             return desc;
59         }
60         
61         public int idx() {
62             return type;
63         }
64
65         public static TYPE get(int idx) {
66             for (TYPE nt : TYPE.values()) {
67                 if (idx==nt.type) {
68                     return nt;
69                 }
70             }
71             return null;
72         }
73     }
74
75
76     public static final TreeMap<String,List<Notification>> data = new TreeMap<>();
77     public static final Date now = new Date();
78     
79     public final String user;
80     public final TYPE type;
81     public Date last;
82     public int checkSum;
83     public Message msg;
84     private int current;
85     public Organization org;
86     public int count;
87
88     public static Creator<Notification> v2_0_18 = new Creator<Notification>() {
89         @Override
90         public Notification create(Row row) {
91             int idx =row.getInt(1);
92             TYPE typeCreator = TYPE.get(idx);
93             if (typeCreator==null) {
94                 return null;
95             }
96             return new Notification(row.getString(0), typeCreator, row.getTimestamp(2), row.getInt(3));
97         }
98
99         @Override
100         public String select() {
101             return "SELECT user,type,last,checksum FROM authz.notify LIMIT 100000";
102         }
103     };
104     
105     private Notification(String user, TYPE nt, Date last, int checksum) {
106         this.user = user;
107         this.type = nt;
108         this.last = last;
109         this.checkSum = checksum;
110         current = 0;
111         count = 0;
112     }
113     
114     public static void load(Trans trans, Session session, Creator<Notification> creator ) {
115         trans.info().log( "query: " + creator.select() );
116         TimeTaken tt = trans.start("Load Notify", Env.REMOTE);
117        
118         ResultSet results;
119         try {
120             Statement stmt = new SimpleStatement(creator.select());
121             results = session.execute(stmt);
122         } finally {
123             tt.done();
124         }
125         int count = 0;
126         tt = trans.start("Process Notify", Env.SUB);
127
128         try {
129             for (Row row : results.all()) {
130                 ++count;
131                 try {
132                     Notification not = creator.create(row);
133                     List<Notification> ln = data.get(not.user);
134                     if (ln==null) {
135                         ln = new ArrayList<>();
136                         data.put(not.user, ln);
137                     }
138                     ln.add(not);
139                 } finally {
140                     tt.done();
141                 }
142             }
143         } finally {
144             tt.done();
145             trans.info().log("Found",count,"Notify Records");
146         }
147     }
148     
149     public static Notification get(String user, TYPE type) {
150         List<Notification> ln = data.get(user);
151         if (ln!=null) {
152             for (Notification n : ln) {
153                 if (type.equals(n.type)) {
154                     return n;
155                 }
156             }
157         }
158         return null;
159     }
160
161     public static Notification create(String user, TYPE type) {
162         return new Notification(user,type,null,0);
163     }
164
165     
166     public void set(Message msg) {
167         this.msg = msg; 
168     }
169
170     public int checksum() {
171         if (msg==null) {
172             current=0;
173         } else if (current==0) {
174             for (String l : msg.lines) {
175                 for (byte b : l.getBytes()) {
176                     current+=b;
177                 }
178             }
179         }
180         return current;
181     }
182     
183     public boolean update(AuthzTrans trans, Session session, boolean dryRun) {
184         checksum();
185         if (last==null || current==0 || current!=checkSum) {
186             last = now;
187             current = checksum();
188             String update = "UPDATE authz.notify SET " +
189                     "last = '" + Chrono.utcStamp(last) +
190                     "', checksum=" +
191                     current +
192                     " WHERE user='" +
193                     user + 
194                     "' AND type=" +
195                     type.idx() +
196                     ";";
197             if (dryRun) {
198                 trans.info().log("Would",update);
199             } else {
200                 session.execute(update);
201             }
202             return true;
203         }
204         return false;
205     }
206
207     public String toString() {
208         return "\"" + user + "\",\"" + type.name() + "\",\"" 
209                 + Chrono.dateTime(last)+ "\", "  + checkSum;
210     }
211 }