Adding sfi tests in TestSuite.
[sfa.git] / sfa / senslab / slabaggregate.py
index 4bddc8d..52c76bc 100644 (file)
@@ -1,19 +1,12 @@
-#import httplib
-#import json
 import time
 
 
-#from sfa.util.config import Config
-from sfa.util.xrn import hrn_to_urn, urn_to_hrn, urn_to_sliver_id
-from sfa.planetlab.plxrn import PlXrn, hostname_to_urn, slicename_to_hrn
+from sfa.util.xrn import hrn_to_urn, urn_to_hrn
 
 from sfa.rspecs.rspec import RSpec
-from sfa.rspecs.elements.versions.slabv1Node import SlabLocation
-from sfa.storage.alchemy import dbsession
-from sfa.storage.model import  RegRecord
-#from sfa.rspecs.elements.location import Location
+from sfa.rspecs.elements.versions.slabv1Node import SlabPosition
+from sfa.rspecs.elements.location import Location
 from sfa.rspecs.elements.hardware_type import HardwareType
-from sfa.rspecs.elements.node import Node
 #from sfa.rspecs.elements.login import Login
 #from sfa.rspecs.elements.services import Services
 from sfa.rspecs.elements.sliver import Sliver
@@ -21,14 +14,21 @@ from sfa.rspecs.elements.lease import Lease
 from sfa.rspecs.elements.granularity import Granularity
 from sfa.rspecs.version_manager import VersionManager
 
-#from sfa.util.sfatime import datetime_to_epoch
 
 from sfa.rspecs.elements.versions.slabv1Node import SlabNode
 from sfa.util.sfalogging import logger
 
+from sfa.util.xrn import Xrn
 
-def hostname_to_hrn(root_auth, login_base, hostname):
-    return PlXrn(auth=root_auth, hostname=login_base + '_' +hostname).get_hrn()
+def slab_xrn_to_hostname(xrn):
+    return Xrn.unescape(Xrn(xrn=xrn, type='node').get_leaf())
+
+def slab_xrn_object(root_auth, hostname):
+    """Attributes are urn and hrn.
+    Get the hostname using slab_xrn_to_hostname on the urn.
+    
+    """
+    return Xrn('.'.join( [root_auth, Xrn.escape(hostname)]), type='node')
 
 class SlabAggregate:
 
@@ -64,27 +64,34 @@ class SlabAggregate:
                                                                     %(slices))
         if not slices:
             return (sfa_slice, slivers)
-        if isinstance(sfa_slice, list):
-            sfa_slice = slices[0]
-        else:
-            sfa_slice = slices
+        #if isinstance(sfa_slice, list):
+            #sfa_slice = slices[0]
+        #else:
+            #sfa_slice = slices
 
         # sort slivers by node id , if there is a job
         #and therfore, node allocated to this slice
-        if sfa_slice['oar_job_id'] is not -1:
+        for sfa_slice in slices:
             try:
-                
-                for node_id in sfa_slice['node_ids']:
+                   
+                for node in sfa_slice['node_ids']:
                     #node_id = self.driver.root_auth + '.' + node_id
