6ff712089a306ecd7cadb4bc72ccab019c8e204f
[aaf/authz.git] / authz-cass / src / main / java / org / onap / aaf / dao / aaf / cass / DelegateDAO.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.dao.aaf.cass;\r
24 \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
32 \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
40 \r
41 import com.datastax.driver.core.Cluster;\r
42 import com.datastax.driver.core.Row;\r
43 \r
44 public class DelegateDAO extends CassDAOImpl<AuthzTrans, DelegateDAO.Data> {\r
45 \r
46         public static final String TABLE = "delegate";\r
47         private PSInfo psByDelegate;\r
48         \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
51                 init(trans);\r
52         }\r
53 \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
56                 init(trans);\r
57         }\r
58         \r
59         private static final int KEYLIMIT = 1;\r
60         public static class Data implements Bytification {\r
61                 public String user;\r
62                 public String delegate;\r
63                 public Date expires;\r
64 \r
65                 @Override\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
70                 }\r
71                 \r
72                 @Override\r
73                 public void reconstitute(ByteBuffer bb) throws IOException {\r
74                         DelegateLoader.dflt.unmarshal(this, toDIS(bb));\r
75                 }\r
76         }\r
77         \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
82 \r
83                 public static final DelegateLoader dflt = new DelegateLoader(KEYLIMIT);\r
84 \r
85                 public DelegateLoader(int keylimit) {\r
86                         super(keylimit);\r
87                 }\r
88                 \r
89                 @Override\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
94                         return data;\r
95                 }\r
96 \r
97                 @Override\r
98                 protected void key(Data data, int idx, Object[] obj) {\r
99                         obj[idx]=data.user;\r
100                 }\r
101 \r
102                 @Override\r
103                 protected void body(Data data, int _idx, Object[] obj) {\r
104                         int idx = _idx;\r
105 \r
106                         obj[idx]=data.delegate;\r
107                         obj[++idx]=data.expires;\r
108                 }\r
109 \r
110                 @Override\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
116                 }\r
117 \r
118                 @Override\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
126                 }\r
127         }       \r
128         \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
133 \r
134         }\r
135 \r
136         public Result<List<DelegateDAO.Data>> readByDelegate(AuthzTrans trans, String delegate) {\r
137                 return psByDelegate.read(trans, R_TEXT, new Object[]{delegate});\r
138         }\r
139 }\r