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