blob: be10945674de5015b92e3bb279ec98276a588bd3 [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
Junxiao Shi811c0102016-08-10 04:12:45 +000039static inline bool
40nteHasStrategyChoiceEntry(const name_tree::Entry& nte)
41{
42 return nte.getStrategyChoiceEntry() != nullptr;
43}
44
Junxiao Shia49a1ab2016-07-15 18:24:36 +000045StrategyChoice::StrategyChoice(NameTree& nameTree, unique_ptr<Strategy> defaultStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -070046 : m_nameTree(nameTree)
Junxiao Shib5888d22014-05-26 07:35:22 -070047 , m_nItems(0)
Junxiao Shibb5105f2014-03-03 12:06:45 -070048{
Junxiao Shia49a1ab2016-07-15 18:24:36 +000049 this->setDefaultStrategy(std::move(defaultStrategy));
Junxiao Shibb5105f2014-03-03 12:06:45 -070050}
51
52bool
Junxiao Shie93d6a32014-09-07 16:13:22 -070053StrategyChoice::hasStrategy(const Name& strategyName, bool isExact) const
Junxiao Shibb5105f2014-03-03 12:06:45 -070054{
Junxiao Shie93d6a32014-09-07 16:13:22 -070055 if (isExact) {
56 return m_strategyInstances.count(strategyName) > 0;
57 }
58 else {
Junxiao Shib184e532016-05-26 18:09:57 +000059 return this->getStrategy(strategyName) != nullptr;
Junxiao Shie93d6a32014-09-07 16:13:22 -070060 }
Junxiao Shibb5105f2014-03-03 12:06:45 -070061}
62
Junxiao Shia49a1ab2016-07-15 18:24:36 +000063std::pair<bool, Strategy*>
64StrategyChoice::install(unique_ptr<Strategy> strategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -070065{
Junxiao Shib184e532016-05-26 18:09:57 +000066 BOOST_ASSERT(strategy != nullptr);
Junxiao Shia49a1ab2016-07-15 18:24:36 +000067 Name strategyName = strategy->getName();
68 // copying Name, so that strategyName remains available even if strategy is deallocated
Junxiao Shibb5105f2014-03-03 12:06:45 -070069
Junxiao Shia49a1ab2016-07-15 18:24:36 +000070 bool isInserted = false;
71 StrategyInstanceTable::iterator it;
72 std::tie(it, isInserted) = m_strategyInstances.emplace(strategyName, std::move(strategy));
73
74 if (!isInserted) {
Junxiao Shibb5105f2014-03-03 12:06:45 -070075 NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName");
Junxiao Shibb5105f2014-03-03 12:06:45 -070076 }
Junxiao Shia49a1ab2016-07-15 18:24:36 +000077 return std::make_pair(isInserted, it->second.get());
Junxiao Shibb5105f2014-03-03 12:06:45 -070078}
79
Junxiao Shia49a1ab2016-07-15 18:24:36 +000080Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -070081StrategyChoice::getStrategy(const Name& strategyName) const
82{
Junxiao Shia49a1ab2016-07-15 18:24:36 +000083 Strategy* candidate = nullptr;
Junxiao Shi838c4f12014-11-03 18:55:24 -070084 for (auto it = m_strategyInstances.lower_bound(strategyName);
Junxiao Shie93d6a32014-09-07 16:13:22 -070085 it != m_strategyInstances.end() && strategyName.isPrefixOf(it->first); ++it) {
86 switch (it->first.size() - strategyName.size()) {
87 case 0: // exact match
Junxiao Shi838c4f12014-11-03 18:55:24 -070088 return it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -070089 case 1: // unversioned strategyName matches versioned strategy
Junxiao Shi838c4f12014-11-03 18:55:24 -070090 candidate = it->second.get();
Junxiao Shie93d6a32014-09-07 16:13:22 -070091 break;
92 }
93 }
94 return candidate;
95}
96
Junxiao Shibb5105f2014-03-03 12:06:45 -070097bool
98StrategyChoice::insert(const Name& prefix, const Name& strategyName)
99{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700100 Strategy* strategy = this->getStrategy(strategyName);
101 if (strategy == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700102 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed");
103 return false;
104 }
105
Junxiao Shi7f358432016-08-11 17:06:33 +0000106 name_tree::Entry& nte = m_nameTree.lookup(prefix);
107 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -0700108 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +0000109 if (entry != nullptr) {
Junxiao Shie93d6a32014-09-07 16:13:22 -0700110 if (entry->getStrategy().getName() == strategy->getName()) {
111 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getName());
112 return true;
113 }
Junxiao Shi838c4f12014-11-03 18:55:24 -0700114 oldStrategy = &entry->getStrategy();
Junxiao Shie93d6a32014-09-07 16:13:22 -0700115 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getName() <<
116 " to " << strategy->getName());
117 }
118
Junxiao Shib184e532016-05-26 18:09:57 +0000119 if (entry == nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700120 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000121 auto newEntry = make_unique<Entry>(prefix);
122 entry = newEntry.get();
Junxiao Shi7f358432016-08-11 17:06:33 +0000123 nte.setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700124 ++m_nItems;
Junxiao Shie93d6a32014-09-07 16:13:22 -0700125 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700126 }
127
Junxiao Shi838c4f12014-11-03 18:55:24 -0700128 this->changeStrategy(*entry, *oldStrategy, *strategy);
129 entry->setStrategy(*strategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700130 return true;
131}
132
133void
134StrategyChoice::erase(const Name& prefix)
135{
136 BOOST_ASSERT(prefix.size() > 0);
137
Junxiao Shi811c0102016-08-10 04:12:45 +0000138 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000139 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700140 return;
141 }
142
Junxiao Shiff10da62016-07-13 17:57:43 +0000143 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000144 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700145 return;
146 }
147
148 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700149
150 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700151 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700152
Junxiao Shiff10da62016-07-13 17:57:43 +0000153 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000154 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700155 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700156}
157
Junxiao Shi838c4f12014-11-03 18:55:24 -0700158std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700159StrategyChoice::get(const Name& prefix) const
160{
Junxiao Shi811c0102016-08-10 04:12:45 +0000161 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000162 if (nte == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000163 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700164 }
165
Junxiao Shiff10da62016-07-13 17:57:43 +0000166 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000167 if (entry == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000168 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700169 }
170
Junxiao Shiff10da62016-07-13 17:57:43 +0000171 return {true, entry->getStrategy().getName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700172}
173
Junxiao Shidd8f6612016-08-12 15:42:52 +0000174template<typename K>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700175Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000176StrategyChoice::findEffectiveStrategyImpl(const K& key) const
Junxiao Shibb5105f2014-03-03 12:06:45 -0700177{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000178 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000179 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700180 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700181}
182
183Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000184StrategyChoice::findEffectiveStrategy(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +0800185{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000186 return this->findEffectiveStrategyImpl(prefix);
HangZhangcb4fc832014-03-11 16:57:11 +0800187}
188
189Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700190StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
191{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000192 return this->findEffectiveStrategyImpl(pitEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700193}
194
Junxiao Shi7bb01512014-03-05 21:34:09 -0700195Strategy&
196StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
197{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000198 return this->findEffectiveStrategyImpl(measurementsEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700199}
200
Junxiao Shibb5105f2014-03-03 12:06:45 -0700201void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000202StrategyChoice::setDefaultStrategy(unique_ptr<Strategy> strategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700203{
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000204 bool isInstalled = false;
205 Strategy* instance = nullptr;
206 std::tie(isInstalled, instance) = this->install(std::move(strategy));
207 BOOST_ASSERT(isInstalled);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700208
Junxiao Shiff10da62016-07-13 17:57:43 +0000209 auto entry = make_unique<Entry>(Name());
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000210 entry->setStrategy(*instance);
Junxiao Shiff10da62016-07-13 17:57:43 +0000211
Junxiao Shibb5105f2014-03-03 12:06:45 -0700212 // don't use .insert here, because it will invoke findEffectiveStrategy
213 // which expects an existing root entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000214 name_tree::Entry& nte = m_nameTree.lookup(Name());
215 nte.setStrategyChoiceEntry(std::move(entry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700216 ++m_nItems;
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000217 NFD_LOG_INFO("setDefaultStrategy " << instance->getName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700218}
219
Junxiao Shi838c4f12014-11-03 18:55:24 -0700220static inline void
221clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700222{
Junxiao Shie3cf2852016-08-09 03:50:56 +0000223 NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700224
225 for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
226 pitEntry->clearStrategyInfo();
227 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
228 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
229 }
230 for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
231 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
232 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700233 }
Junxiao Shib184e532016-05-26 18:09:57 +0000234 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700235 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700236 }
237}
238
Junxiao Shibb5105f2014-03-03 12:06:45 -0700239void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000240StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700241{
Junxiao Shi838c4f12014-11-03 18:55:24 -0700242 if (&oldStrategy == &newStrategy) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700243 return;
244 }
245
Junxiao Shi838c4f12014-11-03 18:55:24 -0700246 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ")"
247 << " from " << oldStrategy.getName()
248 << " to " << newStrategy.getName());
249
250 // reset StrategyInfo on a portion of NameTree,
251 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000252 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000253 BOOST_ASSERT(rootNte != nullptr);
Junxiao Shi60607c72014-11-26 22:40:36 -0700254 auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700255 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
256 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700257 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700258 }
Junxiao Shib184e532016-05-26 18:09:57 +0000259 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700260 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700261 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700262 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700263 });
Junxiao Shi60607c72014-11-26 22:40:36 -0700264 for (const name_tree::Entry& nte : ntChanged) {
265 clearStrategyInfo(nte);
266 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700267}
268
Junxiao Shib5888d22014-05-26 07:35:22 -0700269StrategyChoice::const_iterator
270StrategyChoice::begin() const
271{
Junxiao Shi811c0102016-08-10 04:12:45 +0000272 return const_iterator(m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry).begin());
Junxiao Shib5888d22014-05-26 07:35:22 -0700273}
274
Junxiao Shiff10da62016-07-13 17:57:43 +0000275} // namespace strategy_choice
Junxiao Shibb5105f2014-03-03 12:06:45 -0700276} // namespace nfd