enable dce in simulation is now automatic
[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 mobility(self):
60         from nepi.resources.ns3.ns3mobilitymodel import NS3BaseMobilityModel
61         mobility = self.get_connected(NS3BaseMobilityModel.get_rtype())
62         if mobility: return mobility[0]
63         return None
64
65     @property
66     def devices(self):
67         from nepi.resources.ns3.ns3netdevice import NS3BaseNetDevice
68         devices = self.get_connected(NS3BaseNetDevice.get_rtype())
69
70         if not devices: 
71             msg = "Node not connected to devices"
72             self.error(msg)
73             raise RuntimeError, msg
74
75         return devices
76
77     @property
78     def dceapplications(self):
79         from nepi.resources.ns3.ns3dceapplication import NS3BaseDceApplication
80         dceapplications = self.get_connected(NS3BaseDceApplication.get_rtype())
81
82         return dceapplications
83
84     @property
85     def _rms_to_wait(self):
86         rms = set()
87         rms.add(self.simulation)
88
89         ipv4 = self.ipv4
90         if ipv4:
91             rms.add(ipv4)
92
93         mobility = self.mobility
94         if mobility:
95             rms.add(mobility)
96
97         return rms
98
99     def _configure_object(self):
100         ### node.AggregateObject(PacketSocketFactory())
101         uuid_packet_socket_factory = self.simulation.create("PacketSocketFactory")
102         self.simulation.invoke(self.uuid, "AggregateObject", uuid_packet_socket_factory)
103
104         dceapplications = self.dceapplications
105         if dceapplications:
106             self._add_dce(dceapplications)
107
108     def _connect_object(self):
109         ipv4 = self.ipv4
110         if ipv4:
111             self.simulation.invoke(self.uuid, "AggregateObject", ipv4.uuid)
112
113         if self.get("enableStack"):
114             uuid_stack_helper = self.simulation.create("InternetStackHelper")
115             self.simulation.invoke(uuid_stack_helper, "Install", self.uuid)
116
117         mobility = self.mobility
118         if mobility:
119             self.simulation.invoke(self.uuid, "AggregateObject", mobility.uuid)
120
121     def _add_dce(self, dceapplications):
122         dceapp = dceapplications[0]
123
124         container_uuid = self.simulation.create("NodeContainer")
125         self.simulation.invoke(container_uuid, "Add", self.uuid)
126         with dceapp.dce_manager_lock:
127             self.simulation.invoke(dceapp.dce_manager_helper_uuid, 
128                     "Install", container_uuid)
129