Update AAF Version 1.0.0
[aaf/authz.git] / authz-cass / src / main / java / com / att / dao / aaf / cass / ArtiDAO.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.HashSet;\r
32 import java.util.List;\r
33 import java.util.Set;\r
34 \r
35 import com.att.authz.env.AuthzTrans;\r
36 import com.att.authz.layer.Result;\r
37 import com.att.dao.Bytification;\r
38 import com.att.dao.CassDAOImpl;\r
39 import com.att.dao.Loader;\r
40 import com.att.dao.Streamer;\r
41 import com.att.inno.env.util.Chrono;\r
42 import com.datastax.driver.core.Cluster;\r
43 import com.datastax.driver.core.Row;\r
44 \r
45 /**\r
46  * CredDAO manages credentials. \r
47  * Date: 7/19/13\r
48  */\r
49 public class ArtiDAO extends CassDAOImpl<AuthzTrans,ArtiDAO.Data> {\r
50     public static final String TABLE = "artifact";\r
51     \r
52     private HistoryDAO historyDAO;\r
53     private PSInfo psByMechID,psByMachine;\r
54         \r
55     public ArtiDAO(AuthzTrans trans, Cluster cluster, String keyspace) {\r
56         super(trans, ArtiDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));\r
57         init(trans);\r
58     }\r
59 \r
60     public ArtiDAO(AuthzTrans trans, HistoryDAO hDao, CacheInfoDAO ciDao) {\r
61         super(trans, ArtiDAO.class.getSimpleName(),hDao, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));\r
62         historyDAO = hDao;\r
63         init(trans);\r
64     }\r
65 \r
66     public static final int KEYLIMIT = 2;\r
67         public static class Data implements Bytification {\r
68                 public String                           mechid;\r
69                 public String                           machine;\r
70         private Set<String>                     type;\r
71         public String                                   sponsor;\r
72         public String                                   ca;\r
73         public String                                   dir;\r
74         public String                                   appName;\r
75         public String                                   os_user;\r
76         public String                                   notify;\r
77         public Date                                     expires;\r
78         public int                                              renewDays;\r
79         \r
80 //      // Getters\r
81                 public Set<String> type(boolean mutable) {\r
82                         if (type == null) {\r
83                                 type = new HashSet<String>();\r
84                         } else if (mutable && !(type instanceof HashSet)) {\r
85                                 type = new HashSet<String>(type);\r
86                         }\r
87                         return type;\r
88                 }\r
89 \r
90 \r
91                 @Override\r
92                 public ByteBuffer bytify() throws IOException {\r
93                         ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
94                         ArtifactLoader.deflt.marshal(this,new DataOutputStream(baos));\r
95                         return ByteBuffer.wrap(baos.toByteArray());\r
96                 }\r
97                 \r
98                 @Override\r
99                 public void reconstitute(ByteBuffer bb) throws IOException {\r
100                         ArtifactLoader.deflt.unmarshal(this, toDIS(bb));\r
101                 }\r
102 \r
103                 public String toString() {\r
104                         return mechid + ' ' + machine + ' ' + Chrono.dateTime(expires);\r
105                 }\r
106     }\r
107 \r
108     private static class ArtifactLoader extends Loader<Data> implements Streamer<Data>{\r
109                 public static final int MAGIC=95829343;\r
110         public static final int VERSION=1;\r
111         public static final int BUFF_SIZE=48; // Note: \r
112 \r
113         public static final ArtifactLoader deflt = new ArtifactLoader(KEYLIMIT);\r
114         public ArtifactLoader(int keylimit) {\r
115             super(keylimit);\r
116         }\r
117 \r
118         @Override\r
119         public Data load(Data data, Row row) {\r
120             data.mechid = row.getString(0);\r
121             data.machine = row.getString(1);\r
122             data.type = row.getSet(2, String.class);\r
123             data.sponsor = row.getString(3);\r
124             data.ca = row.getString(4);\r
125             data.dir = row.getString(5);\r
126             data.appName = row.getString(6);\r
127             data.os_user = row.getString(7);\r
128             data.notify = row.getString(8);\r
129             data.expires = row.getDate(9);\r
130             data.renewDays = row.getInt(10);\r
131             return data;\r
132         }\r
133 \r
134         @Override\r
135         protected void key(final Data data, final int idx, Object[] obj) {\r
136                 int i;\r
137             obj[i=idx] = data.mechid;\r
138             obj[++i] = data.machine;\r
139         }\r
140 \r
141         @Override\r
142         protected void body(final Data data, final int idx, Object[] obj) {\r
143             int i;\r
144             obj[i=idx] = data.type;\r
145             obj[++i] = data.sponsor;\r
146             obj[++i] = data.ca;\r
147             obj[++i] = data.dir;\r
148             obj[++i] = data.appName;\r
149             obj[++i] = data.os_user;\r
150             obj[++i] = data.notify;\r
151             obj[++i] = data.expires;\r
152             obj[++i] = data.renewDays;\r
153         }\r
154 \r
155                 @Override\r
156                 public void marshal(Data data, DataOutputStream os) throws IOException {\r
157                         writeHeader(os,MAGIC,VERSION);\r
158                         writeString(os, data.mechid);\r
159                         writeString(os, data.machine);\r
160                         os.writeInt(data.type.size());\r
161                         for(String s : data.type) {\r
162                                 writeString(os, s);\r
163                         }\r
164                         writeString(os, data.sponsor);\r
165                         writeString(os, data.ca);\r
166                         writeString(os, data.dir);\r
167                         writeString(os, data.appName);\r
168                         writeString(os, data.os_user);\r
169                         writeString(os, data.notify);\r
170                         os.writeLong(data.expires==null?-1:data.expires.getTime());\r
171                         os.writeInt(data.renewDays);\r
172                 }\r
173 \r
174                 @Override\r
175                 public void unmarshal(Data data, DataInputStream is) throws IOException {\r
176                         /*int version = */readHeader(is,MAGIC,VERSION);\r
177                         // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields\r
178                         byte[] buff = new byte[BUFF_SIZE];\r
179                         data.mechid = readString(is,buff);\r
180                         data.machine = readString(is,buff);\r
181                         int size = is.readInt();\r
182                         data.type = new HashSet<String>(size);\r
183                         for(int i=0;i<size;++i) {\r
184                                 data.type.add(readString(is,buff));\r
185                         }\r
186                         data.sponsor = readString(is,buff);\r
187                         data.ca = readString(is,buff);\r
188                         data.dir = readString(is,buff);\r
189                         data.appName = readString(is,buff);\r
190                         data.os_user = readString(is,buff);\r
191                         data.notify = readString(is,buff);\r
192                         long l = is.readLong();\r
193                         data.expires = l<0?null:new Date(l);\r
194                         data.renewDays = is.readInt();\r
195                 }\r
196     }\r
197 \r
198     private void init(AuthzTrans trans) {\r
199         // Set up sub-DAOs\r
200         if(historyDAO==null) {\r
201                 historyDAO = new HistoryDAO(trans,this);\r
202         }\r
203         \r
204         String[] helpers = setCRUD(trans, TABLE, Data.class, ArtifactLoader.deflt);\r
205 \r
206                 psByMechID = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE + \r
207                                 " WHERE mechid = ?", new ArtifactLoader(1) {\r
208                         @Override\r
209                         protected void key(Data data, int idx, Object[] obj) {\r
210                                 obj[idx]=data.type;\r
211                         }\r
212                 },readConsistency);\r
213 \r
214                 psByMachine = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE + \r
215                                 " WHERE machine = ?", new ArtifactLoader(1) {\r
216                         @Override\r
217                         protected void key(Data data, int idx, Object[] obj) {\r
218                                 obj[idx]=data.type;\r
219                         }\r
220                 },readConsistency);\r
221 \r
222     }\r
223     \r
224         \r
225     public Result<List<Data>> readByMechID(AuthzTrans trans, String mechid) {\r
226                 return psByMechID.read(trans, R_TEXT, new Object[]{mechid});\r
227         }\r
228 \r
229         public Result<List<ArtiDAO.Data>> readByMachine(AuthzTrans trans, String machine) {\r
230                 return psByMachine.read(trans, R_TEXT, new Object[]{machine});\r
231         }\r
232 \r
233         /**\r
234      * Log Modification statements to History\r
235      *\r
236      * @param modified        which CRUD action was done\r
237      * @param data            entity data that needs a log entry\r
238      * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data\r
239      */\r
240     @Override\r
241     protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {\r
242         boolean memo = override.length>0 && override[0]!=null;\r
243         boolean subject = override.length>1 && override[1]!=null;\r
244 \r
245         HistoryDAO.Data hd = HistoryDAO.newInitedData();\r
246         hd.user = trans.user();\r
247         hd.action = modified.name();\r
248         hd.target = TABLE;\r
249         hd.subject = subject?override[1]: data.mechid;\r
250         hd.memo = memo\r
251                 ? String.format("%s by %s", override[0], hd.user)\r
252                 : String.format("%sd %s for %s",modified.name(),data.mechid,data.machine);\r
253         // Detail?\r
254                 if(modified==CRUD.delete) {\r
255                                 try {\r
256                                         hd.reconstruct = data.bytify();\r
257                                 } catch (IOException e) {\r
258                                         trans.error().log(e,"Could not serialize CredDAO.Data");\r
259                                 }\r
260                         }\r
261 \r
262         if(historyDAO.create(trans, hd).status!=Status.OK) {\r
263                 trans.error().log("Cannot log to History");\r
264         }\r
265     }\r
266 }\r