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