blob: d1ba482b31674b275548de0af9710052af0049de [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
25#ifndef NFD_MGMT_FACE_MANAGER_HPP
26#define NFD_MGMT_FACE_MANAGER_HPP
27
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
Steve DiBenedetto7564d972014-03-24 14:28:46 -060034#include <ndn-cpp-dev/management/nfd-control-parameters.hpp>
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070035#include <ndn-cpp-dev/management/nfd-control-response.hpp>
36
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
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070079PROTECTED_WITH_TESTS_ELSE_PRIVATE:
80
81 void
82 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
83
84 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060085 createFace(const Interest& request,
86 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070087
88 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060089 destroyFace(const Interest& request,
90 ControlParameters& parameters);
91
92 VIRTUAL_WITH_TESTS bool
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060093 extractLocalControlParameters(const Interest& request,
94 ControlParameters& parameters,
95 ControlCommand& command,
96 shared_ptr<LocalFace>& outFace,
97 LocalControlFeature& outFeature);
Steve DiBenedetto7564d972014-03-24 14:28:46 -060098
99 VIRTUAL_WITH_TESTS void
100 enableLocalControl(const Interest& request,
101 ControlParameters& parambeters);
102
103 VIRTUAL_WITH_TESTS void
104 disableLocalControl(const Interest& request,
105 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700106
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600107 void
108 ignoreUnsignedVerb(const Interest& request);
109
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700110 void
Alexander Afanasyevbbe3f0c2014-03-23 11:44:01 -0700111 addCreatedFaceToForwarder(const shared_ptr<Face>& newFace);
112
113 void
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700114 onCreated(const Name& requestName,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600115 ControlParameters& parameters,
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700116 const shared_ptr<Face>& newFace);
117
118 void
119 onConnectFailed(const Name& requestName, const std::string& reason);
120
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600121 void
122 onAddFace(shared_ptr<Face> face);
123
124 void
125 onRemoveFace(shared_ptr<Face> face);
126
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700127private:
128 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600129 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700130
131 void
132 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
133
134 void
135 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
136
137 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600138 processSectionUdp(const ConfigSection& configSection,
139 bool isDryRun,
140 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700141
142 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600143 processSectionEther(const ConfigSection& configSection,
144 bool isDryRun,
145 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700146
147 /** \brief parse a config option that can be either "yes" or "no"
148 * \throw ConfigFile::Error value is neither "yes" nor "no"
149 * \return true if "yes", false if "no"
150 */
151 bool
152 parseYesNo(const ConfigSection::const_iterator& i,
153 const std::string& optionName,
154 const std::string& sectionName);
155
156private:
157 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
158 FactoryMap m_factories;
159 FaceTable& m_faceTable;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600160 FaceStatusPublisher m_statusPublisher;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600161 NotificationStream m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700162
163 typedef function<void(FaceManager*,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600164 const Interest&,
165 ControlParameters&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700166
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600167 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
168 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700169
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600170 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700171
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600172 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
173 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700174
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700175
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600176 const SignedVerbDispatchTable m_signedVerbDispatch;
177 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
178
179 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
180
181 // number of components in an invalid signed command (i.e. should be signed, but isn't)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600182 // (/localhost/nfd/faces + verb + parameters) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700183 static const size_t COMMAND_UNSIGNED_NCOMPS;
184
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600185 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700186 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
187 static const size_t COMMAND_SIGNED_NCOMPS;
188
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600189 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
190 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
191
192 static const Name LIST_COMMAND_PREFIX;
193 static const size_t LIST_COMMAND_NCOMPS;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600194
195 static const Name EVENTS_COMMAND_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700196};
197
198inline bool
199FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
200 const std::string& optionName,
201 const std::string& sectionName)
202{
203 const std::string value = i->second.get_value<std::string>();
204 if (value == "yes")
205 {
206 return true;
207 }
208 else if (value == "no")
209 {
210 return false;
211 }
212
213 throw ConfigFile::Error("Invalid value for option \"" +
214 optionName + "\" in \"" +
215 sectionName + "\" section");
216
217}
218
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600219inline void
220FaceManager::ignoreUnsignedVerb(const Interest& request)
221{
222 // do nothing
223}
224
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700225} // namespace nfd
226
227#endif // NFD_MGMT_FACE_MANAGER_HPP