type may be None
[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             self.hrn_to_urn()
35         #def slicename_to_hrn(auth_hrn, slicename):
36         elif slicename is not None:
37             self.type='slice'
38             # split at the first _
39             parts = slicename.split("_",1)
40             self.hrn = ".".join([auth] + parts )
41             self.hrn_to_urn()
42         #def email_to_hrn(auth_hrn, email):
43         elif email is not None:
44             self.type='person'
45             # keep only the part before '@' and replace special chars into _
46             self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])
47             self.hrn_to_urn()
48         else:
49             Xrn.__init__ (self,**kwargs)
50
51     #def hrn_to_pl_slicename(hrn):
52     def pl_slicename (self):
53         self._normalize()
54         leaf = self.leaf
55         leaf = re.sub('[^a-zA-Z0-9]', '', leaf)
56         return self.pl_login_base() + '_' + leaf
57
58     #def hrn_to_pl_authname(hrn):
59     def pl_authname (self):
60         self._normalize()
61         return self.authority[-1]
62
63     #def hrn_to_pl_login_base(hrn):
64     def pl_login_base (self):
65         self._normalize()
66         base = self.authority[-1]
67         
68         # Fix up names of GENI Federates
69         base = base.lower()
70         base = re.sub('\\\[^a-zA-Z0-9]', '', base)
71
72         if len(base) > 20:
73             base = base[len(base)-20:]
74         
75         return base