AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / cass / DelegateDAO.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.dao.cass;
23
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;
31
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;
39
40 import com.datastax.driver.core.Cluster;
41 import com.datastax.driver.core.Row;
42
43 public class DelegateDAO extends CassDAOImpl<AuthzTrans, DelegateDAO.Data> {
44
45         public static final String TABLE = "delegate";
46         private PSInfo psByDelegate;
47         
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));
50                 init(trans);
51         }
52
53         public DelegateDAO(AuthzTrans trans, AbsCassDAO<AuthzTrans,?> aDao) {
54                 super(trans, DelegateDAO.class.getSimpleName(),aDao,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
55                 init(trans);
56         }
57         
58         private static final int KEYLIMIT = 1;
59         public static class Data implements Bytification {
60                 public String user;
61                 public String delegate;
62                 public Date expires;
63
64                 @Override
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());
69                 }
70                 
71                 @Override
72                 public void reconstitute(ByteBuffer bb) throws IOException {
73                         DelegateLoader.dflt.unmarshal(this, toDIS(bb));
74                 }
75         }
76         
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;
81
82                 public static final DelegateLoader dflt = new DelegateLoader(KEYLIMIT);
83
84                 public DelegateLoader(int keylimit) {
85                         super(keylimit);
86                 }
87                 
88                 @Override
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);
93                         return data;
94                 }
95
96                 @Override
97                 protected void key(Data data, int idx, Object[] obj) {
98                         obj[idx]=data.user;
99                 }
100
101                 @Override
102                 protected void body(Data data, int _idx, Object[] obj) {
103                         int idx = _idx;
104
105                         obj[idx]=data.delegate;
106                         obj[++idx]=data.expires;
107                 }
108
109                 @Override
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());
115                 }
116
117                 @Override
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());
125                 }
126         }       
127         
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);
132
133         }
134
135         public Result<List<DelegateDAO.Data>> readByDelegate(AuthzTrans trans, String delegate) {
136                 return psByDelegate.read(trans, R_TEXT, new Object[]{delegate});
137         }
138 }