blob: 7726869e7481e152261fe2e3f049e48ba04a5791 [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>
akmhoque53353462014-04-22 08:43:45 -05006
akmhoque157b0a42014-05-13 00:26:37 -05007#include <ndn-cxx/name.hpp>
8
akmhoque53353462014-04-22 08:43:45 -05009#include "conf-parameter.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050010#include "conf-file-processor.hpp"
akmhoque53353462014-04-22 08:43:45 -050011#include "adjacent.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050012#include "utility/name-helper.hpp"
akmhoque53353462014-04-22 08:43:45 -050013
14
15namespace nlsr {
16
17using namespace std;
18
akmhoque157b0a42014-05-13 00:26:37 -050019bool
akmhoqueb6450b12014-04-24 00:01:03 -050020ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -050021{
akmhoque157b0a42014-05-13 00:26:37 -050022 bool ret = true;
23 ifstream inputFile;
24 inputFile.open(m_confFileName.c_str());
25 if (!inputFile.is_open()) {
26 string msg = "Failed to read configuration file: ";
27 msg += m_confFileName;
28 cerr << msg << endl;
29 ret = false;
30 }
31 ret = load(inputFile);
32 inputFile.close();
33 return ret;
34}
35
36bool
37ConfFileProcessor::load(istream& input)
38{
39 boost::property_tree::ptree pt;
40 bool ret = true;
41 try {
42 boost::property_tree::read_info(input, pt);
43 }
44 catch (const boost::property_tree::info_parser_error& error) {
45 stringstream msg;
46 std::cerr << "Failed to parse configuration file " << std::endl;
47 std::cerr << m_confFileName << std::endl;
48 return false;
49 }
50 for (boost::property_tree::ptree::const_iterator tn = pt.begin();
51 tn != pt.end(); ++tn) {
52 std::string section = tn->first;
53 boost::property_tree::ptree SectionAttributeTree = tn ->second;
54 ret = processSection(section, SectionAttributeTree);
55 if (ret == false) {
56 break;
57 }
58 }
59 return ret;
60}
61
62bool
63ConfFileProcessor::processSection(const std::string& section,
64 boost::property_tree::ptree SectionAttributeTree)
65{
66 bool ret = true;
67 if (section == "general")
akmhoque53353462014-04-22 08:43:45 -050068 {
akmhoque157b0a42014-05-13 00:26:37 -050069 ret = processConfSectionGeneral(SectionAttributeTree);
70 }
71 else if (section == "neighbors")
72 {
73 ret = processConfSectionNeighbors(SectionAttributeTree);
74 }
75 else if (section == "hyperbolic")
76 {
77 ret = processConfSectionHyperbolic(SectionAttributeTree);
78 }
79 else if (section == "fib")
80 {
81 ret = processConfSectionFib(SectionAttributeTree);
82 }
83 else if (section == "advertising")
84 {
85 ret = processConfSectionAdvertising(SectionAttributeTree);
86 }
87 else
88 {
89 std::cerr << "Wrong configuration Command: " << section << std::endl;
90 }
91 return ret;
92}
93
94bool
95ConfFileProcessor::processConfSectionGeneral(boost::property_tree::ptree
96 SectionAttributeTree)
97{
98 try {
99 std::string network = SectionAttributeTree.get<string>("network");
100 std::string site = SectionAttributeTree.get<string>("site");
101 std::string router = SectionAttributeTree.get<string>("router");
102 ndn::Name networkName(network);
103 if (!networkName.empty()) {
104 m_nlsr.getConfParameter().setNetwork(networkName);
105 }
106 else {
107 cerr << " Network can not be null or empty or in bad URI format :(!" << endl;
108 return false;
109 }
110 ndn::Name siteName(site);
111 if (!siteName.empty()) {
112 m_nlsr.getConfParameter().setSiteName(siteName);
113 }
114 else {
115 cerr << "Site can not be null or empty or in bad URI format:( !" << endl;
116 return false;
117 }
118 ndn::Name routerName(router);
119 if (!routerName.empty()) {
120 m_nlsr.getConfParameter().setRouterName(routerName);
121 }
122 else {
123 cerr << " Router name can not be null or empty or in bad URI format:( !" << endl;
124 return false;
125 }
126 }
127 catch (const std::exception& ex) {
128 cerr << ex.what() << endl;
129 return false;
130 }
131
132 try {
133 int32_t lsaRefreshTime = SectionAttributeTree.get<int32_t>("lsa-refresh-time");
134 if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN &&
135 lsaRefreshTime <= LSA_REFRESH_TIME_MAX) {
136 m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime);
137 }
138 else {
139 std::cerr << "Wrong value for lsa-refresh-time ";
140 std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";;
141 std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
142 return false;
143 }
144 }
145 catch (const std::exception& ex) {
146 std::cerr << ex.what() << std::endl;
147 return false;
148 }
149
150 try {
151 std::string logLevel = SectionAttributeTree.get<string>("log-level");
152 if ( boost::iequals(logLevel, "info") || boost::iequals(logLevel, "debug")) {
153 m_nlsr.getConfParameter().setLogLevel(logLevel);
154 }
155 else {
156 std::cerr << "Wrong value for log-level ";
157 std::cerr << "Allowed value: INFO, DEBUG" << std::endl;
158 return false;
159 }
160 }
161 catch (const std::exception& ex) {
162 std::cerr << ex.what() << std::endl;
163 return false;
164 }
165
166 return true;
167}
168
169bool
170ConfFileProcessor::processConfSectionNeighbors(boost::property_tree::ptree
171 SectionAttributeTree)
172{
173 try {
174 int retrials = SectionAttributeTree.get<int>("hello-retries");
175 if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) {
176 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
177 }
178 else {
179 std::cerr << "Wrong value for hello-retries. ";
180 std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
181 std::cerr << HELLO_RETRIES_MAX << std::endl;
182 return false;
183 }
184 }
185 catch (const std::exception& ex) {
186 std::cerr << ex.what() << std::endl;
187 return false;
188 }
189 try {
190 int timeOut = SectionAttributeTree.get<int>("hello-timeout");
191 if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) {
192 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
193 }
194 else {
195 std::cerr << "Wrong value for hello-timeout. ";
196 std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
197 std::cerr << HELLO_TIMEOUT_MAX << std::endl;
198 return false;
199 }
200 }
201 catch (const std::exception& ex) {
202 std::cerr << ex.what() << std::endl;
203 }
204 try {
205 int interval = SectionAttributeTree.get<int>("hello-interval");
206 if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) {
207 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
208 }
209 else {
210 std::cerr << "Wrong value for hello-interval. ";
211 std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
212 std::cerr << HELLO_INTERVAL_MAX << std::endl;
213 return false;
214 }
215 }
216 catch (const std::exception& ex) {
217 std::cerr << ex.what() << std::endl;
218 }
219 for (boost::property_tree::ptree::const_iterator tn =
220 SectionAttributeTree.begin(); tn != SectionAttributeTree.end(); ++tn) {
221
222 if (tn->first == "neighbor")
akmhoque53353462014-04-22 08:43:45 -0500223 {
akmhoque157b0a42014-05-13 00:26:37 -0500224 try {
225 boost::property_tree::ptree CommandAttriTree = tn->second;
226 std::string name = CommandAttriTree.get<std::string>("name");
227 std::string faceUri = CommandAttriTree.get<std::string>("face-uri");
228 double linkCost = CommandAttriTree.get<double>("link-cost",
229 Adjacent::DEFAULT_LINK_COST);
230 ndn::Name neighborName(name);
231 if (!neighborName.empty()) {
232 Adjacent adj(name, faceUri, linkCost, ADJACENT_STATUS_INACTIVE, 0);
233 m_nlsr.getAdjacencyList().insert(adj);
234 }
235 else {
236 cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
237 std::cerr << " or bad URI format" << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500238 }
239 }
akmhoque157b0a42014-05-13 00:26:37 -0500240 catch (const std::exception& ex) {
241 std::cerr << ex.what() << std::endl;
242 return false;
243 }
akmhoque53353462014-04-22 08:43:45 -0500244 }
245 }
akmhoque157b0a42014-05-13 00:26:37 -0500246 return true;
akmhoque53353462014-04-22 08:43:45 -0500247}
248
akmhoque157b0a42014-05-13 00:26:37 -0500249bool
250ConfFileProcessor::processConfSectionHyperbolic(boost::property_tree::ptree
251 SectionAttributeTree)
akmhoque53353462014-04-22 08:43:45 -0500252{
akmhoque157b0a42014-05-13 00:26:37 -0500253 std::string state;
254 try {
255 state= SectionAttributeTree.get<string>("state","off");
256 if (boost::iequals(state, "off")) {
257 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF);
258 }
259 else if (boost::iequals(state, "on")) {
260 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
261 }
262 else if (state == "dry-run") {
263 m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
264 }
265 else {
266 std::cerr << "Wrong format for hyperbolic state." << std::endl;
267 std::cerr << "Allowed value: off, on, dry-run" << std::endl;
268 return false;
269 }
akmhoque53353462014-04-22 08:43:45 -0500270 }
akmhoque157b0a42014-05-13 00:26:37 -0500271 catch (const std::exception& ex) {
272 std::cerr << ex.what() << std::endl;
273 return false;
akmhoque53353462014-04-22 08:43:45 -0500274 }
akmhoque157b0a42014-05-13 00:26:37 -0500275
276 try {
277 /* Radius and angle is mandatory configuration parameter in hyperbolic section.
278 * Even if router can have hyperbolic routing calculation off but other router
279 * in the network may use hyperbolic routing calculation for FIB generation.
280 * So each router need to advertise its hyperbolic coordinates in the network
281 */
282 double radius = SectionAttributeTree.get<double>("radius");
283 double angle = SectionAttributeTree.get<double>("angle");
284 if (!m_nlsr.getConfParameter().setCorR(radius)) {
285 return false;
286 }
287 m_nlsr.getConfParameter().setCorTheta(angle);
akmhoque53353462014-04-22 08:43:45 -0500288 }
akmhoque157b0a42014-05-13 00:26:37 -0500289 catch (const std::exception& ex) {
290 std::cerr << ex.what() << std::endl;
291 if (state == "on" || state == "dry-run") {
292 return false;
293 }
akmhoque53353462014-04-22 08:43:45 -0500294 }
akmhoque157b0a42014-05-13 00:26:37 -0500295
296 return true;
akmhoque53353462014-04-22 08:43:45 -0500297}
298
akmhoque157b0a42014-05-13 00:26:37 -0500299bool
300ConfFileProcessor::processConfSectionFib(boost::property_tree::ptree
301 SectionAttributeTree)
akmhoque53353462014-04-22 08:43:45 -0500302{
akmhoque157b0a42014-05-13 00:26:37 -0500303 try {
304 int maxFacesPerPrefixNumber =
305 SectionAttributeTree.get<int>("max-faces-per-prefix");
306 if (maxFacesPerPrefixNumber >= MAX_FACES_PER_PREFIX_MIN &&
307 maxFacesPerPrefixNumber <= MAX_FACES_PER_PREFIX_MAX)
akmhoque53353462014-04-22 08:43:45 -0500308 {
akmhoque157b0a42014-05-13 00:26:37 -0500309 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefixNumber);
akmhoque53353462014-04-22 08:43:45 -0500310 }
akmhoque157b0a42014-05-13 00:26:37 -0500311 else {
312 std::cerr << "Wrong value for max-faces-per-prefix. ";
313 std::cerr << "NLSR will user default value";
314 std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
315 return false;
akmhoque53353462014-04-22 08:43:45 -0500316 }
akmhoque53353462014-04-22 08:43:45 -0500317 }
akmhoque157b0a42014-05-13 00:26:37 -0500318 catch (const std::exception& ex) {
319 cerr << ex.what() << endl;
320 return false;
321 }
322 return true;
akmhoque53353462014-04-22 08:43:45 -0500323}
324
akmhoque157b0a42014-05-13 00:26:37 -0500325bool
326ConfFileProcessor::processConfSectionAdvertising(boost::property_tree::ptree
327 SectionAttributeTree)
akmhoque53353462014-04-22 08:43:45 -0500328{
akmhoque157b0a42014-05-13 00:26:37 -0500329 for (boost::property_tree::ptree::const_iterator tn =
330 SectionAttributeTree.begin(); tn != SectionAttributeTree.end(); ++tn) {
331 if (tn->first == "prefix") {
332 try {
333 std::string prefix = tn->second.data();
334 ndn::Name namePrefix(prefix);
335 if (!namePrefix.empty()) {
336 m_nlsr.getNamePrefixList().insert(namePrefix);
337 }
338 else {
339 std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI";
340 std::cerr << std::endl;
341 return false;
342 }
343 }
344 catch (const std::exception& ex) {
345 std::cerr << ex.what() << std::endl;
346 return false;
347 }
akmhoque53353462014-04-22 08:43:45 -0500348 }
akmhoque53353462014-04-22 08:43:45 -0500349 }
akmhoque157b0a42014-05-13 00:26:37 -0500350 return true;
akmhoque53353462014-04-22 08:43:45 -0500351}
akmhoque157b0a42014-05-13 00:26:37 -0500352}//namespace NLSR