1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.dao.aaf.cass;
\r
25 import java.nio.ByteBuffer;
\r
26 import java.util.Date;
\r
27 import java.util.List;
\r
28 import java.util.UUID;
\r
30 import org.onap.aaf.authz.env.AuthzTrans;
\r
31 import org.onap.aaf.authz.layer.Result;
\r
32 import org.onap.aaf.dao.CassDAOImpl;
\r
33 import org.onap.aaf.dao.DAOException;
\r
34 import org.onap.aaf.dao.Loader;
\r
36 import com.datastax.driver.core.Cluster;
\r
37 import com.datastax.driver.core.ResultSet;
\r
38 import com.datastax.driver.core.Row;
\r
41 * FutureDAO stores Construction information to create
\r
42 * elements at another time.
\r
46 public class FutureDAO extends CassDAOImpl<AuthzTrans,FutureDAO.Data> {
\r
47 private static final String TABLE = "future";
\r
48 private final HistoryDAO historyDAO;
\r
49 // private static String createString;
\r
50 private PSInfo psByStartAndTarget;
\r
52 public FutureDAO(AuthzTrans trans, Cluster cluster, String keyspace) {
\r
53 super(trans, FutureDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
\r
54 historyDAO = new HistoryDAO(trans, this);
\r
58 public FutureDAO(AuthzTrans trans, HistoryDAO hDAO) {
\r
59 super(trans, FutureDAO.class.getSimpleName(),hDAO, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
\r
64 public static final int KEYLIMIT = 1;
\r
65 public static class Data {
\r
67 public String target;
\r
70 public Date expires;
\r
71 public ByteBuffer construct; // this is a blob in cassandra
\r
74 private static class FLoader extends Loader<Data> {
\r
79 public FLoader(int keylimit) {
\r
84 public Data load(Data data, Row row) {
\r
85 data.id = row.getUUID(0);
\r
86 data.target = row.getString(1);
\r
87 data.memo = row.getString(2);
\r
88 data.start = row.getDate(3);
\r
89 data.expires = row.getDate(4);
\r
90 data.construct = row.getBytes(5);
\r
95 protected void key(Data data, int idx, Object[] obj) {
\r
100 protected void body(Data data, int _idx, Object[] obj) {
\r
103 obj[idx] = data.target;
\r
104 obj[++idx] = data.memo;
\r
105 obj[++idx] = data.start;
\r
106 obj[++idx] = data.expires;
\r
107 obj[++idx] = data.construct;
\r
111 private void init(AuthzTrans trans) {
\r
113 String[] helpers = setCRUD(trans, TABLE, Data.class, new FLoader(KEYLIMIT));
\r
115 // Uh, oh. Can't use "now()" in Prepared Statements (at least at this level)
\r
116 // createString = "INSERT INTO " + TABLE + " ("+helpers[FIELD_COMMAS] +") VALUES (now(),";
\r
118 // // Need a specialty Creator to handle the "now()"
\r
119 // replace(CRUD.Create, new PSInfo(trans, "INSERT INTO future (" + helpers[FIELD_COMMAS] +
\r
120 // ") VALUES(now(),?,?,?,?,?)",new FLoader(0)));
\r
122 // Other SELECT style statements... match with a local Method
\r
123 psByStartAndTarget = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] +
\r
124 " FROM future WHERE start <= ? and target = ? ALLOW FILTERING", new FLoader(2) {
\r
126 protected void key(Data data, int _idx, Object[] obj) {
\r
129 obj[idx]=data.start;
\r
130 obj[++idx]=data.target;
\r
132 },readConsistency);
\r
137 public Result<List<Data>> readByStartAndTarget(AuthzTrans trans, Date start, String target) throws DAOException {
\r
138 return psByStartAndTarget.read(trans, R_TEXT, new Object[]{start, target});
\r
142 * Override create to add secondary ID to Subject in History, and create Data.ID, if it is null
\r
144 public Result<FutureDAO.Data> create(AuthzTrans trans, FutureDAO.Data data, String id) {
\r
145 // If ID is not set (typical), create one.
\r
146 if(data.id==null) {
\r
147 StringBuilder sb = new StringBuilder(trans.user());
\r
148 sb.append(data.target);
\r
149 sb.append(System.currentTimeMillis());
\r
150 data.id = UUID.nameUUIDFromBytes(sb.toString().getBytes());
\r
152 Result<ResultSet> rs = createPS.exec(trans, C_TEXT, data);
\r
154 return Result.err(rs);
\r
156 wasModified(trans, CRUD.create, data, null, id);
\r
157 return Result.ok(data);
\r
161 * Log Modification statements to History
\r
163 * @param modified which CRUD action was done
\r
164 * @param data entity data that needs a log entry
\r
165 * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
\r
168 protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
\r
169 boolean memo = override.length>0 && override[0]!=null;
\r
170 boolean subject = override.length>1 && override[1]!=null;
\r
171 HistoryDAO.Data hd = HistoryDAO.newInitedData();
\r
172 hd.user = trans.user();
\r
173 hd.action = modified.name();
\r
175 hd.subject = subject?override[1]:"";
\r
176 hd.memo = memo?String.format("%s by %s", override[0], hd.user):data.memo;
\r
178 if(historyDAO.create(trans, hd).status!=Status.OK) {
\r
179 trans.error().log("Cannot log to History");
\r