Add Redis Sentinel based database discovery
[ric-plt/sdl.git] / src / redis / asyncdatabasediscovery.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 #include "private/redis/asyncdatabasediscovery.hpp"
18 #include "private/databaseconfiguration.hpp"
19 #include "private/logger.hpp"
20 #include <cstdlib>
21 #include "config.h"
22 #if HAVE_HIREDIS
23 #include "private/redis/asynchiredisdatabasediscovery.hpp"
24 #endif
25 #if HAVE_SENTINEL
26 #include "private/redis/asyncsentineldatabasediscovery.hpp"
27 #endif
28 #include "private/abort.hpp"
29
30 using namespace shareddatalayer::redis;
31
32 std::shared_ptr<AsyncDatabaseDiscovery> AsyncDatabaseDiscovery::create(std::shared_ptr<Engine> engine,
33                                                                        const boost::optional<Namespace>& ns,
34                                                                        const DatabaseConfiguration& staticDatabaseConfiguration,
35                                                                        std::shared_ptr<Logger> logger)
36 {
37     auto staticAddresses(staticDatabaseConfiguration.getServerAddresses());
38
39     if (staticAddresses.empty())
40         staticAddresses = staticDatabaseConfiguration.getDefaultServerAddresses();
41
42     auto staticDbType(staticDatabaseConfiguration.getDbType());
43
44     if (staticDbType == DatabaseConfiguration::DbType::REDIS_CLUSTER)
45 #if HAVE_HIREDIS_VIP
46         return std::make_shared<AsyncHiredisDatabaseDiscovery>(engine,
47                                                                ns,
48                                                                DatabaseInfo::Type::CLUSTER,
49                                                                staticAddresses,
50                                                                logger);
51 #else
52         SHAREDDATALAYER_ABORT("No Hiredis vip for Redis cluster configuration");
53 #endif
54     else
55     {
56 #if HAVE_HIREDIS
57 #if HAVE_SENTINEL
58         static_cast<void>(ns);
59         return std::make_shared<AsyncSentinelDatabaseDiscovery>(engine,
60                                                                 logger);
61 #else
62         return std::make_shared<AsyncHiredisDatabaseDiscovery>(engine,
63                                                                ns,
64                                                                DatabaseInfo::Type::SINGLE,
65                                                                staticAddresses,
66                                                                logger);
67 #endif
68 #else
69         static_cast<void>(logger);
70         SHAREDDATALAYER_ABORT("No Hiredis");
71 #endif
72     }
73 }