def delete_security_group_rule(self, context, id): kwargs = {'context': context, 'security_group_rule_id': id} self._registry_notify(resources.SECURITY_GROUP_RULE, events.BEFORE_DELETE, id=id, exc_cls=ext_sg.SecurityGroupRuleInUse, **kwargs) with db_api.context_manager.writer.using(context): query = model_query.query_with_hooks( context, sg_models.SecurityGroupRule).filter( sg_models.SecurityGroupRule.id == id) self._registry_notify(resources.SECURITY_GROUP_RULE, events.PRECOMMIT_DELETE, exc_cls=ext_sg.SecurityGroupRuleInUse, id=id, **kwargs) try: sg_rule = query.one() # As there is a filter on a primary key it is not possible for # MultipleResultsFound to be raised context.session.delete(sg_rule) except exc.NoResultFound: raise ext_sg.SecurityGroupRuleNotFound(id=id) kwargs['security_group_id'] = sg_rule['security_group_id'] registry.notify(resources.SECURITY_GROUP_RULE, events.AFTER_DELETE, self, **kwargs)
def _get_rbac_policy(self, context, id): object_type = self._get_object_type(context, id) dbmodel = models.get_type_model_map()[object_type] try: return model_query.query_with_hooks( context, dbmodel).filter(dbmodel.id == id).one() except exc.NoResultFound: raise ext_rbac.RbacPolicyNotFound(id=id, object_type=object_type)
def _delete_port_security_group_bindings(self, context, port_id): with db_api.context_manager.writer.using(context): query = model_query.query_with_hooks( context, sg_models.SecurityGroupPortBinding) bindings = query.filter( sg_models.SecurityGroupPortBinding.port_id == port_id) for binding in bindings: context.session.delete(binding)
def _get_default_sg_id(self, context, tenant_id): try: query = model_query.query_with_hooks( context, sg_models.DefaultSecurityGroup) default_group = query.filter_by(tenant_id=tenant_id).one() return default_group['security_group_id'] except exc.NoResultFound: pass
def _get_security_group_rule(self, context, id): try: query = model_query.query_with_hooks(context, sg_models.SecurityGroupRule) sgr = query.filter(sg_models.SecurityGroupRule.id == id).one() except exc.NoResultFound: raise ext_sg.SecurityGroupRuleNotFound(id=id) return sgr
def get_mac_learning_state(self, context, port_id): try: query = model_query.query_with_hooks(context, nsx_models.MacLearningState) state = query.filter( nsx_models.MacLearningState.port_id == port_id).one() return state.mac_learning_enabled except exc.NoResultFound: return None
def _update_mac_learning_state(self, context, port_id, enabled): try: query = model_query.query_with_hooks( context, nsx_models.MacLearningState) state = query.filter( nsx_models.MacLearningState.port_id == port_id).one() state.update({mac.MAC_LEARNING: enabled}) except exc.NoResultFound: self._create_mac_learning_state(context, {'id': port_id, mac.MAC_LEARNING: enabled})
def _get_agent_by_type_and_host(self, context, agent_type, host): query = model_query.query_with_hooks(context, agent_model.Agent) try: agent_db = query.filter(agent_model.Agent.agent_type == agent_type, agent_model.Agent.host == host).one() return agent_db except exc.NoResultFound: raise ext_agent.AgentNotFoundByTypeHost(agent_type=agent_type, host=host) except exc.MultipleResultsFound: raise ext_agent.MultipleAgentFoundByTypeHost(agent_type=agent_type, host=host)
def _delete_port_queue_mapping(self, context, port_id): query = model_query.query_with_hooks(context, nsx_models.PortQueueMapping) try: binding = query.filter( nsx_models.PortQueueMapping.port_id == port_id).one() except exc.NoResultFound: # return since this can happen if we are updating a port that # did not already have a queue on it. There is no need to check # if there is one before deleting if we return here. return with db_api.context_manager.writer.using(context): context.session.delete(binding)
def _model_query(context, model): return _model_query.query_with_hooks(context, model)
def _check_for_queue_and_create(self, context, port): """Check for queue and create. This function determines if a port should be associated with a queue. It works by first querying NetworkQueueMapping to determine if the network is associated with a queue. If so, then it queries NetworkQueueMapping for all the networks that are associated with this queue. Next, it queries against all the ports on these networks with the port device_id. Finally it queries PortQueueMapping. If that query returns a queue_id that is returned. Otherwise a queue is created that is the size of the queue associated with the network and that queue_id is returned. If the network is not associated with a queue we then query to see if there is a default queue in the system. If so, a copy of that is created and the queue_id is returned. Otherwise None is returned. None is also returned if the port does not have a device_id or if the device_owner is network: """ queue_to_create = None # If there is no device_id don't create a queue. The queue will be # created on update port when the device_id is present. Also don't # apply QoS to network ports. if (not port.get('device_id') or port['device_owner'].startswith('network:')): return # Check if there is a queue associated with the network filters = {'network_id': [port['network_id']]} network_queue_id = self._get_network_queue_bindings( context, filters, ['queue_id']) if network_queue_id: # get networks that queue is associated with filters = {'queue_id': [network_queue_id[0]['queue_id']]} networks_with_same_queue = self._get_network_queue_bindings( context, filters) # get the ports on these networks with the same_queue and device_id filters = { 'device_id': [port.get('device_id')], 'network_id': [ network['network_id'] for network in networks_with_same_queue ] } query = model_query.query_with_hooks(context, models_v2.Port.id) model_query.apply_filters(query, models_v2.Port, filters, context) ports_ids = [p[0] for p in query] if ports_ids: # shared queue already exists find the queue id queues = self._get_port_queue_bindings(context, {'port_id': ports_ids}, ['queue_id']) if queues: return queues[0]['queue_id'] # get the size of the queue we want to create queue_to_create = self.get_qos_queue( context, network_queue_id[0]['queue_id']) else: # check for default queue filters = {'default': [True]} # context is elevated since default queue is owned by admin queue_to_create = self.get_qos_queues(context.elevated(), filters) if not queue_to_create: return queue_to_create = queue_to_create[0] # create the queue tenant_id = port['tenant_id'] if port.get(qos.RXTX_FACTOR) and queue_to_create.get('max'): queue_to_create['max'] = int(queue_to_create['max'] * port[qos.RXTX_FACTOR]) queue = { 'qos_queue': { 'name': queue_to_create.get('name'), 'min': queue_to_create.get('min'), 'max': queue_to_create.get('max'), 'dscp': queue_to_create.get('dscp'), 'qos_marking': queue_to_create.get('qos_marking'), 'tenant_id': tenant_id } } return self.create_qos_queue(context, queue, False)['id']