#!/usr/bin/env bash # Advanced Client api test # Compile and run a client # Magic line must be first in script (see README.md) s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi # Which format to use as datastore format internally : ${format:=xml} APPNAME=example cfg=$dir/conf_yang.xml fyang=$dir/example-client.yang cfile=$dir/example-client.c pdir=$dir/plugin app=$dir/clixon-app debug=0 if [ ! -d $pdir ]; then mkdir $pdir fi # Define default restconfig config: RESTCONFIG RESTCONFIG=$(restconf_config none false) cat < $cfg $cfg clixon-restconf:allow-auth-none /usr/local/share/clixon $IETFRFC $fyang /usr/local/lib/$APPNAME/clispec /usr/local/lib/$APPNAME/cli $APPNAME /usr/local/var/$APPNAME/$APPNAME.sock $pdir /usr/local/var/$APPNAME/$APPNAME.pidfile $dir $format $RESTCONFIG EOF cat < $fyang module clixon-client { yang-version 1.1; namespace "urn:example:clixon-client"; prefix exc; description "Clixon client example yang"; revision 2021-01-14 { description "Added table/paramater/value as the primary data example"; } /* Generic config data */ container table{ list parameter{ key name; leaf name{ type string; } leaf value{ type uint32; } } } } EOF cat< $cfile #include #include #include #include // debug #include // debug #include int main(int argc, char **argv) { int retval = -1; clixon_handle h = NULL; /* clixon handle */ clixon_client_handle ch = NULL; /* clixon client handle */ clicon_log_init("client", LOG_DEBUG, CLICON_LOG_STDERR); // debug clicon_debug_init($debug, NULL); // debug /* Provide a clixon config-file, get a clixon handle */ if ((h = clixon_client_init("$cfg")) == NULL) return -1; /* Make a conenction over netconf or ssh/netconf */ if ((ch = clixon_client_connect(h, CLIXON_CLIENT_NETCONF)) == NULL) return -1; /* Here are read functions depending on an example YANG * (Need an example YANG and XML input to confd) */ { uint32_t u = 0; if (clixon_client_get_uint32(ch, &u, "urn:example:clixon-client", "/table/parameter[name='a']/value") < 0) goto done; printf("%u\n", u); /* for test output */ } retval = 0; done: clixon_client_disconnect(ch); clixon_client_terminate(h); printf("done\n"); /* for test output */ return retval; } EOF new "compile $cfile -> $app" expectpart "$($CC -g -Wall -I/usr/local/include $cfile -o $app -L /usr/local/lib -lclixon)" 0 "" new "test params: -s init -f $cfg" if [ $BE -ne 0 ]; then new "kill old backend" sudo clixon_backend -zf $cfg if [ $? -ne 0 ]; then err fi new "start backend" start_backend -s init -f $cfg fi new "waiting" wait_backend if [ $RC -ne 0 ]; then new "kill old restconf daemon" stop_restconf_pre new "start restconf daemon" start_restconf -f $cfg fi new "wait restconf" wait_restconf XML='a42
' # Add a set of entries using restconf new "POST the XML" expectpart "$(curl $CURLOPTS -X POST -H 'Content-Type: application/yang-data+xml' $RCPROTO://localhost/restconf/data -d "$XML")" 0 "HTTP/$HVER 201" new "Check entries" expectpart "$(curl $CURLOPTS -X GET $RCPROTO://localhost/restconf/data/clixon-client:table -H 'Accept: application/yang-data+xml')" 0 "HTTP/$HVER 200" "$XML" new "Run $app" expectpart "$($app)" 0 '^42$' if [ $RC -ne 0 ]; then new "Kill restconf daemon" stop_restconf fi if [ $BE -ne 0 ]; then new "Kill backend" # Check if premature kill pid=`pgrep -u root -f clixon_backend` if [ -z "$pid" ]; then err "backend already dead" fi # kill backend stop_backend -f $cfg fi # unset conditional parameters unset format # Set by restconf_config unset RESTCONFIG rm -rf $dir new "endtest" endtest