Bring over Extend Batch programs
[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     private Notification(String user, TYPE nt, Date last, int checksum) {
89         this.user = user;
90         this.type = nt;
91         this.last = last;
92         this.checkSum = checksum;
93         current = 0;
94         count = 0;
95     }
96     
97     public static void load(Trans trans, Session session, Creator<Notification> creator ) {
98         trans.info().log( "query: " + creator.select() );
99         TimeTaken tt = trans.start("Load Notify", Env.REMOTE);
100        
101         ResultSet results;
102         try {
103             Statement stmt = new SimpleStatement(creator.select());
104             results = session.execute(stmt);
105         } finally {
106             tt.done();
107         }
108         int count = 0;
109         tt = trans.start("Process Notify", Env.SUB);
110
111         try {
112             for (Row row : results.all()) {
113                 ++count;
114                 try {
115                     Notification not = creator.create(row);
116                     List<Notification> ln = data.get(not.user);
117                     if (ln==null) {
118                         ln = new ArrayList<>();
119                         data.put(not.user, ln);
120                     }
121                     ln.add(not);
122                 } finally {
123                     tt.done();
124                 }
125             }
126         } finally {
127             tt.done();
128             trans.info().log("Found",count,"Notify Records");
129         }
130     }
131     
132     public static Notification get(String user, TYPE type) {
133         List<Notification> ln = data.get(user);
134         if (ln!=null) {
135             for (Notification n : ln) {
136                 if (type.equals(n.type)) {
137                     return n;
138                 }
139             }
140         }
141         return null;
142     }
143
144     public static Notification create(String user, TYPE type) {
145         return new Notification(user,type,null,0);
146     }
147     
148     public static Creator<Notification> v2_0_18 = new Creator<Notification>() {
149         @Override
150         public Notification create(Row row) {
151             int idx =row.getInt(1);
152             TYPE typeCreator = TYPE.get(idx);
153             if (typeCreator==null) {
154                 return null;
155             }
156             return new Notification(row.getString(0), typeCreator, row.getTimestamp(2), row.getInt(3));
157         }
158
159         @Override
160         public String select() {
161             return "SELECT user,type,last,checksum FROM authz.notify LIMIT 100000";
162         }
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 }