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 NITOS API calls to the underlying testbed
12 For safety this is limited to a set of hard-coded calls
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'
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
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
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
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)
51 plc_direct_access=True
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,
60 self.proxy = PLC.Shell.Shell ()
63 logger.debug('nitosshell access - xmlrpc')
64 #self.plauth = { 'AuthMethod': 'password',
65 # 'Username': config.SFA_PLC_USER,
66 # 'AuthString': config.SFA_PLC_PASSWORD,
68 self.proxy = xmlrpclib.Server(url, verbose = False, allow_none = True)
70 def __getattr__(self, name):
71 def func(*args, **kwds):
73 if name in NitosShell.direct_calls: actual_name=name
74 # if name in NitosShell.alias_calls: actual_name=NitosShell.alias_calls[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))