Almost working local example for linux ns-3 simulator
[nepi.git] / src / nepi / resources / ns3 / ns3netdevice.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
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 as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
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 from nepi.execution.attribute import Attribute, Flags
21 from nepi.execution.resource import clsinit_copy
22 from nepi.resources.ns3.ns3base import NS3Base
23
24 import ipaddr
25
26 @clsinit_copy
27 class NS3BaseNetDevice(NS3Base):
28     _rtype = "abstract::ns3::NetDevice"
29
30     @classmethod
31     def _register_attributes(cls):
32         mac = Attribute("mac", "MAC address for device",
33                 flags = Flags.Design)
34
35         ip = Attribute("ip", "IP address for device",
36                 flags = Flags.Design)
37
38         prefix = Attribute("prefix", "Network prefix for device",
39                 flags = Flags.Design)
40
41         cls._register_attribute(mac)
42         cls._register_attribute(ip)
43         cls._register_attribute(prefix)
44
45     @property
46     def node(self):
47         from nepi.resources.ns3.ns3node import NS3BaseNode
48         nodes = self.get_connected(NS3BaseNode.get_rtype())
49
50         if not nodes: 
51             msg = "Device not connected to node"
52             self.error(msg)
53             raise RuntimeError, msg
54
55         return nodes[0]
56
57     @property
58     def channel(self):
59         from nepi.resources.ns3.ns3channel import NS3BaseChannel
60         channels = self.get_connected(NS3BaseChannel.get_rtype())
61
62         if not channels: 
63             msg = "Device not connected to channel"
64             self.error(msg)
65             raise RuntimeError, msg
66
67         return channels[0]
68
69     @property
70     def _rms_to_wait(self):
71         others = set()
72         
73         node = self.node
74         others.add(node)
75
76         ipv4 = node.ipv4
77         if node.ipv4:
78             others.add(ipv4)
79
80         others.add(self.channel)
81         return others
82
83     def _configure_object(self):
84         # Set Mac
85         mac = self.get("mac")
86         if mac:
87             mac_uuid = self.simulator.create("Mac48Address", mac)
88         else:
89             mac_uuid = self.simulator.invoke("singleton::Mac48Address", "Allocate")
90         self.simulator.invoke(self.uuid, "SetAddress", mac_uuid)
91
92         # Set IP address
93         ip = self.get("ip")
94         prefix = self.get("prefix")
95
96         i = ipaddr.IPAddress(ip)
97         if i.version == 4:
98             # IPv4
99             ipv4 = self.node.ipv4
100             ifindex_uuid = self.simulator.invoke(ipv4.uuid, "AddInterface", 
101                     self.uuid)
102             ipv4_addr_uuid = self.simulator.create("Ipv4Address", ip)
103             ipv4_mask_uuid = self.simulator.create("Ipv4Mask", "/%s" % str(prefix))
104             inaddr_uuid = self.simulator.create("Ipv4InterfaceAddress", 
105                     ipv4_addr_uuid, ipv4_mask_uuid)
106             self.simulator.invoke(ipv4.uuid, "AddAddress", ifindex_uuid, 
107                     inaddr_uuid)
108             self.simulator.invoke(ipv4.uuid, "SetMetric", ifindex_uuid, 1)
109             self.simulator.invoke(ipv4.uuid, "SetUp", ifindex_uuid)
110         else:
111             # IPv6
112             # TODO!
113             pass
114
115     def _connect_object(self):
116         node = self.node
117         if node and node.uuid not in self.connected:
118             self.simulator.invoke(node.uuid, "AddDevice", self.uuid)
119             self._connected.add(node.uuid)
120
121         channel = self.channel
122         if channel and channel.uuid not in self.connected:
123             self.simulator.invoke(self.uuid, "Attach", channel.uuid)
124             self._connected.add(channel.uuid)
125