applied the except and raise fixers to the master branch to close the gap with py3
[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 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, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 from nepi.execution.attribute import Attribute, Flags
20 from nepi.execution.resource import clsinit_copy
21 from nepi.execution.trace import Trace
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     @classmethod
46     def _register_traces(cls):
47         pcap = Trace("pcap", "Dump traffic sniffed on the network device in Pcap format")
48         promisc_pcap = Trace("promiscPcap", "Dump traffic sniffed in promiscuous mode on the network device in Pcap format")
49         ascii = Trace("ascii", "Dump traffic sniffed on the network device in Ascii format")
50
51         cls._register_trace(pcap)
52         cls._register_trace(promisc_pcap)
53         cls._register_trace(ascii)
54
55     def __init__(self, ec, guid):
56         super(NS3BaseNetDevice, self).__init__(ec, guid)
57         self._ascii_helper_uuid = None
58         self._device_helper_uuid = None
59
60     @property
61     def node(self):
62         from nepi.resources.ns3.ns3node import NS3BaseNode
63         nodes = self.get_connected(NS3BaseNode.get_rtype())
64
65         if not nodes: 
66             msg = "Device not connected to node"
67             self.error(msg)
68             raise RuntimeError(msg)
69
70         return nodes[0]
71
72     @property
73     def channel(self):
74         from nepi.resources.ns3.ns3channel import NS3BaseChannel
75         channels = self.get_connected(NS3BaseChannel.get_rtype())
76
77         if not channels: 
78             msg = "Device not connected to channel"
79             self.error(msg)
80             raise RuntimeError(msg)
81
82         return channels[0]
83
84     @property
85     def queue(self):
86         from nepi.resources.ns3.ns3queue import NS3BaseQueue
87         queue = self.get_connected(NS3BaseQueue.get_rtype())
88
89         if not queue: 
90             msg = "Device not connected to queue"
91             self.error(msg)
92             raise RuntimeError(msg)
93
94         return queue[0]
95
96     @property
97     def ascii_helper_uuid(self):
98         if not self._ascii_helper_uuid:
99             self._ascii_helper_uuid = self.simulation.create("AsciiTraceHelper")
100         return self._ascii_helper_uuid
101
102     @property
103     def device_helper_uuid(self):
104         if not self._device_helper_uuid:
105             rtype = self.get_rtype()
106             if rtype == "ns3::PointToPointNetDevice":
107                 classname = "PointToPointHelper"
108             elif rtype == "ns3::CsmaNetDevice":
109                 classname = "CsmaHelper"
110             elif rtype == "ns3::EmuNetDevice":
111                 classname = "EmuHelper"
112             elif rtype == "ns3::FdNetDevice":
113                 classname = "FdNetDeviceHelper"
114             elif rtype in [ "ns3::BaseStationNetDevice", "SubscriberStationNetDevice" ]:
115                 classname = "WimaxHelper"
116             elif rtype == "ns3::WifiNetDevice":
117                 classname = "YansWifiPhyHelper"
118             elif rtype == "ns3::FdNetDevice":
119                 classname = "FdNetDeviceHelper"
120
121             self._device_helper_uuid = self.simulation.create(classname)
122
123         return self._device_helper_uuid
124
125     @property
126     def _rms_to_wait(self):
127         rms = set([self.node, self.channel])
128         return rms
129
130     def _configure_object(self):
131         # Set Mac
132         self._configure_mac_address()
133
134         # Set IP address
135         self._configure_ip_address()
136         
137         # Enable traces
138         self._configure_traces()
139
140     def _configure_mac_address(self):
141         mac = self.get("mac")
142         if mac:
143             mac_uuid = self.simulation.create("Mac48Address", mac)
144         else:
145             mac_uuid = self.simulation.invoke("singleton::Mac48Address", "Allocate")
146
147         self.simulation.invoke(self.uuid, "SetAddress", mac_uuid)
148
149     def _configure_ip_address(self):
150         ip = self.get("ip")
151         prefix = self.get("prefix")
152
153         i = ipaddr.IPAddress(ip)
154         if i.version == 4:
155             # IPv4
156             ipv4 = self.node.ipv4
157             ifindex_uuid = self.simulation.invoke(ipv4.uuid, "AddInterface", 
158                     self.uuid)
159             ipv4_addr_uuid = self.simulation.create("Ipv4Address", ip)
160             ipv4_mask_uuid = self.simulation.create("Ipv4Mask", "/%s" % str(prefix))
161             inaddr_uuid = self.simulation.create("Ipv4InterfaceAddress", 
162                     ipv4_addr_uuid, ipv4_mask_uuid)
163             self.simulation.invoke(ipv4.uuid, "AddAddress", ifindex_uuid, 
164                     inaddr_uuid)
165             self.simulation.invoke(ipv4.uuid, "SetMetric", ifindex_uuid, 1)
166             self.simulation.invoke(ipv4.uuid, "SetUp", ifindex_uuid)
167         else:
168             # IPv6
169             # TODO!
170             pass
171
172     def _configure_traces(self):
173         if self.trace_enabled("pcap"):
174             helper_uuid = self.device_helper_uuid
175
176             filename = "trace-pcap-netdev-%d.pcap" % self.guid
177             self._trace_filename["pcap"] = filename
178
179             filepath = self.simulation.trace_filepath(filename)
180
181             self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
182                     self.uuid, promiscuous = False, explicitFilename = True)
183
184         if self.trace_enabled("promiscPcap"):
185             helper_uuid = self.device_helper_uuid
186
187             filename = "trace-promisc-pcap-netdev-%d.pcap" % self.guid
188             self._trace_filename["promiscPcap"] = filename
189
190             filepath = self.simulation.trace_filepath(filename)
191
192             self.simulation.invoke(helper_uuid, "EnablePcap", filepath, 
193                     self.uuid, promiscuous = True, explicitFilename = True)
194
195         if self.trace_enabled("ascii"):
196             helper_uuid = self.device_helper_uuid
197             ascii_helper_uuid = self.ascii_helper_uuid
198
199             filename = "trace-ascii-netdev-%d.tr" % self.guid
200             self._trace_filename["ascii"] = filename
201
202             filepath = self.simulation.trace_filepath(filename)
203             stream_uuid = self.simulation.invoke(ascii_helper_uuid, 
204                     "CreateFileStream", filepath) 
205             self.simulation.invoke(helper_uuid, "EnableAscii", stream_uuid,
206                     self.uuid)
207
208     def _connect_object(self):
209         node = self.node
210         if node and node.uuid not in self.connected:
211             self.simulation.invoke(node.uuid, "AddDevice", self.uuid)
212             self._connected.add(node.uuid)
213
214         channel = self.channel
215         if channel and channel.uuid not in self.connected:
216             self.simulation.invoke(self.uuid, "Attach", channel.uuid)
217             self._connected.add(channel.uuid)
218         
219         # Verify that the device has a queue. If no queue is added a segfault 
220         # error occurs
221         queue = self.queue
222