Improve debug display
[nepi.git] / src / nepi / testbeds / omf / omf_api.py
1 import datetime
2 import logging
3 import ssl
4 import sys
5 import time
6
7 from nepi.testbeds.omf.omf_client import OMFClient
8 from nepi.testbeds.omf.omf_messages import MessageHandler
9
10 class OmfAPI(object):
11     def __init__(self, slice, host, port, password, xmpp_root = None):
12         date = datetime.datetime.now().strftime("%Y-%m-%dt%H.%M.%S")
13         tz = -time.altzone if time.daylight != 0 else -time.timezone
14         date += "%+06.2f" % (tz / 3600) # timezone difference is in seconds
15         self._user = "%s-%s" % (slice, date)
16         self._slice = slice
17         self._host = host
18         self._port = port
19         self._password = password
20         self._hostnames = []
21         self._xmpp_root = xmpp_root or "OMF_5.4"
22
23         self._logger = logging.getLogger("nepi.testbeds.omf")
24
25         # OMF xmpp client
26         self._client = None
27         # message handler
28         self._message = None
29
30         if sys.version_info < (3, 0):
31             reload(sys)
32             sys.setdefaultencoding('utf8')
33
34         # instantiate the xmpp client
35         self._init_client()
36
37         # register xmpp nodes for the experiment
38         self._enroll_experiment()
39
40         # register xmpp logger for the experiment
41         self._enroll_logger()
42
43     def _init_client(self):
44         jid = "%s@%s" % (self._user, self._host)
45         xmpp = OMFClient(jid, self._password)
46         # PROTOCOL_SSLv3 required for compatibility with OpenFire
47         xmpp.ssl_version = ssl.PROTOCOL_SSLv3
48
49         if xmpp.connect((self._host, self._port)):
50             xmpp.process(threaded=True)
51             while not xmpp.ready:
52                 time.sleep(1)
53             self._client = xmpp
54             self._message = MessageHandler(self._slice, self._user)
55         else:
56             msg = "Unable to connect to the XMPP server."
57             self._logger.error(msg)
58             raise RuntimeError(msg)
59
60     def _enroll_experiment(self):
61         xmpp_node = self._exp_session_id
62         self._client.create(xmpp_node)
63         #print "Create experiment sesion id topics !!" 
64         self._client.subscribe(xmpp_node)
65         #print "Subscribe to experiment sesion id topics !!" 
66
67
68         address = "/%s/%s/%s/%s" % (self._host, self._xmpp_root, self._slice, self._user)
69         print address
70         payload = self._message.newexpfunction(self._user, address)
71         slice_sid = "/%s/%s" % (self._xmpp_root, self._slice)
72         self._client.publish(payload, slice_sid)
73
74     def _enroll_logger(self):
75         xmpp_node = self._logger_session_id
76         self._client.create(xmpp_node)
77         self._client.subscribe(xmpp_node)
78
79         payload = self._message.logfunction("2", 
80                 "nodeHandler::NodeHandler", 
81                 "INFO", 
82                 "OMF Experiment Controller 5.4 (git 529a626)")
83         self._client.publish(payload, xmpp_node)
84
85     def _host_session_id(self, hostname):
86         return "/%s/%s/%s/%s" % (self._xmpp_root, self._slice, self._user, hostname)
87
88     def _host_resource_id(self, hostname):
89         return "/%s/%s/resources/%s" % (self._xmpp_root, self._slice, hostname)
90
91     @property
92     def _exp_session_id(self):
93         return "/%s/%s/%s" % (self._xmpp_root, self._slice, self._user)
94
95     @property
96     def _logger_session_id(self):
97         return "/%s/%s/%s/LOGGER" % (self._xmpp_root, self._slice, self._user)
98
99     def delete(self, hostname):
100         if not hostname in self._hostnames:
101             return
102
103         self._hostnames.remove(hostname)
104
105         xmpp_node = self._host_session_id(hostname)
106         self._client.delete(xmpp_node)
107
108     def enroll_host(self, hostname):
109         if hostname in self._hostnames:
110             return 
111
112         self._hostnames.append(hostname)
113
114         xmpp_node =  self._host_session_id(hostname)
115         self._client.create(xmpp_node)
116         self._client.subscribe(xmpp_node)
117
118         xmpp_node =  self._host_resource_id(hostname)
119         self._client.subscribe(xmpp_node)
120
121         payload = self._message.enrollfunction("1", "*", "1", hostname)
122         self._client.publish(payload, xmpp_node)
123
124     def configure(self, hostname, attribute, value): 
125         payload = self._message.configurefunction(hostname, value, attribute)
126         xmpp_node =  self._host_session_id(hostname)
127         self._client.publish(payload, xmpp_node)
128
129     def execute(self, hostname, app_id, arguments, path, env):
130         payload = self._message.executefunction(hostname, app_id, arguments, path, env)
131         xmpp_node =  self._host_session_id(hostname)
132         self._client.publish(payload, xmpp_node)
133
134     def exit(self, hostname, app_id):
135         payload = self._message.exitfunction(hostname, app_id)
136         xmpp_node =  self._host_session_id(hostname)
137         self._client.publish(payload, xmpp_node)
138
139     def disconnect(self):
140         self._client.delete(self._exp_session_id)
141         self._client.delete(self._logger_session_id)
142
143         for hostname in self._hostnames[:]:
144             self.delete(hostname)
145
146         time.sleep(5)
147         self._client.disconnect()
148