f1fb2bedf2447e3d88341c2500029e46147e65cc
[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 #include "private/databaseconfigurationimpl.hpp"
18 #include <arpa/inet.h>
19
20 using namespace shareddatalayer;
21
22 namespace
23 {
24     const std::string& getDefaultHost()
25     {
26         static const std::string defaultHost("localhost");
27         return defaultHost;
28     }
29
30     const uint16_t DEFAULT_PORT(6379U);
31 }
32
33 DatabaseConfigurationImpl::DatabaseConfigurationImpl():
34     dbType(DbType::UNKNOWN)
35 {
36 }
37
38 DatabaseConfigurationImpl::~DatabaseConfigurationImpl()
39 {
40 }
41
42 void DatabaseConfigurationImpl::checkAndApplyDbType(const std::string& type)
43 {
44     if (type == "redis-standalone")
45         dbType = DatabaseConfiguration::DbType::REDIS_STANDALONE;
46     else if (type == "redis-cluster")
47         dbType = DatabaseConfiguration::DbType::REDIS_CLUSTER;
48     else
49         throw DatabaseConfiguration::InvalidDbType(type);
50 }
51
52 DatabaseConfiguration::DbType DatabaseConfigurationImpl::getDbType() const
53 {
54    return dbType;
55 }
56
57 void DatabaseConfigurationImpl::checkAndApplyServerAddress(const std::string& address)
58 {
59     serverAddresses.push_back(HostAndPort(address, htons(DEFAULT_PORT)));
60 }
61
62 bool DatabaseConfigurationImpl::isEmpty() const
63 {
64     return serverAddresses.empty();
65 }
66
67 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses() const
68 {
69     return serverAddresses;
70 }
71
72 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getDefaultServerAddresses() const
73 {
74     return { HostAndPort(getDefaultHost(), htons(DEFAULT_PORT)) };
75 }