Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 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 | |
Timo Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 22 | #include "private/abort.hpp" |
| 23 | #include "private/namespaceconfigurationsimpl.hpp" |
| 24 | #include <sstream> |
| 25 | #include <iomanip> |
| 26 | |
| 27 | using namespace shareddatalayer; |
| 28 | |
| 29 | namespace |
| 30 | { |
| 31 | const NamespaceConfiguration getDefaultNamespaceConfiguration() |
| 32 | { |
| 33 | return {"", true, false, "<default>"}; |
| 34 | } |
| 35 | } |
| 36 | |
| 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() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | NamespaceConfigurationsImpl::~NamespaceConfigurationsImpl() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void NamespaceConfigurationsImpl::addNamespaceConfiguration(const NamespaceConfiguration& namespaceConfiguration) |
| 50 | { |
| 51 | if (!namespaceConfigurationsLookupTable.empty()) |
| 52 | SHAREDDATALAYER_ABORT("Cannot add namespace configurations after lookup table is initialized"); |
| 53 | |
| 54 | namespaceConfigurations.push_back(namespaceConfiguration); |
| 55 | } |
| 56 | |
| 57 | std::string NamespaceConfigurationsImpl::getDescription(const std::string& ns) const |
| 58 | { |
| 59 | NamespaceConfiguration namespaceConfiguration = findConfigurationForNamespace(ns); |
| 60 | std::string sourceInfo = namespaceConfiguration.sourceName; |
| 61 | |
| 62 | if (!namespaceConfiguration.namespacePrefix.empty()) |
| 63 | sourceInfo += (" prefix: " + namespaceConfiguration.namespacePrefix); |
| 64 | |
| 65 | std::ostringstream os; |
| 66 | os << std::boolalpha; |
| 67 | os << sourceInfo << ", "; |
| 68 | os << "useDbBackend: " << namespaceConfiguration.dbBackendIsUsed << ", "; |
| 69 | os << "enableNotifications: " << namespaceConfiguration.notificationsAreEnabled; |
| 70 | return os.str(); |
| 71 | } |
| 72 | |
| 73 | bool NamespaceConfigurationsImpl::isDbBackendUseEnabled(const std::string& ns) const |
| 74 | { |
| 75 | return findConfigurationForNamespace(ns).dbBackendIsUsed; |
| 76 | } |
| 77 | |
| 78 | bool NamespaceConfigurationsImpl::areNotificationsEnabled(const std::string& ns) const |
| 79 | { |
| 80 | return findConfigurationForNamespace(ns).notificationsAreEnabled; |
| 81 | } |
| 82 | |
| 83 | const NamespaceConfiguration& NamespaceConfigurationsImpl::findConfigurationForNamespace(const std::string& ns) const |
| 84 | { |
| 85 | if (namespaceConfigurationsLookupTable.count(ns) > 0) |
| 86 | return namespaceConfigurations.at(namespaceConfigurationsLookupTable.at(ns)); |
| 87 | |
| 88 | size_t longestMatchingPrefixLen(0); |
| 89 | std::vector<int>::size_type foundIndex(0); |
| 90 | bool configurationFound(false); |
| 91 | |
| 92 | for(std::vector<int>::size_type i = (defaultConfigurationIndex + 1); i < namespaceConfigurations.size(); i++) |
| 93 | { |
| 94 | const auto prefixLen(namespaceConfigurations[i].namespacePrefix.length()); |
| 95 | |
| 96 | if (ns.compare(0, prefixLen, namespaceConfigurations[i].namespacePrefix) == 0) |
| 97 | { |
| 98 | if (prefixLen >= longestMatchingPrefixLen) |
| 99 | { |
| 100 | configurationFound = true; |
| 101 | foundIndex = i; |
| 102 | longestMatchingPrefixLen = prefixLen; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (!configurationFound) |
| 108 | foundIndex = defaultConfigurationIndex; |
| 109 | |
| 110 | namespaceConfigurationsLookupTable[ns] = foundIndex; |
| 111 | return namespaceConfigurations[foundIndex]; |
| 112 | } |
| 113 | |
| 114 | bool NamespaceConfigurationsImpl::isEmpty() const |
| 115 | { |
| 116 | /* Empty when contains only default configuration */ |
| 117 | return namespaceConfigurations.size() == 1; |
| 118 | } |
| 119 | |
| 120 | bool NamespaceConfigurationsImpl::isNamespaceInLookupTable(const std::string& ns) const |
| 121 | { |
| 122 | return namespaceConfigurationsLookupTable.count(ns) > 0; |
| 123 | } |