Merge the OMF 6 branch
[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 from nepi.execution.attribute import Flags
23 from nepi.execution.trace import TraceAttr
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         self._trace_filename = dict()
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 simulation(self):
46         return self.node.simulation
47
48     @property
49     def node(self):
50         from nepi.resources.ns3.ns3node import NS3BaseNode
51         nodes = self.get_connected(NS3BaseNode.get_rtype())
52         if nodes: return nodes[0]
53         return None
54
55     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
56         filename = self._trace_filename.get(name)
57         if not filename:
58             self.error("Can not resolve trace %s. Did you enabled it?" % name)
59             return ""
60
61         return self.simulation.trace(filename, attr, block, offset)
62
63     @property
64     def _rms_to_wait(self):
65         """ Returns the collection of ns-3 RMs that this RM needs to
66         wait for before start
67
68         This method should be overriden to wait for other ns-3
69         objects to be deployed before proceeding with the deployment
70
71         """
72         rms = set()
73         node = self.node
74         if node: rms.add(node)
75         return rms
76
77     def _instantiate_object(self):
78         if self.uuid:
79             return 
80
81         kwargs = dict()
82         for attr in self._attrs.values():
83             if not ( attr.has_flag(Flags.Construct) and attr.has_changed() ):
84                 continue
85
86             kwargs[attr.name] = attr._value
87
88         self._uuid = self.simulation.factory(self.get_rtype(), **kwargs)
89
90     def _configure_object(self):
91         pass
92
93     def _connect_object(self):
94         node = self.node
95         if node and node.uuid not in self.connected:
96             self.simulation.invoke(node.uuid, "AggregateObject", self.uuid)
97             self._connected.add(node.uuid)
98
99     def _wait_rms(self):
100         """ Returns True if dependent RMs are not yer READY, False otherwise"""
101         for rm in self._rms_to_wait:
102             if rm.state < ResourceState.READY:
103                 return True
104         return False
105
106     def do_provision(self):
107         # TODO: create run dir for ns3 object !!!!
108         # self.simulation.node.mkdir(self.run_home)
109
110         self._instantiate_object()
111         self._connect_object()
112         self._configure_object()
113       
114         self.info("Provisioning finished")
115
116         super(NS3Base, self).do_provision()
117
118     def do_deploy(self):
119         if self._wait_rms():
120             self.debug("---- RESCHEDULING DEPLOY ----" )
121             self.ec.schedule(reschedule_delay, self.deploy)
122         else:
123             self.do_discover()
124             self.do_provision()
125
126             self.set_ready()
127
128     def do_start(self):
129         if self.state == ResourceState.READY:
130             # No need to do anything, simulation.Run() will start every object
131             self.info("Starting")
132             self.set_started()
133         else:
134             msg = " Failed "
135             self.error(msg, out, err)
136             raise RuntimeError, msg
137
138     def do_stop(self):
139         if self.state == ResourceState.STARTED:
140             # No need to do anything, simulation.Destroy() will stop every object
141             self.info("Stopping command '%s'" % command)
142             self.set_stopped()
143     
144     @property
145     def state(self):
146         return self._state
147
148     def get(self, name):
149         if self.state in [ResourceState.READY, ResourceState.STARTED] and \
150                 self.has_flag(name, Flags.Reserved) and \
151                 not self.has_flag(name, Flags.NoRead): 
152             return self.simulation.ns3_get(self.uuid, name)
153         else:
154             value = super(NS3Base, self).get(name)
155
156         return value
157
158     def set(self, name, value):
159         if self.state in [ResourceState.READY, ResourceState.STARTED] and \
160                 self.has_flag(name, Flags.Reserved) and \
161                 not (self.has_flag(Flags.NoWrite) or self.has_flag(name, Flags.Design)): 
162             self.simulation.ns3_set(self.uuid, name, value)
163         
164         value = super(NS3Base, self).set(name, value)
165
166         return value
167