networking_generic_switch.devices.restconf_devices package

Submodules

networking_generic_switch.devices.restconf_devices.openconfig module

class networking_generic_switch.devices.restconf_devices.openconfig.RestconfOpenConfigSwitch(device_cfg, *args, **kwargs)

Bases: OpenConfigModelMixin, RestconfSwitch

RESTCONF OpenConfig switch driver.

Manages network and port operations using OpenConfig YANG models over RESTCONF transport (RFC 8040). Callable class variables build OpenConfig model objects that the base class serialises to JSON (RFC 7951) and sends via HTTP PATCH.

static ADD_NETWORK(self, segmentation_id, network_name, **kwargs)

Build OpenConfig objects to create a VLAN.

Parameters:
  • segmentation_id – VLAN ID.

  • network_name – Name to assign to the VLAN on the device.

Returns:

list of OpenConfig model objects.

static ADD_NETWORK_TO_TRUNK(self, segmentation_id, trunk_ports, **kwargs)

Build OpenConfig objects to tag trunk ports with a VLAN.

When physnet_vlans is provided the complete set of trunk VLANs is written with operation="replace" so that the device converges to the desired state. Falls back to a single-VLAN merge when physnet_vlans is None.

Parameters:
  • segmentation_id – VLAN ID to add to trunk ports.

  • trunk_ports – List of switch interface names.

  • physnet_vlans – (kwarg) Complete set of VLAN IDs on the physical network, or None.

Returns:

list of OpenConfig model objects.

static ADD_SUBPORTS_ON_TRUNK(self, binding_profile, port_id, subports, trunk_details=None, **kwargs)

Build OpenConfig objects to add subport VLANs on a trunk port.

When trunk_details is provided the complete set of subport VLANs is written with operation="replace" so that the device converges to the desired state. Falls back to a per-VLAN merge when trunk_details is None.

Parameters:
  • binding_profile – Binding profile of the parent port.

  • port_id – Switch interface name.

  • subports – List of subport dicts (delta being added).

  • trunk_details – Full trunk details dict from the parent port, or None.

Returns:

list of OpenConfig model objects.

static DELETE_NETWORK(self, segmentation_id, network_name, **kwargs)

Build OpenConfig objects to remove a VLAN.

Not all devices support outright VLAN removal, so the VLAN is also set to SUSPENDED with a neutron-DELETED- name prefix as a fallback.

Parameters:
  • segmentation_id – VLAN ID.

  • network_name – Original network name (unused in the delete payload but accepted for signature compatibility).

Returns:

list of OpenConfig model objects.

static DELETE_PORT(self, port_id, segmentation_id, **kwargs)

Build OpenConfig objects to remove VLAN config from a port.

Parameters:
  • port_id – Switch interface name.

  • segmentation_id – VLAN ID.

Returns:

list of OpenConfig model objects.

static DEL_SUBPORTS_ON_TRUNK(self, binding_profile, port_id, subports, trunk_details=None, **kwargs)

Build OpenConfig objects to remove subport VLANs from a trunk.

When trunk_details is provided the remaining set of subport VLANs (already excluding the deleted ones) is written with operation="replace". Falls back to per-element operation="remove" when trunk_details is None.

Parameters:
  • binding_profile – Binding profile of the parent port.

  • port_id – Switch interface name.

  • subports – List of subport dicts (delta being removed).

  • trunk_details – Full trunk details dict from the parent port, or None.

Returns:

list of OpenConfig model objects.

static DISABLE_PORT(self, port_id, **kwargs)

Build OpenConfig objects to disable an interface.

Parameters:

port_id – Switch interface name.

Returns:

list of OpenConfig model objects.

static ENABLE_PORT(self, port_id, **kwargs)

Build OpenConfig objects to enable an interface.

Parameters:

port_id – Switch interface name.

Returns:

list of OpenConfig model objects.

static PLUG_PORT_TO_NETWORK(self, port_id, segmentation_id, **kwargs)

Build OpenConfig objects to assign a VLAN to a port.

When trunk_details is provided the port is configured as a trunk with native_vlan set to segmentation_id and all subport VLANs in trunk_vlans using operation="replace". Without trunk_details the port is set to access mode.

