/
annotated-topology-reader.cpp
576 lines (454 loc) · 17.2 KB
/
annotated-topology-reader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2011-2015 Regents of the University of California.
*
* This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
* contributors.
*
* ndnSIM is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
// Based on the code by Hajime Tazaki <tazaki@sfc.wide.ad.jp>
#include "annotated-topology-reader.hpp"
#include "ns3/nstime.h"
#include "ns3/log.h"
#include "ns3/assert.h"
#include "ns3/names.h"
#include "ns3/net-device-container.h"
#include "ns3/point-to-point-helper.h"
#include "ns3/point-to-point-net-device.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/drop-tail-queue.h"
#include "ns3/ipv4-interface.h"
#include "ns3/ipv4.h"
#include "ns3/string.h"
#include "ns3/pointer.h"
#include "ns3/uinteger.h"
#include "ns3/ipv4-address.h"
#include "ns3/error-model.h"
#include "ns3/constant-position-mobility-model.h"
#include "ns3/double.h"
#include "model/ndn-l3-protocol.hpp"
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <set>
#ifdef NS3_MPI
#include <ns3/mpi-interface.h>
#endif
using namespace std;
namespace ns3 {
NS_LOG_COMPONENT_DEFINE("AnnotatedTopologyReader");
AnnotatedTopologyReader::AnnotatedTopologyReader(const std::string& path, double scale /*=1.0*/)
: m_path(path)
, m_randX(CreateObject<UniformRandomVariable>())
, m_randY(CreateObject<UniformRandomVariable>())
, m_scale(scale)
, m_requiredPartitions(1)
{
NS_LOG_FUNCTION(this);
m_randX->SetAttribute("Min", DoubleValue(0));
m_randX->SetAttribute("Max", DoubleValue(100.0));
m_randY->SetAttribute("Min", DoubleValue(0));
m_randY->SetAttribute("Max", DoubleValue(100.0));
SetMobilityModel("ns3::ConstantPositionMobilityModel");
}
void
AnnotatedTopologyReader::SetBoundingBox(double ulx, double uly, double lrx, double lry)
{
NS_LOG_FUNCTION(this << ulx << uly << lrx << lry);
m_randX->SetAttribute("Min", DoubleValue(ulx));
m_randX->SetAttribute("Max", DoubleValue(lrx));
m_randY->SetAttribute("Min", DoubleValue(uly));
m_randY->SetAttribute("Max", DoubleValue(lry));
}
void
AnnotatedTopologyReader::SetMobilityModel(const std::string& model)
{
NS_LOG_FUNCTION(this << model);
m_mobilityFactory.SetTypeId(model);
}
AnnotatedTopologyReader::~AnnotatedTopologyReader()
{
NS_LOG_FUNCTION(this);
}
Ptr<Node>
AnnotatedTopologyReader::CreateNode(const std::string name, uint32_t systemId)
{
NS_LOG_FUNCTION(this << name);
m_requiredPartitions = std::max(m_requiredPartitions, systemId + 1);
Ptr<Node> node = CreateObject<Node>(systemId);
Names::Add(m_path, name, node);
m_nodes.Add(node);
return node;
}
Ptr<Node>
AnnotatedTopologyReader::CreateNode(const std::string name, double posX, double posY,
uint32_t systemId)
{
NS_LOG_FUNCTION(this << name << posX << posY);
m_requiredPartitions = std::max(m_requiredPartitions, systemId + 1);
Ptr<Node> node = CreateObject<Node>(systemId);
Ptr<MobilityModel> loc = DynamicCast<MobilityModel>(m_mobilityFactory.Create());
node->AggregateObject(loc);
loc->SetPosition(Vector(posX, posY, 0));
Names::Add(m_path, name, node);
m_nodes.Add(node);
return node;
}
NodeContainer
AnnotatedTopologyReader::GetNodes() const
{
return m_nodes;
}
const std::list<TopologyReader::Link>&
AnnotatedTopologyReader::GetLinks() const
{
return m_linksList;
}
NodeContainer
AnnotatedTopologyReader::Read(void)
{
ifstream topgen;
topgen.open(GetFileName().c_str());
if (!topgen.is_open() || !topgen.good()) {
NS_FATAL_ERROR("Cannot open file " << GetFileName() << " for reading");
return m_nodes;
}
while (!topgen.eof()) {
string line;
getline(topgen, line);
if (line == "router")
break;
}
if (topgen.eof()) {
NS_FATAL_ERROR("Topology file " << GetFileName() << " does not have \"router\" section");
return m_nodes;
}
while (!topgen.eof()) {
string line;
getline(topgen, line);
if (line[0] == '#')
continue; // comments
if (line == "link")
break; // stop reading nodes
istringstream lineBuffer(line);
string name, city;
double latitude = 0, longitude = 0;
uint32_t systemId = 0;
lineBuffer >> name >> city >> latitude >> longitude >> systemId;
if (name.empty())
continue;
Ptr<Node> node;
if (abs(latitude) > 0.001 && abs(latitude) > 0.001)
node = CreateNode(name, m_scale * longitude, -m_scale * latitude, systemId);
else {
Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable>();
node = CreateNode(name, var->GetValue(0, 200), var->GetValue(0, 200), systemId);
// node = CreateNode (name, systemId);
}
}
map<string, set<string>> processedLinks; // to eliminate duplications
if (topgen.eof()) {
NS_LOG_ERROR("Topology file " << GetFileName() << " does not have \"link\" section");
return m_nodes;
}
// SeekToSection ("link");
while (!topgen.eof()) {
string line;
getline(topgen, line);
if (line == "")
continue;
if (line[0] == '#')
continue; // comments
// NS_LOG_DEBUG ("Input: [" << line << "]");
istringstream lineBuffer(line);
string from, to, capacity, metric, delay, maxPackets, lossRate;
lineBuffer >> from >> to >> capacity >> metric >> delay >> maxPackets >> lossRate;
if (processedLinks[to].size() != 0
&& processedLinks[to].find(from) != processedLinks[to].end()) {
continue; // duplicated link
}
processedLinks[from].insert(to);
Ptr<Node> fromNode = Names::Find<Node>(m_path, from);
NS_ASSERT_MSG(fromNode != 0, from << " node not found");
Ptr<Node> toNode = Names::Find<Node>(m_path, to);
NS_ASSERT_MSG(toNode != 0, to << " node not found");
Link link(fromNode, from, toNode, to);
link.SetAttribute("DataRate", capacity);
link.SetAttribute("OSPF", metric);
if (!delay.empty())
link.SetAttribute("Delay", delay);
if (!maxPackets.empty())
link.SetAttribute("MaxPackets", maxPackets);
// Saran Added lossRate
if (!lossRate.empty())
link.SetAttribute("LossRate", lossRate);
AddLink(link);
NS_LOG_DEBUG("New link " << from << " <==> " << to << " / " << capacity << " with " << metric
<< " metric (" << delay << ", " << maxPackets << ", " << lossRate
<< ")");
}
NS_LOG_INFO("Annotated topology created with " << m_nodes.GetN() << " nodes and " << LinksSize()
<< " links");
topgen.close();
ApplySettings();
return m_nodes;
}
void
AnnotatedTopologyReader::AssignIpv4Addresses(Ipv4Address base)
{
Ipv4AddressHelper address(base, Ipv4Mask("/24"));
BOOST_FOREACH (const Link& link, m_linksList) {
address.Assign(NetDeviceContainer(link.GetFromNetDevice(), link.GetToNetDevice()));
base = Ipv4Address(base.Get() + 256);
address.SetBase(base, Ipv4Mask("/24"));
}
}
void
AnnotatedTopologyReader::ApplyOspfMetric()
{
BOOST_FOREACH (const Link& link, m_linksList) {
NS_LOG_DEBUG("OSPF: " << link.GetAttribute("OSPF"));
uint16_t metric = boost::lexical_cast<uint16_t>(link.GetAttribute("OSPF"));
{
Ptr<Ipv4> ipv4 = link.GetFromNode()->GetObject<Ipv4>();
if (ipv4 != 0) {
int32_t interfaceId = ipv4->GetInterfaceForDevice(link.GetFromNetDevice());
NS_ASSERT(interfaceId >= 0);
ipv4->SetMetric(interfaceId, metric);
}
Ptr<ndn::L3Protocol> ndn = link.GetFromNode()->GetObject<ndn::L3Protocol>();
if (ndn != 0) {
shared_ptr<ndn::Face> face = ndn->getFaceByNetDevice(link.GetFromNetDevice());
NS_ASSERT(face != 0);
face->setMetric(metric);
}
}
{
Ptr<Ipv4> ipv4 = link.GetToNode()->GetObject<Ipv4>();
if (ipv4 != 0) {
int32_t interfaceId = ipv4->GetInterfaceForDevice(link.GetToNetDevice());
NS_ASSERT(interfaceId >= 0);
ipv4->SetMetric(interfaceId, metric);
}
Ptr<ndn::L3Protocol> ndn = link.GetToNode()->GetObject<ndn::L3Protocol>();
if (ndn != 0) {
shared_ptr<ndn::Face> face = ndn->getFaceByNetDevice(link.GetToNetDevice());
NS_ASSERT(face != 0);
face->setMetric(metric);
}
}
}
}
void
AnnotatedTopologyReader::ApplySettings()
{
#ifdef NS3_MPI
if (MpiInterface::IsEnabled() && MpiInterface::GetSize() != m_requiredPartitions) {
std::cerr << "MPI interface is enabled, but number of partitions (" << MpiInterface::GetSize()
<< ") is not equal to number of partitions in the topology (" << m_requiredPartitions
<< ")";
exit(-1);
}
#endif
PointToPointHelper p2p;
BOOST_FOREACH (Link& link, m_linksList) {
// cout << "Link: " << Findlink.GetFromNode () << ", " << link.GetToNode () << endl;
string tmp;
////////////////////////////////////////////////
if (link.GetAttributeFailSafe("MaxPackets", tmp)) {
NS_LOG_INFO("MaxPackets = " + link.GetAttribute("MaxPackets"));
try {
std::string maxPackets = link.GetAttribute("MaxPackets");
// compatibility mode. Only DropTailQueue is supported
p2p.SetQueue("ns3::DropTailQueue<Packet>", "MaxSize", StringValue(maxPackets + "p"));
}
catch (...) {
typedef boost::tokenizer<boost::escaped_list_separator<char>> tokenizer;
std::string value = link.GetAttribute("MaxPackets");
tokenizer tok(value);
tokenizer::iterator token = tok.begin();
p2p.SetQueue(*token);
for (token++; token != tok.end(); token++) {
boost::escaped_list_separator<char> separator('\\', '=', '\"');
tokenizer attributeTok(*token, separator);
tokenizer::iterator attributeToken = attributeTok.begin();
string attribute = *attributeToken;
attributeToken++;
if (attributeToken == attributeTok.end()) {
NS_LOG_ERROR("Queue attribute [" << *token
<< "] should be in form <Attribute>=<Value>");
continue;
}
string value = *attributeToken;
p2p.SetQueueAttribute(attribute, StringValue(value));
}
}
}
if (link.GetAttributeFailSafe("DataRate", tmp)) {
NS_LOG_INFO("DataRate = " + link.GetAttribute("DataRate"));
p2p.SetDeviceAttribute("DataRate", StringValue(link.GetAttribute("DataRate")));
}
if (link.GetAttributeFailSafe("Delay", tmp)) {
NS_LOG_INFO("Delay = " + link.GetAttribute("Delay"));
p2p.SetChannelAttribute("Delay", StringValue(link.GetAttribute("Delay")));
}
NetDeviceContainer nd = p2p.Install(link.GetFromNode(), link.GetToNode());
link.SetNetDevices(nd.Get(0), nd.Get(1));
////////////////////////////////////////////////
if (link.GetAttributeFailSafe("LossRate", tmp)) {
NS_LOG_INFO("LinkError = " + link.GetAttribute("LossRate"));
typedef boost::tokenizer<boost::escaped_list_separator<char>> tokenizer;
std::string value = link.GetAttribute("LossRate");
tokenizer tok(value);
tokenizer::iterator token = tok.begin();
ObjectFactory factory(*token);
for (token++; token != tok.end(); token++) {
boost::escaped_list_separator<char> separator('\\', '=', '\"');
tokenizer attributeTok(*token, separator);
tokenizer::iterator attributeToken = attributeTok.begin();
string attribute = *attributeToken;
attributeToken++;
if (attributeToken == attributeTok.end()) {
NS_LOG_ERROR("ErrorModel attribute [" << *token
<< "] should be in form <Attribute>=<Value>");
continue;
}
string value = *attributeToken;
factory.Set(attribute, StringValue(value));
}
nd.Get(0)->SetAttribute("ReceiveErrorModel", PointerValue(factory.Create<ErrorModel>()));
nd.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue(factory.Create<ErrorModel>()));
}
}
}
void
AnnotatedTopologyReader::SaveTopology(const std::string& file)
{
ofstream os(file.c_str(), ios::trunc);
os << "# any empty lines and lines starting with '#' symbol is ignored\n"
<< "\n"
<< "# The file should contain exactly two sections: router and link, each starting with the "
"corresponding keyword\n"
<< "\n"
<< "# router section defines topology nodes and their relative positions (e.g., to use in "
"visualizer)\n"
<< "router\n"
<< "\n"
<< "# each line in this section represents one router and should have the following data\n"
<< "# node comment yPos xPos\n";
for (NodeContainer::Iterator node = m_nodes.Begin(); node != m_nodes.End(); node++) {
std::string name = Names::FindName(*node);
Ptr<MobilityModel> mobility = (*node)->GetObject<MobilityModel>();
Vector position = mobility->GetPosition();
os << name << "\t"
<< "NA"
<< "\t" << -position.y << "\t" << position.x << "\n";
}
os
<< "# link section defines point-to-point links between nodes and characteristics of these "
"links\n"
<< "\n"
<< "link\n"
<< "\n"
<< "# Each line should be in the following format (only first two are required, the rest can "
"be omitted)\n"
<< "# srcNode dstNode bandwidth metric delay queue\n"
<< "# bandwidth: link bandwidth\n"
<< "# metric: routing metric\n"
<< "# delay: link delay\n"
<< "# queue: MaxPackets for transmission queue on the link (both directions)\n"
<< "# error: comma-separated list, specifying class for ErrorModel and necessary attributes\n";
for (std::list<Link>::const_iterator link = m_linksList.begin(); link != m_linksList.end();
link++) {
os << Names::FindName(link->GetFromNode()) << "\t";
os << Names::FindName(link->GetToNode()) << "\t";
string tmp;
if (link->GetAttributeFailSafe("DataRate", tmp))
os << link->GetAttribute("DataRate") << "\t";
else
NS_FATAL_ERROR("DataRate must be specified for the link");
if (link->GetAttributeFailSafe("OSPF", tmp))
os << link->GetAttribute("OSPF") << "\t";
else
os << "1\t";
if (link->GetAttributeFailSafe("Delay", tmp)) {
os << link->GetAttribute("Delay") << "\t";
if (link->GetAttributeFailSafe("MaxPackets", tmp)) {
os << link->GetAttribute("MaxPackets") << "\t";
if (link->GetAttributeFailSafe("LossRate", tmp)) {
os << link->GetAttribute("LossRate") << "\t";
}
}
}
os << "\n";
}
}
/// @cond include_hidden
template<class Names>
class name_writer {
public:
name_writer(Names _names)
: names(_names)
{
}
template<class VertexOrEdge>
void
operator()(std::ostream& out, const VertexOrEdge& v) const
{
// out << "[label=\"" << names[v] << "\",style=filled,fillcolor=\"" << colors[v] << "\"]";
out << "[shape=\"circle\",width=0.1,label=\"\",style=filled,fillcolor=\"green\"]";
}
private:
Names names;
};
template<class Names>
inline name_writer<Names>
make_name_writer(Names n)
{
return name_writer<Names>(n);
}
/// @endcond
void
AnnotatedTopologyReader::SaveGraphviz(const std::string& file)
{
typedef boost::adjacency_list_traits<boost::setS, boost::setS, boost::undirectedS> Traits;
typedef boost::property<boost::vertex_name_t, std::string,
boost::property<boost::vertex_index_t, uint32_t>> nodeProperty;
typedef boost::no_property edgeProperty;
typedef boost::adjacency_list<boost::setS, boost::setS, boost::undirectedS, nodeProperty,
edgeProperty> Graph;
typedef map<string, Traits::vertex_descriptor> node_map_t;
node_map_t graphNodes;
Graph graph;
for (NodeContainer::Iterator node = m_nodes.Begin(); node != m_nodes.End(); node++) {
std::pair<node_map_t::iterator, bool> retval = graphNodes.insert(
make_pair(Names::FindName(*node), add_vertex(nodeProperty(Names::FindName(*node)), graph)));
// NS_ASSERT (ok == true);
put(boost::vertex_index, graph, retval.first->second, (*node)->GetId());
}
for (std::list<Link>::const_iterator link = m_linksList.begin(); link != m_linksList.end();
link++) {
node_map_t::iterator from = graphNodes.find(Names::FindName(link->GetFromNode()));
node_map_t::iterator to = graphNodes.find(Names::FindName(link->GetToNode()));
// add_edge (node->second, otherNode->second, m_graph);
boost::add_edge(from->second, to->second, graph);
}
ofstream of(file.c_str());
boost::property_map<Graph, boost::vertex_name_t>::type names = get(boost::vertex_name, graph);
write_graphviz(of, graph, make_name_writer(names));
}
}