the big cleanup: remove unused code relating to openstack/nova
[sfa.git] / sfa / nitos / nitosxrn.py
1 from __future__ import print_function
2
3 # specialized Xrn class for NITOS
4 import re
5 from sfa.util.xrn import Xrn
6
7 # temporary helper functions to use this module instead of namespace
8
9
10 def hostname_to_hrn(auth, login_base, hostname):
11     return NitosXrn(auth=auth + '.' + login_base, hostname=hostname).get_hrn()
12
13
14 def hostname_to_urn(auth, login_base, hostname):
15     return NitosXrn(auth=auth + '.' + login_base, hostname=hostname).get_urn()
16
17
18 def slicename_to_hrn(auth_hrn, site_name, slicename):
19     return NitosXrn(auth=auth_hrn + '.' + site_name, slicename=slicename).get_hrn()
20 # hack to convert nitos user name to hrn
21
22
23 def username_to_hrn(auth_hrn, site_name, username):
24     return NitosXrn(auth=auth_hrn + '.' + site_name, slicename=username).get_hrn()
25
26
27 def email_to_hrn(auth_hrn, email):
28     return NitosXrn(auth=auth_hrn, email=email).get_hrn()
29
30
31 def hrn_to_nitos_slicename(hrn):
32     return NitosXrn(xrn=hrn, type='slice').nitos_slicename()
33 # removed-dangerous - was used for non-slice objects
34 # def hrn_to_nitos_login_base (hrn):
35 #    return NitosXrn(xrn=hrn,type='slice').nitos_login_base()
36
37
38 def hrn_to_nitos_authname(hrn):
39     return NitosXrn(xrn=hrn, type='any').nitos_authname()
40
41
42 def xrn_to_hostname(hrn):
43     return Xrn.unescape(NitosXrn(xrn=hrn, type='node').get_leaf())
44
45
46 def channel_to_hrn(auth, login_base, channel):
47     return NitosXrn(auth=auth + '.' + login_base, channel=channel).get_hrn()
48
49
50 def channel_to_urn(auth, login_base, channel):
51     return NitosXrn(auth=auth + '.' + login_base, channel=channel).get_urn()
52
53
54 def xrn_to_channel(hrn):
55     return Xrn.unescape(NitosXrn(xrn=hrn, type='channel').get_leaf())
56
57
58 class NitosXrn (Xrn):
59
60     @staticmethod
61     def site_hrn(auth, login_base):
62         return '.'.join([auth, login_base])
63
64     def __init__(self, auth=None, hostname=None, slicename=None, email=None, interface=None, channel=None, **kwargs):
65         # def hostname_to_hrn(auth_hrn, login_base, hostname):
66         if hostname is not None:
67             self.type = 'node'
68             # keep only the first part of the DNS name
69             #self.hrn='.'.join( [auth,hostname.split(".")[0] ] )
70             # escape the '.' in the hostname
71             self.hrn = '.'.join([auth, Xrn.escape(hostname)])
72             self.hrn_to_urn()
73         # def slicename_to_hrn(auth_hrn, slicename):
74         elif slicename is not None:
75             self.type = 'slice'
76             self.hrn = ".".join([auth] + [slicename.replace(".", "_")])
77             self.hrn_to_urn()
78         # def email_to_hrn(auth_hrn, email):
79         elif email is not None:
80             self.type = 'person'
81             # keep only the part before '@' and replace special chars into _
82             self.hrn = '.'.join(
83                 [auth, email.split('@')[0].replace(".", "_").replace("+", "_")])
84             self.hrn_to_urn()
85         elif interface is not None:
86             self.type = 'interface'
87             self.hrn = auth + '.' + interface
88             self.hrn_to_urn()
89         elif channel is not None:
90             self.type = 'channel'
91             self.hrn = ".".join([auth] + [channel])
92             self.hrn_to_urn()
93         else:
94             Xrn.__init__(self, **kwargs)
95
96     # def hrn_to_pl_slicename(hrn):
97     def nitos_slicename(self):
98         self._normalize()
99         leaf = self.leaf
100         sliver_id_parts = leaf.split(':')
101         name = sliver_id_parts[0]
102         name = re.sub('[^a-zA-Z0-9_]', '', name)
103         # return self.nitos_login_base() + '_' + name
104         return name
105
106     # def hrn_to_pl_authname(hrn):
107     def nitos_authname(self):
108         self._normalize()
109         return self.authority[-1]
110
111     def interface_name(self):
112         self._normalize()
113         return self.leaf
114
115     def nitos_login_base(self):
116         self._normalize()
117         if self.type and self.type.startswith('authority'):
118             base = self.leaf
119         else:
120             base = self.authority[-1]
121
122         # Fix up names of GENI Federates
123         base = base.lower()
124         base = re.sub('\\\[^a-zA-Z0-9]', '', base)
125
126         if len(base) > 20:
127             base = base[len(base) - 20:]
128
129         return base
130
131
132 if __name__ == '__main__':
133
134         #nitosxrn = NitosXrn(auth="omf.nitos",slicename="aminesl")
135         #slice_hrn = nitosxrn.get_hrn()
136         #slice_name = NitosXrn(xrn="omf.nitos.aminesl",type='slice').nitos_slicename()
137     slicename = "giorgos_n"
138     hrn = slicename_to_hrn("pla", "nitos", slicename)
139     print(hrn)