The CBOR encoding of an instance of a leaf or leaf-list representation node depends on the built-in type of that representation node. The following subsection defines the CBOR encoding of each built-in type supported by YANG, as listed in
Section 4.2.4 of
RFC 7950. Each subsection shows an example value assigned to a representation node instance of the discussed built-in type.
Leafs of type uint8, uint16, uint32, and uint64
MUST be encoded using a CBOR unsigned integer data item (major type 0).
The following example shows the encoding of an 'mtu' leaf representation node instance set to 1280 bytes.
Definition example adapted from [
RFC 8344]:
leaf mtu {
type uint16 {
range "68..max";
}
}
CBOR diagnostic notation: 1280
CBOR encoding: 19 0500
Leafs of type int8, int16, int32, and int64
MUST be encoded using either a CBOR unsigned integer (major type 0) or a CBOR negative integer (major type 1), depending on the actual value.
The following example shows the encoding of a 'timezone-utc-offset' leaf representation node instance set to -300 minutes.
Definition example adapted from [
RFC 7317]:
leaf timezone-utc-offset {
type int16 {
range "-1500 .. 1500";
}
}
CBOR diagnostic notation: -300
CBOR encoding: 39 012B
Leafs of type decimal64
MUST be encoded using a decimal fraction, as defined in
Section 3.4.4 of
RFC 8949.
The following example shows the encoding of a 'my-decimal' leaf representation node instance set to 2.57.
Definition example adapted from [
RFC 7317]:
leaf my-decimal {
type decimal64 {
fraction-digits 2;
range "1 .. 3.14 | 10 | 20..max";
}
}
CBOR diagnostic notation: 4([-2, 257])
CBOR encoding: C4 82 21 19 0101
Leafs of type string
MUST be encoded using a CBOR text string data item (major type 3).
The following example shows the encoding of a 'name' leaf representation node instance set to "eth0".
Definition example adapted from [
RFC 8343]:
leaf name {
type string;
}
CBOR diagnostic notation: "eth0"
CBOR encoding: 64 65746830
Leafs of type boolean
MUST be encoded using a CBOR simple value 'true' (major type 7, additional information 21) or 'false' (major type 7, additional information 20).
The following example shows the encoding of an 'enabled' leaf representation node instance set to 'true'.
Definition example adapted from [
RFC 7317]:
leaf enabled {
type boolean;
}
CBOR diagnostic notation: true
CBOR encoding: F5
Leafs of type enumeration
MUST be encoded using a CBOR unsigned integer (major type 0) or CBOR negative integer (major type 1), depending on the actual value, or exceptionally as a tagged text string (see below). Enumeration values are either explicitly assigned using the YANG statement 'value' or automatically assigned based on the algorithm defined in
Section 9.6.4.2 of
RFC 7950.
The following example shows the encoding of an 'oper-status' leaf representation node instance set to 'testing'.
Definition example adapted from [
RFC 7317]:
leaf oper-status {
type enumeration {
enum up { value 1; }
enum down { value 2; }
enum testing { value 3; }
enum unknown { value 4; }
enum dormant { value 5; }
enum not-present { value 6; }
enum lower-layer-down { value 7; }
}
}
CBOR diagnostic notation: 3
CBOR encoding: 03
Values of 'enumeration' types defined in a 'union' type
MUST be encoded using a CBOR text string data item (major type 3) and
MUST contain one of the names assigned by 'enum' statements in YANG (see also
Section 6.12). The encoding
MUST be enclosed by the enumeration CBOR tag, as specified in
Section 9.3.
Definition example adapted from [
RFC 7950]:
type union {
type int32;
type enumeration {
enum unbounded;
}
}
CBOR diagnostic notation: 44("unbounded")
CBOR encoding: D8 2C 69 756E626F756E646564
Keeping in mind that bit positions are either explicitly assigned using the YANG statement 'position' or automatically assigned based on the algorithm defined in
Section 9.7.4.2 of
RFC 7950, each element of type bits could be seen as a set of bit positions (or offsets from position 0) that have a value of either 1, which represents the bit being set, or 0, which represents that the bit is not set.
Leafs of type bits
MUST be encoded either using a CBOR array (major type 4) or byte string (major type 2) or exceptionally as a tagged text string (see below). In case CBOR array representation is used, each element is either (1) a positive integer (major type 0 with value 0 being disallowed) that can be used to calculate the offset of the next byte string or (2) a byte string (major type 2) that carries the information regarding whether certain bits are set or not. The initial offset value is 0, and each unsigned integer modifies the offset value of the next byte string by the integer value multiplied by 8. For example, if the bit offset is 0 and there is an integer with value 5, the first byte of the byte string that follows will represent bit positions 40 to 47, with both ends included. If the byte string has a second byte, it will carry information about bits 48 to 55, and so on. Within each byte, bits are assigned from least to most significant. After the byte string, the offset is modified by the number of bytes in the byte string multiplied by 8. Bytes with no bits set (zero bytes) at the end of the byte string are never generated. If they occur at the end of the array, the zero bytes are simply omitted; if they occur at the end of a byte string preceding an integer, the zero bytes are removed and the integer is adjusted upwards by the number of zero bytes that were removed. An example follows.
The following example shows the encoding of an 'alarm-state' leaf representation node instance with the 'critical' (position 2), 'warning' (position 8), and 'indeterminate' (position 128) flags set.
typedef alarm-state {
type bits {
bit unknown;
bit under-repair;
bit critical;
bit major;
bit minor;
bit warning {
position 8;
}
bit indeterminate {
position 128;
}
}
}
leaf alarm-state {
type alarm-state;
}
CBOR diagnostic notation: [h'0401', 14, h'01']
CBOR encoding: 83 42 0401 0E 41 01
In a number of cases, the array would only need to have one element -- a byte string with a few bytes inside. For this case, it is
REQUIRED to omit the array element and have only the byte array that would have been inside. To illustrate this, let us consider the same example YANG definition but this time encoding only 'under-repair' and 'critical' flags. The result would be
CBOR diagnostic notation: h'06'
CBOR encoding: 41 06
Elements in the array
MUST be either byte strings that do not end in a zero byte or positive unsigned integers, where byte strings and integers
MUST alternate, i.e., adjacent byte strings or adjacent integers are an error. An array with a single byte string
MUST instead be encoded as just that byte string. An array with a single positive integer is an error. Note that a recipient can handle trailing zero bytes in the byte strings using the normal rules without any issue, so an implementation
MAY silently accept them.
Values of 'bits' types defined in a 'union' type
MUST be encoded using a CBOR text string data item (major type 3) and
MUST contain a space-separated sequence of names of 'bits' that are set (see also
Section 6.12). The encoding
MUST be enclosed by the bits CBOR tag, as specified in
Section 9.3.
The following example shows the encoding of an 'alarm-state' leaf representation node instance defined using a union type with the 'under-repair' and 'critical' flags set.
Definition example:
leaf alarm-state-2 {
type union {
type alarm-state;
type bits {
bit extra-flag;
}
}
}
CBOR diagnostic notation: 43("under-repair critical")
CBOR encoding: D8 2B 75 756E6465722D72657061697220637269746963616C
Leafs of type binary
MUST be encoded using a CBOR byte string data item (major type 2).
The following example shows the encoding of an 'aes128-key' leaf representation node instance set to 0x1f1ce6a3f42660d888d92a4d8030476e.
Definition example:
leaf aes128-key {
type binary {
length 16;
}
}
CBOR diagnostic notation: h'1F1CE6A3F42660D888D92A4D8030476E'
CBOR encoding: 50 1F1CE6A3F42660D888D92A4D8030476E
Leafs of type leafref
MUST be encoded using the rules of the representation node referenced by the 'path' YANG statement.
The following example shows the encoding of an 'interface-state-ref' leaf representation node instance set to "eth1".
Definition example adapted from [
RFC 8343]:
typedef interface-state-ref {
type leafref {
path "/interfaces-state/interface/name";
}
}
container interfaces-state {
list interface {
key "name";
leaf name {
type string;
}
leaf-list higher-layer-if {
type interface-state-ref;
}
}
}
CBOR diagnostic notation: "eth1"
CBOR encoding: 64 65746831
This specification supports two approaches for encoding identityref: as a YANG Schema Item iDentifier, as defined in
Section 3.2, or as a name, as defined in
Section 6.8 of
RFC 7951. See
Section 6.12 for an exceptional case when this representation needs to be tagged.
When representation nodes of type identityref are implemented using SIDs, they
MUST be encoded using a CBOR unsigned integer data item (major type 0). (Note that, as they are not used in the position of CBOR map keys, no delta mechanism is employed for SIDs used for identityref.)
The following example shows the encoding of a 'type' leaf representation node instance set to the value 'iana-if-type:ethernetCsmacd' (SID 1880).
Definition example adapted from [
RFC 7317]:
identity interface-type {
}
identity iana-interface-type {
base interface-type;
}
identity ethernetCsmacd {
base iana-interface-type;
}
leaf type {
type identityref {
base interface-type;
}
}
CBOR diagnostic notation: 1880
CBOR encoding: 19 0758
Alternatively, an identityref
MAY be encoded using a name, as defined in
Section 3.3. When names are used, identityref
MUST be encoded using a CBOR text string data item (major type 3). If the identity is defined in a different module than the leaf node containing the identityref data node, the namespace-qualified form
MUST be used. Otherwise, both the simple and namespace-qualified forms are permitted. Names and namespaces are defined in
Section 3.3.
The following example shows the encoding of the identity 'iana-if-type:ethernetCsmacd' using its namespace-qualified name. This example is described in
Section 6.10.1.
CBOR diagnostic notation: "iana-if-type:ethernetCsmacd"
CBOR encoding: 78 1B 69616E612D69662D747970653A65746865726E657443736D616364
Leafs of type empty
MUST be encoded using the CBOR null value (major type 7, additional information 22).
The following example shows the encoding of an 'is-router' leaf representation node instance when present.
Definition example adapted from [
RFC 8344]:
leaf is-router {
type empty;
}
CBOR diagnostic notation: null
CBOR encoding: F6
Leafs of type union
MUST be encoded using the rules associated with one of the types listed. When used in a union, the following YANG datatypes are enclosed by a CBOR tag to avoid confusion between different YANG datatypes encoded using the same CBOR major type.
-
bits
-
enumeration
-
identityref
-
instance-identifier
See
Section 9.3 for the assigned value of these CBOR tags.
As mentioned in Sections [
6.6] and in [
6.7], 'enumeration' and 'bits' are encoded as a CBOR text string data item (major type 3) when defined within a 'union' type. (This adds considerable complexity but is necessary because of an idiosyncrasy of the YANG data model for unions; the work-around allows compatibility to be maintained with the encoding of overlapping unions in XML and JSON. See also
Section 9.12 of
RFC 7950.)
The following example shows the encoding of an 'ip-address' leaf representation node instance when set to "2001:db8:a0b:12f0::1".
Definition example adapted from [
RFC 6991]:
typedef ipv4-address {
type string {
pattern
'(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}'
+ '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
+ '(%[\p{N}\p{L}]+)?';
}
}
typedef ipv6-address {
type string {
pattern '((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}'
+ '((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|'
+ '(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}'
+ '(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))'
+ '(%[\p{N}\p{L}]+)?';
pattern '(([^:]+:){6}(([^:]+:[^:]+)|(.*\..*)))|'
+ '((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)'
+ '(%.+)?';
}
}
typedef ip-address {
type union {
type ipv4-address;
type ipv6-address;
}
}
leaf address {
type ip-address;
}
CBOR diagnostic notation: "2001:db8:a0b:12f0::1"
CBOR encoding: 74 323030313A6462383A6130623A313266303A3A31
This specification supports two approaches for encoding an instance-identifier: one based on YANG Schema Item iDentifier, as defined in
Section 3.2, and one based on names, as defined in
Section 3.3. See
Section 6.12 for an exceptional case when this representation needs to be tagged.
SIDs uniquely identify a schema node. In the case of a single instance schema node, i.e., a schema node defined at the root of a YANG module or submodule or schema nodes defined within a container, the SID is sufficient to identify this instance (representation node). (Note that no delta mechanism is employed for SIDs used for identityref, see
Section 6.10.1.)
In the case of a representation node that is an entry of a YANG list, a SID is combined with the list key(s) to identify each instance within the YANG list(s).
Instance-identifiers of single instance schema nodes
MUST be encoded using a CBOR unsigned integer data item (major type 0) and set to the targeted schema node SID.
Instance-identifiers of representation node entries of a YANG list
MUST be encoded using a CBOR array data item (major type 4) containing the following entries:
-
The first entry MUST be encoded as a CBOR unsigned integer data item (major type 0) and set to the targeted schema node SID.
-
The following entries MUST contain the value of each key required to identify the instance of the targeted schema node. These keys MUST be ordered as defined in the 'key' YANG statement, starting from the top-level list, and followed by each subordinate list(s).
Examples within this section assume the definition of a schema node of type 'instance-identifier':
Definition example adapted from [
RFC 7950]:
container system {
...
leaf reporting-entity {
type instance-identifier;
}
First example:
The following example shows the encoding of the 'reporting-entity' value referencing data node instance "/system/contact" (SID 1741).
Definition example adapted from [
RFC 7317]:
container system {
leaf contact {
type string;
}
leaf hostname {
type inet:domain-name;
}
}
CBOR diagnostic notation: 1741
CBOR encoding: 19 06CD
Second example:
This example aims to show how a representation node entry of a YANG list is identified. It uses a somewhat arbitrarily modified YANG module version from [
RFC 7317] by adding
country to the leafs and keys of
authorized-key.
The following example shows the encoding of the 'reporting-entity' value referencing list instance "/system/authentication/user/authorized-key/key-data" (which is assumed to have SID 1734) for username "bob" and authorized-key with name "admin" and country "france".
list user {
key name;
leaf name {
type string;
}
leaf password {
type ianach:crypt-hash;
}
list authorized-key {
key "name country";
leaf country {
type string;
}
leaf name {
type string;
}
leaf algorithm {
type string;
}
leaf key-data {
type binary;
}
}
}
CBOR diagnostic notation: [1734, "bob", "admin", "france"]
CBOR encoding:
84 # array(4)
19 06C6 # unsigned(1734)
63 # text(3)
626F62 # "bob"
65 # text(5)
61646D696E # "admin"
66 # text(6)
6672616E6365 # "france"
Third example:
The following example shows the encoding of the 'reporting-entity' value referencing the list instance "/system/authentication/user" (SID 1730), corresponding to username "jack".
CBOR diagnostic notation: [1730, "jack"]
CBOR encoding:
82 # array(2)
19 06C2 # unsigned(1730)
64 # text(4)
6A61636B # "jack"
An 'instance-identifier' value is encoded as a text string that is analogous to the lexical representation in XML encoding; see
Section 9.13.2 of
RFC 7950. However, the encoding of namespaces in instance-identifier values follows the rules stated in
Section 3.3, namely:
-
The leftmost (top-level) data node name is always in the namespace-qualified form.
-
Any subsequent data node name is in the namespace-qualified form if the node is defined in a module other than its parent node; otherwise, the simple form is used. This rule also holds for node names appearing in predicates.
For example,
/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ip
is a valid instance-identifier value because the data nodes "interfaces", "interface", and "name" are defined in the module "ietf-interfaces", whereas "ipv4" and "ip" are defined in "ietf-ip".
The resulting XML Path Language (XPath)
MUST be encoded using a CBOR text string data item (major type 3).
First example:
This example is described in
Section 6.13.1.
CBOR diagnostic notation: "/ietf-system:system/contact"
CBOR encoding:
78 1B 2F696574662D73797374656D3A73797374656D2F636F6E74616374
Second example:
This example is described in
Section 6.13.1.
CBOR diagnostic notation (the line break is inserted for exposition only):
"/ietf-system:system/authentication/user[name='bob']/
authorized-key[name='admin'][country='france']/key-data"
CBOR encoding:
78 6B
2F696574662D73797374656D3A73797374656D2F61757468656E74696361
74696F6E2F757365725B6E616D653D27626F62275D2F617574686F72697A
65642D6B65795B6E616D653D2761646D696E275D5B636F756E7472793D27
6672616E6365275D2F6B65792D64617461
Third example:
This example is described in
Section 6.13.1.
CBOR diagnostic notation:
"/ietf-system:system/authentication/user[name='jack']"
CBOR encoding:
78 34 # text(52)
2F696574662D73797374656D3A73797374656D2F61757468656E74696361
74696F6E2F757365725B6E616D653D276A61636B275D