blob: 75477bfd3f499bf40c2fdec790e89fc37de18d21 [file] [log] [blame]
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedettoef04f272014-06-04 14:28:31 -06003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -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/>.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_MGMT_FACE_MANAGER_HPP
27#define NFD_DAEMON_MGMT_FACE_MANAGER_HPP
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070028
29#include "common.hpp"
Junxiao Shi15b12e72014-08-09 19:56:24 -070030#include "core/notification-stream.hpp"
Steve DiBenedetto7564d972014-03-24 14:28:46 -060031#include "face/local-face.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070032#include "mgmt/manager-base.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060033#include "mgmt/face-status-publisher.hpp"
Steve DiBenedettoef04f272014-06-04 14:28:31 -060034#include "mgmt/channel-status-publisher.hpp"
Chengyu Fan320d2332014-10-29 16:40:33 -060035#include "mgmt/face-query-status-publisher.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070036
Alexander Afanasyev4a771362014-04-24 21:29:33 -070037#include <ndn-cxx/management/nfd-control-parameters.hpp>
38#include <ndn-cxx/management/nfd-control-response.hpp>
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070039
40namespace nfd {
41
42const std::string FACE_MANAGER_PRIVILEGE = "faces";
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060043
Davide Pesavento52a18f92014-04-10 00:55:01 +020044class ConfigFile;
45class Face;
46class FaceTable;
Steve DiBenedetto7564d972014-03-24 14:28:46 -060047class LocalFace;
Davide Pesavento52a18f92014-04-10 00:55:01 +020048class NetworkInterfaceInfo;
49class ProtocolFactory;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070050
51class FaceManager : public ManagerBase
52{
53public:
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060054 class Error : public ManagerBase::Error
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070055 {
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060056 public:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070057 Error(const std::string& what) : ManagerBase::Error(what) {}
58 };
59
60 /**
61 * \throws FaceManager::Error if localPort is an invalid port number
62 */
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070063 FaceManager(FaceTable& faceTable,
Vince Lehman5144f822014-07-23 15:12:56 -070064 shared_ptr<InternalFace> face,
65 ndn::KeyChain& keyChain);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070066
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060067 virtual
68 ~FaceManager();
69
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070070 /** \brief Subscribe to a face management section(s) for the config file
71 */
72 void
73 setConfigFile(ConfigFile& configFile);
74
75 void
76 onFaceRequest(const Interest& request);
77
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060078PUBLIC_WITH_TESTS_ELSE_PRIVATE:
79 void
80 listFaces(const Interest& request);
81
Steve DiBenedettoef04f272014-06-04 14:28:31 -060082 void
83 listChannels(const Interest& request);
84
Chengyu Fan320d2332014-10-29 16:40:33 -060085 void
86 listQueriedFaces(const Interest& request);
87
Alexander Afanasyev5959b012014-06-02 19:18:12 +030088 shared_ptr<ProtocolFactory>
89 findFactory(const std::string& protocol);
90
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070091PROTECTED_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070092 void
93 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
94
95 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060096 createFace(const Interest& request,
97 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070098
99 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600100 destroyFace(const Interest& request,
101 ControlParameters& parameters);
102
103 VIRTUAL_WITH_TESTS bool
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600104 extractLocalControlParameters(const Interest& request,
105 ControlParameters& parameters,
106 ControlCommand& command,
107 shared_ptr<LocalFace>& outFace,
108 LocalControlFeature& outFeature);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600109
110 VIRTUAL_WITH_TESTS void
111 enableLocalControl(const Interest& request,
112 ControlParameters& parambeters);
113
114 VIRTUAL_WITH_TESTS void
115 disableLocalControl(const Interest& request,
116 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700117
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600118 void
119 ignoreUnsignedVerb(const Interest& request);
120
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700121 void
Alexander Afanasyevbbe3f0c2014-03-23 11:44:01 -0700122 addCreatedFaceToForwarder(const shared_ptr<Face>& newFace);
123
124 void
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700125 onCreated(const Name& requestName,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600126 ControlParameters& parameters,
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700127 const shared_ptr<Face>& newFace);
128
129 void
130 onConnectFailed(const Name& requestName, const std::string& reason);
131
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600132 void
133 onAddFace(shared_ptr<Face> face);
134
135 void
136 onRemoveFace(shared_ptr<Face> face);
137
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700138private:
139 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600140 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700141
142 void
143 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
144
145 void
146 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
147
148 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600149 processSectionUdp(const ConfigSection& configSection,
150 bool isDryRun,
Davide Pesaventob499a602014-11-18 22:36:56 +0100151 const std::vector<NetworkInterfaceInfo>& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700152
153 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600154 processSectionEther(const ConfigSection& configSection,
155 bool isDryRun,
Davide Pesaventob499a602014-11-18 22:36:56 +0100156 const std::vector<NetworkInterfaceInfo>& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700157
Wentao Shang53df1632014-04-21 12:01:32 -0700158 void
159 processSectionWebSocket(const ConfigSection& configSection, bool isDryRun);
160
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700161 /** \brief parse a config option that can be either "yes" or "no"
162 * \throw ConfigFile::Error value is neither "yes" nor "no"
163 * \return true if "yes", false if "no"
164 */
165 bool
166 parseYesNo(const ConfigSection::const_iterator& i,
167 const std::string& optionName,
168 const std::string& sectionName);
169
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600170PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventob499a602014-11-18 22:36:56 +0100171 typedef std::map<std::string/*protocol*/, shared_ptr<ProtocolFactory>> FactoryMap;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700172 FactoryMap m_factories;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600173
174private:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700175 FaceTable& m_faceTable;
Junxiao Shi1e064172014-12-14 19:37:46 -0700176 signal::ScopedConnection m_faceAddConn;
177 signal::ScopedConnection m_faceRemoveConn;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600178 FaceStatusPublisher m_faceStatusPublisher;
179 ChannelStatusPublisher m_channelStatusPublisher;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700180 NotificationStream<AppFace> m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700181
182 typedef function<void(FaceManager*,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600183 const Interest&,
184 ControlParameters&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700185
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600186 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
187 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700188
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600189 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700190
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600191 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
192 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700193
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600194 const SignedVerbDispatchTable m_signedVerbDispatch;
195 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
196
197 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
198
199 // number of components in an invalid signed command (i.e. should be signed, but isn't)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600200 // (/localhost/nfd/faces + verb + parameters) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700201 static const size_t COMMAND_UNSIGNED_NCOMPS;
202
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600203 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700204 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
205 static const size_t COMMAND_SIGNED_NCOMPS;
206
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600207 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
208 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
209
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600210 static const Name FACES_LIST_DATASET_PREFIX;
211 static const size_t FACES_LIST_DATASET_NCOMPS;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600212
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600213 static const Name CHANNELS_LIST_DATASET_PREFIX;
214 static const size_t CHANNELS_LIST_DATASET_NCOMPS;
215
Chengyu Fan320d2332014-10-29 16:40:33 -0600216 static const Name FACES_QUERY_DATASET_PREFIX;
217 static const size_t FACES_QUERY_DATASET_NCOMPS;
218
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600219 static const Name FACE_EVENTS_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700220};
221
222inline bool
223FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
224 const std::string& optionName,
225 const std::string& sectionName)
226{
227 const std::string value = i->second.get_value<std::string>();
228 if (value == "yes")
229 {
230 return true;
231 }
232 else if (value == "no")
233 {
234 return false;
235 }
236
237 throw ConfigFile::Error("Invalid value for option \"" +
238 optionName + "\" in \"" +
239 sectionName + "\" section");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700240}
241
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600242inline void
243FaceManager::ignoreUnsignedVerb(const Interest& request)
244{
245 // do nothing
246}
247
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700248} // namespace nfd
249
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700250#endif // NFD_DAEMON_MGMT_FACE_MANAGER_HPP