Parameters:
  • port_id – Switch interface name.

  • segmentation_id – VLAN ID.

  • trunk_details – (kwarg) Trunk information dict from the parent port, or None.

Returns:

list of OpenConfig model objects.

static REMOVE_NETWORK_FROM_TRUNK(self, segmentation_id, trunk_ports, **kwargs)

Build OpenConfig objects to untag trunk ports from a VLAN.

When physnet_vlans is provided the complete set (which already excludes the deleted VLAN) is written with operation="replace". Falls back to per-element operation="remove" when physnet_vlans is None.

Parameters:
  • segmentation_id – VLAN ID to remove from trunk ports.

  • trunk_ports – List of switch interface names.

  • physnet_vlans – (kwarg) Complete set of VLAN IDs on the physical network after deletion, or None.

Returns:

list of OpenConfig model objects.

property support_trunk_on_ports

networking_generic_switch.devices.restconf_devices.restconf_switch module

class networking_generic_switch.devices.restconf_devices.restconf_switch.RestconfSwitch(device_cfg, *args, **kwargs)

Bases: GenericSwitchDevice

Base class for RESTCONF-based switch drivers.

Provides HTTP session management, RESTCONF JSON serialization (RFC 8040 / RFC 7951), URL construction, retry logic, and coordination locking.

Subclasses must assign callable class variables (ADD_NETWORK, etc.) that build YANG model objects. The dispatch methods in this class invoke those callables, serialize the result to JSON via yutils.config_to_restconf_json, and send to the device over HTTPS.

ADD_NETWORK_TO_TRUNK = None
ADD_SUBPORTS_ON_TRUNK = None
DEL_SUBPORTS_ON_TRUNK = None
DISABLE_PORT = None
ENABLE_PORT = None
REMOVE_NETWORK_FROM_TRUNK = None
add_network(segmentation_id, network_id, physnet_vlans=None)

Create a VLAN on the device.

Parameters:
  • segmentation_id – VLAN ID of the network.

  • network_id – UUID of the Neutron network.

  • physnet_vlans – Complete set of VLAN segmentation IDs on the physical network, or None if convergence is not active.

add_security_group(sg)

Add a security group to a switch

Parameters:

sg – Security group object including rules

add_subports_on_trunk(binding_profile, port_id, subports, trunk_details=None)

Allow subports on trunk.

Parameters:
  • binding_profile – Binding profile of the parent port.

  • port_id – Name of the switch port.

  • subports – List of subport objects.

  • trunk_details – Full trunk details dict from the parent port.

Raises:

GenericSwitchNotSupported if not implemented by subclass.

bind_security_group(sg, port_id, port_ids)

Apply a security group to a port

The rules in the provided security group will also be used to assert the state with the switch.

Parameters:
  • sg – Security group object including rules

  • port_id – Name of switch port to bind group to

  • port_ids – Names of all switch ports currently bound to this group

del_network(segmentation_id, network_id, physnet_vlans=None)

Remove a VLAN from the device.

Parameters:
  • segmentation_id – VLAN ID of the network.

  • network_id – UUID of the Neutron network.

  • physnet_vlans – Complete set of VLAN segmentation IDs on the physical network, or None if convergence is not active.

del_security_group(sg_id)

Delete a security group

Parameters:

sg_id – Security group ID

del_subports_on_trunk(binding_profile, port_id, subports, trunk_details=None)

Remove subports from trunk.

Parameters:
  • binding_profile – Binding profile of the parent port.

  • port_id – Name of the switch port.

  • subports – List of subport objects.

  • trunk_details – Full trunk details dict from the parent port.

Raises:

GenericSwitchNotSupported if not implemented by subclass.

delete_from_device(path)

Delete a resource from the device via RESTCONF DELETE.

Parameters:

path – RESTCONF resource path to delete.

delete_port(port_id, segmentation_id, trunk_details=None, default_vlan=None)

Delete a port from a network.

Parameters:
  • port_id – Name of the switch interface.

  • segmentation_id – VLAN ID of the network.

  • trunk_details – Trunk information if port is part of a trunk.

  • default_vlan – Default VLAN ID when port is unconfigured.

get_from_device(path)

Read data from the device via RESTCONF GET.

Parameters:

path – RESTCONF resource path (appended to the base URL).

Returns:

Parsed JSON response as a dict.

