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