blob: b42bf804aa79c2354fcd5eeaa15e92b1fcbd590c [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <fstream>
akmhoque157b0a42014-05-13 00:26:37 -05003#include <boost/algorithm/string.hpp>
4#include <boost/property_tree/info_parser.hpp>
5#include <boost/property_tree/ptree.hpp>
akmhoque674b0b12014-05-20 14:33:28 -05006#include <boost/filesystem.hpp>
akmhoque53353462014-04-22 08:43:45 -05007
akmhoque157b0a42014-05-13 00:26:37 -05008#include <ndn-cxx/name.hpp>
9
akmhoque53353462014-04-22 08:43:45 -050010#include "conf-parameter.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050011#include "conf-file-processor.hpp"
akmhoque53353462014-04-22 08:43:45 -050012#include "adjacent.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050013#include "utility/name-helper.hpp"
akmhoque53353462014-04-22 08:43:45 -050014
15
16namespace nlsr {
17
18using namespace std;
19
akmhoque157b0a42014-05-13 00:26:37 -050020bool
akmhoqueb6450b12014-04-24 00:01:03 -050021ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -050022{
akmhoque157b0a42014-05-13 00:26:37 -050023 bool ret = true;
24 ifstream inputFile;
25 inputFile.open(m_confFileName.c_str());
26 if (!inputFile.is_open()) {
27 string msg = "Failed to read configuration file: ";
28 msg += m_confFileName;
29 cerr << msg << endl;
30 ret = false;
31 }
32 ret = load(inputFile);
33 inputFile.close();
34 return ret;
35}
36
37bool
38ConfFileProcessor::load(istream& input)
39{
40 boost::property_tree::ptree pt;
41 bool ret = true;
42 try {
43 boost::property_tree::read_info(input, pt);
44 }
45 catch (const boost::property_tree::info_parser_error& error) {
46 stringstream msg;
47 std::cerr << "Failed to parse configuration file " << std::endl;
48 std::cerr << m_confFileName << std::endl;
49 return false;
50 }
51 for (boost::property_tree::ptree::const_iterator tn = pt.begin();
52 tn != pt.end(); ++tn) {
53 std::string section = tn->first;
54 boost::property_tree::ptree SectionAttributeTree = tn ->second;
55 ret = processSection(section, SectionAttributeTree);
56 if (ret == false) {
57 break;
58 }
59 }
60 return ret;
61}
62
63bool
64ConfFileProcessor::processSection(const std::string& section,
65 boost::property_tree::ptree SectionAttributeTree)
66{
67 bool ret = true;
68 if (section == "general")
akmhoque53353462014-04-22 08:43:45 -050069 {
akmhoque157b0a42014-05-13 00:26:37 -050070 ret = processConfSectionGeneral(SectionAttributeTree);
71 }
72 else if (section == "neighbors")
73 {
74 ret = processConfSectionNeighbors(SectionAttributeTree);
75 }
76 else if (section == "hyperbolic")
77 {
78 ret = processConfSectionHyperbolic(SectionAttributeTree);
79 }
80 else if (section == "fib")
81 {
82 ret = processConfSectionFib(SectionAttributeTree);
83 }
84 else if (section == "advertising")
85 {
86 ret = processConfSectionAdvertising(SectionAttributeTree);
87 }
88 else
89 {
90 std::cerr << "Wrong configuration Command: " << section << std::endl;
91 }
92 return ret;
93}
94
95bool
96ConfFileProcessor::processConfSectionGeneral(boost::property_tree::ptree
97 SectionAttributeTree)
98{
99 try {
100 std::string network = SectionAttributeTree.get<string>("network");
101 std::string site = SectionAttributeTree.get<string>("site");
102 std::string router = SectionAttributeTree.get<string>("router");
103 ndn::Name networkName(network);
104 if (!networkName.empty()) {
105 m_nlsr.getConfParameter().setNetwork(networkName);
106 }
107 else {
108 cerr << " Network can not be null or empty or in bad URI format :(!" << endl;
109 return false;
110 }
111 ndn::Name siteName(site);
112 if (!siteName.empty()) {
113 m_nlsr.getConfParameter().setSiteName(siteName);
114 }
115 else {
116 cerr << "Site can not be null or empty or in bad URI format:( !" << endl;
117 return false;
118 }
119 ndn::Name routerName(router);
120 if (!routerName.empty()) {
121 m_nlsr.getConfParameter().setRouterName(routerName);
122 }
123 else {
124 cerr << " Router name can not be null or empty or in bad URI format:( !" << endl;
125 return false;
126 }
127 }
128 catch (const std::exception& ex) {
129 cerr << ex.what() << endl;
130 return false;
131 }
132
133 try {
134 int32_t lsaRefreshTime = SectionAttributeTree.get<int32_t>("lsa-refresh-time");
135 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN &&
136 lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
137 m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime);
138 }
139 else {
140 std::cerr << "Wrong value for lsa-refresh-time ";
141 std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";;
142 std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
143 return false;
144 }
145 }
146 catch (const std::exception& ex) {
147 std::cerr << ex.what() << std::endl;
148 return false;
149 }
150
151 try {
152 std::string logLevel = SectionAttributeTree.get<string>("log-level");
153 if ( boost::iequals(logLevel, "info") || boost::iequals(logLevel, "debug")) {
154 m_nlsr.getConfParameter().setLogLevel(logLevel);
155 }
156 else {
157 std::cerr << "Wrong value for log-level ";
158 std::cerr << "Allowed value: INFO, DEBUG" << std::endl;
159 return false;
160 }
161 }
162 catch (const std::exception& ex) {
163 std::cerr << ex.what() << std::endl;
164 return false;
165 }
166
akmhoque674b0b12014-05-20 14:33:28 -0500167 try {
168 std::string logDir = SectionAttributeTree.get<string>("log-dir");
169 if (boost::filesystem::exists(logDir)) {
170 if (boost::filesystem::is_directory(logDir)) {
171 std::string testFileName=logDir+"/test.log";
172 ofstream testOutFile;
173 testOutFile.open(testFileName.c_str());
174 if (testOutFile.is_open() && testOutFile.good()) {
175 m_nlsr.getConfParameter().setLogDir(logDir);
176 }
177 else {
178 std::cerr << "User does not have read and write permission on the directory";
179 std::cerr << std::endl;
180 return false;
181 }
182 testOutFile.close();
183 remove(testFileName.c_str());
184 }
185 else {
186 std::cerr << "Provided path is not a directory" << std::endl;
187 return false;
188 }
189 }
190 else {
191 std::cerr << "Log directory provided does not exists" << std::endl;
192 return false;
193 }
194 }
195 catch (const std::exception& ex) {
196 std::cerr << "You must configure log directory" << std::endl;
197 std::cerr << ex.what() << std::endl;
198 return false;
199 }
200 try {
201 std::string seqDir = SectionAttributeTree.get<string>("seq-dir");
202 if (boost::filesystem::exists(seqDir)) {
203 if (boost::filesystem::is_directory(seqDir)) {
204 std::string testFileName=seqDir+"/test.seq";
205 ofstream testOutFile;
206 testOutFile.open(testFileName.c_str());
207 if (testOutFile.is_open() && testOutFile.good()) {
208 m_nlsr.getConfParameter().setSeqFileDir(seqDir);
209 }
210 else {
211 std::cerr << "User does not have read and write permission on the directory";
212 std::cerr << std::endl;
213 return false;
214 }
215 testOutFile.close();
216 remove(testFileName.c_str());
217 }
218 else {
219 std::cerr << "Provided path is not a directory" << std::endl;
220 return false;
221 }
222 }
223 else {
224 std::cerr << "Seq directory provided does not exists" << std::endl;
225 return false;
226 }
227 }
228 catch (const std::exception& ex) {
229 std::cerr << "You must configure sequence directory" << std::endl;
230 std::cerr << ex.what() << std::endl;
231 return false;
232 }
akmhoque157b0a42014-05-13 00:26:37 -0500233 return true;
234}
235
236bool
237ConfFileProcessor::processConfSectionNeighbors(boost::property_tree::ptree
238 SectionAttributeTree)
239{
240 try {
241 int retrials = SectionAttributeTree.get<int>("hello-retries");
242 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
243 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
244 }
245 else {
246 std::cerr << "Wrong value for hello-retries. ";
247 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
248 std::cerr << HELLO_RETRIES_MAX << std::endl;
249 return false;
250 }
251 }
252 catch (const std::exception& ex) {
253 std::cerr << ex.what() << std::endl;
254 return false;
255 }
256 try {
257 int timeOut = SectionAttributeTree.get<int>("hello-timeout");
258 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
259 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
260 }
261 else {
262 std::cerr << "Wrong value for hello-timeout. ";
263 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
264 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
265 return false;
266 }
267 }
268 catch (const std::exception& ex) {
269 std::cerr << ex.what() << std::endl;
270 }
271 try {
272 int interval = SectionAttributeTree.get<int>("hello-interval");
273 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
274 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
275 }
276 else {
277 std::cerr << "Wrong value for hello-interval. ";
278 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
279 std::cerr << HELLO_INTERVAL_MAX << std::endl;
280 return false;
281 }
282 }
283 catch (const std::exception& ex) {
284 std::cerr << ex.what() << std::endl;
285 }
286 for (boost::property_tree::ptree::const_iterator tn =
287 SectionAttributeTree.begin(); tn != SectionAttributeTree.end(); ++tn) {
288
289 if (tn->first == "neighbor")
akmhoque53353462014-04-22 08:43:45 -0500290 {
akmhoque157b0a42014-05-13 00:26:37 -0500291 try {
292 boost::property_tree::ptree CommandAttriTree = tn->second;
293 std::string name = CommandAttriTree.get<std::string>("name");
294 std::string faceUri = CommandAttriTree.get<std::string>("face-uri");
295 double linkCost = CommandAttriTree.get<double>("link-cost",
296 Adjacent::DEFAULT_LINK_COST);
297 ndn::Name neighborName(name);
298 if (!neighborName.empty()) {
299 Adjacent adj(name, faceUri, linkCost, ADJACENT_STATUS_INACTIVE, 0);
300 m_nlsr.getAdjacencyList().insert(adj);
301 }
302 else {
akmhoque674b0b12014-05-20 14:33:28 -0500303 std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
akmhoque157b0a42014-05-13 00:26:37 -0500304 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500305 }
306 }
akmhoque157b0a42014-05-13 00:26:37 -0500307 catch (const std::exception& ex) {
308 std::cerr << ex.what() << std::endl;
309 return false;
310 }
akmhoque53353462014-04-22 08:43:45 -0500311 }
312 }
akmhoque157b0a42014-05-13 00:26:37 -0500313 return true;
akmhoque53353462014-04-22 08:43:45 -0500314}
315
akmhoque157b0a42014-05-13 00:26:37 -0500316bool
317ConfFileProcessor::processConfSectionHyperbolic(boost::property_tree::ptree
318 SectionAttributeTree)
akmhoque53353462014-04-22 08:43:45 -0500319{
akmhoque157b0a42014-05-13 00:26:37 -0500320 std::string state;
321 try {
322 state= SectionAttributeTree.get<string>("state","off");
323 if (boost::iequals(state, "off")) {
324 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
325 }
326 else if (boost::iequals(state, "on")) {
327 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
328 }
329 else if (state == "dry-run") {
330 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
331 }
332 else {
333 std::cerr << "Wrong format for hyperbolic state." << std::endl;
334 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
335 return false;
336 }
akmhoque53353462014-04-22 08:43:45 -0500337 }
akmhoque157b0a42014-05-13 00:26:37 -0500338 catch (const std::exception& ex) {
339 std::cerr << ex.what() << std::endl;
340 return false;
akmhoque53353462014-04-22 08:43:45 -0500341 }
akmhoque157b0a42014-05-13 00:26:37 -0500342
343 try {
344 /* Radius and angle is mandatory configuration parameter in hyperbolic section.
345 * Even if router can have hyperbolic routing calculation off but other router
346 * in the network may use hyperbolic routing calculation for FIB generation.
347 * So each router need to advertise its hyperbolic coordinates in the network
348 */
349 double radius = SectionAttributeTree.get<double>("radius");
350 double angle = SectionAttributeTree.get<double>("angle");
351 if (!m_nlsr.getConfParameter().setCorR(radius)) {
352 return false;
353 }
354 m_nlsr.getConfParameter().setCorTheta(angle);
akmhoque53353462014-04-22 08:43:45 -0500355 }
akmhoque157b0a42014-05-13 00:26:37 -0500356 catch (const std::exception& ex) {
357 std::cerr << ex.what() << std::endl;
358 if (state == "on" || state == "dry-run") {
359 return false;
360 }
akmhoque53353462014-04-22 08:43:45 -0500361 }
akmhoque157b0a42014-05-13 00:26:37 -0500362
363 return true;
akmhoque53353462014-04-22 08:43:45 -0500364}
365
akmhoque157b0a42014-05-13 00:26:37 -0500366bool
367ConfFileProcessor::processConfSectionFib(boost::property_tree::ptree
368 SectionAttributeTree)
akmhoque53353462014-04-22 08:43:45 -0500369{
akmhoque157b0a42014-05-13 00:26:37 -0500370 try {
371 int maxFacesPerPrefixNumber =
372 SectionAttributeTree.get<int>("max-faces-per-prefix");
373 if (maxFacesPerPrefixNumber >= MAX_FACES_PER_PREFIX_MIN &&
374 maxFacesPerPrefixNumber <= MAX_FACES_PER_PREFIX_MAX)
akmhoque53353462014-04-22 08:43:45 -0500375 {
akmhoque157b0a42014-05-13 00:26:37 -0500376 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefixNumber);
akmhoque53353462014-04-22 08:43:45 -0500377 }
akmhoque157b0a42014-05-13 00:26:37 -0500378 else {
379 std::cerr << "Wrong value for max-faces-per-prefix. ";
380 std::cerr << "NLSR will user default value";
381 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
382 return false;
akmhoque53353462014-04-22 08:43:45 -0500383 }
akmhoque53353462014-04-22 08:43:45 -0500384 }
akmhoque157b0a42014-05-13 00:26:37 -0500385 catch (const std::exception& ex) {
386 cerr << ex.what() << endl;
387 return false;
388 }
389 return true;
akmhoque53353462014-04-22 08:43:45 -0500390}
391
akmhoque157b0a42014-05-13 00:26:37 -0500392bool
393ConfFileProcessor::processConfSectionAdvertising(boost::property_tree::ptree
394 SectionAttributeTree)
akmhoque53353462014-04-22 08:43:45 -0500395{
akmhoque157b0a42014-05-13 00:26:37 -0500396 for (boost::property_tree::ptree::const_iterator tn =
397 SectionAttributeTree.begin(); tn != SectionAttributeTree.end(); ++tn) {
398 if (tn->first == "prefix") {
399 try {
400 std::string prefix = tn->second.data();
401 ndn::Name namePrefix(prefix);
402 if (!namePrefix.empty()) {
403 m_nlsr.getNamePrefixList().insert(namePrefix);
404 }
405 else {
akmhoque674b0b12014-05-20 14:33:28 -0500406 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500407 return false;
408 }
409 }
410 catch (const std::exception& ex) {
411 std::cerr << ex.what() << std::endl;
412 return false;
413 }
akmhoque53353462014-04-22 08:43:45 -0500414 }
akmhoque53353462014-04-22 08:43:45 -0500415 }
akmhoque157b0a42014-05-13 00:26:37 -0500416 return true;
akmhoque53353462014-04-22 08:43:45 -0500417}
akmhoque157b0a42014-05-13 00:26:37 -0500418}//namespace NLSR