2 * ============LICENSE_START====================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
24 package org.onap.aaf.auth.dao.cass;
26 import java.io.ByteArrayOutputStream;
27 import java.io.DataInputStream;
28 import java.io.DataOutputStream;
29 import java.io.IOException;
30 import java.nio.ByteBuffer;
31 import java.util.Date;
32 import java.util.List;
34 import org.onap.aaf.auth.dao.AbsCassDAO;
35 import org.onap.aaf.auth.dao.Bytification;
36 import org.onap.aaf.auth.dao.CassDAOImpl;
37 import org.onap.aaf.auth.dao.Loader;
38 import org.onap.aaf.auth.dao.Streamer;
39 import org.onap.aaf.auth.env.AuthzTrans;
40 import org.onap.aaf.auth.layer.Result;
42 import com.datastax.driver.core.Cluster;
43 import com.datastax.driver.core.Row;
45 public class DelegateDAO extends CassDAOImpl<AuthzTrans, DelegateDAO.Data> {
47 public static final String TABLE = "delegate";
48 private PSInfo psByDelegate;
49 private static final int KEYLIMIT = 1;
51 public DelegateDAO(AuthzTrans trans, Cluster cluster, String keyspace) {
52 super(trans, DelegateDAO.class.getSimpleName(),cluster,keyspace,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
56 public DelegateDAO(AuthzTrans trans, AbsCassDAO<AuthzTrans,?> aDao) {
57 super(trans, DelegateDAO.class.getSimpleName(),aDao,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
62 public static class Data implements Bytification {
63 public static String user;
64 public static String delegate;
65 public static Date expires;
68 public ByteBuffer bytify() throws IOException {
69 ByteArrayOutputStream baos = new ByteArrayOutputStream();
70 DelegateLoader.dflt.marshal(this,new DataOutputStream(baos));
71 return ByteBuffer.wrap(baos.toByteArray());
75 public void reconstitute(ByteBuffer bb) throws IOException {
76 DelegateLoader.dflt.unmarshal(this, toDIS(bb));
80 private static class DelegateLoader extends Loader<Data> implements Streamer<Data>{
81 public static final int MAGIC=0xD823ACF2;
82 public static final int VERSION=1;
83 public static final int BUFF_SIZE=48;
85 public static final DelegateLoader dflt = new DelegateLoader(KEYLIMIT);
87 public DelegateLoader(int keylimit) {
92 public Data load(Data data, Row row) {
93 data.user = row.getString(0);
94 data.delegate = row.getString(1);
95 data.expires = row.getTimestamp(2);
100 protected void key(Data data, int idx, Object[] obj) {
105 protected void body(Data data, int _idx, Object[] obj) {
108 obj[idx]=data.delegate;
109 obj[++idx]=data.expires;
113 public void marshal(Data data, DataOutputStream os) throws IOException {
114 writeHeader(os,MAGIC,VERSION);
115 writeString(os, data.user);
116 writeString(os, data.delegate);
117 os.writeLong(data.expires.getTime());
121 public void unmarshal(Data data, DataInputStream is) throws IOException {
122 /*int version = */readHeader(is,MAGIC,VERSION);
123 // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
124 byte[] buff = new byte[BUFF_SIZE];
125 data.user = readString(is, buff);
126 data.delegate = readString(is,buff);
127 data.expires = new Date(is.readLong());
131 private void init(AuthzTrans trans) {
132 String[] helpers = setCRUD(trans, TABLE, Data.class, DelegateLoader.dflt);
133 psByDelegate = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
134 " WHERE delegate = ?", new DelegateLoader(1),readConsistency);
138 public Result<List<DelegateDAO.Data>> readByDelegate(AuthzTrans trans, String delegate) {
139 return psByDelegate.read(trans, R_TEXT, new Object[]{delegate});