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