|
21 | 21 | from neutron_lib.api.definitions import ip_allocation as ipalloc_apidef
|
22 | 22 | from neutron_lib.api.definitions import port as port_def
|
23 | 23 | from neutron_lib.api.definitions import portbindings as portbindings_def
|
| 24 | +from neutron_lib.api.definitions import provider_net as pnet_def |
24 | 25 | from neutron_lib.api.definitions import subnetpool as subnetpool_def
|
25 | 26 | from neutron_lib.api import validators
|
26 | 27 | from neutron_lib.callbacks import events
|
|
57 | 58 | from neutron.conf import experimental as c_exp
|
58 | 59 | from neutron.db import db_base_plugin_common
|
59 | 60 | from neutron.db import ipam_pluggable_backend
|
| 61 | +from neutron.db.models import segment as segment_db |
60 | 62 | from neutron.db import models_v2
|
61 | 63 | from neutron.db import rbac_db_mixin as rbac_mixin
|
62 | 64 | from neutron.db import rbac_db_models
|
@@ -130,6 +132,33 @@ def _port_filter_hook(context, original_model, conditions):
|
130 | 132 | return conditions
|
131 | 133 |
|
132 | 134 |
|
| 135 | +def _network_result_filter_hook(query, filters): |
| 136 | + # This filter matches the provider network attributes, defined in |
| 137 | + # ``neutron_lib.api.definitions.provider_net.ATTRIBUTES``. |
| 138 | + attr_to_field = { |
| 139 | + pnet_def.NETWORK_TYPE: segment_db.NetworkSegment.network_type, |
| 140 | + pnet_def.PHYSICAL_NETWORK: segment_db.NetworkSegment.physical_network, |
| 141 | + pnet_def.SEGMENTATION_ID: segment_db.NetworkSegment.segmentation_id |
| 142 | + } |
| 143 | + |
| 144 | + if any(attr for attr in pnet_def.ATTRIBUTES if attr in filters): |
| 145 | + query = query.join( |
| 146 | + segment_db.NetworkSegment, |
| 147 | + segment_db.NetworkSegment.network_id == models_v2.Network.id) |
| 148 | + for attr in pnet_def.ATTRIBUTES: |
| 149 | + if attr not in filters: |
| 150 | + continue |
| 151 | + |
| 152 | + value = filters[attr] |
| 153 | + field = attr_to_field[attr] |
| 154 | + if utils.is_iterable_not_string(value): |
| 155 | + query = query.filter(field.in_(value)) |
| 156 | + else: |
| 157 | + query = query.filter(field == value) |
| 158 | + |
| 159 | + return query |
| 160 | + |
| 161 | + |
133 | 162 | @registry.has_registry_receivers
|
134 | 163 | class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
|
135 | 164 | neutron_plugin_base_v2.NeutronPluginBaseV2,
|
@@ -164,6 +193,12 @@ def __new__(cls, *args, **kwargs):
|
164 | 193 | query_hook=_port_query_hook,
|
165 | 194 | filter_hook=_port_filter_hook,
|
166 | 195 | result_filters=None)
|
| 196 | + model_query.register_hook( |
| 197 | + models_v2.Network, |
| 198 | + 'network', |
| 199 | + query_hook=None, |
| 200 | + filter_hook=None, |
| 201 | + result_filters=_network_result_filter_hook) |
167 | 202 | return super().__new__(cls, *args, **kwargs)
|
168 | 203 |
|
169 | 204 | @staticmethod
|
|
0 commit comments