RIC:1060: Change in PTL
[ric-plt/sdl.git] / src / databaseconfigurationimpl.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 "private/databaseconfigurationimpl.hpp"
23 #include <arpa/inet.h>
24 #include <boost/crc.hpp>
25 #include <boost/lexical_cast.hpp>
26 #include <boost/algorithm/string.hpp>
27
28 using namespace shareddatalayer;
29
30 namespace
31 {
32     const std::string& getDefaultHost()
33     {
34         static const std::string defaultHost("localhost");
35         return defaultHost;
36     }
37
38     const uint16_t DEFAULT_PORT(6379U);
39     const uint16_t DEFAULT_SENTINEL_PORT(26379U);
40     const std::string DEFAULT_SENTINEL_MASTER_GROUP_NAME("dbaasmaster");
41 }
42
43 DatabaseConfigurationImpl::DatabaseConfigurationImpl():
44     dbType(DbType::UNKNOWN)
45 {
46 }
47
48 DatabaseConfigurationImpl::~DatabaseConfigurationImpl()
49 {
50 }
51
52 void DatabaseConfigurationImpl::checkAndApplyDbType(const std::string& type)
53 {
54     if (type == "redis-standalone")
55         dbType = DatabaseConfiguration::DbType::REDIS_STANDALONE;
56     else if (type == "redis-cluster")
57         dbType = DatabaseConfiguration::DbType::REDIS_CLUSTER;
58     else if (type == "redis-sentinel")
59         dbType = DatabaseConfiguration::DbType::REDIS_SENTINEL;
60     else if (type == "sdl-standalone-cluster")
61         dbType = DatabaseConfiguration::DbType::SDL_STANDALONE_CLUSTER;
62     else if (type == "sdl-sentinel-cluster")
63         dbType = DatabaseConfiguration::DbType::SDL_SENTINEL_CLUSTER;
64     else
65         throw DatabaseConfiguration::InvalidDbType(type);
66 }
67
68 DatabaseConfiguration::DbType DatabaseConfigurationImpl::getDbType() const
69 {
70    return dbType;
71 }
72
73 void DatabaseConfigurationImpl::checkAndApplyServerAddress(const std::string& address)
74 {
75     serverAddresses.push_back(HostAndPort(address, htons(DEFAULT_PORT)));
76 }
77
78 bool DatabaseConfigurationImpl::isEmpty() const
79 {
80     return serverAddresses.empty();
81 }
82
83 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses() const
84 {
85     return serverAddresses;
86 }
87
88 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses(const boost::optional<std::size_t>& addressIndex) const
89 {
90     if (addressIndex)
91         return { HostAndPort(serverAddresses.at(*addressIndex)) };
92
93     return serverAddresses;
94 }
95
96 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getDefaultServerAddresses() const
97 {
98     return { HostAndPort(getDefaultHost(), htons(DEFAULT_PORT)) };
99 }
100
101 void DatabaseConfigurationImpl::checkAndApplySentinelPorts(const std::string& portsEnvStr)
102 {
103     std::vector<std::string> ports;
104     boost::split(ports, portsEnvStr, boost::is_any_of(","));
105
106     for (auto port : ports)
107     {
108         try {
109             sentinelPorts.push_back(htons(boost::lexical_cast<uint16_t>(port)));
110         }
111         catch (boost::bad_lexical_cast const &) {
112             continue;
113         }
114     }
115 }
116
117 boost::optional<HostAndPort> DatabaseConfigurationImpl::getSentinelAddress(const boost::optional<std::size_t>& addressIndex) const
118 {
119     std::size_t index(addressIndex ? *addressIndex : 0);
120     uint16_t port((sentinelPorts.size() > 0 && index < sentinelPorts.size()) ? sentinelPorts.at(index) : htons(DEFAULT_SENTINEL_PORT));
121
122     if (!(serverAddresses.size() > 0))
123         return {};
124
125     return { HostAndPort(serverAddresses.at(index).getHost(), port) };
126 }
127
128 void DatabaseConfigurationImpl::checkAndApplySentinelMasterNames(const std::string& sentinelMasterNamesEnvStr)
129 {
130     boost::split(sentinelMasterNames, sentinelMasterNamesEnvStr, boost::is_any_of(","));
131 }
132
133 std::string DatabaseConfigurationImpl::getSentinelMasterName(const boost::optional<std::size_t>& addressIndex) const
134 {
135     std::size_t index(addressIndex ? *addressIndex : 0);
136     return ((sentinelMasterNames.size() > 0 && index < sentinelMasterNames.size()) ? sentinelMasterNames.at(index) : DEFAULT_SENTINEL_MASTER_GROUP_NAME);
137 }