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')
31 ratio = float(12) / len(site_hrn)
32 auths_tab = site_hrn.split('.')
34 for auth in auths_tab:
35 auth2 = auth[:int(len(auth)*ratio)]
36 auths_tab2.append(auth2)
38 return '8'.join(auths_tab2)
43 def site_hrn (auth, login_base):
44 return '.'.join([auth,login_base])
46 def __init__ (self, auth=None, hostname=None, slicename=None, email=None, interface=None, **kwargs):
47 #def hostname_to_hrn(auth_hrn, login_base, hostname):
48 if hostname is not None:
50 # keep only the first part of the DNS name
51 #self.hrn='.'.join( [auth,hostname.split(".")[0] ] )
52 # escape the '.' in the hostname
53 self.hrn='.'.join( [auth,Xrn.escape(hostname)] )
55 #def slicename_to_hrn(auth_hrn, slicename):
56 elif slicename is not None:
58 # split at the first _
59 parts = slicename.split("_",1)
60 self.hrn = ".".join([auth] + parts )
62 #def email_to_hrn(auth_hrn, email):
63 elif email is not None:
65 # keep only the part before '@' and replace special chars into _
66 self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])
68 elif interface is not None:
69 self.type = 'interface'
70 self.hrn = auth + '.' + interface
73 Xrn.__init__ (self,**kwargs)
75 #def hrn_to_pl_slicename(hrn):
76 def pl_slicename (self):
79 sliver_id_parts = leaf.split(':')
80 name = sliver_id_parts[0]
81 name = re.sub('[^a-zA-Z0-9_]', '', name)
82 return self.pl_login_base() + '_' + name
84 #def hrn_to_pl_authname(hrn):
85 def pl_authname (self):
87 return self.authority[-1]
89 def interface_name(self):
93 def pl_login_base (self):
95 if self.type and self.type.startswith('authority'):
98 base = self.authority[-1]
100 # Fix up names of GENI Federates
102 base = re.sub('[\\\\]*[^a-zA-Z0-9]', '', base)