def run(self): # Subscribe to endpoints IPEndpoint.subscribe(self.session) while not self._exit: if IPEndpoint.has_events(self.session): try: self.handle_endpoint_event() except ConnectionError: logging.error('Could not handle endpoint event due to ConnectionError')
def handle_endpoint_event(self): num_eps = MAX_ENDPOINTS dirty_epgs = {} while IPEndpoint.has_events(self.session) and num_eps: ep = IPEndpoint.get_event(self.session) logging.info('for Endpoint: %s', ep.name) epg = ep.get_parent() app = epg.get_parent() tenant = app.get_parent() print 'Endpoint found', ep.name print 'Has EPG policy', (tenant.name, app.name, epg.name), (tenant.name, app.name, epg.name) in self._endpoint_db if (tenant.name, app.name, epg.name) in self._endpoint_db: # Store the Endpoint in our Endpoint DB if ep.is_deleted(): try: self._endpoint_db[(tenant.name, app.name, epg.name)].remove(ep) except ValueError: logging.error( 'Tried to delete endpoint that was not in database.' ) else: self._endpoint_db[(tenant.name, app.name, epg.name)].append(ep) # Mark the EPG as dirty (we need to push to the hosts) if (tenant.name, app.name, epg.name) not in dirty_epgs: dirty_epgs[(tenant.name, app.name, epg.name)] = [] if not ep.is_deleted(): dirty_epgs[(tenant.name, app.name, epg.name)].append(ep) num_eps -= 1 start_time = time.time() for epg in dirty_epgs: (tenant_name, app_name, epg_name) = epg epg_policy = self._tool.config.get_epg_policy( tenant_name, app_name, epg_name) contract_policy = self._tool.config.get_contract_policy( epg_policy.contract) # Create the temporary equivalent of a /etc/ansible/hosts file for all hosts host_ips = '' new_host_ips = '' for ep in self._endpoint_db[epg]: host_ips += ep.name + '\n' for ep in dirty_epgs[epg]: assert not ep.is_deleted() new_host_ips += ep.name + '\n' self.process_policy(contract_policy, epg, host_ips, new_host_ips) end_time = time.time() print 'Time taken:', end_time - start_time
def handle_endpoint_event(self): num_eps = MAX_ENDPOINTS dirty_epgs = {} while IPEndpoint.has_events(self.session) and num_eps: ep = IPEndpoint.get_event(self.session) logging.info('for Endpoint: %s', ep.name) epg = ep.get_parent() app = epg.get_parent() tenant = app.get_parent() print 'Endpoint found', ep.name print 'Has EPG policy', (tenant.name, app.name, epg.name), (tenant.name, app.name, epg.name) in self._endpoint_db if (tenant.name, app.name, epg.name) in self._endpoint_db: # Store the Endpoint in our Endpoint DB if ep.is_deleted(): try: self._endpoint_db[(tenant.name, app.name, epg.name)].remove(ep) except ValueError: logging.error('Tried to delete endpoint that was not in database.') else: self._endpoint_db[(tenant.name, app.name, epg.name)].append(ep) # Mark the EPG as dirty (we need to push to the hosts) if (tenant.name, app.name, epg.name) not in dirty_epgs: dirty_epgs[(tenant.name, app.name, epg.name)] = [] if not ep.is_deleted(): dirty_epgs[(tenant.name, app.name, epg.name)].append(ep) num_eps -= 1 start_time = time.time() for epg in dirty_epgs: (tenant_name, app_name, epg_name) = epg epg_policy = self._tool.config.get_epg_policy(tenant_name, app_name, epg_name) contract_policy = self._tool.config.get_contract_policy(epg_policy.contract) # Create the temporary equivalent of a /etc/ansible/hosts file for all hosts host_ips = '' new_host_ips = '' for ep in self._endpoint_db[epg]: host_ips += ep.name + '\n' for ep in dirty_epgs[epg]: assert not ep.is_deleted() new_host_ips += ep.name + '\n' self.process_policy(contract_policy, epg, host_ips, new_host_ips) end_time = time.time() print 'Time taken:', end_time - start_time