Better mgt of external slices/users/sites + fixes
[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 xrn_to_ext_slicename (xrn):
26     slice_hrn=PlXrn(xrn=xrn,type='slice').get_hrn()
27     site_hrn = get_authority(slice_hrn)
28     login_base = '8'.join(site_hrn.split('.'))
29     slice_name = '_'.join([login_base, slice_hrn.split('.')[-1]])
30     return slice_name
31
32 def hrn_to_ext_loginbase (hrn):
33     site_hrn =  get_authority(hrn)
34     login_base = '8'.join(site_hrn.split('.'))[:20]
35     return login_base
36
37 def top_auth (hrn):
38     return hrn.split('.')[0]
39
40 class PlXrn (Xrn):
41
42     @staticmethod 
43     def site_hrn (auth, login_base):
44         return '.'.join([auth,login_base])
45
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:
49             self.type='node'
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)] )
54             self.hrn_to_urn()
55         #def slicename_to_hrn(auth_hrn, slicename):
56         elif slicename is not None:
57             self.type='slice'
58             # split at the first _
59             parts = slicename.split("_",1)
60             self.hrn = ".".join([auth] + parts )
61             self.hrn_to_urn()
62         #def email_to_hrn(auth_hrn, email):
63         elif email is not None:
64             self.type='person'
65             # keep only the part before '@' and replace special chars into _
66             self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])
67             self.hrn_to_urn()
68         elif interface is not None:
69             self.type = 'interface'
70             self.hrn = auth + '.' + interface
71             self.hrn_to_urn()
72         else:
73             Xrn.__init__ (self,**kwargs)
74
75     #def hrn_to_pl_slicename(hrn):
76     def pl_slicename (self):
77         self._normalize()
78         leaf = self.leaf
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
83
84     #def hrn_to_pl_authname(hrn):
85     def pl_authname (self):
86         self._normalize()
87         return self.authority[-1]
88
89     def interface_name(self):
90         self._normalize()
91         return self.leaf
92
93     def pl_login_base (self):
94         self._normalize()
95         if self.type and self.type.startswith('authority'):
96             base = self.leaf 
97         else:
98             base = self.authority[-1]
99         
100         # Fix up names of GENI Federates
101         base = base.lower()
102         base = re.sub('\\\[^a-zA-Z0-9]', '', base)
103
104         if len(base) > 20:
105             base = base[len(base)-20:]
106         
107         return base