import datetime import logging import ssl import sys import time from neco.resources.omf.omf_client import OMFClient from neco.resources.omf.omf_messages_5_4 import MessageHandler class OMFAPI(object): """ .. class:: Class Args : :param slice: Xmpp Slice :type slice: Str :param host: Xmpp Server :type host: Str :param port: Xmpp Port :type port: Str :param password: Xmpp password :type password: Str :param xmpp_root: Root of the Xmpp Topic Architecture :type xmpp_root: Str .. note:: This class is the implementation of an OMF 5.4 API. Since the version 5.4.1, the Topic Architecture start with OMF_5.4 instead of OMF used for OMF5.3 """ def __init__(self, slice, host, port, password, xmpp_root = None): date = datetime.datetime.now().strftime("%Y-%m-%dt%H.%M.%S") tz = -time.altzone if time.daylight != 0 else -time.timezone date += "%+06.2f" % (tz / 3600) # timezone difference is in seconds self._user = "%s-%s" % (slice, date) self._slice = slice self._host = host self._port = port self._password = password self._hostnames = [] self._xmpp_root = xmpp_root or "OMF_5.4" self._logger = logging.getLogger("neco.resources.omf") # OMF xmpp client self._client = None # message handler self._message = None if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') # instantiate the xmpp client self._init_client() # register xmpp nodes for the experiment self._enroll_experiment() self._enroll_newexperiment() # register xmpp logger for the experiment self._enroll_logger() def _init_client(self): """ Initialize XMPP Client """ jid = "%s@%s" % (self._user, self._host) xmpp = OMFClient(jid, self._password) # PROTOCOL_SSLv3 required for compatibility with OpenFire xmpp.ssl_version = ssl.PROTOCOL_SSLv3 if xmpp.connect((self._host, self._port)): xmpp.process(threaded=True) while not xmpp.ready: time.sleep(1) self._client = xmpp self._message = MessageHandler(self._slice, self._user) else: msg = "Unable to connect to the XMPP server." self._logger.error(msg) raise RuntimeError(msg) def _enroll_experiment(self): """ Create and Subscribe to the Session Topic """ xmpp_node = self._exp_session_id self._client.create(xmpp_node) #print "Create experiment sesion id topics !!" self._client.subscribe(xmpp_node) #print "Subscribe to experiment sesion id topics !!" def _enroll_newexperiment(self): """ Publish New Experiment Message """ address = "/%s/%s/%s/%s" % (self._host, self._xmpp_root, self._slice, self._user) print address payload = self._message.newexpfunction(self._user, address) slice_sid = "/%s/%s" % (self._xmpp_root, self._slice) self._client.publish(payload, slice_sid) def _enroll_logger(self): """ Create and Subscribe to the Logger Topic """ xmpp_node = self._logger_session_id self._client.create(xmpp_node) self._client.subscribe(xmpp_node) payload = self._message.logfunction("2", "nodeHandler::NodeHandler", "INFO", "OMF Experiment Controller 5.4 (git 529a626)") self._client.publish(payload, xmpp_node) def _host_session_id(self, hostname): """ Return the Topic Name as /xmpp_root/slice/user/hostname :param hostname: Full hrn of the node :type hostname: str """ return "/%s/%s/%s/%s" % (self._xmpp_root, self._slice, self._user, hostname) def _host_resource_id(self, hostname): """ Return the Topic Name as /xmpp_root/slice/resources/hostname :param hostname: Full hrn of the node :type hostname: str """ return "/%s/%s/resources/%s" % (self._xmpp_root, self._slice, hostname) @property def _exp_session_id(self): """ Return the Topic Name as /xmpp_root/slice/user """ return "/%s/%s/%s" % (self._xmpp_root, self._slice, self._user) @property def _logger_session_id(self): """ Return the Topic Name as /xmpp_root/slice/LOGGER """ return "/%s/%s/%s/LOGGER" % (self._xmpp_root, self._slice, self._user) def delete(self, hostname): """ Delete the topic corresponding to the hostname for this session :param hostname: Full hrn of the node :type hostname: str """ if not hostname in self._hostnames: return self._hostnames.remove(hostname) xmpp_node = self._host_session_id(hostname) self._client.delete(xmpp_node) def enroll_host(self, hostname): """ Create and Subscribe to the session topic and the resources corresponding to the hostname :param hostname: Full hrn of the node :type hostname: str """ if hostname in self._hostnames: return self._hostnames.append(hostname) xmpp_node = self._host_session_id(hostname) self._client.create(xmpp_node) self._client.subscribe(xmpp_node) xmpp_node = self._host_resource_id(hostname) self._client.subscribe(xmpp_node) payload = self._message.enrollfunction("1", "*", "1", hostname) self._client.publish(payload, xmpp_node) def configure(self, hostname, attribute, value): """ Configure attribute on the node :param hostname: Full hrn of the node :type hostname: str :param attribute: Attribute that need to be configured (often written as /net/wX/attribute, with X the interface number) :type attribute: str :param value: Value of the attribute :type value: str """ payload = self._message.configurefunction(hostname, value, attribute) xmpp_node = self._host_session_id(hostname) self._client.publish(payload, xmpp_node) def execute(self, hostname, app_id, arguments, path, env): """ Execute command on the node :param hostname: Full hrn of the node :type hostname: str :param app_id: Application Id (Any id that represents in a unique way the application) :type app_id: str :param arguments: Arguments of the application :type arguments: str :param path: Path of the application :type path: str :param env: Environnement values for the application :type env: str """ payload = self._message.executefunction(hostname, app_id, arguments, path, env) xmpp_node = self._host_session_id(hostname) self._client.publish(payload, xmpp_node) def exit(self, hostname, app_id): """ Kill an application started with OMF :param hostname: Full hrn of the node :type hostname: str :param app_id: Application Id of the application you want to stop :type app_id: str """ payload = self._message.exitfunction(hostname, app_id) xmpp_node = self._host_session_id(hostname) self._client.publish(payload, xmpp_node) def disconnect(self): """ Delete the sesion and logger topic and disconnect """ self._client.delete(self._exp_session_id) self._client.delete(self._logger_session_id) for hostname in self._hostnames[:]: self.delete(hostname) time.sleep(1) self._client.disconnect() class OMFAPIFactory(object): """ .. note:: It allows the different RM to use the same xmpp client if they use the same credentials. For the moment, it is focused on Xmpp. """ # XXX: put '_apis' instead of '_Api' _Api = dict() @classmethod def get_api(cls, slice, host, port, password): """ Get an Api :param slice: Xmpp Slice Name :type slice: str :param host: Xmpp Server Adress :type host: str :param port: Xmpp Port (Default : 5222) :type port: str :param password: Xmpp Password :type password: str """ if slice and host and port and password: key = cls._hash_api(slice, host, port) if key in cls._Api: return cls._Api[key] else : return cls.create_api(slice, host, port, password) return None @classmethod def create_api(cls, slice, host, port, password): """ Create an API if this one doesn't exist yet with this credentials :param slice: Xmpp Slice Name :type slice: str :param host: Xmpp Server Adress :type host: str :param port: Xmpp Port (Default : 5222) :type port: str :param password: Xmpp Password :type password: str """ OmfApi = OMFAPI(slice, host, port, password) key = cls._hash_api(slice, host, port) cls._Api[key] = OmfApi return OmfApi # XXX: this is not a hash :) # From wikipedia: "A hash function is any algorithm or subroutine that maps large data # sets of variable length to smaller data sets of a fixed length." # The idea is to apply a function to get a smaller string. Use hashlib instead. # e.g: # import hashlib # res = slice + "_" + host + "_" + port # hashlib.md5(res).hexdigest() # # XXX: change method name for 'make_key' @classmethod def _hash_api(cls, slice, host, port): """ Hash the credentials in order to create a key :param slice: Xmpp Slice Name :type slice: str :param host: Xmpp Server Adress :type host: str :param port: Xmpp Port (Default : 5222) :type port: str """ res = slice + "_" + host + "_" + port return res