rib: move RibManager to mgmt
Refs: #4528
Change-Id: Idff4ce8fe28b48163794cd12f0d185f8ca4233e3
diff --git a/daemon/mgmt/rib-manager.hpp b/daemon/mgmt/rib-manager.hpp
new file mode 100644
index 0000000..876ed75
--- /dev/null
+++ b/daemon/mgmt/rib-manager.hpp
@@ -0,0 +1,267 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_MGMT_RIB_MANAGER_HPP
+#define NFD_DAEMON_MGMT_RIB_MANAGER_HPP
+
+#include "core/config-file.hpp"
+#include "core/manager-base.hpp"
+#include "rib/rib.hpp"
+
+#include <ndn-cxx/mgmt/nfd/controller.hpp>
+#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
+#include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
+#include <ndn-cxx/security/validator-config.hpp>
+#include <ndn-cxx/util/scheduler.hpp>
+
+namespace nfd {
+
+/**
+ * @brief Serve commands and datasets of NFD RIB management protocol.
+ * @sa https://redmine.named-data.net/projects/nfd/wiki/RibMgmt
+ */
+class RibManager : public ManagerBase
+{
+public:
+ class Error : public std::runtime_error
+ {
+ public:
+ using std::runtime_error::runtime_error;
+ };
+
+ RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
+ ndn::nfd::Controller& nfdController, Dispatcher& dispatcher,
+ ndn::util::Scheduler& scheduler);
+
+ /**
+ * @brief Apply localhost_security configuration.
+ */
+ void
+ applyLocalhostConfig(const ConfigSection& section, const std::string& filename);
+
+ /**
+ * @brief Apply localhop_security configuration and allow accepting commands on
+ * /localhop/nfd/rib prefix.
+ */
+ void
+ enableLocalhop(const ConfigSection& section, const std::string& filename);
+
+ /**
+ * @brief Disallow accepting commands on /localhop/nfd/rib prefix.
+ */
+ void
+ disableLocalhop();
+
+ /**
+ * @brief Start accepting commands and dataset requests.
+ */
+ void
+ registerWithNfd();
+
+ /**
+ * @brief Enable NDNLP IncomingFaceId field in order to support self-registration commands.
+ */
+ void
+ enableLocalFields();
+
+public: // self-learning support
+ enum class SlAnnounceResult {
+ OK, ///< RIB and FIB have been updated
+ ERROR, ///< unspecified error
+ VALIDATION_FAILURE, ///< the announcement cannot be verified against the trust schema
+ EXPIRED, ///< the announcement has expired
+ NOT_FOUND, ///< route does not exist (slRenew only)
+ };
+
+ using SlAnnounceCallback = std::function<void(SlAnnounceResult res)>;
+ using SlFindAnnCallback = std::function<void(optional<ndn::PrefixAnnouncement>)>;
+
+ /** \brief Insert a route by prefix announcement from self-learning strategy.
+ * \param pa A prefix announcement. It must contain the Data.
+ * \param faceId Face on which the announcement arrives.
+ * \param maxLifetime Maximum route lifetime as imposed by self-learning strategy.
+ * \param cb Callback to receive the operation result.
+ *
+ * If \p pa passes validation and is unexpired, inserts or replaces a route for the announced
+ * name and faceId whose lifetime is set to the earlier of now+maxLifetime or prefix
+ * announcement expiration time, updates FIB, and invokes \p cb with SlAnnounceResult::OK.
+ * In case \p pa expires when validation completes, invokes \p cb with SlAnnounceResult::EXPIRED.
+ * If \p pa cannot be verified by the trust schema given in rib.localhop_security config key,
+ * or the relevant config has not been loaded via \c enableLocalHop, invokes \p cb with
+ * SlAnnounceResult::VALIDATION_FAILURE.
+ *
+ * Self-learning strategy invokes this method after receiving a Data carrying a prefix
+ * announcement.
+ */
+ void
+ slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId, time::milliseconds maxLifetime,
+ const SlAnnounceCallback& cb);
+
+ /** \brief Renew a route created by prefix announcement from self-learning strategy.
+ * \param name Data name, for finding RIB entry by longest-prefix-match.
+ * \param faceId Nexthop face.
+ * \param maxLifetime Maximum route lifetime as imposed by self-learning strategy.
+ * \param cb Callback to receive the operation result.
+ *
+ * If the specified route exists, prolongs its lifetime to the earlier of now+maxLifetime or
+ * prefix announcement expiration time, and invokes \p cb with SlAnnounceResult::OK.
+ * If the prefix announcement has expired, invokes \p cb with SlAnnounceResult::EXPIRED.
+ * If the route is not found, invokes \p cb with SlAnnounceResult::NOT_FOUND.
+ *
+ * Self-learning strategy invokes this method after an Interest forwarded via a learned route
+ * is satisfied.
+ *
+ * \bug In current implementation, if an slAnnounce operation is in progress to create a Route
+ * or replace a prefix announcement, slRenew could fail because Route does not exist in
+ * existing RIB, or overwrite the new prefix announcement with an old one.
+ */
+ void
+ slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
+ const SlAnnounceCallback& cb);
+
+ /** \brief Retrieve an outgoing prefix announcement for self-learning strategy.
+ * \param name Data name.
+ * \param cb Callback to receive a prefix announcement that announces a prefix of \p name, or
+ * nullopt if no RIB entry is found by longest-prefix-match of \p name.
+ *
+ * Self-learning strategy invokes this method before sending a Data in reply to a discovery
+ * Interest, so as to attach a prefix announcement onto that Data.
+ *
+ * \bug In current implementation, if an slAnnounce operation is in progress, slFindAnn does not
+ * wait for that operation to complete and its result reflects the prior RIB state.
+ */
+ void
+ slFindAnn(const Name& name, const SlFindAnnCallback& cb) const;
+
+private: // RIB and FibUpdater actions
+ enum class RibUpdateResult
+ {
+ OK,
+ ERROR,
+ EXPIRED,
+ };
+
+ static SlAnnounceResult
+ getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r);
+
+ /** \brief Start adding a route to RIB and FIB.
+ * \param name route name
+ * \param route route parameters; may contain absolute expiration time
+ * \param expires relative expiration time; if specified, overwrites \c route.expires
+ * \param done completion callback
+ */
+ void
+ beginAddRoute(const Name& name, rib::Route route, optional<time::nanoseconds> expires,
+ const std::function<void(RibUpdateResult)>& done);
+
+ /** \brief Start removing a route from RIB and FIB.
+ * \param name route name
+ * \param route route parameters
+ * \param done completion callback
+ */
+ void
+ beginRemoveRoute(const Name& name, const rib::Route& route,
+ const std::function<void(RibUpdateResult)>& done);
+
+ void
+ beginRibUpdate(const rib::RibUpdate& update,
+ const std::function<void(RibUpdateResult)>& done);
+
+private: // management Dispatcher related
+ void
+ registerTopPrefix(const Name& topPrefix);
+
+ /** \brief Serve rib/register command.
+ */
+ void
+ registerEntry(const Name& topPrefix, const Interest& interest,
+ ControlParameters parameters,
+ const ndn::mgmt::CommandContinuation& done);
+
+ /** \brief Serve rib/unregister command.
+ */
+ void
+ unregisterEntry(const Name& topPrefix, const Interest& interest,
+ ControlParameters parameters,
+ const ndn::mgmt::CommandContinuation& done);
+
+ /** \brief Serve rib/list dataset.
+ */
+ void
+ listEntries(const Name& topPrefix, const Interest& interest,
+ ndn::mgmt::StatusDatasetContext& context);
+
+ void
+ setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters);
+
+ ndn::mgmt::Authorization
+ makeAuthorization(const std::string& verb) override;
+
+private: // Face monitor
+ void
+ fetchActiveFaces();
+
+ void
+ onFetchActiveFacesFailure(uint32_t code, const std::string& reason);
+
+ void
+ onFaceDestroyedEvent(uint64_t faceId);
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+ void
+ scheduleActiveFaceFetch(const time::seconds& timeToWait);
+
+ void
+ removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces);
+
+ void
+ onNotification(const ndn::nfd::FaceEventNotification& notification);
+
+public:
+ static const Name LOCALHOP_TOP_PREFIX;
+
+private:
+ rib::Rib& m_rib;
+ ndn::KeyChain& m_keyChain;
+ ndn::nfd::Controller& m_nfdController;
+ Dispatcher& m_dispatcher;
+ ndn::util::Scheduler& m_scheduler;
+
+ ndn::nfd::FaceMonitor m_faceMonitor;
+ ndn::ValidatorConfig m_localhostValidator;
+ ndn::ValidatorConfig m_localhopValidator;
+ bool m_isLocalhopEnabled;
+
+ ndn::util::scheduler::ScopedEventId m_activeFaceFetchEvent;
+ using FaceIdSet = std::set<uint64_t>;
+ FaceIdSet m_registeredFaces; ///< contains FaceIds with one or more Routes in the RIB
+};
+
+std::ostream&
+operator<<(std::ostream& os, RibManager::SlAnnounceResult res);
+
+} // namespace nfd
+
+#endif // NFD_DAEMON_MGMT_RIB_MANAGER_HPP