-                    sliver = Sliver({'sliver_id': urn_to_sliver_id(slice_urn, \
-                                    sfa_slice['record_id_slice'], node_id),
+                    sliver = Sliver({'sliver_id': Xrn(slice_urn, type='slice', id=node['hostname'], authority=self.driver.hrn).urn, 
                                     'name': sfa_slice['slice_hrn'],
                                     'type': 'slab-node', 
                                     'tags': []})
-                    slivers[node_id] = sliver
+                   
+                    slivers[node['hostname']] = sliver
             except KeyError:
                 logger.log_exc("SLABAGGREGATE \t \
                                         get_slice_and_slivers KeyError ")
+        
+        #Add default sliver attribute :
+        #connection information for senslab
+        tmp = sfa_slice['slice_hrn'].split('.')
+        ldap_username = tmp[1].split('_')[0]
+        vmaddr = 'ssh ' + ldap_username + '@grenoble.senslab.info'
+        slivers['default_sliver'] =  {'vm': vmaddr , 'login': ldap_username}
         ## sort sliver attributes by node id    
         ##tags = self.driver.GetSliceTags({'slice_tag_id': slice['slice_tag_ids']})
         ##for tag in tags:
@@ -97,7 +104,7 @@ class SlabAggregate:
             ##slivers[tag['node_id']]['tags'].append(tag)
         logger.debug("SLABAGGREGATE api get_slice_and_slivers  slivers %s "\
                                                              %(slivers))
-        return (sfa_slice, slivers)
+        return (slices, slivers)
             
 
         
@@ -109,6 +116,9 @@ class SlabAggregate:
         # but what is the role of the slivers parameter ?
         # So i assume that slice['node_ids'] will be the same as slivers for us
         #filter_dict = {}
+        #if slice_xrn:
+            #if not slices or not slices['node_ids']:
+                #return ([],[])
         tags_filter = {}
         
         # get the granularity in second for the reservation system
@@ -141,9 +151,7 @@ class SlabAggregate:
         #tag_ids = []
         nodes_dict = {}
         for node in nodes:
-            #site_ids.append(node['site_id'])
-            #interface_ids.extend(node['interface_ids'])
-            #tag_ids.extend(node['node_tag_ids'])
+           
             nodes_dict[node['node_id']] = node
         
         # get sites
@@ -152,99 +160,103 @@ class SlabAggregate:
         #interfaces = self.get_interfaces({'interface_id':interface_ids}) 
         # get tags
         #node_tags = self.get_node_tags(tags_filter)
-       
-
-        reserved_nodes = self.driver.GetNodesCurrentlyInUse()
+        
+        #if slices, this means we got to list all the nodes given to this slice
+        # Make a list of all the nodes in the slice before getting their attributes
         rspec_nodes = []
+        slice_nodes_list = []
+        if slices:
+            for one_slice in slices:
+                for node in one_slice['node_ids']:
+                    slice_nodes_list.append(node['hostname'])
+                   
+        reserved_nodes = self.driver.GetNodesCurrentlyInUse()
+        logger.debug("SLABAGGREGATE api get_rspec slice_nodes_list  %s "\
+                                                             %(slice_nodes_list)) 
         for node in nodes:
             # skip whitelisted nodes
             #if node['slice_ids_whitelist']:
                 #if not slice or slice['slice_id'] not in node['slice_ids_whitelist']:
                     #continue
             #rspec_node = Node()
-            rspec_node = SlabNode()
-            # xxx how to retrieve site['login_base']
-            #site_id=node['site_id']
-            #site=sites_dict[site_id]
-            rspec_node['mobile'] = node['mobile']
-            rspec_node['archi'] = node['archi']
-            rspec_node['radio'] = node['radio']
-            rspec_node['component_id'] = \
-                                        hostname_to_urn(self.driver.root_auth, \
-                                        node['site'], node['hostname'])
-            rspec_node['component_name'] = node['hostname']  
-            rspec_node['component_manager_id'] = \
-                            hrn_to_urn(self.driver.root_auth, 'authority+sa')
-            #rspec_node['component_manager_id'] = Xrn(self.driver.root_auth, 'authority+sa').get_urn()
-            rspec_node['authority_id'] = \
-                hrn_to_urn(PlXrn.site_hrn(self.driver.root_auth, \
-                                                node['site']), 'authority+sa')
-            # do not include boot state (<available> element) in the manifest rspec
+            logger.debug("SLABAGGREGATE api get_rspec node  %s "\
+                                                             %(node)) 
+            if slice_nodes_list == [] or node['hostname'] in slice_nodes_list:
+                   
+                rspec_node = SlabNode()
+                # xxx how to retrieve site['login_base']
+                #site_id=node['site_id']
+                #site=sites_dict[site_id]
+                rspec_node['mobile'] = node['mobile']
+                rspec_node['archi'] = node['archi']
+                rspec_node['radio'] = node['radio']
+    
+                slab_xrn = slab_xrn_object(self.driver.root_auth, node['hostname'])
+                rspec_node['component_id'] = slab_xrn.urn
+                rspec_node['component_name'] = node['hostname']  
+                rspec_node['component_manager_id'] = \
+                                hrn_to_urn(self.driver.root_auth, 'authority+sa')
+                
+                # Senslab's nodes are federated : there is only one authority 
+                # for all Senslab sites, registered in SFA.
+                # Removing the part including the site in authority_id SA 27/07/12
+                rspec_node['authority_id'] = rspec_node['component_manager_id']  
+    
+                # do not include boot state (<available> element) in the manifest rspec
+                
+               
+                rspec_node['boot_state'] = node['boot_state']
+                if node['hostname'] in reserved_nodes:
+                    rspec_node['boot_state'] = "Reserved"
+                rspec_node['exclusive'] = 'true'
+                rspec_node['hardware_types'] = [HardwareType({'name': 'slab-node'})]
+    
+                # only doing this because protogeni rspec needs
+                # to advertise available initscripts 
+                #rspec_node['pl_initscripts'] = None
+                # add site/interface info to nodes.
+                # assumes that sites, interfaces and tags have already been prepared.
+                #site = sites_dict[node['site_id']]
+                location = Location({'country':'France'})
+                rspec_node['location'] = location
             
-            #if not slice:
-            #    rspec_node['boot_state'] = node['boot_state']
-            #    if node['hostname'] in reserved_nodes:
-            #        rspec_node['boot_state'] = "Reserved"
-            rspec_node['boot_state'] = node['boot_state']
-            if node['hostname'] in reserved_nodes:
-                rspec_node['boot_state'] = "Reserved"
-            rspec_node['exclusive'] = 'True'
-            rspec_node['hardware_types'] = [HardwareType({'name': 'slab-node'})]
-
-            # only doing this because protogeni rspec needs
-            # to advertise available initscripts 
-            #rspec_node['pl_initscripts'] = None
-            # add site/interface info to nodes.
-            # assumes that sites, interfaces and tags have already been prepared.
-            #site = sites_dict[node['site_id']]
-         
-            try:
-                if node['posx'] and node['posy'] and node['posz']:  
-                    location = SlabLocation()
-                    location['longitude'] = node['posx']
-                    location['latitude'] = node['posy']
-                    location['hauteur'] = node['posz']
+            
+                position = SlabPosition()
+                for field in position :
+                    try:
+                        position[field] = node[field]
+                    except KeyError, error :
+                        logger.log_exc("SLABAGGREGATE\t get_rspec position %s "%(error))
+    
+                rspec_node['position'] = position
+                #rspec_node['interfaces'] = []
+               
+                #tags = [PLTag(node_tags[tag_id]) for tag_id in node['node_tag_ids']]
+                # Granularity
+                granularity = Granularity({'grain': grain})
+                rspec_node['granularity'] = granularity
+                rspec_node['tags'] = []
+                if node['hostname'] in slivers:
+                    # add sliver info
+                    sliver = slivers[node['hostname']]
+                    rspec_node['sliver_id'] = sliver['sliver_id']
+                    rspec_node['client_id'] = node['hostname']
+                    rspec_node['slivers'] = [sliver]
+                    
+                    # slivers always provide the ssh service
+                    #login = Login({'authentication': 'ssh-keys', 'hostname': node['hostname'], 'port':'22', 'username': sliver['name']})
+                    #service = Services({'login': login})
+                    #rspec_node['services'] = [service]
+                rspec_nodes.append(rspec_node)
 
-                    rspec_node['location'] = location
-            except KeyError:
-                pass
-            #rspec_node['interfaces'] = []
-            #if_count=0
-            #for if_id in node['interface_ids']:
-                #interface = Interface(interfaces[if_id]) 
-                #interface['ipv4'] = interface['ip']
-                #interface['component_id'] = PlXrn(auth=self.driver.hrn, 
-                                                #interface='node%s:eth%s' % (node['node_id'], if_count)).get_urn()
-                # interfaces in the manifest need a client id
-                #if slice:
-                    #interface['client_id'] = "%s:%s" % (node['node_id'], if_id)            
-                #rspec_node['interfaces'].append(interface)
-                #if_count+=1
-        
-            #tags = [PLTag(node_tags[tag_id]) for tag_id in node['node_tag_ids']]
-            # Granularity
-            granularity = Granularity({'grain': grain})
-            rspec_node['granularity'] = granularity
-            rspec_node['tags'] = []
-            if node['hostname'] in slivers:
-                # add sliver info
-                sliver = slivers[node['hostname']]
-                rspec_node['sliver_id'] = sliver['sliver_id']
-                rspec_node['client_id'] = node['hostname']
-                rspec_node['slivers'] = [sliver]
-                
-                # slivers always provide the ssh service
-                #login = Login({'authentication': 'ssh-keys', 'hostname': node['hostname'], 'port':'22', 'username': sliver['name']})
-                #service = Services({'login': login})
-                #rspec_node['services'] = [service]
-            rspec_nodes.append(rspec_node)
-        #logger.debug("SLABAGGREGATE \t get_nodes rspec_nodes %s"%(rspec_nodes))
         return (rspec_nodes)       
 
     def get_leases(self, slice_record = None, options = {}):
     
         now = int(time.time())
         lease_filter = {'clip': now }
+        
+        #self.driver.synchronize_oar_and_slice_table()
         #if slice_record:
             #lease_filter.update({'name': slice_record['name']})
         return_fields = ['lease_id', 'hostname', 'site_id', \
@@ -260,8 +272,10 @@ class SlabAggregate:
                 rspec_lease = Lease()
                 rspec_lease['lease_id'] = lease['lease_id']
                 site = node['site_id']
-                rspec_lease['component_id'] = hostname_to_urn(self.driver.hrn, \
-                                        site, node['hostname'])
+                slab_xrn = slab_xrn_object(self.driver.root_auth, node['hostname'])
+                rspec_lease['component_id'] = slab_xrn.urn
+                #rspec_lease['component_id'] = hostname_to_urn(self.driver.hrn, \
+                                        #site, node['hostname'])
                 rspec_lease['slice_id'] = lease['slice_id']
                 rspec_lease['start_time'] = lease['t_from']
                 rspec_lease['duration'] = (lease['t_until'] - lease['t_from']) \
@@ -317,9 +331,21 @@ class SlabAggregate:
         #nodes, links = self.get_nodes(slice, slivers)
         logger.debug("\r\n \r\n SlabAggregate \tget_rspec ******* slice_xrn %s \r\n \r\n"\
                                             %(slice_xrn)) 
-        if not options.get('list_leases') or options.get('list_leases') and options['list_leases'] != 'leases':
+                                            
+        try:                                    
+            lease_option = options['list_leases']
+        except KeyError:
+            #If no options are specified, at least print the resources
+            if slice_xrn :
+                lease_option = 'all'
+            pass 
+        
+        if lease_option in ['all', 'resources']:
+        #if not options.get('list_leases') or options.get('list_leases') and options['list_leases'] != 'leases':
             nodes = self.get_nodes(slices, slivers) 
-            #In case creating a job slice _xrn is not set to None
+            logger.debug("SlabAggregate \tget_rspec **** \
+                        nodes %s \r\n" %(nodes))
+            #In case creating a job,  slice_xrn is not set to None
             rspec.version.add_nodes(nodes)
             if slice_xrn :
                 #Get user associated with this slice
@@ -327,24 +353,26 @@ class SlabAggregate:
                                                 #slices['record_id_user']).first()
 
                 #ldap_username = (user.hrn).split('.')[1]
-                ldap_username = slices['slice_hrn']
+                
+                
+                #for one_slice in slices :
+                ldap_username = slices[0]['slice_hrn']
                 tmp = ldap_username.split('.')
                 ldap_username = tmp[1].split('_')[0]
-                logger.debug("SlabAggregate \tget_rspec **** \
-                        ldap_username %s \r\n" %(ldap_username))
-                rspec.version.add_connection_information(ldap_username)
+              
+                if version.type == "Slab":
+                    rspec.version.add_connection_information(ldap_username)
 
-            default_sliver = slivers.get(None, [])
+            default_sliver = slivers.get('default_sliver', [])
             if default_sliver:
-                default_sliver_attribs = default_sliver.get('tags', [])
+                #default_sliver_attribs = default_sliver.get('tags', [])
                 logger.debug("SlabAggregate \tget_rspec **** \
-                        default_sliver_attribs %s \r\n" %(default_sliver_attribs))
-                for attrib in default_dbsessionsliver_attribs:
-                    
-    
-                    rspec.version.add_default_sliver_attribute(attrib['tagname'], \
-                                                                attrib['value'])   
-        if options.get('list_leases') or options.get('list_leases') and options['list_leases'] != 'resources':
+                        default_sliver%s \r\n" %(default_sliver))
+                for attrib in default_sliver:
+                    rspec.version.add_default_sliver_attribute(attrib, \
+                                                               default_sliver[attrib])  
+        if lease_option in ['all','leases']:                                                         
+        #if options.get('list_leases') or options.get('list_leases') and options['list_leases'] != 'resources':
             leases = self.get_leases(slices)
             rspec.version.add_leases(leases)