Add extra line about src files are part of RIC platform project
[ric-plt/sdl.git] / src / asyncstorageimpl.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #include "config.h"
23 #include "private/error.hpp"
24 #include "private/abort.hpp"
25 #include "private/asyncstorageimpl.hpp"
26 #include "private/configurationreader.hpp"
27 #include "private/asyncdummystorage.hpp"
28 #include "private/engine.hpp"
29 #include "private/logger.hpp"
30 #if HAVE_REDIS
31 #include "private/redis/asyncredisstorage.hpp"
32 #endif
33
34 using namespace shareddatalayer;
35 using namespace shareddatalayer::redis;
36
37 namespace
38 {
39         std::shared_ptr<AsyncDatabaseDiscovery> asyncDatabaseDiscoveryCreator(std::shared_ptr<Engine> engine,
40                                                                               const DatabaseConfiguration& databaseConfiguration,
41                                                                               std::shared_ptr<Logger> logger)
42         {
43             return AsyncDatabaseDiscovery::create(engine,
44                                                   boost::none,
45                                                   databaseConfiguration,
46                                                   logger);
47         }
48 }
49
50 AsyncStorageImpl::AsyncStorageImpl(std::shared_ptr<Engine> engine,
51                                    const boost::optional<PublisherId>& pId,
52                                    std::shared_ptr<Logger> logger):
53     engine(engine),
54     databaseConfiguration(std::make_shared<DatabaseConfigurationImpl>()),
55     namespaceConfigurations(std::make_shared<NamespaceConfigurationsImpl>()),
56     publisherId(pId),
57     logger(logger),
58     asyncDatabaseDiscoveryCreator(::asyncDatabaseDiscoveryCreator)
59 {
60     ConfigurationReader configurationReader(logger);
61     configurationReader.readDatabaseConfiguration(std::ref(*databaseConfiguration));
62     configurationReader.readNamespaceConfigurations(std::ref(*namespaceConfigurations));
63 }
64
65 // Meant for UT usage
66 AsyncStorageImpl::AsyncStorageImpl(std::shared_ptr<Engine> engine,
67                                    const boost::optional<PublisherId>& pId,
68                                    std::shared_ptr<DatabaseConfiguration> databaseConfiguration,
69                                    std::shared_ptr<NamespaceConfigurations> namespaceConfigurations,
70                                    std::shared_ptr<Logger> logger,
71                                    const AsyncDatabaseDiscoveryCreator& asyncDatabaseDiscoveryCreator):
72     engine(engine),
73     databaseConfiguration(databaseConfiguration),
74     namespaceConfigurations(namespaceConfigurations),
75     publisherId(pId),
76     logger(logger),
77     asyncDatabaseDiscoveryCreator(asyncDatabaseDiscoveryCreator)
78 {
79 }
80
81 AsyncStorage& AsyncStorageImpl::getRedisHandler()
82 {
83 #if HAVE_REDIS
84     static AsyncRedisStorage redisHandler{engine,
85                                           asyncDatabaseDiscoveryCreator(
86                                               engine,
87                                               std::ref(*databaseConfiguration),
88                                               logger),
89                                           publisherId,
90                                           namespaceConfigurations,
91                                           logger};
92
93     return redisHandler;
94 #else
95     logger->error() << "Redis operations cannot be performed, Redis not enabled";
96     SHAREDDATALAYER_ABORT("Invalid configuration.");
97 #endif
98 }
99
100 AsyncStorage& AsyncStorageImpl::getDummyHandler()
101 {
102     static AsyncDummyStorage dummyHandler{engine};
103     return dummyHandler;
104 }
105
106 AsyncStorage& AsyncStorageImpl::getOperationHandler(const std::string& ns)
107 {
108     if (namespaceConfigurations->isDbBackendUseEnabled(ns))
109         return getRedisHandler();
110
111     return getDummyHandler();
112 }
113
114 int AsyncStorageImpl::fd() const
115 {
116     return engine->fd();
117 }
118
119 void AsyncStorageImpl::handleEvents()
120 {
121     engine->handleEvents();
122 }
123
124 void AsyncStorageImpl::waitReadyAsync(const Namespace& ns,
125                                       const ReadyAck& readyAck)
126 {
127     getOperationHandler(ns).waitReadyAsync(ns, readyAck);
128 }
129
130 void AsyncStorageImpl::setAsync(const Namespace& ns,
131                                 const DataMap& dataMap,
132                                 const ModifyAck& modifyAck)
133 {
134     getOperationHandler(ns).setAsync(ns, dataMap, modifyAck);
135 }
136
137 void AsyncStorageImpl::setIfAsync(const Namespace& ns,
138                                   const Key& key,
139                                   const Data& oldData,
140                                   const Data& newData,
141                                   const ModifyIfAck& modifyIfAck)
142 {
143     getOperationHandler(ns).setIfAsync(ns, key, oldData, newData, modifyIfAck);
144 }
145
146 void AsyncStorageImpl::removeIfAsync(const Namespace& ns,
147                                      const Key& key,
148                                      const Data& data,
149                                      const ModifyIfAck& modifyIfAck)
150 {
151     getOperationHandler(ns).removeIfAsync(ns, key, data, modifyIfAck);
152 }
153
154 void AsyncStorageImpl::setIfNotExistsAsync(const Namespace& ns,
155                                            const Key& key,
156                                            const Data& data,
157                                            const ModifyIfAck& modifyIfAck)
158 {
159     getOperationHandler(ns).setIfNotExistsAsync(ns, key, data, modifyIfAck);
160 }
161
162 void AsyncStorageImpl::getAsync(const Namespace& ns,
163                                 const Keys& keys,
164                                 const GetAck& getAck)
165 {
166     getOperationHandler(ns).getAsync(ns, keys, getAck);
167 }
168
169 void AsyncStorageImpl::removeAsync(const Namespace& ns,
170                                    const Keys& keys,
171                                    const ModifyAck& modifyAck)
172 {
173     getOperationHandler(ns).removeAsync(ns, keys, modifyAck);
174 }
175
176 void AsyncStorageImpl::findKeysAsync(const Namespace& ns,
177                                      const std::string& keyPrefix,
178                                      const FindKeysAck& findKeysAck)
179 {
180     getOperationHandler(ns).findKeysAsync(ns, keyPrefix, findKeysAck);
181 }
182
183 void AsyncStorageImpl::removeAllAsync(const Namespace& ns,
184                                        const ModifyAck& modifyAck)
185 {
186     getOperationHandler(ns).removeAllAsync(ns, modifyAck);
187 }