Sonar Fixes, Formatting
[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  * Modifications Copyright (C) 2018-19 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.dao.cass;
25
26 import java.io.DataInputStream;
27 import java.io.DataOutputStream;
28 import java.io.IOException;
29 import java.util.List;
30
31 import org.onap.aaf.auth.dao.AbsCassDAO;
32 import org.onap.aaf.auth.dao.CassDAOImpl;
33 import org.onap.aaf.auth.dao.Loader;
34 import org.onap.aaf.auth.dao.Streamer;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.layer.Result;
37 import org.onap.aaf.misc.env.APIException;
38
39 import com.datastax.driver.core.Cluster;
40 import com.datastax.driver.core.Row;
41
42 /**
43  * CredDAO manages credentials.
44  * @author Jonathan
45  * Date: 6/25/18
46  */
47 public class ConfigDAO extends CassDAOImpl<AuthzTrans,ConfigDAO.Data> {
48     public static final String TABLE_NAME = "config";
49     public static final int CACHE_SEG = 0x40; // yields segment 0x0-0x3F
50     public static final int KEYLIMIT = 2;
51     private PSInfo psName;
52
53     public ConfigDAO(AuthzTrans trans, Cluster cluster, String keyspace) throws APIException, IOException {
54         super(trans, ConfigDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE_NAME, readConsistency(trans,TABLE_NAME), writeConsistency(trans,TABLE_NAME));
55         init(trans);
56     }
57
58     public ConfigDAO(AuthzTrans trans, AbsCassDAO<AuthzTrans,?> aDao) throws APIException, IOException {
59         super(trans, ConfigDAO.class.getSimpleName(),aDao, Data.class,TABLE_NAME, readConsistency(trans,TABLE_NAME), writeConsistency(trans,TABLE_NAME));
60         init(trans);
61     }
62
63     public static class Data  {
64         public String                    name;
65         public String                    tag;
66         public String                    value;
67     }
68
69     private static class ConfigLoader extends Loader<Data> implements Streamer<Data>{
70         public static final int MAGIC=2673849;
71         public static final int VERSION=1;
72         public static final int BUFF_SIZE=48;
73
74         public static final ConfigLoader deflt = new ConfigLoader(KEYLIMIT);
75         public ConfigLoader(int keylimit) {
76             super(keylimit);
77         }
78
79         @Override
80         public Data load(Data data, Row row) {
81             data.name = row.getString(0);
82             data.tag = row.getString(1);
83             data.value = row.getString(2);
84             return data;
85         }
86
87         @Override
88         protected void key(Data data, int idx, Object[] obj) {
89             obj[idx] = data.name;
90             obj[++idx] = data.tag;
91         }
92
93         @Override
94         protected void body(Data data, int _idx, Object[] obj) {
95             obj[_idx] = data.value;
96         }
97
98         @Override
99         public void marshal(Data data, DataOutputStream os) throws IOException {
100             writeHeader(os,MAGIC,VERSION);
101             writeString(os, data.name);
102             writeString(os, data.tag);
103             writeString(os, data.value);
104         }
105
106         @Override
107         public void unmarshal(Data data, DataInputStream is) throws IOException {
108             /*int version = */readHeader(is,MAGIC,VERSION);
109             // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
110             byte[] buff = new byte[BUFF_SIZE];
111             data.name = readString(is,buff);
112             data.tag = readString(is,buff);
113             data.value = readString(is,buff);
114         }
115     }
116
117     private void init(AuthzTrans trans) throws APIException, IOException {
118         String[] helpers = setCRUD(trans, TABLE_NAME, Data.class, ConfigLoader.deflt);
119
120         psName = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE_NAME +
121                 " WHERE name = ?", ConfigLoader.deflt,readConsistency);
122     }
123
124
125     /**
126      * Log Modification statements to History
127      *
128      * @param modified        which CRUD action was done
129      * @param data            entity data that needs a log entry
130      * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
131      */
132     @Override
133     protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
134         // not an auditable table.
135     }
136
137     public Result<List<Data>> readName(AuthzTrans trans, String name) {
138         return psName.read(trans, R_TEXT, new Object[]{name});
139     }
140
141
142 }