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;
39 public class ApplicationConfigDaoCassandraImpl extends CassandraBaseDao<ApplicationConfigEntity>
40 implements ApplicationConfigDao {
42 private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
43 private static final Mapper<ApplicationConfigEntity> mapper =
44 noSqlDb.getMappingManager().mapper(ApplicationConfigEntity.class);
45 private static final ApplicationConfigAccessor accessor =
46 noSqlDb.getMappingManager().createAccessor(ApplicationConfigAccessor.class);
49 protected Mapper<ApplicationConfigEntity> getMapper() {
54 protected Object[] getKeys(ApplicationConfigEntity entity) {
55 return new Object[]{entity.getNamespace(), entity.getKey(), entity.getValue()};
59 public Collection<ApplicationConfigEntity> list(ApplicationConfigEntity entity) {
60 return accessor.list(entity.getNamespace()).all();
64 public void create(ApplicationConfigEntity entity) {
65 accessor.updateApplicationConfigData(entity.getNamespace(), entity.getKey(), entity.getValue());
69 public void update(ApplicationConfigEntity entity) {
70 accessor.updateApplicationConfigData(entity.getNamespace(), entity.getKey(), entity.getValue());
74 public ApplicationConfigEntity get(ApplicationConfigEntity entity) {
75 return accessor.get(entity.getNamespace(), entity.getKey());
79 public long getValueTimestamp(String namespace, String key) {
80 ResultSet resultSet = accessor.getValueAndTimestampOfConfigurationValue(namespace, key);
82 return resultSet.one().getLong("writetime(value)");
86 public ConfigurationData getConfigurationData(String namespace, String key) {
87 //String value = accessor.getValue(namespace, key).one().getString("value");
88 ResultSet resultSet = accessor.getValueAndTimestampOfConfigurationValue(namespace, key);
89 Row one = resultSet.one();
91 if (Objects.nonNull(one)) {
92 return new ConfigurationData(one.getString("value"), one.getLong("writetime(value)"));
100 interface ApplicationConfigAccessor {
102 @Query("select namespace, key, value from application_config where namespace=?")
103 Result<ApplicationConfigEntity> list(String namespace);
105 @Query("insert into application_config (namespace, key, value) values (?,?,?)")
106 ResultSet updateApplicationConfigData(String namespace, String key, String value);
108 @Query("select namespace, key, value from application_config where namespace=? and key=?")
109 ApplicationConfigEntity get(String namespace, String key);
111 @Query("select value, writetime(value) from application_config where namespace=? and key=?")
112 ResultSet getValueAndTimestampOfConfigurationValue(String namespace, String key);