blob: 8d90103c9ead366b9bc90b3546c1c9f3b46af410 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1e46be32015-01-08 20:18:05 -07003 * Copyright (c) 2014-2015, 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"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070030#include "core/config-file.hpp"
Vince Lehmancd16c832014-07-23 15:14:55 -070031#include "rib-status-publisher.hpp"
Yanbiao Lic17de832014-11-21 17:51:45 -080032#include "remote-registrator.hpp"
Vince Lehman76c751c2014-11-18 17:36:38 -060033#include "fib-updater.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070034
Alexander Afanasyev4a771362014-04-24 21:29:33 -070035#include <ndn-cxx/security/validator-config.hpp>
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070036#include <ndn-cxx/management/nfd-face-monitor.hpp>
Alexander Afanasyev4a771362014-04-24 21:29:33 -070037#include <ndn-cxx/management/nfd-controller.hpp>
38#include <ndn-cxx/management/nfd-control-command.hpp>
39#include <ndn-cxx/management/nfd-control-response.hpp>
40#include <ndn-cxx/management/nfd-control-parameters.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070041
42namespace nfd {
43namespace rib {
44
45using ndn::nfd::ControlCommand;
46using ndn::nfd::ControlResponse;
47using ndn::nfd::ControlParameters;
48
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070049using ndn::nfd::FaceEventNotification;
50
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070051class RibManager : noncopyable
52{
53public:
Yingdi Yue5224e92014-04-29 18:04:02 -070054 class Error : public std::runtime_error
55 {
56 public:
57 explicit
58 Error(const std::string& what)
59 : std::runtime_error(what)
60 {
61 }
62 };
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070063
Vince Lehman72446ec2014-07-09 10:50:02 -050064 explicit
65 RibManager(ndn::Face& face);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070066
Vince Lehman26b215c2014-08-17 15:00:41 -050067 ~RibManager();
68
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070069 void
70 registerWithNfd();
71
72 void
73 enableLocalControlHeader();
74
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070075 void
76 setConfigFile(ConfigFile& configFile);
77
Vince Lehman76c751c2014-11-18 17:36:38 -060078 void
79 onRibUpdateSuccess(const RibUpdate& update);
Vince Lehman4387e782014-06-19 16:57:45 -050080
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070081 void
Vince Lehman76c751c2014-11-18 17:36:38 -060082 onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error);
83
84private:
85 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070086 onConfig(const ConfigSection& configSection,
87 bool isDryRun,
88 const std::string& filename);
89
90 void
Junxiao Shia3295742014-05-16 22:40:10 -070091 startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest);
92
93 void
Yingdi Yue5224e92014-04-29 18:04:02 -070094 onLocalhopRequest(const Interest& request);
95
96 void
97 onLocalhostRequest(const Interest& request);
98
99 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700100 sendResponse(const Name& name,
101 const ControlResponse& response);
102
103 void
104 sendResponse(const Name& name,
105 uint32_t code,
106 const std::string& text);
107
108 void
Vince Lehman4387e782014-06-19 16:57:45 -0500109 sendSuccessResponse(const shared_ptr<const Interest>& request,
110 const ControlParameters& parameters);
111
112 void
113 sendErrorResponse(uint32_t code, const std::string& error,
114 const shared_ptr<const Interest>& request);
115
116 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700117 registerEntry(const shared_ptr<const Interest>& request,
118 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700119
120 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700121 unregisterEntry(const shared_ptr<const Interest>& request,
122 ControlParameters& parameters);
123
Vince Lehman76c751c2014-11-18 17:36:38 -0600124private:
Vince Lehman281ded72014-08-21 12:17:08 -0500125 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700126 onCommandValidated(const shared_ptr<const Interest>& request);
127
128 void
129 onCommandValidationFailed(const shared_ptr<const Interest>& request,
130 const std::string& failureInfo);
131
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700132
133 void
134 onCommandError(uint32_t code, const std::string& error,
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700135 const shared_ptr<const Interest>& request,
Vince Lehman218be0a2015-01-15 17:25:20 -0600136 const Route& route);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700137
138 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700139 onRegSuccess(const shared_ptr<const Interest>& request,
140 const ControlParameters& parameters,
Vince Lehman218be0a2015-01-15 17:25:20 -0600141 const Route& route);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700142
143 void
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700144 onUnRegSuccess(const shared_ptr<const Interest>& request,
145 const ControlParameters& parameters,
Vince Lehman218be0a2015-01-15 17:25:20 -0600146 const Route& route);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700147
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600148PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700149 void
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600150 onNrdCommandPrefixAddNextHopSuccess(const Name& prefix,
151 const ndn::nfd::ControlParameters& result);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700152
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600153private:
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700154 void
155 onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
156
157 void
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700158 onControlHeaderSuccess();
159
160 void
161 onControlHeaderError(uint32_t code, const std::string& reason);
Vince Lehman281ded72014-08-21 12:17:08 -0500162
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700163 static bool
164 extractParameters(const Name::Component& parameterComponent,
165 ControlParameters& extractedParameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700166
167 bool
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700168 validateParameters(const ControlCommand& command,
169 ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700170
171 void
172 onNotification(const FaceEventNotification& notification);
173
Vince Lehman76c751c2014-11-18 17:36:38 -0600174PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Vince Lehman4387e782014-06-19 16:57:45 -0500175 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600176 onFaceDestroyedEvent(uint64_t faceId);
Alexander Afanasyev63108c42014-07-07 19:10:47 -0700177
Vince Lehman76c751c2014-11-18 17:36:38 -0600178private:
Vince Lehmancd16c832014-07-23 15:14:55 -0700179 void
180 listEntries(const Interest& request);
181
Vince Lehmancd613c52014-07-30 14:34:49 -0500182 void
Vince Lehman26b215c2014-08-17 15:00:41 -0500183 scheduleActiveFaceFetch(const time::seconds& timeToWait);
184
185 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500186 fetchActiveFaces();
187
188 void
189 fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer);
190
191 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500192 onFetchFaceStatusTimeout();
193
Vince Lehman281ded72014-08-21 12:17:08 -0500194PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Vince Lehman26b215c2014-08-17 15:00:41 -0500195 /** \param buffer Face dataset contents
196 */
197 void
198 removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer);
199
200PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700201 Rib m_managedRib;
Vince Lehman281ded72014-08-21 12:17:08 -0500202
203private:
Vince Lehman72446ec2014-07-09 10:50:02 -0500204 ndn::Face& m_face;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700205 ndn::nfd::Controller m_nfdController;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700206 ndn::KeyChain m_keyChain;
Yingdi Yue5224e92014-04-29 18:04:02 -0700207 ndn::ValidatorConfig m_localhostValidator;
208 ndn::ValidatorConfig m_localhopValidator;
Alexander Afanasyev585e5a62014-08-12 11:49:31 -0700209 ndn::nfd::FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700210 bool m_isLocalhopEnabled;
Yanbiao Lic17de832014-11-21 17:51:45 -0800211 RemoteRegistrator m_remoteRegistrator;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700212
Vince Lehmancd16c832014-07-23 15:14:55 -0700213 RibStatusPublisher m_ribStatusPublisher;
214
Vince Lehman76c751c2014-11-18 17:36:38 -0600215PUBLIC_WITH_TESTS_ELSE_PRIVATE:
216 FibUpdater m_fibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -0500217
Vince Lehman76c751c2014-11-18 17:36:38 -0600218private:
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700219 typedef function<void(RibManager*,
220 const shared_ptr<const Interest>& request,
Vince Lehmancd16c832014-07-23 15:14:55 -0700221 ControlParameters& parameters)> SignedVerbProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700222
Vince Lehmancd16c832014-07-23 15:14:55 -0700223 typedef std::map<name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700224
Vince Lehmancd16c832014-07-23 15:14:55 -0700225 typedef std::pair<name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700226
227
Vince Lehmancd16c832014-07-23 15:14:55 -0700228 const SignedVerbDispatchTable m_signedVerbDispatch;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700229
230 static const Name COMMAND_PREFIX; // /localhost/nrd
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600231
232PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700233 static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd
234
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600235private:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700236 // number of components in an invalid, but not malformed, unsigned command.
237 // (/localhost/nrd + verb + options) = 4
238 static const size_t COMMAND_UNSIGNED_NCOMPS;
239
240 // number of components in a valid signed Interest.
241 // 8 with signed Interest support.
242 static const size_t COMMAND_SIGNED_NCOMPS;
243
Vince Lehmancd16c832014-07-23 15:14:55 -0700244 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
245
246 typedef function<void(RibManager*, const Interest&)> UnsignedVerbProcessor;
247 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
248 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
249
250 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
251 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
252
253 static const Name LIST_COMMAND_PREFIX;
254 static const size_t LIST_COMMAND_NCOMPS;
Vince Lehmancd613c52014-07-30 14:34:49 -0500255
256 static const Name FACES_LIST_DATASET_PREFIX;
257
Vince Lehman26b215c2014-08-17 15:00:41 -0500258 static const time::seconds ACTIVE_FACE_FETCH_INTERVAL;
Junxiao Shi1e46be32015-01-08 20:18:05 -0700259 scheduler::EventId m_activeFaceFetchEvent;
Vince Lehman26b215c2014-08-17 15:00:41 -0500260
261 typedef std::set<uint64_t> FaceIdSet;
262 /** \brief contains FaceIds with one or more Routes in the RIB
263 */
264 FaceIdSet m_registeredFaces;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700265};
266
267} // namespace rib
268} // namespace nfd
269
270#endif // NFD_RIB_RIB_MANAGER_HPP