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 public static Creator<Notification> v2_0_18 = new Creator<Notification>() {
90 public Notification create(Row row) {
91 int idx =row.getInt(1);
92 TYPE typeCreator = TYPE.get(idx);
93 if (typeCreator==null) {
96 return new Notification(row.getString(0), typeCreator, row.getTimestamp(2), row.getInt(3));
100 public String select() {
101 return "SELECT user,type,last,checksum FROM authz.notify LIMIT 100000";
105 private Notification(String user, TYPE nt, Date last, int checksum) {
109 this.checkSum = checksum;
114 public static void load(Trans trans, Session session, Creator<Notification> creator ) {
115 trans.info().log( "query: " + creator.select() );
116 TimeTaken tt = trans.start("Load Notify", Env.REMOTE);
120 Statement stmt = new SimpleStatement(creator.select());
121 results = session.execute(stmt);
126 tt = trans.start("Process Notify", Env.SUB);
129 for (Row row : results.all()) {
132 Notification not = creator.create(row);
133 List<Notification> ln = data.get(not.user);
135 ln = new ArrayList<>();
136 data.put(not.user, ln);
145 trans.info().log("Found",count,"Notify Records");
149 public static Notification get(String user, TYPE type) {
150 List<Notification> ln = data.get(user);
152 for (Notification n : ln) {
153 if (type.equals(n.type)) {
161 public static Notification create(String user, TYPE type) {
162 return new Notification(user,type,null,0);
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;