9722ffed6a6f7ad53f67b121910560163f8dd933
[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 def hostname_to_hrn (auth, login_base, hostname):
9     return NitosXrn(auth=auth+'.'+login_base,hostname=hostname).get_hrn()
10 def hostname_to_urn(auth, login_base, hostname):
11     return NitosXrn(auth=auth+'.'+login_base,hostname=hostname).get_urn()
12 def slicename_to_hrn (auth_hrn,site_name,slicename):
13     return NitosXrn(auth=auth_hrn+'.'+site_name,slicename=slicename).get_hrn()
14 # hack to convert nitos user name to hrn
15 def username_to_hrn (auth_hrn,site_name,username):
16     return NitosXrn(auth=auth_hrn+'.'+site_name,slicename=username).get_hrn()
17 def email_to_hrn (auth_hrn, email):
18     return NitosXrn(auth=auth_hrn, email=email).get_hrn()
19 def hrn_to_nitos_slicename (hrn):
20     return NitosXrn(xrn=hrn,type='slice').nitos_slicename()
21 # removed-dangerous - was used for non-slice objects
22 #def hrn_to_nitos_login_base (hrn):
23 #    return NitosXrn(xrn=hrn,type='slice').nitos_login_base()
24 def hrn_to_nitos_authname (hrn):
25     return NitosXrn(xrn=hrn,type='any').nitos_authname()
26 def xrn_to_hostname(hrn):
27     return Xrn.unescape(NitosXrn(xrn=hrn, type='node').get_leaf())
28 def channel_to_hrn (auth, login_base, channel):
29     return NitosXrn(auth=auth+'.'+login_base, channel=channel).get_hrn()
30 def channel_to_urn (auth, login_base, channel):
31     return NitosXrn(auth=auth+'.'+login_base, channel=channel).get_urn()
32 def xrn_to_channel(hrn):
33     return Xrn.unescape(NitosXrn(xrn=hrn, type='channel').get_leaf())
34
35 class NitosXrn (Xrn):
36
37     @staticmethod 
38     def site_hrn (auth, login_base):
39         return '.'.join([auth,login_base])
40
41     def __init__ (self, auth=None, hostname=None, slicename=None, email=None, interface=None, channel=None, **kwargs):
42         #def hostname_to_hrn(auth_hrn, login_base, hostname):
43         if hostname is not None:
44             self.type='node'
45             # keep only the first part of the DNS name
46             #self.hrn='.'.join( [auth,hostname.split(".")[0] ] )
47             # escape the '.' in the hostname
48             self.hrn='.'.join( [auth,Xrn.escape(hostname)] )
49             self.hrn_to_urn()
50         #def slicename_to_hrn(auth_hrn, slicename):
51         elif slicename is not None:
52             self.type='slice'
53             self.hrn = ".".join([auth] + [slicename.replace(".", "_")])
54             self.hrn_to_urn()
55         #def email_to_hrn(auth_hrn, email):
56         elif email is not None:
57             self.type='person'
58             # keep only the part before '@' and replace special chars into _
59             self.hrn='.'.join([auth,email.split('@')[0].replace(".", "_").replace("+", "_")])
60             self.hrn_to_urn()
61         elif interface is not None:
62             self.type = 'interface'
63             self.hrn = auth + '.' + interface
64             self.hrn_to_urn()
65         elif channel is not None:
66             self.type='channel'
67             self.hrn=".".join([auth] + [channel])
68             self.hrn_to_urn()
69         else:
70             Xrn.__init__ (self,**kwargs)
71
72     #def hrn_to_pl_slicename(hrn):
73     def nitos_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 self.nitos_login_base() + '_' + name
80         return name
81
82     #def hrn_to_pl_authname(hrn):
83     def nitos_authname (self):
84         self._normalize()
85         return self.authority[-1]
86
87     def interface_name(self):
88         self._normalize()
89         return self.leaf
90
91     def nitos_login_base (self):
92         self._normalize()
93         if self.type and self.type.startswith('authority'):
94             base = self.leaf 
95         else:
96             base = self.authority[-1]
97         
98         # Fix up names of GENI Federates
99         base = base.lower()
100         base = re.sub('\\\[^a-zA-Z0-9]', '', base)
101
102         if len(base) > 20:
103             base = base[len(base)-20:]
104         
105         return base
106
107
108 if __name__ == '__main__':
109
110         #nitosxrn = NitosXrn(auth="omf.nitos",slicename="aminesl")
111         #slice_hrn = nitosxrn.get_hrn()
112         #slice_name = NitosXrn(xrn="omf.nitos.aminesl",type='slice').nitos_slicename()
113         slicename = "giorgos_n"
114         hrn = slicename_to_hrn("pla", "nitos", slicename)
115         print(hrn)