2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 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>
19 # Julien Tribino <julien.tribino@inria.fr>
21 from nepi.util.timefuncs import tnow
22 from nepi.execution.resource import ResourceManager, clsinit_copy, \
24 from nepi.execution.attribute import Attribute, Flags
25 from nepi.resources.omf.omf_resource import ResourceGateway, OMFResource
26 from nepi.resources.omf.omf_api_factory import OMFAPIFactory
30 reschedule_check = "1s"
31 confirmation_counter = 3600
34 class OMFNode(OMFResource):
36 .. class:: Class Args :
38 :param ec: The Experiment controller
39 :type ec: ExperimentController
40 :param guid: guid of the RM
42 :param creds: Credentials to communicate with the rm (XmppClient for OMF)
47 _authorized_connections = ["OMFApplication" , "OMFWifiInterface"]
50 def _register_attributes(cls):
51 """Register the attributes of an OMF Node
54 hostname = Attribute("hostname", "Hostname of the machine")
56 cls._register_attribute(hostname)
58 # XXX: We don't necessary need to have the credentials at the
59 # moment we create the RM
60 def __init__(self, ec, guid):
62 :param ec: The Experiment controller
63 :type ec: ExperimentController
64 :param guid: guid of the RM
68 super(OMFNode, self).__init__(ec, guid)
72 # For performance tests
74 self.begin_deploy_time = None
81 def valid_connection(self, guid):
82 """ Check if the connection with the guid in parameter is possible.
83 Only meaningful connections are allowed.
85 :param guid: Guid of the current RM
90 rm = self.ec.get_resource(guid)
91 if rm.get_rtype() in self._authorized_connections:
92 msg = "Connection between %s %s and %s %s accepted" % (
93 self.get_rtype(), self._guid, rm.get_rtype(), guid)
97 msg = "Connection between %s %s and %s %s refused" % (
98 self.get_rtype(), self._guid, rm.get_rtype(), guid)
104 """ Deploy the RM. It means : Send Xmpp Message Using OMF protocol
105 to enroll the node into the experiment.
108 ## For performance test
110 self.begin_deploy_time = tnow()
113 if not self.get('xmppServer'):
114 msg = "XmppServer is not initialzed. XMPP Connections impossible"
116 raise RuntimeError, msg
118 if not self.get('version'):
119 msg = "Version of OMF is not indicated"
121 raise RuntimeError, msg
123 if not (self.get('xmppUser') or self.get('xmppPort')
124 or self.get('xmppPassword')):
125 msg = "Credentials are not all initialzed. Default values will be used"
128 if not self._omf_api :
129 self._omf_api = OMFAPIFactory.get_api(self.get('version'),
130 self.get('xmppServer'), self.get('xmppUser'), self.get('xmppPort'),
131 self.get('xmppPassword'), exp_id = self.exp_id)
133 if not self.get('hostname') :
134 msg = "Hostname's value is not initialized"
136 raise RuntimeError, msg
138 if self.get('version') == "5":
139 self._omf_api.enroll_host(self.get('hostname'))
141 self._omf_api.enroll_topic(self.get('hostname'))
143 super(OMFNode, self).do_deploy()
145 def do_release(self):
146 """ Clean the RM at the end of the experiment by unenrolling
147 the node from the topic
150 from nepi.resources.omf.application import OMFApplication
151 rm_list = self.get_connected(OMFApplication.get_rtype())
154 if rm.state < ResourceState.RELEASED:
155 self.ec.schedule(self.reschedule_delay, self.release)
158 from nepi.resources.omf.interface import OMFWifiInterface
159 rm_list = self.get_connected(OMFWifiInterface.get_rtype())
162 if rm.state < ResourceState.RELEASED:
163 self.ec.schedule(self.reschedule_delay, self.release)
167 if self.get('version') == "5":
168 self._omf_api.release(self.get('hostname'))
170 self._omf_api.unenroll_topic(self.get('hostname'))
172 OMFAPIFactory.release_api(self.get('version'),
173 self.get('xmppServer'), self.get('xmppUser'), self.get('xmppPort'),
174 self.get('xmppPassword'), exp_id = self.exp_id)
176 super(OMFNode, self).do_release()