Merge branch 'upstreammaster'
[sfa.git] / sfa / planetlab / plxrn.py
1 # specialized Xrn class for PlanetLab
2 import re
3 from sfa.util.xrn import Xrn
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 class PlXrn (Xrn):
25
26     @staticmethod 
27     def site_hrn (auth, login_base):
28         return '.'.join([auth,login_base])
29
30     def __init__ (self, auth=None, hostname=None, slicename=None, email=None, interface=None, **kwargs):
31         #def hostname_to_hrn(auth_hrn, login_base, hostname):
32         if hostname is not None:
33             self.type='node'
34             # keep only the first part of the DNS name
35             #self.hrn='.'.join( [auth,hostname.split(".")[0] ] )
36             # escape the '.' in the hostname
37             self.hrn='.'.join( [auth,Xrn.escape(hostname)] )
38             self.hrn_to_urn()
39         #def slicename_to_hrn(auth_hrn, slicename):
40         elif slicename is not None:
41             self.type='slice'
42             # split at the first _
43             parts = slicename.split("_",1)
44             self.hrn = ".".join([auth] + parts )
45             self.hrn_to_urn()
46         #def email_to_hrn(auth_hrn, email):
47         elif email is not None:
48             self.type='person'
49             # keep only the part before '@' and replace special chars into _
50             self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])
51             self.hrn_to_urn()
52         elif interface is not None:
53             self.type = 'interface'
54             self.hrn = auth + '.' + interface
55             self.hrn_to_urn()
56         else:
57             Xrn.__init__ (self,**kwargs)
58
59     #def hrn_to_pl_slicename(hrn):
60     def pl_slicename (self):
61         self._normalize()
62         leaf = self.leaf
63         sliver_id_parts = leaf.split(':')
64         name = sliver_id_parts[0]
65         name = re.sub('[^a-zA-Z0-9_]', '', name)
66         return self.pl_login_base() + '_' + name
67
68     #def hrn_to_pl_authname(hrn):
69     def pl_authname (self):
70         self._normalize()
71         return self.authority[-1]
72
73     def interface_name(self):
74         self._normalize()
75         return self.leaf
76
77     def pl_login_base (self):
78         self._normalize()
79         if self.type and self.type.startswith('authority'):
80             base = self.leaf 
81         else:
82             base = self.authority[-1]
83         
84         # Fix up names of GENI Federates
85         base = base.lower()
86         base = re.sub('\\\[^a-zA-Z0-9]', '', base)
87
88         if len(base) > 20:
89             base = base[len(base)-20:]
90         
91         return base