b7cd576557f5434d62df67a8ad3c32f498647b82
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.core.utilities.applicationconfig.dao.impl;
22
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;
35
36 import java.util.Collection;
37 import java.util.Objects;
38
39
40 public class ApplicationConfigDaoCassandraImpl extends CassandraBaseDao<ApplicationConfigEntity>
41     implements
42     ApplicationConfigDao {
43
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);
49
50   @Override
51   protected Mapper<ApplicationConfigEntity> getMapper() {
52     return mapper;
53   }
54
55   @Override
56   protected Object[] getKeys(ApplicationConfigEntity entity) {
57     return new Object[]{entity.getNamespace(), entity.getKey(), entity.getValue()};
58   }
59
60   @Override
61   public void create(ApplicationConfigEntity entity) {
62     accessor.updateApplicationConfigData(entity.getNamespace(), entity.getKey(), entity.getValue());
63   }
64
65   @Override
66   public void update(ApplicationConfigEntity entity) {
67     accessor.updateApplicationConfigData(entity.getNamespace(), entity.getKey(), entity.getValue());
68   }
69
70   @Override
71   public ApplicationConfigEntity get(ApplicationConfigEntity entity) {
72     return accessor.get(entity.getNamespace(), entity.getKey());
73   }
74
75   @Override
76   public Collection<ApplicationConfigEntity> list(ApplicationConfigEntity entity) {
77     return accessor.list(entity.getNamespace()).all();
78   }
79
80   @Override
81   public long getValueTimestamp(String namespace, String key) {
82     ResultSet resultSet = accessor.getValueAndTimestampOfConfigurationValue(namespace, key);
83
84     return resultSet.one().getLong("writetime(value)");
85   }
86
87   @Override
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();
92
93     if (Objects.nonNull(one)) {
94       return new ConfigurationData(one.getString("value"), one.getLong("writetime(value)"));
95     }
96
97     return null;
98   }
99
100
101   @Accessor
102   interface ApplicationConfigAccessor {
103
104     @Query("select namespace, key, value from application_config where namespace=?")
105     Result<ApplicationConfigEntity> list(String namespace);
106
107     @Query("insert into application_config (namespace, key, value) values (?,?,?)")
108     ResultSet updateApplicationConfigData(String namespace, String key, String value);
109
110     @Query("select namespace, key, value from application_config where namespace=? and key=?")
111     ApplicationConfigEntity get(String namespace, String key);
112
113     @Query("select value, writetime(value) from application_config where namespace=? and key=?")
114     ResultSet getValueAndTimestampOfConfigurationValue(String namespace, String key);
115
116   }
117 }