Restricted yang (sub)module file match to match RFC6020 exactly

This commit is contained in:
Olof hagsand 2017-06-25 17:48:21 +02:00
parent 148d5d9432
commit 43c39160a5
10 changed files with 86 additions and 55 deletions

View file

@ -1,6 +1,8 @@
# Clixon CHANGELOG # Clixon CHANGELOG
- Genearlized yang type resolution. - Restricted yang (sub)module file match to match RFC6020 exactly
- Generalized yang type resolution to all included (sub)modules not just the topmost
- Generic map_str2int generic mapping tables - Generic map_str2int generic mapping tables

View file

@ -644,6 +644,7 @@ compare_dbs(clicon_handle h,
{ {
cxobj *xc1 = NULL; /* running xml */ cxobj *xc1 = NULL; /* running xml */
cxobj *xc2 = NULL; /* candidate xml */ cxobj *xc2 = NULL; /* candidate xml */
cxobj *xerr;
int retval = -1; int retval = -1;
int astext; int astext;
@ -655,10 +656,18 @@ compare_dbs(clicon_handle h,
astext = cv_int32_get(cvec_i(argv, 0)); astext = cv_int32_get(cvec_i(argv, 0));
else else
astext = 0; astext = 0;
if (clicon_rpc_get_config(h, "running", "/", 0, &xc1) < 0) if (clicon_rpc_get_config(h, "running", "/", &xc1) < 0)
goto done; goto done;
if (clicon_rpc_get_config(h, "candidate", "/", 0, &xc2) < 0) if ((xerr = xpath_first(xc1, "/rpc-error")) != NULL){
clicon_rpc_generate_error(xerr);
goto done; goto done;
}
if (clicon_rpc_get_config(h, "candidate", "/", &xc2) < 0)
goto done;
if ((xerr = xpath_first(xc2, "/rpc-error")) != NULL){
clicon_rpc_generate_error(xerr);
goto done;
}
if (compare_xmls(xc1, xc2, astext) < 0) /* astext? */ if (compare_xmls(xc1, xc2, astext) < 0) /* astext? */
goto done; goto done;
retval = 0; retval = 0;
@ -800,6 +809,7 @@ save_config_file(clicon_handle h,
char *dbstr; char *dbstr;
char *varstr; char *varstr;
cxobj *xt = NULL; cxobj *xt = NULL;
cxobj *xerr;
FILE *f = NULL; FILE *f = NULL;
if (cvec_len(argv) != 2){ if (cvec_len(argv) != 2){
@ -823,8 +833,12 @@ save_config_file(clicon_handle h,
goto done; goto done;
} }
filename = cv_string_get(cv); filename = cv_string_get(cv);
if (clicon_rpc_get_config(h, dbstr,"/", 0, &xt) < 0) if (clicon_rpc_get_config(h, dbstr,"/", &xt) < 0)
goto done; goto done;
if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){
clicon_rpc_generate_error(xerr);
goto done;
}
if ((f = fopen(filename, "wb")) == NULL){ if ((f = fopen(filename, "wb")) == NULL){
clicon_err(OE_CFG, errno, "Creating file %s", filename); clicon_err(OE_CFG, errno, "Creating file %s", filename);
goto done; goto done;
@ -1122,6 +1136,7 @@ cli_copy_config(clicon_handle h,
char *tovar; char *tovar;
cg_var *tocv; cg_var *tocv;
char *toname; char *toname;
cxobj *xerr;
if (cvec_len(argv) != 5){ if (cvec_len(argv) != 5){
clicon_err(OE_PLUGIN, 0, "%s: Requires four elements: <db> <xpath> <keyname> <from> <to>", __FUNCTION__); clicon_err(OE_PLUGIN, 0, "%s: Requires four elements: <db> <xpath> <keyname> <from> <to>", __FUNCTION__);
@ -1162,8 +1177,12 @@ cli_copy_config(clicon_handle h,
cprintf(cb, xpath, keyname, fromname); cprintf(cb, xpath, keyname, fromname);
/* Get from object configuration and store in x1 */ /* Get from object configuration and store in x1 */
if (clicon_rpc_get_config(h, db, cbuf_get(cb), 0, &x1) < 0) if (clicon_rpc_get_config(h, db, cbuf_get(cb), &x1) < 0)
goto done; goto done;
if ((xerr = xpath_first(x1, "/rpc-error")) != NULL){
clicon_rpc_generate_error(xerr);
goto done;
}
/* Get to variable -> cv -> to name */ /* Get to variable -> cv -> to name */
if ((tocv = cvec_find_var(cvv, tovar)) == NULL){ if ((tocv = cvec_find_var(cvv, tovar)) == NULL){

View file

@ -104,6 +104,7 @@ expand_dbvar(void *h,
cxobj *xt = NULL; cxobj *xt = NULL;
char *xpath = NULL; char *xpath = NULL;
cxobj **xvec = NULL; cxobj **xvec = NULL;
cxobj *xerr;
size_t xlen = 0; size_t xlen = 0;
cxobj *x; cxobj *x;
char *bodystr; char *bodystr;
@ -140,8 +141,12 @@ expand_dbvar(void *h,
if (api_path_fmt2xpath(api_path, cvv, &xpath) < 0) if (api_path_fmt2xpath(api_path, cvv, &xpath) < 0)
goto done; goto done;
/* XXX read whole configuration, why not send xpath? */ /* XXX read whole configuration, why not send xpath? */
if (clicon_rpc_get_config(h, dbstr, "/", 0, &xt) < 0) if (clicon_rpc_get_config(h, dbstr, "/", &xt) < 0)
goto done; goto done;
if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){
clicon_rpc_generate_error(xerr);
goto done;
}
/* One round to detect duplicates /* One round to detect duplicates
* XXX The code below would benefit from some cleanup * XXX The code below would benefit from some cleanup
*/ */
@ -379,6 +384,7 @@ cli_show_config(clicon_handle h,
char *val = NULL; char *val = NULL;
cxobj *xt = NULL; cxobj *xt = NULL;
cxobj *xc; cxobj *xc;
cxobj *xerr;
enum genmodel_type gt; enum genmodel_type gt;
if (cvec_len(argv) != 3 && cvec_len(argv) != 4){ if (cvec_len(argv) != 3 && cvec_len(argv) != 4){
@ -426,8 +432,12 @@ cli_show_config(clicon_handle h,
else else
cprintf(cbxpath, "%s", xpath); cprintf(cbxpath, "%s", xpath);
/* Get configuration from database */ /* Get configuration from database */
if (clicon_rpc_get_config(h, db, cbuf_get(cbxpath), 0, &xt) < 0) if (clicon_rpc_get_config(h, db, cbuf_get(cbxpath), &xt) < 0)
goto done; goto done;
if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){
clicon_rpc_generate_error(xerr);
goto done;
}
/* Print configuration according to format */ /* Print configuration according to format */
switch (format){ switch (format){
case FORMAT_XML: case FORMAT_XML:
@ -487,6 +497,7 @@ show_conf_xpath(clicon_handle h,
char *xpath; char *xpath;
cg_var *cv; cg_var *cv;
cxobj *xt = NULL; cxobj *xt = NULL;
cxobj *xerr;
cxobj **xv = NULL; cxobj **xv = NULL;
size_t xlen; size_t xlen;
int i; int i;
@ -505,8 +516,12 @@ show_conf_xpath(clicon_handle h,
} }
cv = cvec_find_var(cvv, "xpath"); cv = cvec_find_var(cvv, "xpath");
xpath = cv_string_get(cv); xpath = cv_string_get(cv);
if (clicon_rpc_get_config(h, str, xpath, 0, &xt) < 0) if (clicon_rpc_get_config(h, str, xpath, &xt) < 0)
goto done; goto done;
if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){
clicon_rpc_generate_error(xerr);
goto done;
}
if (xpath_vec(xt, xpath, &xv, &xlen) < 0) if (xpath_vec(xt, xpath, &xv, &xlen) < 0)
goto done; goto done;
for (i=0; i<xlen; i++) for (i=0; i<xlen; i++)

View file

@ -157,6 +157,7 @@ api_data_get_gen(clicon_handle h,
yang_spec *yspec; yang_spec *yspec;
cxobj *xret = NULL; cxobj *xret = NULL;
cxobj *xerr; cxobj *xerr;
cxobj *xtag;
cbuf *cbj = NULL;; cbuf *cbj = NULL;;
int code; int code;
const char *reason_phrase; const char *reason_phrase;
@ -170,7 +171,7 @@ api_data_get_gen(clicon_handle h,
goto done; goto done;
} }
clicon_debug(1, "%s path:%s", __FUNCTION__, cbuf_get(path)); clicon_debug(1, "%s path:%s", __FUNCTION__, cbuf_get(path));
if (clicon_rpc_get_config(h, "running", cbuf_get(path), 1, &xret) < 0){ if (clicon_rpc_get_config(h, "running", cbuf_get(path), &xret) < 0){
notfound(r); notfound(r);
goto done; goto done;
} }
@ -182,22 +183,22 @@ api_data_get_gen(clicon_handle h,
cbuf_free(cb); cbuf_free(cb);
} }
#endif #endif
if (strcmp(xml_name(xret), "rpc-error") == 0){ if ((xerr = xpath_first(xret, "/rpc-error")) != NULL){
if ((cbj = cbuf_new()) == NULL) if ((cbj = cbuf_new()) == NULL)
goto done; goto done;
if ((xerr = xpath_first(xret, "/error-tag")) == NULL){ if ((xtag = xpath_first(xerr, "/error-tag")) == NULL){
notfound(r); /* bad reply? */ notfound(r); /* bad reply? */
goto done; goto done;
} }
code = clicon_str2int(netconf_restconf_map, xml_body(xerr)); code = clicon_str2int(netconf_restconf_map, xml_body(xtag));
if ((reason_phrase = clicon_int2str(http_reason_phrase_map, code)) == NULL) if ((reason_phrase = clicon_int2str(http_reason_phrase_map, code)) == NULL)
reason_phrase=""; reason_phrase="";
clicon_debug(1, "%s code:%d reason phrase:%s", clicon_debug(1, "%s code:%d reason phrase:%s",
__FUNCTION__, code, reason_phrase); __FUNCTION__, code, reason_phrase);
if (xml_name_set(xret, "error") < 0) if (xml_name_set(xerr, "error") < 0)
goto done; goto done;
if (xml2json_cbuf(cbj, xret, 1) < 0) if (xml2json_cbuf(cbj, xerr, 1) < 0)
goto done; goto done;
FCGX_FPrintF(r->out, "Status: %d %s\r\n", code, reason_phrase); FCGX_FPrintF(r->out, "Status: %d %s\r\n", code, reason_phrase);
FCGX_FPrintF(r->out, "Content-Type: application/yang-data+json\r\n\r\n"); FCGX_FPrintF(r->out, "Content-Type: application/yang-data+json\r\n\r\n");

View file

@ -85,7 +85,7 @@ install: $(PLUGIN)
install-include: install-include:
uninstall: uninstall:
rm -rf $(DESTDIR)$(clixon_LIBDIR)/xmldb/$(PLUGIN) rm -rf $(DESTDIR)$(libdir)/xmldb/$(PLUGIN)
TAGS: TAGS:
find . -name '*.[chyl]' -print | etags - find . -name '*.[chyl]' -print | etags -

View file

@ -81,7 +81,7 @@ mycallback(clicon_handle h, cvec *cvv, cvec *argv)
/* Show eth0 interfaces config using XPATH */ /* Show eth0 interfaces config using XPATH */
if (clicon_rpc_get_config(h, "running","/interfaces/interface[name=eth0]", if (clicon_rpc_get_config(h, "running","/interfaces/interface[name=eth0]",
0, &xret) < 0) &xret) < 0)
goto done; goto done;
xml_print(stdout, xret); xml_print(stdout, xret);

View file

@ -45,8 +45,7 @@ int clicon_rpc_msg(clicon_handle h, struct clicon_msg *msg, cxobj **xret0,
int clicon_rpc_netconf(clicon_handle h, char *xmlst, cxobj **xret, int *sp); int clicon_rpc_netconf(clicon_handle h, char *xmlst, cxobj **xret, int *sp);
int clicon_rpc_netconf_xml(clicon_handle h, cxobj *xml, cxobj **xret, int *sp); int clicon_rpc_netconf_xml(clicon_handle h, cxobj *xml, cxobj **xret, int *sp);
int clicon_rpc_generate_error(cxobj *xerr); int clicon_rpc_generate_error(cxobj *xerr);
int clicon_rpc_get_config(clicon_handle h, char *db, char *xpath, int clicon_rpc_get_config(clicon_handle h, char *db, char *xpath, cxobj **xret);
int errmode, cxobj **xret);
int clicon_rpc_edit_config(clicon_handle h, char *db, enum operation_type op, int clicon_rpc_edit_config(clicon_handle h, char *db, enum operation_type op,
char *xml); char *xml);
int clicon_rpc_copy_config(clicon_handle h, char *db1, char *db2); int clicon_rpc_copy_config(clicon_handle h, char *db1, char *db2);

View file

@ -230,31 +230,33 @@ clicon_rpc_generate_error(cxobj *xerr)
* @param[in] h CLICON handle * @param[in] h CLICON handle
* @param[in] db Name of database * @param[in] db Name of database
* @param[in] xpath XPath (or "") * @param[in] xpath XPath (or "")
* @param[in] errmode 0 if xml errors are returned as clicon_err * @param[out] xt XML tree. Free with xml_free.
* 1 if xml errors are in xt and return 0. * Either <config> or <rpc-error>.
* @param[out] xt XML tree. must be freed by caller with xml_free
* @retval 0 OK * @retval 0 OK
* @retval -1 Error, fatal or xml * @retval -1 Error, fatal or xml
* @code * @code
* cxobj *xt = NULL; * cxobj *xt = NULL;
* if (clicon_rpc_get_config(h, "running", "/", 0, &xt) < 0) * if (clicon_rpc_get_config(h, "running", "/", &xt) < 0)
* err; * err;
* if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){
* clicon_rpc_generate_error(xerr);
* err;
* }
* if (xt) * if (xt)
* xml_free(xt); * xml_free(xt);
* @endcode * @endcode
* @see clicon_rpc_generate_error
*/ */
int int
clicon_rpc_get_config(clicon_handle h, clicon_rpc_get_config(clicon_handle h,
char *db, char *db,
char *xpath, char *xpath,
int errmode,
cxobj **xt) cxobj **xt)
{ {
int retval = -1; int retval = -1;
struct clicon_msg *msg = NULL; struct clicon_msg *msg = NULL;
cbuf *cb = NULL; cbuf *cb = NULL;
cxobj *xret = NULL; cxobj *xret = NULL;
cxobj *xerr;
cxobj *xd; cxobj *xd;
if ((cb = cbuf_new()) == NULL) if ((cb = cbuf_new()) == NULL)
@ -267,22 +269,12 @@ clicon_rpc_get_config(clicon_handle h,
goto done; goto done;
if (clicon_rpc_msg(h, msg, &xret, NULL) < 0) if (clicon_rpc_msg(h, msg, &xret, NULL) < 0)
goto done; goto done;
if (errmode == 0){ /* Move this to caller */ /* Send xml error back: first check error, then ok */
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){ if ((xd = xpath_first(xret, "/rpc-reply/rpc-error")) != NULL)
clicon_rpc_generate_error(xerr); xd = xml_parent(xd); /* point to rpc-reply */
goto done; else if ((xd = xpath_first(xret, "/rpc-reply/data/config")) == NULL)
}
if ((xd = xpath_first(xret, "//data/config")) == NULL)
if ((xd = xml_new("config", NULL)) == NULL) if ((xd = xml_new("config", NULL)) == NULL)
goto done; goto done;
}
else{ /* Send xml error back (this should be default behaviour) */
if ((xd = xpath_first(xret, "//rpc-error")) == NULL){
if ((xd = xpath_first(xret, "//data/config")) == NULL)
if ((xd = xml_new("config", NULL)) == NULL)
goto done;
}
}
if (xt){ if (xt){
if (xml_rm(xd) < 0) if (xml_rm(xd) < 0)
goto done; goto done;

View file

@ -71,10 +71,6 @@
#include "clixon_yang_type.h" #include "clixon_yang_type.h"
#include "clixon_yang_parse.h" #include "clixon_yang_parse.h"
/* Instead of using dynamic type lookup, use a cache that is evaluated early
for static scope type binding */
#define YANG_TYPE_CACHE 1
/* Mapping between yang keyword string <--> clicon constants */ /* Mapping between yang keyword string <--> clicon constants */
static const map_str2int ykmap[] = { static const map_str2int ykmap[] = {
@ -1392,7 +1388,7 @@ yang_parse_file(clicon_handle h,
* @param[in] module Name of main YANG module. * @param[in] module Name of main YANG module.
* @param[out] fbuf Buffer containing filename * @param[out] fbuf Buffer containing filename
* *
* @retval 1 Match founbd, Most recent entry returned in fbuf * @retval 1 Match found, Most recent entry returned in fbuf
* @retval 0 No matching entry found * @retval 0 No matching entry found
* @retval -1 Error * @retval -1 Error
*/ */
@ -1406,17 +1402,20 @@ yang_parse_find_match(clicon_handle h,
struct dirent *dp = NULL; struct dirent *dp = NULL;
int ndp; int ndp;
cbuf *regex = NULL; cbuf *regex = NULL;
char *regexstr;
if ((regex = cbuf_new()) == NULL){ if ((regex = cbuf_new()) == NULL){
clicon_err(OE_YANG, errno, "cbuf_new"); clicon_err(OE_YANG, errno, "cbuf_new");
goto done; goto done;
} }
cprintf(regex, "^%s.*(.yang)$", module); /* RFC 6020: The name of the file SHOULD be of the form:
regexstr = cbuf_get(regex); module-or-submodule-name ['@' revision-date] ( '.yang' / '.yin' )
revision-date ::= 4DIGIT "-" 2DIGIT "-" 2DIGIT
*/
cprintf(regex, "^%s(@[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])?(.yang)$",
module);
if ((ndp = clicon_file_dirent(yang_dir, if ((ndp = clicon_file_dirent(yang_dir,
&dp, &dp,
regexstr, cbuf_get(regex),
S_IFREG)) < 0) S_IFREG)) < 0)
goto done; goto done;
/* Entries are sorted, last entry should be most recent date */ /* Entries are sorted, last entry should be most recent date */
@ -1587,10 +1586,9 @@ yang_parse(clicon_handle h,
/* Add top module name as dbspec-name */ /* Add top module name as dbspec-name */
clicon_dbspec_name_set(h, ymod->ys_argument); clicon_dbspec_name_set(h, ymod->ys_argument);
#ifdef YANG_TYPE_CACHE
/* Resolve all types */ /* Resolve all types */
yang_apply((yang_node*)ysp, ys_resolve_type, NULL); yang_apply((yang_node*)ysp, ys_resolve_type, NULL);
#endif
/* Step 2: Macro expansion of all grouping/uses pairs. Expansion needs marking */ /* Step 2: Macro expansion of all grouping/uses pairs. Expansion needs marking */
if (yang_expand((yang_node*)ysp) < 0) if (yang_expand((yang_node*)ysp) < 0)
goto done; goto done;

View file

@ -178,7 +178,8 @@ yang_type_cache_get(yang_type_cache *ycache,
} }
int int
yang_type_cache_cp(yang_type_cache **ycnew, yang_type_cache *ycold) yang_type_cache_cp(yang_type_cache **ycnew,
yang_type_cache *ycold)
{ {
int retval = -1; int retval = -1;
int options; int options;
@ -211,7 +212,8 @@ yang_type_cache_free(yang_type_cache *ycache)
/*! Resolve types: populate type caches */ /*! Resolve types: populate type caches */
int int
ys_resolve_type(yang_stmt *ys, void *arg) ys_resolve_type(yang_stmt *ys,
void *arg)
{ {
int retval = -1; int retval = -1;
int options = 0x0; int options = 0x0;
@ -333,7 +335,9 @@ cv2yang_type(enum cv_type cv_type)
* @param[out] cvtype * @param[out] cvtype
*/ */
int int
clicon_type2cv(char *origtype, char *restype, enum cv_type *cvtype) clicon_type2cv(char *origtype,
char *restype,
enum cv_type *cvtype)
{ {
int retval = -1; int retval = -1;
@ -700,7 +704,7 @@ ys_typedef_up(yang_stmt *ys)
This is a sanity check of base identity of identity-ref and for identity This is a sanity check of base identity of identity-ref and for identity
statements. statements.
Return true if node is identityref and is derived from identity_name Return true if node is identityref and is derived from identity_name
The derived-from() function returns true if the (first) node (in The derived-from() function returns true if the (first) node (in
document order in the argument "nodes") is a node of type identityref, document order in the argument "nodes") is a node of type identityref,
and its value is an identity that is derived from the identity and its value is an identity that is derived from the identity
@ -718,7 +722,8 @@ Return true if node is identityref and is derived from identity_name
vad är det denna function ska göra? Svar: 1 vad är det denna function ska göra? Svar: 1
*/ */
yang_stmt * yang_stmt *
yang_find_identity(yang_stmt *ys, char *identity) yang_find_identity(yang_stmt *ys,
char *identity)
{ {
char *id; char *id;
char *prefix = NULL; char *prefix = NULL;