blob: 104097079666f5beedfc684eb2d033b01b82b3f5 [file] [log] [blame]
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070024
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070025#ifndef NFD_DAEMON_MGMT_FACE_MANAGER_HPP
26#define NFD_DAEMON_MGMT_FACE_MANAGER_HPP
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070027
28#include "common.hpp"
Steve DiBenedetto7564d972014-03-24 14:28:46 -060029#include "face/local-face.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070030#include "mgmt/manager-base.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060031#include "mgmt/face-status-publisher.hpp"
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060032#include "mgmt/notification-stream.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070033
Alexander Afanasyev4a771362014-04-24 21:29:33 -070034#include <ndn-cxx/management/nfd-control-parameters.hpp>
35#include <ndn-cxx/management/nfd-control-response.hpp>
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070036
37namespace nfd {
38
39const std::string FACE_MANAGER_PRIVILEGE = "faces";
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060040
Davide Pesavento52a18f92014-04-10 00:55:01 +020041class ConfigFile;
42class Face;
43class FaceTable;
Steve DiBenedetto7564d972014-03-24 14:28:46 -060044class LocalFace;
Davide Pesavento52a18f92014-04-10 00:55:01 +020045class NetworkInterfaceInfo;
46class ProtocolFactory;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070047
48class FaceManager : public ManagerBase
49{
50public:
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060051 class Error : public ManagerBase::Error
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070052 {
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060053 public:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070054 Error(const std::string& what) : ManagerBase::Error(what) {}
55 };
56
57 /**
58 * \throws FaceManager::Error if localPort is an invalid port number
59 */
60
61 FaceManager(FaceTable& faceTable,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070062 shared_ptr<InternalFace> face);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070063
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060064 virtual
65 ~FaceManager();
66
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070067 /** \brief Subscribe to a face management section(s) for the config file
68 */
69 void
70 setConfigFile(ConfigFile& configFile);
71
72 void
73 onFaceRequest(const Interest& request);
74
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060075PUBLIC_WITH_TESTS_ELSE_PRIVATE:
76 void
77 listFaces(const Interest& request);
78
Alexander Afanasyev5959b012014-06-02 19:18:12 +030079 shared_ptr<ProtocolFactory>
80 findFactory(const std::string& protocol);
81
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070082PROTECTED_WITH_TESTS_ELSE_PRIVATE:
83
84 void
85 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
86
87 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060088 createFace(const Interest& request,
89 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070090
91 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060092 destroyFace(const Interest& request,
93 ControlParameters& parameters);
94
95 VIRTUAL_WITH_TESTS bool
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060096 extractLocalControlParameters(const Interest& request,
97 ControlParameters& parameters,
98 ControlCommand& command,
99 shared_ptr<LocalFace>& outFace,
100 LocalControlFeature& outFeature);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600101
102 VIRTUAL_WITH_TESTS void
103 enableLocalControl(const Interest& request,
104 ControlParameters& parambeters);
105
106 VIRTUAL_WITH_TESTS void
107 disableLocalControl(const Interest& request,
108 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700109
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600110 void
111 ignoreUnsignedVerb(const Interest& request);
112
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700113 void
Alexander Afanasyevbbe3f0c2014-03-23 11:44:01 -0700114 addCreatedFaceToForwarder(const shared_ptr<Face>& newFace);
115
116 void
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700117 onCreated(const Name& requestName,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600118 ControlParameters& parameters,
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700119 const shared_ptr<Face>& newFace);
120
121 void
122 onConnectFailed(const Name& requestName, const std::string& reason);
123
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600124 void
125 onAddFace(shared_ptr<Face> face);
126
127 void
128 onRemoveFace(shared_ptr<Face> face);
129
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700130private:
131 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600132 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700133
134 void
135 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
136
137 void
138 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
139
140 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600141 processSectionUdp(const ConfigSection& configSection,
142 bool isDryRun,
143 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700144
145 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600146 processSectionEther(const ConfigSection& configSection,
147 bool isDryRun,
148 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700149
Wentao Shang53df1632014-04-21 12:01:32 -0700150 void
151 processSectionWebSocket(const ConfigSection& configSection, bool isDryRun);
152
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700153 /** \brief parse a config option that can be either "yes" or "no"
154 * \throw ConfigFile::Error value is neither "yes" nor "no"
155 * \return true if "yes", false if "no"
156 */
157 bool
158 parseYesNo(const ConfigSection::const_iterator& i,
159 const std::string& optionName,
160 const std::string& sectionName);
161
162private:
163 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
164 FactoryMap m_factories;
165 FaceTable& m_faceTable;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600166 FaceStatusPublisher m_statusPublisher;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600167 NotificationStream m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700168
169 typedef function<void(FaceManager*,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600170 const Interest&,
171 ControlParameters&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700172
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600173 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
174 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700175
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600176 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700177
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600178 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
179 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700180
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700181
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600182 const SignedVerbDispatchTable m_signedVerbDispatch;
183 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
184
185 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
186
187 // number of components in an invalid signed command (i.e. should be signed, but isn't)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600188 // (/localhost/nfd/faces + verb + parameters) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700189 static const size_t COMMAND_UNSIGNED_NCOMPS;
190
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600191 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700192 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
193 static const size_t COMMAND_SIGNED_NCOMPS;
194
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600195 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
196 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
197
198 static const Name LIST_COMMAND_PREFIX;
199 static const size_t LIST_COMMAND_NCOMPS;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600200
201 static const Name EVENTS_COMMAND_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700202};
203
204inline bool
205FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
206 const std::string& optionName,
207 const std::string& sectionName)
208{
209 const std::string value = i->second.get_value<std::string>();
210 if (value == "yes")
211 {
212 return true;
213 }
214 else if (value == "no")
215 {
216 return false;
217 }
218
219 throw ConfigFile::Error("Invalid value for option \"" +
220 optionName + "\" in \"" +
221 sectionName + "\" section");
222
223}
224
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600225inline void
226FaceManager::ignoreUnsignedVerb(const Interest& request)
227{
228 // do nothing
229}
230
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700231} // namespace nfd
232
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700233#endif // NFD_DAEMON_MGMT_FACE_MANAGER_HPP