Adding synthetic network stack to ns-3 node RM
[nepi.git] / src / nepi / resources / ns3 / ns3node.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, Types
21 from nepi.execution.resource import clsinit_copy
22 from nepi.resources.ns3.ns3base import NS3Base
23
24 @clsinit_copy
25 class NS3BaseNode(NS3Base):
26     _rtype = "abstract::ns3::Node"
27
28     @classmethod
29     def _register_attributes(cls):
30         enablestack = Attribute("enableStack", 
31                 "Install network stack in Node, including: ARP, "
32                 "IP4, ICMP, UDP and TCP ",
33                 type = Types.Bool,
34                 default = False,
35                 flags = Flags.Design)
36
37         cls._register_attribute(enablestack)
38
39     @property
40     def simulation(self):
41         from nepi.resources.ns3.ns3simulation import NS3Simulation
42         for guid in self.connections:
43             rm = self.ec.get_resource(guid)
44             if isinstance(rm, NS3Simulation):
45                 return rm
46
47         msg = "Node not connected to simulation"
48         self.error(msg)
49         raise RuntimeError, msg
50  
51     @property
52     def ipv4(self):
53         from nepi.resources.ns3.ns3ipv4l3protocol import NS3BaseIpv4L3Protocol
54         ipv4s = self.get_connected(NS3BaseIpv4L3Protocol.get_rtype())
55         if ipv4s: return ipv4s[0]
56         return None
57
58     @property
59     def arp(self):
60         from nepi.resources.ns3.ns3arpl3protocol import NS3BaseArpL3Protocol
61         arps = self.get_connected(NS3BaseArpL3Protocol.get_rtype())
62         if arps: return arps[0]
63         return None
64
65     @property
66     def mobility(self):
67         from nepi.resources.ns3.ns3mobilitymodel import NS3BaseMobilityModel
68         mobility = self.get_connected(NS3BaseMobilityModel.get_rtype())
69         if mobility: return mobility[0]
70         return None
71
72     @property
73     def devices(self):
74         from nepi.resources.ns3.ns3netdevice import NS3BaseNetDevice
75         devices = self.get_connected(NS3BaseNetDevice.get_rtype())
76
77         if not devices: 
78             msg = "Node not connected to devices"
79             self.error(msg)
80             raise RuntimeError, msg
81
82         return devices
83
84     @property
85     def dceapplications(self):
86         from nepi.resources.ns3.ns3dceapplication import NS3BaseDceApplication
87         dceapplications = self.get_connected(NS3BaseDceApplication.get_rtype())
88
89         return dceapplications
90
91     @property
92     def _rms_to_wait(self):
93         rms = set()
94         rms.add(self.simulation)
95
96         ipv4 = self.ipv4
97         if ipv4:
98             rms.add(ipv4)
99
100         arp = self.arp
101         if arp:
102             rms.add(arp)
103
104         mobility = self.mobility
105         if mobility:
106             rms.add(mobility)
107
108         return rms
109
110     def _configure_object(self):
111         if self.get("enableStack"):
112             uuid_stack_helper = self.simulation.create("InternetStackHelper")
113             self.simulation.invoke(uuid_stack_helper, "Install", self.uuid)
114
115             # Retrieve IPV4 object
116             ipv4_uuid = self.simulation.invoke(self.uuid, "retrieveObject",
117                     "ns3::Ipv4L3Protocol")
118             
119             # Add IPv4 RM to the node
120             ipv4 = self.ec.register_resource("ns3::Ipv4L3Protocol")
121             self.ec.register_connection(self.guid, ipv4)
122             ipv4rm = self.ec.get_resource(ipv4)
123             ipv4rm._uuid = ipv4_uuid
124             ipv4rm.set_started()
125         else:
126             ### node.AggregateObject(PacketSocketFactory())
127             uuid_packet_socket_factory = self.simulation.create("PacketSocketFactory")
128             self.simulation.invoke(self.uuid, "AggregateObject", uuid_packet_socket_factory)
129
130         dceapplications = self.dceapplications
131         if dceapplications:
132             self._add_dce(dceapplications)
133
134     def _connect_object(self):
135         if not self.get("enableStack"):
136             ipv4 = self.ipv4
137             if ipv4:
138                 self.simulation.invoke(self.uuid, "AggregateObject", ipv4.uuid)
139                 self._connected.add(ipv4.uuid)
140                 ipv4._connected.add(self.uuid)
141
142             arp = self.arp
143             if arp:
144                 self.simulation.invoke(self.uuid, "AggregateObject", arp.uuid)
145                 self._connected.add(arp.uuid)
146                 arp._connected.add(self.uuid)
147
148         mobility = self.mobility
149         if mobility:
150             self.simulation.invoke(self.uuid, "AggregateObject", mobility.uuid)
151             self._connected.add(mobility.uuid)
152             mobility._connected.add(self.uuid)
153
154     def _add_dce(self, dceapplications):
155         dceapp = dceapplications[0]
156
157         container_uuid = self.simulation.create("NodeContainer")
158         self.simulation.invoke(container_uuid, "Add", self.uuid)
159         with dceapp.dce_manager_lock:
160             self.simulation.invoke(dceapp.dce_manager_helper_uuid, 
161                     "Install", container_uuid)
162