The following article illustrates how to utilize the GET2 callback in examples. Let us go through simple examples that will illustrate how to utilize the GET2 callbacks for container YANG node types.

Consider this simplified example, that represents GET2 callback for the “interface-state” container.


  ...

  container interfaces-state {
     config false;

     leaf in-errors { type uint32; }
  ...
  }

  ...



NOTE:

The GET2 callbacks for “container” elements DO NOT required to fill in any children elements. All additional leafs are optional to fill in. Any other descendant complex elements, such as lists, other containers, and choice elements must have their own callbacks functions.


The following C API code exemplifies a simple GET2 callback function for the data model, which returns the “interfaces-state” container element with an additional leaf element when it is requested.


/********************************************************************
* FUNCTION  get2_container_interfaces
*
* Path: /interfaces-state
*
* INPUTS: 
*   scb == session control block making the request 
*   msg == incoming XML message header 
*   get2cb == get2 control block for this callback request
* 
* OUTPUTS: 
*   return_valQ is filled with malloced val_value_t nodes
* 
* RETURNS: 
*    status: 
*      NO_ERR if executed OK and found OK 
*      ERR_NCX_NO_INSTANCE warning if no instance found
*
********************************************************************/
static status_t 
    get2_container_interfaces (ses_cb_t *scb, 
                               xml_msg_hdr_t *msg, 
                               getcb_get2_t *get2cb) 
{ 
    (void)scb; 
    (void)msg; 

    /* check the callback mode type */ 
    getcb_mode_t cbmode = GETCB_GET2_CBMODE(get2cb); 
    switch (cbmode) { 
    case GETCB_GET_VALUE: 
        break; 
    case GETCB_GETNEXT_VALUE: 
        return ERR_NCX_NO_INSTANCE; 
    default: 
        return SET_ERROR(ERR_INTERNAL_VAL); 
    } 

    /* get the template for the “interfaces-state” container */ 
    obj_template_t *obj = GETCB_GET2_OBJ(get2cb); 
    status_t res = NO_ERR; 

    /* pretend leaf 'in-errors' is present */ 
    val_value_t *retval =
    agt_make_leaf2(obj,
          EXAMPLE_MODNAME, 
                            (const xmlChar *)"in-errors", 
                            (const xmlChar *)"8", 
                            &res); 
    if (retval) { 
        getcb_add_return_val(get2cb, retval); 
    } else { 
        return res; 
    } 

    /* choice and other complex nodes will be checked in a separate call */ 

    return res; 

}  /* get2_container_interfaces */ 



The agt_make_leaf2 API returns malloced value structure or NULL if some error. Alternatively, if you know the object template that defines the leaf node that you want to generate, the following more generic API can be used:



    ... 

    obj_template_t *leafobj =
    obj_find_child(obj,
          EXAMPLE_MODNAM,
          (const xmlChar *)"in-errors");
    if (!leafobj) {
    /* report an error or do not try to generate the leaf, just skip it */
    return ERR_NCX_DEF_NOT_FOUND;
    }

    /* pretend leaf 'in-errors' is present */ 
    val_value_t *retval = 
         val_make_simval_obj(leafobj, 
                             (const xmlChar *)"8", 
                              &res);
    ...



In the scenario above, make sure that the leaf object template was found. If it was not found, you may report an error immediately or ignore that node and skip the node generation process. However, do not generate the leaf node, if the object template was not found. 


NOTE:

In the SIL, when you fill the data in get2cb with help of val_make_simval_obj() or analogous APIs, You don’t need to free the buffer because the server does clean up get2cb.


To ensure that the GET2 callbacks are working as expected an application may retrieve running configuration and device state information as follows:



 <?xml version="1.0" encoding="UTF-8"?> 
  <rpc message-id="3" 
     xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> 
    <get> 
      <filter type="subtree"> 
        <interfaces-state xmlns="http://yumaworks.com/ns/ietf-interfaces-example" /> 

      </filter> 
    </get> 
  </rpc> 


The server may reply with:


     <rpc-reply message-id="101"
                xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
        <interfaces-state xmlns="http://yumaworks.com/ns/ietf-interfaces-example"> 
           <in-errors>8</in-errors> 

        </interfaces-state> 
     </rpc-reply>