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