summaryrefslogtreecommitdiffstats
path: root/authz-cass/src/main/java/org/onap/aaf/dao/aaf/cass/FutureDAO.java
blob: 4fda97a1ae97da625d6005973568f43b27da5d04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package org.onap.aaf.dao.aaf.cass;

import java.nio.ByteBuffer;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.onap.aaf.authz.env.AuthzTrans;
import org.onap.aaf.authz.layer.Result;
import org.onap.aaf.dao.CassDAOImpl;
import org.onap.aaf.dao.DAOException;
import org.onap.aaf.dao.Loader;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;

/**
 * FutureDAO stores Construction information to create 
 * elements at another time.
 * 
 * 8/20/2013
 */
public class FutureDAO extends CassDAOImpl<AuthzTrans,FutureDAO.Data> {
    private static final String TABLE = "future";
	private final HistoryDAO historyDAO;
//	private static String createString;
	private PSInfo psByStartAndTarget;
	
    public FutureDAO(AuthzTrans trans, Cluster cluster, String keyspace) {
        super(trans, FutureDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
		historyDAO = new HistoryDAO(trans, this);
        init(trans);
    }

    public FutureDAO(AuthzTrans trans, HistoryDAO hDAO) {
        super(trans, FutureDAO.class.getSimpleName(),hDAO, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
        historyDAO=hDAO;
        init(trans);
    }

    public static final int KEYLIMIT = 1;
    public static class Data {
        public UUID         id;
        public String		target;
        public String		memo;
        public Date       	start;
        public Date       	expires;
        public ByteBuffer 	construct;  //   this is a blob in cassandra
    }

    private static class FLoader extends Loader<Data> {
        public FLoader() {
            super(KEYLIMIT);
        }

        public FLoader(int keylimit) {
            super(keylimit);
        }

        @Override
	public Data load(Data data, Row row) {
            data.id 		= row.getUUID(0);
            data.target		= row.getString(1);
            data.memo       = row.getString(2);
            data.start 		= row.getDate(3);
            data.expires 	= row.getDate(4);
            data.construct 	= row.getBytes(5);
            return data;
        }

        @Override
        protected void key(Data data, int idx, Object[] obj) {
            obj[idx] = data.id;
        }

        @Override
        protected void body(Data data, int _idx, Object[] obj) {
	    int idx = _idx;

            obj[idx] = data.target;
            obj[++idx] = data.memo;
            obj[++idx] = data.start;
            obj[++idx] = data.expires;
            obj[++idx] = data.construct;
        }
    }

    private void init(AuthzTrans trans) {
        // Set up sub-DAOs
        String[] helpers = setCRUD(trans, TABLE, Data.class, new FLoader(KEYLIMIT));

        // Uh, oh.  Can't use "now()" in Prepared Statements (at least at this level)
//		createString = "INSERT INTO " + TABLE + " ("+helpers[FIELD_COMMAS] +") VALUES (now(),";
//
//		// Need a specialty Creator to handle the "now()"
//		replace(CRUD.Create, new PSInfo(trans, "INSERT INTO future (" +  helpers[FIELD_COMMAS] +
//					") VALUES(now(),?,?,?,?,?)",new FLoader(0)));
		
		// Other SELECT style statements... match with a local Method
		psByStartAndTarget = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] +
				" FROM future WHERE start <= ? and target = ? ALLOW FILTERING", new FLoader(2) {
			@Override
			protected void key(Data data, int _idx, Object[] obj) {
			    	int idx = _idx;

				obj[idx]=data.start;
				obj[++idx]=data.target;
			}
		},readConsistency);
		

    }

    public Result<List<Data>> readByStartAndTarget(AuthzTrans trans, Date start, String target) throws DAOException {
		return psByStartAndTarget.read(trans, R_TEXT, new Object[]{start, target});
	}

    /**
	 * Override create to add secondary ID to Subject in History, and create Data.ID, if it is null
     */
	public Result<FutureDAO.Data> create(AuthzTrans trans,	FutureDAO.Data data, String id) {
		// If ID is not set (typical), create one.
		if(data.id==null) {
			StringBuilder sb = new StringBuilder(trans.user());
			sb.append(data.target);
			sb.append(System.currentTimeMillis());
			data.id = UUID.nameUUIDFromBytes(sb.toString().getBytes());
		}
		Result<ResultSet> rs = createPS.exec(trans, C_TEXT, data);
		if(rs.notOK()) {
			return Result.err(rs);
		}
		wasModified(trans, CRUD.create, data, null, id);
		return Result.ok(data);	
	}

	/**
	 * Log Modification statements to History
	 *
	 * @param modified        which CRUD action was done
	 * @param data            entity data that needs a log entry
	 * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
	 */
	@Override
	protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
		boolean memo = override.length>0 && override[0]!=null;
		boolean subject = override.length>1 && override[1]!=null;
		HistoryDAO.Data hd = HistoryDAO.newInitedData();
	    hd.user = trans.user();
		hd.action = modified.name();
		hd.target = TABLE;
		hd.subject = subject?override[1]:"";
	    hd.memo = memo?String.format("%s by %s", override[0], hd.user):data.memo;
	
		if(historyDAO.create(trans, hd).status!=Status.OK) {
	    	trans.error().log("Cannot log to History");
		}
	}
    
}