blob: 233f3675f1b6ad2f1e35f9b16fb8e6ddb56daabc [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/*
Junxiao Shi057d1492018-03-20 17:14:18 +00003 * Copyright (c) 2014-2018, 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 Shib5888d22014-05-26 07:35:22 -070053 , m_nItems(0)
Junxiao Shibb5105f2014-03-03 12:06:45 -070054{
Junxiao Shic34d1672016-12-09 15:57:59 +000055}
56
57void
58StrategyChoice::setDefaultStrategy(const Name& strategyName)
59{
Junxiao Shic34d1672016-12-09 15:57:59 +000060 auto entry = make_unique<Entry>(Name());
Junxiao Shif15058a2016-12-11 20:15:05 +000061 entry->setStrategy(Strategy::create(strategyName, m_forwarder));
Junxiao Shi4cb74312016-12-25 20:48:47 +000062 NFD_LOG_INFO("setDefaultStrategy " << entry->getStrategyInstanceName());
Junxiao Shic34d1672016-12-09 15:57:59 +000063
64 // don't use .insert here, because it will invoke findEffectiveStrategy
65 // which expects an existing root entry
66 name_tree::Entry& nte = m_nameTree.lookup(Name());
67 nte.setStrategyChoiceEntry(std::move(entry));
68 ++m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -070069}
70
Junxiao Shi530cf002017-01-03 14:43:16 +000071StrategyChoice::InsertResult
Junxiao Shibb5105f2014-03-03 12:06:45 -070072StrategyChoice::insert(const Name& prefix, const Name& strategyName)
73{
Junxiao Shi4e837f82017-10-02 22:14:43 +000074 if (prefix.size() > NameTree::getMaxDepth()) {
75 return InsertResult::DEPTH_EXCEEDED;
76 }
77
Junxiao Shi7f566dd2016-12-27 02:28:31 +000078 unique_ptr<Strategy> strategy;
Junxiao Shi18739c42016-12-22 08:03:00 +000079 try {
Junxiao Shi7f566dd2016-12-27 02:28:31 +000080 strategy = Strategy::create(strategyName, m_forwarder);
Junxiao Shi18739c42016-12-22 08:03:00 +000081 }
82 catch (const std::invalid_argument& e) {
83 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
Junxiao Shi530cf002017-01-03 14:43:16 +000084 return InsertResult(InsertResult::EXCEPTION, e.what());
Junxiao Shi18739c42016-12-22 08:03:00 +000085 }
86
Junxiao Shi838c4f12014-11-03 18:55:24 -070087 if (strategy == nullptr) {
Junxiao Shi7f566dd2016-12-27 02:28:31 +000088 NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered");
Junxiao Shi530cf002017-01-03 14:43:16 +000089 return InsertResult::NOT_REGISTERED;
Junxiao Shibb5105f2014-03-03 12:06:45 -070090 }
91
Junxiao Shi057d1492018-03-20 17:14:18 +000092 name_tree::Entry& nte = m_nameTree.lookup(prefix);
Junxiao Shi7f358432016-08-11 17:06:33 +000093 Entry* entry = nte.getStrategyChoiceEntry();
Junxiao Shi838c4f12014-11-03 18:55:24 -070094 Strategy* oldStrategy = nullptr;
Junxiao Shib184e532016-05-26 18:09:57 +000095 if (entry != nullptr) {
Junxiao Shi4cb74312016-12-25 20:48:47 +000096 if (entry->getStrategyInstanceName() == strategy->getInstanceName()) {
97 NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getInstanceName());
Junxiao Shi530cf002017-01-03 14:43:16 +000098 return InsertResult::OK;
Junxiao Shie93d6a32014-09-07 16:13:22 -070099 }
Junxiao Shi838c4f12014-11-03 18:55:24 -0700100 oldStrategy = &entry->getStrategy();
Junxiao Shi4cb74312016-12-25 20:48:47 +0000101 NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getInstanceName() <<
102 " to " << strategy->getInstanceName());
Junxiao Shie93d6a32014-09-07 16:13:22 -0700103 }
Junxiao Shif15058a2016-12-11 20:15:05 +0000104 else {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700105 oldStrategy = &this->findEffectiveStrategy(prefix);
Junxiao Shiff10da62016-07-13 17:57:43 +0000106 auto newEntry = make_unique<Entry>(prefix);
107 entry = newEntry.get();
Junxiao Shi7f358432016-08-11 17:06:33 +0000108 nte.setStrategyChoiceEntry(std::move(newEntry));
Junxiao Shibb5105f2014-03-03 12:06:45 -0700109 ++m_nItems;
Junxiao Shi4cb74312016-12-25 20:48:47 +0000110 NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getInstanceName());
Junxiao Shibb5105f2014-03-03 12:06:45 -0700111 }
112
Junxiao Shi838c4f12014-11-03 18:55:24 -0700113 this->changeStrategy(*entry, *oldStrategy, *strategy);
Junxiao Shi7f566dd2016-12-27 02:28:31 +0000114 entry->setStrategy(std::move(strategy));
Junxiao Shi530cf002017-01-03 14:43:16 +0000115 return InsertResult::OK;
116}
117
118StrategyChoice::InsertResult::InsertResult(Status status, const std::string& exceptionMessage)
119 : m_status(status)
120 , m_exceptionMessage(exceptionMessage)
121{
122}
123
124std::ostream&
125operator<<(std::ostream& os, const StrategyChoice::InsertResult& res)
126{
127 switch (res.m_status) {
128 case StrategyChoice::InsertResult::OK:
129 return os << "OK";
130 case StrategyChoice::InsertResult::NOT_REGISTERED:
131 return os << "Strategy not registered";
132 case StrategyChoice::InsertResult::EXCEPTION:
133 return os << "Error instantiating strategy: " << res.m_exceptionMessage;
Junxiao Shi4e837f82017-10-02 22:14:43 +0000134 case StrategyChoice::InsertResult::DEPTH_EXCEEDED:
135 return os << "Prefix has too many components (limit is "
136 << to_string(NameTree::getMaxDepth()) << ")";
Junxiao Shi530cf002017-01-03 14:43:16 +0000137 }
138 return os;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700139}
140
141void
142StrategyChoice::erase(const Name& prefix)
143{
144 BOOST_ASSERT(prefix.size() > 0);
145
Junxiao Shi811c0102016-08-10 04:12:45 +0000146 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000147 if (nte == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700148 return;
149 }
150
Junxiao Shiff10da62016-07-13 17:57:43 +0000151 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000152 if (entry == nullptr) {
Junxiao Shibb5105f2014-03-03 12:06:45 -0700153 return;
154 }
155
156 Strategy& oldStrategy = entry->getStrategy();
Junxiao Shie349ea12014-03-12 01:32:42 -0700157
158 Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
Junxiao Shi838c4f12014-11-03 18:55:24 -0700159 this->changeStrategy(*entry, oldStrategy, parentStrategy);
Junxiao Shie349ea12014-03-12 01:32:42 -0700160
Junxiao Shiff10da62016-07-13 17:57:43 +0000161 nte->setStrategyChoiceEntry(nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000162 m_nameTree.eraseIfEmpty(nte);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700163 --m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700164}
165
Junxiao Shi838c4f12014-11-03 18:55:24 -0700166std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700167StrategyChoice::get(const Name& prefix) const
168{
Junxiao Shi811c0102016-08-10 04:12:45 +0000169 name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
Junxiao Shib184e532016-05-26 18:09:57 +0000170 if (nte == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000171 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700172 }
173
Junxiao Shiff10da62016-07-13 17:57:43 +0000174 Entry* entry = nte->getStrategyChoiceEntry();
Junxiao Shib184e532016-05-26 18:09:57 +0000175 if (entry == nullptr) {
Junxiao Shiff10da62016-07-13 17:57:43 +0000176 return {false, Name()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700177 }
178
Junxiao Shi4cb74312016-12-25 20:48:47 +0000179 return {true, entry->getStrategyInstanceName()};
Junxiao Shibb5105f2014-03-03 12:06:45 -0700180}
181
Junxiao Shidd8f6612016-08-12 15:42:52 +0000182template<typename K>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700183Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000184StrategyChoice::findEffectiveStrategyImpl(const K& key) const
Junxiao Shibb5105f2014-03-03 12:06:45 -0700185{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000186 const name_tree::Entry* nte = m_nameTree.findLongestPrefixMatch(key, &nteHasStrategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000187 BOOST_ASSERT(nte != nullptr);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700188 return nte->getStrategyChoiceEntry()->getStrategy();
Junxiao Shibb5105f2014-03-03 12:06:45 -0700189}
190
191Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000192StrategyChoice::findEffectiveStrategy(const Name& prefix) const
HangZhangcb4fc832014-03-11 16:57:11 +0800193{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000194 return this->findEffectiveStrategyImpl(prefix);
HangZhangcb4fc832014-03-11 16:57:11 +0800195}
196
197Strategy&
Junxiao Shibb5105f2014-03-03 12:06:45 -0700198StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
199{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000200 return this->findEffectiveStrategyImpl(pitEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700201}
202
Junxiao Shi7bb01512014-03-05 21:34:09 -0700203Strategy&
204StrategyChoice::findEffectiveStrategy(const measurements::Entry& measurementsEntry) const
205{
Junxiao Shidd8f6612016-08-12 15:42:52 +0000206 return this->findEffectiveStrategyImpl(measurementsEntry);
Junxiao Shi7bb01512014-03-05 21:34:09 -0700207}
208
Junxiao Shi838c4f12014-11-03 18:55:24 -0700209static inline void
210clearStrategyInfo(const name_tree::Entry& nte)
Junxiao Shie349ea12014-03-12 01:32:42 -0700211{
Junxiao Shie3cf2852016-08-09 03:50:56 +0000212 NFD_LOG_TRACE("clearStrategyInfo " << nte.getName());
Junxiao Shi838c4f12014-11-03 18:55:24 -0700213
214 for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
215 pitEntry->clearStrategyInfo();
216 for (const pit::InRecord& inRecord : pitEntry->getInRecords()) {
217 const_cast<pit::InRecord&>(inRecord).clearStrategyInfo();
218 }
219 for (const pit::OutRecord& outRecord : pitEntry->getOutRecords()) {
220 const_cast<pit::OutRecord&>(outRecord).clearStrategyInfo();
221 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700222 }
Junxiao Shib184e532016-05-26 18:09:57 +0000223 if (nte.getMeasurementsEntry() != nullptr) {
Junxiao Shi838c4f12014-11-03 18:55:24 -0700224 nte.getMeasurementsEntry()->clearStrategyInfo();
Junxiao Shie349ea12014-03-12 01:32:42 -0700225 }
226}
227
Junxiao Shibb5105f2014-03-03 12:06:45 -0700228void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000229StrategyChoice::changeStrategy(Entry& entry, Strategy& oldStrategy, Strategy& newStrategy)
Junxiao Shibb5105f2014-03-03 12:06:45 -0700230{
Junxiao Shi55e21b92017-01-23 03:27:47 +0000231 const Name& oldInstanceName = oldStrategy.getInstanceName();
232 const Name& newInstanceName = newStrategy.getInstanceName();
233 if (Strategy::areSameType(oldInstanceName, newInstanceName)) {
234 // same Strategy subclass type: no need to clear StrategyInfo
235 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
236 << oldInstanceName << " -> " << newInstanceName
237 << " same-type");
Junxiao Shibb5105f2014-03-03 12:06:45 -0700238 return;
239 }
240
Junxiao Shi55e21b92017-01-23 03:27:47 +0000241 NFD_LOG_INFO("changeStrategy(" << entry.getPrefix() << ") "
242 << oldInstanceName << " -> " << newInstanceName);
Junxiao Shi838c4f12014-11-03 18:55:24 -0700243
244 // reset StrategyInfo on a portion of NameTree,
245 // where entry's effective strategy is covered by the changing StrategyChoice entry
Junxiao Shi7f358432016-08-11 17:06:33 +0000246 const name_tree::Entry* rootNte = m_nameTree.getEntry(entry);
Junxiao Shif2420fc2016-08-11 13:18:21 +0000247 BOOST_ASSERT(rootNte != nullptr);
Junxiao Shi60607c72014-11-26 22:40:36 -0700248 auto&& ntChanged = m_nameTree.partialEnumerate(entry.getPrefix(),
Junxiao Shi838c4f12014-11-03 18:55:24 -0700249 [&rootNte] (const name_tree::Entry& nte) -> std::pair<bool, bool> {
250 if (&nte == rootNte) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700251 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700252 }
Junxiao Shib184e532016-05-26 18:09:57 +0000253 if (nte.getStrategyChoiceEntry() != nullptr) {
Junxiao Shi60607c72014-11-26 22:40:36 -0700254 return {false, false};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700255 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700256 return {true, true};
Junxiao Shi838c4f12014-11-03 18:55:24 -0700257 });
Junxiao Shi60607c72014-11-26 22:40:36 -0700258 for (const name_tree::Entry& nte : ntChanged) {
259 clearStrategyInfo(nte);
260 }
Junxiao Shibb5105f2014-03-03 12:06:45 -0700261}
262
Junxiao Shib30863e2016-08-15 21:32:01 +0000263StrategyChoice::Range
264StrategyChoice::getRange() const
Junxiao Shib5888d22014-05-26 07:35:22 -0700265{
Junxiao Shib30863e2016-08-15 21:32:01 +0000266 return m_nameTree.fullEnumerate(&nteHasStrategyChoiceEntry) |
267 boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(
268 &name_tree::Entry::getStrategyChoiceEntry));
Junxiao Shib5888d22014-05-26 07:35:22 -0700269}
270
Junxiao Shiff10da62016-07-13 17:57:43 +0000271} // namespace strategy_choice
Junxiao Shibb5105f2014-03-03 12:06:45 -0700272} // namespace nfd