[filter in netconf - one specific entry #100](https://github.com/clicon/clixon/issues/100)
This commit is contained in:
parent
46552066d1
commit
ba2f40728b
6 changed files with 134 additions and 43 deletions
|
|
@ -23,6 +23,7 @@
|
|||
* Main example yang changed to incorporate augmented state, new revision is 2019-11-15.
|
||||
|
||||
### Corrected Bugs
|
||||
* [filter in netconf - one specific entry #100](https://github.com/clicon/clixon/issues/100)
|
||||
* [xpath_tree2cbuf() changes integers into floating point representations #99](https://github.com/clicon/clixon/issues/99)
|
||||
* [xml_parse_string() is slow for a long XML string #96](https://github.com/clicon/clixon/issues/96)
|
||||
* Mandatory variables can no longer be deleted.
|
||||
|
|
|
|||
|
|
@ -121,7 +121,6 @@ xml_filter_recursive(cxobj *xfilter,
|
|||
int remove_s;
|
||||
|
||||
*remove_me = 0;
|
||||
assert(xfilter && xparent && strcmp(xml_name(xfilter), xml_name(xparent))==0);
|
||||
/* 1. Check selection */
|
||||
if (xml_child_nr(xfilter) == 0)
|
||||
goto match;
|
||||
|
|
|
|||
|
|
@ -80,6 +80,31 @@
|
|||
</rpc>
|
||||
*/
|
||||
|
||||
static int
|
||||
netconf_get_config_subtree(clicon_handle h,
|
||||
cxobj *xfilter,
|
||||
cxobj **xret)
|
||||
{
|
||||
int retval = -1;
|
||||
cxobj *xdata;
|
||||
|
||||
/* a subtree filter is comprised of zero or more element subtrees*/
|
||||
if ((xdata = xpath_first(*xret, "/rpc-reply/data")) == NULL)
|
||||
goto ok;
|
||||
if (xml_filter(xfilter, xdata) < 0){
|
||||
xml_parse_va(xret, NULL, "<rpc-reply><rpc-error>"
|
||||
"<error-tag>operation-failed</error-tag>"
|
||||
"<error-type>applicatio</error-type>"
|
||||
"<error-severity>error</error-severity>"
|
||||
"<error-info>filtering</error-info>"
|
||||
"</rpc-error></rpc-reply>");
|
||||
}
|
||||
ok:
|
||||
retval = 0;
|
||||
// done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Get configuration
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] xn Sub-tree (under xorig) at <rpc>...</rpc> level.
|
||||
|
|
@ -136,37 +161,22 @@ netconf_get_config(clicon_handle h,
|
|||
cxobj *xfilter; /* filter */
|
||||
int retval = -1;
|
||||
char *ftype = NULL;
|
||||
cxobj *xfilterconf;
|
||||
cxobj *xconf;
|
||||
|
||||
/* ie <filter>...</filter> */
|
||||
if ((xfilter = xpath_first(xn, "filter")) != NULL)
|
||||
ftype = xml_find_value(xfilter, "type");
|
||||
if (ftype == NULL || strcmp(ftype, "xpath")==0){
|
||||
if (xfilter == NULL || ftype == NULL || strcmp(ftype, "xpath")==0){
|
||||
if (clicon_rpc_netconf_xml(h, xml_parent(xn), xret, NULL) < 0)
|
||||
goto done;
|
||||
}
|
||||
else if (strcmp(ftype, "subtree")==0){
|
||||
/* Default rfc filter is subtree. I prefer xpath and use it internally.
|
||||
Get whole subtree and then filter aftwerwards. This is suboptimal.
|
||||
Therefore please use xpath.
|
||||
/* Get whole config first, then filter. This is suboptimal
|
||||
*/
|
||||
if (clicon_rpc_netconf_xml(h, xml_parent(xn), xret, NULL) < 0)
|
||||
goto done;
|
||||
if (xfilter &&
|
||||
(xfilterconf = xpath_first(xfilter, "//configuration"))!= NULL &&
|
||||
(xconf = xpath_first(*xret, "/rpc-reply/data")) != NULL){
|
||||
/* xml_filter removes parts of xml tree not matching */
|
||||
if ((strcmp(xml_name(xfilterconf), xml_name(xconf))!=0) ||
|
||||
xml_filter(xfilterconf, xconf) < 0){
|
||||
xml_parse_va(xret, NULL, "<rpc-reply><rpc-error>"
|
||||
"<error-tag>operation-failed</error-tag>"
|
||||
"<error-type>applicatio</error-type>"
|
||||
"<error-severity>error</error-severity>"
|
||||
"<error-info>filtering</error-info>"
|
||||
"</rpc-error></rpc-reply>");
|
||||
}
|
||||
}
|
||||
/* Now filter on whole tree */
|
||||
if (netconf_get_config_subtree(h, xfilter, xret) < 0)
|
||||
goto done;
|
||||
}
|
||||
else{
|
||||
xml_parse_va(xret, NULL, "<rpc-reply><rpc-error>"
|
||||
|
|
@ -348,37 +358,22 @@ netconf_get(clicon_handle h,
|
|||
cxobj *xfilter; /* filter */
|
||||
int retval = -1;
|
||||
char *ftype = NULL;
|
||||
cxobj *xfilterconf;
|
||||
cxobj *xconf;
|
||||
|
||||
/* ie <filter>...</filter> */
|
||||
if ((xfilter = xpath_first(xn, "filter")) != NULL)
|
||||
ftype = xml_find_value(xfilter, "type");
|
||||
if (ftype == NULL || strcmp(ftype, "xpath")==0){
|
||||
if (xfilter == NULL || ftype == NULL || strcmp(ftype, "xpath")==0){
|
||||
if (clicon_rpc_netconf_xml(h, xml_parent(xn), xret, NULL) < 0)
|
||||
goto done;
|
||||
}
|
||||
else if (strcmp(ftype, "subtree")==0){
|
||||
/* Default rfc filter is subtree. I prefer xpath and use it internally.
|
||||
Get whole subtree and then filter aftwerwards. This is suboptimal.
|
||||
Therefore please use xpath.
|
||||
/* Get whole config + state first, then filter. This is suboptimal
|
||||
*/
|
||||
if (clicon_rpc_netconf_xml(h, xml_parent(xn), xret, NULL) < 0)
|
||||
goto done;
|
||||
if (xfilter &&
|
||||
(xfilterconf = xpath_first(xfilter, "//configuration"))!= NULL &&
|
||||
(xconf = xpath_first(*xret, "/rpc-reply/data")) != NULL){
|
||||
/* xml_filter removes parts of xml tree not matching */
|
||||
if ((strcmp(xml_name(xfilterconf), xml_name(xconf))!=0) ||
|
||||
xml_filter(xfilterconf, xconf) < 0){
|
||||
xml_parse_va(xret, NULL, "<rpc-reply><rpc-error>"
|
||||
"<error-tag>operation-failed</error-tag>"
|
||||
"<error-type>applicatio</error-type>"
|
||||
"<error-severity>error</error-severity>"
|
||||
"<error-info>filtering</error-info>"
|
||||
"</rpc-error></rpc-reply>");
|
||||
}
|
||||
}
|
||||
/* Now filter on whole tree */
|
||||
if (netconf_get_config_subtree(h, xfilter, xret) < 0)
|
||||
goto done;
|
||||
}
|
||||
else{
|
||||
xml_parse_va(xret, NULL, "<rpc-reply><rpc-error>"
|
||||
|
|
|
|||
|
|
@ -400,7 +400,7 @@ xml2ns(cxobj *x,
|
|||
/*! Add a namespace attribute to an XML node, either default or specific prefix
|
||||
* @param[in] x XML tree
|
||||
* @param[in] prefix prefix/ns localname. If NULL then set default xmlns
|
||||
* @param[out] namespace URI namespace (or NULL). Will be copied
|
||||
* @param[in] ns URI namespace (or NULL). Will be copied
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
* @see xml2ns
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></sou
|
|||
new "netconf commit"
|
||||
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><commit/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
|
||||
|
||||
new "netconf edit config merge"
|
||||
new "netconf edit config merge eth2"
|
||||
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"><interface><name>eth2</name><type>ex:eth</type></interface></interfaces></config><default-operation>merge</default-operation></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
|
||||
|
||||
# Note, the type here is non-existant identityref, fails on validation
|
||||
|
|
|
|||
96
test/test_netconf_filter.sh
Executable file
96
test/test_netconf_filter.sh
Executable file
|
|
@ -0,0 +1,96 @@
|
|||
#!/usr/bin/env bash
|
||||
# Test netconf filter, subtree and xpath
|
||||
# Note subtree namespaces not implemented
|
||||
|
||||
# Magic line must be first in script (see README.md)
|
||||
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
|
||||
|
||||
APPNAME=example
|
||||
|
||||
cfg=$dir/conf_yang.xml
|
||||
fyang=$dir/filter.yang
|
||||
|
||||
cat <<EOF > $cfg
|
||||
<clixon-config xmlns="http://clicon.org/config">
|
||||
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
|
||||
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
|
||||
<CLICON_YANG_MAIN_FILE>$fyang</CLICON_YANG_MAIN_FILE>
|
||||
<CLICON_CLISPEC_DIR>/usr/local/lib/$APPNAME/clispec</CLICON_CLISPEC_DIR>
|
||||
<CLICON_CLI_DIR>/usr/local/lib/$APPNAME/cli</CLICON_CLI_DIR>
|
||||
<CLICON_CLI_MODE>$APPNAME</CLICON_CLI_MODE>
|
||||
<CLICON_SOCK>/usr/local/var/$APPNAME/$APPNAME.sock</CLICON_SOCK>
|
||||
<CLICON_BACKEND_DIR>/usr/local/lib/$APPNAME/backend</CLICON_BACKEND_DIR>
|
||||
<CLICON_BACKEND_PIDFILE>/usr/local/var/$APPNAME/$APPNAME.pidfile</CLICON_BACKEND_PIDFILE>
|
||||
<CLICON_XMLDB_DIR>/usr/local/var/$APPNAME</CLICON_XMLDB_DIR>
|
||||
</clixon-config>
|
||||
EOF
|
||||
|
||||
cat <<EOF > $fyang
|
||||
module filter{
|
||||
yang-version 1.1;
|
||||
namespace "urn:example:filter";
|
||||
prefix fi;
|
||||
container x{
|
||||
list y {
|
||||
key a;
|
||||
leaf a{
|
||||
type string;
|
||||
}
|
||||
leaf b{
|
||||
type string;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
EOF
|
||||
|
||||
new "test params: -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 -s init -f $cfg"
|
||||
start_backend -s init -f $cfg
|
||||
fi
|
||||
|
||||
new "waiting"
|
||||
wait_backend
|
||||
|
||||
new "Add two entries"
|
||||
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><x xmlns="urn:example:filter"><y><a>1</a><b>1</b></y><y><a>2</a><b>2</b></y></x></config></edit-config></rpc>]]>]]>' '^<rpc-reply><ok/></rpc-reply>]]>]]>$'
|
||||
|
||||
new "netconf commit"
|
||||
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><commit/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
|
||||
|
||||
new "wrong filter type"
|
||||
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><get><filter type='foo'><x xmlns='urn:example:filter'><y><a>1</a></y></x></filter></get></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-tag>operation-failed</error-tag><error-type>applicatio</error-type><error-severity>error</error-severity><error-message>filter type not supported</error-message><error-info>type</error-info></rpc-error></rpc-reply>]]>]]>$'
|
||||
|
||||
new "get-config subtree one"
|
||||
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><get-config><source><running/></source><filter type='subtree'><x xmlns='urn:example:filter'><y><a>1</a></y></x></filter></get-config></rpc>]]>]]>" '^<rpc-reply><data><x xmlns="urn:example:filter"><y><a>1</a><b>1</b></y></x></data></rpc-reply>]]>]]>$'
|
||||
|
||||
new "get subtree one"
|
||||
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><get><filter type='subtree'><x xmlns='urn:example:filter'><y><a>1</a></y></x></filter></get></rpc>]]>]]>" '^<rpc-reply><data><x xmlns="urn:example:filter"><y><a>1</a><b>1</b></y></x></data></rpc-reply>]]>]]>$'
|
||||
|
||||
new "get-config xpath one"
|
||||
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><get><filter type='xpath' select=\"/fi:x/fi:y[fi:a='1']\" xmlns:fi='urn:example:filter' /></get></rpc>]]>]]>" '^<rpc-reply><data><x xmlns="urn:example:filter"><y><a>1</a><b>1</b></y></x></data></rpc-reply>]]>]]>$'
|
||||
|
||||
new "get xpath one"
|
||||
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><get><filter type='xpath' select=\"/fi:x/fi:y[fi:a='1']\" xmlns:fi='urn:example:filter' /></get></rpc>]]>]]>" '^<rpc-reply><data><x xmlns="urn:example:filter"><y><a>1</a><b>1</b></y></x></data></rpc-reply>]]>]]>$'
|
||||
|
||||
if [ $BE -eq 0 ]; then
|
||||
exit # BE
|
||||
fi
|
||||
|
||||
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
|
||||
|
||||
rm -rf $dir
|
||||
Loading…
Add table
Add a link
Reference in a new issue