Add standard stream logger
[ric-plt/sdl.git] / src / namespaceconfigurationsimpl.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/abort.hpp"
18 #include "private/namespaceconfigurationsimpl.hpp"
19 #include <sstream>
20 #include <iomanip>
21
22 using namespace shareddatalayer;
23
24 namespace
25 {
26     const NamespaceConfiguration getDefaultNamespaceConfiguration()
27     {
28         return {"", true, false, "<default>"};
29     }
30 }
31
32 NamespaceConfigurationsImpl::NamespaceConfigurationsImpl():
33     namespaceConfigurations({getDefaultNamespaceConfiguration()}),
34     //Current implementation assumes that this index is zero. If changing the index, do needed modifications to implementation.
35     defaultConfigurationIndex(0),
36     namespaceConfigurationsLookupTable()
37 {
38 }
39
40 NamespaceConfigurationsImpl::~NamespaceConfigurationsImpl()
41 {
42 }
43
44 void NamespaceConfigurationsImpl::addNamespaceConfiguration(const NamespaceConfiguration& namespaceConfiguration)
45 {
46     if (!namespaceConfigurationsLookupTable.empty())
47         SHAREDDATALAYER_ABORT("Cannot add namespace configurations after lookup table is initialized");
48
49     namespaceConfigurations.push_back(namespaceConfiguration);
50 }
51
52 std::string NamespaceConfigurationsImpl::getDescription(const std::string& ns) const
53 {
54     NamespaceConfiguration namespaceConfiguration = findConfigurationForNamespace(ns);
55     std::string sourceInfo = namespaceConfiguration.sourceName;
56
57     if (!namespaceConfiguration.namespacePrefix.empty())
58         sourceInfo += (" prefix: " + namespaceConfiguration.namespacePrefix);
59
60     std::ostringstream os;
61     os << std::boolalpha;
62     os << sourceInfo << ", ";
63     os << "useDbBackend: " << namespaceConfiguration.dbBackendIsUsed << ", ";
64     os << "enableNotifications: " << namespaceConfiguration.notificationsAreEnabled;
65     return os.str();
66 }
67
68 bool NamespaceConfigurationsImpl::isDbBackendUseEnabled(const std::string& ns) const
69 {
70     return findConfigurationForNamespace(ns).dbBackendIsUsed;
71 }
72
73 bool NamespaceConfigurationsImpl::areNotificationsEnabled(const std::string& ns) const
74 {
75     return findConfigurationForNamespace(ns).notificationsAreEnabled;
76 }
77
78 const NamespaceConfiguration& NamespaceConfigurationsImpl::findConfigurationForNamespace(const std::string& ns) const
79 {
80     if (namespaceConfigurationsLookupTable.count(ns) > 0)
81         return namespaceConfigurations.at(namespaceConfigurationsLookupTable.at(ns));
82
83     size_t longestMatchingPrefixLen(0);
84     std::vector<int>::size_type foundIndex(0);
85     bool configurationFound(false);
86
87     for(std::vector<int>::size_type i = (defaultConfigurationIndex + 1); i < namespaceConfigurations.size(); i++)
88     {
89         const auto prefixLen(namespaceConfigurations[i].namespacePrefix.length());
90
91         if (ns.compare(0, prefixLen, namespaceConfigurations[i].namespacePrefix) == 0)
92         {
93             if (prefixLen >= longestMatchingPrefixLen)
94             {
95                 configurationFound = true;
96                 foundIndex = i;
97                 longestMatchingPrefixLen = prefixLen;
98             }
99         }
100     }
101
102     if (!configurationFound)
103         foundIndex = defaultConfigurationIndex;
104
105     namespaceConfigurationsLookupTable[ns] = foundIndex;
106     return namespaceConfigurations[foundIndex];
107 }
108
109 bool NamespaceConfigurationsImpl::isEmpty() const
110 {
111     /* Empty when contains only default configuration */
112     return namespaceConfigurations.size() == 1;
113 }
114
115 bool NamespaceConfigurationsImpl::isNamespaceInLookupTable(const std::string& ns) const
116 {
117     return namespaceConfigurationsLookupTable.count(ns) > 0;
118 }