blob: 7c7ff003ed33c8ee40fe195ad9d6cbe9f89ad2df [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Licf0db022016-01-29 00:54:25 -08003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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"
Yanbiao Licf0db022016-01-29 00:54:25 -080031#include "core/manager-base.hpp"
Vince Lehmancd16c832014-07-23 15:14:55 -070032#include "rib-status-publisher.hpp"
Yanbiao Lid7c96362015-01-30 23:58:24 -080033#include "auto-prefix-propagator.hpp"
Vince Lehman76c751c2014-11-18 17:36:38 -060034#include "fib-updater.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070035
Alexander Afanasyev4a771362014-04-24 21:29:33 -070036#include <ndn-cxx/security/validator-config.hpp>
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070037#include <ndn-cxx/management/nfd-face-monitor.hpp>
Alexander Afanasyev4a771362014-04-24 21:29:33 -070038#include <ndn-cxx/management/nfd-controller.hpp>
39#include <ndn-cxx/management/nfd-control-command.hpp>
40#include <ndn-cxx/management/nfd-control-response.hpp>
41#include <ndn-cxx/management/nfd-control-parameters.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070042
43namespace nfd {
44namespace rib {
45
46using ndn::nfd::ControlCommand;
47using ndn::nfd::ControlResponse;
48using ndn::nfd::ControlParameters;
49
Alexander Afanasyev585e5a62014-08-12 11:49:31 -070050using ndn::nfd::FaceEventNotification;
51
Yanbiao Licf0db022016-01-29 00:54:25 -080052class RibManager : public nfd::ManagerBase
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070053{
54public:
Yingdi Yue5224e92014-04-29 18:04:02 -070055 class Error : public std::runtime_error
56 {
57 public:
58 explicit
59 Error(const std::string& what)
60 : std::runtime_error(what)
61 {
62 }
63 };
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070064
Yanbiao Licf0db022016-01-29 00:54:25 -080065public:
66 RibManager(Dispatcher& dispatcher, ndn::Face& face, ndn::KeyChain& keyChain);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070067
Vince Lehman26b215c2014-08-17 15:00:41 -050068 ~RibManager();
69
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070070 void
71 registerWithNfd();
72
73 void
74 enableLocalControlHeader();
75
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070076 void
77 setConfigFile(ConfigFile& configFile);
78
Vince Lehman76c751c2014-11-18 17:36:38 -060079 void
80 onRibUpdateSuccess(const RibUpdate& update);
Vince Lehman4387e782014-06-19 16:57:45 -050081
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070082 void
Vince Lehman76c751c2014-11-18 17:36:38 -060083 onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error);
84
Yanbiao Licf0db022016-01-29 00:54:25 -080085private: // initialization helpers
Vince Lehman76c751c2014-11-18 17:36:38 -060086 void
Yanbiao Licf0db022016-01-29 00:54:25 -080087 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070088
89 void
Yanbiao Licf0db022016-01-29 00:54:25 -080090 registerTopPrefix(const Name& topPrefix);
91
92private: // ControlCommand and StatusDataset
93 void
94 registerEntry(const Name& topPrefix, const Interest& interest,
95 ControlParameters parameters,
96 const ndn::mgmt::CommandContinuation& done);
Junxiao Shia3295742014-05-16 22:40:10 -070097
98 void
Yanbiao Licf0db022016-01-29 00:54:25 -080099 unregisterEntry(const Name& topPrefix, const Interest& interest,
100 ControlParameters parameters,
101 const ndn::mgmt::CommandContinuation& done);
Yingdi Yue5224e92014-04-29 18:04:02 -0700102
103 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800104 listEntries(const Name& topPrefix, const Interest& interest,
105 ndn::mgmt::StatusDatasetContext& context);
Yingdi Yue5224e92014-04-29 18:04:02 -0700106
107 void
Yanbiao Licf0db022016-01-29 00:54:25 -0800108 setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700109
Yanbiao Licf0db022016-01-29 00:54:25 -0800110private: // command validation
111 /**
112 * @brief validate a request for ControlCommand.
113 *
114 * This is called by the dispatcher.
115 *
116 * @pre params != null
117 * @pre typeid(*params) == typeid(ndn::nfd::ControlParameters)
118 *
119 * @param prefix the top prefix
120 * @param interest a request for ControlCommand
121 * @param params the parameters for ControlCommand
122 * @param accept callback of successful validation, take the requester string as a argument
123 * @param reject callback of failure in validation, take the action code as a argument
124 *
125 * use m_localhostValidator / m_localhopValidator to validate commands according to @p prefix.
126 */
127 virtual void
128 authorize(const Name& prefix, const Interest& interest,
129 const ndn::mgmt::ControlParameters* params,
130 ndn::mgmt::AcceptContinuation accept,
131 ndn::mgmt::RejectContinuation reject) override;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700132
Yanbiao Licf0db022016-01-29 00:54:25 -0800133private: // Face monitor
Vince Lehman26b215c2014-08-17 15:00:41 -0500134 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500135 fetchActiveFaces();
136
137 void
138 fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer);
139
140 void
Vince Lehmancd613c52014-07-30 14:34:49 -0500141 onFetchFaceStatusTimeout();
142
Yanbiao Licf0db022016-01-29 00:54:25 -0800143 void
144 onFaceDestroyedEvent(uint64_t faceId);
145
Vince Lehman281ded72014-08-21 12:17:08 -0500146PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yanbiao Licf0db022016-01-29 00:54:25 -0800147 void
148 scheduleActiveFaceFetch(const time::seconds& timeToWait);
149
150 /**
151 * @brief remove invalid faces
152 *
153 * @param buffer Face dataset contents
Vince Lehman26b215c2014-08-17 15:00:41 -0500154 */
155 void
156 removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer);
157
Yanbiao Licf0db022016-01-29 00:54:25 -0800158 /**
159 * @brief response to face events
160 *
161 * @param notification
162 */
163 void
164 onNotification(const FaceEventNotification& notification);
165
166private:
167 void
168 onNrdCommandPrefixAddNextHopSuccess(const Name& prefix,
169 const ndn::nfd::ControlParameters& result);
170
171 void
172 onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
173
174 void
175 onControlHeaderSuccess();
176
177 void
178 onControlHeaderError(uint32_t code, const std::string& reason);
Vince Lehman281ded72014-08-21 12:17:08 -0500179
180private:
Vince Lehman72446ec2014-07-09 10:50:02 -0500181 ndn::Face& m_face;
Vince Lehmanc1dfdb42015-07-16 12:17:36 -0500182 ndn::KeyChain& m_keyChain;
Alexander Afanasyevb3893c92014-05-15 01:49:54 -0700183 ndn::nfd::Controller m_nfdController;
Yanbiao Licf0db022016-01-29 00:54:25 -0800184 ndn::nfd::FaceMonitor m_faceMonitor;
Yingdi Yue5224e92014-04-29 18:04:02 -0700185 ndn::ValidatorConfig m_localhostValidator;
186 ndn::ValidatorConfig m_localhopValidator;
Yingdi Yue5224e92014-04-29 18:04:02 -0700187 bool m_isLocalhopEnabled;
Yanbiao Lid7c96362015-01-30 23:58:24 -0800188 AutoPrefixPropagator m_prefixPropagator;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700189
Vince Lehman76c751c2014-11-18 17:36:38 -0600190PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yanbiao Licf0db022016-01-29 00:54:25 -0800191 Rib m_rib;
Vince Lehman76c751c2014-11-18 17:36:38 -0600192 FibUpdater m_fibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -0500193
Vince Lehman76c751c2014-11-18 17:36:38 -0600194private:
Yanbiao Licf0db022016-01-29 00:54:25 -0800195 static const Name LOCAL_HOST_TOP_PREFIX;
196 static const Name LOCAL_HOP_TOP_PREFIX;
Junxiao Shifde3f542016-07-10 19:54:53 +0000197 static const std::string MGMT_MODULE_NAME;
Vince Lehmancd613c52014-07-30 14:34:49 -0500198 static const Name FACES_LIST_DATASET_PREFIX;
Vince Lehman26b215c2014-08-17 15:00:41 -0500199 static const time::seconds ACTIVE_FACE_FETCH_INTERVAL;
Junxiao Shi1e46be32015-01-08 20:18:05 -0700200 scheduler::EventId m_activeFaceFetchEvent;
Vince Lehman26b215c2014-08-17 15:00:41 -0500201
202 typedef std::set<uint64_t> FaceIdSet;
203 /** \brief contains FaceIds with one or more Routes in the RIB
204 */
205 FaceIdSet m_registeredFaces;
Yanbiao Licf0db022016-01-29 00:54:25 -0800206
207 std::function<void(const Name& topPrefix)> m_addTopPrefix;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700208};
209
210} // namespace rib
211} // namespace nfd
212
213#endif // NFD_RIB_RIB_MANAGER_HPP