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