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