Remote ns-3 with ping working
[nepi.git] / src / nepi / resources / ns3 / ns3base.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.resource import ResourceManager, clsinit_copy, \
21         ResourceState, reschedule_delay
22
23 from nepi.execution.attribute import Flags
24
25 reschedule_delay = "2s"
26
27 @clsinit_copy
28 class NS3Base(ResourceManager):
29     _rtype = "abstract::ns3::Object"
30     _backend_type = "ns3"
31
32     SIMULATOR_UUID = "singleton::Simulator"
33
34     def __init__(self, ec, guid):
35         super(NS3Base, self).__init__(ec, guid)
36         self._uuid = None
37         self._connected = set()
38
39     @property
40     def connected(self):
41         return self._connected
42
43     @property
44     def uuid(self):
45         return self._uuid
46
47     @property
48     def simulation(self):
49         return self.node.simulation
50
51     @property
52     def node(self):
53         from nepi.resources.ns3.ns3node import NS3BaseNode
54         nodes = self.get_connected(NS3BaseNode.get_rtype())
55         if nodes: return nodes[0]
56         return None
57
58     @property
59     def _rms_to_wait(self):
60         """ Returns the collection of ns-3 RMs that this RM needs to
61         wait for before start
62
63         This method should be overriden to wait for other ns-3
64         objects to be deployed before proceeding with the deployment
65
66         """
67         rms = set()
68         node = self.node
69         if node: rms.add(node)
70         return rms
71
72     def _instantiate_object(self):
73         if self.uuid:
74             return 
75
76         kwargs = dict()
77         for attr in self._attrs.values():
78             if not (attr.has_changed() and attr.has_flag(Flags.Construct)):
79                 continue
80
81             kwargs[attr.name] = attr.value
82
83         self._uuid = self.simulation.factory(self.get_rtype(), **kwargs)
84
85     def _configure_object(self):
86         pass
87
88     def _connect_object(self):
89         node = self.node
90         if node and node.uuid not in self.connected:
91             self.simulation.invoke(node.uuid, "AggregateObject", self.uuid)
92             self._connected.add(node.uuid)
93
94     def _wait_rms(self):
95         """ Returns True if dependent RMs are not yer READY, False otherwise"""
96         for rm in self._rms_to_wait:
97             if rm and rm.state < ResourceState.READY:
98                 return True
99         return False
100
101     def do_provision(self):
102         # TODO: create run dir for ns3 object !!!!
103         # self.simulation.node.mkdir(self.run_home)
104
105         self._instantiate_object()
106         self._connect_object()
107         self._configure_object()
108       
109         self.info("Provisioning finished")
110
111         super(NS3Base, self).do_provision()
112
113     def do_deploy(self):
114         if self._wait_rms():
115             self.debug("---- RESCHEDULING DEPLOY ----" )
116             self.ec.schedule(reschedule_delay, self.deploy)
117         else:
118             self.info("Entering deploy")
119             self.do_discover()
120             self.do_provision()
121
122             self.set_ready()
123
124     def do_start(self):
125         if self.state == ResourceState.READY:
126             # No need to do anything, simulation.Run() will start every object
127             self.info("Starting")
128             self.set_started()
129         else:
130             msg = " Failed "
131             self.error(msg, out, err)
132             raise RuntimeError, msg
133
134     def do_stop(self):
135         if self.state == ResourceState.STARTED:
136             # No need to do anything, simulation.Destroy() will stop every object
137             self.info("Stopping command '%s'" % command)
138             self.set_stopped()
139     
140     @property
141     def state(self):
142         return self._state
143