2 * ============LICENSE_START====================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
24 package org.onap.aaf.auth.batch.helpers;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.TreeMap;
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;
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;
45 public class Notification {
47 OA("Owner Approval",1),SA("Supervisor Approval",2),CN("Credential Expiration",20);
52 private TYPE(String desc,int type) {
57 public String desc() {
65 public static TYPE get(int idx) {
66 for (TYPE nt : TYPE.values()) {
76 public static final TreeMap<String,List<Notification>> data = new TreeMap<>();
77 public static final Date now = new Date();
79 public final String user;
80 public final TYPE type;
85 public Organization org;
88 private Notification(String user, TYPE nt, Date last, int checksum) {
92 this.checkSum = checksum;
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);
103 Statement stmt = new SimpleStatement(creator.select());
104 results = session.execute(stmt);
109 tt = trans.start("Process Notify", Env.SUB);
112 for (Row row : results.all()) {
115 Notification not = creator.create(row);
116 List<Notification> ln = data.get(not.user);
118 ln = new ArrayList<>();
119 data.put(not.user, ln);
128 trans.info().log("Found",count,"Notify Records");
132 public static Notification get(String user, TYPE type) {
133 List<Notification> ln = data.get(user);
135 for (Notification n : ln) {
136 if (type.equals(n.type)) {
144 public static Notification create(String user, TYPE type) {
145 return new Notification(user,type,null,0);
148 public static Creator<Notification> v2_0_18 = new Creator<Notification>() {
150 public Notification create(Row row) {
151 int idx =row.getInt(1);
152 TYPE typeCreator = TYPE.get(idx);
153 if (typeCreator==null) {
156 return new Notification(row.getString(0), typeCreator, row.getTimestamp(2), row.getInt(3));
160 public String select() {
161 return "SELECT user,type,last,checksum FROM authz.notify LIMIT 100000";
166 public void set(Message msg) {
170 public int checksum() {
173 } else if (current==0) {
174 for (String l : msg.lines) {
175 for (byte b : l.getBytes()) {
183 public boolean update(AuthzTrans trans, Session session, boolean dryRun) {
185 if (last==null || current==0 || current!=checkSum) {
187 current = checksum();
188 String update = "UPDATE authz.notify SET " +
189 "last = '" + Chrono.utcStamp(last) +
198 trans.info().log("Would",update);
200 session.execute(update);
207 public String toString() {
208 return "\"" + user + "\",\"" + type.name() + "\",\""
209 + Chrono.dateTime(last)+ "\", " + checkSum;