Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / cass / ConfigDAO.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.DataInputStream;
25 import java.io.DataOutputStream;
26 import java.io.IOException;
27 import java.util.List;
28
29 import org.onap.aaf.auth.dao.AbsCassDAO;
30 import org.onap.aaf.auth.dao.CassDAOImpl;
31 import org.onap.aaf.auth.dao.Loader;
32 import org.onap.aaf.auth.dao.Streamer;
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.layer.Result;
35 import org.onap.aaf.misc.env.APIException;
36
37 import com.datastax.driver.core.Cluster;
38 import com.datastax.driver.core.Row;
39
40 /**
41  * CredDAO manages credentials. 
42  * @author Jonathan
43  * Date: 6/25/18
44  */
45 public class ConfigDAO extends CassDAOImpl<AuthzTrans,ConfigDAO.Data> {
46     public static final String TABLE = "config";
47     public static final int CACHE_SEG = 0x40; // yields segment 0x0-0x3F
48     private PSInfo psName;
49     
50     public ConfigDAO(AuthzTrans trans, Cluster cluster, String keyspace) throws APIException, IOException {
51         super(trans, ConfigDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
52         init(trans);
53     }
54
55     public ConfigDAO(AuthzTrans trans, AbsCassDAO<AuthzTrans,?> aDao) throws APIException, IOException {
56         super(trans, ConfigDAO.class.getSimpleName(),aDao, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
57         init(trans);
58     }
59     
60     public static final int KEYLIMIT = 2;
61     public static class Data  {
62         public String                    name;
63         public String                    tag;
64         public String                    value;
65     }
66
67     private static class ConfigLoader extends Loader<Data> implements Streamer<Data>{
68         public static final int MAGIC=2673849;
69         public static final int VERSION=1;
70         public static final int BUFF_SIZE=48; 
71
72         public static final ConfigLoader deflt = new ConfigLoader(KEYLIMIT);
73         public ConfigLoader(int keylimit) {
74             super(keylimit);
75         }
76
77         @Override
78         public Data load(Data data, Row row) {
79             data.name = row.getString(0);
80             data.tag = row.getString(1);
81             data.value = row.getString(2);
82             return data;
83         }
84
85         @Override
86         protected void key(Data data, int idx, Object[] obj) {
87             obj[idx] = data.name;
88             obj[++idx] = data.tag;
89         }
90
91         @Override
92         protected void body(Data data, int _idx, Object[] obj) {
93             obj[_idx] = data.value;
94         }
95
96         @Override
97         public void marshal(Data data, DataOutputStream os) throws IOException {
98             writeHeader(os,MAGIC,VERSION);
99             writeString(os, data.name);
100             writeString(os, data.tag);
101             writeString(os, data.value);
102         }
103
104         @Override
105         public void unmarshal(Data data, DataInputStream is) throws IOException {
106             /*int version = */readHeader(is,MAGIC,VERSION);
107             // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
108             byte[] buff = new byte[BUFF_SIZE];
109             data.name = readString(is,buff);
110             data.tag = readString(is,buff);
111             data.value = readString(is,buff);
112         }
113     }
114     
115     private void init(AuthzTrans trans) throws APIException, IOException {
116         String[] helpers = setCRUD(trans, TABLE, Data.class, ConfigLoader.deflt);
117
118         psName = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
119                 " WHERE name = ?", ConfigLoader.deflt,readConsistency);
120     }
121     
122
123     /**
124      * Log Modification statements to History
125      *
126      * @param modified        which CRUD action was done
127      * @param data            entity data that needs a log entry
128      * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
129      */
130     @Override
131     protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
132         // not an auditable table.
133     }
134     
135     public Result<List<Data>> readName(AuthzTrans trans, String name) {
136         return psName.read(trans, R_TEXT, new Object[]{name});
137     }
138
139
140 }