2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.dao.cass;
24 import java.io.DataInputStream;
25 import java.io.DataOutputStream;
26 import java.io.IOException;
27 import java.util.List;
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;
37 import com.datastax.driver.core.Cluster;
38 import com.datastax.driver.core.Row;
41 * CredDAO manages credentials.
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;
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));
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));
60 public static final int KEYLIMIT = 2;
61 public static class Data {
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;
72 public static final ConfigLoader deflt = new ConfigLoader(KEYLIMIT);
73 public ConfigLoader(int keylimit) {
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);
86 protected void key(Data data, int idx, Object[] obj) {
88 obj[++idx] = data.tag;
92 protected void body(Data data, int _idx, Object[] obj) {
93 obj[_idx] = data.value;
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);
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);
115 private void init(AuthzTrans trans) throws APIException, IOException {
116 String[] helpers = setCRUD(trans, TABLE, Data.class, ConfigLoader.deflt);
118 psName = new PSInfo(trans, SELECT_SP + helpers[FIELD_COMMAS] + " FROM " + TABLE +
119 " WHERE name = ?", ConfigLoader.deflt,readConsistency);
124 * Log Modification statements to History
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
131 protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
132 // not an auditable table.
135 public Result<List<Data>> readName(AuthzTrans trans, String name) {
136 return psName.read(trans, R_TEXT, new Object[]{name});