|
31 | 31 | from oslo_log import log as logging
|
32 | 32 | from oslo_utils import uuidutils
|
33 | 33 |
|
| 34 | +from neutron.db.models import agent as agent_model |
| 35 | +from neutron.db.models import segment as segment_model |
34 | 36 | from neutron.db import segments_db as db
|
35 | 37 | from neutron import manager
|
36 | 38 | from neutron.objects import base as base_obj
|
@@ -243,14 +245,43 @@ def update_segment_host_mapping(context, host, current_segment_ids):
|
243 | 245 | entry.segment_id, entry.host)
|
244 | 246 |
|
245 | 247 |
|
246 |
| -def get_hosts_mapped_with_segments(context): |
| 248 | +def get_hosts_mapped_with_segments(context, include_agent_types=None, |
| 249 | + exclude_agent_types=None): |
247 | 250 | """Get hosts that are mapped with segments.
|
248 | 251 |
|
249 | 252 | L2 providers can use this method to get an overview of SegmentHostMapping,
|
250 | 253 | and then delete the stale SegmentHostMapping.
|
| 254 | +
|
| 255 | + When using both include_agent_types and exclude_agent_types, |
| 256 | + exclude_agent_types is most significant. |
| 257 | + All hosts without agent are excluded when using any agent_type filter. |
| 258 | +
|
| 259 | + :param context: current running context information |
| 260 | + :param include_agent_types: (set) List of agent types, include hosts |
| 261 | + with matching agents. |
| 262 | + :param exclude_agent_types: (set) List of agent types, exclude hosts |
| 263 | + with matching agents. |
251 | 264 | """
|
252 |
| - segment_host_mapping = network.SegmentHostMapping.get_objects(context) |
253 |
| - return {row.host for row in segment_host_mapping} |
| 265 | + def add_filter_by_agent_types(qry, include, exclude): |
| 266 | + qry = qry.join( |
| 267 | + agent_model.Agent, |
| 268 | + segment_model.SegmentHostMapping.host == agent_model.Agent.host) |
| 269 | + if include: |
| 270 | + qry = qry.filter(agent_model.Agent.agent_type.in_(include)) |
| 271 | + if exclude: |
| 272 | + qry = qry.filter(agent_model.Agent.agent_type.not_in(exclude)) |
| 273 | + |
| 274 | + return qry |
| 275 | + |
| 276 | + with db_api.CONTEXT_READER.using(context): |
| 277 | + query = context.session.query(segment_model.SegmentHostMapping) |
| 278 | + if include_agent_types or exclude_agent_types: |
| 279 | + query = add_filter_by_agent_types(query, include_agent_types, |
| 280 | + exclude_agent_types) |
| 281 | + |
| 282 | + res = query.all() |
| 283 | + |
| 284 | + return {row.host for row in res} |
254 | 285 |
|
255 | 286 |
|
256 | 287 | def _get_phys_nets(agent):
|
|
0 commit comments