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