trashed remoteshell and use plshell instead
[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     def __init__ ( self, config ) :
30         self.url = config.SFA_PLC_URL
31         # xxx attempt to use the 'capability' auth mechanism for higher performance
32         # when the PLC db is local
33         # xxx todo
34         is_local = False
35         if is_local:
36             try:
37                 import PLC.Shell
38                 plc_direct_access=True
39             except:
40                 plc_direct_access=False
41         if is_local and plc_direct_access:
42             logger.info('plshell - capability access')
43             self.plauth = { 'AuthMethod': 'capability',
44                             'UserName':   config.SFA_PLC_USER,
45                             'AuthString': config.SFA_PLC_PASSWORD,
46                             }
47             self.proxy = PLC.Shell.Shell ()
48
49         else:
50             logger.info('plshell - xmlrpc access')
51             self.plauth = { 'AuthMethod': 'password',
52                             'Username':   config.SFA_PLC_USER,
53                             'AuthString': config.SFA_PLC_PASSWORD,
54                             }
55             self.proxy = xmlrpclib.Server(self.url, verbose = 0, allow_none = True)
56
57     def __getattr__(self, name):
58         def func(*args, **kwds):
59             actual_name=None
60             if name in PlShell.direct_calls: actual_name=name
61             if name in PlShell.alias_calls: actual_name=PlShell.alias_calls[name]
62             if not actual_name:
63                 raise Exception, "Illegal method call %s for PL driver"%(name)
64             result=getattr(self.proxy, actual_name)(self.plauth, *args, **kwds)
65             logger.info('%s (%s) returned ... %s'%(name,actual_name,result))
66             return result
67         return func