the code was failing here as the path and finemane for the cache file is not set
[sfa.git] / sfa / plc / slices.py
index cfeed46..3c53504 100644 (file)
@@ -11,34 +11,25 @@ from sfa.util.namespace import *
 from sfa.util.rspec import *
 from sfa.util.specdict import *
 from sfa.util.faults import *
 from sfa.util.rspec import *
 from sfa.util.specdict import *
 from sfa.util.faults import *
-from sfa.util.storage import *
 from sfa.util.record import SfaRecord
 from sfa.util.policy import Policy
 from sfa.util.prefixTree import prefixTree
 from sfa.util.debug import log
 from sfa.util.record import SfaRecord
 from sfa.util.policy import Policy
 from sfa.util.prefixTree import prefixTree
 from sfa.util.debug import log
-from sfa.server.aggregate import Aggregates
-from sfa.server.registry import Registries
 
 MAXINT =  2L**31-1
 
 
 MAXINT =  2L**31-1
 
-class Slices(SimpleStorage):
+class Slices:
 
     rspec_to_slice_tag = {'max_rate':'net_max_rate'}
 
     def __init__(self, api, ttl = .5, origin_hrn=None):
         self.api = api
 
     rspec_to_slice_tag = {'max_rate':'net_max_rate'}
 
     def __init__(self, api, ttl = .5, origin_hrn=None):
         self.api = api
-        self.ttl = ttl
-        self.threshold = None
-        path = self.api.config.SFA_DATA_DIR
-        filename = ".".join([self.api.interface, self.api.hrn, "slices"])
-        filepath = path + os.sep + filename
-        self.slices_file = filepath
-        SimpleStorage.__init__(self, self.slices_file)
+        #filepath = path + os.sep + filename
         self.policy = Policy(self.api)    
         self.policy = Policy(self.api)    
-        self.load()
         self.origin_hrn = origin_hrn
 
         self.origin_hrn = origin_hrn
 
-    def get_slivers(self, hrn, node=None):
+    def get_slivers(self, xrn, node=None):
+        hrn, type = urn_to_hrn(xrn)
          
         slice_name = hrn_to_pl_slicename(hrn)
         # XX Should we just call PLCAPI.GetSliceTicket(slice_name) instead
          
         slice_name = hrn_to_pl_slicename(hrn)
         # XX Should we just call PLCAPI.GetSliceTicket(slice_name) instead
@@ -142,7 +133,8 @@ class Slices(SimpleStorage):
 
         return slivers
  
 
         return slivers
  
-    def get_peer(self, hrn):
+    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  
         # 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  
@@ -163,7 +155,9 @@ class Slices(SimpleStorage):
 
         return peer
 
 
         return peer
 
-    def get_sfa_peer(self, hrn):
+    def get_sfa_peer(self, xrn):
+        hrn, type = urn_to_hrn(xrn)
+
         # return the authority for this hrn or None if we are the authority
         sfa_peer = None
         slice_authority = get_authority(hrn)
         # return the authority for this hrn or None if we are the authority
         sfa_peer = None
         slice_authority = get_authority(hrn)
@@ -174,82 +168,10 @@ class Slices(SimpleStorage):
 
         return sfa_peer 
 
 
         return sfa_peer 
 
-    def refresh(self):
-        """
-        Update the cached list of slices
-        """
-        # Reload components list
-        now = datetime.datetime.now()
-        if not self.has_key('threshold') or not self.has_key('timestamp') or \
-           now > datetime.datetime.fromtimestamp(time.mktime(time.strptime(self['threshold'], self.api.time_format))):
-            if self.api.interface in ['aggregate']:
-                self.refresh_slices_aggregate()
-            elif self.api.interface in ['slicemgr']:
-                self.refresh_slices_smgr()
-
-    def refresh_slices_aggregate(self):
-        slices = self.api.plshell.GetSlices(self.api.plauth, {'peer_id': None}, ['name'])
-        slice_hrns = [slicename_to_hrn(self.api.hrn, slice['name']) for slice in slices]
-
-         # update timestamp and threshold
-        timestamp = datetime.datetime.now()
-        hr_timestamp = timestamp.strftime(self.api.time_format)
-        delta = datetime.timedelta(hours=self.ttl)
-        threshold = timestamp + delta
-        hr_threshold = threshold.strftime(self.api.time_format)
-        
-        slice_details = {'hrn': slice_hrns,
-                         'timestamp': hr_timestamp,
-                         'threshold': hr_threshold
-                        }
-        self.update(slice_details)
-        self.write()     
-        
-
-    def refresh_slices_smgr(self):
-        slice_hrns = []
-        aggregates = Aggregates(self.api)
-        credential = self.api.getCredential()
-        for aggregate in aggregates:
-            success = False
-            # request hash is optional so lets try the call without it 
-            try:
-                slices = aggregates[aggregate].get_slices(credential)
-                slice_hrns.extend(slices)
-                success = True
-            except:
-                print >> log, "%s" % (traceback.format_exc())
-                print >> log, "Error calling slices at aggregate %(aggregate)s" % locals()
-
-            # try sending the request hash if the previous call failed 
-            if not success:
-                arg_list = [credential]
-                try:
-                    slices = aggregates[aggregate].get_slices(credential)
-                    slice_hrns.extend(slices)
-                    success = True
-                except:
-                    print >> log, "%s" % (traceback.format_exc())
-                    print >> log, "Error calling slices at aggregate %(aggregate)s" % locals()
-
-        # update timestamp and threshold
-        timestamp = datetime.datetime.now()
-        hr_timestamp = timestamp.strftime(self.api.time_format)
-        delta = datetime.timedelta(hours=self.ttl)
-        threshold = timestamp + delta
-        hr_threshold = threshold.strftime(self.api.time_format)
-
-        slice_details = {'hrn': slice_hrns,
-                         'timestamp': hr_timestamp,
-                         'threshold': hr_threshold
-                        }
-        self.update(slice_details)
-        self.write()
-
-
     def verify_site(self, registry, credential, slice_hrn, peer, sfa_peer):
         authority = get_authority(slice_hrn)
     def verify_site(self, registry, credential, slice_hrn, peer, sfa_peer):
         authority = get_authority(slice_hrn)
