Provision(), Delete() and PerformOperationalAction() handle single slivers
[sfa.git] / sfa / planetlab / plslices.py
index 703dac3..1417984 100644 (file)
@@ -10,7 +10,6 @@ from sfa.planetlab.vlink import VLink
 from sfa.planetlab.topology import Topology
 from sfa.planetlab.plxrn import PlXrn, hrn_to_pl_slicename, xrn_to_hostname, top_auth, hash_loginbase
 from sfa.storage.model import SliverAllocation
-from sfa.storage.alchemy import dbsession
 
 MAXINT =  2L**31-1
 
@@ -126,27 +125,6 @@ class PlSlices:
 
         return slivers
  
-    def get_peer(self, xrn):
-        hrn, type = urn_to_hrn(xrn)
-        # Becaues of myplc federation,  we first need to determine if this
-        # slice belongs to out local plc or a myplc peer. We will assume it 
-        # is a local site, unless we find out otherwise  
-        peer = None
-
-        # get this slice's authority (site)
-        slice_authority = get_authority(hrn)
-
-        # get this site's authority (sfa root authority or sub authority)
-        site_authority = get_authority(slice_authority).lower()
-
-        # check if we are already peered with this site_authority, if so
-        peers = self.driver.shell.GetPeers({}, ['peer_id', 'peername', 'shortname', 'hrn_root'])
-        for peer_record in peers:
-            names = [name.lower() for name in peer_record.values() if isinstance(name, StringTypes)]
-            if site_authority in names:
-                peer = peer_record
-
-        return peer
 
     def get_sfa_peer(self, xrn):
         hrn, type = urn_to_hrn(xrn)
@@ -161,7 +139,7 @@ class PlSlices:
 
         return sfa_peer
 
-    def verify_slice_leases(self, slice, rspec_requested_leases, peer):
+    def verify_slice_leases(self, slice, rspec_requested_leases):
 
         leases = self.driver.shell.GetLeases({'name':slice['name'], 'clip':int(time.time())}, ['lease_id','name', 'hostname', 't_from', 't_until'])
         grain = self.driver.shell.GetLeaseGranularity()
@@ -215,8 +193,6 @@ class PlSlices:
    
 
         try:
-            if peer:
-                self.driver.shell.UnBindObjectFromPeer('slice', slice['slice_id'], peer['shortname'])
             self.driver.shell.DeleteLeases(deleted_leases_id)
             for lease in added_leases:
                 self.driver.shell.AddLeases(lease['hostname'], slice['name'], lease['t_from'], lease['t_until'])
@@ -227,7 +203,7 @@ class PlSlices:
         return leases
 
 
-    def verify_slice_nodes(self, slice_urn, slice, rspec_nodes, peer):
+    def verify_slice_nodes(self, slice_urn, slice, rspec_nodes):
         
         slivers = {}
         for node in rspec_nodes:
@@ -251,8 +227,6 @@ class PlSlices:
         added_nodes = list(set(slivers.keys()).difference(current_slivers))        
 
         try:
-            if peer:
-                self.driver.shell.UnBindObjectFromPeer('slice', slice['slice_id'], peer['shortname'])
             self.driver.shell.AddSliceToNodes(slice['name'], added_nodes)
             self.driver.shell.DeleteSliceFromNodes(slice['name'], deleted_nodes)
             
@@ -272,7 +246,7 @@ class PlSlices:
                                       component_id=component_id,
                                       slice_urn = slice_urn, 
                                       allocation_state='geni_allocated')      
-            record.sync()
+            record.sync(self.driver.api.dbsession())
         return resulting_nodes
 
     def free_egre_key(self):
@@ -348,43 +322,7 @@ class PlSlices:
                         
         
 
-    def handle_peer(self, site, slice, persons, peer):
-        if peer:
-            # bind site
-            try:
-                if site:
-                    self.driver.shell.BindObjectToPeer('site', site['site_id'], peer['shortname'], slice['site_id'])
-            except Exception,e:
-                self.driver.shell.DeleteSite(site['site_id'])
-                raise e
-            
-            # bind slice
-            try:
-                if slice:
-                    self.driver.shell.BindObjectToPeer('slice', slice['slice_id'], peer['shortname'], slice['slice_id'])
-            except Exception,e:
-                self.driver.shell.DeleteSlice(slice['slice_id'])
-                raise e 
-
-            # bind persons
-            for person in persons:
-                try:
-                    self.driver.shell.BindObjectToPeer('person', 
-                                                     person['person_id'], peer['shortname'], person['peer_person_id'])
-
-                    for (key, remote_key_id) in zip(person['keys'], person['key_ids']):
-                        try:
-                            self.driver.shell.BindObjectToPeer( 'key', key['key_id'], peer['shortname'], remote_key_id)
-                        except:
-                            self.driver.shell.DeleteKey(key['key_id'])
-                            logger("failed to bind key: %s to peer: %s " % (key['key_id'], peer['shortname']))
-                except Exception,e:
-                    self.driver.shell.DeletePerson(person['person_id'])
-                    raise e       
-
-        return slice
-
-    def verify_site(self, slice_xrn, slice_record={}, peer=None, sfa_peer=None, options={}):
+    def verify_site(self, slice_xrn, slice_record={}, sfa_peer=None, options={}):
         (slice_hrn, type) = urn_to_hrn(slice_xrn)
         top_auth_hrn = top_auth(slice_hrn)
         site_hrn = '.'.join(slice_hrn.split('.')[:-1])
