blob: 5778237dd1b7d0e5fca1629545279957463145b1 [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shib184e532016-05-26 18:09:57 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shiee5a4442014-07-27 17:13:43 -070024 */
Junxiao Shibb5105f2014-03-03 12:06:45 -070025
26#include "strategy-choice.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070028#include "fw/strategy.hpp"
29#include "pit-entry.hpp"
Junxiao Shi7bb01512014-03-05 21:34:09 -070030#include "measurements-entry.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070031
32namespace nfd {
Junxiao Shiff10da62016-07-13 17:57:43 +000033namespace strategy_choice {
Junxiao Shibb5105f2014-03-03 12:06:45 -070034
Junxiao Shibb5105f2014-03-03 12:06:45 -070035using fw::Strategy;
36
37NFD_LOG_INIT("StrategyChoice");
38
39StrategyChoice::StrategyChoice(NameTree& nameTree, shared_ptr<Strategy> defaultStrategy)
40 : m_nameTree(nameTree)
Junxiao Shib5888d22014-05-26 07:35:22 -070041 , m_nItems(0)
Junxiao Shibb5105f2014-03-03 12:06:45 -070042{
43 this->setDefaultStrategy(defaultStrategy);
44}
45
46bool
Junxiao Shie93d6a32014-09-07 16:13:22 -070047StrategyChoice::hasStrategy(const Name& strategyName, bool isExact) const
Junxiao Shibb5105f2014-03-03 12:06:45 -070048{
Junxiao Shie93d6a32014-09-07 16:13:22 -070049 if (isExact) {
50 return m_strategyInstances.count(strategyName) > 0;
51 }
52 else {
Junxiao Shib184e532016-05-26 18:09:57 +000053 return this->getStrategy(strategyName) != nullptr;
Junxiao Shie93d6a32014-09-07 16:13:22 -070054 }
Junxiao Shibb5105f2014-03-03 12:06:45 -070055}
56
57bool
58StrategyChoice::install(shared_ptr<Strategy> strategy)
59{
Junxiao Shib184e532016-05-26 18:09:57 +000060 BOOST_ASSERT(strategy != nullptr);
Junxiao Shibb5105f2014-03-03 12:06:45 -070061 const Name& strategyName = strategy->getName();
62
Junxiao Shi838c4f12014-11-03 18:55:24 -070063 if (this->hasStrategy(strategyName, true)) {
Junxiao Shibb5105f2014-03-03 12:06:45 -070064 NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName");
65 return false;
66 }
67
68 m_strategyInstances[strategyName] = strategy;
69 return true;
70}
71
Junxiao Shi838c4f12014-11-03 18:55:24 -070072fw::Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -070073StrategyChoice::getStrategy(const Name& strategyName) const
74{
Junxiao Shi838c4f12014-11-03 18:55:24 -070075 fw::Strategy* candidate = nullptr;
76 for (auto it = m_strategyInstances.lower_bound(strategyName);
Junxiao Shie93d6a32014-09-07 16:13:22 -070077 it != m_strategyInstances.end() && strategyName.isPrefixOf(it->first); ++it) {
78 switch (it->first.size() - strategyName.size()) {
79 case 0: // exact match
Junxiao Shi838c4f12014-11-03 18:55:24 -070080 return it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -070081 case 1: // unversioned strategyName matches versioned strategy
Junxiao Shi838c4f12014-11-03 18:55:24 -070082 candidate = it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -070083 break;
84 }
85 }
86 return candidate;
87}
88
Junxiao Shibb5105f2014-03-03 12:06:45 -070089bool
90StrategyChoice::insert(const Name& prefix, const Name& strategyName)
91{
Junxiao Shi838c4f12014-11-03 18:55:24 -070092 Strategy* strategy = this->getStrategy(strategyName);
93 if (strategy == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -070094 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed");
95 return false;
96 }
97
Junxiao Shi838c4f12014-11-03 18:55:24 -070098 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +000099 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700100 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +0000101 if (entry != nullptr) {
Junxiao Shie93d6a32014-09-07 16:13:22 -0700102 if (entry->getStrategy().getName() == strategy->getName()) {
103 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getName());
104 return true;
105 }
Junxiao Shi838c4f12014-11-03 18:55:24 -0700106 oldStrategy = &entry->getStrategy();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700107 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getName() <<
108 " to " << strategy->getName());
109 }
110
Junxiao Shib184e532016-05-26 18:09:57 +0000111 if (entry == nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700112 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000113 auto newEntry = make_unique<Entry>(prefix);
114 entry = newEntry.get();
115 nte->setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700116 ++m_nItems;
Junxiao Shie93d6a32014-09-07 16:13:22 -0700117 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700118 }
119
Junxiao Shi838c4f12014-11-03 18:55:24 -0700120 this->changeStrategy(*entry, *oldStrategy, *strategy);
121 entry->setStrategy(*strategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700122 return true;
123}
124
125void
126StrategyChoice::erase(const Name& prefix)
127{
128 BOOST_ASSERT(prefix.size() > 0);
129
Junxiao Shi838c4f12014-11-03 18:55:24 -0700130 shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000131 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700132 return;
133 }
134
Junxiao Shiff10da62016-07-13 17:57:43 +0000135 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000136 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700137 return;
138 }
139
140 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700141
142 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700143 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700144
Junxiao Shiff10da62016-07-13 17:57:43 +0000145 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700146 m_nameTree.eraseEntryIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700147 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700148}
149
Junxiao Shi838c4f12014-11-03 18:55:24 -0700150std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700151StrategyChoice::get(const Name& prefix) const
152{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700153 shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000154 if (nte == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000155 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700156 }
157
Junxiao Shiff10da62016-07-13 17:57:43 +0000158 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000159 if (entry == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000160 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700161 }
162
Junxiao Shiff10da62016-07-13 17:57:43 +0000163 return {true, entry->getStrategy().getName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700164}
165
166Strategy&
167StrategyChoice::findEffectiveStrategy(const Name& prefix) const
168{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700169 shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(prefix,
Junxiao Shiff10da62016-07-13 17:57:43 +0000170 [] (const name_tree::Entry& entry) { return entry.getStrategyChoiceEntry() != nullptr; });
Junxiao Shi838c4f12014-11-03 18:55:24 -0700171
Junxiao Shib184e532016-05-26 18:09:57 +0000172 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700173 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700174}
175
176Strategy&
Junxiao Shi838c4f12014-11-03 18:55:24 -0700177StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nte) const
HangZhangcb4fc832014-03-11 16:57:11 +0800178{
Junxiao Shiff10da62016-07-13 17:57:43 +0000179 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000180 if (entry != nullptr)
HangZhangcb4fc832014-03-11 16:57:11 +0800181 return entry->getStrategy();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700182
183 nte = m_nameTree.findLongestPrefixMatch(nte,
Junxiao Shiff10da62016-07-13 17:57:43 +0000184 [] (const name_tree::Entry& entry) { return entry.getStrategyChoiceEntry() != nullptr; });
Junxiao Shi838c4f12014-11-03 18:55:24 -0700185
Junxiao Shib184e532016-05-26 18:09:57 +0000186 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700187 return nte->getStrategyChoiceEntry()->getStrategy();
HangZhangcb4fc832014-03-11 16:57:11 +0800188}
189
190Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700191StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
192{
Junxiao Shib184e532016-05-26 18:09:57 +0000193 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry);
194 BOOST_ASSERT(nte != nullptr);
HangZhangcb4fc832014-03-11 16:57:11 +0800195
Junxiao Shi838c4f12014-11-03 18:55:24 -0700196 return this->findEffectiveStrategy(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700197}
198
Junxiao Shi7bb01512014-03-05 21:34:09 -0700199Strategy&
200StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
201{
Junxiao Shib184e532016-05-26 18:09:57 +0000202 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(measurementsEntry);
203 BOOST_ASSERT(nte != nullptr);
HangZhangcb4fc832014-03-11 16:57:11 +0800204
Junxiao Shi838c4f12014-11-03 18:55:24 -0700205 return this->findEffectiveStrategy(nte);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700206}
207
Junxiao Shibb5105f2014-03-03 12:06:45 -0700208void
209StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy)
210{
211 this->install(strategy);
212
Junxiao Shiff10da62016-07-13 17:57:43 +0000213 auto entry = make_unique<Entry>(Name());
214 entry->setStrategy(*strategy);
215
Junxiao Shibb5105f2014-03-03 12:06:45 -0700216 // don't use .insert here, because it will invoke findEffectiveStrategy
217 // which expects an existing root entry
Junxiao Shi838c4f12014-11-03 18:55:24 -0700218 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(Name());
Junxiao Shiff10da62016-07-13 17:57:43 +0000219 nte->setStrategyChoiceEntry(std::move(entry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700220 ++m_nItems;
Junxiao Shi838c4f12014-11-03 18:55:24 -0700221 NFD_LOG_INFO("setDefaultStrategy " << strategy->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700222}
223
Junxiao Shi838c4f12014-11-03 18:55:24 -0700224static inline void
225clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700226{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700227 NFD_LOG_TRACE("clearStrategyInfo " << nte.getPrefix());
228
229 for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
230 pitEntry->clearStrategyInfo();
231 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
232 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
233 }
234 for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
235 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
236 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700237 }
Junxiao Shib184e532016-05-26 18:09:57 +0000238 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700239 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700240 }
241}
242
Junxiao Shibb5105f2014-03-03 12:06:45 -0700243void
Junxiao Shiff10da62016-07-13 17:57:43 +0000244StrategyChoice::changeStrategy(Entry& entry,
Junxiao Shi838c4f12014-11-03 18:55:24 -0700245 fw::Strategy& oldStrategy,
246 fw::Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700247{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700248 if (&oldStrategy == &newStrategy) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700249 return;
250 }
251
Junxiao Shi838c4f12014-11-03 18:55:24 -0700252 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ")"
253 << " from " << oldStrategy.getName()
254 << " to " << newStrategy.getName());
255
256 // reset StrategyInfo on a portion of NameTree,
257 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shib184e532016-05-26 18:09:57 +0000258 const name_tree::Entry* rootNte = m_nameTree.lookup(entry).get();
Junxiao Shi60607c72014-11-26 22:40:36 -0700259 auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700260 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
261 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700262 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700263 }
Junxiao Shib184e532016-05-26 18:09:57 +0000264 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700265 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700266 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700267 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700268 });
Junxiao Shi60607c72014-11-26 22:40:36 -0700269 for (const name_tree::Entry& nte : ntChanged) {
270 clearStrategyInfo(nte);
271 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700272}
273
Junxiao Shib5888d22014-05-26 07:35:22 -0700274StrategyChoice::const_iterator
275StrategyChoice::begin() const
276{
Junxiao Shi60607c72014-11-26 22:40:36 -0700277 auto&& enumerable = m_nameTree.fullEnumerate(
Junxiao Shi838c4f12014-11-03 18:55:24 -0700278 [] (const name_tree::Entry& entry) {
Junxiao Shib184e532016-05-26 18:09:57 +0000279 return entry.getStrategyChoiceEntry() != nullptr;
Junxiao Shi60607c72014-11-26 22:40:36 -0700280 });
281 return const_iterator(enumerable.begin());
Junxiao Shib5888d22014-05-26 07:35:22 -0700282}
283
Junxiao Shiff10da62016-07-13 17:57:43 +0000284} // namespace strategy_choice
Junxiao Shibb5105f2014-03-03 12:06:45 -0700285} // namespace nfd