6d6534ebbb700727842abee5cdc1e2b84e5e2ad2
[aaf/authz.git] / authz-cass / src / main / java / com / att / 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 com.att.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 com.att.authz.env.AuthzTrans;\r
34 import com.att.authz.layer.Result;\r
35 import com.att.dao.AbsCassDAO;\r
36 import com.att.dao.Bytification;\r
37 import com.att.dao.CassDAOImpl;\r
38 import com.att.dao.Loader;\r
39 import com.att.dao.Streamer;\r
40 import com.datastax.driver.core.Cluster;\r
41 import com.datastax.driver.core.Row;\r
42 \r
43 public class DelegateDAO extends CassDAOImpl<AuthzTrans, DelegateDAO.Data> {\r
44 \r
45         public static final String TABLE = "delegate";\r
46         private PSInfo psByDelegate;\r
47         \r
48         public DelegateDAO(AuthzTrans trans, Cluster cluster, String keyspace) {\r
49                 super(trans, DelegateDAO.class.getSimpleName(),cluster,keyspace,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));\r
50                 init(trans);\r
51         }\r
52 \r
53         public DelegateDAO(AuthzTrans trans, AbsCassDAO<AuthzTrans,?> aDao) {\r
54                 super(trans, DelegateDAO.class.getSimpleName(),aDao,Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));\r
55                 init(trans);\r
56         }\r
57         \r
58         private static final int KEYLIMIT = 1;\r
59         public static class Data implements Bytification {\r
60                 public String user;\r
61                 public String delegate;\r
62                 public Date expires;\r
63 \r
64                 @Override\r
65                 public ByteBuffer bytify() throws IOException {\r
66                         ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
67                         DelegateLoader.dflt.marshal(this,new DataOutputStream(baos));\r
68                         return ByteBuffer.wrap(baos.toByteArray());\r
69                 }\r
70                 \r
71                 @Override\r
72                 public void reconstitute(ByteBuffer bb) throws IOException {\r
73                         DelegateLoader.dflt.unmarshal(this, toDIS(bb));\r
74                 }\r
75         }\r
76         \r
77         private static class DelegateLoader extends Loader<Data> implements Streamer<Data>{\r
78                 public static final int MAGIC=0xD823ACF2;\r
79         public static final int VERSION=1;\r
80         public static final int BUFF_SIZE=48;\r
81 \r
82                 public static final DelegateLoader dflt = new DelegateLoader(KEYLIMIT);\r
83 \r
84                 public DelegateLoader(int keylimit) {\r
85                         super(keylimit);\r
86                 }\r
87                 \r
88                 @Override\r
89                 public Data load(Data data, Row row) {\r
90                         data.user = row.getString(0);\r
91                         data.delegate = row.getString(1);\r
92                         data.expires = row.getDate(2);\r
93                         return data;\r
94                 }\r
95 \r
96                 @Override\r
97                 protected void key(Data data, int idx, Object[] obj) {\r
98                         obj[idx]=data.user;\r
99                 }\r
100 \r
101                 @Override\r
102                 protected void body(Data data, int _idx, Object[] obj) {\r
103                         int idx = _idx;\r
104 \r
105                         obj[idx]=data.delegate;\r
106                         obj[++idx]=data.expires;\r
107                 }\r
108 \r
109                 @Override\r
110                 public void marshal(Data data, DataOutputStream os) throws IOException {\r
111                         writeHeader(os,MAGIC,VERSION);\r
112                         writeString(os, data.user);\r
113                         writeString(os, data.delegate);\r
114                         os.writeLong(data.expires.getTime());\r
115                 }\r
116 \r
117                 @Override\r
118                 public void unmarshal(Data data, DataInputStream is) throws IOException {\r
119                         /*int version = */readHeader(is,MAGIC,VERSION);\r
120                         // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields\r
121                         byte[] buff = new byte[BUFF_SIZE];\r
122                         data.user = readString(is, buff);\r
123                         data.delegate = readString(is,buff);\r
124                         data.expires = new Date(is.readLong());\r
125                 }\r
126         }       \r
127         \r
128         private void init(AuthzTrans trans) {\r
129                 String[] helpers = setCRUD(trans, TABLE, Data.class, DelegateLoader.dflt);\r
130                 psByDelegate = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +\r
131                                 " WHERE delegate = ?", new DelegateLoader(1),readConsistency);\r
132 \r
133         }\r
134 \r
135         public Result<List<DelegateDAO.Data>> readByDelegate(AuthzTrans trans, String delegate) {\r
136                 return psByDelegate.read(trans, R_TEXT, new Object[]{delegate});\r
137         }\r
138 }\r