NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
strategy-choice-manager.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
28 #include "core/logger.hpp"
29 #include "mgmt/app-face.hpp"
30 
31 namespace nfd {
32 
33 NFD_LOG_INIT("StrategyChoiceManager");
34 
35 const Name StrategyChoiceManager::COMMAND_PREFIX = "/localhost/nfd/strategy-choice";
36 
37 const size_t StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS =
38  StrategyChoiceManager::COMMAND_PREFIX.size() +
39  1 + // verb
40  1; // verb parameters
41 
42 const size_t StrategyChoiceManager::COMMAND_SIGNED_NCOMPS =
43  StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS +
44  4; // (timestamp, nonce, signed info tlv, signature tlv)
45 
46 const Name StrategyChoiceManager::LIST_DATASET_PREFIX("/localhost/nfd/strategy-choice/list");
47 
49  shared_ptr<InternalFace> face,
50  ndn::KeyChain& keyChain)
51  : ManagerBase(face, STRATEGY_CHOICE_PRIVILEGE, keyChain)
52  , m_strategyChoice(strategyChoice)
53  , m_listPublisher(strategyChoice, *m_face, LIST_DATASET_PREFIX, keyChain)
54 {
55  face->setInterestFilter("/localhost/nfd/strategy-choice",
57 }
58 
60 {
61 
62 }
63 
64 void
66 {
67  const Name& command = request.getName();
68  const size_t commandNComps = command.size();
69 
70  if (command == LIST_DATASET_PREFIX)
71  {
72  listStrategies(request);
73  return;
74  }
75  else if (commandNComps <= COMMAND_PREFIX.size())
76  {
77  // command is too short to have a verb
78  NFD_LOG_DEBUG("command result: malformed");
79  sendResponse(command, 400, "Malformed command");
80  return;
81  }
82 
83  if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
84  commandNComps < COMMAND_SIGNED_NCOMPS)
85  {
86  NFD_LOG_DEBUG("command result: unsigned verb: " << command);
87  sendResponse(command, 401, "Signature required");
88 
89  return;
90  }
91  else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
92  !COMMAND_PREFIX.isPrefixOf(command))
93  {
94  NFD_LOG_DEBUG("command result: malformed");
95  sendResponse(command, 400, "Malformed command");
96  return;
97  }
98 
99  validate(request,
100  bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest, this, _1),
101  bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
102 }
103 
104 void
105 StrategyChoiceManager::listStrategies(const Interest& request)
106 {
107  m_listPublisher.publish();
108 }
109 
110 void
111 StrategyChoiceManager::onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request)
112 {
113  static const Name::Component VERB_SET("set");
114  static const Name::Component VERB_UNSET("unset");
115 
116  const Name& command = request->getName();
117  const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
118 
119  ControlParameters parameters;
120  if (!extractParameters(parameterComponent, parameters))
121  {
122  sendResponse(command, 400, "Malformed command");
123  return;
124  }
125 
126  const Name::Component& verb = command.at(COMMAND_PREFIX.size());
127  ControlResponse response;
128  if (verb == VERB_SET)
129  {
130  setStrategy(parameters, response);
131  }
132  else if (verb == VERB_UNSET)
133  {
134  unsetStrategy(parameters, response);
135  }
136  else
137  {
138  NFD_LOG_DEBUG("command result: unsupported verb: " << verb);
139  setResponse(response, 501, "Unsupported command");
140  }
141 
142  sendResponse(command, response);
143 }
144 
145 void
146 StrategyChoiceManager::setStrategy(ControlParameters& parameters,
147  ControlResponse& response)
148 {
150 
151  if (!validateParameters(command, parameters))
152  {
153  NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
154  setResponse(response, 400, "Malformed command");
155  return;
156  }
157 
158  const Name& prefix = parameters.getName();
159  const Name& selectedStrategy = parameters.getStrategy();
160 
161  if (!m_strategyChoice.hasStrategy(selectedStrategy))
162  {
163  NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: "
164  << parameters.getStrategy());
165  setResponse(response, 504, "Unsupported strategy");
166  return;
167  }
168 
169  if (m_strategyChoice.insert(prefix, selectedStrategy))
170  {
171  NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
172  auto currentStrategyChoice = m_strategyChoice.get(prefix);
173  BOOST_ASSERT(currentStrategyChoice.first);
174  parameters.setStrategy(currentStrategyChoice.second);
175  setResponse(response, 200, "Success", parameters.wireEncode());
176  }
177  else
178  {
179  NFD_LOG_DEBUG("strategy-choice result: FAIL reason: not-installed");
180  setResponse(response, 405, "Strategy not installed");
181  }
182 }
183 
184 void
185 StrategyChoiceManager::unsetStrategy(ControlParameters& parameters,
186  ControlResponse& response)
187 {
189 
190  if (!validateParameters(command, parameters))
191  {
192  static const Name ROOT_PREFIX;
193  if (parameters.hasName() && parameters.getName() == ROOT_PREFIX)
194  {
195  NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unset-root");
196  setResponse(response, 403, "Cannot unset root prefix strategy");
197  }
198  else
199  {
200  NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
201  setResponse(response, 400, "Malformed command");
202  }
203  return;
204  }
205 
206  m_strategyChoice.erase(parameters.getName());
207 
208  NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
209  setResponse(response, 200, "Success", parameters.wireEncode());
210 }
211 
212 
213 
214 } // namespace nfd
represents a strategy-choice/set command
const Name & getName() const
Definition: interest.hpp:216
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:36
represents the Strategy Choice table
represents parameters in a ControlCommand request or response
bool insert(const Name &prefix, const Name &strategyName)
set strategy of prefix to be strategyName
void sendResponse(const Name &name, const ControlResponse &response)
represents an Interest packet
Definition: interest.hpp:45
represents a strategy-choice/set command
ControlParameters & setStrategy(const Name &strategy)
std::pair< bool, Name > get(const Name &prefix) const
get strategy Name of prefix
size_t wireEncode(EncodingImpl< TAG > &encoder) const
static bool extractParameters(const Name::Component &parameterComponent, ControlParameters &extractedParameters)
ndn::mgmt::ControlResponse ControlResponse
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
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)
size_t size() const
Get the number of components.
Definition: name.hpp:408
Name abstraction to represent an absolute name.
Definition: name.hpp:46
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
Component holds a read-only name component value.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:33
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.cpp:320
void setResponse(ControlResponse &response, uint32_t code, const std::string &text)
virtual bool validateParameters(const ControlCommand &command, ControlParameters &parameters)
const std::string STRATEGY_CHOICE_PRIVILEGE
void validate(const Interest &interest, const ndn::OnInterestValidated &onValidated, const ndn::OnInterestValidationFailed &onValidationFailed)
const Component & at(ssize_t i) const
Get component at the specified index.
Definition: name.hpp:442