blob: 6f90a80b9f6c6396402e66705770bf004dacd67e [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
Yingdi Yue5224e92014-04-29 18:04:02 -070059 RibManager();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070060
61 void
62 registerWithNfd();
63
64 void
65 enableLocalControlHeader();
66
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070067 void
68 setConfigFile(ConfigFile& configFile);
69
70private:
Vince Lehman4387e782014-06-19 16:57:45 -050071 typedef uint32_t TransactionId;
72
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070073 void
74 onConfig(const ConfigSection& configSection,
75 bool isDryRun,
76 const std::string& filename);
77
78 void
Junxiao Shia3295742014-05-16 22:40:10 -070079 startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest);
80
81 void
Yingdi Yue5224e92014-04-29 18:04:02 -070082 onLocalhopRequest(const Interest& request);
83
84 void
85 onLocalhostRequest(const Interest& request);
86
87 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070088 sendResponse(const Name& name,
89 const ControlResponse& response);
90
91 void
92 sendResponse(const Name& name,
93 uint32_t code,
94 const std::string& text);
95
96 void
Vince Lehman4387e782014-06-19 16:57:45 -050097 sendSuccessResponse(const shared_ptr<const Interest>& request,
98 const ControlParameters& parameters);
99
100 void
101 sendErrorResponse(uint32_t code, const std::string& error,
102 const shared_ptr<const Interest>& request);
103
104 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700105 registerEntry(const shared_ptr<const Interest>& request,
106 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700107
108 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700109 unregisterEntry(const shared_ptr<const Interest>& request,
110 ControlParameters& parameters);
111
112 void
113 onCommandValidated(const shared_ptr<const Interest>& request);
114
115 void
116 onCommandValidationFailed(const shared_ptr<const Interest>& request,
117 const std::string& failureInfo);
118
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700119
120 void
121 onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700122 const shared_ptr<const Interest>& request,
Vince12e49462014-06-09 13:29:32 -0500123 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700124
125 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700126 onRegSuccess(const shared_ptr<const Interest>& request,
127 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500128 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700129
130 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700131 onUnRegSuccess(const shared_ptr<const Interest>& request,
132 const ControlParameters& parameters,
Vince12e49462014-06-09 13:29:32 -0500133 const FaceEntry& faceEntry);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700134
135 void
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700136 onNrdCommandPrefixAddNextHopSuccess(const Name& prefix);
137
138 void
139 onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
140
141 void
Vince Lehman4387e782014-06-19 16:57:45 -0500142 onAddNextHopSuccess(const shared_ptr<const Interest>& request,
143 const ControlParameters& parameters,
144 const TransactionId transactionId,
145 const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700146
147 void
Vince Lehman4387e782014-06-19 16:57:45 -0500148 onAddNextHopError(uint32_t code, const std::string& error,
149 const shared_ptr<const Interest>& request,
150 const TransactionId transactionId, const bool shouldSendResponse);
151
152 void
153 onRemoveNextHopSuccess(const shared_ptr<const Interest>& request,
154 const ControlParameters& parameters,
155 const TransactionId transactionId,
156 const bool shouldSendResponse);
157
158 void
159 onRemoveNextHopError(uint32_t code, const std::string& error,
160 const shared_ptr<const Interest>& request,
161 const TransactionId transactionId, const bool shouldSendResponse);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700162
163 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700164 onControlHeaderSuccess();
165
166 void
167 onControlHeaderError(uint32_t code, const std::string& reason);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700168 static bool
169 extractParameters(const Name::Component& parameterComponent,
170 ControlParameters& extractedParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700171
172 bool
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700173 validateParameters(const ControlCommand& command,
174 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700175
176 void
177 onNotification(const FaceEventNotification& notification);
178
Vince Lehman4387e782014-06-19 16:57:45 -0500179 void
180 sendUpdatesToFib(const shared_ptr<const Interest>& request,
181 const ControlParameters& parameters);
182
183 void
184 sendUpdatesToFibAfterFaceDestroyEvent();
185
186 /** \brief Checks if the transaction has received all of the expected responses
187 * from the FIB.
188 * \return{ True if the transaction with the passed transactionId has applied
189 * all of its FIB updates successfully; otherwise, false }
190 */
191 bool
192 isTransactionComplete(const TransactionId transactionId);
193
194 void
195 invalidateTransaction(const TransactionId transactionId);
196
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700197private:
198 Rib m_managedRib;
Yingdi Yuf4db0b52014-04-17 13:17:39 -0700199 ndn::Face m_face;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700200 ndn::nfd::Controller m_nfdController;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700201 ndn::KeyChain m_keyChain;
Yingdi Yue5224e92014-04-29 18:04:02 -0700202 ndn::ValidatorConfig m_localhostValidator;
203 ndn::ValidatorConfig m_localhopValidator;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700204 FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700205 bool m_isLocalhopEnabled;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700206
Vince Lehman4387e782014-06-19 16:57:45 -0500207 /** \brief The last transaction ID for FIB update response messages.
208 * Each group of FIB updates applied to the FIB is assigned an incrementing
209 * ID that is used to track the number of successfully applied updates.
210 */
211 TransactionId m_lastTransactionId;
212
213 /// table of FIB update transactions => count of pending FIB updates
214 typedef std::map<TransactionId, int> FibTransactionTable;
215
216 /** \brief Table used to track the number of FIB updates that have not yet received
217 * a response from the FIB.
218 * The table maps a transaction ID to the number of updates remaining for that
219 * specific transaction.
220 */
221 FibTransactionTable m_pendingFibTransactions;
222
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700223 typedef function<void(RibManager*,
224 const shared_ptr<const Interest>& request,
225 ControlParameters& parameters)> VerbProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700226
227 typedef std::map<name::Component, VerbProcessor> VerbDispatchTable;
228
229 typedef std::pair<name::Component, VerbProcessor> VerbAndProcessor;
230
231
232 const VerbDispatchTable m_verbDispatch;
233
234 static const Name COMMAND_PREFIX; // /localhost/nrd
235 static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd
236
237 // number of components in an invalid, but not malformed, unsigned command.
238 // (/localhost/nrd + verb + options) = 4
239 static const size_t COMMAND_UNSIGNED_NCOMPS;
240
241 // number of components in a valid signed Interest.
242 // 8 with signed Interest support.
243 static const size_t COMMAND_SIGNED_NCOMPS;
244
245 static const VerbAndProcessor COMMAND_VERBS[];
246};
247
248} // namespace rib
249} // namespace nfd
250
251#endif // NFD_RIB_RIB_MANAGER_HPP