blob: 0a5579e884dfb176ca407b737a51114cfc6fa5d0 [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"
Steve DiBenedetto7564d972014-03-24 14:28:46 -060030#include "face/local-face.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070031#include "mgmt/manager-base.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060032#include "mgmt/face-status-publisher.hpp"
Steve DiBenedettoef04f272014-06-04 14:28:31 -060033#include "mgmt/channel-status-publisher.hpp"
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060034#include "mgmt/notification-stream.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070035
Alexander Afanasyev4a771362014-04-24 21:29:33 -070036#include <ndn-cxx/management/nfd-control-parameters.hpp>
37#include <ndn-cxx/management/nfd-control-response.hpp>
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070038
39namespace nfd {
40
41const std::string FACE_MANAGER_PRIVILEGE = "faces";
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060042
Davide Pesavento52a18f92014-04-10 00:55:01 +020043class ConfigFile;
44class Face;
45class FaceTable;
Steve DiBenedetto7564d972014-03-24 14:28:46 -060046class LocalFace;
Davide Pesavento52a18f92014-04-10 00:55:01 +020047class NetworkInterfaceInfo;
48class ProtocolFactory;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070049
50class FaceManager : public ManagerBase
51{
52public:
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060053 class Error : public ManagerBase::Error
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070054 {
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060055 public:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070056 Error(const std::string& what) : ManagerBase::Error(what) {}
57 };
58
59 /**
60 * \throws FaceManager::Error if localPort is an invalid port number
61 */
62
63 FaceManager(FaceTable& faceTable,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070064 shared_ptr<InternalFace> face);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070065
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060066 virtual
67 ~FaceManager();
68
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070069 /** \brief Subscribe to a face management section(s) for the config file
70 */
71 void
72 setConfigFile(ConfigFile& configFile);
73
74 void
75 onFaceRequest(const Interest& request);
76
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060077PUBLIC_WITH_TESTS_ELSE_PRIVATE:
78 void
79 listFaces(const Interest& request);
80
Steve DiBenedettoef04f272014-06-04 14:28:31 -060081 void
82 listChannels(const Interest& request);
83
Alexander Afanasyev5959b012014-06-02 19:18:12 +030084 shared_ptr<ProtocolFactory>
85 findFactory(const std::string& protocol);
86
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070087PROTECTED_WITH_TESTS_ELSE_PRIVATE:
88
89 void
90 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
91
92 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060093 createFace(const Interest& request,
94 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070095
96 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060097 destroyFace(const Interest& request,
98 ControlParameters& parameters);
99
100 VIRTUAL_WITH_TESTS bool
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600101 extractLocalControlParameters(const Interest& request,
102 ControlParameters& parameters,
103 ControlCommand& command,
104 shared_ptr<LocalFace>& outFace,
105 LocalControlFeature& outFeature);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600106
107 VIRTUAL_WITH_TESTS void
108 enableLocalControl(const Interest& request,
109 ControlParameters& parambeters);
110
111 VIRTUAL_WITH_TESTS void
112 disableLocalControl(const Interest& request,
113 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700114
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600115 void
116 ignoreUnsignedVerb(const Interest& request);
117
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700118 void
Alexander Afanasyevbbe3f0c2014-03-23 11:44:01 -0700119 addCreatedFaceToForwarder(const shared_ptr<Face>& newFace);
120
121 void
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700122 onCreated(const Name& requestName,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600123 ControlParameters& parameters,
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700124 const shared_ptr<Face>& newFace);
125
126 void
127 onConnectFailed(const Name& requestName, const std::string& reason);
128
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600129 void
130 onAddFace(shared_ptr<Face> face);
131
132 void
133 onRemoveFace(shared_ptr<Face> face);
134
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700135private:
136 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600137 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700138
139 void
140 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
141
142 void
143 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
144
145 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600146 processSectionUdp(const ConfigSection& configSection,
147 bool isDryRun,
148 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700149
150 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600151 processSectionEther(const ConfigSection& configSection,
152 bool isDryRun,
153 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700154
Wentao Shang53df1632014-04-21 12:01:32 -0700155 void
156 processSectionWebSocket(const ConfigSection& configSection, bool isDryRun);
157
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700158 /** \brief parse a config option that can be either "yes" or "no"
159 * \throw ConfigFile::Error value is neither "yes" nor "no"
160 * \return true if "yes", false if "no"
161 */
162 bool
163 parseYesNo(const ConfigSection::const_iterator& i,
164 const std::string& optionName,
165 const std::string& sectionName);
166
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600167PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700168 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600169
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700170 FactoryMap m_factories;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600171
172private:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700173 FaceTable& m_faceTable;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600174 FaceStatusPublisher m_faceStatusPublisher;
175 ChannelStatusPublisher m_channelStatusPublisher;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600176 NotificationStream m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700177
178 typedef function<void(FaceManager*,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600179 const Interest&,
180 ControlParameters&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700181
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600182 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
183 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700184
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600185 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700186
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600187 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
188 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700189
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700190
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600191 const SignedVerbDispatchTable m_signedVerbDispatch;
192 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
193
194 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
195
196 // number of components in an invalid signed command (i.e. should be signed, but isn't)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600197 // (/localhost/nfd/faces + verb + parameters) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700198 static const size_t COMMAND_UNSIGNED_NCOMPS;
199
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600200 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700201 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
202 static const size_t COMMAND_SIGNED_NCOMPS;
203
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600204 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
205 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
206
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600207 static const Name FACES_LIST_DATASET_PREFIX;
208 static const size_t FACES_LIST_DATASET_NCOMPS;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600209
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600210 static const Name CHANNELS_LIST_DATASET_PREFIX;
211 static const size_t CHANNELS_LIST_DATASET_NCOMPS;
212
213 static const Name FACE_EVENTS_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700214};
215
216inline bool
217FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
218 const std::string& optionName,
219 const std::string& sectionName)
220{
221 const std::string value = i->second.get_value<std::string>();
222 if (value == "yes")
223 {
224 return true;
225 }
226 else if (value == "no")
227 {
228 return false;
229 }
230
231 throw ConfigFile::Error("Invalid value for option \"" +
232 optionName + "\" in \"" +
233 sectionName + "\" section");
234
235}
236
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600237inline void
238FaceManager::ignoreUnsignedVerb(const Interest& request)
239{
240 // do nothing
241}
242
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700243} // namespace nfd
244
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700245#endif // NFD_DAEMON_MGMT_FACE_MANAGER_HPP