96cbf28a4d439f6af4c791347bc57d4f729192d0
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / cass / FutureDAO.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.dao.cass;
25
26 import java.nio.ByteBuffer;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.UUID;
30
31 import org.onap.aaf.auth.dao.CassDAOImpl;
32 import org.onap.aaf.auth.dao.DAOException;
33 import org.onap.aaf.auth.dao.Loader;
34 import org.onap.aaf.auth.env.AuthzTrans;
35 import org.onap.aaf.auth.layer.Result;
36
37 import com.datastax.driver.core.Cluster;
38 import com.datastax.driver.core.ResultSet;
39 import com.datastax.driver.core.Row;
40
41 /**
42  * FutureDAO stores Construction information to create 
43  * elements at another time.
44  * 
45  * @author Jonathan
46  * 8/20/2013
47  */
48 public class FutureDAO extends CassDAOImpl<AuthzTrans,FutureDAO.Data> {
49     private static final String TABLE = "future";
50     private final HistoryDAO historyDAO;
51     private PSInfo psByStartAndTarget;
52     public static final int KEYLIMIT = 1;
53
54     public FutureDAO(AuthzTrans trans, Cluster cluster, String keyspace) {
55         super(trans, FutureDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
56         historyDAO = new HistoryDAO(trans, this);
57         init(trans);
58     }
59
60     public FutureDAO(AuthzTrans trans, HistoryDAO hDAO) {
61         super(trans, FutureDAO.class.getSimpleName(),hDAO, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
62         historyDAO=hDAO;
63         init(trans);
64     }
65
66
67     public static class Data {
68         public UUID         id;
69         public String        target;
70         public String        memo;
71         public Date           start;
72         public Date           expires;
73         public ByteBuffer     construct;  //   this is a blob in cassandra
74     }
75
76     private static class FLoader extends Loader<Data> {
77         public FLoader() {
78             super(KEYLIMIT);
79         }
80
81         public FLoader(int keylimit) {
82             super(keylimit);
83         }
84
85         @Override
86     public Data load(Data data, Row row) {
87             data.id         = row.getUUID(0);
88             data.target        = row.getString(1);
89             data.memo       = row.getString(2);
90             data.start         = row.getTimestamp(3);
91             data.expires     = row.getTimestamp(4);
92             data.construct     = row.getBytes(5);
93             return data;
94         }
95
96         @Override
97         protected void key(Data data, int idx, Object[] obj) {
98             obj[idx] = data.id;
99         }
100
101         @Override
102         protected void body(Data data, int _idx, Object[] obj) {
103         int idx = _idx;
104
105             obj[idx] = data.target;
106             obj[++idx] = data.memo;
107             obj[++idx] = data.start;
108             obj[++idx] = data.expires;
109             obj[++idx] = data.construct;
110         }
111     }
112
113     private void init(AuthzTrans trans) {
114         // Set up sub-DAOs
115         String[] helpers = setCRUD(trans, TABLE, Data.class, new FLoader(KEYLIMIT));
116
117         // Other SELECT style statements... match with a local Method
118         psByStartAndTarget = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] +
119                 " FROM future WHERE start <= ? and target = ? ALLOW FILTERING", new FLoader(2) {
120             @Override
121             protected void key(Data data, int _idx, Object[] obj) {
122                     int idx = _idx;
123
124                 obj[idx]=data.start;
125                 obj[++idx]=data.target;
126             }
127         },readConsistency);
128         
129
130     }
131
132     public Result<List<Data>> readByStartAndTarget(AuthzTrans trans, Date start, String target) {
133         return psByStartAndTarget.read(trans, R_TEXT, new Object[]{start, target});
134     }
135
136     /**
137      * Override create to add secondary ID to Subject in History, and create Data.ID, if it is null
138      */
139     public Result<FutureDAO.Data> create(AuthzTrans trans,    FutureDAO.Data data, String id) {
140         // If ID is not set (typical), create one.
141         if (data.id==null) {
142             StringBuilder sb = new StringBuilder(trans.user());
143             sb.append(data.target);
144             sb.append(System.currentTimeMillis());
145             data.id = UUID.nameUUIDFromBytes(sb.toString().getBytes());
146         }
147         Result<ResultSet> rs = createPS.exec(trans, C_TEXT, data);
148         if (rs.notOK()) {
149             return Result.err(rs);
150         }
151         wasModified(trans, CRUD.create, data, null, id);
152         return Result.ok(data);    
153     }
154
155     /**
156      * Log Modification statements to History
157      *
158      * @param modified        which CRUD action was done
159      * @param data            entity data that needs a log entry
160      * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
161      */
162     @Override
163     protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
164         boolean memo = override.length>0 && override[0]!=null;
165         boolean subject = override.length>1 && override[1]!=null;
166         HistoryDAO.Data hd = HistoryDAO.newInitedData();
167         hd.user = trans.user();
168         hd.action = modified.name();
169         hd.target = TABLE;
170         hd.subject = subject?override[1]:"";
171         hd.memo = memo?String.format("%s by %s", override[0], hd.user):data.memo;
172     
173         if (historyDAO.create(trans, hd).status!=Status.OK) {
174             trans.error().log("Cannot log to History");
175         }
176     }
177     
178 }