1 """ specialized Xrn class for Iotlab. SA
4 from sfa.util.xrn import Xrn
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)
11 :returns: node's hostname.
15 return Xrn.unescape(Xrn(xrn=xrn, type='node').get_leaf())
18 def xrn_object(root_auth, hostname):
19 """Creates a valid xrn object from the node's hostname and the authority
22 :param hostname: the node's hostname.
23 :param root_auth: the SFA root authority.
24 :type hostname: string
25 :type root_auth: string
27 :returns: the iotlab node's xrn
31 return Xrn('.'.join([root_auth, Xrn.escape(hostname)]), type='node')
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.
38 :param hostname: Node hostname.
39 :type hostname: string.
44 return IotlabXrn(auth=auth, hostname=hostname).get_hrn()
46 def hostname_to_urn(auth, hostname):
47 """Turns node hostname into urn.
48 :param auth: Site authority.
50 :param hostname: Node hostname.
51 :type hostname: string.
56 return IotlabXrn(auth=auth, hostname=hostname).get_urn()
58 # def slicename_to_hrn (auth_hrn, slicename):
59 # return IotlabXrn(auth=auth_hrn, slicename=slicename).get_hrn()
61 # def hrn_to_iotlab_slicename (hrn):
62 # return IotlabXrn(xrn=hrn, type='slice').iotlab_slicename()
64 # def hrn_to_iotlab_authname (hrn):
65 # return IotlabXrn(xrn=hrn, type='any').iotlab_authname()
68 class IotlabXrn (Xrn):
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.
75 """Returns the site hrn, which is also the testbed authority in
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:
85 # keep only the first part of the DNS name
86 # escape the '.' in the hostname
87 self.hrn = '.'.join( [auth, Xrn.escape(hostname)] )
90 elif login is not None:
92 self.hrn = '.'.join([auth, login])
94 #def slicename_to_hrn(auth_hrn, slicename):
95 elif slicename is not None:
97 slicename = '_'.join([login, "slice"])
98 self.hrn = '.'.join([auth, slicename])
100 # split at the first _
103 Xrn.__init__ (self, **kwargs)
106 def iotlab_slicename (self):
107 """Returns the slice name from an iotlab slice hrn.
110 :returns: slice name.
115 sliver_id_parts = leaf.split(':')
116 name = sliver_id_parts[0]
117 name = re.sub('[^a-zA-Z0-9_]', '', name)
120 #def hrn_to_pl_authname(hrn):
121 # def iotlab_authname (self):
123 # return self.authority[-1]
125 # def iotlab_login_base (self):
127 # if self.type and self.type.startswith('authority'):
130 # base = self.authority[-1]
132 # # Fix up names of GENI Federates
133 # base = base.lower()
134 # base = re.sub('\\\[^a-zA-Z0-9]', '', base)
137 # base = base[len(base)-20:]