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
10 def hostname_to_hrn(auth, login_base, hostname):
11 return PlXrn(auth=auth + '.' + login_base, hostname=hostname).get_hrn()
14 def hostname_to_urn(auth, login_base, hostname):
15 return PlXrn(auth=auth + '.' + login_base, hostname=hostname).get_urn()
18 def slicename_to_hrn(auth_hrn, slicename):
19 return PlXrn(auth=auth_hrn, slicename=slicename).get_hrn()
22 def email_to_hrn(auth_hrn, email):
23 return PlXrn(auth=auth_hrn, email=email).get_hrn()
26 def hrn_to_pl_slicename(hrn):
27 return PlXrn(xrn=hrn, type='slice').pl_slicename()
28 # removed-dangerous - was used for non-slice objects
29 # def hrn_to_pl_login_base (hrn):
30 # return PlXrn(xrn=hrn,type='slice').pl_login_base()
33 def hrn_to_pl_authname(hrn):
34 return PlXrn(xrn=hrn, type='any').pl_authname()
37 def xrn_to_hostname(hrn):
38 return Xrn.unescape(PlXrn(xrn=hrn, type='node').get_leaf())
40 # helpers to handle external objects created via fedaration
44 return hrn.split('.')[0]
47 def hash_loginbase(site_hrn):
48 if len(site_hrn) <= 12:
49 return site_hrn.replace('.', '8').replace('_', '8')
50 ratio = float(12) / len(site_hrn)
51 auths_tab = site_hrn.split('.')
53 for auth in auths_tab:
55 auth = auth.replace('_', '8')
56 auth2 = auth[:int(len(auth) * ratio)]
57 auths_tab2.append(auth2)
58 return '8'.join(auths_tab2)
64 def site_hrn(auth, login_base):
65 return '.'.join([auth, login_base])
67 def __init__(self, auth=None, hostname=None, slicename=None, email=None, interface=None, **kwargs):
68 # def hostname_to_hrn(auth_hrn, login_base, hostname):
69 if hostname is not None:
71 # keep only the first part of the DNS name
72 #self.hrn='.'.join( [auth,hostname.split(".")[0] ] )
73 # escape the '.' in the hostname
74 self.hrn = '.'.join([auth, Xrn.escape(hostname)])
76 # def slicename_to_hrn(auth_hrn, slicename):
77 elif slicename is not None:
79 # split at the first _
80 parts = slicename.split("_", 1)
81 self.hrn = ".".join([auth] + parts)
83 # def email_to_hrn(auth_hrn, email):
84 elif email is not None:
86 # keep only the part before '@' and replace special chars into _
88 [auth, email.split('@')[0].replace(".", "_").replace("+", "_")])
90 elif interface is not None:
91 self.type = 'interface'
92 self.hrn = auth + '.' + interface
95 Xrn.__init__(self, **kwargs)
97 # def hrn_to_pl_slicename(hrn):
98 def pl_slicename(self):
101 sliver_id_parts = leaf.split(':')
102 name = sliver_id_parts[0]
103 name = re.sub('[^a-zA-Z0-9_]', '', name)
104 return self.pl_login_base() + '_' + name
106 # def hrn_to_pl_authname(hrn):
107 def pl_authname(self):
109 return self.authority[-1]
111 def interface_name(self):
115 def pl_login_base(self):
117 if self.type and self.type.startswith('authority'):
120 base = self.authority[-1]
122 # Fix up names of GENI Federates
124 base = re.sub('[\\\\]*[^a-zA-Z0-9]', '', base)
132 'onelab.upmc.fit_demo',
133 'onelab.upmc.fit_demo.some_other',
136 if __name__ == '__main__':
138 print(("{} - hash_loginbase -> {}".format(test, hash_loginbase(test))))