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