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