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