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