2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2014 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.resource import ResourceManager, clsinit_copy, \
21 ResourceState, reschedule_delay
23 from nepi.execution.attribute import Flags
25 reschedule_delay = "2s"
28 class NS3Base(ResourceManager):
29 _rtype = "abstract::ns3::Object"
32 SIMULATOR_UUID = "singleton::Simulator"
34 def __init__(self, ec, guid):
35 super(NS3Base, self).__init__(ec, guid)
37 self._connected = set()
41 return self._connected
49 return self.node.simulation
53 from nepi.resources.ns3.ns3node import NS3BaseNode
54 nodes = self.get_connected(NS3BaseNode.get_rtype())
55 if nodes: return nodes[0]
59 def _rms_to_wait(self):
60 """ Returns the collection of ns-3 RMs that this RM needs to
63 This method should be overriden to wait for other ns-3
64 objects to be deployed before proceeding with the deployment
69 if node: rms.add(node)
72 def _instantiate_object(self):
77 for attr in self._attrs.values():
78 if not (attr.has_changed() and attr.has_flag(Flags.Construct)):
81 kwargs[attr.name] = attr.value
83 self._uuid = self.simulation.factory(self.get_rtype(), **kwargs)
85 def _configure_object(self):
88 def _connect_object(self):
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)
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:
101 def do_provision(self):
102 # TODO: create run dir for ns3 object !!!!
103 # self.simulation.node.mkdir(self.run_home)
105 self._instantiate_object()
106 self._connect_object()
107 self._configure_object()
109 self.info("Provisioning finished")
111 super(NS3Base, self).do_provision()
115 self.debug("---- RESCHEDULING DEPLOY ----" )
116 self.ec.schedule(reschedule_delay, self.deploy)
118 self.info("Entering deploy")
125 if self.state == ResourceState.READY:
126 # No need to do anything, simulation.Run() will start every object
127 self.info("Starting")
131 self.error(msg, out, err)
132 raise RuntimeError, msg
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)