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
24 from nepi.resources.ns3.ns3simulator import NS3Simulator
27 class NS3Base(ResourceManager):
28 _rtype = "abstract::ns3::Object"
32 super(NS3Base, self).__init__()
34 self._connected = set()
38 return self._connected
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
51 if node: return node.simulator
56 from nepi.resources.ns3.ns3node import NS3BaseNode
57 nodes = self.get_connected(NS3BaseNode.get_rtype())
58 if nodes: return nodes[0]
62 def others_to_wait(self):
65 if node: others.add(node)
68 def _instantiate_object(self):
73 for attr in self._attrs:
74 if not attr.value or attr.has_flag(Flags.ReadOnly):
77 kwargs[attr.name] = attr.value
79 self.uuid = self.simulator.factory(self.get_rtype(), **kwargs)
81 def _configure_object(self):
84 def _connect_object(self):
86 if node and node.uuid not in self.connected:
87 self.simulator.invoke(node.uuid, "AggregateObject", self.uuid)
88 self._connected.add(node.uuid)
90 def _wait_others(self):
91 """ Returns the collection of ns-3 RMs that this RM needs to
94 This method should be overriden to wait for other ns-3
95 objects to be deployed before proceeding with the deployment
98 for other in self.others_to_wait:
99 if other and other.state < ResourceState.READY:
103 def do_provision(self):
104 # create run dir for ns3 object
105 # self.simulator.node.mkdir(self.run_home)
107 self._instantiate_object()
108 self._connect_object()
109 self._configure_object()
111 self.info("Provisioning finished")
113 super(NS3Base, self).do_provision()
116 if not self.simulator or self.simulator.state < ResourceState.READY or \
118 self.debug("---- RESCHEDULING DEPLOY ----" )
120 # ccnd needs to wait until node is deployed and running
121 self.ec.schedule(reschedule_delay, self.deploy)
123 # TODO: CREATE AND CONFIGURE NS-3 C++ OBJECT
127 self.debug("----- READY ---- ")
131 if self.state == ResourceState.READY:
132 # No need to do anything, simulator.Run() will start every object
133 self.info("Starting")
137 self.error(msg, out, err)
138 raise RuntimeError, msg
141 if self.state == ResourceState.STARTED:
142 # No need to do anything, simulator.Destroy() will stop every object
143 self.info("Stopping command '%s'" % command)