|
| 1 | +"""Compatibility helpers for the EVPN router extension. |
| 2 | +
|
| 3 | +The EVPN router feature (the ``evpn_vni`` attribute, its API extension and its |
| 4 | +policies) was upstreamed into neutron. On a neutron that carries it (e.g. the |
| 5 | +understack/2026.1 branch) core provides all of: |
| 6 | +
|
| 7 | +* ``neutron_lib.api.definitions.evpn`` -- the router ``evpn_vni`` attribute |
| 8 | +* ``neutron.extensions.evpn`` -- the API extension |
| 9 | +* ``neutron.conf.policies.evpn`` -- the create/get ``router:evpn_vni`` policies |
| 10 | +
|
| 11 | +If ``neutron_understack`` also registers any of these, neutron-server fails at |
| 12 | +startup -- most visibly ``DuplicatePolicyError`` raised from |
| 13 | +``neutron.policy.register_rules()``, which aborts policy initialization for the |
| 14 | +whole service. When core owns EVPN, Understack must register none of them and |
| 15 | +contribute only its runtime VNI allocation (the service-plugin callbacks). |
| 16 | +
|
| 17 | +Every surface that would otherwise register an EVPN artifact keys off |
| 18 | +``core_provides_evpn()`` so the decision is made in exactly one place. |
| 19 | +""" |
| 20 | + |
| 21 | +from neutron.conf import policies as core_policies |
| 22 | + |
| 23 | +from neutron_understack.api.definitions import understack_vni |
| 24 | + |
| 25 | +try: |
| 26 | + from neutron_lib.api.definitions import evpn as core_evpn_apidef |
| 27 | +except ImportError: |
| 28 | + core_evpn_apidef = None |
| 29 | + |
| 30 | +# The router EVPN policy name core registers when it owns the feature. This is |
| 31 | +# also the exact rule neutron_understack would otherwise re-register, so its |
| 32 | +# presence in core's policy list is what triggers the DuplicatePolicyError. |
| 33 | +_CORE_EVPN_POLICY = "create_router:evpn_vni" |
| 34 | + |
| 35 | + |
| 36 | +def core_provides_evpn(): |
| 37 | + """Return True when neutron core registers the EVPN router feature itself. |
| 38 | +
|
| 39 | + Checks the actual policy list neutron.policy.register_rules() consumes |
| 40 | + (``neutron.conf.policies.list_rules()``), rather than merely whether the |
| 41 | + extension module is importable -- core registers these policies |
| 42 | + unconditionally at startup, independent of which extensions are loaded. |
| 43 | + """ |
| 44 | + return any(rule.name == _CORE_EVPN_POLICY for rule in core_policies.list_rules()) |
| 45 | + |
| 46 | + |
| 47 | +def api_definition(): |
| 48 | + """The api-definition to use for the router VNI extension. |
| 49 | +
|
| 50 | + Prefer core's ``evpn`` api-definition when core owns the feature so the |
| 51 | + ``evpn_vni`` attribute is defined exactly once; otherwise fall back to |
| 52 | + Understack's own definition. |
| 53 | + """ |
| 54 | + if core_provides_evpn() and core_evpn_apidef is not None: |
| 55 | + return core_evpn_apidef |
| 56 | + return understack_vni |
0 commit comments