4 from urlparse import urlparse
6 from sfa.util.sfalogging import logger
10 A simple xmlrpc shell to a myplc instance
11 This class can receive all PLCAPI calls to the underlying testbed
12 For safety this is limited to a set of hard-coded calls
15 direct_calls = ['AddNode', 'AddPerson', 'AddPersonKey', 'AddPersonToSite',
16 'AddPersonToSlice', 'AddRoleToPerson', 'AddSite', 'AddSiteTag', 'AddSlice',
17 'AddSliceTag', 'AddSliceToNodes', 'BindObjectToPeer', 'DeleteKey',
18 'DeleteNode', 'DeletePerson', 'DeletePersonFromSlice', 'DeleteSite',
19 'DeleteSlice', 'DeleteSliceFromNodes', 'DeleteSliceTag', 'GetInitScripts',
20 'GetInterfaces', 'GetKeys', 'GetNodeTags', 'GetPeers',
21 'GetPersons', 'GetSlices', 'GetSliceTags', 'GetTagTypes',
22 'UnBindObjectFromPeer', 'UpdateNode', 'UpdatePerson', 'UpdateSite',
23 'UpdateSlice', 'UpdateSliceTag',
24 # also used as-is in importer
25 'GetSites','GetNodes',
26 # Lease management methods
27 'GetLeases', 'GetLeaseGranularity', 'DeleteLeases','UpdateLeases',
29 # HRN management methods
30 'SetPersonHrn', 'GetPersonHrn', 'SetSliceHrn', 'GetSliceHrn',
31 'SetNodeHrn', 'GetNodeHrn'
33 # support for other names - this is experimental
34 alias_calls = { 'get_authorities':'GetSites',
35 'get_nodes':'GetNodes',
39 # use the 'capability' auth mechanism for higher performance when the PLC db is local
40 def __init__ ( self, config ) :
41 url = config.SFA_PLC_URL
42 # try to figure if the url is local
43 hostname=urlparse(url).hostname
45 if hostname == 'localhost': is_local=True
46 # otherwise compare IP addresses;
47 # this might fail for any number of reasons, so let's harden that
49 # xxx todo this seems to result in a DNS request for each incoming request to the AM
50 # should be cached or improved
51 url_ip=socket.gethostbyname(hostname)
52 local_ip=socket.gethostbyname(socket.gethostname())
53 if url_ip==local_ip: is_local=True
59 # too bad this is not installed properly
60 plcapi_path="/usr/share/plc_api"
61 if plcapi_path not in sys.path: sys.path.append(plcapi_path)
63 plc_direct_access=True
65 plc_direct_access=False
66 if is_local and plc_direct_access:
67 logger.info('plshell access - capability')
68 self.plauth = { 'AuthMethod': 'capability',
69 'Username': config.SFA_PLC_USER,
70 'AuthString': config.SFA_PLC_PASSWORD,
72 self.proxy = PLC.Shell.Shell ()
75 logger.info('plshell access - xmlrpc')
76 self.plauth = { 'AuthMethod': 'password',
77 'Username': config.SFA_PLC_USER,
78 'AuthString': config.SFA_PLC_PASSWORD,
80 self.proxy = xmlrpclib.Server(url, verbose = False, allow_none = True)
82 def __getattr__(self, name):
83 def func(*args, **kwds):
85 if name in PlShell.direct_calls: actual_name=name
86 if name in PlShell.alias_calls: actual_name=PlShell.alias_calls[name]
88 raise Exception, "Illegal method call %s for PL driver"%(name)
89 result=getattr(self.proxy, actual_name)(self.plauth, *args, **kwds)
90 logger.debug('PlShell %s (%s) returned ... '%(name,actual_name))