blob: 7312bba9dbbc546ef8c3152fdd4dce70e28e4ed0 [file] [log] [blame]
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_MGMT_FACE_MANAGER_HPP
8#define NFD_MGMT_FACE_MANAGER_HPP
9
10#include "common.hpp"
11#include "face/face.hpp"
Steve DiBenedetto7564d972014-03-24 14:28:46 -060012#include "face/local-face.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070013#include "mgmt/app-face.hpp"
14#include "mgmt/manager-base.hpp"
15#include "mgmt/config-file.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060016#include "mgmt/face-status-publisher.hpp"
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060017#include "mgmt/notification-stream.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060018#include "fw/face-table.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070019
Steve DiBenedetto7564d972014-03-24 14:28:46 -060020#include <ndn-cpp-dev/management/nfd-control-parameters.hpp>
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070021#include <ndn-cpp-dev/management/nfd-control-response.hpp>
22
23namespace nfd {
24
25const std::string FACE_MANAGER_PRIVILEGE = "faces";
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060026
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070027class ProtocolFactory;
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060028class NetworkInterfaceInfo;
Steve DiBenedetto7564d972014-03-24 14:28:46 -060029class LocalFace;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070030
31class FaceManager : public ManagerBase
32{
33public:
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060034 class Error : public ManagerBase::Error
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070035 {
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060036 public:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070037 Error(const std::string& what) : ManagerBase::Error(what) {}
38 };
39
40 /**
41 * \throws FaceManager::Error if localPort is an invalid port number
42 */
43
44 FaceManager(FaceTable& faceTable,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070045 shared_ptr<InternalFace> face);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070046
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060047 virtual
48 ~FaceManager();
49
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070050 /** \brief Subscribe to a face management section(s) for the config file
51 */
52 void
53 setConfigFile(ConfigFile& configFile);
54
55 void
56 onFaceRequest(const Interest& request);
57
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060058PUBLIC_WITH_TESTS_ELSE_PRIVATE:
59 void
60 listFaces(const Interest& request);
61
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070062PROTECTED_WITH_TESTS_ELSE_PRIVATE:
63
64 void
65 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
66
67 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060068 createFace(const Interest& request,
69 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070070
71 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060072 destroyFace(const Interest& request,
73 ControlParameters& parameters);
74
75 VIRTUAL_WITH_TESTS bool
76 validateLocalControlParameters(const Interest& request,
77 ControlParameters& parameters,
78 shared_ptr<LocalFace>& outFace,
79 LocalControlFeature& outFeature);
80
81 VIRTUAL_WITH_TESTS void
82 enableLocalControl(const Interest& request,
83 ControlParameters& parambeters);
84
85 VIRTUAL_WITH_TESTS void
86 disableLocalControl(const Interest& request,
87 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070088
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060089 void
90 ignoreUnsignedVerb(const Interest& request);
91
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070092 void
Alexander Afanasyevbbe3f0c2014-03-23 11:44:01 -070093 addCreatedFaceToForwarder(const shared_ptr<Face>& newFace);
94
95 void
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070096 onCreated(const Name& requestName,
Steve DiBenedetto7564d972014-03-24 14:28:46 -060097 ControlParameters& parameters,
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070098 const shared_ptr<Face>& newFace);
99
100 void
101 onConnectFailed(const Name& requestName, const std::string& reason);
102
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600103 void
104 onAddFace(shared_ptr<Face> face);
105
106 void
107 onRemoveFace(shared_ptr<Face> face);
108
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700109private:
110 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600111 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700112
113 void
114 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
115
116 void
117 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
118
119 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600120 processSectionUdp(const ConfigSection& configSection,
121 bool isDryRun,
122 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700123
124 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600125 processSectionEther(const ConfigSection& configSection,
126 bool isDryRun,
127 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700128
129 /** \brief parse a config option that can be either "yes" or "no"
130 * \throw ConfigFile::Error value is neither "yes" nor "no"
131 * \return true if "yes", false if "no"
132 */
133 bool
134 parseYesNo(const ConfigSection::const_iterator& i,
135 const std::string& optionName,
136 const std::string& sectionName);
137
138private:
139 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
140 FactoryMap m_factories;
141 FaceTable& m_faceTable;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600142 FaceStatusPublisher m_statusPublisher;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600143 NotificationStream m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700144
145 typedef function<void(FaceManager*,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600146 const Interest&,
147 ControlParameters&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700148
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600149 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
150 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700151
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600152 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700153
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600154 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
155 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700156
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700157
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600158 const SignedVerbDispatchTable m_signedVerbDispatch;
159 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
160
161 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
162
163 // number of components in an invalid signed command (i.e. should be signed, but isn't)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600164 // (/localhost/nfd/faces + verb + parameters) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700165 static const size_t COMMAND_UNSIGNED_NCOMPS;
166
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600167 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700168 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
169 static const size_t COMMAND_SIGNED_NCOMPS;
170
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600171 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
172 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
173
174 static const Name LIST_COMMAND_PREFIX;
175 static const size_t LIST_COMMAND_NCOMPS;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600176
177 static const Name EVENTS_COMMAND_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700178};
179
180inline bool
181FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
182 const std::string& optionName,
183 const std::string& sectionName)
184{
185 const std::string value = i->second.get_value<std::string>();
186 if (value == "yes")
187 {
188 return true;
189 }
190 else if (value == "no")
191 {
192 return false;
193 }
194
195 throw ConfigFile::Error("Invalid value for option \"" +
196 optionName + "\" in \"" +
197 sectionName + "\" section");
198
199}
200
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600201inline void
202FaceManager::ignoreUnsignedVerb(const Interest& request)
203{
204 // do nothing
205}
206
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700207} // namespace nfd
208
209#endif // NFD_MGMT_FACE_MANAGER_HPP