blob: edcff580a449d9f11eb1994d20c4c70017353885 [file] [log] [blame]
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
21#include "annotated-topology-reader.h"
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -070022
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -080023#include "ns3/nstime.h"
24#include "ns3/log.h"
25#include "ns3/assert.h"
26#include "ns3/names.h"
27#include "ns3/net-device-container.h"
28#include "ns3/point-to-point-helper.h"
29#include "ns3/point-to-point-net-device.h"
30#include "ns3/internet-stack-helper.h"
31#include "ns3/ipv4-address-helper.h"
32#include "ns3/ipv4-global-routing-helper.h"
33#include "ns3/drop-tail-queue.h"
34#include "ns3/ipv4-interface.h"
35#include "ns3/ipv4.h"
36#include "ns3/string.h"
37#include "ns3/pointer.h"
38#include "ns3/uinteger.h"
39#include "ns3/ipv4-address.h"
40
41#include "ns3/constant-position-mobility-model.h"
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -080042
43#include <boost/foreach.hpp>
44#include <boost/lexical_cast.hpp>
45
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -080046#include <set>
47
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -070048using namespace std;
49
50namespace ns3
51{
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -070052
53NS_LOG_COMPONENT_DEFINE ("AnnotatedTopologyReader");
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -070054
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -080055AnnotatedTopologyReader::AnnotatedTopologyReader (const std::string &path)
56 : m_path (path)
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080057 , m_randX (0, 100.0)
58 , m_randY (0, 100.0)
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -070059{
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -080060 NS_LOG_FUNCTION (this);
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080061
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080062 SetMobilityModel ("ns3::ConstantPositionMobilityModel");
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -070063}
64
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080065void
66AnnotatedTopologyReader::SetBoundingBox (double ulx, double uly, double lrx, double lry)
67{
68 NS_LOG_FUNCTION (this << ulx << uly << lrx << lry);
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -080069
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080070 m_randX = UniformVariable (ulx, lrx);
71 m_randY = UniformVariable (uly, lry);
72}
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -080073
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080074void
75AnnotatedTopologyReader::SetMobilityModel (const std::string &model)
76{
77 NS_LOG_FUNCTION (this << model);
78 m_mobilityFactory.SetTypeId (model);
79}
80
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -070081AnnotatedTopologyReader::~AnnotatedTopologyReader ()
82{
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -080083 NS_LOG_FUNCTION (this);
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -070084}
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -080085
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080086Ptr<Node>
87AnnotatedTopologyReader::CreateNode (const std::string name)
88{
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080089 return CreateNode (name, m_randX.GetValue (), m_randY.GetValue ());
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080090}
91
92Ptr<Node>
93AnnotatedTopologyReader::CreateNode (const std::string name, double posX, double posY)
94{
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080095 NS_LOG_FUNCTION (this << name << posX << posY);
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080096 Ptr<Node> node = CreateObject<Node> ();
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080097 Ptr<MobilityModel> loc = DynamicCast<MobilityModel> (m_mobilityFactory.Create ());
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080098 node->AggregateObject (loc);
99
100 loc->SetPosition (Vector (posX, posY, 0));
101
102 Names::Add (m_path, name, node);
103
104 return node;
105}
106
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700107NodeContainer
108AnnotatedTopologyReader::Read (void)
109{
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800110 ifstream topgen;
111 topgen.open (GetFileName ().c_str ());
112 NodeContainer nodes;
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700113
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800114 if ( !topgen.is_open () )
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700115 {
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800116 NS_LOG_ERROR ("Cannot open file " << GetFileName () << " for reading");
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800117 return nodes;
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700118 }
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800119
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800120 while (!topgen.eof ())
121 {
122 string line;
123 getline (topgen, line);
124
125 if (line == "router") break;
126 }
127
128 while (!topgen.eof ())
129 {
130 string line;
131 getline (topgen,line);
132 if (line[0] == '#') continue; // comments
133 if (line=="link") break; // stop reading nodes
134
135 istringstream lineBuffer (line);
136 string name, city;
137 double latitude, longitude;
138
139 lineBuffer >> name >> city >> latitude >> longitude;
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800140 Ptr<Node> node = CreateNode (name, longitude, -latitude);
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800141 nodes.Add (node);
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800142 }
143
144 map<string, set<string> > processedLinks; // to eliminate duplications
145
146 // SeekToSection ("link");
147 while (!topgen.eof ())
148 {
149 string line;
150 getline (topgen,line);
151 if (line == "") continue;
152 if (line[0] == '#') continue; // comments
153
154 // NS_LOG_DEBUG ("Input: [" << line << "]");
155
156 istringstream lineBuffer (line);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800157 string from, to, capacity, metric, delay, maxPackets;
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800158
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800159 lineBuffer >> from >> to >> capacity >> metric >> delay >> maxPackets;
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800160
161 if (processedLinks[to].size () != 0 &&
162 processedLinks[to].find (from) != processedLinks[to].end ())
163 {
164 continue; // duplicated link
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700165 }
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800166 processedLinks[from].insert (to);
167
168 Ptr<Node> fromNode = Names::Find<Node> (m_path, from);
169 NS_ASSERT (fromNode != 0);
170 Ptr<Node> toNode = Names::Find<Node> (m_path, to);
171 NS_ASSERT (fromNode != 0);
172
173 Link link (fromNode, from, toNode, to);
174
175 link.SetAttribute ("DataRate", capacity);
176 link.SetAttribute ("OSPF", metric);
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800177
178 if (!delay.empty ())
179 link.SetAttribute ("Delay", delay);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800180 if (!maxPackets.empty ())
181 link.SetAttribute ("MaxPackets", maxPackets);
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800182
183 AddLink (link);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800184 NS_LOG_DEBUG ("New link " << from << " <==> " << to << " / " << capacity << " with " << metric << " metric (" << delay << ", " << maxPackets << ")");
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700185 }
186
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -0800187 NS_LOG_INFO ("Annotated topology created with " << nodes.GetN () << " nodes and " << LinksSize () << " links");
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800188 topgen.close ();
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700189
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800190 ApplySettings ();
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800191
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800192 return nodes;
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700193}
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700194
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800195void
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800196AnnotatedTopologyReader::AssignIpv4Addresses (Ipv4Address base)
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800197{
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800198 Ipv4AddressHelper address (base, Ipv4Mask ("/24"));
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700199
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800200 BOOST_FOREACH (const Link &link, m_linksList)
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800201 {
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800202 address.Assign (NetDeviceContainer (link.GetFromNetDevice (),
203 link.GetToNetDevice ()));
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700204
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800205 base = Ipv4Address (base.Get () + 256);
206 address.SetBase (base, Ipv4Mask ("/24"));
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800207 }
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700208
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800209 ApplyOspfMetric ();
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800210}
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700211
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800212void
213AnnotatedTopologyReader::ApplyOspfMetric ()
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800214{
Alexander Afanasyev0777f1c2011-12-12 20:22:45 -0800215 BOOST_FOREACH (const Link &link, m_linksList)
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800216 {
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800217 NS_LOG_DEBUG ("OSPF: " << link.GetAttribute ("OSPF"));
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800218 uint16_t metric = boost::lexical_cast<uint16_t> (link.GetAttribute ("OSPF"));
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700219
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800220 {
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800221 Ptr<Ipv4> ipv4 = link.GetFromNode ()->GetObject<Ipv4> ();
222 NS_ASSERT (ipv4 != 0);
Alexander Afanasyev0777f1c2011-12-12 20:22:45 -0800223
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800224 int32_t interfaceId = ipv4->GetInterfaceForDevice (link.GetFromNetDevice ());
225 NS_ASSERT (interfaceId >= 0);
Alexander Afanasyev0777f1c2011-12-12 20:22:45 -0800226
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800227 ipv4->SetMetric (interfaceId,metric);
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800228 }
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700229
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800230 {
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800231 Ptr<Ipv4> ipv4 = link.GetToNode ()->GetObject<Ipv4> ();
232 NS_ASSERT (ipv4 != 0);
233
234 int32_t interfaceId = ipv4->GetInterfaceForDevice (link.GetToNetDevice ());
235 NS_ASSERT (interfaceId >= 0);
236
237 ipv4->SetMetric (interfaceId,metric);
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800238 }
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800239 }
240}
241
242void
243AnnotatedTopologyReader::ApplySettings ()
244{
245 PointToPointHelper p2p;
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800246
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800247 BOOST_FOREACH (Link &link, m_linksList)
248 {
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800249 string tmp;
250
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800251 if (link.GetAttributeFailSafe ("DataRate", tmp))
252 {
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800253 NS_LOG_INFO ("DataRate = " + link.GetAttribute("DataRate"));
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800254 p2p.SetDeviceAttribute ("DataRate", StringValue (link.GetAttribute ("DataRate")));
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800255 }
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800256
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800257 if (link.GetAttributeFailSafe ("Delay", tmp))
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800258 {
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800259 NS_LOG_INFO ("Delay = " + link.GetAttribute("Delay"));
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800260 p2p.SetChannelAttribute ("Delay", StringValue (link.GetAttribute ("Delay")));
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800261 }
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800262
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800263 NetDeviceContainer nd = p2p.Install(link.GetFromNode (), link.GetToNode ());
264 link.SetNetDevices (nd.Get (0), nd.Get (1));
265
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800266 if (link.GetAttributeFailSafe ("MaxPackets", tmp))
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800267 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800268 NS_LOG_INFO ("MaxPackets = " + link.GetAttribute ("MaxPackets"));
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800269
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800270 PointerValue txQueue;
271
272 link.GetToNetDevice ()->GetAttribute ("TxQueue", txQueue);
273 NS_ASSERT (txQueue.Get<DropTailQueue> () != 0);
274 txQueue.Get<DropTailQueue> ()->SetAttribute ("MaxPackets", StringValue (link.GetAttribute ("MaxPackets")));
275
276 link.GetFromNetDevice ()->GetAttribute ("TxQueue", txQueue);
277 NS_ASSERT (txQueue.Get<DropTailQueue> () != 0);
278 txQueue.Get<DropTailQueue> ()->SetAttribute ("MaxPackets", StringValue (link.GetAttribute ("MaxPackets")));
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800279 }
Ilya Moiseenko7e14efa2011-12-12 17:56:22 -0800280
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800281 }
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800282}
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800283
284}