Ignoring 'abstract' RMs upon ResourceFactory discover
[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 @clsinit_copy
25 class NS3BaseNetDevice(NS3Base):
26     _rtype = "abstract::ns3::NetDevice"
27
28     @classmethod
29     def _register_attributes(cls):
30         mac = Attribute("mac", "MAC address for device",
31                 flags = Flags.ExecReadOnly)
32
33         ip = Attribute("ip", "IP address for device",
34                 flags = Flags.ExecReadOnly)
35
36         prefix = Attribute("prefix", "Network prefix for device",
37                 flags = Flags.ExecReadOnly)
38
39         cls._register_attribute(mac)
40         cls._register_attribute(ip)
41         cls._register_attribute(prefix)
42
43     @property
44     def channel(self):
45         from nepi.resources.ns3.ns3channel import NS3Channel
46         channels = self.get_connected(NS3BaseChannel.get_rtype())
47         if channels: return channels[0]
48         return None
49
50     @property
51     def others_to_wait(self):
52         others = set()
53         node = self.node
54         if node: others.add(node)
55         
56         channel = self.channel
57         if channel: others.add(channel)
58         return others
59
60     def _configure_object(self):
61         # Set Mac
62         mac = self.get("mac")
63         if mac:
64             mac_uuid = self.simulator.create("Mac48Address", mac)
65         else:
66             mac_uuid = self.simulator.invoke("singleton::Mac48Address", "Allocate")
67         self.simulator.invoke(self.uuid, "SetAddress", mac_uuid)
68
69         # Set IP address
70         ip = self.get("ip")
71         prefix = self.get("prefix")
72
73         i = ipaddr.IPAddress(ip)
74         if i.version == 4:
75             # IPv4
76             ipv4 = self.node.ipv4
77             ifindex_uuid = self.simulator.invoke(ipv4.uuid, "AddInterface", 
78                     self.uuid)
79             ipv4_addr_uuid = self.simulator.create("Ipv4Address", ip)
80             ipv4_mask_uuid = self.simulator.create("Ipv4Mask", "/%s" % str(prefix))
81             inaddr_uuid = self.simulator.create("Ipv4InterfaceAddress", 
82                     ipv4_addr_uuid, ipv4_mask_uuid)
83             self.simulator.invoke(ipv4.uuid, "AddAddress", ifindex_uuid, 
84                     inaddr_uuid)
85             self.simulator.invoke(ipv4.uuid, "SetMetric", ifindex_uuid, 1)
86             self.simulator.invoke(ipv4.uuid, "SetUp", ifindex_uuid)
87         else:
88             # IPv6
89             # TODO!
90             pass
91
92     def _connect_object(self):
93         node = self.node
94         if node and node.uuid not in self.connected:
95             self.simulator.invoke(node.uuid, "AddDevice", self.uuid)
96             self._connected.add(node.uuid)
97
98         channel = self.channel
99         if channel and channel.uuid not in self.connected:
100             self.simulator.invoke(self.uuid, "Attach", channel.uuid)
101             self._connected.add(channel.uuid)
102