Merge branch 'geni-v3' of http://git.onelab.eu/sfa into geni-v3
[sfa.git] / sfa / iotlab / iotlabxrn.py
index c7a147c..bae7036 100644 (file)
@@ -1,4 +1,5 @@
-# specialized Xrn class for Dummy TB
+""" specialized Xrn class for Iotlab. SA
+"""
 import re
 from sfa.util.xrn import Xrn
 
@@ -31,31 +32,59 @@ def xrn_object(root_auth, hostname):
 
 # temporary helper functions to use this module instead of namespace
 def hostname_to_hrn (auth, hostname):
-    return IotlabXrn(auth=auth ,hostname=hostname).get_hrn()
+    """Turns node hostname into hrn.
+    :param auth: Site authority.
+    :type auth: string
+    :param hostname: Node hostname.
+    :type hostname: string.
+
+    :returns: Node's hrn.
+    :rtype: string
+    """
+    return IotlabXrn(auth=auth, hostname=hostname).get_hrn()
+
 def hostname_to_urn(auth, hostname):
-    return IotlabXrn(auth=auth,hostname=hostname).get_urn()
-def slicename_to_hrn (auth_hrn, slicename):
-    return IotlabXrn(auth=auth_hrn,slicename=slicename).get_hrn()
+    """Turns node hostname into urn.
+    :param auth: Site authority.
+    :type auth: string
+    :param hostname: Node hostname.
+    :type hostname: string.
 
-def hrn_to_iotlab_slicename (hrn):
-    return IotlabXrn(xrn=hrn,type='slice').iotlab_slicename()
-def hrn_to_iotlab_authname (hrn):
-    return IotlabXrn(xrn=hrn,type='any').iotlab_authname()
+    :returns: Node's urn.
+    :rtype: string
+    """
+    return IotlabXrn(auth=auth, hostname=hostname).get_urn()
 
+# def slicename_to_hrn (auth_hrn, slicename):
+    # return IotlabXrn(auth=auth_hrn, slicename=slicename).get_hrn()
+
+# def hrn_to_iotlab_slicename (hrn):
+#     return IotlabXrn(xrn=hrn, type='slice').iotlab_slicename()
+
+# def hrn_to_iotlab_authname (hrn):
+#     return IotlabXrn(xrn=hrn, type='any').iotlab_authname()
 
-class IotlabXrn (Xrn):
 
+class IotlabXrn (Xrn):
+    """
+    Defines methods to turn a hrn/urn into a urn/hrn, or to get the name
+    of the slice/user from the hrn.
+    """
     @staticmethod
     def site_hrn (auth):
+        """Returns the site hrn, which is also the testbed authority in
+            iotlab/cortexlab.
+        """
         return auth
 
-    def __init__ (self, auth=None, hostname=None, login=None, slicename=None,**kwargs):
+    def __init__ (self, auth=None, hostname=None, login=None,
+                                            slicename=None, **kwargs):
         #def hostname_to_hrn(auth_hrn, login_base, hostname):
         if hostname is not None:
-            self.type ='node'
+            self.type = 'node'
             # keep only the first part of the DNS name
             # escape the '.' in the hostname
-            self.hrn ='.'.join( [auth, Xrn.escape(hostname)] )
+            self.hrn = '.'.join( [auth, Xrn.escape(hostname)] )
             self.hrn_to_urn()
 
         elif login is not None:
@@ -64,17 +93,23 @@ class IotlabXrn (Xrn):
             self.hrn_to_urn()
         #def slicename_to_hrn(auth_hrn, slicename):
         elif slicename is not None:
-            self.type ='slice'
+            self.type = 'slice'
             slicename = '_'.join([login, "slice"])
             self.hrn = '.'.join([auth, slicename])
             self.hrn_to_urn()
             # split at the first _
 
         else:
-            Xrn.__init__ (self,**kwargs)
+            Xrn.__init__ (self, **kwargs)
+
 
-    #def hrn_to_pl_slicename(hrn):
     def iotlab_slicename (self):
+        """Returns the slice name from an iotlab slice hrn.
+
+        :rtype: string
+        :returns: slice name.
+        """
+
         self._normalize()
         leaf = self.leaf
         sliver_id_parts = leaf.split(':')
@@ -83,22 +118,22 @@ class IotlabXrn (Xrn):
         return name
 
     #def hrn_to_pl_authname(hrn):
-    def iotlab_authname (self):
-        self._normalize()
-        return self.authority[-1]
+    def iotlab_authname (self):
+        self._normalize()
+        return self.authority[-1]
 
-    def iotlab_login_base (self):
-        self._normalize()
-        if self.type and self.type.startswith('authority'):
-            base = self.leaf
-        else:
-            base = self.authority[-1]
+    def iotlab_login_base (self):
+        self._normalize()
+        if self.type and self.type.startswith('authority'):
+            base = self.leaf
+        else:
+            base = self.authority[-1]
 
-        # Fix up names of GENI Federates
-        base = base.lower()
-        base = re.sub('\\\[^a-zA-Z0-9]', '', base)
+        # Fix up names of GENI Federates
+        base = base.lower()
+        base = re.sub('\\\[^a-zA-Z0-9]', '', base)
 
-        if len(base) > 20:
-            base = base[len(base)-20:]
+        if len(base) > 20:
+            base = base[len(base)-20:]
 
-        return base
+        return base