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