Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/sonic-yang-mgmt/sonic_yang.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import libyang as ly
import syslog
from _libyang import ffi
from libyang.util import ly_array_iter

from json import dump
from glob import glob
Expand Down Expand Up @@ -159,6 +161,20 @@ def _yjson_node(self, snode: Any) -> Tuple[Optional[str], Dict[str, Any]]:
# legacy yJson consumers don't read those branches.
return None, d

@staticmethod
def union_member_types(type_obj: ly.Type) -> List[ly.Type]:
"""Return the compiled member types of a union, in compiled order.

A nested union (such as inet:ip-address) is already flattened into its
members by libyang. A non-union returns []. Use this over
`Type.union_types()`, which pairs compiled members with parsed ones
and so drops the trailing members of a union holding a nested union.
"""
if type_obj.base() != ly.Type.UNION:
return []
union = ffi.cast('struct lysc_type_union *', type_obj.cdata)
return [ly.Type(type_obj.context, member, None) for member in ly_array_iter(union.types)]

def _yjson_type(self, type_obj: Any) -> Dict[str, Any]:
# Prefer the typedef-aware name (e.g. "stypes:hwsku") so consumers
# that text-match on prefixed type names continue to work; fall back
Expand All @@ -171,7 +187,12 @@ def _yjson_type(self, type_obj: Any) -> Dict[str, Any]:
if path:
d['path'] = {'@value': path}
elif base == 'union':
members = [self._yjson_type(t) for t in type_obj.union_types()]
member_types = SonicYang.union_member_types(type_obj)
named = list(type_obj.union_types())
if len(named) == len(member_types):
# The same members, carrying their typedef names.
member_types = named
members = [self._yjson_type(t) for t in member_types]
if members:
d['type'] = members[0] if len(members) == 1 else members
return d
Expand Down
43 changes: 43 additions & 0 deletions src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ def test_xlate_rev_xlate(self, sonic_yang_data):
@pytest.fixture
def dhcp_match_config(self):
return {
"VLAN": {
"Vlan100": {"vlanid": "100"},
},
"DHCP_SERVER_IPV4": {
"Vlan100": {
"mode": "PORT",
Expand Down Expand Up @@ -785,5 +788,45 @@ def test_loaddata_default_logs_syslog_on_success(self, sonic_yang_data, monkeypa

return

BGP_NEIGHBOR_XPATH = "/sonic-bgp-neighbor:sonic-bgp-neighbor/BGP_NEIGHBOR/BGP_NEIGHBOR_LIST"

def _leaf_type(self, syc, leaf):
snodes = list(syc.ctx.find_path("{}/{}".format(self.BGP_NEIGHBOR_XPATH, leaf)))
assert len(snodes) == 1
return snodes[0].type()

def test_union_member_types_keeps_trailing_leafrefs(self, sonic_yang_data):
# neighbor is a union of inet:ip-address (itself a union of ipv4 and
# ipv6) and four leafrefs; libyang compiles it flat into six members.
syc = sonic_yang_data['syc']
neighbor = self._leaf_type(syc, "neighbor")

members = sy.SonicYang.union_member_types(neighbor)
assert [m.basename() for m in members] == ['string', 'string'] + ['leafref'] * 4
assert 'VLAN_SUB_INTERFACE_LIST' in members[-1].leafref_path()
# union_types() pairs the six compiled members with the five parsed
# ones and drops the last.
assert len(list(neighbor.union_types())) == 5

def test_union_member_types_of_non_union(self, sonic_yang_data):
syc = sonic_yang_data['syc']
assert sy.SonicYang.union_member_types(self._leaf_type(syc, "peer_group_name")) == []

def test_yjson_union_keeps_trailing_leafrefs(self, sonic_yang_data):
syc = sonic_yang_data['syc']

def child(node, kind, name):
children = node[kind] if isinstance(node[kind], list) else [node[kind]]
return next(c for c in children if c['@name'] == name)

module = next(m['module'] for m in syc.yJson if m['module']['@name'] == 'sonic-bgp-neighbor')
node = child(module, 'container', 'sonic-bgp-neighbor')
node = child(node, 'container', 'BGP_NEIGHBOR')
node = child(node, 'list', 'BGP_NEIGHBOR_LIST')
node = child(node, 'leaf', 'neighbor')
members = node['type']['type']
assert len(members) == 6
assert 'VLAN_SUB_INTERFACE_LIST' in members[-1]['path']['@value']

def teardown_class(self):
pass
13 changes: 12 additions & 1 deletion src/sonic-yang-models/doc/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,11 @@ The table key can be a plain neighbor IP (e.g. `BGP_NEIGHBOR|10.0.0.61`) or a
form, `vrf_name` accepts a VRF name (e.g. `default`, `Vrf1`) or a VNet name
(e.g. `Vnet1`) for overlay BGP neighbors.

An unnumbered neighbor is keyed by a port, port channel, VLAN or VLAN sub-interface,
which must exist in `PORT`, `PORTCHANNEL`, `VLAN` or `VLAN_SUB_INTERFACE`.
`local_addr` may name a port, port channel, loopback or VLAN, which must exist in
`PORT`, `PORTCHANNEL`, `LOOPBACK_INTERFACE` or `VLAN`.

```
{
"BGP_NEIGHBOR": {
Expand Down Expand Up @@ -1210,6 +1215,9 @@ instance is supported in SONiC.

### DHCPV4_RELAY

The key names a VLAN, which must exist in `VLAN`. `source_interface` must exist in `PORT`,
`PORTCHANNEL`, `VLAN` or `LOOPBACK_INTERFACE`.

```
{
"DHCPV4_RELAY": {
Expand Down Expand Up @@ -1247,6 +1255,7 @@ BMP related configuration are defined in **bgp_neighbor_table**,**bgp_rib_in_tab

### DHCP_SERVER_IPV4
IPV4 DHCP Server related configuration are defined in **DHCP_SERVER_IPV4**, **DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS**, **DHCP_SERVER_IPV4_RANGE**, **DHCP_SERVER_IPV4_PORT**, **DHCP_SERVER_IPV4_MATCH**, **DHCP_SERVER_IPV4_BINDING** tables.
The **DHCP_SERVER_IPV4** key names a VLAN, which must exist in `VLAN`, or the midplane bridge.
```
{
"DHCP_SERVER_IPV4": {
Expand Down Expand Up @@ -2065,6 +2074,7 @@ The **MUX_LINKMGR** table is used for dualtor device configuration.
### NEIGH

The **NEIGH** table is used to keep track of resolved and static neighbors.
The interface in the key must exist in `PORT`, `PORTCHANNEL` or `VLAN`.

Resolve case:
```
Expand Down Expand Up @@ -3527,6 +3537,7 @@ For the flags that can be changed by reconfiguration, we can update entries in `
### RADIUS

The RADIUS and RADIUS_SERVER tables define RADIUS configuration parameters. RADIUS table carries global configuration while RADIUS_SERVER table carries per server configuration.
A RADIUS_SERVER `src_intf` must exist in `PORT`, `PORTCHANNEL`, `VLAN`, `LOOPBACK_INTERFACE` or `MGMT_PORT`.

```
"RADIUS": {
Expand Down Expand Up @@ -3868,7 +3879,7 @@ Like NTP global configuration, DASH HA global configuration must have one entry

**dpu_vnet**: Name of the vnet used for VNET tunnel route in SmartSwitch HA scenarios. Replaces `vnet_name`.

**dpu_vlan**: DPU VLAN identifier, referencing a VLAN name from the VLAN table.
**dpu_vlan**: The VLAN hamgrd resolves the DPU PA address on; it must exist in the VLAN table.

**cp_data_channel_port**: Control plane data channel port, used for bulk sync.

Expand Down
4 changes: 4 additions & 0 deletions src/sonic-yang-models/tests/files/sample_config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,10 @@
"vlanid": "12",
"mtu": "9216",
"admin_status": "up"
},
"Vlan222": {
"description": "dhcpv4 relay",
"vlanid": "222"
}

},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

def match_binding_data(match_name="vendor", binding_name="b1"):
return {
"sonic-vlan:sonic-vlan": {
"VLAN": {
"VLAN_LIST": [{"name": "Vlan100"}],
},
},
"sonic-dhcp-server-ipv4:sonic-dhcp-server-ipv4": {
"DHCP_SERVER_IPV4": {
"DHCP_SERVER_IPV4_LIST": [{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,17 @@ def test_dash_ha_global_config_dpu_vnet_invalid(self, yang_model):
yang_model.load_data(data, 'Invalid leafref value "NonExistentVnet"')

def test_dash_ha_global_config_dpu_vlan(self, yang_model):
"""Test dpu_vlan string field"""
"""Test dpu_vlan referencing an existing VLAN"""
data = {
"sonic-vlan:sonic-vlan": {
"sonic-vlan:VLAN": {
"VLAN_LIST": [
{
"name": "Vlan100"
}
]
}
},
"sonic-smart-switch:sonic-smart-switch": {
"sonic-smart-switch:DASH_HA_GLOBAL_CONFIG": {
"global": {
Expand All @@ -557,6 +566,20 @@ def test_dash_ha_global_config_dpu_vlan(self, yang_model):
}
yang_model.load_data(data)

def test_dash_ha_global_config_dpu_vlan_invalid(self, yang_model):
"""Test dpu_vlan with non-existent VLAN reference"""
data = {
"sonic-smart-switch:sonic-smart-switch": {
"sonic-smart-switch:DASH_HA_GLOBAL_CONFIG": {
"global": {
"dpu_vlan": "Vlan100",
"cp_data_channel_port": 11234
}
}
}
}
yang_model.load_data(data, 'Invalid leafref value "Vlan100"')

@pytest.mark.parametrize(
"dpu_name, error_message", [
("dpu0", None),
Expand Down
33 changes: 33 additions & 0 deletions src/sonic-yang-models/tests/yang_model_tests/tests/bgp.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,38 @@
"BGP_SENTINEL_INVALID_RANGE_IPV6": {
"desc": "does not satisfy.",
"eStr": "Invalid value \"fc00:f0::/129\" in \"ip_range\" element."
},
"BGP_NEIGHBOR_VLAN_VALID": {
"desc": "A BGP neighbor on an existing VLAN is accepted."
},
"BGP_NEIGHBOR_VLAN_MISSING": {
"desc": "A BGP neighbor naming a VLAN that does not exist is refused.",
"eStrKey": "LeafRef"
},
"BGP_NEIGHBOR_SUBINTF_VALID": {
"desc": "A BGP neighbor on an existing VLAN sub-interface is accepted."
},
"BGP_NEIGHBOR_SUBINTF_MISSING": {
"desc": "A BGP neighbor naming a VLAN sub-interface that does not exist is refused.",
"eStrKey": "LeafRef"
},
"BGP_NEIGHBOR_LOCAL_ADDR_VLAN_VALID": {
"desc": "A BGP neighbor local_addr naming an existing VLAN is accepted."
},
"BGP_NEIGHBOR_LOCAL_ADDR_VLAN_MISSING": {
"desc": "A BGP neighbor local_addr naming a VLAN that does not exist is refused.",
"eStrKey": "LeafRef"
},
"BGP_NEIGHBOR_VLAN_4095": {
"desc": "A BGP neighbor on Vlan4095 is refused.",
"eStr": [
"Vlan4095 is reserved"
]
},
"BGP_NEIGHBOR_LOCAL_ADDR_VLAN_4095": {
"desc": "A BGP neighbor local_addr of Vlan4095 is refused.",
"eStr": [
"Vlan4095 is reserved"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,25 @@
"desc": "Internal BGP neighbor description contains a newline.",
"eStrKey": "Pattern",
"eStr": ["name"]
},
"BGP_INTERNAL_VLAN_NEIGHBOR_VALID": {
"desc": "A BGP_INTERNAL_NEIGHBOR on an existing VLAN is accepted."
},
"BGP_INTERNAL_VLAN_NEIGHBOR_MISSING": {
"desc": "A BGP_INTERNAL_NEIGHBOR naming a VLAN that does not exist is refused.",
"eStrKey": "LeafRef"
},
"BGP_INTERNAL_SUBINTF_NEIGHBOR_VALID": {
"desc": "A BGP_INTERNAL_NEIGHBOR on an existing VLAN sub-interface is accepted."
},
"BGP_INTERNAL_SUBINTF_NEIGHBOR_MISSING": {
"desc": "A BGP_INTERNAL_NEIGHBOR naming a VLAN sub-interface that does not exist is refused.",
"eStrKey": "LeafRef"
},
"BGP_INTERNAL_VLAN_NEIGHBOR_4095": {
"desc": "A BGP_INTERNAL_NEIGHBOR on Vlan4095 is refused.",
"eStr": [
"Vlan4095 is reserved"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,25 @@
"desc": "VOQ chassis BGP neighbor description contains a newline.",
"eStrKey": "Pattern",
"eStr": ["name"]
},
"BGP_VOQ_CHASSIS_VLAN_NEIGHBOR_VALID": {
"desc": "A BGP_VOQ_CHASSIS_NEIGHBOR on an existing VLAN is accepted."
},
"BGP_VOQ_CHASSIS_VLAN_NEIGHBOR_MISSING": {
"desc": "A BGP_VOQ_CHASSIS_NEIGHBOR naming a VLAN that does not exist is refused.",
"eStrKey": "LeafRef"
},
"BGP_VOQ_CHASSIS_SUBINTF_NEIGHBOR_VALID": {
"desc": "A BGP_VOQ_CHASSIS_NEIGHBOR on an existing VLAN sub-interface is accepted."
},
"BGP_VOQ_CHASSIS_SUBINTF_NEIGHBOR_MISSING": {
"desc": "A BGP_VOQ_CHASSIS_NEIGHBOR naming a VLAN sub-interface that does not exist is refused.",
"eStrKey": "LeafRef"
},
"BGP_VOQ_CHASSIS_VLAN_NEIGHBOR_4095": {
"desc": "A BGP_VOQ_CHASSIS_NEIGHBOR on Vlan4095 is refused.",
"eStr": [
"Vlan4095 is reserved"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,15 @@
"DHCP_SERVER_IPV4_RANGE_THREE_IP": {
"desc": "Three ips in DHCP_SERVER_IPV4_RANGE",
"eStrKey": "Must"
},
"DHCP_SERVER_IPV4_VLAN_MISSING": {
"desc": "Configure DHCP_SERVER_IPV4 on a VLAN that does not exist.",
"eStrKey": "LeafRef"
},
"DHCP_SERVER_IPV4_VLAN_4095": {
"desc": "Configure DHCP_SERVER_IPV4 on Vlan4095.",
"eStr": [
"Vlan4095 is reserved"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,46 @@
"DHCPv4_RELAY_WITH_SERVER_VRF_AND_MISSING_SOURCE_INTERFACE_OPTION": {
"desc": "Add dhcpv4 relay with server_vrf and missing source-interface option.",
"eStrKey": "Must"
},
"DHCPv4_RELAY_VLAN_MISSING": {
"desc": "Add dhcpv4 relay on a VLAN that does not exist.",
"eStrKey": "LeafRef"
},
"DHCPv4_RELAY_SOURCE_INTERFACE_VLAN_VALID": {
"desc": "Add dhcpv4 relay with an existing VLAN as source_interface."
},
"DHCPv4_RELAY_SOURCE_INTERFACE_VLAN_MISSING": {
"desc": "Add dhcpv4 relay with a source_interface VLAN that does not exist.",
"eStrKey": "LeafRef"
},
"DHCPv4_RELAY_VLAN_4095": {
"desc": "Add dhcpv4 relay on Vlan4095.",
"eStr": [
"Vlan4095 is reserved"
]
},
"DHCPv4_RELAY_SOURCE_INTERFACE_VLAN_4095": {
"desc": "Add dhcpv4 relay with Vlan4095 as source_interface.",
"eStr": [
"Vlan4095 is reserved"
]
},
"DHCPv4_RELAY_WITH_NO_IP": {
"desc": "Reject DHCPV4_RELAY.dhcpv4_servers with no ip.",
"eStr": [
"DHCPv4 servers require an IPv4 prefix on the same VLAN"
]
},
"DHCPv4_RELAY_WITH_WRONG_IP_FAMILY": {
"desc": "Reject DHCPV4_RELAY.dhcpv4_servers with wrong ip family.",
"eStr": [
"DHCPv4 servers require an IPv4 prefix on the same VLAN"
]
},
"DHCPv4_RELAY_WITH_IP_ON_OTHER_VLAN": {
"desc": "Reject DHCPV4_RELAY.dhcpv4_servers with ip on other vlan.",
"eStr": [
"DHCPv4 servers require an IPv4 prefix on the same VLAN"
]
}
}
Loading