def prepare_bottom_external_subnet_by_bottom_name(self, context, subnet, region_name, b_net_name, top_subnet_id): t_ctx = t_context.get_context_from_neutron_context(context) pod = db_api.get_pod_by_name(t_ctx, region_name) b_client = self._get_client(region_name) bottom_network = b_client.list_networks(t_ctx, [{ 'key': 'name', 'comparator': 'eq', 'value': b_net_name }]) if not bottom_network: raise t_exceptions.InvalidInput( reason='bottom network not found for %(b_net_name)s' % {'b_net_name': b_net_name}) body = { 'subnet': { 'name': top_subnet_id, 'network_id': bottom_network[0]['id'], 'tenant_id': subnet['tenant_id'] } } attrs = ('ip_version', 'cidr', 'gateway_ip', 'allocation_pools', 'enable_dhcp') for attr in attrs: if validators.is_attr_set(subnet.get(attr)): body['subnet'][attr] = subnet[attr] self.prepare_bottom_element(t_ctx, subnet['tenant_id'], pod, {'id': top_subnet_id}, t_constants.RT_SUBNET, body)
def update_subnet(self, context, _id, subnet): """update bottom subnet Can not directly use ML2 plugin's update_subnet function, because it will call local plugin's get_subnet in a transaction, the local plugin's get_subnet will create a dhcp port when subnet's enable_dhcp attribute is changed from False to True, but neutron doesn't allow calling create_port in a transaction and will raise an exception. :param context: neutron context :param _id: subnet_id :param subnet: update body :return: updated subnet """ t_ctx = t_context.get_context_from_neutron_context(context) b_subnet = self.core_plugin.get_subnet(context, _id) origin_enable_dhcp = b_subnet['enable_dhcp'] req_enable_dhcp = subnet['subnet'].get('enable_dhcp') # when request enable dhcp, and origin dhcp is disabled, # ensure subnet dhcp port is created if req_enable_dhcp and not origin_enable_dhcp: self._ensure_subnet_dhcp_port(t_ctx, context, b_subnet) res = self.core_plugin.update_subnet(context, _id, subnet) return res
def delete_flow_classifier_precommit(self, context): t_ctx = t_context.get_context_from_neutron_context( context._plugin_context) flowclassifier_id = context.current['id'] db_api.create_recycle_resource( t_ctx, flowclassifier_id, t_constants.RT_FLOW_CLASSIFIER, t_ctx.project_id)
def update_port(self, context, _id, port): # ovs agent will not call update_port, it updates port status via rpc # and direct db operation profile_dict = port['port'].get(portbindings.PROFILE, {}) if profile_dict.pop(t_constants.PROFILE_FORCE_UP, None): port['port']['status'] = q_constants.PORT_STATUS_ACTIVE port['port'][ portbindings.VNIC_TYPE] = q_constants.ATTR_NOT_SPECIFIED b_port = self.core_plugin.update_port(context, _id, port) if self._need_top_update(b_port, port['port']): region_name = self._get_neutron_region() update_dict = { portbindings.PROFILE: { t_constants.PROFILE_REGION: region_name, t_constants.PROFILE_DEVICE: b_port['device_owner'] } } self._fill_agent_info_in_profile( context, _id, port['port'][portbindings.HOST_ID], update_dict[portbindings.PROFILE]) if directory.get_plugin('trunk'): trunk_details = b_port.get('trunk_details') if trunk_details: update_dict['binding:profile'].update({ t_constants.PROFILE_LOCAL_TRUNK_ID: trunk_details['trunk_id'] }) t_ctx = t_context.get_context_from_neutron_context(context) self.neutron_handle.handle_update(t_ctx, 'port', _id, {'port': update_dict}) return b_port
def prepare_bottom_router_gateway(self, n_context, region_name, segment_name): t_ctx = t_context.get_context_from_neutron_context(n_context) pod = db_api.get_pod_by_name(t_ctx, region_name) b_client = self._get_client(pod['region_name']) # when using new l3 network model, a local router will # be created automatically for an external net, and the # router's name is same to the net's id b_router = b_client.list_routers(t_ctx, filters=[{ 'key': 'name', 'comparator': 'eq', 'value': segment_name }]) if not b_router: raise t_exceptions.NotFound() b_nets = b_client.list_networks(t_ctx, filters=[{ 'key': 'name', 'comparator': 'eq', 'value': segment_name }]) if not b_nets: raise t_exceptions.NotFound() b_info = {'network_id': b_nets[0]['id']} return b_client.action_routers(t_ctx, 'add_gateway', b_router[0]['id'], b_info)
def _is_valid_network(self, context, network_id): try: self.core_plugin.get_network(context, network_id) except q_exceptions.NotFound: if self._in_subnet_delete(context): raise t_ctx = t_context.get_context_from_neutron_context(context) t_network = self.neutron_handle.handle_get(t_ctx, 'network', network_id) region_name = self._get_neutron_region() located = self._is_network_located_in_region( t_network, region_name) if not located: LOG.error( 'network: %(network_id)s not located in current ' 'region: %(region_name)s, ' 'az_hints: %(az_hints)s', { 'network_id': t_network['id'], 'region_name': region_name, 'az_hints': t_network[az_def.AZ_HINTS] }) return located self._create_bottom_network(context, network_id) return True
def delete_security_group_rule(self, q_context, _id): rule = self.get_security_group_rule(q_context, _id) if rule['remote_group_id']: raise n_exceptions.RemoteGroupNotSupported() sg_id = rule['security_group_id'] sg = self.get_security_group(q_context, sg_id) if sg['name'] == 'default': raise n_exceptions.DefaultGroupUpdateNotSupported() t_context = context.get_context_from_neutron_context(q_context) mappings = db_api.get_bottom_mappings_by_top_id( t_context, sg_id, constants.RT_SG) try: for pod, b_sg_id in mappings: client = self._get_client(pod['pod_name']) rule['security_group_id'] = b_sg_id b_sg = client.get_security_groups(t_context, b_sg_id) for b_rule in b_sg['security_group_rules']: if not self._compare_rule(b_rule, rule): continue self._safe_delete_security_group_rule(t_context, client, b_rule['id']) break except Exception: raise n_exceptions.BottomPodOperationFailure( resource='security group rule', pod_name=pod['pod_name']) super(TricircleSecurityGroupMixin, self).delete_security_group_rule(q_context, _id)
def create_security_group_rule(self, q_context, security_group_rule): rule = security_group_rule['security_group_rule'] if rule['remote_group_id']: raise n_exceptions.RemoteGroupNotSupported() sg_id = rule['security_group_id'] sg = self.get_security_group(q_context, sg_id) if sg['name'] == 'default': raise n_exceptions.DefaultGroupUpdateNotSupported() new_rule = super(TricircleSecurityGroupMixin, self).create_security_group_rule(q_context, security_group_rule) t_context = context.get_context_from_neutron_context(q_context) mappings = db_api.get_bottom_mappings_by_top_id( t_context, sg_id, constants.RT_SG) try: for pod, b_sg_id in mappings: client = self._get_client(pod['pod_name']) rule['security_group_id'] = b_sg_id self._safe_create_security_group_rule( t_context, client, {'security_group_rule': rule}) except Exception: super(TricircleSecurityGroupMixin, self).delete_security_group_rule(q_context, new_rule['id']) raise n_exceptions.BottomPodOperationFailure( resource='security group rule', pod_name=pod['pod_name']) return new_rule
def delete_port_pair_precommit(self, context): t_ctx = t_context.get_context_from_neutron_context( context._plugin_context) portpair_id = context.current['id'] db_api.create_recycle_resource( t_ctx, portpair_id, t_constants.RT_PORT_PAIR, t_ctx.project_id)
def delete_flow_classifier_precommit(self, context): t_ctx = t_context.get_context_from_neutron_context( context._plugin_context) flowclassifier_id = context.current['id'] db_api.create_recycle_resource(t_ctx, flowclassifier_id, t_constants.RT_FLOW_CLASSIFIER, t_ctx.project_id)
def get_networks(self, context, filters=None, fields=None, sorts=None, limit=None, marker=None, page_reverse=False): # if id is not specified in the filter, we just return network data in # local Neutron server, otherwise id is specified, we need to retrieve # network data from central Neutron server and create network which # doesn't exist in local Neutron server. if not filters or 'id' not in filters: return self.core_plugin.get_networks( context, filters, fields, sorts, limit, marker, page_reverse) b_full_networks = self.core_plugin.get_networks( context, filters, None, sorts, limit, marker, page_reverse) b_networks = [] for b_network in b_full_networks: subnet_ids = self._ensure_subnet(context, b_network, False) if subnet_ids: b_network['subnets'] = subnet_ids b_networks.append(db_utils.resource_fields(b_network, fields)) if len(b_networks) == len(filters['id']): return b_networks t_ctx = t_context.get_context_from_neutron_context(context) if self._skip_non_api_query(t_ctx): return b_networks t_ctx.auth_token = client.Client.get_admin_token(context.project_id) raw_client = self.neutron_handle._get_client(t_ctx) params = self._construct_params(filters, sorts, limit, marker, page_reverse) t_networks = raw_client.list_networks(**params)['networks'] t_id_set = set([network['id'] for network in t_networks]) b_id_set = set([network['id'] for network in b_networks]) missing_id_set = t_id_set - b_id_set if missing_id_set: missing_networks = [network for network in t_networks if ( network['id'] in missing_id_set)] for network in missing_networks: region_name = self._get_neutron_region() located = self._is_network_located_in_region(network, region_name) if not located: LOG.error('network: %(net_id)s not located in current ' 'region: %(region_name)s, ' 'az_hints: %(az_hints)s', {'net_id': network['id'], 'region_name': region_name, 'az_hints': network[az_def.AZ_HINTS]}) continue self._adapt_network_body(network) network.pop('qos_policy_id', None) b_network = self.core_plugin.create_network( context, {'network': network}) subnet_ids = self._ensure_subnet(context, network) if subnet_ids: b_network['subnets'] = subnet_ids b_networks.append(db_utils.resource_fields(b_network, fields)) return b_networks
def update_port_pair_group_precommit(self, context): plugin_context = context._plugin_context t_ctx = t_context.get_context_from_neutron_context( context._plugin_context) port_pair_group = context.current mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, port_pair_group['id'], t_constants.RT_PORT_PAIR_GROUP) if mappings: portchain_id = self._get_chain_id_by_group_id( plugin_context, context._plugin, port_pair_group['id']) if port_pair_group['port_pairs']: net_id = self._get_net_id_by_portpairgroups( plugin_context, context._plugin, [port_pair_group['id']]) elif context.original['port_pairs']: portpair_id = context.original['port_pairs'][0] port_pair = context._plugin._get_port_pair( plugin_context, portpair_id) net_id = self._get_net_id_by_port_id(plugin_context, port_pair['ingress']) else: net_id = '' if not portchain_id and not net_id: return self.xjob_handler.sync_service_function_chain( t_ctx, port_pair_group['project_id'], portchain_id, net_id, t_constants.POD_NOT_SPECIFIED)
def delete_port_pair_precommit(self, context): t_ctx = t_context.get_context_from_neutron_context( context._plugin_context) portpair_id = context.current['id'] db_api.create_recycle_resource(t_ctx, portpair_id, t_constants.RT_PORT_PAIR, t_ctx.project_id)
def update_port_pair_group_precommit(self, context): plugin_context = context._plugin_context t_ctx = t_context.get_context_from_neutron_context( context._plugin_context) port_pair_group = context.current mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, port_pair_group['id'], t_constants.RT_PORT_PAIR_GROUP) if mappings: portchain_id = self._get_chain_id_by_group_id( plugin_context, context._plugin, port_pair_group['id']) if port_pair_group['port_pairs']: net_id = self._get_net_id_by_portpairgroups( plugin_context, context._plugin, [port_pair_group['id']]) elif context.original['port_pairs']: portpair_id = context.original['port_pairs'][0] port_pair = context._plugin._get_port_pair( plugin_context, portpair_id) net_id = self._get_net_id_by_port_id( plugin_context, port_pair['ingress']) else: net_id = '' if not portchain_id and not net_id: return self.xjob_handler.sync_service_function_chain( t_ctx, port_pair_group['project_id'], portchain_id, net_id, t_constants.POD_NOT_SPECIFIED)
def update_port(self, context, _id, port): # ovs agent will not call update_port, it updates port status via rpc # and direct db operation profile_dict = port['port'].get(portbindings.PROFILE, {}) if profile_dict.pop(t_constants.PROFILE_FORCE_UP, None): port['port']['status'] = q_constants.PORT_STATUS_ACTIVE port['port'][ portbindings.VNIC_TYPE] = q_constants.ATTR_NOT_SPECIFIED b_port = self.core_plugin.update_port(context, _id, port) if self._need_top_update(b_port, port['port']): region_name = self._get_neutron_region() update_dict = {portbindings.PROFILE: { t_constants.PROFILE_REGION: region_name, t_constants.PROFILE_DEVICE: b_port['device_owner']}} self._fill_agent_info_in_profile( context, _id, port['port'][portbindings.HOST_ID], update_dict[portbindings.PROFILE]) if directory.get_plugin('trunk'): trunk_details = b_port.get('trunk_details') if trunk_details: update_dict['binding:profile'].update({ t_constants.PROFILE_LOCAL_TRUNK_ID: trunk_details['trunk_id']}) t_ctx = t_context.get_context_from_neutron_context(context) self.neutron_handle.handle_update(t_ctx, 'port', _id, {'port': update_dict}) return b_port
def get_networks(self, context, filters=None, fields=None, sorts=None, limit=None, marker=None, page_reverse=False): # if id is not specified in the filter, we just return network data in # local Neutron server, otherwise id is specified, we need to retrieve # network data from central Neutron server and create network which # doesn't exist in local Neutron server. if not filters or 'id' not in filters: return self.core_plugin.get_networks( context, filters, fields, sorts, limit, marker, page_reverse) b_full_networks = self.core_plugin.get_networks( context, filters, None, sorts, limit, marker, page_reverse) b_networks = [] for b_network in b_full_networks: subnet_ids = self._ensure_subnet(context, b_network, False) if subnet_ids: b_network['subnets'] = subnet_ids b_networks.append(self._fields(b_network, fields)) if len(b_networks) == len(filters['id']): return b_networks t_ctx = t_context.get_context_from_neutron_context(context) if self._skip_non_api_query(t_ctx): return b_networks t_ctx.auth_token = client.Client.get_admin_token(context.project_id) raw_client = self.neutron_handle._get_client(t_ctx) params = self._construct_params(filters, sorts, limit, marker, page_reverse) t_networks = raw_client.list_networks(**params)['networks'] t_id_set = set([network['id'] for network in t_networks]) b_id_set = set([network['id'] for network in b_networks]) missing_id_set = t_id_set - b_id_set if missing_id_set: missing_networks = [network for network in t_networks if ( network['id'] in missing_id_set)] for network in missing_networks: region_name = self._get_neutron_region() located = self._is_network_located_in_region(network, region_name) if not located: LOG.error('network: %(net_id)s not located in current ' 'region: %(region_name)s, ' 'az_hints: %(az_hints)s', {'net_id': network['id'], 'region_name': region_name, 'az_hints': network[az_def.AZ_HINTS]}) continue self._adapt_network_body(network) network.pop('qos_policy_id', None) b_network = self.core_plugin.create_network( context, {'network': network}) subnet_ids = self._ensure_subnet(context, network) if subnet_ids: b_network['subnets'] = subnet_ids b_networks.append(self._fields(b_network, fields)) return b_networks
def after_resource_create(self, resource, event, trigger, **kwargs): context = kwargs['context'] _resource_arg = kwargs[resource] resource_arg = {} for field in ('id', 'name', 'project_id','status','admin_state_up'): value = _resource_arg.get(field,None) if value is not None: resource_arg[field] = value resource_arg['region_name'] = self._get_neutron_region() resource_arg['resource_type'] = resource resource_body = { "tricircle_resource" : resource_arg } print "MaXiao after_resource_create called for resource : "+str(resource)+" event: "+str(event)+"resource_body: "+str(resource_body) t_ctx = t_context.get_context_from_neutron_context(context) try: print "MaXiao after_resource_create called for resource : "+str(resource)+" event: "+str(event)+"resource_arg: "+str(resource_arg) result = self.tricircle_handle.handle_create( t_ctx, 'tricircle_resource', resource_body) print "MaXiao after_resource_create called for resource : "+str(resource)+" event: "+str(event)+"resource_arg: "+str(resource_arg) return result except Exception as e: print "MaXiao exception "+str(e) return {}
def prepare_bottom_external_subnet_by_bottom_name( self, context, subnet, region_name, b_net_name, top_subnet_id): t_ctx = t_context.get_context_from_neutron_context(context) pod = db_api.get_pod_by_name(t_ctx, region_name) b_client = self._get_client(region_name) bottom_network = b_client.list_networks( t_ctx, [{'key': 'name', 'comparator': 'eq', 'value': b_net_name}] ) if not bottom_network: raise t_exceptions.InvalidInput( reason='bottom network not found for %(b_net_name)s' % {'b_net_name': b_net_name}) body = { 'subnet': { 'name': top_subnet_id, 'network_id': bottom_network[0]['id'], 'tenant_id': subnet['tenant_id'] } } attrs = ('ip_version', 'cidr', 'gateway_ip', 'allocation_pools', 'enable_dhcp') for attr in attrs: if validators.is_attr_set(subnet.get(attr)): body['subnet'][attr] = subnet[attr] self.prepare_bottom_element( t_ctx, subnet['tenant_id'], pod, {'id': top_subnet_id}, t_constants.RT_SUBNET, body)
def create_port(self, context, port): port_body = port['port'] network_id = port_body['network_id'] # get_network will create bottom network if it doesn't exist self.get_network(context, network_id) t_ctx = t_context.get_context_from_neutron_context(context) raw_client = self.neutron_handle._get_client(t_ctx) def get_top_port_by_ip(ip): params = { 'fixed_ips': 'ip_address=%s' % ip, 'network_id': network_id } t_ports = raw_client.list_ports(**params)['ports'] if not t_ports: raise q_exceptions.InvalidIpForNetwork( ip_address=fixed_ip['ip_address']) return t_ports[0] if port_body['fixed_ips'] is not q_constants.ATTR_NOT_SPECIFIED and ( port_body.get('device_owner') != (q_constants.DEVICE_OWNER_LOADBALANCERV2)): if not self._is_special_port(port_body): fixed_ip = port_body['fixed_ips'][0] ip_address = fixed_ip.get('ip_address') if not ip_address: # dhcp agent may request to create a dhcp port without # specifying ip address, we just raise an exception to # reject this request raise q_exceptions.InvalidIpForNetwork(ip_address='None') t_port = get_top_port_by_ip(ip_address) elif helper.NetworkHelper.is_need_top_sync_port( port_body, cfg.CONF.client.bridge_cidr): # for port that needs to be synced with top port, we keep ids # the same ip_address = port_body['fixed_ips'][0]['ip_address'] port_body['id'] = get_top_port_by_ip(ip_address)['id'] t_port = port_body else: self._handle_dvr_snat_port(t_ctx, port_body) t_port = port_body else: self._adapt_port_body_for_client(port['port']) t_port = raw_client.create_port(port)['port'] if not self._is_special_port(port_body): subnet_id = t_port['fixed_ips'][0]['subnet_id'] # get_subnet will create bottom subnet if it doesn't exist self.get_subnet(context, subnet_id) for field in ('name', 'device_id', 'device_owner', 'binding:host_id'): if port_body.get(field): t_port[field] = port_body[field] self._handle_security_group(t_ctx, context, t_port) self._create_shadow_agent(context, port_body) b_port = self.core_plugin.create_port(context, {'port': t_port}) return b_port
def create_port(self, context, port): port_body = port['port'] network_id = port_body['network_id'] # get_network will create bottom network if it doesn't exist self.get_network(context, network_id) t_ctx = t_context.get_context_from_neutron_context(context) raw_client = self.neutron_handle._get_client(t_ctx) def get_top_port_by_ip(ip): params = {'fixed_ips': 'ip_address=%s' % ip, 'network_id': network_id} t_ports = raw_client.list_ports(**params)['ports'] if not t_ports: raise q_exceptions.InvalidIpForNetwork( ip_address=fixed_ip['ip_address']) return t_ports[0] if port_body['fixed_ips'] is not q_constants.ATTR_NOT_SPECIFIED and ( port_body.get('device_owner') != ( q_constants.DEVICE_OWNER_LOADBALANCERV2)): if not self._is_special_port(port_body): fixed_ip = port_body['fixed_ips'][0] ip_address = fixed_ip.get('ip_address') if not ip_address: # dhcp agent may request to create a dhcp port without # specifying ip address, we just raise an exception to # reject this request raise q_exceptions.InvalidIpForNetwork(ip_address='None') t_port = get_top_port_by_ip(ip_address) elif helper.NetworkHelper.is_need_top_sync_port( port_body, cfg.CONF.client.bridge_cidr): # for port that needs to be synced with top port, we keep ids # the same ip_address = port_body['fixed_ips'][0]['ip_address'] port_body['id'] = get_top_port_by_ip(ip_address)['id'] t_port = port_body else: self._handle_dvr_snat_port(t_ctx, port_body) t_port = port_body else: self._adapt_port_body_for_client(port['port']) t_port = raw_client.create_port(port)['port'] if not self._is_special_port(port_body): subnet_id = t_port['fixed_ips'][0]['subnet_id'] # get_subnet will create bottom subnet if it doesn't exist self.get_subnet(context, subnet_id) for field in ('name', 'device_id', 'device_owner', 'binding:host_id'): if port_body.get(field): t_port[field] = port_body[field] self._handle_security_group(t_ctx, context, t_port) self._create_shadow_agent(context, port_body) t_port.pop('qos_policy_id', None) b_port = self.core_plugin.create_port(context, {'port': t_port}) return b_port
def get_trunks(self, context, filters=None, fields=None, sorts=None, limit=None, marker=None, page_reverse=False): ret = [] bottom_top_map = {} top_bottom_map = {} t_ctx = t_context.get_context_from_neutron_context(context) route_filters = [{'key': 'resource_type', 'comparator': 'eq', 'value': t_constants.RT_TRUNK}] routes = db_api.list_resource_routings(t_ctx, route_filters) for route in routes: bottom_top_map[route['bottom_id']] = route['top_id'] top_bottom_map[route['top_id']] = route['bottom_id'] if limit: if marker: mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, marker, t_constants.RT_TRUNK) # if mapping exists, we retrieve trunk information # from bottom, otherwise from top if mappings: pod_id = mappings[0][0]['pod_id'] current_pod = db_api.get_pod(t_ctx, pod_id) ret = self._get_trunks_from_pod_with_limit( context, current_pod, bottom_top_map, top_bottom_map, filters, limit, marker) else: ret = self._get_trunks_from_top_with_limit( context, top_bottom_map, filters, limit, marker) else: current_pod = db_api.get_next_bottom_pod(t_ctx) # if current_pod exists, we retrieve trunk information # from bottom, otherwise from top if current_pod: ret = self._get_trunks_from_pod_with_limit( context, current_pod, bottom_top_map, top_bottom_map, filters, limit, None) else: ret = self._get_trunks_from_top_with_limit( context, top_bottom_map, filters, limit, None) else: pods = db_api.list_pods(t_ctx) _filters = self._transform_trunk_filters(filters, top_bottom_map) for pod in pods: if not pod['az_name']: continue client = self._get_client(pod['region_name']) pod_trunks = client.list_trunks(t_ctx, filters=_filters) ret.extend(pod_trunks) ret = self._map_trunks_from_bottom_to_top(ret, bottom_top_map) top_trunks = self._get_trunks_from_top(context, top_bottom_map, filters) ret.extend(top_trunks) return [super(TricircleTrunkPlugin, self)._fields(trunk, fields) for trunk in ret]
def create_port_chain_precommit(self, context): plugin_context = context._plugin_context t_ctx = t_context.get_context_from_neutron_context(plugin_context) port_chain = context.current net_id = self._get_net_id_by_portpairgroups( plugin_context, context._plugin, port_chain['port_pair_groups']) if net_id: self.xjob_handler.sync_service_function_chain( t_ctx, port_chain['project_id'], port_chain['id'], net_id, t_constants.POD_NOT_SPECIFIED)
def _create_bottom_network(self, context, _id): t_ctx = t_context.get_context_from_neutron_context(context) t_network = self.neutron_handle.handle_get(t_ctx, 'network', _id) if not t_network: raise q_exceptions.NetworkNotFound(net_id=_id) self._adapt_network_body(t_network) t_network.pop('qos_policy_id', None) b_network = self.core_plugin.create_network(context, {'network': t_network}) return t_network, b_network
def remove_bottom_external_network_by_name( self, n_context, region_name, name): t_ctx = t_context.get_context_from_neutron_context(n_context) b_client = self._get_client(region_name) b_net = b_client.list_networks( t_ctx, [{'key': 'name', 'comparator': 'eq', 'value': name}]) if b_net: b_client.delete_networks(t_ctx, b_net[0]['id'])
def remove_bottom_router_by_name(self, n_context, region_name, router_name): t_ctx = t_context.get_context_from_neutron_context(n_context) b_client = self._get_client(region_name) bottom_router = b_client.list_routers( t_ctx, [{'key': 'name', 'comparator': 'eq', 'value': router_name}]) if bottom_router: b_client.delete_routers(t_ctx, bottom_router[0]['id'])
def delete_policy_precommit(self, q_context, policy): """Delete policy precommit. :param q_context: current running context information :param policy: a QoSPolicy object being deleted """ t_context = context.get_context_from_neutron_context(q_context) policy_id = policy['id'] self.xjob_handler.delete_qos_policy( t_context, t_context.project_id, policy_id, t_constants.POD_NOT_SPECIFIED)
def remove_bottom_router_by_name(self, n_context, region_name, router_name): t_ctx = t_context.get_context_from_neutron_context(n_context) b_client = self._get_client(region_name) bottom_router = b_client.list_routers(t_ctx, [{ 'key': 'name', 'comparator': 'eq', 'value': router_name }]) if bottom_router: b_client.delete_routers(t_ctx, bottom_router[0]['id'])
def remove_bottom_external_subnet_by_name(self, context, region_name, b_subnet_name): t_ctx = t_context.get_context_from_neutron_context(context) b_client = self._get_client(region_name) bottom_subnet = b_client.list_subnets(t_ctx, [{ 'key': 'name', 'comparator': 'eq', 'value': b_subnet_name }]) if bottom_subnet: b_client.delete_subnets(t_ctx, bottom_subnet[0]['id'])
def delete_trunk(self, context, trunk_id): t_ctx = t_context.get_context_from_neutron_context(context) res = super(TricircleTrunkPlugin, self).get_trunk(context, trunk_id) with context.session.begin(): super(TricircleTrunkPlugin, self).delete_trunk(context, trunk_id) mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, trunk_id, t_constants.RT_TRUNK) if mappings: b_pod = mappings[0][0] self.xjob_handler.sync_trunk(t_ctx, res['project_id'], trunk_id, b_pod['pod_id'])
def get_subnet(self, context, _id, fields=None): t_ctx = t_context.get_context_from_neutron_context(context) try: b_subnet = self.core_plugin.get_subnet(context, _id, fields) except q_exceptions.NotFound: t_subnet = self.neutron_handle.handle_get(t_ctx, 'subnet', _id) if not t_subnet: raise q_exceptions.SubnetNotFound(subnet_id=_id) b_subnet = self._create_bottom_subnet(t_ctx, context, t_subnet) if b_subnet['enable_dhcp']: self._ensure_subnet_dhcp_port(t_ctx, context, b_subnet) return self._fields(b_subnet, fields)
def get_subnets(self, context, filters=None, fields=None, sorts=None, limit=None, marker=None, page_reverse=False): # if id is not specified in the filter, we just return subnet data in # local Neutron server, otherwise id is specified, we need to retrieve # subnet data from central Neutron server and create subnet which # doesn't exist in local Neutron server. if not filters or 'id' not in filters: return self.core_plugin.get_subnets(context, filters, fields, sorts, limit, marker, page_reverse) t_ctx = t_context.get_context_from_neutron_context(context) b_full_subnets = self.core_plugin.get_subnets(context, filters, None, sorts, limit, marker, page_reverse) b_subnets = [] for b_subnet in b_full_subnets: if b_subnet['enable_dhcp']: self._ensure_subnet_dhcp_port(t_ctx, context, b_subnet) b_subnets.append(self._fields(b_subnet, fields)) if len(b_subnets) == len(filters['id']): return b_subnets if self._skip_non_api_query(t_ctx): return b_subnets raw_client = self.neutron_handle._get_client(t_ctx) params = self._construct_params(filters, sorts, limit, marker, page_reverse) t_subnets = raw_client.list_subnets(**params)['subnets'] t_id_set = set([subnet['id'] for subnet in t_subnets]) b_id_set = set([subnet['id'] for subnet in b_subnets]) missing_id_set = t_id_set - b_id_set if missing_id_set: missing_subnets = [ subnet for subnet in t_subnets if (subnet['id'] in missing_id_set) ] for subnet in missing_subnets: valid = self._is_valid_network(context, subnet['network_id']) if not valid: continue b_subnet = self._create_bottom_subnet(t_ctx, context, subnet) if b_subnet['enable_dhcp']: self._ensure_subnet_dhcp_port(t_ctx, context, b_subnet) b_subnets.append(self._fields(b_subnet, fields)) return b_subnets
def update_trunk(self, context, trunk_id, trunk): # update trunk t_ctx = t_context.get_context_from_neutron_context(context) with context.session.begin(): res = super(TricircleTrunkDriver, self).update_trunk( context, trunk_id, trunk) mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, trunk_id, t_constants.RT_TRUNK) if mappings: b_pod = mappings[0][0] self.xjob_handler.sync_trunk(t_ctx, res['project_id'], trunk_id, b_pod['pod_id']) return res
def get_port(self, context, _id, fields=None): try: b_port = self.core_plugin.get_port(context, _id, fields) except q_exceptions.NotFound: t_ctx = t_context.get_context_from_neutron_context(context) t_port = self.neutron_handle.handle_get(t_ctx, 'port', _id) if not t_port: raise q_exceptions.PortNotFound(port_id=_id) self._ensure_network_subnet(context, t_port) self._adapt_port_body_for_call(t_port) self._handle_security_group(t_ctx, context, t_port) b_port = self.core_plugin.create_port(context, {'port': t_port}) return self._fields(b_port, fields)
def update_port(self, context, _id, port): if port['port'].get('device_owner', '').startswith('compute') and ( port['port'].get('binding:host_id')): # we check both "device_owner" and "binding:host_id" to ensure the # request comes from nova. and ovs agent will not call update_port. # it updates port status via rpc and direct db operation region_name = cfg.CONF.nova.region_name update_dict = {'binding:profile': { t_constants.PROFILE_REGION: region_name}} t_ctx = t_context.get_context_from_neutron_context(context) self.neutron_handle.handle_update(t_ctx, 'port', _id, {'port': update_dict}) return self.core_plugin.update_port(context, _id, port)
def get_security_group(self, context, _id, fields=None, tenant_id=None): try: return self.core_plugin.get_security_group(context, _id, fields, tenant_id) except q_exceptions.NotFound: t_ctx = t_context.get_context_from_neutron_context(context) t_sg = self.neutron_handle.handle_get(t_ctx, 'security_group', _id) if not t_sg: raise ext_sg.SecurityGroupNotFound(id=_id) self.core_plugin.create_security_group(context, {'security_group': t_sg}) return self.core_plugin.get_security_group(context, _id, fields, tenant_id)
def get_subnet(self, context, _id, fields=None): t_ctx = t_context.get_context_from_neutron_context(context) try: b_subnet = self.core_plugin.get_subnet(context, _id) except q_exceptions.NotFound: if self._skip_non_api_query(t_ctx): raise q_exceptions.SubnetNotFound(subnet_id=_id) t_subnet = self.neutron_handle.handle_get(t_ctx, 'subnet', _id) if not t_subnet: raise q_exceptions.SubnetNotFound(subnet_id=_id) b_subnet = self._create_bottom_subnet(t_ctx, context, t_subnet) if b_subnet['enable_dhcp']: self._ensure_subnet_dhcp_port(t_ctx, context, b_subnet) return self._fields(b_subnet, fields)
def get_security_group(self, context, _id, fields=None, tenant_id=None): try: return self.core_plugin.get_security_group( context, _id, fields, tenant_id) except q_exceptions.NotFound: t_ctx = t_context.get_context_from_neutron_context(context) t_sg = self.neutron_handle.handle_get(t_ctx, 'security_group', _id) if not t_sg: raise ext_sg.SecurityGroupNotFound(id=_id) self.core_plugin.create_security_group(context, {'security_group': t_sg}) return self.core_plugin.get_security_group( context, _id, fields, tenant_id)
def remove_subports(self, context, trunk_id, subports): t_ctx = t_context.get_context_from_neutron_context(context) with context.session.begin(): self.update_subports_device_id(context, subports, '', '') res = super(TricircleTrunkDriver, self).remove_subports( context, trunk_id, subports) mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, trunk_id, t_constants.RT_TRUNK) if mappings: b_pod = mappings[0][0] self.xjob_handler.sync_trunk( t_ctx, res['project_id'], trunk_id, b_pod['pod_id']) return res
def add_subports(self, context, trunk_id, subports): t_ctx = t_context.get_context_from_neutron_context(context) with context.session.begin(): res = super(TricircleTrunkPlugin, self).add_subports( context, trunk_id, subports) self.update_subports_device_id(context, subports, trunk_id, t_constants.DEVICE_OWNER_SUBPORT) mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, trunk_id, t_constants.RT_TRUNK) if mappings: b_pod = mappings[0][0] self.xjob_handler.sync_trunk( t_ctx, res['project_id'], trunk_id, b_pod['pod_id']) return res
def delete_port(self, context, _id, l3_port_check=True): t_ctx = t_context.get_context_from_neutron_context(context) try: b_port = self.core_plugin.get_port(context, _id) # to support floating ip, we create a copy port if the target port # is not in the pod where the real external network is located. to # distinguish it from normal port, we name it with a prefix do_top_delete = b_port['device_owner'].startswith( q_constants.DEVICE_OWNER_COMPUTE_PREFIX) skip_top_delete = t_constants.RT_SD_PORT in b_port['name'] except q_exceptions.NotFound: return if do_top_delete and not skip_top_delete: self.neutron_handle.handle_delete(t_ctx, t_constants.RT_PORT, _id) self.core_plugin.delete_port(context, _id, l3_port_check)
def update_port_chain_precommit(self, context): plugin_context = context._plugin_context t_ctx = t_context.get_context_from_neutron_context(plugin_context) port_chain = context.current mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, port_chain['id'], t_constants.RT_PORT_CHAIN) if mappings: net_id = self._get_net_id_by_portpairgroups( plugin_context, context._plugin, port_chain['port_pair_groups']) if not net_id: return self.xjob_handler.sync_service_function_chain( t_ctx, port_chain['project_id'], port_chain['id'], net_id, t_constants.POD_NOT_SPECIFIED)
def get_port(self, context, _id, fields=None): try: b_port = self.core_plugin.get_port(context, _id, fields) except q_exceptions.NotFound: t_ctx = t_context.get_context_from_neutron_context(context) if self._skip_non_api_query(t_ctx): raise q_exceptions.PortNotFound(port_id=_id) t_port = self.neutron_handle.handle_get(t_ctx, 'port', _id) if not t_port: raise q_exceptions.PortNotFound(port_id=_id) self._ensure_network_subnet(context, t_port) self._adapt_port_body_for_call(t_port) self._handle_security_group(t_ctx, context, t_port) b_port = self.core_plugin.create_port(context, {'port': t_port}) return self._fields(b_port, fields)
def get_ports(self, context, filters=None, fields=None, sorts=None, limit=None, marker=None, page_reverse=False): # if id is not specified in the filter, we just return port data in # local Neutron server, otherwise id is specified, we need to retrieve # port data from central Neutron server and create port which doesn't # exist in local Neutron server. if not filters or 'id' not in filters: return self.core_plugin.get_ports(context, filters, fields, sorts, limit, marker, page_reverse) b_ports = self.core_plugin.get_ports(context, filters, fields, sorts, limit, marker, page_reverse) if len(b_ports) == len(filters['id']): return b_ports id_set = set(filters['id']) b_id_set = set([port['id'] for port in b_ports]) missing_id_set = id_set - b_id_set t_ctx = t_context.get_context_from_neutron_context(context) if self._skip_non_api_query(t_ctx): return b_ports raw_client = self.neutron_handle._get_client(t_ctx) t_ports = [] for port_id in missing_id_set: # use list_port will cause infinite API call since central Neutron # server will also use list_port to retrieve port information from # local Neutron server, so we show_port one by one try: t_port = raw_client.show_port(port_id)['port'] t_ports.append(t_port) except Exception: # user passes a nonexistent port id pass for port in t_ports: self._ensure_network_subnet(context, port) self._adapt_port_body_for_call(port) self._handle_security_group(t_ctx, context, port) port.pop('qos_policy_id', None) b_port = self.core_plugin.create_port(context, {'port': port}) b_ports.append(self._fields(b_port, fields)) return b_ports
def get_network(self, context, _id, fields=None): try: b_network = self.core_plugin.get_network(context, _id, fields) subnet_ids = self._ensure_subnet(context, b_network, False) except q_exceptions.NotFound: t_ctx = t_context.get_context_from_neutron_context(context) t_network = self.neutron_handle.handle_get(t_ctx, 'network', _id) if not t_network: raise q_exceptions.NetworkNotFound(net_id=_id) self._adapt_network_body(t_network) b_network = self.core_plugin.create_network(context, {'network': t_network}) subnet_ids = self._ensure_subnet(context, t_network) if subnet_ids: b_network['subnets'] = subnet_ids return self._fields(b_network, fields)
def prepare_bottom_router(self, n_context, net, b_router_name): t_ctx = t_context.get_context_from_neutron_context(n_context) # use the first pod az_name = net[az_def.AZ_HINTS][0] pod = db_api.find_pod_by_az_or_region(t_ctx, az_name) body = { 'router': { 'name': b_router_name, 'tenant_id': net['tenant_id'], 'admin_state_up': True, 'distributed': False } } return self.prepare_bottom_element( t_ctx, net['tenant_id'], pod, {'id': b_router_name}, t_constants.RT_ROUTER, body)
def remove_bottom_router_gateway( self, n_context, region_name, b_net_name): t_ctx = t_context.get_context_from_neutron_context(n_context) pod = db_api.get_pod_by_name(t_ctx, region_name) b_client = self._get_client(pod['region_name']) # when using new l3 network model, a local router will # be created automatically for an external net, and the # router's name is same to the net's id b_router = b_client.list_routers( t_ctx, filters=[{'key': 'name', 'comparator': 'eq', 'value': b_net_name}]) if not b_router: raise t_exceptions.NotFound() return b_client.action_routers( t_ctx, 'remove_gateway', b_router[0]['id'], b_router[0]['id'])
def delete_port_pair(self, context): t_ctx = t_context.get_context_from_neutron_context( context._plugin_context) portpair_id = context.current['id'] mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, portpair_id, t_constants.RT_PORT_PAIR) for b_pod, b_portpair_id in mappings: b_region_name = b_pod['region_name'] b_client = self._get_client(b_region_name) try: b_client.delete_port_pairs(t_ctx, b_portpair_id) except client_exceptions.NotFound: LOG.debug(('port pair: %(portpair_id)s not found, ' 'region name: %(name)s'), {'portpair_id': portpair_id, 'name': b_region_name}) db_api.delete_mappings_by_bottom_id(t_ctx, b_portpair_id)
def delete_flow_classifier(self, context): t_ctx = t_context.get_context_from_neutron_context( context._plugin_context) flowclassifier_id = context.current['id'] mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, flowclassifier_id, t_constants.RT_FLOW_CLASSIFIER) for b_pod, b_classifier_id in mappings: b_region_name = b_pod['region_name'] b_client = self._get_client(b_region_name) try: b_client.delete_flow_classifiers(t_ctx, b_classifier_id) except client_exceptions.NotFound: LOG.debug(('flow classifier: %(classifier_id)s not found, ' 'region name: %(name)s'), {'classifier_id': flowclassifier_id, 'name': b_region_name}) db_api.delete_mappings_by_bottom_id(t_ctx, b_classifier_id)