Merge branch 'geni-v3' into dbsession
[sfa.git] / sfa / planetlab / plaggregate.py
index b5e8073..399b3a2 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/python
 from collections import defaultdict
-from sfa.util.xrn import Xrn, hrn_to_urn, urn_to_hrn
+from sfa.util.xrn import Xrn, hrn_to_urn, urn_to_hrn, get_authority, get_leaf
 from sfa.util.sfatime import utcparse, datetime_to_string
 from sfa.util.sfalogging import logger
 from sfa.util.faults import SliverDoesNotExist
@@ -18,10 +18,9 @@ from sfa.rspecs.elements.lease import Lease
 from sfa.rspecs.elements.granularity import Granularity
 from sfa.rspecs.version_manager import VersionManager
 
-from sfa.planetlab.plxrn import PlXrn, hostname_to_urn, hrn_to_pl_slicename, slicename_to_hrn
+from sfa.planetlab.plxrn import PlXrn, hostname_to_urn, hrn_to_pl_slicename, slicename_to_hrn, top_auth, hash_loginbase
 from sfa.planetlab.vlink import get_tc_rate
 from sfa.planetlab.topology import Topology
-from sfa.storage.alchemy import dbsession
 from sfa.storage.model import SliverAllocation
 
 
@@ -129,7 +128,17 @@ class PlAggregate:
                 except ValueError:
                     pass 
             else:  
-                names.add(xrn.pl_slicename())
+                slice_hrn = xrn.get_hrn()
+                top_auth_hrn = top_auth(slice_hrn)
+                site_hrn = '.'.join(slice_hrn.split('.')[:-1])
+                slice_part = slice_hrn.split('.')[-1]
+                if top_auth_hrn == self.driver.hrn:
+                    login_base = slice_hrn.split('.')[-2][:12]
+                else:
+                    login_base = hash_loginbase(site_hrn)
+
+                slice_name = '_'.join([login_base, slice_part])
+                names.add(slice_name)
 
         filter = {}
         if names:
@@ -141,7 +150,7 @@ class PlAggregate:
         if not slices:
             return []
         slice = slices[0]     
-        slice['hrn'] = PlXrn(auth=self.driver.hrn, slicename=slice['name']).hrn   
+        slice['hrn'] = slice_hrn   
 
         # get sliver users
         persons = []
@@ -165,10 +174,10 @@ class PlAggregate:
         # construct user key info
         users = []
         for person in persons:
-            name = person['email'][0:person['email'].index('@')]
+            person_urn = hrn_to_urn(self.driver.shell.GetPersonHrn(int(person['person_id'])), 'user')
             user = {
                 'login': slice['name'], 
-                'user_urn': Xrn('%s.%s' % (self.driver.hrn, name), type='user').urn,
+                'user_urn': person_urn,
                 'keys': [keys[k_id]['key'] for k_id in person['key_ids'] if k_id in keys]
             }
             users.append(user)
@@ -193,7 +202,7 @@ class PlAggregate:
         rspec_node = NodeElement()
         # xxx how to retrieve site['login_base']
         site=sites[node['site_id']]
-        rspec_node['component_id'] = PlXrn(self.driver.hrn, hostname=node['hostname']).get_urn()
+        rspec_node['component_id'] = hostname_to_urn(self.driver.hrn, site['login_base'], node['hostname'])
         rspec_node['component_name'] = node['hostname']
         rspec_node['component_manager_id'] = Xrn(self.driver.hrn, 'authority+cm').get_urn()
         rspec_node['authority_id'] = hrn_to_urn(PlXrn.site_hrn(self.driver.hrn, site['login_base']), 'authority+sa')
@@ -203,7 +212,13 @@ class PlAggregate:
             rspec_node['available'] = 'true'
         else:
             rspec_node['available'] = 'false'
-        rspec_node['exclusive'] = 'false'
+
+        #distinguish between Shared and Reservable nodes
+        if node['node_type'] == 'reservable':
+            rspec_node['exclusive'] = 'true'
+        else:
+            rspec_node['exclusive'] = 'false'
+
         rspec_node['hardware_types'] = [HardwareType({'name': 'plab-pc'}),
                                         HardwareType({'name': 'pc'})]
         # only doing this because protogeni rspec needs
@@ -311,6 +326,9 @@ class PlAggregate:
                     op_status = 'geni_unknown'
             else:
                 allocation_status = 'geni_unallocated'    
+        else:
+            allocation_status = 'geni_unallocated'
+            op_status = 'geni_failed'
         # required fields
         geni_sliver = {'geni_sliver_urn': rspec_node['sliver_id'],
                        'geni_expires': rspec_node['expires'],
@@ -322,9 +340,6 @@ class PlAggregate:
 
     def get_leases(self, slice=None, options={}):
         
-        if slice_xrn and not slice:
-            return []
-
         now = int(time.time())
         filter={}
         filter.update({'clip':now})
@@ -350,8 +365,8 @@ class PlAggregate:
             site_id=lease['site_id']
             site=sites_dict[site_id]
 
-            rspec_lease['component_id'] = hostname_to_urn(self.driver.hrn, site['login_base'], lease['hostname'])
-            slice_hrn = slicename_to_hrn(self.driver.hrn, lease['name'])
+            rspec_lease['component_id'] = hrn_to_urn(self.driver.shell.GetNodeHrn(lease['hostname']), 'node')
+            slice_hrn = self.driver.shell.GetSliceHrn(lease['slice_id'])
             slice_urn = hrn_to_urn(slice_hrn, 'slice')
             rspec_lease['slice_id'] = slice_urn
             rspec_lease['start_time'] = lease['t_from']