Upgrade to latest oparent
[aaf/authz.git] / authz-batch / src / main / java / com / att / authz / helpers / Notification.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
11 import com.att.authz.actions.Message;
12 import com.att.authz.env.AuthzTrans;
13 import com.att.authz.org.EmailWarnings;
14 import com.att.authz.org.Organization;
15 import com.att.authz.org.Organization.Notify;
16 import com.att.authz.org.Organization.Identity;
17 import com.att.authz.org.OrganizationException;
18 import com.att.authz.org.OrganizationFactory;
19 import org.onap.aaf.inno.env.Env;
20 import org.onap.aaf.inno.env.TimeTaken;
21 import org.onap.aaf.inno.env.Trans;
22 import org.onap.aaf.inno.env.util.Chrono;
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.core.Row;
25 import com.datastax.driver.core.Session;
26 import com.datastax.driver.core.SimpleStatement;
27 import com.datastax.driver.core.Statement;
28
29 public class Notification {
30         
31     public static final TreeMap<String,List<Notification>> data = new TreeMap<String,List<Notification>>();
32     public static final long now = System.currentTimeMillis();
33     
34     public final String user;
35         public final Notify type;
36         public final Date last;
37         public final int checksum;
38         public Message msg;
39         private int current;
40         public Organization org;
41         public int count;
42         private long graceEnds,lastdays;
43         
44         private Notification(String user, int type, Date last, int checksum) {
45                 this.user = user;
46                 this.type = Notify.from(type);
47                 this.last = last;
48                 this.checksum = checksum;
49                 current = 0;
50                 count = 0;
51         }
52         
53         private Notification(String user, Notify type, Date last, int checksum) {
54                 this.user = user;
55                 this.type = type;
56                 this.last = last;
57                 this.checksum = checksum;
58                 current = 0;
59                 count = 0;
60         }
61         
62         public static void load(Trans trans, Session session, Creator<Notification> creator ) {
63                 trans.info().log( "query: " + creator.select() );
64         TimeTaken tt = trans.start("Load Notify", Env.REMOTE);
65        
66         ResultSet results;
67                 try {
68                 Statement stmt = new SimpleStatement(creator.select());
69                 results = session.execute(stmt);
70         } finally {
71                 tt.done();
72         }
73                 int count = 0;
74         tt = trans.start("Process Notify", Env.SUB);
75
76         try {
77                 for(Row row : results.all()) {
78                         ++count;
79                         try {
80                                 Notification not = creator.create(row);
81                                 List<Notification> ln = data.get(not.user);
82                                 if(ln==null) {
83                                         ln = new ArrayList<Notification>();
84                                         data.put(not.user, ln);
85                                 }
86                                 ln.add(not);
87                         } finally {
88                                 tt.done();
89                         }
90                 }
91         } finally {
92                 tt.done();
93                 trans.info().log("Found",count,"Notify Records");
94         }
95         }
96         
97         public static Notification get(String user, Notify type) {
98                 List<Notification> ln = data.get(user);
99                 if(ln!=null) {
100                 for(Notification n : ln) {
101                         if(type.equals(n.type)) {
102                                 return n;
103                         }
104                 }
105                 }
106                 return null;
107         }
108
109         private static Notification getOrCreate(String user, Notify type) {
110                 List<Notification> ln = data.get(user);
111                 Notification n = null;
112                 if(ln==null) {
113                         ln = new ArrayList<Notification>();
114                         data.put(user, ln);
115                 } else {
116                         for(Notification n2 : ln) {
117                         if(type.equals(n2.type)) {
118                                 n=n2;
119                                 break;
120                         }
121                 }
122                 }
123                 if(n==null) {
124                         n = new Notification(user, type, new Date(), 0);
125                         ln.add(n);
126                 }
127                 return n;
128         }
129         
130         public static Notification add(AuthzTrans trans, UserRole ur) {
131                 Notification n = getOrCreate(ur.user,Notify.RoleExpiration);
132                 if(n.org==null) {
133                         try {
134                                 n.org = OrganizationFactory.obtain(trans.env(), ur.ns);
135                         } catch (OrganizationException e) {
136                                 trans.error().log(ur.ns, " does not have a Namespace");
137                         }
138                 }
139                 
140                 if(n.count==0) {
141                         EmailWarnings ew = n.org.emailWarningPolicy();
142                         n.graceEnds = ew.roleEmailInterval();
143                         n.lastdays = ew.emailUrgentWarning();
144                 }
145                 ++n.count;
146
147                 /*
148                 StringBuilder sb = new StringBuilder();
149                 sb.append("ID: ");
150                 sb.append(ur.user);
151                 User ouser;
152                 try {
153                         ouser = n.org.getUser(trans, ur.user);
154                         if(ouser!=null) {
155                                 sb.append(" (");
156                                 sb.append(ouser.fullName());
157                                 sb.append(')');
158                         }
159                 } catch (Exception e) {
160                 }
161                 sb.append("  Role: ");
162                 sb.append(ur.role);
163                 sb.append("  Expire");
164                 if(now<ur.expires.getTime()) {
165                         sb.append("s: ");
166                 } else {
167                         sb.append("d: ");
168                 }
169                 sb.append(Chrono.dateOnlyStamp(ur.expires));
170                 sb.append("\n  If you wish to extend, type\n");
171                 sb.append("\trole user extend ");
172                 sb.append(ur.role);
173                 sb.append(' ');
174                 sb.append(ur.user);
175                 sb.append("\n  If you wish to delete, type\n");
176                 sb.append("\trole user del ");
177                 sb.append(ur.role);
178                 sb.append(' ');
179                 sb.append(ur.user);
180                 sb.append('\n');
181                 n.msg.add(sb.toString());
182                 n.current=0;
183                 */
184                 return n;
185         }
186
187         public static Notification addApproval(AuthzTrans trans, Identity ou) {
188                 Notification n = getOrCreate(ou.id(),Notify.Approval);
189                 if(n.org==null) {
190                         n.org = ou.org();
191                 }
192                 if(n.count==0) { // first time.
193                         EmailWarnings ew = n.org.emailWarningPolicy();
194                         n.graceEnds = ew.apprEmailInterval();
195                         n.lastdays = ew.emailUrgentWarning();
196                 }
197                 ++n.count;
198                 return n;
199         }
200
201         public static Creator<Notification> v2_0_14 = new Creator<Notification>() {
202                 @Override
203                 public Notification create(Row row) {
204                         return new Notification(row.getString(0), row.getInt(1), row.getDate(2),row.getInt(3));
205                 }
206
207                 @Override
208                 public String select() {
209                         return "select user,type,last,checksum from authz.notify";
210                 }
211         };
212
213         public void set(Message msg) {
214                 this.msg = msg; 
215         }
216
217         public int checksum() {
218                 if(current==0) {
219                         for(String l : msg.lines) {
220                                 for(byte b : l.getBytes()) {
221                                         current+=b;
222                                 }
223                         }
224                 }
225                 return current;
226         }
227         
228         public boolean update(AuthzTrans trans, Session session, boolean dryRun) {
229                 String update = update();
230                 if(update!=null) {
231                         if(dryRun) {
232                                 trans.info().log(update);
233                         } else {
234                                 session.execute(update);
235                         }
236                         return true; // Updated info, expect to notify
237                 }
238                 return false;
239         }
240
241         /** 
242          * Returns an Update String for CQL if there is data.
243          * 
244          * Returns null if nothing to update
245          * @return
246          */
247         private String update() {
248                 // If this has been done before, there is no change in checkSum and the last time notified is within GracePeriod
249                 if(checksum!=0 && checksum()==checksum && now < last.getTime()+graceEnds && now > last.getTime()+lastdays) {
250                         return null;
251                 } else {
252                         return "UPDATE authz.notify SET last = '" +
253                                         Chrono.dateOnlyStamp(last) +
254                                         "', checksum=" +
255                                         current +
256                                         " WHERE user='" +
257                                         user + 
258                                         "' AND type=" +
259                                         type.getValue() +
260                                         ";";
261                 }
262         }
263
264 //      public void text(Email email) {
265 //              for(String s : msg) {
266 //                      email.line(s);
267 //              }
268 //      }
269 //
270         public String toString() {
271                 return "\"" + user + "\",\"" + type.name() + "\",\""  + Chrono.dateOnlyStamp(last);
272         }
273 }