35 const Name StrategyChoiceManager::COMMAND_PREFIX =
"/localhost/nfd/strategy-choice";
37 const size_t StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS =
38 StrategyChoiceManager::COMMAND_PREFIX.size() +
42 const size_t StrategyChoiceManager::COMMAND_SIGNED_NCOMPS =
43 StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS +
46 const Name StrategyChoiceManager::LIST_DATASET_PREFIX(
"/localhost/nfd/strategy-choice/list");
49 shared_ptr<InternalFace> face,
50 ndn::KeyChain& keyChain)
52 , m_strategyChoice(strategyChoice)
53 , m_listPublisher(strategyChoice, *m_face, LIST_DATASET_PREFIX, keyChain)
55 face->setInterestFilter(
"/localhost/nfd/strategy-choice",
67 const Name& command = request.getName();
68 const size_t commandNComps = command.size();
70 if (command == LIST_DATASET_PREFIX)
72 listStrategies(request);
75 else if (commandNComps <= COMMAND_PREFIX.size())
83 if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
84 commandNComps < COMMAND_SIGNED_NCOMPS)
91 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
92 !COMMAND_PREFIX.isPrefixOf(command))
100 bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest,
this, _1),
105 StrategyChoiceManager::listStrategies(
const Interest& request)
111 StrategyChoiceManager::onValidatedStrategyChoiceRequest(
const shared_ptr<const Interest>& request)
113 static const Name::Component VERB_SET(
"set");
114 static const Name::Component VERB_UNSET(
"unset");
116 const Name& command = request->getName();
117 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
119 ControlParameters parameters;
126 const Name::Component& verb = command.at(COMMAND_PREFIX.size());
127 ControlResponse response;
128 if (verb == VERB_SET)
130 setStrategy(parameters, response);
132 else if (verb == VERB_UNSET)
134 unsetStrategy(parameters, response);
146 StrategyChoiceManager::setStrategy(ControlParameters& parameters,
147 ControlResponse& response)
149 ndn::nfd::StrategyChoiceSetCommand command;
153 NFD_LOG_DEBUG(
"strategy-choice result: FAIL reason: malformed");
158 const Name& prefix = parameters.getName();
159 const Name& selectedStrategy = parameters.getStrategy();
161 if (!m_strategyChoice.
hasStrategy(selectedStrategy))
163 NFD_LOG_DEBUG(
"strategy-choice result: FAIL reason: unknown-strategy: "
164 << parameters.getStrategy());
165 setResponse(response, 504,
"Unsupported strategy");
169 if (m_strategyChoice.
insert(prefix, selectedStrategy))
172 auto currentStrategyChoice = m_strategyChoice.
get(prefix);
173 BOOST_ASSERT(currentStrategyChoice.first);
174 parameters.setStrategy(currentStrategyChoice.second);
175 setResponse(response, 200,
"Success", parameters.wireEncode());
179 NFD_LOG_DEBUG(
"strategy-choice result: FAIL reason: not-installed");
180 setResponse(response, 405,
"Strategy not installed");
185 StrategyChoiceManager::unsetStrategy(ControlParameters& parameters,
186 ControlResponse& response)
188 ndn::nfd::StrategyChoiceUnsetCommand command;
192 static const Name ROOT_PREFIX;
193 if (parameters.hasName() && parameters.getName() == ROOT_PREFIX)
195 NFD_LOG_DEBUG(
"strategy-choice result: FAIL reason: unset-root");
196 setResponse(response, 403,
"Cannot unset root prefix strategy");
200 NFD_LOG_DEBUG(
"strategy-choice result: FAIL reason: malformed");
206 m_strategyChoice.
erase(parameters.getName());
209 setResponse(response, 200,
"Success", parameters.wireEncode());
#define NFD_LOG_DEBUG(expression)
represents the Strategy Choice table
bool insert(const Name &prefix, const Name &strategyName)
set strategy of prefix to be strategyName
void sendResponse(const Name &name, const ControlResponse &response)
std::pair< bool, Name > get(const Name &prefix) const
get strategy Name of prefix
static bool extractParameters(const Name::Component ¶meterComponent, ControlParameters &extractedParameters)
void erase(const Name &prefix)
make prefix to inherit strategy from its parent
void onStrategyChoiceRequest(const Interest &request)
void onCommandValidationFailed(const shared_ptr< const Interest > &command, const std::string &error)
StrategyChoiceManager(StrategyChoice &strategyChoice, shared_ptr< InternalFace > face, ndn::KeyChain &keyChain)
bool hasStrategy(const Name &strategyName, bool isExact=false) const
determines if a strategy is installed
virtual ~StrategyChoiceManager()
#define NFD_LOG_INIT(name)
void setResponse(ControlResponse &response, uint32_t code, const std::string &text)
virtual bool validateParameters(const ControlCommand &command, ControlParameters ¶meters)
const std::string STRATEGY_CHOICE_PRIVILEGE
void validate(const Interest &interest, const ndn::OnInterestValidated &onValidated, const ndn::OnInterestValidationFailed &onValidationFailed)