bugs fixing
[sfa.git] / sfa / nitos / nitosshell.py
1 import sys
2 import xmlrpclib
3 import socket
4 from urlparse import urlparse
5
6 from sfa.util.sfalogging import logger
7
8 class NitosShell:
9     """
10     A simple xmlrpc shell to a myplc instance
11     This class can receive all NITOS API  calls to the underlying testbed
12     For safety this is limited to a set of hard-coded calls
13     """
14     
15     direct_calls = ['getNodes','getChannels','getSlices','getUsers','getReservedNodes',
16                     'getReservedChannels','getTestbedInfo'
17                     'reserveNodes','reserveChannels','addSlice','addUser','addUserToSlice',
18                     'addUserKey','addNode', 'addChannel',
19                     'updateReservedNodes','updateReservedChannels','updateSlice','updateUser',
20                     'updateNode', 'updateChannel',
21                     'deleteNode','deleteChannel','deleteSlice','deleteUser', 'deleteUserFromSLice',
22                     'deleteKey', 'releaseNodes', 'releaseChannels'
23                     ]
24
25
26     # use the 'capability' auth mechanism for higher performance when the PLC db is local    
27     def __init__ ( self, config ) :
28         url = config.SFA_NITOS_URL
29 #        url = "http://195.251.17.239:8080/RPC2"
30         # try to figure if the url is local
31         hostname=urlparse(url).hostname
32         is_local=False
33         if hostname == 'localhost': is_local=True
34         # otherwise compare IP addresses; 
35         # this might fail for any number of reasons, so let's harden that
36         try:
37             # xxx todo this seems to result in a DNS request for each incoming request to the AM
38             # should be cached or improved
39             url_ip=socket.gethostbyname(hostname)
40             local_ip=socket.gethostbyname(socket.gethostname())
41             if url_ip==local_ip: is_local=True
42         except:
43             pass
44
45         if is_local:
46             try:
47                 # too bad this is not installed properly
48                 plcapi_path="/usr/share/plc_api"
49                 if plcapi_path not in sys.path: sys.path.append(plcapi_path)
50                 import PLC.Shell
51                 plc_direct_access=True
52             except:
53                 plc_direct_access=False
54         if is_local and plc_direct_access:
55             logger.debug('plshell access - capability')
56             #self.plauth = { 'AuthMethod': 'capability',
57             #                'Username':   config.SFA_PLC_USER,
58             #                'AuthString': config.SFA_PLC_PASSWORD,
59             #                }
60             self.proxy = PLC.Shell.Shell ()
61
62         else:
63             logger.debug('nitosshell access - xmlrpc')
64             #self.plauth = { 'AuthMethod': 'password',
65             #                'Username':   config.SFA_PLC_USER,
66             #                'AuthString': config.SFA_PLC_PASSWORD,
67             #                }
68             self.proxy = xmlrpclib.Server(url, verbose = False, allow_none = True)
69
70     def __getattr__(self, name):
71         def func(*args, **kwds):
72             actual_name=None
73             if name in NitosShell.direct_calls: actual_name=name
74 #            if name in NitosShell.alias_calls: actual_name=NitosShell.alias_calls[name]
75             if not actual_name:
76                 raise Exception, "Illegal method call %s for NITOS driver"%(name)
77             actual_name = "scheduler.server." + actual_name
78             result=getattr(self.proxy, actual_name)(*args, **kwds)
79             logger.debug('NitosShell %s (%s) returned ... '%(name,actual_name))
80             return result
81         return func
82