aea015b04d08fab6aa5044f8e83a783ecc0ec65f
[nepi.git] / test / resources / linux / ns3 / ns3client.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.linux.ns3.ns3client 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         if os.path.exists(self.socket_name):
45             os.remove(self.socket_name) 
46
47     def tearDown(self):
48         os.remove(self.socket_name) 
49
50     def test_runtime_attr_modify(self):
51         thread = threading.Thread(target = run_server,
52                 args = [self.socket_name])
53
54         thread.setDaemon(True)
55         thread.start()
56
57         time.sleep(3)
58
59         # Verify that the communication socket was created
60         self.assertTrue(os.path.exists(self.socket_name))
61
62         # Instantiate the NS3 client
63         client = LinuxNS3Client(self.socket_name)
64  
65         # Define a real time simulation 
66         stype = client.create("StringValue", "ns3::RealtimeSimulatorImpl")
67         client.invoke("singleton::GlobalValue", "Bind", "SimulatorImplementationType", stype)
68         btrue = client.create("BooleanValue", True)
69         client.invoke("singleton::GlobalValue", "Bind", "ChecksumEnabled", btrue)
70         
71         # Create Node
72         n1 = client.create("Node")
73         self.assertTrue(n1.startswith("uuid"))
74
75         ## Install internet stack
76         ipv41 = client.create("Ipv4L3Protocol")
77         client.invoke(n1, "AggregateObject", ipv41)
78
79         arp1 = client.create("ArpL3Protocol")
80         client.invoke(n1, "AggregateObject", arp1)
81         
82         icmp1 = client.create("Icmpv4L4Protocol")
83         client.invoke(n1, "AggregateObject", icmp1)
84
85         ## Add IPv4 routing
86         lr1 = client.create("Ipv4ListRouting")
87         client.invoke(ipv41, "SetRoutingProtocol", lr1)
88         sr1 = client.create("Ipv4StaticRouting")
89         client.invoke(lr1, "AddRoutingProtocol", sr1, 1)
90
91         ## NODE 2
92         n2 = client.create("Node")
93
94         ## Install internet stack
95         ipv42 = client.create("Ipv4L3Protocol")
96         client.invoke(n2, "AggregateObject", ipv42)
97
98         arp2 = client.create("ArpL3Protocol")
99         client.invoke(n2, "AggregateObject", arp2)
100         
101         icmp2 = client.create("Icmpv4L4Protocol")
102         client.invoke(n2, "AggregateObject", icmp2)
103
104         ## Add IPv4 routing
105         lr2 = client.create("Ipv4ListRouting")
106         client.invoke(ipv42, "SetRoutingProtocol", lr2)
107         sr2 = client.create("Ipv4StaticRouting")
108         client.invoke(lr2, "AddRoutingProtocol", sr2, 1)
109
110         ##### Create p2p device and enable ascii tracing
111         p2pHelper = client.create("PointToPointHelper")
112         asciiHelper = client.create("AsciiTraceHelper")
113
114         # Iface for node1
115         p1 = client.create("PointToPointNetDevice")
116         client.invoke(n1, "AddDevice", p1)
117         q1 = client.create("DropTailQueue")
118         client.invoke(p1, "SetQueue", q1)
119       
120         # Add IPv4 address
121         ifindex1 = client.invoke(ipv41, "AddInterface", p1)
122         mask1 = client.create("Ipv4Mask", "/30")
123         addr1 = client.create("Ipv4Address", "10.0.0.1")
124         inaddr1 = client.create("Ipv4InterfaceAddress", addr1, mask1)
125         client.invoke(ipv41, "AddAddress", ifindex1, inaddr1)
126         client.invoke(ipv41, "SetMetric", ifindex1, 1)
127         client.invoke(ipv41, "SetUp", ifindex1)
128
129         # Enable collection of Ascii format to a specific file
130         filepath1 = "trace-p2p-1.tr"
131         stream1 = client.invoke(asciiHelper, "CreateFileStream", filepath1)
132         client.invoke(p2pHelper, "EnableAscii", stream1, p1)
133        
134         # Iface for node2
135         p2 = client.create("PointToPointNetDevice")
136         client.invoke(n2, "AddDevice", p2)
137         q2 = client.create("DropTailQueue")
138         client.invoke(p2, "SetQueue", q2)
139
140         # Add IPv4 address
141         ifindex2 = client.invoke(ipv42, "AddInterface", p2)
142         mask2 = client.create("Ipv4Mask", "/30")
143         addr2 = client.create("Ipv4Address", "10.0.0.2")
144         inaddr2 = client.create("Ipv4InterfaceAddress", addr2, mask2)
145         client.invoke(ipv42, "AddAddress", ifindex2, inaddr2)
146         client.invoke(ipv42, "SetMetric", ifindex2, 1)
147         client.invoke(ipv42, "SetUp", ifindex2)
148
149         # Enable collection of Ascii format to a specific file
150         filepath2 = "trace-p2p-2.tr"
151         stream2 = client.invoke(asciiHelper, "CreateFileStream", filepath2)
152         client.invoke(p2pHelper, "EnableAscii", stream2, p2)
153
154         # Create channel
155         chan = client.create("PointToPointChannel")
156         client.set(chan, "Delay", "0s")
157         client.invoke(p1, "Attach", chan)
158         client.invoke(p2, "Attach", chan)
159
160         ### create pinger
161         ping = client.create("V4Ping")
162         client.invoke(n1, "AddApplication", ping)
163         client.set (ping, "Remote", "10.0.0.2")
164         client.set (ping, "Interval", "1s")
165         client.set (ping, "Verbose", True)
166         client.set (ping, "StartTime", "0s")
167         client.set (ping, "StopTime", "20s")
168
169         ### run Simulation
170         client.stop(time = "21s")
171         client.start()
172
173         time.sleep(1)
174
175         client.set(chan, "Delay", "5s")
176
177         time.sleep(5)
178
179         client.set(chan, "Delay", "0s")
180
181         # wait until simulation is over
182         client.shutdown()
183
184
185 if __name__ == '__main__':
186     unittest.main()
187