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 def __init__(self, ec, guid):
33 super(NS3Base, self).__init__(ec, guid)
35 self._connected = set()
39 return self._connected
47 return self.node.simulation
51 from nepi.resources.ns3.ns3node import NS3BaseNode
52 nodes = self.get_connected(NS3BaseNode.get_rtype())
53 if nodes: return nodes[0]
57 def _rms_to_wait(self):
58 """ Returns the collection of ns-3 RMs that this RM needs to
61 This method should be overriden to wait for other ns-3
62 objects to be deployed before proceeding with the deployment
67 if node: rms.add(node)
70 def _instantiate_object(self):
75 for attr in self._attrs.values():
76 if not (attr.has_changed() and attr.has_flag(Flags.Construct)):
79 kwargs[attr.name] = attr.value
81 self._uuid = self.simulation.factory(self.get_rtype(), **kwargs)
83 def _configure_object(self):
86 def _connect_object(self):
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)
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:
99 def do_provision(self):
100 # TODO: create run dir for ns3 object !!!!
101 # self.simulation.node.mkdir(self.run_home)
103 self._instantiate_object()
104 self._connect_object()
105 self._configure_object()
107 self.info("Provisioning finished")
109 super(NS3Base, self).do_provision()
113 self.debug("---- RESCHEDULING DEPLOY ----" )
114 self.ec.schedule(reschedule_delay, self.deploy)
116 self.info("Entering deploy")
123 if self.state == ResourceState.READY:
124 # No need to do anything, simulation.Run() will start every object
125 self.info("Starting")
129 self.error(msg, out, err)
130 raise RuntimeError, msg
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)