28bbf79ddea3154fb70f707d43e1f7f512b79b16
[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 /*
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                                                                        std::shared_ptr<Logger> logger)
39 {
40     auto staticAddresses(staticDatabaseConfiguration.getServerAddresses());
41
42     if (staticAddresses.empty())
43         staticAddresses = staticDatabaseConfiguration.getDefaultServerAddresses();
44
45     auto staticDbType(staticDatabaseConfiguration.getDbType());
46
47     if (staticDbType == DatabaseConfiguration::DbType::REDIS_CLUSTER)
48 #if HAVE_HIREDIS_VIP
49         return std::make_shared<AsyncHiredisDatabaseDiscovery>(engine,
50                                                                ns,
51                                                                DatabaseInfo::Type::CLUSTER,
52                                                                staticAddresses,
53                                                                logger);
54 #else
55         SHAREDDATALAYER_ABORT("No Hiredis vip for Redis cluster configuration");
56 #endif
57     else
58     {
59 #if HAVE_HIREDIS
60         if (staticDbType == DatabaseConfiguration::DbType::REDIS_SENTINEL)
61         {
62             static_cast<void>(ns);
63             auto sentinelAddress(staticDatabaseConfiguration.getSentinelAddress());
64             if (sentinelAddress)
65                 return std::make_shared<AsyncSentinelDatabaseDiscovery>(engine,
66                                                                         logger,
67                                                                         *sentinelAddress,
68                                                                         staticDatabaseConfiguration.getSentinelMasterName());
69             else
70                 SHAREDDATALAYER_ABORT("Sentinel address not configured.");
71         }
72         else
73         {
74             return std::make_shared<AsyncHiredisDatabaseDiscovery>(engine,
75                                                                    ns,
76                                                                    DatabaseInfo::Type::SINGLE,
77                                                                    staticAddresses,
78                                                                    logger);
79         }
80 #else
81         static_cast<void>(logger);
82         SHAREDDATALAYER_ABORT("No Hiredis");
83 #endif
84     }
85 }