2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
21 package org.openecomp.core.utilities.applicationconfig.dao.impl;
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.core.Row;
25 import com.datastax.driver.mapping.Mapper;
26 import com.datastax.driver.mapping.Result;
27 import com.datastax.driver.mapping.annotations.Accessor;
28 import com.datastax.driver.mapping.annotations.Query;
29 import org.openecomp.core.dao.impl.CassandraBaseDao;
30 import org.openecomp.core.nosqldb.api.NoSqlDb;
31 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
32 import org.openecomp.core.utilities.applicationconfig.dao.ApplicationConfigDao;
33 import org.openecomp.core.utilities.applicationconfig.dao.type.ApplicationConfigEntity;
34 import org.openecomp.core.utilities.applicationconfig.type.ConfigurationData;
36 import java.util.Collection;
37 import java.util.Objects;
40 public class ApplicationConfigDaoCassandraImpl extends CassandraBaseDao<ApplicationConfigEntity>
42 ApplicationConfigDao {
44 private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
45 private static final Mapper<ApplicationConfigEntity> mapper =
46 noSqlDb.getMappingManager().mapper(ApplicationConfigEntity.class);
47 private static final ApplicationConfigAccessor accessor =
48 noSqlDb.getMappingManager().createAccessor(ApplicationConfigAccessor.class);
51 protected Mapper<ApplicationConfigEntity> getMapper() {
56 protected Object[] getKeys(ApplicationConfigEntity entity) {
57 return new Object[]{entity.getNamespace(), entity.getKey(), entity.getValue()};
61 public void create(ApplicationConfigEntity entity) {
62 accessor.updateApplicationConfigData(entity.getNamespace(), entity.getKey(), entity.getValue());
66 public void update(ApplicationConfigEntity entity) {
67 accessor.updateApplicationConfigData(entity.getNamespace(), entity.getKey(), entity.getValue());
71 public ApplicationConfigEntity get(ApplicationConfigEntity entity) {
72 return accessor.get(entity.getNamespace(), entity.getKey());
76 public Collection<ApplicationConfigEntity> list(ApplicationConfigEntity entity) {
77 return accessor.list(entity.getNamespace()).all();
81 public long getValueTimestamp(String namespace, String key) {
82 ResultSet resultSet = accessor.getValueAndTimestampOfConfigurationValue(namespace, key);
84 return resultSet.one().getLong("writetime(value)");
88 public ConfigurationData getConfigurationData(String namespace, String key) {
89 //String value = accessor.getValue(namespace, key).one().getString("value");
90 ResultSet resultSet = accessor.getValueAndTimestampOfConfigurationValue(namespace, key);
91 Row one = resultSet.one();
93 if (Objects.nonNull(one)) {
94 return new ConfigurationData(one.getString("value"), one.getLong("writetime(value)"));
102 interface ApplicationConfigAccessor {
104 @Query("select namespace, key, value from application_config where namespace=?")
105 Result<ApplicationConfigEntity> list(String namespace);
107 @Query("insert into application_config (namespace, key, value) values (?,?,?)")
108 ResultSet updateApplicationConfigData(String namespace, String key, String value);
110 @Query("select namespace, key, value from application_config where namespace=? and key=?")
111 ApplicationConfigEntity get(String namespace, String key);
113 @Query("select value, writetime(value) from application_config where namespace=? and key=?")
114 ResultSet getValueAndTimestampOfConfigurationValue(String namespace, String key);