2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
24 package org.onap.aaf.auth.dao.cass;
26 import java.io.DataInputStream;
27 import java.io.DataOutputStream;
28 import java.io.IOException;
29 import java.util.List;
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;
39 import com.datastax.driver.core.Cluster;
40 import com.datastax.driver.core.Row;
43 * CredDAO manages credentials.
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;
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));
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));
63 public static class Data {
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;
74 public static final ConfigLoader deflt = new ConfigLoader(KEYLIMIT);
75 public ConfigLoader(int keylimit) {
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);
88 protected void key(Data data, int idx, Object[] obj) {
90 obj[++idx] = data.tag;
94 protected void body(Data data, int _idx, Object[] obj) {
95 obj[_idx] = data.value;
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);
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);
117 private void init(AuthzTrans trans) throws APIException, IOException {
118 String[] helpers = setCRUD(trans, TABLE_NAME, Data.class, ConfigLoader.deflt);
120 psName = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE_NAME +
121 " WHERE name = ?", ConfigLoader.deflt,readConsistency);
126 * Log Modification statements to History
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
133 protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
134 // not an auditable table.
137 public Result<List<Data>> readName(AuthzTrans trans, String name) {
138 return psName.read(trans, R_TEXT, new Object[]{name});