1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * Copyright © 2017 Amdocs
\r
7 * * ===========================================================================
\r
8 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * * you may not use this file except in compliance with the License.
\r
10 * * You may obtain a copy of the License at
\r
12 * * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * * Unless required by applicable law or agreed to in writing, software
\r
15 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * * See the License for the specific language governing permissions and
\r
18 * * limitations under the License.
\r
19 * * ============LICENSE_END====================================================
\r
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.dao.aaf.cass;
\r
26 import java.io.ByteArrayOutputStream;
\r
27 import java.io.DataInputStream;
\r
28 import java.io.DataOutputStream;
\r
29 import java.io.IOException;
\r
30 import java.nio.ByteBuffer;
\r
31 import java.util.HashSet;
\r
32 import java.util.List;
\r
33 import java.util.Set;
\r
35 import com.att.authz.env.AuthzTrans;
\r
36 import com.att.authz.layer.Result;
\r
37 import com.att.dao.Bytification;
\r
38 import com.att.dao.Cached;
\r
39 import com.att.dao.CassAccess;
\r
40 import com.att.dao.CassDAOImpl;
\r
41 import com.att.dao.Loader;
\r
42 import com.att.dao.Streamer;
\r
43 import com.att.dao.aaf.hl.Question;
\r
44 import com.att.inno.env.APIException;
\r
45 import com.att.inno.env.util.Split;
\r
46 import com.datastax.driver.core.Cluster;
\r
47 import com.datastax.driver.core.Row;
\r
48 import com.datastax.driver.core.exceptions.DriverException;
\r
50 public class RoleDAO extends CassDAOImpl<AuthzTrans,RoleDAO.Data> {
\r
52 public static final String TABLE = "role";
\r
53 public static final int CACHE_SEG = 0x40; // yields segment 0x0-0x3F
\r
55 private final HistoryDAO historyDAO;
\r
56 private final CacheInfoDAO infoDAO;
\r
58 private PSInfo psChildren, psNS, psName;
\r
60 public RoleDAO(AuthzTrans trans, Cluster cluster, String keyspace) throws APIException, IOException {
\r
61 super(trans, RoleDAO.class.getSimpleName(),cluster,keyspace,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
\r
63 historyDAO = new HistoryDAO(trans, this);
\r
64 infoDAO = new CacheInfoDAO(trans,this);
\r
68 public RoleDAO(AuthzTrans trans, HistoryDAO hDAO, CacheInfoDAO ciDAO) {
\r
69 super(trans, RoleDAO.class.getSimpleName(),hDAO,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
\r
76 //////////////////////////////////////////
\r
77 // Data Definition, matches Cassandra DM
\r
78 //////////////////////////////////////////
\r
79 private static final int KEYLIMIT = 2;
\r
81 * Data class that matches the Cassandra Table "role"
\r
83 public static class Data extends CacheableData implements Bytification {
\r
86 public Set<String> perms;
\r
87 public String description;
\r
89 ////////////////////////////////////////
\r
91 public Set<String> perms(boolean mutable) {
\r
92 if (perms == null) {
\r
93 perms = new HashSet<String>();
\r
94 } else if (mutable && !(perms instanceof HashSet)) {
\r
95 perms = new HashSet<String>(perms);
\r
100 public static Data create(NsDAO.Data ns, String name) {
\r
101 NsSplit nss = new NsSplit(ns,name);
\r
102 RoleDAO.Data rv = new Data();
\r
108 public String fullName() {
\r
109 return ns + '.' + name;
\r
112 public String encode() {
\r
113 return ns + '|' + name;
\r
117 * Decode Perm String, including breaking into appropriate Namespace
\r
124 public static Result<Data> decode(AuthzTrans trans, Question q, String r) {
\r
125 String[] ss = Split.splitTrim('|', r,2);
\r
126 Data data = new Data();
\r
127 if(ss[1]==null) { // older 1 part encoding must be evaluated for NS
\r
128 Result<NsSplit> nss = q.deriveNsSplit(trans, ss[0]);
\r
130 return Result.err(nss);
\r
132 data.ns=nss.value.ns;
\r
133 data.name=nss.value.name;
\r
134 } else { // new 4 part encoding
\r
138 return Result.ok(data);
\r
142 * Decode from UserRole Data
\r
146 public static RoleDAO.Data decode(UserRoleDAO.Data urdd) {
\r
147 RoleDAO.Data rd = new RoleDAO.Data();
\r
149 rd.name = urdd.rname;
\r
155 * Decode Perm String, including breaking into appropriate Namespace
\r
162 public static Result<String[]> decodeToArray(AuthzTrans trans, Question q, String p) {
\r
163 String[] ss = Split.splitTrim('|', p,2);
\r
164 if(ss[1]==null) { // older 1 part encoding must be evaluated for NS
\r
165 Result<NsSplit> nss = q.deriveNsSplit(trans, ss[0]);
\r
167 return Result.err(nss);
\r
169 ss[0] = nss.value.ns;
\r
170 ss[1] = nss.value.name;
\r
172 return Result.ok(ss);
\r
176 public int[] invalidate(Cached<?,?> cache) {
\r
178 seg(cache,ns,name),
\r
185 public ByteBuffer bytify() throws IOException {
\r
186 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
187 RoleLoader.deflt.marshal(this,new DataOutputStream(baos));
\r
188 return ByteBuffer.wrap(baos.toByteArray());
\r
192 public void reconstitute(ByteBuffer bb) throws IOException {
\r
193 RoleLoader.deflt.unmarshal(this, toDIS(bb));
\r
197 public String toString() {
\r
198 return ns + '.' + name;
\r
202 private static class RoleLoader extends Loader<Data> implements Streamer<Data> {
\r
203 public static final int MAGIC=923577343;
\r
204 public static final int VERSION=1;
\r
205 public static final int BUFF_SIZE=96;
\r
207 public static final RoleLoader deflt = new RoleLoader(KEYLIMIT);
\r
209 public RoleLoader(int keylimit) {
\r
214 public Data load(Data data, Row row) {
\r
215 // Int more efficient
\r
216 data.ns = row.getString(0);
\r
217 data.name = row.getString(1);
\r
218 data.perms = row.getSet(2,String.class);
\r
219 data.description = row.getString(3);
\r
224 protected void key(Data data, int _idx, Object[] obj) {
\r
227 obj[++idx]=data.name;
\r
231 protected void body(Data data, int _idx, Object[] obj) {
\r
233 obj[idx]=data.perms;
\r
234 obj[++idx]=data.description;
\r
238 public void marshal(Data data, DataOutputStream os) throws IOException {
\r
239 writeHeader(os,MAGIC,VERSION);
\r
240 writeString(os, data.ns);
\r
241 writeString(os, data.name);
\r
242 writeStringSet(os,data.perms);
\r
243 writeString(os, data.description);
\r
247 public void unmarshal(Data data, DataInputStream is) throws IOException {
\r
248 /*int version = */readHeader(is,MAGIC,VERSION);
\r
249 // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
\r
250 byte[] buff = new byte[BUFF_SIZE];
\r
251 data.ns = readString(is, buff);
\r
252 data.name = readString(is,buff);
\r
253 data.perms = readStringSet(is,buff);
\r
254 data.description = readString(is,buff);
\r
258 private void init(AuthzTrans trans) {
\r
259 String[] helpers = setCRUD(trans, TABLE, Data.class, RoleLoader.deflt);
\r
261 psNS = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
\r
262 " WHERE ns = ?", new RoleLoader(1),readConsistency);
\r
264 psName = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
\r
265 " WHERE name = ?", new RoleLoader(1),readConsistency);
\r
267 psChildren = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
\r
268 " WHERE ns=? AND name > ? AND name < ?",
\r
269 new RoleLoader(3) {
\r
271 protected void key(Data data, int _idx, Object[] obj) {
\r
273 obj[idx] = data.ns;
\r
274 obj[++idx]=data.name + DOT;
\r
275 obj[++idx]=data.name + DOT_PLUS_ONE;
\r
277 },readConsistency);
\r
281 public Result<List<Data>> readNS(AuthzTrans trans, String ns) {
\r
282 return psNS.read(trans, R_TEXT + " NS " + ns, new Object[]{ns});
\r
285 public Result<List<Data>> readName(AuthzTrans trans, String name) {
\r
286 return psName.read(trans, R_TEXT + name, new Object[]{name});
\r
289 public Result<List<Data>> readChildren(AuthzTrans trans, String ns, String role) {
\r
290 if(role.length()==0 || "*".equals(role)) {
\r
291 return psChildren.read(trans, R_TEXT, new Object[]{ns, FIRST_CHAR, LAST_CHAR});
\r
293 return psChildren.read(trans, R_TEXT, new Object[]{ns, role+DOT, role+DOT_PLUS_ONE});
\r
298 * Add a single Permission to the Role's Permission Collection
\r
307 public Result<Void> addPerm(AuthzTrans trans, RoleDAO.Data role, PermDAO.Data perm) {
\r
308 // Note: Prepared Statements for Collection updates aren't supported
\r
309 String pencode = perm.encode();
\r
311 getSession(trans).execute(UPDATE_SP + TABLE + " SET perms = perms + {'" +
\r
312 pencode + "'} WHERE " +
\r
313 "ns = '" + role.ns + "' AND name = '" + role.name + "';");
\r
314 } catch (DriverException | APIException | IOException e) {
\r
315 reportPerhapsReset(trans,e);
\r
316 return Result.err(Result.ERR_Backend, CassAccess.ERR_ACCESS_MSG);
\r
319 wasModified(trans, CRUD.update, role, "Added permission " + pencode + " to role " + role.fullName());
\r
320 return Result.ok();
\r
324 * Remove a single Permission from the Role's Permission Collection
\r
332 public Result<Void> delPerm(AuthzTrans trans, RoleDAO.Data role, PermDAO.Data perm) {
\r
333 // Note: Prepared Statements for Collection updates aren't supported
\r
335 String pencode = perm.encode();
\r
339 getSession(trans).execute(UPDATE_SP + TABLE + " SET perms = perms - {'" +
\r
340 pencode + "'} WHERE " +
\r
341 "ns = '" + role.ns + "' AND name = '" + role.name + "';");
\r
342 } catch (DriverException | APIException | IOException e) {
\r
343 reportPerhapsReset(trans,e);
\r
344 return Result.err(Result.ERR_Backend, CassAccess.ERR_ACCESS_MSG);
\r
347 //TODO how can we tell when it doesn't?
\r
348 wasModified(trans, CRUD.update, role, "Removed permission " + pencode + " from role " + role.fullName() );
\r
349 return Result.ok();
\r
353 * Add description to role
\r
358 * @param description
\r
361 public Result<Void> addDescription(AuthzTrans trans, String ns, String name, String description) {
\r
363 getSession(trans).execute(UPDATE_SP + TABLE + " SET description = '"
\r
364 + description + "' WHERE ns = '" + ns + "' AND name = '" + name + "';");
\r
365 } catch (DriverException | APIException | IOException e) {
\r
366 reportPerhapsReset(trans,e);
\r
367 return Result.err(Result.ERR_Backend, CassAccess.ERR_ACCESS_MSG);
\r
370 Data data = new Data();
\r
373 wasModified(trans, CRUD.update, data, "Added description " + description + " to role " + data.fullName(), null );
\r
374 return Result.ok();
\r
379 * Log Modification statements to History
\r
380 * @param modified which CRUD action was done
\r
381 * @param data entity data that needs a log entry
\r
382 * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
\r
385 protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
\r
386 boolean memo = override.length>0 && override[0]!=null;
\r
387 boolean subject = override.length>1 && override[1]!=null;
\r
389 HistoryDAO.Data hd = HistoryDAO.newInitedData();
\r
390 hd.user = trans.user();
\r
391 hd.action = modified.name();
\r
393 hd.subject = subject ? override[1] : data.fullName();
\r
394 hd.memo = memo ? override[0] : (data.fullName() + " was " + modified.name() + 'd' );
\r
395 if(modified==CRUD.delete) {
\r
397 hd.reconstruct = data.bytify();
\r
398 } catch (IOException e) {
\r
399 trans.error().log(e,"Could not serialize RoleDAO.Data");
\r
403 if(historyDAO.create(trans, hd).status!=Status.OK) {
\r
404 trans.error().log("Cannot log to History");
\r
406 if(infoDAO.touch(trans, TABLE,data.invalidate(cache)).notOK()) {
\r
407 trans.error().log("Cannot touch CacheInfo for Role");
\r