blob: bb06f2b244f14245f804e2c5daec9215fa2ccdec [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 */
63
64 FaceManager(FaceTable& faceTable,
Vince Lehman5144f822014-07-23 15:12:56 -070065 shared_ptr<InternalFace> face,
66 ndn::KeyChain& keyChain);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070067
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060068 virtual
69 ~FaceManager();
70
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070071 /** \brief Subscribe to a face management section(s) for the config file
72 */
73 void
74 setConfigFile(ConfigFile& configFile);
75
76 void
77 onFaceRequest(const Interest& request);
78
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060079PUBLIC_WITH_TESTS_ELSE_PRIVATE:
80 void
81 listFaces(const Interest& request);
82
Steve DiBenedettoef04f272014-06-04 14:28:31 -060083 void
84 listChannels(const Interest& request);
85
Chengyu Fan320d2332014-10-29 16:40:33 -060086 void
87 listQueriedFaces(const Interest& request);
88
Alexander Afanasyev5959b012014-06-02 19:18:12 +030089 shared_ptr<ProtocolFactory>
90 findFactory(const std::string& protocol);
91
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070092PROTECTED_WITH_TESTS_ELSE_PRIVATE:
93
94 void
95 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
96
97 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060098 createFace(const Interest& request,
99 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700100
101 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600102 destroyFace(const Interest& request,
103 ControlParameters& parameters);
104
105 VIRTUAL_WITH_TESTS bool
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600106 extractLocalControlParameters(const Interest& request,
107 ControlParameters& parameters,
108 ControlCommand& command,
109 shared_ptr<LocalFace>& outFace,
110 LocalControlFeature& outFeature);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600111
112 VIRTUAL_WITH_TESTS void
113 enableLocalControl(const Interest& request,
114 ControlParameters& parambeters);
115
116 VIRTUAL_WITH_TESTS void
117 disableLocalControl(const Interest& request,
118 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700119
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600120 void
121 ignoreUnsignedVerb(const Interest& request);
122
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700123 void
Alexander Afanasyevbbe3f0c2014-03-23 11:44:01 -0700124 addCreatedFaceToForwarder(const shared_ptr<Face>& newFace);
125
126 void
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700127 onCreated(const Name& requestName,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600128 ControlParameters& parameters,
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700129 const shared_ptr<Face>& newFace);
130
131 void
132 onConnectFailed(const Name& requestName, const std::string& reason);
133
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600134 void
135 onAddFace(shared_ptr<Face> face);
136
137 void
138 onRemoveFace(shared_ptr<Face> face);
139
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700140private:
141 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600142 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700143
144 void
145 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
146
147 void
148 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
149
150 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600151 processSectionUdp(const ConfigSection& configSection,
152 bool isDryRun,
153 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700154
155 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600156 processSectionEther(const ConfigSection& configSection,
157 bool isDryRun,
158 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700159
Wentao Shang53df1632014-04-21 12:01:32 -0700160 void
161 processSectionWebSocket(const ConfigSection& configSection, bool isDryRun);
162
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700163 /** \brief parse a config option that can be either "yes" or "no"
164 * \throw ConfigFile::Error value is neither "yes" nor "no"
165 * \return true if "yes", false if "no"
166 */
167 bool
168 parseYesNo(const ConfigSection::const_iterator& i,
169 const std::string& optionName,
170 const std::string& sectionName);
171
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600172PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700173 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600174
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700175 FactoryMap m_factories;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600176
177private:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700178 FaceTable& m_faceTable;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600179 FaceStatusPublisher m_faceStatusPublisher;
180 ChannelStatusPublisher m_channelStatusPublisher;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700181 NotificationStream<AppFace> m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700182
183 typedef function<void(FaceManager*,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600184 const Interest&,
185 ControlParameters&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700186
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600187 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
188 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700189
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600190 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700191
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600192 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
193 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700194
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700195
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600196 const SignedVerbDispatchTable m_signedVerbDispatch;
197 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
198
199 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
200
201 // number of components in an invalid signed command (i.e. should be signed, but isn't)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600202 // (/localhost/nfd/faces + verb + parameters) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700203 static const size_t COMMAND_UNSIGNED_NCOMPS;
204
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600205 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700206 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
207 static const size_t COMMAND_SIGNED_NCOMPS;
208
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600209 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
210 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
211
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600212 static const Name FACES_LIST_DATASET_PREFIX;
213 static const size_t FACES_LIST_DATASET_NCOMPS;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600214
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600215 static const Name CHANNELS_LIST_DATASET_PREFIX;
216 static const size_t CHANNELS_LIST_DATASET_NCOMPS;
217
Chengyu Fan320d2332014-10-29 16:40:33 -0600218 static const Name FACES_QUERY_DATASET_PREFIX;
219 static const size_t FACES_QUERY_DATASET_NCOMPS;
220
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600221 static const Name FACE_EVENTS_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700222};
223
224inline bool
225FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
226 const std::string& optionName,
227 const std::string& sectionName)
228{
229 const std::string value = i->second.get_value<std::string>();
230 if (value == "yes")
231 {
232 return true;
233 }
234 else if (value == "no")
235 {
236 return false;
237 }
238
239 throw ConfigFile::Error("Invalid value for option \"" +
240 optionName + "\" in \"" +
241 sectionName + "\" section");
242
243}
244
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600245inline void
246FaceManager::ignoreUnsignedVerb(const Interest& request)
247{
248 // do nothing
249}
250
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700251} // namespace nfd
252
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700253#endif // NFD_DAEMON_MGMT_FACE_MANAGER_HPP