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