blob: 6a3994e08dadf6f16602b115d8599b799007bd11 [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"
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,
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
Alexander Afanasyev5959b012014-06-02 19:18:12 +030085 shared_ptr<ProtocolFactory>
86 findFactory(const std::string& protocol);
87
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070088PROTECTED_WITH_TESTS_ELSE_PRIVATE:
89
90 void
91 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
92
93 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060094 createFace(const Interest& request,
95 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070096
97 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060098 destroyFace(const Interest& request,
99 ControlParameters& parameters);
100
101 VIRTUAL_WITH_TESTS bool
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600102 extractLocalControlParameters(const Interest& request,
103 ControlParameters& parameters,
104 ControlCommand& command,
105 shared_ptr<LocalFace>& outFace,
106 LocalControlFeature& outFeature);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600107
108 VIRTUAL_WITH_TESTS void
109 enableLocalControl(const Interest& request,
110 ControlParameters& parambeters);
111
112 VIRTUAL_WITH_TESTS void
113 disableLocalControl(const Interest& request,
114 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700115
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600116 void
117 ignoreUnsignedVerb(const Interest& request);
118
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700119 void
Alexander Afanasyevbbe3f0c2014-03-23 11:44:01 -0700120 addCreatedFaceToForwarder(const shared_ptr<Face>& newFace);
121
122 void
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700123 onCreated(const Name& requestName,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600124 ControlParameters& parameters,
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700125 const shared_ptr<Face>& newFace);
126
127 void
128 onConnectFailed(const Name& requestName, const std::string& reason);
129
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600130 void
131 onAddFace(shared_ptr<Face> face);
132
133 void
134 onRemoveFace(shared_ptr<Face> face);
135
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700136private:
137 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600138 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700139
140 void
141 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
142
143 void
144 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
145
146 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600147 processSectionUdp(const ConfigSection& configSection,
148 bool isDryRun,
149 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700150
151 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600152 processSectionEther(const ConfigSection& configSection,
153 bool isDryRun,
154 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700155
Wentao Shang53df1632014-04-21 12:01:32 -0700156 void
157 processSectionWebSocket(const ConfigSection& configSection, bool isDryRun);
158
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700159 /** \brief parse a config option that can be either "yes" or "no"
160 * \throw ConfigFile::Error value is neither "yes" nor "no"
161 * \return true if "yes", false if "no"
162 */
163 bool
164 parseYesNo(const ConfigSection::const_iterator& i,
165 const std::string& optionName,
166 const std::string& sectionName);
167
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600168PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700169 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600170
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700171 FactoryMap m_factories;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600172
173private:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700174 FaceTable& m_faceTable;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600175 FaceStatusPublisher m_faceStatusPublisher;
176 ChannelStatusPublisher m_channelStatusPublisher;
Junxiao Shi15b12e72014-08-09 19:56:24 -0700177 NotificationStream<AppFace> m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700178
179 typedef function<void(FaceManager*,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600180 const Interest&,
181 ControlParameters&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700182
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600183 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
184 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700185
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600186 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700187
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600188 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
189 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700190
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700191
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600192 const SignedVerbDispatchTable m_signedVerbDispatch;
193 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
194
195 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
196
197 // number of components in an invalid signed command (i.e. should be signed, but isn't)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600198 // (/localhost/nfd/faces + verb + parameters) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700199 static const size_t COMMAND_UNSIGNED_NCOMPS;
200
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600201 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700202 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
203 static const size_t COMMAND_SIGNED_NCOMPS;
204
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600205 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
206 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
207
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600208 static const Name FACES_LIST_DATASET_PREFIX;
209 static const size_t FACES_LIST_DATASET_NCOMPS;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600210
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600211 static const Name CHANNELS_LIST_DATASET_PREFIX;
212 static const size_t CHANNELS_LIST_DATASET_NCOMPS;
213
214 static const Name FACE_EVENTS_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700215};
216
217inline bool
218FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
219 const std::string& optionName,
220 const std::string& sectionName)
221{
222 const std::string value = i->second.get_value<std::string>();
223 if (value == "yes")
224 {
225 return true;
226 }
227 else if (value == "no")
228 {
229 return false;
230 }
231
232 throw ConfigFile::Error("Invalid value for option \"" +
233 optionName + "\" in \"" +
234 sectionName + "\" section");
235
236}
237
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600238inline void
239FaceManager::ignoreUnsignedVerb(const Interest& request)
240{
241 // do nothing
242}
243
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700244} // namespace nfd
245
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700246#endif // NFD_DAEMON_MGMT_FACE_MANAGER_HPP