2 Copyright (c) 2018-2019 Nokia.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #include "private/abort.hpp"
23 #include "private/namespaceconfigurationsimpl.hpp"
27 using namespace shareddatalayer;
31 const NamespaceConfiguration getDefaultNamespaceConfiguration()
33 return {"", true, false, "<default>"};
37 NamespaceConfigurationsImpl::NamespaceConfigurationsImpl():
38 namespaceConfigurations({getDefaultNamespaceConfiguration()}),
39 //Current implementation assumes that this index is zero. If changing the index, do needed modifications to implementation.
40 defaultConfigurationIndex(0),
41 namespaceConfigurationsLookupTable()
45 NamespaceConfigurationsImpl::~NamespaceConfigurationsImpl()
49 void NamespaceConfigurationsImpl::addNamespaceConfiguration(const NamespaceConfiguration& namespaceConfiguration)
51 if (!namespaceConfigurationsLookupTable.empty())
52 SHAREDDATALAYER_ABORT("Cannot add namespace configurations after lookup table is initialized");
54 namespaceConfigurations.push_back(namespaceConfiguration);
57 std::string NamespaceConfigurationsImpl::getDescription(const std::string& ns) const
59 NamespaceConfiguration namespaceConfiguration = findConfigurationForNamespace(ns);
60 std::string sourceInfo = namespaceConfiguration.sourceName;
62 if (!namespaceConfiguration.namespacePrefix.empty())
63 sourceInfo += (" prefix: " + namespaceConfiguration.namespacePrefix);
65 std::ostringstream os;
67 os << sourceInfo << ", ";
68 os << "useDbBackend: " << namespaceConfiguration.dbBackendIsUsed << ", ";
69 os << "enableNotifications: " << namespaceConfiguration.notificationsAreEnabled;
73 bool NamespaceConfigurationsImpl::isDbBackendUseEnabled(const std::string& ns) const
75 return findConfigurationForNamespace(ns).dbBackendIsUsed;
78 bool NamespaceConfigurationsImpl::areNotificationsEnabled(const std::string& ns) const
80 return findConfigurationForNamespace(ns).notificationsAreEnabled;
83 const NamespaceConfiguration& NamespaceConfigurationsImpl::findConfigurationForNamespace(const std::string& ns) const
85 if (namespaceConfigurationsLookupTable.count(ns) > 0)
86 return namespaceConfigurations.at(namespaceConfigurationsLookupTable.at(ns));
88 size_t longestMatchingPrefixLen(0);
89 std::vector<int>::size_type foundIndex(0);
90 bool configurationFound(false);
92 for(std::vector<int>::size_type i = (defaultConfigurationIndex + 1); i < namespaceConfigurations.size(); i++)
94 const auto prefixLen(namespaceConfigurations[i].namespacePrefix.length());
96 if (ns.compare(0, prefixLen, namespaceConfigurations[i].namespacePrefix) == 0)
98 if (prefixLen >= longestMatchingPrefixLen)
100 configurationFound = true;
102 longestMatchingPrefixLen = prefixLen;
107 if (!configurationFound)
108 foundIndex = defaultConfigurationIndex;
110 namespaceConfigurationsLookupTable[ns] = foundIndex;
111 return namespaceConfigurations[foundIndex];
114 bool NamespaceConfigurationsImpl::isEmpty() const
116 /* Empty when contains only default configuration */
117 return namespaceConfigurations.size() == 1;
120 bool NamespaceConfigurationsImpl::isNamespaceInLookupTable(const std::string& ns) const
122 return namespaceConfigurationsLookupTable.count(ns) > 0;