@@ -422,7 +360,7 @@ class PlSlices:
         return site
 
 
-    def verify_slice(self, slice_hrn, slice_record, peer, sfa_peer, expiration, options={}):
+    def verify_slice(self, slice_hrn, slice_record, sfa_peer, expiration, options={}):
         top_auth_hrn = top_auth(slice_hrn)
         site_hrn = '.'.join(slice_hrn.split('.')[:-1])
         slice_part = slice_hrn.split('.')[-1]
@@ -439,9 +377,15 @@ class PlSlices:
         slice_exists = [slice for slice in slices if slice['hrn'] == slice_hrn]
         expires = int(datetime_to_epoch(utcparse(expiration)))
         if not slice_exists:
+            if slice_record:
+                url = slice_record.get('url', slice_hrn)
+                description = slice_record.get('description', slice_hrn)
+            else:
+                url = slice_hrn
+                description = slice_hrn
             slice = {'name': slice_name,
-                     'url': slice_record.get('url', slice_hrn),
-                     'description': slice_record.get('description', slice_hrn)}
+                     'url': url,
+                     'description': description}
             # add the slice                          
             slice['slice_id'] = self.driver.shell.AddSlice(slice)
             # set the slice HRN
@@ -460,7 +404,7 @@ class PlSlices:
         return self.driver.shell.GetSlices(int(slice['slice_id']))[0]
 
 
-    def verify_persons(self, slice_hrn, slice_record, users, peer, sfa_peer, options={}):
+    def verify_persons(self, slice_hrn, slice_record, users, sfa_peer, options={}):
         top_auth_hrn = top_auth(slice_hrn)
         site_hrn = '.'.join(slice_hrn.split('.')[:-1])
         slice_part = slice_hrn.split('.')[-1]
@@ -477,8 +421,10 @@ class PlSlices:
         slice_name = '_'.join([login_base, slice_part])
 
         persons = self.driver.shell.GetPersons({'peer_id': None},['person_id','email','hrn'])
-        site = self.driver.shell.GetSites({'peer_id': None, 'login_base': login_base})[0]
-        slice = self.driver.shell.GetSlices({'peer_id': None, 'name': slice_name})[0]
+        sites = self.driver.shell.GetSites({'peer_id': None}, ['node_ids', 'site_id', 'name', 'person_ids', 'slice_ids', 'login_base', 'hrn'])
+        site = [site for site in sites if site['hrn'] == site_hrn][0]
+        slices = self.driver.shell.GetSlices({'peer_id': None}, ['slice_id', 'node_ids', 'person_ids', 'expires', 'site_id', 'name', 'hrn'])
+        slice = [slice for slice in slices if slice['hrn'] == slice_hrn][0]
         slice_persons = self.driver.shell.GetPersons({'peer_id': None, 'person_id': slice['person_ids']},['person_id','email','hrn'])
 
         persons_by_hrn = {}
@@ -500,7 +446,7 @@ class PlSlices:
 
         # Add persons or add persons to slice
         for person_hrn in persons_to_add:
-             person_email = users_by_hrn[person_hrn].get('email', None)
+             person_email = users_by_hrn[person_hrn].get('email', "%s@geni.net"%person_hrn.split('.')[-1])
              if person_email and person_email in persons_by_email.keys():
                  # check if the user already exist in PL
                  person_id = persons_by_email[person_email]['person_id']
@@ -518,10 +464,10 @@ class PlSlices:
                  self.driver.shell.AddRoleToPerson('user', int(person_id))
                  # enable the account 
                  self.driver.shell.UpdatePerson(int(person_id), {'enabled': True})
-                 self.driver.shell.SetPersonHrn(int(person_id), person_hrn)
                  self.driver.shell.SetPersonSfaCreated(int(person_id), 'True')
                  self.driver.shell.AddPersonToSite(int(person_id), site['site_id'])
                  self.driver.shell.AddPersonToSlice(int(person_id), slice['slice_id'])
+                 self.driver.shell.SetPersonHrn(int(person_id), person_hrn)
 
                  # Add keys
                  for key in users_by_hrn[person_hrn].get('keys', []):
@@ -541,12 +487,12 @@ class PlSlices:
              person_id = slice_persons_by_hrn[person_hrn].get('person_id')
              persons_to_verify_keys[person_id] = users_by_hrn[person_hrn]
 
-        self.verify_keys(persons_to_verify_keys, peer, options)
+        self.verify_keys(persons_to_verify_keys, options)
 
         return persons_to_add
 
 
-    def verify_keys(self, persons_to_verify_keys, peer, options={}):
+    def verify_keys(self, persons_to_verify_keys, options={}):
         # we only add keys that comes from sfa to persons in PL
         for person_id in persons_to_verify_keys:
              person_sfa_keys = persons_to_verify_keys[person_id].get('keys', [])