Collection syntax change because of Sonar
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / cass / OAuthTokenDAO.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.HashSet;
31 import java.util.List;
32 import java.util.Set;
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 import org.onap.aaf.misc.env.util.Chrono;
42
43 import com.datastax.driver.core.Cluster;
44 import com.datastax.driver.core.Row;
45
46 /**
47  * CredDAO manages credentials. 
48  * @author Jonathan
49  * Date: 7/19/13
50  */
51 public class OAuthTokenDAO extends CassDAOImpl<AuthzTrans,OAuthTokenDAO.Data> {
52     public static final String TABLE = "oauth_token";
53         private AbsCassDAO<AuthzTrans, Data>.PSInfo psByUser;
54     
55     public OAuthTokenDAO(AuthzTrans trans, Cluster cluster, String keyspace) {
56         super(trans, OAuthTokenDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
57         init(trans);
58     }
59     
60     public OAuthTokenDAO(AuthzTrans trans, AbsCassDAO<AuthzTrans,?> aDao) {
61                 super(trans, OAuthTokenDAO.class.getSimpleName(),aDao, Data.class, TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
62                 init(trans);
63     }
64
65
66     public static final int KEYLIMIT = 1;
67         public static class Data implements Bytification {
68                 public String                           id;
69                 public String                                   client_id;
70                 public String                                   user;
71                 public boolean                                  active;
72         public int                                              type;
73                 public String                                   refresh;
74         public Date                                     expires;
75         public long                                             exp_sec;
76         public String                                   content;  
77         public Set<String>                      scopes;
78         public String                                   state;
79         public String                                   req_ip; // requesting
80
81                 public Set<String> scopes(boolean mutable) {
82                         if (scopes == null) {
83                                 scopes = new HashSet<>();
84                         } else if (mutable && !(scopes instanceof HashSet)) {
85                                 scopes = new HashSet<>(scopes);
86                         }
87                         return scopes;
88                 }
89
90                 @Override
91                 public ByteBuffer bytify() throws IOException {
92                         ByteArrayOutputStream baos = new ByteArrayOutputStream();
93                         OAuthLoader.deflt.marshal(this,new DataOutputStream(baos));
94                         return ByteBuffer.wrap(baos.toByteArray());
95                 }
96                 
97                 @Override
98                 public void reconstitute(ByteBuffer bb) throws IOException {
99                         OAuthLoader.deflt.unmarshal(this, toDIS(bb));
100                 }
101
102                 public String toString() {
103                         return user.toString() + ' ' + id.toString() + ' ' + Chrono.dateTime(expires) + (active?"":"in") + "active";
104                 }
105     }
106
107     private static class OAuthLoader extends Loader<Data> implements Streamer<Data>{
108                 public static final int MAGIC=235677843;
109                 public static final int VERSION=1;
110                 public static final int BUFF_SIZE=96; // Note: only used when  
111         
112                 public static final OAuthLoader deflt = new OAuthLoader(KEYLIMIT);
113                 public OAuthLoader(int keylimit) {
114                     super(keylimit);
115                 }
116         
117                 @Override
118         public Data load(Data data, Row row) {
119             data.id = row.getString(0);
120             data.client_id = row.getString(1);
121             data.user = row.getString(2);
122             data.active = row.getBool(3);
123             data.type = row.getInt(4);
124             data.refresh = row.getString(5);
125             data.expires = row.getTimestamp(6);
126             data.exp_sec = row.getLong(7);
127             data.content = row.getString(8);
128             data.scopes = row.getSet(9,String.class);
129             data.state = row.getString(10);
130             data.req_ip = row.getString(11);
131             return data;
132         }
133
134         @Override
135         protected void key(final Data data, final int idx, Object[] obj) {
136             obj[idx] = data.id;
137         }
138
139         @Override
140         protected void body(final Data data, final int idx, Object[] obj) {
141             int i;
142             obj[i=idx] = data.client_id;
143             obj[++i] = data.user;
144             obj[++i] = data.active;
145             obj[++i] = data.type;
146             obj[++i] = data.refresh;
147             obj[++i] = data.expires;
148             obj[++i] = data.exp_sec;
149             obj[++i] = data.content;
150             obj[++i] = data.scopes;
151             obj[++i] = data.state;
152             obj[++i] = data.req_ip;
153         }
154
155                 @Override
156                 public void marshal(Data data, DataOutputStream os) throws IOException {
157                         writeHeader(os,MAGIC,VERSION);
158                         writeString(os, data.id);
159                         writeString(os, data.client_id);
160                         writeString(os, data.user);
161                         os.writeBoolean(data.active);
162                         os.writeInt(data.type);
163                         writeString(os, data.refresh);
164                         os.writeLong(data.expires==null?-1:data.expires.getTime());
165                         os.writeLong(data.exp_sec);
166                         writeString(os, data.content);
167                         writeStringSet(os,data.scopes);
168                         writeString(os, data.state);
169                         writeString(os, data.req_ip);
170                 }
171
172
173                 @Override
174                 public void unmarshal(Data data, DataInputStream is) throws IOException {
175                         /*int version = */readHeader(is,MAGIC,VERSION);
176                         // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
177                         byte[] buff = new byte[BUFF_SIZE]; // used only if fits
178                         data.id = readString(is,buff);
179                         data.client_id = readString(is,buff);
180                         data.user = readString(is,buff);
181                         data.active = is.readBoolean();
182                         data.type = is.readInt();
183                         data.refresh = readString(is,buff);
184                         long l = is.readLong();
185                         data.expires = l<0?null:new Date(l);
186                         data.exp_sec = is.readLong();
187                         data.content = readString(is,buff); // note, large strings still ok with small buffer
188                         data.scopes = readStringSet(is,buff);
189                         data.state = readString(is,buff);
190                         data.req_ip = readString(is,buff);
191                 }
192     }
193
194     private void init(AuthzTrans trans) {
195         String[] helpers = setCRUD(trans, TABLE, Data.class, OAuthLoader.deflt);
196         psByUser = new PSInfo(trans, "SELECT " + helpers[0] + " from " + TABLE + " WHERE user=?",OAuthLoader.deflt,readConsistency);
197     }
198
199         /**
200      * Log Modification statements to History
201      *
202      * @param modified        which CRUD action was done
203      * @param data            entity data that needs a log entry
204      * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
205      */
206     @Override
207     protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
208     }
209
210         public Result<List<Data>> readByUser(AuthzTrans trans, String user) {
211                 return psByUser.read(trans, "Read By User", new Object[]{user});
212         }
213 }