3 from sfa.util.sfalogging import logger
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
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',
24 # support for other names - this is experimental
25 alias_calls = { 'get_authorities':'GetSites',
26 'get_nodes':'GetNodes',
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
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
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)
48 plc_direct_access=True
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,
57 self.proxy = PLC.Shell.Shell ()
60 logger.debug('plshell access - xmlrpc')
61 self.plauth = { 'AuthMethod': 'password',
62 'Username': config.SFA_PLC_USER,
63 'AuthString': config.SFA_PLC_PASSWORD,
65 self.proxy = xmlrpclib.Server(url, verbose = 0, allow_none = True)
67 def __getattr__(self, name):
68 def func(*args, **kwds):
70 if name in PlShell.direct_calls: actual_name=name
71 if name in PlShell.alias_calls: actual_name=PlShell.alias_calls[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))