5d84249412981683d5b54d3c9d18f47cfc1b0e33
[sfa.git] / sfa / iotlab / iotlabxrn.py
1 # specialized Xrn class for Dummy TB
2 import re
3 from sfa.util.xrn import Xrn
4
5 def xrn_to_hostname(xrn):
6     """Returns a node's hostname from its xrn.
7     :param xrn: The nodes xrn identifier.
8     :type xrn: Xrn (from sfa.util.xrn)
9
10     :returns: node's hostname.
11     :rtype: string
12
13     """
14     return Xrn.unescape(Xrn(xrn=xrn, type='node').get_leaf())
15
16
17 def xrn_object(root_auth, hostname):
18     """Creates a valid xrn object from the node's hostname and the authority
19     of the SFA server.
20
21     :param hostname: the node's hostname.
22     :param root_auth: the SFA root authority.
23     :type hostname: string
24     :type root_auth: string
25
26     :returns: the iotlab node's xrn
27     :rtype: Xrn
28
29     """
30     return Xrn('.'.join([root_auth, Xrn.escape(hostname)]), type='node')
31
32 # temporary helper functions to use this module instead of namespace
33 def hostname_to_hrn (auth, hostname):
34     return IotlabXrn(auth=auth ,hostname=hostname).get_hrn()
35 def hostname_to_urn(auth, hostname):
36     return IotlabXrn(auth=auth,hostname=hostname).get_urn()
37 def slicename_to_hrn (auth_hrn, slicename):
38     return IotlabXrn(auth=auth_hrn,slicename=slicename).get_hrn()
39
40 def hrn_to_iotlab_slicename (hrn):
41     return IotlabXrn(xrn=hrn,type='slice').iotlab_slicename()
42 def hrn_to_iotlab_authname (hrn):
43     return IotlabXrn(xrn=hrn,type='any').iotlab_authname()
44
45
46 class IotlabXrn (Xrn):
47
48     @staticmethod
49     def site_hrn (auth, testbed_name):
50         return '.'.join([auth, testbed_name])
51
52     def __init__ (self, auth=None, hostname=None, login=None, slicename=None,**kwargs):
53         #def hostname_to_hrn(auth_hrn, login_base, hostname):
54         if hostname is not None:
55             self.type ='node'
56             # keep only the first part of the DNS name
57             # escape the '.' in the hostname
58             self.hrn ='.'.join( [auth, Xrn.escape(hostname)] )
59             self.hrn_to_urn()
60
61         elif login is not None:
62             self.type = 'person'
63             self.hrn = '.'.join([auth, login])
64             self.hrn_to_urn()
65         #def slicename_to_hrn(auth_hrn, slicename):
66         elif slicename is not None:
67             self.type ='slice'
68             slicename = '_'.join([login, "slice"])
69             self.hrn = '.'.join([auth, slicename])
70             self.hrn_to_urn()
71             # split at the first _
72
73
74
75         else:
76             Xrn.__init__ (self,**kwargs)
77
78     #def hrn_to_pl_slicename(hrn):
79     def iotlab_slicename (self):
80         self._normalize()
81         leaf = self.leaf
82         sliver_id_parts = leaf.split(':')
83         name = sliver_id_parts[0]
84         name = re.sub('[^a-zA-Z0-9_]', '', name)
85         return name
86
87     #def hrn_to_pl_authname(hrn):
88     def iotlab_authname (self):
89         self._normalize()
90         return self.authority[-1]
91
92     def iotlab_login_base (self):
93         self._normalize()
94         if self.type and self.type.startswith('authority'):
95             base = self.leaf
96         else:
97             base = self.authority[-1]
98
99         # Fix up names of GENI Federates
100         base = base.lower()
101         base = re.sub('\\\[^a-zA-Z0-9]', '', base)
102
103         if len(base) > 20:
104             base = base[len(base)-20:]
105
106         return base