A bits built-in type represents a bit set.  A bits value is a set of flags identified by small integer position numbers starting at 0.  Each bit number has an assigned name.

A bits type can be restricted with the "bit" statement. It is repeatedly used to specify each assigned named bit of a bits type. "bit" takes a string argument that is the assigned name of the bit. The "position" statement, which is optional, takes as an argument a non-negative integer value that specifies the bit's position within a hypothetical bit field.


Consider the following YANG module example:


module example {
  namespace "http://yumaworks.com/ns/example";
  prefix "example";

  revision 2019-07-30 {
    description
      "Initial example module for bits type";
  }
  leaf yang-bits-example {
    type bits {
      bit zero { position 0; }
      bit one { position 1; }
      bit two { position 2; }
      bit ten { position 10; }
      bit last;
    }
    default "two";
  }
}


This leaf can be accessed using several ways described below.


1) Using RESTCONF request:


To create a new "yang-bits-example" node using RESTCONF client, the client might send the following POST request:


      POST /restconf/data/  HTTP/1.1
      Host: example-server
      Content-Type: application/yang-data+json

      {
        "yang-bit-example":"one two"
      }


If the resource is created, the server might respond as follows:


      HTTP/1.1 201 Created
      Server: example-server
      Location: https://example-server/restconf/data/yang-bits-example


If the POST method succeeds, a "201 Created" Status is returned and there is no response message-body.  A "Location" header field identifying the resource node that was created. If the data resource already exists, then the POST request will fail and a "409 Conflict" status-line will be returned.


To retrieve just created node, the GET method can be sent by the client as follows:


      GET /restconf/data/yang-bits-type HTTP/1.1
      Host: example-server
      Accept: application/yang-data+xml


The server might respond as follows:


      HTTP/1.1 200 OK
      Server: example-server
      Content-Type: application/yang-data+xml

      <yang-bits-example xmlns="http://yumaworks.com/ns/example">one two</yang-bits-example>

2) Using yangcli-pro:


To create a new "yang-bits-example" node using yangcli-pro client, the client might send the following POST request:


user@server> create /yang-bits-example value='one two'
user@server> commit


To retrieve just created bits leaf, the following request might be sent:


user@server> sget /yang-bits-example


The server might respond with rpc-reply, containing below information:


<rpc-reply>
 <data>
   <yang-bits-example xmlns="http://yumaworks.com/ns/example">one two</yang-bits-example>
 </data>
</rpc-reply>


The following example illustrates how the bits leaf can be set by using inline JSON file:


edit-config target=candidate config="""
{"config": {
    "yang-bits-example" : "one two"
 }
}
"""


The following example illustrates how the bits leaf can be set by using inline XML file:


edit-config target=candidate config="""
<config>
  <yang-bits-example>one two</yang-bits-example>
</config>
"""


To retrieve just created bits leaf, the following request might be sent:


user@server> sget /yang-bits-example


The server might respond with rpc-reply, containing below information:


<rpc-reply>
 <data>
   <yang-bits-example xmlns="http://yumaworks.com/ns/example">one two</yang-bits-example>
 </data>
</rpc-reply>


Yangcli-pro will output this message as follows.


rpc-reply {
  data {
      yang-bits-example "one two"
  }
}

3) Using DB-API:


The following example illustrates how to set bits leaf value using DB-API.


/********************************************************************
* FUNCTION send_test_edit
*
* This is an example send edit function for bits node.
**********************************************************************/
static void
    send_test_edit (void)
{
    const xmlChar *path_str = (const xmlChar *)"/";
    const xmlChar *operation_str = (const xmlChar *)"merge";
    const xmlChar *value_str = (const xmlChar *)
    "<config>"
    "<yang-bits-example xmlns='http://yumaworks.com/ns/example'>one two</yang-bits-example>"
    "</config>";

    const xmlChar *patch_id_str = NULL;
    boolean system_edit = FALSE;
    status_t res = db_api_send_edit_ex(path_str,
                                       operation_str,
                                       value_str,
                                       patch_id_str,
                                       system_edit);
    if (res != NO_ERR) {
        log_error("\nSend test edit for bits node failed %s %s = %s (%s)\n",
                  operation_str, path_str, value_str,
                  get_error_string(res));
    } else if (LOGDEBUG) {
        log_debug("\nSend test edit for bits node OK  %s %s = %s\n",
                  operation_str, path_str, value_str);
    }

}  /* send_test_edit */

BITS leaf and SIL code:


The following API can be used with the bits type val value node. Refer to val.h:

  • val_make_simval_obj()Function that malloc a new val value node and does not need to know the type, it will be picked automatically based on the provided object type.


Following code example creates the value of bits node and outputs it to the standard log output using val_make_simval_obj().

This is just an example of how the following API can be used:


...
    /* Create a new bits leaf type node */
    val_value_t *return_val =
        val_make_simval_obj(obj, (const xmlChar *)"one two", &res);
    if (return_val) {
        log_info("\nCreated node; type:%s",
                 VAL_TYPDEF(return_val)->typenamestr);
    }
...


The server might output the following logging information to the standard output:


...
Created node; type:'bits'
...