Sonar Fixes, Formatting
[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  * 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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.dao.cass;
25
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;
33
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;
41
42 import com.datastax.driver.core.Cluster;
43 import com.datastax.driver.core.Row;
44
45 public class DelegateDAO extends CassDAOImpl<AuthzTrans, DelegateDAO.Data> {
46
47     public static final String TABLE = "delegate";
48     private PSInfo psByDelegate;
49     private static final int KEYLIMIT = 1;
50
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));
53         init(trans);
54     }
55
56     public DelegateDAO(AuthzTrans trans, AbsCassDAO<AuthzTrans,?> aDao) {
57         super(trans, DelegateDAO.class.getSimpleName(),aDao,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
58         init(trans);
59     }
60
61
62     public static class Data implements Bytification {
63         public String user;
64         public String delegate;
65         public Date expires;
66
67         @Override
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());
72         }
73
74         @Override
75         public void reconstitute(ByteBuffer bb) throws IOException {
76             DelegateLoader.dflt.unmarshal(this, toDIS(bb));
77         }
78     }
79
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;
84
85         public static final DelegateLoader dflt = new DelegateLoader(KEYLIMIT);
86
87         public DelegateLoader(int keylimit) {
88             super(keylimit);
89         }
90
91         @Override
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);
96             return data;
97         }
98
99         @Override
100         protected void key(Data data, int idx, Object[] obj) {
101             obj[idx]=data.user;
102         }
103
104         @Override
105         protected void body(Data data, int _idx, Object[] obj) {
106                 int idx = _idx;
107
108             obj[idx]=data.delegate;
109             obj[++idx]=data.expires;
110         }
111
112         @Override
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());
118         }
119
120         @Override
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());
128         }
129     }
130
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);
135
136     }
137
138     public Result<List<DelegateDAO.Data>> readByDelegate(AuthzTrans trans, String delegate) {
139         return psByDelegate.read(trans, R_TEXT, new Object[]{delegate});
140     }
141 }