f9cf63c3cfafab85249885df930a226472a42005
[sfa.git] / sfa / planetlab / plxrn.py
1 from __future__ import print_function
2
3 # specialized Xrn class for PlanetLab
4 import re
5 from sfa.util.xrn import Xrn, get_authority
6
7 # temporary helper functions to use this module instead of namespace
8
9
10 def hostname_to_hrn(auth, login_base, hostname):
11     return PlXrn(auth=auth + '.' + login_base, hostname=hostname).get_hrn()
12
13
14 def hostname_to_urn(auth, login_base, hostname):
15     return PlXrn(auth=auth + '.' + login_base, hostname=hostname).get_urn()
16
17
18 def slicename_to_hrn(auth_hrn, slicename):
19     return PlXrn(auth=auth_hrn, slicename=slicename).get_hrn()
20
21
22 def email_to_hrn(auth_hrn, email):
23     return PlXrn(auth=auth_hrn, email=email).get_hrn()
24
25
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()
31
32
33 def hrn_to_pl_authname(hrn):
34     return PlXrn(xrn=hrn, type='any').pl_authname()
35
36
37 def xrn_to_hostname(hrn):
38     return Xrn.unescape(PlXrn(xrn=hrn, type='node').get_leaf())
39
40 # helpers to handle external objects created via fedaration
41
42
43 def top_auth(hrn):
44     return hrn.split('.')[0]
45
46
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('.')
52     auths_tab2 = []
53     for auth in auths_tab:
54
55         auth = auth.replace('_', '8')
56         auth2 = auth[:int(len(auth) * ratio)]
57         auths_tab2.append(auth2)
58     return '8'.join(auths_tab2)
59
60
61 class PlXrn (Xrn):
62
63     @staticmethod
64     def site_hrn(auth, login_base):
65         return '.'.join([auth, login_base])
66
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:
70             self.type = 'node'
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)])
75             self.hrn_to_urn()
76         # def slicename_to_hrn(auth_hrn, slicename):
77         elif slicename is not None:
78             self.type = 'slice'
79             # split at the first _
80             parts = slicename.split("_", 1)
81             self.hrn = ".".join([auth] + parts)
82             self.hrn_to_urn()
83         # def email_to_hrn(auth_hrn, email):
84         elif email is not None:
85             self.type = 'person'
86             # keep only the part before '@' and replace special chars into _
87             self.hrn = '.'.join(
88                 [auth, email.split('@')[0].replace(".", "_").replace("+", "_")])
89             self.hrn_to_urn()
90         elif interface is not None:
91             self.type = 'interface'
92             self.hrn = auth + '.' + interface
93             self.hrn_to_urn()
94         else:
95             Xrn.__init__(self, **kwargs)
96
97     # def hrn_to_pl_slicename(hrn):
98     def pl_slicename(self):
99         self._normalize()
100         leaf = self.leaf
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
105
106     # def hrn_to_pl_authname(hrn):
107     def pl_authname(self):
108         self._normalize()
109         return self.authority[-1]
110
111     def interface_name(self):
112         self._normalize()
113         return self.leaf
114
115     def pl_login_base(self):
116         self._normalize()
117         if self.type and self.type.startswith('authority'):
118             base = self.leaf
119         else:
120             base = self.authority[-1]
121
122         # Fix up names of GENI Federates
123         base = base.lower()
124         base = re.sub('[\\\\]*[^a-zA-Z0-9]', '', base)
125
126         return base
127
128 tests = [
129     'inria.foo.x',
130     'in.foo.x_y',
131     'inria.foo.longer',
132     'onelab.upmc.fit_demo',
133     'onelab.upmc.fit_demo.some_other',
134 ]
135
136 if __name__ == '__main__':
137     for test in tests:
138         print(("{} - hash_loginbase -> {}".format(test, hash_loginbase(test))))