Adding linux ns3 server unit test
[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         # Ns3 RMs should be connected to the simulator through a ns3 node 
46         node = self.node
47         if node: return node.simulator
48         return None
49          
50     @property
51     def node(self):
52         from nepi.resources.ns3.ns3node import NS3BaseNode
53         nodes = self.get_connected(NS3BaseNode.get_rtype())
54         if nodes: return nodes[0]
55         return None
56
57     @property
58     def others_to_wait(self):
59         others = set()
60         node = self.node
61         if node: others.add(node)
62         return others
63
64     def _instantiate_object(self):
65         if self.uuid:
66             return 
67
68         kwargs = dict()
69         for attr in self._attrs.values():
70             if not (attr.has_changed() and attr.has_flag(Flags.Construct)):
71                 continue
72
73             kwargs[attr.name] = attr.value
74
75         self._uuid = self.simulator.factory(self.get_rtype(), **kwargs)
76
77     def _configure_object(self):
78         pass
79
80     def _connect_object(self):
81         node = self.node
82         if node and node.uuid not in self.connected:
83             self.simulator.invoke(node.uuid, "AggregateObject", self.uuid)
84             self._connected.add(node.uuid)
85
86     def _wait_others(self):
87         """ Returns the collection of ns-3 RMs that this RM needs to
88         wait for before start
89
90         This method should be overriden to wait for other ns-3
91         objects to be deployed before proceeding with the deployment
92
93         """
94         for other in self.others_to_wait:
95             if other and other.state < ResourceState.READY:
96                 return True
97         return False
98
99     def do_provision(self):
100         # create run dir for ns3 object
101         # self.simulator.node.mkdir(self.run_home)
102
103         self._instantiate_object()
104         self._connect_object()
105         self._configure_object()
106       
107         self.info("Provisioning finished")
108
109         super(NS3Base, self).do_provision()
110
111     def do_deploy(self):
112         if not self.simulator or self.simulator.state < ResourceState.READY or \
113                 self._wait_others():
114             self.debug("---- RESCHEDULING DEPLOY ----" )
115             
116             # ccnd needs to wait until node is deployed and running
117             self.ec.schedule(reschedule_delay, self.deploy)
118         else:
119             # TODO: CREATE AND CONFIGURE NS-3 C++ OBJECT
120             self.do_discover()
121             self.do_provision()
122
123             self.debug("----- READY ---- ")
124             self.set_ready()
125
126     def do_start(self):
127         if self.state == ResourceState.READY:
128             # No need to do anything, simulator.Run() will start every object
129             self.info("Starting")
130             self.set_started()
131         else:
132             msg = " Failed "
133             self.error(msg, out, err)
134             raise RuntimeError, msg
135
136     def do_stop(self):
137         if self.state == ResourceState.STARTED:
138             # No need to do anything, simulator.Destroy() will stop every object
139             self.info("Stopping command '%s'" % command)
140             self.set_stopped()
141     
142     @property
143     def state(self):
144         return self._state
145