8142b1e4a05be3ecfea612875495c49bf9939e8b
[sfa.git] / sfa / nitos / nitosshell.py
1 import sys
2 import socket
3 from urlparse import urlparse
4
5 from sfa.util.sfalogging import logger
6 from sfa.util.py23 import xmlrpc_client
7
8
9 class NitosShell:
10     """
11     A simple xmlrpc shell to a NITOS Scheduler instance
12     This class can receive all NITOS API  calls to the underlying testbed
13     For safety this is limited to a set of hard-coded calls
14     """
15
16     direct_calls = ['getNodes', 'getChannels', 'getSlices', 'getUsers', 'getReservedNodes',
17                     'getReservedChannels', 'getTestbedInfo',
18                     'reserveNodes', 'reserveChannels', 'addSlice', 'addUser', 'addUserToSlice',
19                     'addUserKey', 'addNode', 'addChannel',
20                     'updateReservedNodes', 'updateReservedChannels', 'updateSlice', 'updateUser',
21                     'updateNode', 'updateChannel',
22                     'deleteNode', 'deleteChannel', 'deleteSlice', 'deleteUser', 'deleteUserFromSLice',
23                     'deleteKey', 'releaseNodes', 'releaseChannels'
24                     ]
25
26     # use the 'capability' auth mechanism for higher performance when the PLC
27     # db is local
28     def __init__(self, config):
29         url = config.SFA_NITOS_URL
30         self.proxy = xmlrpc_client.ServerProxy(
31             url, verbose=False, allow_none=True)
32
33     def __getattr__(self, name):
34         def func(*args, **kwds):
35             actual_name = None
36             if name in NitosShell.direct_calls:
37                 actual_name = name
38             if not actual_name:
39                 raise Exception(
40                     "Illegal method call %s for NITOS driver" % (name))
41             actual_name = "scheduler.server." + actual_name
42             result = getattr(self.proxy, actual_name)(*args, **kwds)
43             logger.debug('NitosShell %s (%s) returned ... ' %
44                          (name, actual_name))
45             return result
46         return func