push addional code
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-config-lib / src / main / java / org / openecomp / core / utilities / applicationconfig / dao / impl / ApplicationConfigDaoCassandraImpl.java
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 public class ApplicationConfigDaoCassandraImpl extends CassandraBaseDao<ApplicationConfigEntity>
40     implements ApplicationConfigDao {
41
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);
47
48   @Override
49   protected Mapper<ApplicationConfigEntity> getMapper() {
50     return mapper;
51   }
52
53   @Override
54   protected Object[] getKeys(ApplicationConfigEntity entity) {
55     return new Object[]{entity.getNamespace(), entity.getKey(), entity.getValue()};
56   }
57
58   @Override
59   public Collection<ApplicationConfigEntity> list(ApplicationConfigEntity entity) {
60     return accessor.list(entity.getNamespace()).all();
61   }
62
63   @Override
64   public void create(ApplicationConfigEntity entity) {
65     accessor.updateApplicationConfigData(entity.getNamespace(), entity.getKey(), entity.getValue());
66   }
67
68   @Override
69   public void update(ApplicationConfigEntity entity) {
70     accessor.updateApplicationConfigData(entity.getNamespace(), entity.getKey(), entity.getValue());
71   }
72
73   @Override
74   public ApplicationConfigEntity get(ApplicationConfigEntity entity) {
75     return accessor.get(entity.getNamespace(), entity.getKey());
76   }
77
78   @Override
79   public long getValueTimestamp(String namespace, String key) {
80     ResultSet resultSet = accessor.getValueAndTimestampOfConfigurationValue(namespace, key);
81
82     return resultSet.one().getLong("writetime(value)");
83   }
84
85   @Override
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();
90
91     if (Objects.nonNull(one)) {
92       return new ConfigurationData(one.getString("value"), one.getLong("writetime(value)"));
93     }
94
95     return null;
96   }
97
98
99   @Accessor
100   interface ApplicationConfigAccessor {
101
102     @Query("select namespace, key, value from application_config where namespace=?")
103     Result<ApplicationConfigEntity> list(String namespace);
104
105     @Query("insert into application_config (namespace, key, value) values (?,?,?)")
106     ResultSet updateApplicationConfigData(String namespace, String key, String value);
107
108     @Query("select namespace, key, value from application_config where namespace=? and key=?")
109     ApplicationConfigEntity get(String namespace, String key);
110
111     @Query("select value, writetime(value) from application_config where namespace=? and key=?")
112     ResultSet getValueAndTimestampOfConfigurationValue(String namespace, String key);
113
114   }
115 }