Allocate, Describe, Provision now working for iotlab.
[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):
50         return auth
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         else:
74             Xrn.__init__ (self,**kwargs)
75
76     #def hrn_to_pl_slicename(hrn):
77     def iotlab_slicename (self):
78         self._normalize()
79         leaf = self.leaf
80         sliver_id_parts = leaf.split(':')
81         name = sliver_id_parts[0]
82         name = re.sub('[^a-zA-Z0-9_]', '', name)
83         return name
84
85     #def hrn_to_pl_authname(hrn):
86     def iotlab_authname (self):
87         self._normalize()
88         return self.authority[-1]
89
90     def iotlab_login_base (self):
91         self._normalize()
92         if self.type and self.type.startswith('authority'):
93             base = self.leaf
94         else:
95             base = self.authority[-1]
96
97         # Fix up names of GENI Federates
98         base = base.lower()
99         base = re.sub('\\\[^a-zA-Z0-9]', '', base)
100
101         if len(base) > 20:
102             base = base[len(base)-20:]
103
104         return base