blob: 194a90c58dd2fd3a89f17c448ff06e6bf8a03821 [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 Afanasyev011b8592011-12-21 14:45:27 -080055AnnotatedTopologyReader::AnnotatedTopologyReader (const std::string &path, double scale/*=1.0*/)
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -080056 : m_path (path)
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080057 , m_randX (0, 100.0)
58 , m_randY (0, 100.0)
Alexander Afanasyev011b8592011-12-21 14:45:27 -080059 , m_scale (scale)
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -070060{
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -080061 NS_LOG_FUNCTION (this);
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080062
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080063 SetMobilityModel ("ns3::ConstantPositionMobilityModel");
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -070064}
65
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080066void
67AnnotatedTopologyReader::SetBoundingBox (double ulx, double uly, double lrx, double lry)
68{
69 NS_LOG_FUNCTION (this << ulx << uly << lrx << lry);
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -080070
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080071 m_randX = UniformVariable (ulx, lrx);
72 m_randY = UniformVariable (uly, lry);
73}
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -080074
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080075void
76AnnotatedTopologyReader::SetMobilityModel (const std::string &model)
77{
78 NS_LOG_FUNCTION (this << model);
79 m_mobilityFactory.SetTypeId (model);
80}
81
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -070082AnnotatedTopologyReader::~AnnotatedTopologyReader ()
83{
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -080084 NS_LOG_FUNCTION (this);
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -070085}
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -080086
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080087Ptr<Node>
88AnnotatedTopologyReader::CreateNode (const std::string name)
89{
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080090 return CreateNode (name, m_randX.GetValue (), m_randY.GetValue ());
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080091}
92
93Ptr<Node>
94AnnotatedTopologyReader::CreateNode (const std::string name, double posX, double posY)
95{
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080096 NS_LOG_FUNCTION (this << name << posX << posY);
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080097 Ptr<Node> node = CreateObject<Node> ();
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -080098 Ptr<MobilityModel> loc = DynamicCast<MobilityModel> (m_mobilityFactory.Create ());
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -080099 node->AggregateObject (loc);
100
101 loc->SetPosition (Vector (posX, posY, 0));
102
103 Names::Add (m_path, name, node);
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -0800104 m_nodes.Add (node);
Alexander Afanasyevae3b7c32011-12-13 13:20:06 -0800105
106 return node;
107}
108
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700109NodeContainer
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -0800110AnnotatedTopologyReader::GetNodes () const
111{
112 return m_nodes;
113}
114
115
116NodeContainer
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700117AnnotatedTopologyReader::Read (void)
118{
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800119 ifstream topgen;
120 topgen.open (GetFileName ().c_str ());
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700121
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800122 if ( !topgen.is_open () )
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700123 {
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800124 NS_LOG_ERROR ("Cannot open file " << GetFileName () << " for reading");
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -0800125 return m_nodes;
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700126 }
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800127
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800128 while (!topgen.eof ())
129 {
130 string line;
131 getline (topgen, line);
132
133 if (line == "router") break;
134 }
135
136 while (!topgen.eof ())
137 {
138 string line;
139 getline (topgen,line);
140 if (line[0] == '#') continue; // comments
141 if (line=="link") break; // stop reading nodes
142
143 istringstream lineBuffer (line);
144 string name, city;
145 double latitude, longitude;
146
147 lineBuffer >> name >> city >> latitude >> longitude;
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -0800148 if (name.empty ()) continue;
149
Alexander Afanasyev011b8592011-12-21 14:45:27 -0800150 Ptr<Node> node = CreateNode (name, m_scale*longitude, -m_scale*latitude);
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800151 }
152
153 map<string, set<string> > processedLinks; // to eliminate duplications
154
155 // SeekToSection ("link");
156 while (!topgen.eof ())
157 {
158 string line;
159 getline (topgen,line);
160 if (line == "") continue;
161 if (line[0] == '#') continue; // comments
162
163 // NS_LOG_DEBUG ("Input: [" << line << "]");
164
165 istringstream lineBuffer (line);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800166 string from, to, capacity, metric, delay, maxPackets;
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800167
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800168 lineBuffer >> from >> to >> capacity >> metric >> delay >> maxPackets;
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800169
170 if (processedLinks[to].size () != 0 &&
171 processedLinks[to].find (from) != processedLinks[to].end ())
172 {
173 continue; // duplicated link
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700174 }
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800175 processedLinks[from].insert (to);
176
177 Ptr<Node> fromNode = Names::Find<Node> (m_path, from);
178 NS_ASSERT (fromNode != 0);
179 Ptr<Node> toNode = Names::Find<Node> (m_path, to);
180 NS_ASSERT (fromNode != 0);
181
182 Link link (fromNode, from, toNode, to);
183
184 link.SetAttribute ("DataRate", capacity);
185 link.SetAttribute ("OSPF", metric);
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800186
187 if (!delay.empty ())
188 link.SetAttribute ("Delay", delay);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800189 if (!maxPackets.empty ())
190 link.SetAttribute ("MaxPackets", maxPackets);
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800191
192 AddLink (link);
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800193 NS_LOG_DEBUG ("New link " << from << " <==> " << to << " / " << capacity << " with " << metric << " metric (" << delay << ", " << maxPackets << ")");
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700194 }
195
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -0800196 NS_LOG_INFO ("Annotated topology created with " << m_nodes.GetN () << " nodes and " << LinksSize () << " links");
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800197 topgen.close ();
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700198
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800199 ApplySettings ();
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800200
Alexander Afanasyev5beb35a2011-12-21 16:45:13 -0800201 return m_nodes;
Ilya Moiseenko7dd43be2011-08-18 18:57:12 -0700202}
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700203
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800204void
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800205AnnotatedTopologyReader::AssignIpv4Addresses (Ipv4Address base)
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800206{
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800207 Ipv4AddressHelper address (base, Ipv4Mask ("/24"));
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700208
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800209 BOOST_FOREACH (const Link &link, m_linksList)
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800210 {
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800211 address.Assign (NetDeviceContainer (link.GetFromNetDevice (),
212 link.GetToNetDevice ()));
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700213
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800214 base = Ipv4Address (base.Get () + 256);
215 address.SetBase (base, Ipv4Mask ("/24"));
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800216 }
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700217
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800218 ApplyOspfMetric ();
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800219}
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700220
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800221void
222AnnotatedTopologyReader::ApplyOspfMetric ()
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800223{
Alexander Afanasyev0777f1c2011-12-12 20:22:45 -0800224 BOOST_FOREACH (const Link &link, m_linksList)
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800225 {
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800226 NS_LOG_DEBUG ("OSPF: " << link.GetAttribute ("OSPF"));
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800227 uint16_t metric = boost::lexical_cast<uint16_t> (link.GetAttribute ("OSPF"));
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700228
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800229 {
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800230 Ptr<Ipv4> ipv4 = link.GetFromNode ()->GetObject<Ipv4> ();
231 NS_ASSERT (ipv4 != 0);
Alexander Afanasyev0777f1c2011-12-12 20:22:45 -0800232
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800233 int32_t interfaceId = ipv4->GetInterfaceForDevice (link.GetFromNetDevice ());
234 NS_ASSERT (interfaceId >= 0);
Alexander Afanasyev0777f1c2011-12-12 20:22:45 -0800235
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800236 ipv4->SetMetric (interfaceId,metric);
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800237 }
Ilya Moiseenko1eff17d2011-08-17 10:55:53 -0700238
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800239 {
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800240 Ptr<Ipv4> ipv4 = link.GetToNode ()->GetObject<Ipv4> ();
241 NS_ASSERT (ipv4 != 0);
242
243 int32_t interfaceId = ipv4->GetInterfaceForDevice (link.GetToNetDevice ());
244 NS_ASSERT (interfaceId >= 0);
245
246 ipv4->SetMetric (interfaceId,metric);
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800247 }
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800248 }
249}
250
251void
252AnnotatedTopologyReader::ApplySettings ()
253{
254 PointToPointHelper p2p;
Ilya Moiseenko58d26672011-12-08 13:48:06 -0800255
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800256 BOOST_FOREACH (Link &link, m_linksList)
257 {
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800258 string tmp;
259
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800260 if (link.GetAttributeFailSafe ("DataRate", tmp))
261 {
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800262 NS_LOG_INFO ("DataRate = " + link.GetAttribute("DataRate"));
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800263 p2p.SetDeviceAttribute ("DataRate", StringValue (link.GetAttribute ("DataRate")));
Alexander Afanasyev7dbdcaf2011-12-13 21:40:37 -0800264 }
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800265
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800266 if (link.GetAttributeFailSafe ("Delay", tmp))
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800267 {
Alexander Afanasyev8f5a9bb2011-12-18 19:49:02 -0800268 NS_LOG_INFO ("Delay = " + link.GetAttribute("Delay"));
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800269 p2p.SetChannelAttribute ("Delay", StringValue (link.GetAttribute ("Delay")));
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800270 }
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800271
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800272 NetDeviceContainer nd = p2p.Install(link.GetFromNode (), link.GetToNode ());
273 link.SetNetDevices (nd.Get (0), nd.Get (1));
274
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800275 if (link.GetAttributeFailSafe ("MaxPackets", tmp))
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800276 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800277 NS_LOG_INFO ("MaxPackets = " + link.GetAttribute ("MaxPackets"));
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800278
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800279 PointerValue txQueue;
280
281 link.GetToNetDevice ()->GetAttribute ("TxQueue", txQueue);
282 NS_ASSERT (txQueue.Get<DropTailQueue> () != 0);
283 txQueue.Get<DropTailQueue> ()->SetAttribute ("MaxPackets", StringValue (link.GetAttribute ("MaxPackets")));
284
285 link.GetFromNetDevice ()->GetAttribute ("TxQueue", txQueue);
286 NS_ASSERT (txQueue.Get<DropTailQueue> () != 0);
287 txQueue.Get<DropTailQueue> ()->SetAttribute ("MaxPackets", StringValue (link.GetAttribute ("MaxPackets")));
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800288 }
Ilya Moiseenko7e14efa2011-12-12 17:56:22 -0800289
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800290 }
Alexander Afanasyev66e6fd72011-12-12 21:34:51 -0800291}
Alexander Afanasyev8633d5d2011-12-12 18:02:31 -0800292
293}