Add new table JobSenslab so that one slice can
[sfa.git] / sfa / senslab / slabaggregate.py
index 4bddc8d..a64669b 100644 (file)
@@ -5,15 +5,11 @@ 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.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
@@ -26,9 +22,17 @@ from sfa.rspecs.version_manager import VersionManager
 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 +68,28 @@ 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:
-            try:
-                
-                for node_id 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),
-                                    'name': sfa_slice['slice_hrn'],
-                                    'type': 'slab-node', 
-                                    'tags': []})
-                    slivers[node_id] = sliver
-            except KeyError:
-                logger.log_exc("SLABAGGREGATE \t \
-                                        get_slice_and_slivers KeyError ")
+        for sfa_slice in slices:
+            if sfa_slice['oar_job_id'] is not -1:
+                try:
+                    
+                    for node_id 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),
+                                        'name': sfa_slice['slice_hrn'],
+                                        'type': 'slab-node', 
+                                        'tags': []})
+                        slivers[node_id] = sliver
+                except KeyError:
+                    logger.log_exc("SLABAGGREGATE \t \
+                                            get_slice_and_slivers KeyError ")
         ## sort sliver attributes by node id    
         ##tags = self.driver.GetSliceTags({'slice_tag_id': slice['slice_tag_ids']})
         ##for tag in tags:
@@ -169,16 +174,18 @@ class SlabAggregate:
             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'])
+
+            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')
-            #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')
+            
+            # 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
             
             #if not slice:
@@ -197,17 +204,18 @@ class SlabAggregate:
             # 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']
+            location = Location({'country':'France'})
+            rspec_node['location'] = location
+          
+           
+            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['location'] = location
-            except KeyError:
-                pass
+            rspec_node['position'] = position
             #rspec_node['interfaces'] = []
             #if_count=0
             #for if_id in node['interface_ids']:
@@ -238,13 +246,15 @@ class SlabAggregate:
                 #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 +270,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']) \
@@ -331,8 +343,9 @@ class SlabAggregate:
                 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)
+                        ldap_username %s rspec.version %s\r\n" %(ldap_username, rspec.version))
+                if version.type == "Slab":
+                    rspec.version.add_connection_information(ldap_username)
 
             default_sliver = slivers.get(None, [])
             if default_sliver: