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