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.dao.cass;
26 import java.nio.ByteBuffer;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.UUID;
31 import org.onap.aaf.auth.dao.CassDAOImpl;
32 import org.onap.aaf.auth.dao.Loader;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.layer.Result;
36 import com.datastax.driver.core.Cluster;
37 import com.datastax.driver.core.ResultSet;
38 import com.datastax.driver.core.Row;
41 * FutureDAO stores Construction information to create
42 * elements at another time.
47 public class FutureDAO extends CassDAOImpl<AuthzTrans,FutureDAO.Data> {
48 private static final String TABLE = "future";
49 private final HistoryDAO historyDAO;
50 private PSInfo psByStartAndTarget;
51 public static final int KEYLIMIT = 1;
53 public FutureDAO(AuthzTrans trans, Cluster cluster, String keyspace) {
54 super(trans, FutureDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
55 historyDAO = new HistoryDAO(trans, this);
59 public FutureDAO(AuthzTrans trans, HistoryDAO hDAO) {
60 super(trans, FutureDAO.class.getSimpleName(),hDAO, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
66 public static class Data {
72 public String target_key;
73 public Date target_date;
74 public ByteBuffer construct; // this is a blob in cassandra
77 private static class FLoader extends Loader<Data> {
82 public FLoader(int keylimit) {
87 public Data load(Data data, Row row) {
88 data.id = row.getUUID(0);
89 data.target = row.getString(1);
90 data.memo = row.getString(2);
91 data.start = row.getTimestamp(3);
92 data.expires = row.getTimestamp(4);
93 data.target_key = row.getString(5);
94 data.target_date = row.getTimestamp(6);
95 data.construct = row.getBytes(7);
100 protected void key(Data data, int idx, Object[] obj) {
105 protected void body(Data data, int _idx, Object[] obj) {
108 obj[idx] = data.target;
109 obj[++idx] = data.memo;
110 obj[++idx] = data.start;
111 obj[++idx] = data.expires;
112 obj[++idx] = data.target_key;
113 obj[++idx] = data.target_date;
114 obj[++idx] = data.construct;
118 private void init(AuthzTrans trans) {
120 String[] helpers = setCRUD(trans, TABLE, Data.class, new FLoader(KEYLIMIT));
122 // Other SELECT style statements... match with a local Method
123 psByStartAndTarget = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] +
124 " FROM future WHERE start <= ? and target = ? ALLOW FILTERING", new FLoader(2) {
126 protected void key(Data data, int _idx, Object[] obj) {
130 obj[++idx]=data.target;
135 public Result<List<Data>> readByStartAndTarget(AuthzTrans trans, Date start, String target) {
136 return psByStartAndTarget.read(trans, R_TEXT, new Object[]{start, target});
140 * Override create to add secondary ID to Subject in History, and create Data.ID, if it is null
142 public Result<FutureDAO.Data> create(AuthzTrans trans, FutureDAO.Data data, String id) {
143 // If ID is not set (typical), create one.
145 StringBuilder sb = new StringBuilder(trans.user());
146 sb.append(data.target);
147 sb.append(System.currentTimeMillis());
148 data.id = UUID.nameUUIDFromBytes(sb.toString().getBytes());
150 Result<ResultSet> rs = createPS.exec(trans, C_TEXT, data);
152 return Result.err(rs);
154 wasModified(trans, CRUD.create, data, null, id);
155 return Result.ok(data);
159 * Log Modification statements to History
161 * @param modified which CRUD action was done
162 * @param data entity data that needs a log entry
163 * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
166 protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
167 boolean memo = override.length>0 && override[0]!=null;
168 boolean subject = override.length>1 && override[1]!=null;
169 HistoryDAO.Data hd = HistoryDAO.newInitedData();
170 hd.user = trans.user();
171 hd.action = modified.name();
173 hd.subject = subject?override[1]:"";
174 hd.memo = memo?String.format("%s by %s", override[0], hd.user):data.memo;
176 if (historyDAO.create(trans, hd).status!=Status.OK) {
177 trans.error().log("Cannot log to History");