NS3Wrapper server and client
[nepi.git] / test / resources / ns3 / linuxns3client.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20
21
22 # Test based on ns-3 csma/examples/csma-ping.cc file
23 #
24 # Network topology
25 #
26 #       n0    n1   n2   n3
27 #       |     |    |    |
28 #       -----------------
29 #
30 #  node n0 sends IGMP traffic to node n3
31
32
33 from nepi.resources.ns3.ns3server import run_server
34 from nepi.resources.ns3.linuxns3client import LinuxNS3Client
35
36 import os
37 import threading
38 import time
39 import unittest
40
41 class LinuxNS3ClientTest(unittest.TestCase):
42     def setUp(self):
43         self.socket_name = os.path.join("/", "tmp", "NS3WrapperServer.sock")
44
45     def tearDown(self):
46         os.remove(self.socket_name) 
47
48     def test_runtime_attr_modify(self):
49         thread = threading.Thread(target = run_server,
50                 args = [self.socket_name])
51
52         thread.setDaemon(True)
53         thread.start()
54
55         time.sleep(3)
56
57         # Verify that the communication socket was created
58         self.assertTrue(os.path.exists(self.socket_name))
59
60         # Instantiate the NS3 client
61         client = LinuxNS3Client(self.socket_name)
62  
63         # Define a real time simulation 
64         stype = client.create("StringValue", "ns3::RealtimeSimulatorImpl")
65         client.invoke("singleton::GlobalValue", "Bind", "SimulatorImplementationType", stype)
66         btrue = client.create("BooleanValue", True)
67         client.invoke("singleton::GlobalValue", "Bind", "ChecksumEnabled", btrue)
68         
69         # Create Node
70         n1 = client.create("Node")
71         self.assertTrue(n1.startswith("uuid"))
72
73         ## Install internet stack
74         ipv41 = client.create("Ipv4L3Protocol")
75         client.invoke(n1, "AggregateObject", ipv41)
76
77         arp1 = client.create("ArpL3Protocol")
78         client.invoke(n1, "AggregateObject", arp1)
79         
80         icmp1 = client.create("Icmpv4L4Protocol")
81         client.invoke(n1, "AggregateObject", icmp1)
82
83         ## Add IPv4 routing
84         lr1 = client.create("Ipv4ListRouting")
85         client.invoke(ipv41, "SetRoutingProtocol", lr1)
86         sr1 = client.create("Ipv4StaticRouting")
87         client.invoke(lr1, "AddRoutingProtocol", sr1, 1)
88
89         ## NODE 2
90         n2 = client.create("Node")
91
92         ## Install internet stack
93         ipv42 = client.create("Ipv4L3Protocol")
94         client.invoke(n2, "AggregateObject", ipv42)
95
96         arp2 = client.create("ArpL3Protocol")
97         client.invoke(n2, "AggregateObject", arp2)
98         
99         icmp2 = client.create("Icmpv4L4Protocol")
100         client.invoke(n2, "AggregateObject", icmp2)
101
102         ## Add IPv4 routing
103         lr2 = client.create("Ipv4ListRouting")
104         client.invoke(ipv42, "SetRoutingProtocol", lr2)
105         sr2 = client.create("Ipv4StaticRouting")
106         client.invoke(lr2, "AddRoutingProtocol", sr2, 1)
107
108         ##### Create p2p device and enable ascii tracing
109         p2pHelper = client.create("PointToPointHelper")
110         asciiHelper = client.create("AsciiTraceHelper")
111
112         # Iface for node1
113         p1 = client.create("PointToPointNetDevice")
114         client.invoke(n1, "AddDevice", p1)
115         q1 = client.create("DropTailQueue")
116         client.invoke(p1, "SetQueue", q1)
117       
118         # Add IPv4 address
119         ifindex1 = client.invoke(ipv41, "AddInterface", p1)
120         mask1 = client.create("Ipv4Mask", "/30")
121         addr1 = client.create("Ipv4Address", "10.0.0.1")
122         inaddr1 = client.create("Ipv4InterfaceAddress", addr1, mask1)
123         client.invoke(ipv41, "AddAddress", ifindex1, inaddr1)
124         client.invoke(ipv41, "SetMetric", ifindex1, 1)
125         client.invoke(ipv41, "SetUp", ifindex1)
126
127         # Enable collection of Ascii format to a specific file
128         filepath1 = "trace-p2p-1.tr"
129         stream1 = client.invoke(asciiHelper, "CreateFileStream", filepath1)
130         client.invoke(p2pHelper, "EnableAscii", stream1, p1)
131        
132         # Iface for node2
133         p2 = client.create("PointToPointNetDevice")
134         client.invoke(n2, "AddDevice", p2)
135         q2 = client.create("DropTailQueue")
136         client.invoke(p2, "SetQueue", q2)
137
138         # Add IPv4 address
139         ifindex2 = client.invoke(ipv42, "AddInterface", p2)
140         mask2 = client.create("Ipv4Mask", "/30")
141         addr2 = client.create("Ipv4Address", "10.0.0.2")
142         inaddr2 = client.create("Ipv4InterfaceAddress", addr2, mask2)
143         client.invoke(ipv42, "AddAddress", ifindex2, inaddr2)
144         client.invoke(ipv42, "SetMetric", ifindex2, 1)
145         client.invoke(ipv42, "SetUp", ifindex2)
146
147         # Enable collection of Ascii format to a specific file
148         filepath2 = "trace-p2p-2.tr"
149         stream2 = client.invoke(asciiHelper, "CreateFileStream", filepath2)
150         client.invoke(p2pHelper, "EnableAscii", stream2, p2)
151
152         # Create channel
153         chan = client.create("PointToPointChannel")
154         client.set(chan, "Delay", "0s")
155         client.invoke(p1, "Attach", chan)
156         client.invoke(p2, "Attach", chan)
157
158         ### create pinger
159         ping = client.create("V4Ping")
160         client.invoke(n1, "AddApplication", ping)
161         client.set (ping, "Remote", "10.0.0.2")
162         client.set (ping, "Interval", "1s")
163         client.set (ping, "Verbose", True)
164         client.set (ping, "StartTime", "0s")
165         client.set (ping, "StopTime", "20s")
166
167         ### run Simulation
168         client.stop(time = "21s")
169         client.start()
170
171         time.sleep(1)
172
173         client.set(chan, "Delay", "5s")
174
175         time.sleep(5)
176
177         client.set(chan, "Delay", "0s")
178
179         # wait until simulation is over
180         client.shutdown()
181
182
183 if __name__ == '__main__':
184     unittest.main()
185