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====================================================
23 package org.onap.aaf.auth.batch.helpers;
25 import java.security.cert.X509Certificate;
26 import java.util.Date;
27 import java.util.Iterator;
28 import java.util.List;
31 import java.util.TreeMap;
33 import org.onap.aaf.auth.batch.helpers.Cred.Instance;
34 import org.onap.aaf.auth.batch.helpers.ExpireRange.Range;
35 import org.onap.aaf.cadi.util.CSV;
36 import org.onap.aaf.misc.env.Env;
37 import org.onap.aaf.misc.env.TimeTaken;
38 import org.onap.aaf.misc.env.Trans;
39 import org.onap.aaf.misc.env.util.Split;
41 import com.datastax.driver.core.ResultSet;
42 import com.datastax.driver.core.Row;
43 import com.datastax.driver.core.Session;
44 import com.datastax.driver.core.SimpleStatement;
45 import com.datastax.driver.core.Statement;
47 public class LastNotified {
48 private Map<String,Date> lastNotified = new TreeMap<>();
49 private Session session;
50 private static final Date never = new Date(0);
51 private static final String SELECT = "SELECT user,target,key,last FROM authz.notified";
53 public LastNotified(Session session) {
54 this.session = session;
57 public void add(Set<String> users) {
58 StringBuilder query = new StringBuilder();
61 for(String user : users) {
70 add(session.execute(query.toString()),lastNotified, (x,y) -> false);
78 add(session.execute(query.toString()),lastNotified, (x,y) -> false);
83 * Note: target_key CAN also contain a Pipe.
90 public Date lastNotified(String user, String target, String target_key) {
91 String key = user + '|' + target + '|' + target_key;
92 return lastNotified(key);
95 public Date lastNotified(String key) {
96 Date rv = lastNotified.get(key);
99 lastNotified.put(key, rv);
104 private Date add(ResultSet result, Map<String, Date> lastNotified, MarkDelete md) {
107 for(Iterator<Row> iter = result.iterator(); iter.hasNext();) {
109 String ttKey = r.getString(1) + '|' +
112 String fullKey = r.getString(0) + '|' +
114 last=r.getTimestamp(3);
115 if(!md.process(fullKey, last)) {
116 lastNotified.put(fullKey, last);
117 Date d = lastNotified.get(ttKey);
118 if(d==null || d.after(last)) { // put most recent, if different
119 lastNotified.put(ttKey, last);
126 private interface MarkDelete {
127 public boolean process(String fullKey, Date last);
130 private void startQuery(StringBuilder query) {
131 query.append(SELECT + " WHERE user in (");
134 private void endQuery(StringBuilder query) {
138 public void update(StringBuilder query,String user, String target, String key) {
139 query.append("UPDATE authz.notified SET last=dateof(now()) WHERE user='");
141 query.append("' AND target='");
142 query.append(target);
143 query.append("' AND key='");
145 query.append("';\n");
148 public LastNotified loadAll(Trans trans, final Range delRange, final CSV.Writer cw) {
149 trans.debug().log( "query: ",SELECT );
150 TimeTaken tt = trans.start("Read all LastNotified", Env.REMOTE);
154 Statement stmt = new SimpleStatement( SELECT );
155 results = session.execute(stmt);
156 add(results,lastNotified, (fullKey, last) -> {
157 if(delRange.inRange(last)) {
158 String[] params = Split.splitTrim('|', fullKey,3);
159 if(params.length==3) {
160 cw.row("notified",params[0],params[1],params[2]);
172 public static String newKey(UserRole ur) {
173 return "ur|" + ur.user() + '|'+ur.role();
176 public static String newKey(Cred cred, Instance inst) {
177 return "cred|" + cred.id + '|' + inst.type + '|' + inst.tag;
180 public static String newKey(X509 x509, X509Certificate x509Cert) {
181 return "x509|" + x509.id + '|' + x509Cert.getSerialNumber().toString();
184 public static void delete(StringBuilder query, List<String> row) {
185 query.append("DELETE FROM authz.notified WHERE user='");
186 query.append(row.get(1));
187 query.append("' AND target='");
188 query.append(row.get(2));
189 query.append("' AND key='");
190 query.append(row.get(3));
191 query.append("';\n");