A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
test-lte-handover-target.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Budiarto Herman
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: Budiarto Herman <budiarto.herman@magister.fi>
19  *
20  */
21 
22 #include <ns3/test.h>
23 
24 #include <ns3/log.h>
25 #include <ns3/nstime.h>
26 #include <ns3/callback.h>
27 #include <ns3/config.h>
28 #include <ns3/boolean.h>
29 #include <ns3/double.h>
30 #include <ns3/uinteger.h>
31 #include <ns3/simulator.h>
32 
33 #include <ns3/node-container.h>
34 #include <ns3/net-device-container.h>
35 #include <ns3/ipv4-interface-container.h>
36 
37 #include <ns3/lte-helper.h>
38 #include <ns3/point-to-point-epc-helper.h>
39 #include <ns3/internet-stack-helper.h>
40 #include <ns3/point-to-point-helper.h>
41 #include <ns3/ipv4-address-helper.h>
42 #include <ns3/ipv4-static-routing-helper.h>
43 #include <ns3/mobility-helper.h>
44 
45 #include <ns3/data-rate.h>
46 #include <ns3/ipv4-static-routing.h>
47 #include <ns3/position-allocator.h>
48 #include <ns3/lte-enb-net-device.h>
49 #include <ns3/lte-enb-phy.h>
50 
51 NS_LOG_COMPONENT_DEFINE ("LteHandoverTargetTest");
52 
53 namespace ns3 {
54 
55 
68 {
69 public:
88  LteHandoverTargetTestCase (std::string name, Vector uePosition,
89  uint8_t gridSizeX, uint8_t gridSizeY,
90  uint16_t sourceCellId, uint16_t targetCellId,
91  std::string handoverAlgorithmType);
92 
93  virtual ~LteHandoverTargetTestCase ();
94 
102  void HandoverStartCallback (std::string context, uint64_t imsi,
103  uint16_t sourceCellId, uint16_t rnti,
104  uint16_t targetCellId);
105 
110  void CellShutdownCallback ();
111 
112 private:
117  virtual void DoRun ();
118 
123  virtual void DoTeardown ();
124 
125  // simulation parameters
127  uint8_t m_gridSizeX;
128  uint8_t m_gridSizeY;
129  uint16_t m_sourceCellId;
130  uint16_t m_targetCellId;
132 
135 
136 }; // end of class LteHandoverTargetTestCase
137 
138 
140  uint8_t gridSizeX, uint8_t gridSizeY,
141  uint16_t sourceCellId, uint16_t targetCellId,
142  std::string handoverAlgorithmType)
143  : TestCase (name),
144  m_uePosition (uePosition),
145  m_gridSizeX (gridSizeX),
146  m_gridSizeY (gridSizeY),
147  m_sourceCellId (sourceCellId),
148  m_targetCellId (targetCellId),
149  m_handoverAlgorithmType (handoverAlgorithmType),
150  m_sourceEnbDev (0),
151  m_hasHandoverOccurred (false)
152 {
153  NS_LOG_INFO (this << " name=" << name);
154 
155  // SANITY CHECK
156 
157  uint16_t nEnb = gridSizeX * gridSizeY;
158 
159  if (sourceCellId > nEnb)
160  {
161  NS_FATAL_ERROR ("Invalid source cell ID " << sourceCellId);
162  }
163 
164  if (targetCellId > nEnb)
165  {
166  NS_FATAL_ERROR ("Invalid target cell ID " << targetCellId);
167  }
168 }
169 
170 
172 {
173  NS_LOG_FUNCTION (this);
174 }
175 
176 
177 void
178 LteHandoverTargetTestCase::HandoverStartCallback (std::string context, uint64_t imsi,
179  uint16_t sourceCellId, uint16_t rnti,
180  uint16_t targetCellId)
181 {
182  NS_LOG_FUNCTION (this << context << imsi << sourceCellId << rnti << targetCellId);
183 
184  uint64_t timeNowMs = Simulator::Now ().GetMilliSeconds ();
185  NS_TEST_ASSERT_MSG_GT (timeNowMs, 500,
186  "Handover occured but too early");
187  NS_TEST_ASSERT_MSG_EQ (sourceCellId, m_sourceCellId,
188  "Handover occured but with wrong source cell");
189  NS_TEST_ASSERT_MSG_EQ (targetCellId, m_targetCellId,
190  "Handover occured but with wrong target cell");
191  m_hasHandoverOccurred = true;
192 }
193 
194 
195 void
197 {
198  NS_LOG_FUNCTION (this);
199 
200  if (m_sourceEnbDev != 0)
201  {
202  // set the Tx power to 1 dBm
203  NS_ASSERT (m_sourceEnbDev->GetCellId () == m_sourceCellId);
204  NS_LOG_INFO ("Shutting down cell " << m_sourceCellId);
205  Ptr<LteEnbPhy> phy = m_sourceEnbDev->GetPhy ();
206  phy->SetTxPower (1);
207  }
208 }
209 
210 
211 void
213 {
214  NS_LOG_INFO (this << " " << GetName ());
215 
216  Config::SetDefault ("ns3::LteEnbPhy::TxPower", DoubleValue (38)); // micro cell
217  Config::SetDefault ("ns3::LteSpectrumPhy::CtrlErrorModelEnabled",
218  BooleanValue (false)); // disable control channel error model
219 
220  Ptr<LteHelper> lteHelper = CreateObject<LteHelper> ();
221  Ptr<PointToPointEpcHelper> epcHelper = CreateObject<PointToPointEpcHelper> ();
222  lteHelper->SetEpcHelper (epcHelper);
223  lteHelper->SetAttribute ("PathlossModel",
224  StringValue ("ns3::FriisSpectrumPropagationLossModel"));
225  lteHelper->SetAttribute ("UseIdealRrc", BooleanValue (true));
226 
227  if (m_handoverAlgorithmType == "ns3::A2A4RsrqHandoverAlgorithm")
228  {
229  lteHelper->SetHandoverAlgorithmType ("ns3::A2A4RsrqHandoverAlgorithm");
230  lteHelper->SetHandoverAlgorithmAttribute ("ServingCellThreshold",
231  UintegerValue (30));
232  lteHelper->SetHandoverAlgorithmAttribute ("NeighbourCellOffset",
233  UintegerValue (1));
234  }
235  else if (m_handoverAlgorithmType == "ns3::A3RsrpHandoverAlgorithm")
236  {
237  lteHelper->SetHandoverAlgorithmType ("ns3::A3RsrpHandoverAlgorithm");
238  lteHelper->SetHandoverAlgorithmAttribute ("Hysteresis",
239  DoubleValue (1.5));
240  lteHelper->SetHandoverAlgorithmAttribute ("TimeToTrigger",
241  TimeValue (MilliSeconds (128)));
242  }
243  else
244  {
245  NS_FATAL_ERROR ("Unknown handover algorithm " << m_handoverAlgorithmType);
246  }
247 
248  // Create Nodes: eNodeB and UE
249  NodeContainer enbNodes;
250  NodeContainer ueNodes;
251  enbNodes.Create (m_gridSizeX * m_gridSizeY);
252  ueNodes.Create (1);
253 
254  /*
255  * The size of the grid is determined by m_gridSizeX and m_gridSizeY. The
256  * following figure is the topology when m_gridSizeX = 4 and m_gridSizeY = 3.
257  *
258  * 9 -- 10 -- 11 -- 12
259  * | | | |
260  * | | | |
261  * 5 --- 6 --- 7 --- 8
262  * | | | |
263  * | | | |
264  * (0, 0, 0) ---> 1 --- 2 --- 3 --- 4
265  *
266  * The grid starts at (0, 0, 0) point on the bottom left corner. The distance
267  * between two adjacent eNodeBs is 130 m.
268  */
269 
270  // Set up eNodeB position
271  MobilityHelper enbMobility;
272  enbMobility.SetPositionAllocator ("ns3::GridPositionAllocator",
273  "MinX", DoubleValue (0.0),
274  "MinY", DoubleValue (0.0),
275  "DeltaX", DoubleValue (130.0),
276  "DeltaY", DoubleValue (130.0),
277  "GridWidth", UintegerValue (m_gridSizeX),
278  "LayoutType", StringValue ("RowFirst"));
279  enbMobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
280  enbMobility.Install (enbNodes);
281 
282  // Setup UE position
283  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
284  positionAlloc->Add (m_uePosition);
285  MobilityHelper ueMobility;
286  ueMobility.SetPositionAllocator (positionAlloc);
287  ueMobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
288  ueMobility.Install (ueNodes);
289 
290  // Create P-GW node
291  Ptr<Node> pgw = epcHelper->GetPgwNode ();
292 
293  // Create a single RemoteHost
294  NodeContainer remoteHostContainer;
295  remoteHostContainer.Create (1);
296  Ptr<Node> remoteHost = remoteHostContainer.Get (0);
297  InternetStackHelper internet;
298  internet.Install (remoteHostContainer);
299 
300  // Create the Internet
301  PointToPointHelper p2ph;
302  p2ph.SetDeviceAttribute ("DataRate", DataRateValue (DataRate ("100Gb/s")));
303  p2ph.SetDeviceAttribute ("Mtu", UintegerValue (1500));
304  p2ph.SetChannelAttribute ("Delay", TimeValue (Seconds (0.010)));
305  NetDeviceContainer internetDevices = p2ph.Install (pgw, remoteHost);
306  Ipv4AddressHelper ipv4h;
307  ipv4h.SetBase ("1.0.0.0", "255.0.0.0");
308  Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign (internetDevices);
309 
310  // Routing of the Internet Host (towards the LTE network)
311  Ipv4StaticRoutingHelper ipv4RoutingHelper;
312  Ptr<Ipv4StaticRouting> remoteHostStaticRouting = ipv4RoutingHelper.GetStaticRouting (remoteHost->GetObject<Ipv4> ());
313  remoteHostStaticRouting->AddNetworkRouteTo (Ipv4Address ("7.0.0.0"), Ipv4Mask ("255.0.0.0"), 1);
314 
315  // Create Devices and install them in the Nodes (eNB and UE)
316  NetDeviceContainer enbDevs;
317  NetDeviceContainer ueDevs;
318  enbDevs = lteHelper->InstallEnbDevice (enbNodes);
319  ueDevs = lteHelper->InstallUeDevice (ueNodes);
320 
321  // Install the IP stack on the UEs
322  internet.Install (ueNodes);
323  Ipv4InterfaceContainer ueIpIfaces;
324  ueIpIfaces = epcHelper->AssignUeIpv4Address (NetDeviceContainer (ueDevs));
325 
326  // Assign IP address to UEs
327  for (uint32_t u = 0; u < ueNodes.GetN (); ++u)
328  {
329  Ptr<Node> ueNode = ueNodes.Get (u);
330  // Set the default gateway for the UE
331  Ptr<Ipv4StaticRouting> ueStaticRouting = ipv4RoutingHelper.GetStaticRouting (ueNode->GetObject<Ipv4> ());
332  ueStaticRouting->SetDefaultRoute (epcHelper->GetUeDefaultGatewayAddress (), 1);
333  }
334 
335  // Add X2 interface
336  lteHelper->AddX2Interface (enbNodes);
337 
338  // Connect to trace sources in all eNodeB
339  Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverStart",
341  this));
342 
343  // Get the source eNodeB
344  Ptr<NetDevice> sourceEnb = enbDevs.Get (m_sourceCellId - 1);
345  m_sourceEnbDev = sourceEnb->GetObject<LteEnbNetDevice> ();
346  NS_ASSERT (m_sourceEnbDev != 0);
347  NS_ASSERT (m_sourceEnbDev->GetCellId () == m_sourceCellId);
348 
349  // Attach UE to the source eNodeB
350  lteHelper->Attach (ueDevs.Get (0), sourceEnb);
351 
352  // Schedule a "shutdown" of the source eNodeB
353  Simulator::Schedule (Seconds (0.5),
355 
356  // Run simulation
357  Simulator::Stop (Seconds (1));
358  Simulator::Run ();
360 
361 } // end of void LteX2HandoverTargetTestCase::DoRun ()
362 
363 
364 void
366 {
367  NS_LOG_FUNCTION (this);
368  NS_TEST_ASSERT_MSG_EQ (m_hasHandoverOccurred, true, "Handover did not occur");
369 }
370 
371 
372 
382 {
383 public:
385 };
386 
387 
389  : TestSuite ("lte-handover-target", SYSTEM)
390 {
391  // LogComponentEnable ("LteHandoverTargetTest", LOG_PREFIX_ALL);
392  // LogComponentEnable ("LteHandoverTargetTest", LOG_LEVEL_ALL);
393  // LogComponentEnable ("A2A4RsrqHandoverAlgorithm", LOG_PREFIX_ALL);
394  // LogComponentEnable ("A2A4RsrqHandoverAlgorithm", LOG_LEVEL_ALL);
395  // LogComponentEnable ("A3RsrpHandoverAlgorithm", LOG_PREFIX_ALL);
396  // LogComponentEnable ("A3RsrpHandoverAlgorithm", LOG_LEVEL_ALL);
397 
398  /*
399  * 3 --- 4
400  * | |
401  * |o |
402  * 1 --- 2 o = UE
403  */
404  AddTestCase (new LteHandoverTargetTestCase ("4 cells and A2-A4-RSRQ algorithm",
405  Vector (20, 40, 0), 2, 2, 1, 3,
406  "ns3::A2A4RsrqHandoverAlgorithm"),
408  AddTestCase (new LteHandoverTargetTestCase ("4 cells and strongest cell algorithm",
409  Vector (20, 40, 0), 2, 2, 1, 3,
410  "ns3::A3RsrpHandoverAlgorithm"),
412 
413  /*
414  * 4 --- 5 --- 6
415  * | |o |
416  * | | |
417  * 1 --- 2 --- 3 o = UE
418  */
419  AddTestCase (new LteHandoverTargetTestCase ("6 cells and A2-A4-RSRQ algorithm",
420  Vector (150, 90, 0), 3, 2, 5, 2,
421  "ns3::A2A4RsrqHandoverAlgorithm"),
423  AddTestCase (new LteHandoverTargetTestCase ("6 cells and strongest cell algorithm",
424  Vector (150, 90, 0), 3, 2, 5, 2,
425  "ns3::A3RsrpHandoverAlgorithm"),
427 
428 } // end of LteHandoverTargetTestSuite ()
429 
430 
432 
433 
434 } // end of namespace ns3
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Test suite lte-handover-target, verifying that handover algorithms are able to select the right targe...
Hold a bool native type.
Definition: boolean.h:38
holds a vector of std::pair of Ptr and interface index.
static LteHandoverTargetTestSuite g_lteHandoverTargetTestSuiteInstance
void CellShutdownCallback()
A trigger that can be scheduled to "shutdown" the cell pointed by m_sourceCellId by reducing its powe...
hold variables of type string
Definition: string.h:19
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
NetDeviceContainer Install(NodeContainer c)
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
A suite of tests to run.
Definition: test.h:1025
#define NS_ASSERT(condition)
Definition: assert.h:64
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Definition: log.h:298
Build a set of PointToPointNetDevice objects.
encapsulates test code
Definition: test.h:849
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
a 3d vector
Definition: vector.h:31
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
Class for representing data rates.
Definition: data-rate.h:71
#define NS_TEST_ASSERT_MSG_GT(actual, limit, msg)
Test that an actual value is greater than a limit and report and abort if not.
Definition: test.h:773
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
hold objects of type ns3::Time
Definition: nstime.h:961
Hold an unsigned integer type.
Definition: uinteger.h:46
Medium length test.
Definition: test.h:858
holds a vector of ns3::NetDevice pointers
virtual void DoTeardown()
Called at the end of simulation and verifies that a handover has occurred in the simulation.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void HandoverStartCallback(std::string context, uint64_t imsi, uint16_t sourceCellId, uint16_t rnti, uint16_t targetCellId)
Triggers when an eNodeB starts a handover and then verifies that the handover has the right source an...
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
virtual void DoRun()
Run a simulation of a micro-cell network using the parameters provided to the constructor function...
keep track of a set of node pointers.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
Fast test.
Definition: test.h:857
LteHandoverTargetTestCase(std::string name, Vector uePosition, uint8_t gridSizeX, uint8_t gridSizeY, uint16_t sourceCellId, uint16_t targetCellId, std::string handoverAlgorithmType)
Construct a new test case and providing input parameters for the simulation.
Helper class used to assign positions and mobility models to nodes.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
Helper class that adds ns3::Ipv4StaticRouting objects.
hold objects of type ns3::DataRate
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
NS_LOG_COMPONENT_DEFINE("LteHandoverTargetTest")
std::string GetName(void) const
Definition: test.cc:242
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
Hold a floating point type.
Definition: double.h:41
The eNodeB device implementation.
Ptr< T > GetObject(void) const
Definition: object.h:361
int64_t GetMilliSeconds(void) const
Definition: nstime.h:283
Testing a handover algorithm, verifying that it selects the right target cell when more than one opti...
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:137