blob: 6566d1f714c88eff39cdcb913563ea8d81fa4d5b [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince12e49462014-06-09 13:29:32 -05003 * Copyright (c) 2014, 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 Afanasyev3ecec502014-04-16 13:42:44 -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/>.
Vince12e49462014-06-09 13:29:32 -050024 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070025
26#ifndef NFD_RIB_RIB_MANAGER_HPP
27#define NFD_RIB_RIB_MANAGER_HPP
28
29#include "rib.hpp"
30#include "face-monitor.hpp"
31#include "core/config-file.hpp"
32
Alexander Afanasyev4a771362014-04-24 21:29:33 -070033#include <ndn-cxx/security/validator-config.hpp>
34#include <ndn-cxx/management/nfd-controller.hpp>
35#include <ndn-cxx/management/nfd-control-command.hpp>
36#include <ndn-cxx/management/nfd-control-response.hpp>
37#include <ndn-cxx/management/nfd-control-parameters.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070038
39namespace nfd {
40namespace rib {
41
42using ndn::nfd::ControlCommand;
43using ndn::nfd::ControlResponse;
44using ndn::nfd::ControlParameters;
45
46class RibManager : noncopyable
47{
48public:
Yingdi Yue5224e92014-04-29 18:04:02 -070049 class Error : public std::runtime_error
50 {
51 public:
52 explicit
53 Error(const std::string& what)
54 : std::runtime_error(what)
55 {
56 }
57 };
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070058
Vince Lehman72446ec2014-07-09 10:50:02 -050059 explicit
60 RibManager(ndn::Face& face);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070061
62 void
63 registerWithNfd();
64
65 void
66 enableLocalControlHeader();
67
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070068 void
69 setConfigFile(ConfigFile& configFile);
70
71private:
Vince Lehman4387e782014-06-19 16:57:45 -050072 typedef uint32_t TransactionId;
73
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070074 void
75 onConfig(const ConfigSection& configSection,
76 bool isDryRun,
77 const std::string& filename);
78
79 void
Junxiao Shia3295742014-05-16 22:40:10 -070080 startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest);
81
82 void
Yingdi Yue5224e92014-04-29 18:04:02 -070083 onLocalhopRequest(const Interest& request);
84
85 void
86 onLocalhostRequest(const Interest& request);
87
88 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070089 sendResponse(const Name& name,
90 const ControlResponse& response);
91
92 void
93 sendResponse(const Name& name,
94 uint32_t code,
95 const std::string& text);
96
97 void
Vince Lehman4387e782014-06-19 16:57:45 -050098 sendSuccessResponse(const shared_ptr<const Interest>& request,
99 const ControlParameters& parameters);
100
101 void
102 sendErrorResponse(uint32_t code, const std::string& error,
103 const shared_ptr<const Interest>& request);
104
105 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700106 registerEntry(const shared_ptr<const Interest>& request,
107 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700108
109 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700110 unregisterEntry(const shared_ptr<const Interest>& request,
111 ControlParameters& parameters);
112
113 void
114 onCommandValidated(const shared_ptr<const Interest>& request);
115
116 void
117 onCommandValidationFailed(const shared_ptr<const Interest>& request,
118 const std::string& failureInfo);
119
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700120
121 void
122 onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700123 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500124 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700125
126 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700127 onRegSuccess(const shared_ptr<const Interest>& request,
128 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500129 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700130
131 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700132 onUnRegSuccess(const shared_ptr<const Interest>& request,
133 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500134 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700135
136 void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700137 onNrdCommandPrefixAddNextHopSuccess(const Name& prefix);
138
139 void
140 onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
141
142 void
Vince Lehman4387e782014-06-19 16:57:45 -0500143 onAddNextHopSuccess(const shared_ptr<const Interest>& request,
144 const ControlParameters& parameters,
145 const TransactionId transactionId,
146 const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700147
148 void
Vince Lehman4387e782014-06-19 16:57:45 -0500149 onAddNextHopError(uint32_t code, const std::string& error,
150 const shared_ptr<const Interest>& request,
151 const TransactionId transactionId, const bool shouldSendResponse);
152
153 void
154 onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
155 const ControlParameters& parameters,
156 const TransactionId transactionId,
157 const bool shouldSendResponse);
158
159 void
160 onRemoveNextHopError(uint32_t code, const std::string& error,
161 const shared_ptr<const Interest>& request,
162 const TransactionId transactionId, const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700163
164 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700165 onControlHeaderSuccess();
166
167 void
168 onControlHeaderError(uint32_t code, const std::string& reason);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700169 static bool
170 extractParameters(const Name::Component& parameterComponent,
171 ControlParameters& extractedParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700172
173 bool
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700174 validateParameters(const ControlCommand& command,
175 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700176
177 void
178 onNotification(const FaceEventNotification& notification);
179
Vince Lehman4387e782014-06-19 16:57:45 -0500180 void
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700181 processErasureAfterNotification(uint64_t faceId);
182
183 void
Vince Lehman4387e782014-06-19 16:57:45 -0500184 sendUpdatesToFib(const shared_ptr<const Interest>& request,
185 const ControlParameters& parameters);
186
187 void
188 sendUpdatesToFibAfterFaceDestroyEvent();
189
190 /** \brief Checks if the transaction has received all of the expected responses
191 * from the FIB.
192 * \return{ True if the transaction with the passed transactionId has applied
193 * all of its FIB updates successfully; otherwise, false }
194 */
195 bool
196 isTransactionComplete(const TransactionId transactionId);
197
198 void
199 invalidateTransaction(const TransactionId transactionId);
200
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700201private:
202 Rib m_managedRib;
Vince Lehman72446ec2014-07-09 10:50:02 -0500203 ndn::Face& m_face;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700204 ndn::nfd::Controller m_nfdController;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700205 ndn::KeyChain m_keyChain;
Yingdi Yue5224e92014-04-29 18:04:02 -0700206 ndn::ValidatorConfig m_localhostValidator;
207 ndn::ValidatorConfig m_localhopValidator;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700208 FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700209 bool m_isLocalhopEnabled;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700210
Vince Lehman4387e782014-06-19 16:57:45 -0500211 /** \brief The last transaction ID for FIB update response messages.
212 * Each group of FIB updates applied to the FIB is assigned an incrementing
213 * ID that is used to track the number of successfully applied updates.
214 */
215 TransactionId m_lastTransactionId;
216
217 /// table of FIB update transactions => count of pending FIB updates
218 typedef std::map<TransactionId, int> FibTransactionTable;
219
220 /** \brief Table used to track the number of FIB updates that have not yet received
221 * a response from the FIB.
222 * The table maps a transaction ID to the number of updates remaining for that
223 * specific transaction.
224 */
225 FibTransactionTable m_pendingFibTransactions;
226
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700227 typedef function<void(RibManager*,
228 const shared_ptr<const Interest>& request,
229 ControlParameters& parameters)> VerbProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700230
231 typedef std::map<name::Component, VerbProcessor> VerbDispatchTable;
232
233 typedef std::pair<name::Component, VerbProcessor> VerbAndProcessor;
234
235
236 const VerbDispatchTable m_verbDispatch;
237
238 static const Name COMMAND_PREFIX; // /localhost/nrd
239 static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd
240
241 // number of components in an invalid, but not malformed, unsigned command.
242 // (/localhost/nrd + verb + options) = 4
243 static const size_t COMMAND_UNSIGNED_NCOMPS;
244
245 // number of components in a valid signed Interest.
246 // 8 with signed Interest support.
247 static const size_t COMMAND_SIGNED_NCOMPS;
248
249 static const VerbAndProcessor COMMAND_VERBS[];
250};
251
252} // namespace rib
253} // namespace nfd
254
255#endif // NFD_RIB_RIB_MANAGER_HPP