plshell to use 'capability' access method rather than xmlrpc when possible
[sfa.git] / sfa / plc / plshell.py
1 import xmlrpclib
2
3 from sfa.util.sfalogging import logger
4
5 class PlShell:
6     """
7     A simple xmlrpc shell to a myplc instance
8     This class can receive all PLCAPI calls to the underlying testbed
9     For safety this is limited to a set of hard-coded calls
10     """
11     
12     direct_calls = ['AddNode', 'AddPerson', 'AddPersonKey', 'AddPersonToSite',
13                     'AddPersonToSlice', 'AddRoleToPerson', 'AddSite', 'AddSiteTag', 'AddSlice',
14                     'AddSliceTag', 'AddSliceToNodes', 'BindObjectToPeer', 'DeleteKey',
15                     'DeleteNode', 'DeletePerson', 'DeletePersonFromSlice', 'DeleteSite',
16                     'DeleteSlice', 'DeleteSliceFromNodes', 'DeleteSliceTag', 'GetInitScripts',
17                     'GetInterfaces', 'GetKeys', 'GetNodeTags', 'GetPeers',
18                     'GetPersons', 'GetSlices', 'GetSliceTags', 'GetTagTypes',
19                     'UnBindObjectFromPeer', 'UpdateNode', 'UpdatePerson', 'UpdateSite',
20                     'UpdateSlice', 'UpdateSliceTag',
21                     # also used as-is in importer
22                     'GetSites','GetNodes',
23                     ]
24     # support for other names - this is experimental
25     alias_calls = { 'get_authorities':'GetSites',
26                     'get_nodes':'GetNodes',
27                     }
28
29
30     # use the 'capability' auth mechanism for higher performance when the PLC db is local    
31     def __init__ ( self, config ) :
32         url = config.SFA_PLC_URL
33         # try to figure if the url is local
34         hostname=urlparse(url).hostname
35         is_local=False
36         if hostname == 'localhost': is_local=True
37         # otherwise compare IP addresses
38         url_ip=socket.gethostbyname(hostname)
39         local_ip=socket.gethostbyname(socket.gethostname())
40         if url_ip==local_ip: is_local=True
41
42         if is_local:
43             try:
44                 # too bad this is not installed properly
45                 plcapi_path="/usr/share/plc_api"
46                 if plcapi_path not in sys.path: sys.path.append(plcapi_path)
47                 import PLC.Shell
48                 plc_direct_access=True
49             except:
50                 plc_direct_access=False
51         if is_local and plc_direct_access:
52             logger.debug('plshell access - capability')
53             self.plauth = { 'AuthMethod': 'capability',
54                             'Username':   config.SFA_PLC_USER,
55                             'AuthString': config.SFA_PLC_PASSWORD,
56                             }
57             self.proxy = PLC.Shell.Shell ()
58
59         else:
60             logger.debug('plshell access - xmlrpc')
61             self.plauth = { 'AuthMethod': 'password',
62                             'Username':   config.SFA_PLC_USER,
63                             'AuthString': config.SFA_PLC_PASSWORD,
64                             }
65             self.proxy = xmlrpclib.Server(url, verbose = 0, allow_none = True)
66
67     def __getattr__(self, name):
68         def func(*args, **kwds):
69             actual_name=None
70             if name in PlShell.direct_calls: actual_name=name
71             if name in PlShell.alias_calls: actual_name=PlShell.alias_calls[name]
72             if not actual_name:
73                 raise Exception, "Illegal method call %s for PL driver"%(name)
74             result=getattr(self.proxy, actual_name)(self.plauth, *args, **kwds)
75             logger.info('%s (%s) returned ... %s'%(name,actual_name,result))
76             return result
77         return func