1 from __future__ import print_function
3 # specialized Xrn class for PlanetLab
5 from sfa.util.xrn import Xrn, get_authority
7 # temporary helper functions to use this module instead of namespace
8 def hostname_to_hrn (auth, login_base, hostname):
9 return PlXrn(auth=auth+'.'+login_base,hostname=hostname).get_hrn()
10 def hostname_to_urn(auth, login_base, hostname):
11 return PlXrn(auth=auth+'.'+login_base,hostname=hostname).get_urn()
12 def slicename_to_hrn (auth_hrn, slicename):
13 return PlXrn(auth=auth_hrn,slicename=slicename).get_hrn()
14 def email_to_hrn (auth_hrn, email):
15 return PlXrn(auth=auth_hrn, email=email).get_hrn()
16 def hrn_to_pl_slicename (hrn):
17 return PlXrn(xrn=hrn,type='slice').pl_slicename()
18 # removed-dangerous - was used for non-slice objects
19 #def hrn_to_pl_login_base (hrn):
20 # return PlXrn(xrn=hrn,type='slice').pl_login_base()
21 def hrn_to_pl_authname (hrn):
22 return PlXrn(xrn=hrn,type='any').pl_authname()
23 def xrn_to_hostname(hrn):
24 return Xrn.unescape(PlXrn(xrn=hrn, type='node').get_leaf())
26 # helpers to handle external objects created via fedaration
28 return hrn.split('.')[0]
30 def hash_loginbase(site_hrn):
31 if len(site_hrn) <= 12:
32 return site_hrn.replace('.','8').replace('_', '8')
33 ratio = float(12) / len(site_hrn)
34 auths_tab = site_hrn.split('.')
36 for auth in auths_tab:
38 auth = auth.replace('_', '8')
39 auth2 = auth[:int(len(auth)*ratio)]
40 auths_tab2.append(auth2)
41 return '8'.join(auths_tab2)
46 def site_hrn (auth, login_base):
47 return '.'.join([auth,login_base])
49 def __init__ (self, auth=None, hostname=None, slicename=None, email=None, interface=None, **kwargs):
50 #def hostname_to_hrn(auth_hrn, login_base, hostname):
51 if hostname is not None:
53 # keep only the first part of the DNS name
54 #self.hrn='.'.join( [auth,hostname.split(".")[0] ] )
55 # escape the '.' in the hostname
56 self.hrn='.'.join( [auth,Xrn.escape(hostname)] )
58 #def slicename_to_hrn(auth_hrn, slicename):
59 elif slicename is not None:
61 # split at the first _
62 parts = slicename.split("_",1)
63 self.hrn = ".".join([auth] + parts )
65 #def email_to_hrn(auth_hrn, email):
66 elif email is not None:
68 # keep only the part before '@' and replace special chars into _
69 self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])
71 elif interface is not None:
72 self.type = 'interface'
73 self.hrn = auth + '.' + interface
76 Xrn.__init__ (self,**kwargs)
78 #def hrn_to_pl_slicename(hrn):
79 def pl_slicename (self):
82 sliver_id_parts = leaf.split(':')
83 name = sliver_id_parts[0]
84 name = re.sub('[^a-zA-Z0-9_]', '', name)
85 return self.pl_login_base() + '_' + name
87 #def hrn_to_pl_authname(hrn):
88 def pl_authname (self):
90 return self.authority[-1]
92 def interface_name(self):
96 def pl_login_base (self):
98 if self.type and self.type.startswith('authority'):
101 base = self.authority[-1]
103 # Fix up names of GENI Federates
105 base = re.sub('[\\\\]*[^a-zA-Z0-9]', '', base)
113 'onelab.upmc.fit_demo',
114 'onelab.upmc.fit_demo.some_other',
117 if __name__ == '__main__':
119 print(("{} - hash_loginbase -> {}".format(test, hash_loginbase(test))))