Multiple DBAAS Redis Sentinel groups
[ric-plt/sdl.git] / src / databaseconfigurationimpl.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 "private/databaseconfigurationimpl.hpp"
23 #include <arpa/inet.h>
24 #include <boost/crc.hpp>
25
26 using namespace shareddatalayer;
27
28 namespace
29 {
30     const std::string& getDefaultHost()
31     {
32         static const std::string defaultHost("localhost");
33         return defaultHost;
34     }
35
36     const uint16_t DEFAULT_PORT(6379U);
37     const uint16_t DEFAULT_SENTINEL_PORT(26379U);
38 }
39
40 DatabaseConfigurationImpl::DatabaseConfigurationImpl():
41     dbType(DbType::UNKNOWN)
42 {
43 }
44
45 DatabaseConfigurationImpl::~DatabaseConfigurationImpl()
46 {
47 }
48
49 void DatabaseConfigurationImpl::checkAndApplyDbType(const std::string& type)
50 {
51     if (type == "redis-standalone")
52         dbType = DatabaseConfiguration::DbType::REDIS_STANDALONE;
53     else if (type == "redis-cluster")
54         dbType = DatabaseConfiguration::DbType::REDIS_CLUSTER;
55     else if (type == "redis-sentinel")
56         dbType = DatabaseConfiguration::DbType::REDIS_SENTINEL;
57     else if (type == "sdl-cluster")
58         dbType = DatabaseConfiguration::DbType::SDL_CLUSTER;
59     else
60         throw DatabaseConfiguration::InvalidDbType(type);
61 }
62
63 DatabaseConfiguration::DbType DatabaseConfigurationImpl::getDbType() const
64 {
65    return dbType;
66 }
67
68 void DatabaseConfigurationImpl::checkAndApplyServerAddress(const std::string& address)
69 {
70     serverAddresses.push_back(HostAndPort(address, htons(DEFAULT_PORT)));
71 }
72
73 bool DatabaseConfigurationImpl::isEmpty() const
74 {
75     return serverAddresses.empty();
76 }
77
78 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses() const
79 {
80     return serverAddresses;
81 }
82
83 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getDefaultServerAddresses() const
84 {
85     return { HostAndPort(getDefaultHost(), htons(DEFAULT_PORT)) };
86 }
87
88 void DatabaseConfigurationImpl::checkAndApplySentinelAddress(const std::string& address)
89 {
90     sentinelAddress = HostAndPort(address, htons(DEFAULT_SENTINEL_PORT));
91 }
92
93 boost::optional<HostAndPort> DatabaseConfigurationImpl::getSentinelAddress() const
94 {
95     return sentinelAddress;
96 }
97
98 boost::optional<HostAndPort> DatabaseConfigurationImpl::getSentinelAddress(const boost::optional<std::size_t>& addressIndex) const
99 {
100     if (addressIndex)
101         return { HostAndPort(serverAddresses.at(*addressIndex).getHost(), sentinelAddress->getPort()) };
102
103     return getSentinelAddress();
104 }
105
106 void DatabaseConfigurationImpl::checkAndApplySentinelMasterName(const std::string& name)
107 {
108     sentinelMasterName = name;
109 }
110
111 std::string DatabaseConfigurationImpl::getSentinelMasterName() const
112 {
113     return sentinelMasterName;
114 }