2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.auth.dao.cass;
24 import java.io.ByteArrayOutputStream;
25 import java.io.DataInputStream;
26 import java.io.DataOutputStream;
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29 import java.util.Date;
30 import java.util.List;
32 import org.onap.aaf.auth.dao.AbsCassDAO;
33 import org.onap.aaf.auth.dao.Bytification;
34 import org.onap.aaf.auth.dao.CassDAOImpl;
35 import org.onap.aaf.auth.dao.Loader;
36 import org.onap.aaf.auth.dao.Streamer;
37 import org.onap.aaf.auth.env.AuthzTrans;
38 import org.onap.aaf.auth.layer.Result;
40 import com.datastax.driver.core.Cluster;
41 import com.datastax.driver.core.Row;
43 public class DelegateDAO extends CassDAOImpl<AuthzTrans, DelegateDAO.Data> {
45 public static final String TABLE = "delegate";
46 private PSInfo psByDelegate;
48 public DelegateDAO(AuthzTrans trans, Cluster cluster, String keyspace) {
49 super(trans, DelegateDAO.class.getSimpleName(),cluster,keyspace,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
53 public DelegateDAO(AuthzTrans trans, AbsCassDAO<AuthzTrans,?> aDao) {
54 super(trans, DelegateDAO.class.getSimpleName(),aDao,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
58 private static final int KEYLIMIT = 1;
59 public static class Data implements Bytification {
61 public String delegate;
65 public ByteBuffer bytify() throws IOException {
66 ByteArrayOutputStream baos = new ByteArrayOutputStream();
67 DelegateLoader.dflt.marshal(this,new DataOutputStream(baos));
68 return ByteBuffer.wrap(baos.toByteArray());
72 public void reconstitute(ByteBuffer bb) throws IOException {
73 DelegateLoader.dflt.unmarshal(this, toDIS(bb));
77 private static class DelegateLoader extends Loader<Data> implements Streamer<Data>{
78 public static final int MAGIC=0xD823ACF2;
79 public static final int VERSION=1;
80 public static final int BUFF_SIZE=48;
82 public static final DelegateLoader dflt = new DelegateLoader(KEYLIMIT);
84 public DelegateLoader(int keylimit) {
89 public Data load(Data data, Row row) {
90 data.user = row.getString(0);
91 data.delegate = row.getString(1);
92 data.expires = row.getTimestamp(2);
97 protected void key(Data data, int idx, Object[] obj) {
102 protected void body(Data data, int _idx, Object[] obj) {
105 obj[idx]=data.delegate;
106 obj[++idx]=data.expires;
110 public void marshal(Data data, DataOutputStream os) throws IOException {
111 writeHeader(os,MAGIC,VERSION);
112 writeString(os, data.user);
113 writeString(os, data.delegate);
114 os.writeLong(data.expires.getTime());
118 public void unmarshal(Data data, DataInputStream is) throws IOException {
119 /*int version = */readHeader(is,MAGIC,VERSION);
120 // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
121 byte[] buff = new byte[BUFF_SIZE];
122 data.user = readString(is, buff);
123 data.delegate = readString(is,buff);
124 data.expires = new Date(is.readLong());
128 private void init(AuthzTrans trans) {
129 String[] helpers = setCRUD(trans, TABLE, Data.class, DelegateLoader.dflt);
130 psByDelegate = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
131 " WHERE delegate = ?", new DelegateLoader(1),readConsistency);
135 public Result<List<DelegateDAO.Data>> readByDelegate(AuthzTrans trans, String delegate) {
136 return psByDelegate.read(trans, R_TEXT, new Object[]{delegate});