2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.batch.helpers;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.TreeMap;
29 import org.onap.aaf.auth.batch.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;
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;
43 public class Notification {
45 OA("Owner Approval",1),SA("Supervisor Approval",2),CN("Credential Expiration",20);
50 private TYPE(String desc,int type) {
55 public String desc() {
63 public static TYPE get(int idx) {
64 for (TYPE nt : TYPE.values()) {
74 public static final TreeMap<String,List<Notification>> data = new TreeMap<>();
75 public static final Date now = new Date();
77 public final String user;
78 public final TYPE type;
83 public Organization org;
86 private Notification(String user, TYPE nt, Date last, int checksum) {
90 this.checksum = checksum;
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);
101 Statement stmt = new SimpleStatement(creator.select());
102 results = session.execute(stmt);
107 tt = trans.start("Process Notify", Env.SUB);
110 for (Row row : results.all()) {
113 Notification not = creator.create(row);
114 List<Notification> ln = data.get(not.user);
116 ln = new ArrayList<>();
117 data.put(not.user, ln);
126 trans.info().log("Found",count,"Notify Records");
130 public static Notification get(String user, TYPE type) {
131 List<Notification> ln = data.get(user);
133 for (Notification n : ln) {
134 if (type.equals(n.type)) {
142 public static Notification create(String user, TYPE type) {
143 return new Notification(user,type,null,0);
146 public static Creator<Notification> v2_0_18 = new Creator<Notification>() {
148 public Notification create(Row row) {
149 int idx =row.getInt(1);
150 TYPE type = TYPE.get(idx);
154 return new Notification(row.getString(0), type, row.getTimestamp(2), row.getInt(3));
158 public String select() {
159 return "SELECT user,type,last,checksum FROM authz.notify LIMIT 100000";
164 public void set(Message msg) {
168 public int checksum() {
171 } else if (current==0) {
172 for (String l : msg.lines) {
173 for (byte b : l.getBytes()) {
181 public boolean update(AuthzTrans trans, Session session, boolean dryRun) {
183 if (last==null || current==0 || current!=checksum) {
185 current = checksum();
186 String update = "UPDATE authz.notify SET " +
187 "last = '" + Chrono.utcStamp(last) +
196 trans.info().log("Would",update);
198 session.execute(update);
205 public String toString() {
206 return "\"" + user + "\",\"" + type.name() + "\",\""
207 + Chrono.dateTime(last)+ "\", " + checksum;