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