70ff5e02bdf81152291288350b303fcf89a6431c
[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 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())
25
26 # helpers to handle external objects created via fedaration 
27 def top_auth (hrn):
28     return hrn.split('.')[0]
29
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('.')
35     auths_tab2 = []
36     for auth in auths_tab:
37
38         auth = auth.replace('_', '8')
39         auth2 = auth[:int(len(auth)*ratio)]
40         auths_tab2.append(auth2)
41     return '8'.join(auths_tab2)
42
43 class PlXrn (Xrn):
44
45     @staticmethod 
46     def site_hrn (auth, login_base):
47         return '.'.join([auth,login_base])
48
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:
52             self.type='node'
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)] )
57             self.hrn_to_urn()
58         #def slicename_to_hrn(auth_hrn, slicename):
59         elif slicename is not None:
60             self.type='slice'
61             # split at the first _
62             parts = slicename.split("_",1)
63             self.hrn = ".".join([auth] + parts )
64             self.hrn_to_urn()
65         #def email_to_hrn(auth_hrn, email):
66         elif email is not None:
67             self.type='person'
68             # keep only the part before '@' and replace special chars into _
69             self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])
70             self.hrn_to_urn()
71         elif interface is not None:
72             self.type = 'interface'
73             self.hrn = auth + '.' + interface
74             self.hrn_to_urn()
75         else:
76             Xrn.__init__ (self,**kwargs)
77
78     #def hrn_to_pl_slicename(hrn):
79     def pl_slicename (self):
80         self._normalize()
81         leaf = self.leaf
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
86
87     #def hrn_to_pl_authname(hrn):
88     def pl_authname (self):
89         self._normalize()
90         return self.authority[-1]
91
92     def interface_name(self):
93         self._normalize()
94         return self.leaf
95
96     def pl_login_base (self):
97         self._normalize()
98         if self.type and self.type.startswith('authority'):
99             base = self.leaf 
100         else:
101             base = self.authority[-1]
102         
103         # Fix up names of GENI Federates
104         base = base.lower()
105         base = re.sub('[\\\\]*[^a-zA-Z0-9]', '', base)
106
107         return base
108
109 tests = [
110     'inria.foo.x',
111     'in.foo.x_y',
112     'inria.foo.longer',
113     'onelab.upmc.fit_demo',
114     'onelab.upmc.fit_demo.some_other',
115 ]
116
117 if __name__ == '__main__':
118     for test in tests:
119         print(("{} - hash_loginbase -> {}".format(test, hash_loginbase(test))))