blob: b6cb0f7b57e7925e53acfac1bdb92a1ecf35e4bf [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi4e837f82017-10-02 22:14:43 +00002/*
Davide Pesavento50a6af32019-02-21 00:04:40 -05003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shib184e532016-05-26 18:09:57 +00004 * 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"
Davide Pesaventoa3148082018-04-12 18:21:54 -040027
Junxiao Shic34d1672016-12-09 15:57:59 +000028#include "measurements-entry.hpp"
29#include "pit-entry.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060030#include "core/logger.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070031#include "fw/strategy.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070032
Davide Pesavento981db802018-04-10 18:05:04 -040033#include <ndn-cxx/util/concepts.hpp>
34
Junxiao Shibb5105f2014-03-03 12:06:45 -070035namespace nfd {
Junxiao Shiff10da62016-07-13 17:57:43 +000036namespace strategy_choice {
Junxiao Shibb5105f2014-03-03 12:06:45 -070037
Davide Pesavento981db802018-04-10 18:05:04 -040038NDN_CXX_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator);
Junxiao Shi3aba7542016-12-24 02:42:52 +000039
Davide Pesaventoa3148082018-04-12 18:21:54 -040040NFD_LOG_INIT(StrategyChoice);
Junxiao Shibb5105f2014-03-03 12:06:45 -070041
Davide Pesavento981db802018-04-10 18:05:04 -040042using fw::Strategy;
43
Junxiao Shi811c0102016-08-10 04:12:45 +000044static inline bool
45nteHasStrategyChoiceEntry(const name_tree::Entry& nte)
46{
47 return nte.getStrategyChoiceEntry() != nullptr;
48}
49
Junxiao Shic34d1672016-12-09 15:57:59 +000050StrategyChoice::StrategyChoice(Forwarder& forwarder)
51 : m_forwarder(forwarder)
52 , m_nameTree(m_forwarder.getNameTree())
Junxiao Shibb5105f2014-03-03 12:06:45 -070053{
Junxiao Shic34d1672016-12-09 15:57:59 +000054}
55
56void
57StrategyChoice::setDefaultStrategy(const Name& strategyName)
58{
Junxiao Shic34d1672016-12-09 15:57:59 +000059 auto entry = make_unique<Entry>(Name());
Junxiao Shif15058a2016-12-11 20:15:05 +000060 entry->setStrategy(Strategy::create(strategyName, m_forwarder));
Junxiao Shi4cb74312016-12-25 20:48:47 +000061 NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyInstanceName());
Junxiao Shic34d1672016-12-09 15:57:59 +000062
63 // don't use .insert here, because it will invoke findEffectiveStrategy
64 // which expects an existing root entry
65 name_tree::Entry& nte = m_nameTree.lookup(Name());
66 nte.setStrategyChoiceEntry(std::move(entry));
67 ++m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -070068}
69
Junxiao Shi530cf002017-01-03 14:43:16 +000070StrategyChoice::InsertResult
Junxiao Shibb5105f2014-03-03 12:06:45 -070071StrategyChoice::insert(const Name& prefix, const Name& strategyName)
72{
Junxiao Shi4e837f82017-10-02 22:14:43 +000073 if (prefix.size() > NameTree::getMaxDepth()) {
74 return InsertResult::DEPTH_EXCEEDED;
75 }
76
Junxiao Shi7f566dd2016-12-27 02:28:31 +000077 unique_ptr<Strategy> strategy;
Junxiao Shi18739c42016-12-22 08:03:00 +000078 try {
Junxiao Shi7f566dd2016-12-27 02:28:31 +000079 strategy = Strategy::create(strategyName, m_forwarder);
Junxiao Shi18739c42016-12-22 08:03:00 +000080 }
81 catch (const std::invalid_argument& e) {
82 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
Junxiao Shi530cf002017-01-03 14:43:16 +000083 return InsertResult(InsertResult::EXCEPTION, e.what());
Junxiao Shi18739c42016-12-22 08:03:00 +000084 }
85
Junxiao Shi838c4f12014-11-03 18:55:24 -070086 if (strategy == nullptr) {
Junxiao Shi7f566dd2016-12-27 02:28:31 +000087 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered");
Junxiao Shi530cf002017-01-03 14:43:16 +000088 return InsertResult::NOT_REGISTERED;
Junxiao Shibb5105f2014-03-03 12:06:45 -070089 }
90
Junxiao Shi057d1492018-03-20 17:14:18 +000091 name_tree::Entry& nte = m_nameTree.lookup(prefix);
Junxiao Shi7f358432016-08-11 17:06:33 +000092 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -070093 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +000094 if (entry != nullptr) {
Junxiao Shi4cb74312016-12-25 20:48:47 +000095 if (entry->getStrategyInstanceName() == strategy->getInstanceName()) {
96 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getInstanceName());
Junxiao Shi530cf002017-01-03 14:43:16 +000097 return InsertResult::OK;
Junxiao Shie93d6a32014-09-07 16:13:22 -070098 }
Junxiao Shi838c4f12014-11-03 18:55:24 -070099 oldStrategy = &entry->getStrategy();
Junxiao Shi4cb74312016-12-25 20:48:47 +0000100 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getInstanceName() <<
101 " to " << strategy->getInstanceName());
Junxiao Shie93d6a32014-09-07 16:13:22 -0700102 }
Junxiao Shif15058a2016-12-11 20:15:05 +0000103 else {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700104 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000105 auto newEntry = make_unique<Entry>(prefix);
106 entry = newEntry.get();
Junxiao Shi7f358432016-08-11 17:06:33 +0000107 nte.setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700108 ++m_nItems;
Junxiao Shi4cb74312016-12-25 20:48:47 +0000109 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getInstanceName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700110 }
111
Junxiao Shi838c4f12014-11-03 18:55:24 -0700112 this->changeStrategy(*entry, *oldStrategy, *strategy);
Junxiao Shi7f566dd2016-12-27 02:28:31 +0000113 entry->setStrategy(std::move(strategy));
Junxiao Shi530cf002017-01-03 14:43:16 +0000114 return InsertResult::OK;
115}
116
117StrategyChoice::InsertResult::InsertResult(Status status, const std::string& exceptionMessage)
118 : m_status(status)
119 , m_exceptionMessage(exceptionMessage)
120{
121}
122
123std::ostream&
124operator<<(std::ostream& os, const StrategyChoice::InsertResult& res)
125{
126 switch (res.m_status) {
127 case StrategyChoice::InsertResult::OK:
128 return os << "OK";
129 case StrategyChoice::InsertResult::NOT_REGISTERED:
130 return os << "Strategy not registered";
131 case StrategyChoice::InsertResult::EXCEPTION:
132 return os << "Error instantiating strategy: " << res.m_exceptionMessage;
Junxiao Shi4e837f82017-10-02 22:14:43 +0000133 case StrategyChoice::InsertResult::DEPTH_EXCEEDED:
134 return os << "Prefix has too many components (limit is "
135 << to_string(NameTree::getMaxDepth()) << ")";
Junxiao Shi530cf002017-01-03 14:43:16 +0000136 }
137 return os;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700138}
139
140void
141StrategyChoice::erase(const Name& prefix)
142{
143 BOOST_ASSERT(prefix.size() > 0);
144
Junxiao Shi811c0102016-08-10 04:12:45 +0000145 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000146 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700147 return;
148 }
149
Junxiao Shiff10da62016-07-13 17:57:43 +0000150 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000151 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700152 return;
153 }
154
155 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700156
157 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700158 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700159
Junxiao Shiff10da62016-07-13 17:57:43 +0000160 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000161 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700162 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700163}
164
Junxiao Shi838c4f12014-11-03 18:55:24 -0700165std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700166StrategyChoice::get(const Name& prefix) const
167{
Junxiao Shi811c0102016-08-10 04:12:45 +0000168 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000169 if (nte == nullptr) {
Davide Pesavento50a6af32019-02-21 00:04:40 -0500170 return {false, {}};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700171 }
172
Junxiao Shiff10da62016-07-13 17:57:43 +0000173 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000174 if (entry == nullptr) {
Davide Pesavento50a6af32019-02-21 00:04:40 -0500175 return {false, {}};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700176 }
177
Junxiao Shi4cb74312016-12-25 20:48:47 +0000178 return {true, entry->getStrategyInstanceName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700179}
180
Junxiao Shidd8f6612016-08-12 15:42:52 +0000181template<typename K>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700182Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000183StrategyChoice::findEffectiveStrategyImpl(const K& key) const
Junxiao Shibb5105f2014-03-03 12:06:45 -0700184{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000185 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000186 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700187 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700188}
189
190Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000191StrategyChoice::findEffectiveStrategy(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +0800192{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000193 return this->findEffectiveStrategyImpl(prefix);
HangZhangcb4fc832014-03-11 16:57:11 +0800194}
195
196Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700197StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
198{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000199 return this->findEffectiveStrategyImpl(pitEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700200}
201
Junxiao Shi7bb01512014-03-05 21:34:09 -0700202Strategy&
203StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
204{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000205 return this->findEffectiveStrategyImpl(measurementsEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700206}
207
Junxiao Shi838c4f12014-11-03 18:55:24 -0700208static inline void
209clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700210{
Junxiao Shie3cf2852016-08-09 03:50:56 +0000211 NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700212
Davide Pesavento50a6af32019-02-21 00:04:40 -0500213 for (const auto& pitEntry : nte.getPitEntries()) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700214 pitEntry->clearStrategyInfo();
Davide Pesavento50a6af32019-02-21 00:04:40 -0500215 for (const auto& inRecord : pitEntry->getInRecords()) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700216 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
217 }
Davide Pesavento50a6af32019-02-21 00:04:40 -0500218 for (const auto& outRecord : pitEntry->getOutRecords()) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700219 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
220 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700221 }
Junxiao Shib184e532016-05-26 18:09:57 +0000222 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700223 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700224 }
225}
226
Junxiao Shibb5105f2014-03-03 12:06:45 -0700227void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000228StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700229{
Junxiao Shi55e21b92017-01-23 03:27:47 +0000230 const Name& oldInstanceName = oldStrategy.getInstanceName();
231 const Name& newInstanceName = newStrategy.getInstanceName();
232 if (Strategy::areSameType(oldInstanceName, newInstanceName)) {
233 // same Strategy subclass type: no need to clear StrategyInfo
234 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
Davide Pesavento50a6af32019-02-21 00:04:40 -0500235 << oldInstanceName << " -> " << newInstanceName << " same-type");
Junxiao Shibb5105f2014-03-03 12:06:45 -0700236 return;
237 }
238
Junxiao Shi55e21b92017-01-23 03:27:47 +0000239 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
240 << oldInstanceName << " -> " << newInstanceName);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700241
242 // reset StrategyInfo on a portion of NameTree,
243 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000244 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000245 BOOST_ASSERT(rootNte != nullptr);
Davide Pesavento50a6af32019-02-21 00:04:40 -0500246 const auto& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700247 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
248 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700249 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700250 }
Junxiao Shib184e532016-05-26 18:09:57 +0000251 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700252 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700253 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700254 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700255 });
Davide Pesavento50a6af32019-02-21 00:04:40 -0500256 for (const auto& nte : ntChanged) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700257 clearStrategyInfo(nte);
258 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700259}
260
Junxiao Shib30863e2016-08-15 21:32:01 +0000261StrategyChoice::Range
262StrategyChoice::getRange() const
Junxiao Shib5888d22014-05-26 07:35:22 -0700263{
Junxiao Shib30863e2016-08-15 21:32:01 +0000264 return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
265 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
266 &name_tree::Entry::getStrategyChoiceEntry));
Junxiao Shib5888d22014-05-26 07:35:22 -0700267}
268
Junxiao Shiff10da62016-07-13 17:57:43 +0000269} // namespace strategy_choice
Junxiao Shibb5105f2014-03-03 12:06:45 -0700270} // namespace nfd