-        site_records = registry.resolve(credential, authority)
+        authority_urn = hrn_to_urn(authority, 'authority')
+        site_records = registry.resolve(credential, authority_urn)
             
         site = {}
         for site_record in site_records:
             
         site = {}
         for site_record in site_records:
@@ -272,6 +194,9 @@ class Slices(SimpleStorage):
         else:
             site_id = sites[0]['site_id']
             remote_site_id = sites[0]['peer_site_id']
         else:
             site_id = sites[0]['site_id']
             remote_site_id = sites[0]['peer_site_id']
+           old_site = sites[0]
+           #the site is alredy on the remote agg. Let us update(e.g. max_slices field) it with the latest info.
+           self.sync_site(old_site, site, peer)
 
 
         return (site_id, remote_site_id) 
 
 
         return (site_id, remote_site_id) 
@@ -333,10 +258,10 @@ class Slices(SimpleStorage):
             person_record = {}
             person_records = registry.resolve(credential, researcher)
             for record in person_records:
             person_record = {}
             person_records = registry.resolve(credential, researcher)
             for record in person_records:
-                if record['type'] in ['user']:
+                if record['type'] in ['user'] and record['enabled']:
                     person_record = record
             if not person_record:
                     person_record = record
             if not person_record:
-                pass
+                return 1
             person_dict = person_record
             local_person=False
             if peer:
             person_dict = person_record
             local_person=False
             if peer:
@@ -401,8 +326,8 @@ class Slices(SimpleStorage):
 
                     except: pass   
 
 
                     except: pass   
 
-    def create_slice_aggregate(self, hrn, rspec):
-
+    def create_slice_aggregate(self, xrn, rspec):
+        hrn, type = urn_to_hrn(xrn)
         # Determine if this is a peer slice
         peer = self.get_peer(hrn)
         sfa_peer = self.get_sfa_peer(hrn)
         # Determine if this is a peer slice
         peer = self.get_peer(hrn)
         sfa_peer = self.get_sfa_peer(hrn)
@@ -412,8 +337,7 @@ class Slices(SimpleStorage):
         slicename = hrn_to_pl_slicename(hrn) 
         slice = {}
         slice_record = None
         slicename = hrn_to_pl_slicename(hrn) 
         slice = {}
         slice_record = None
-        registries = Registries(self.api)
-        registry = registries[self.api.hrn]
+        registry = self.api.registries[self.api.hrn]
         credential = self.api.getCredential()
 
         site_id, remote_site_id = self.verify_site(registry, credential, hrn, peer, sfa_peer)
         credential = self.api.getCredential()
 
         site_id, remote_site_id = self.verify_site(registry, credential, hrn, peer, sfa_peer)
@@ -479,11 +403,23 @@ class Slices(SimpleStorage):
 
         return 1
 
 
         return 1
 
+    def sync_site(self, old_record, new_record, peer):
+        if old_record['max_slices'] != new_record['max_slices'] or old_record['max_slivers'] != new_record['max_slivers']:
+            if peer:
+                self.api.plshell.UnBindObjectFromPeer(self.api.plauth, 'site', old_record['site_id'], peer)
+           if old_record['max_slices'] != new_record['max_slices']:
+                self.api.plshell.UpdateSite(self.api.plauth, old_record['site_id'], {'max_slices' : new_record['max_slices']})
+           if old_record['max_slivers'] != new_record['max_slivers']:
+               self.api.plshell.UpdateSite(self.api.plauth, old_record['site_id'], {'max_slivers' : new_record['max_slivers']})
+           if peer:
+                self.api.plshell.BindObjectToPeer(self.api.plauth, 'site', old_record['site_id'], peer, old_record['peer_site_id'])
+       return 1
+
     def sync_slice(self, old_record, new_record, peer):
         if old_record['expires'] != new_record['expires']:
             if peer:
                 self.api.plshell.UnBindObjectFromPeer(self.api.plauth, 'slice', old_record['slice_id'], peer)
     def sync_slice(self, old_record, new_record, peer):
         if old_record['expires'] != new_record['expires']:
             if peer:
                 self.api.plshell.UnBindObjectFromPeer(self.api.plauth, 'slice', old_record['slice_id'], peer)
-                self.api.plshell.UpdateSlice(self.api.plauth, old_record['slice_id'], {'expires' : new_record['expires']})
+            self.api.plshell.UpdateSlice(self.api.plauth, old_record['slice_id'], {'expires' : new_record['expires']})
            if peer:
                 self.api.plshell.BindObjectToPeer(self.api.plauth, 'slice', old_record['slice_id'], peer, old_record['peer_slice_id'])
        return 1
            if peer:
                 self.api.plshell.BindObjectToPeer(self.api.plauth, 'slice', old_record['slice_id'], peer, old_record['peer_slice_id'])
        return 1