Merge branch 'master' into eucalyptus-devel
[sfa.git] / sfa / util / 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 def hrn_to_pl_login_base (hrn):
17     return PlXrn(xrn=hrn,type='slice').pl_login_base()
18 def hrn_to_pl_authname (hrn):
19     return PlXrn(xrn=hrn,type='any').pl_authname()
20
21
22 class PlXrn (Xrn):
23
24     @staticmethod 
25     def site_hrn (auth, login_base):
26         return '.'.join(auth,login_base)
27
28     def __init__ (self, auth=None, hostname=None, slicename=None, email=None, **kwargs):
29         #def hostname_to_hrn(auth_hrn, login_base, hostname):
30         if hostname is not None:
31             self.type='node'
32             # keep only the first part of the DNS name
33             #self.hrn='.'.join( [auth,hostname.split(".")[0] ] )
34             # escape the '.' in the hostname
35             self.hrn='.'.join( [auth,Xrn.escape(hostname)] )
36             self.hrn_to_urn()
37         #def slicename_to_hrn(auth_hrn, slicename):
38         elif slicename is not None:
39             self.type='slice'
40             # split at the first _
41             parts = slicename.split("_",1)
42             self.hrn = ".".join([auth] + parts )
43             self.hrn_to_urn()
44         #def email_to_hrn(auth_hrn, email):
45         elif email is not None:
46             self.type='person'
47             # keep only the part before '@' and replace special chars into _
48             self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])
49             self.hrn_to_urn()
50         else:
51             Xrn.__init__ (self,**kwargs)
52
53     #def hrn_to_pl_slicename(hrn):
54     def pl_slicename (self):
55         self._normalize()
56         leaf = self.leaf
57         leaf = re.sub('[^a-zA-Z0-9_]', '', leaf)
58         return self.pl_login_base() + '_' + leaf
59
60     #def hrn_to_pl_authname(hrn):
61     def pl_authname (self):
62         self._normalize()
63         return self.authority[-1]
64
65     #def hrn_to_pl_login_base(hrn):
66     def pl_login_base (self):
67         self._normalize()
68         base = self.authority[-1]
69         
70         # Fix up names of GENI Federates
71         base = base.lower()
72         base = re.sub('\\\[^a-zA-Z0-9]', '', base)
73
74         if len(base) > 20:
75             base = base[len(base)-20:]
76         
77         return base