plug_port_to_network(port_id, segmentation_id, trunk_details=None, default_vlan=None)

Plug a port into a network.

Parameters:
  • port_id – Name of the switch interface.

  • segmentation_id – VLAN ID of the network.

  • trunk_details – Trunk information if port is part of a trunk.

  • default_vlan – Default VLAN ID when port is unconfigured.

plug_switch_to_network(vni, segmentation_id, physnet=None)

Configure L2VNI mapping on the switch.

In VXLAN L2VNI scenarios with hierarchical port binding, Neutron creates a VXLAN network (top segment) and dynamically allocates a local VLAN (bottom segment) on each switch. This method maps the VLAN to the VNI on the switch fabric.

Called during port binding when both conditions are met: - Top bound segment is VXLAN - Bottom bound segment is VLAN

For switches that don’t support VXLAN, this can be left as None (will log a warning but not fail).

Parameters:
  • vni – The VXLAN Network Identifier

  • segmentation_id – VLAN ID to map to the VNI

  • physnet – Physical network name for per-physnet configuration (optional, for future use).

Raises:

GenericSwitchConfigException on configuration failure

send_config_to_device(config, method='PATCH')

Send configuration to the device via RESTCONF.

Serializes model objects to RESTCONF JSON (RFC 7951) and sends one request per top-level container key. NX-OS does not support PATCH to the bare /restconf/data root with multiple containers, so each key is sent to its own resource URL.

Parameters:
  • config – Configuration object or list of configuration objects. Each must implement to_restconf_dict().

  • method – HTTP method to use (default PATCH). Use PUT for operations that replace the resource entirely (e.g. interface trunk-vlan convergence).

property trunk_vlans_converge

Whether trunk VLAN list should be fully converged on each operation.

When True, the mechanism driver queries the Neutron DB for all VLAN segments on the physical network and passes the complete set via the physnet_vlans parameter.

unbind_security_group(sg_id, port_id, port_ids)

Remove a bound security group from a port

Parameters:
  • sg_id – ID of security group to unbind

  • port_id – Name of switch port to unbind group from

  • port_ids – Names of all switch ports currently bound to this group

unplug_switch_from_network(vni, segmentation_id, physnet=None)

Remove L2VNI mapping from the switch.

Removes the VNI-to-VLAN mapping when the last port on a VLAN is unplugged. Called automatically by the cleanup logic in _unplug_port_from_segment() after verifying no ports remain via vlan_has_ports().

Should be idempotent - safely handle cases where the VNI is already removed.

Example (Cisco NX-OS):
interface nve1

no member vni 5000

vlan 100

no vn-segment

Parameters:
  • vni – The VXLAN Network Identifier to remove

  • segmentation_id – VLAN ID from which to remove the VNI mapping

  • physnet – Physical network name (optional, for signature consistency)

Raises:

GenericSwitchConfigException on configuration failure

update_security_group(sg)

Updates an existing a security group on a switch

Rules may have been added or deleted so the driver needs to update the switch state to accurately reflect the provided security group.

Parameters:

sg – Security group object including rules

vlan_has_ports(segmentation_id)

Check if a VLAN has any switch ports currently assigned.

Used by L2VNI cleanup logic to determine if it’s safe to remove the VNI mapping. The VNI should only be removed when no ports remain on the VLAN.

This is a read-only operation and should not acquire locks.

Implementations should: - Query the switch directly (not rely on cached state) - Return True if the VLAN has any ports (access or trunk) - Return True on error (conservative - prevents accidental removal) - Return True if query command is not implemented

Parameters:

segmentation_id – VLAN ID to check

Returns:

True if VLAN has ports assigned, False if empty

vlan_has_vni(segmentation_id, vni)

Check if a VLAN already has a specific VNI mapping configured.

Used for idempotency during port binding to avoid reconfiguring the same VNI mapping multiple times when multiple ports bind to the same VXLAN network.

This is a read-only operation and should not acquire locks.

Implementations should: - Query the switch directly (not rely on cached state) - Return True only if this exact VNI is configured on this VLAN - Return False on error (will attempt to configure) - Return False if query command is not implemented

Parameters:
  • segmentation_id – VLAN ID to check

  • vni – VNI to check for

Returns:

True if VLAN has this VNI configured, False otherwise

Module contents