The following steps are required to run your own module in the netconfd-pro server.

  1. Make the YANG module available to the SDK
  2. Generate the SIL code stubs for the module
  3. Add code to implement the YANG object
  4. Build the SIL code and add it to the server
  5. Run the server with your module and SIL code



addrpc Example


The addrpc module contains a single RPC operation called "add".

It takes two numbers as input and returns the sum of these numbers.



1) Make the YANG module available to the SDK


The following module "addrpc.yang" is used in this example:



module addrpc {
  namespace "http://www.yumaworks.com/ns/addrpc";
  prefix add;
  revision "2020-02-25";

  rpc add {
    description "Get the sum of two numbers";
    input {
      leaf num1 {
        type int32;
        mandatory true;
        description "First number to add";
      }
      leaf num2 {
        type int32;
        mandatory true;
        description "Second number to add";
      }
    }
    output {
      leaf sum {
        type int32;
        mandatory true;
        description "The sum of the 2 numbers";
      }
    }
   }
}



1a) Create the "modules" directory in your home directory and 

> cd $HOME
> mkdir modules


1b) Save the module text to addrpc.yang then add it to the $HOME/modules directory

> cp addrpc.yang modules/



2) Generate the SIL code stubs for the module


Example make_sil_dir_pro command:

> make_sil_dir_pro addrpc

*** /home/andy/modules/addrpc.yang
*** 0 Errors, 0 Warnings

*** /home/andy/modules/addrpc.yang
*** 0 Errors, 0 Warnings

>


This script will generate 3 directories (bin and /lib are for output only).

The src directory will contain all the generated code stubs.

In this example "addrpc.c" and "addrpc.h" are generated.

Only the C file "addrpc.c" needs to be modified to implement the "add" operation.


Example code layout after make_sil_dir_pro:


> ls -lR
.:
total 16
drwxr-xr-x 2 andy andy 4096 Mar  3 09:18 bin
drwxr-xr-x 2 andy andy 4096 Mar  3 09:18 lib
-rw-r--r-- 1 andy andy 1335 Mar  3 09:18 Makefile
drwxr-xr-x 2 andy andy 4096 Mar  3 09:18 src

./bin:
total 0

./lib:
total 0

./src:
total 20
-rw-r--r-- 1 andy andy 7255 Mar  3 09:18 addrpc.c
-rw-r--r-- 1 andy andy 2754 Mar  3 09:18 addrpc.h
-rw-r--r-- 1 andy andy 7011 Mar  3 09:18 Makefile

> 


The following functions are generated in the C file addrpc/src/addrpc.c:

  • y_addrpc_init_static_vars : Do not need to edit this function unless static vars are added
  • y_addrpc_add_validate: RPC validate function for <add> operation: Must add code to properly implement!
  • y_addrpc_add_invoke: RPC invoke function for <add> operation : Must add code to properly implement!
  • y_addrpc_init: Phase 1 initialization to load the module and register SIL callbacks.  No code changes needed
  • y_addrpc_init2: Phase 2 initialization not used in this example.  No code changes needed
  • y_addrpc_cleanup: SIL cleanup to unregister callbacks.  No code changes needed


Example C file addrpc/src/addrpc.c before any modification is shown in attachment addrpc.c.stub.


3) Add code to implement the YANG object


3a) Update the RPC validation function y_addrpc_add_validate


The validation phase is used to validate the input parameters. In this case the 2 parameters "num1" and "num2" are checked for overflow. 


The variable initialization code is changed to make sure the variables are initialized correctly. This is done in the y_addrpc_add_validate and y_addrpc_add_invoke functions.


    val_value_t *v_num1_val = NULL;
    int32 v_num1 = 0;

    val_value_t *v_num2_val = NULL;
    int32 v_num2 = 0;



The following code is added before the final "if" statement to check for errors:


    /* check for overflow casting as uint32
     * cannot rely on wrap as signed number
     */
    int32 test = v_num1 + v_num2;
    if ((v_num1 > 0) && (v_num2 > 0) && (test < 0)) {
        res = ERR_NCX_OPERATION_FAILED;
    } else if ((v_num1 < 0) && (v_num2 < 0) && (test > 0)) {
        res = ERR_NCX_OPERATION_FAILED;
    }



3b) Update the RPC invoke function y_addrpc_add_invoke


The invoke phase is used to execute the RPC operation. In this case, the sum is computed and returned as output data to the client.


The following code is added to the y_addrpc_add_invoke function to create a leaf named "sum" and return it to the server.


    int32 result = v_num1 + v_num2;
    obj_template_t *obj = RPC_MSG_METHOD(msg);
    if (obj) {
        obj = obj_find_child(obj, NULL, NCX_EL_OUTPUT);
    }
    if (obj == NULL) {
        return ERR_NCX_DEF_NOT_FOUND;
    }

    val_value_t *val =
        agt_make_int_leaf(obj,
                          y_addrpc_N_sum,
                          result,
                          &res);
    if (val) {
        agt_rpc_add_return_val(val, msg);
    }


The complete C file after these edits are done can be found in the attachment addrpc.c




4) Build the SIL code and add it to the server


Start from the addrpc directory. The SDK H files are installed in /usr/include/yumapro.


4a) make


> make 
for dir in src; do\
  cd $dir && make && cd ..;\
        done
make[1]: Entering directory '/home/andy/work/addrpc/src'
cc -O2 -DDEBUG=1 -DLINUX=1 -DGCC=1 -DHAS_FLOAT=1 -Wall -Wno-long-long -Wformat-y2k -Winit-self -Wswitch-default -Wextra -Wundef -Wshadow -Wpointer-arith -Wwrite-strings -Wbad-function-cast -Wcast-qual -Waggregate-return -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Winvalid-pch -Wredundant-decls -Wnested-externs -Winline -std=gnu99 -Werror=incompatible-pointer-types -fPIC   \
        -I. -I/usr/include/yumapro/platform -I/usr/include/yumapro/ncx -I/usr/include/yumapro/agt -I/usr/include -I/usr/include/libxml2 -I/usr/include/libxml2/libxml   -c -o ../bin/addrpc.o addrpc.c
cc -O2 -DDEBUG=1 -DLINUX=1 -DGCC=1 -DHAS_FLOAT=1 -Wall -Wno-long-long -Wformat-y2k -Winit-self -Wswitch-default -Wextra -Wundef -Wshadow -Wpointer-arith -Wwrite-strings -Wbad-function-cast -Wcast-qual -Waggregate-return -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Winvalid-pch -Wredundant-decls -Wnested-externs -Winline -std=gnu99 -Werror=incompatible-pointer-types -fPIC   -shared -rdynamic -Wl,-soname,libaddrpc.so -o ../lib/libaddrpc.so ../bin/addrpc.o
make[1]: Leaving directory '/home/andy/work/addrpc/src'
>



4b) sudo make install


> sudo make install
for dir in src; do\
          cd $dir && make install && cd ..;\
        done
make[1]: Entering directory '/home/andy/work/addrpc/src'
mkdir -p /usr/lib/yumapro
install ../lib/libaddrpc.so /usr/lib/yumapro
make[1]: Leaving directory '/home/andy/work/addrpc/src'
> 



5) Run the server with your module and SIL code


The "module" parameter is used to load the module and SIL library into the server


> netconfd-pro --module=addrpc


Example yangcli-pro command:


andy@localhost> add num1=42 num2=1900

RPC Data Reply 2 for session 3 [default]: 

rpc-reply {
  sum 1942
}

andy@localhost>