05c368169da916a34c00701cb284c9137dfc49f5
[sfa.git] / sfa / dummy / dummyxrn.py
1 # specialized Xrn class for Dummy TB
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, testbed_name, hostname):
7     return DummyXrn(auth=auth+'.'+testbed_name,hostname=hostname).get_hrn()
8 def hostname_to_urn(auth, testbed_name, hostname):
9     return DummyXrn(auth=auth+'.'+testbed_name,hostname=hostname).get_urn()
10 def slicename_to_hrn (auth_hrn, slicename):
11     return DummyXrn(auth=auth_hrn,slicename=slicename).get_hrn()
12 def email_to_hrn (auth_hrn, email):
13     return DummyXrn(auth=auth_hrn, email=email).get_hrn()
14 def hrn_to_dummy_slicename (hrn):
15     return DummyXrn(xrn=hrn,type='slice').dummy_slicename()
16 def hrn_to_dummy_authname (hrn):
17     return DummyXrn(xrn=hrn,type='any').dummy_authname()
18 def xrn_to_hostname(hrn):
19     return Xrn.unescape(PlXrn(xrn=hrn, type='node').get_leaf())
20
21 class DummyXrn (Xrn):
22
23     @staticmethod 
24     def site_hrn (auth, testbed_name):
25         return '.'.join([auth,testbed_name])
26
27     def __init__ (self, auth=None, hostname=None, slicename=None, email=None, interface=None, **kwargs):
28         #def hostname_to_hrn(auth_hrn, login_base, hostname):
29         if hostname is not None:
30             self.type='node'
31             # keep only the first part of the DNS name
32             #self.hrn='.'.join( [auth,hostname.split(".")[0] ] )
33             # escape the '.' in the hostname
34             self.hrn='.'.join( [auth,Xrn.escape(hostname)] )
35             self.hrn_to_urn()
36         #def slicename_to_hrn(auth_hrn, slicename):
37         elif slicename is not None:
38             self.type='slice'
39             # split at the first _
40             parts = slicename.split("_",1)
41             self.hrn = ".".join([auth] + parts )
42             self.hrn_to_urn()
43         #def email_to_hrn(auth_hrn, email):
44         elif email is not None:
45             self.type='person'
46             # keep only the part before '@' and replace special chars into _
47             self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])
48             self.hrn_to_urn()
49         elif interface is not None:
50             self.type = 'interface'
51             self.hrn = auth + '.' + interface
52             self.hrn_to_urn()
53         else:
54             Xrn.__init__ (self,**kwargs)
55
56     #def hrn_to_pl_slicename(hrn):
57     def dummy_slicename (self):
58         self._normalize()
59         leaf = self.leaf
60         sliver_id_parts = leaf.split(':')
61         name = sliver_id_parts[0]
62         name = re.sub('[^a-zA-Z0-9_]', '', name)
63         return name
64
65     #def hrn_to_pl_authname(hrn):
66     def dummy_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 dummy_login_base (self):
75         self._normalize()
76         if self.type and self.type.startswith('authority'):
77             base = self.leaf 
78         else:
79             base = self.authority[-1]
80         
81         # Fix up names of GENI Federates
82         base = base.lower()
83         base = re.sub('\\\[^a-zA-Z0-9]', '', base)
84
85         if len(base) > 20:
86             base = base[len(base)-20:]
87         
88         return base