diff --git a/flowspec/models.py b/flowspec/models.py index 255d52067a9eb9b589909197cc33ae97e7482e0b..b2b9e4f444b862dbfb5d06e175aa21c54d2bdd0a 100644 --- a/flowspec/models.py +++ b/flowspec/models.py @@ -467,18 +467,27 @@ class Route(models.Model): self.save() - def check_sync(self): - if not self.is_synced(): + def check_sync(self, netconf_device_queried=None): + if not self.is_synced(netconf_device_queried=netconf_device_queried): #self.status = "OUTOFSYNC" #self.save() self.update_status("OUTOFSYNC") - def is_synced(self): + def is_synced(self, netconf_device_queried=None): + logger.info('models::is_synced(): self='+str(self)) found = False try: - get_device = PR.Retriever() - device = get_device.fetch_device() - routes = device.routing_options[0].routes + # allows for caching of NETCONF GetConfig query, e.g., during tasks::check_sync + if netconf_device_queried==None: + logger.info("models::is_synced(): querying routes newly from NETCONF router") + get_device = PR.Retriever() + parsed_netconf_xml__device_obj = get_device.fetch_device() + else: + logger.info("models::is_synced(): reusing cached query from NETCONF router") + parsed_netconf_xml__device_obj = netconf_device_queried + + parsed_netconf_xml__flows = parsed_netconf_xml__device_obj.routing_options + #logger.info('models::is_synced(): parsed_netconf_xml__flows='+str(parsed_netconf_xml__flows)) except Exception as e: #self.status = "EXPIRED" #self.save() @@ -486,7 +495,9 @@ class Route(models.Model): logger.error('models::is_synced(): No routing options on device. Exception: %s' % e) return True - for route in routes: + for flow in parsed_netconf_xml__flows: + for route in flow.routes: + #logger.debug('models::is_synced(): loop flow='+str(flow)+' route='+str(route)) if route.name == self.name: found = True logger.debug('models::is_synced(): Found a matching rule name') diff --git a/flowspec/tasks.py b/flowspec/tasks.py index e426203ca9787732a4ebbe3110b371be18a6f8dd..69821feb84395634a1972c04ccecfa57dddb5fdc 100644 --- a/flowspec/tasks.py +++ b/flowspec/tasks.py @@ -278,6 +278,15 @@ def check_sync(route_name=None, selected_routes=[]): routes = selected_routes if route_name: routes = routes.filter(name=route_name) + + try: + logger.info("tasks::check_sync(): making single query whose result is to be used during loop processing") + get_device = PR.Retriever() + device = get_device.fetch_device() + except Exception as e: + logger.info("tasks::check_sync(): exception occured during get active routes on router: "+str(e)) + return + for route in routes: if route.has_expired() and (route.status != 'EXPIRED' and route.status != 'ADMININACTIVE' and route.status != 'INACTIVE' and route.status != 'INACTIVE_TODELETE' and route.status != 'PENDING_TODELETE'): if route.status != 'ERROR': @@ -286,7 +295,7 @@ def check_sync(route_name=None, selected_routes=[]): else: if route.status != 'EXPIRED': old_status = route.status - route.check_sync() + route.check_sync(netconf_device_queried=device) new_status = route.status if old_status != new_status: logger.info('status of rule changed during check_sync %s : %s -> %s' % (route.name, old_status, new_status))