XML chanelog revision
This commit is contained in:
parent
3f68cca06c
commit
a0abf8436e
24 changed files with 948 additions and 848 deletions
367
lib/src/clixon_xml_changelog.c
Normal file
367
lib/src/clixon_xml_changelog.c
Normal file
|
|
@ -0,0 +1,367 @@
|
|||
/*
|
||||
*
|
||||
***** BEGIN LICENSE BLOCK *****
|
||||
|
||||
Copyright (C) 2009-2019 Olof Hagsand and Benny Holmgren
|
||||
|
||||
This file is part of CLIXON.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of
|
||||
the GNU General Public License Version 3 or later (the "GPL"),
|
||||
in which case the provisions of the GPL are applicable instead
|
||||
of those above. If you wish to allow use of your version of this file only
|
||||
under the terms of the GPL, and not to allow others to
|
||||
use your version of this file under the terms of Apache License version 2,
|
||||
indicate your decision by deleting the provisions above and replace them with
|
||||
the notice and other provisions required by the GPL. If you do not delete
|
||||
the provisions above, a recipient may use your version of this file under
|
||||
the terms of any one of the Apache License version 2 or the GPL.
|
||||
|
||||
***** END LICENSE BLOCK *****
|
||||
|
||||
* YANG module revision change management.
|
||||
* See draft-wang-netmod-module-revision-management-01
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "clixon_config.h" /* generated by config & autoconf */
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <fnmatch.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syslog.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
/* cligen */
|
||||
#include <cligen/cligen.h>
|
||||
|
||||
/* clixon */
|
||||
#include "clixon_queue.h"
|
||||
#include "clixon_hash.h"
|
||||
#include "clixon_string.h"
|
||||
#include "clixon_err.h"
|
||||
#include "clixon_handle.h"
|
||||
#include "clixon_yang.h"
|
||||
#include "clixon_log.h"
|
||||
#include "clixon_xml.h"
|
||||
#include "clixon_options.h"
|
||||
#include "clixon_xml_map.h"
|
||||
#include "clixon_yang_module.h"
|
||||
#include "clixon_xml_changelog.h"
|
||||
#include "clixon_xpath_ctx.h"
|
||||
#include "clixon_xpath.h"
|
||||
|
||||
/*! Perform a changelog operation
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] xi Changelog item
|
||||
* @param[in] xn XML to upgrade
|
||||
* @note XXX error handling!
|
||||
* @note XXX xn --> xt xpath may not match
|
||||
*/
|
||||
static int
|
||||
changelog_op(clicon_handle h,
|
||||
cxobj *xt,
|
||||
cxobj *xi)
|
||||
|
||||
{
|
||||
int retval = -1;
|
||||
char *op;
|
||||
char *xptarget; /* xpath to target-node */
|
||||
char *xplocation; /* xpath to location-node (move) */
|
||||
char *ftransform; /* transform string format (modify, create) */
|
||||
cxobj *xtrg; /* xml target node */
|
||||
cxobj *xloc; /* xml location node */
|
||||
cxobj *xnew = NULL;
|
||||
cxobj *x;
|
||||
|
||||
if ((op = xml_find_body(xi, "change-operation")) == NULL)
|
||||
goto ok;
|
||||
if ((xptarget = xml_find_body(xi, "target-node")) == NULL)
|
||||
goto ok;
|
||||
/* target node (if any) */
|
||||
if ((xtrg = xpath_first(xt, "%s", xptarget)) == NULL)
|
||||
goto fail;
|
||||
// fprintf(stderr, "%s %s %s\n", __FUNCTION__, op, xml_name(xt));
|
||||
xplocation = xml_find_body(xi, "location-node");
|
||||
ftransform = xml_find_body(xi, "transform");
|
||||
if (strcmp(op, "insert") == 0){
|
||||
/* create a new node by parsing fttransform string and insert it at
|
||||
target */
|
||||
if (ftransform == NULL)
|
||||
goto fail;
|
||||
if (xml_parse_va(&xtrg, NULL, "%s", ftransform) < 0)
|
||||
goto done;
|
||||
}
|
||||
else if (strcmp(op, "delete") == 0){
|
||||
/* delete target */
|
||||
if (xml_purge(xtrg) < 0)
|
||||
goto done;
|
||||
}
|
||||
else if (strcmp(op, "move") == 0){
|
||||
/* Move target node to location */
|
||||
if ((xloc = xpath_first(xt, "%s", xplocation)) == NULL)
|
||||
goto fail;
|
||||
if (xml_addsub(xloc, xtrg) < 0)
|
||||
goto done;
|
||||
}
|
||||
else if (strcmp(op, "replace") == 0){
|
||||
/* create a new node by parsing fttransform string and insert it at
|
||||
target */
|
||||
if (ftransform == NULL)
|
||||
goto fail;
|
||||
/* replace: remove all children of target */
|
||||
while ((x = xml_child_i(xtrg, 0)) != NULL)
|
||||
if (xml_purge(x) < 0)
|
||||
goto done;
|
||||
/* Parse the new node */
|
||||
if (xml_parse_va(&xnew, NULL, "%s", ftransform) < 0)
|
||||
goto done;
|
||||
if (xml_rootchild(xnew, 0, &xnew) < 0)
|
||||
goto done;
|
||||
/* Copy old to new */
|
||||
if (xml_copy(xnew, xtrg) < 0)
|
||||
goto done;
|
||||
if (xml_purge(xnew) < 0)
|
||||
goto done;
|
||||
}
|
||||
ok:
|
||||
retval = 1;
|
||||
done:
|
||||
return retval;
|
||||
fail:
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/*! Iterate through one changelog item
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] xt Changelog list
|
||||
* @param[in] xn XML to upgrade
|
||||
*/
|
||||
static int
|
||||
changelog_iterate(clicon_handle h,
|
||||
cxobj *xt,
|
||||
cxobj *xch)
|
||||
|
||||
{
|
||||
int retval = -1;
|
||||
cxobj **vec = NULL;
|
||||
size_t veclen;
|
||||
int ret;
|
||||
int i;
|
||||
|
||||
if (xpath_vec(xch, "change-log", &vec, &veclen) < 0)
|
||||
goto done;
|
||||
/* Iterate through changelog items */
|
||||
for (i=0; i<veclen; i++){
|
||||
if ((ret = changelog_op(h, xt, vec[i])) < 0)
|
||||
goto done;
|
||||
if (ret == 0)
|
||||
goto fail;
|
||||
}
|
||||
retval = 1;
|
||||
done:
|
||||
if (vec)
|
||||
free(vec);
|
||||
return retval;
|
||||
fail:
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/*! Automatic upgrade using changelog
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] xt Top-level XML tree to be updated (includes other ns as well)
|
||||
* @param[in] namespace Namespace of module (for info)
|
||||
* @param[in] from From revision on the form YYYYMMDD
|
||||
* @param[in] to To revision on the form YYYYMMDD (0 not in system)
|
||||
* @param[in] arg User argument given at rpc_callback_register()
|
||||
* @param[out] cbret Return xml tree, eg <rpc-reply>..., <rpc-error..
|
||||
* @retval 1 OK
|
||||
* @retval 0 Invalid
|
||||
* @retval -1 Error
|
||||
* @see upgrade_callback_register where this function should be registered
|
||||
*/
|
||||
int
|
||||
xml_changelog_upgrade(clicon_handle h,
|
||||
cxobj *xt,
|
||||
char *namespace,
|
||||
uint32_t from,
|
||||
uint32_t to,
|
||||
void *arg,
|
||||
cbuf *cbret)
|
||||
{
|
||||
int retval = -1;
|
||||
cxobj *xchlog; /* changelog */
|
||||
cxobj **vec = NULL;
|
||||
cxobj *xch;
|
||||
size_t veclen;
|
||||
char *b;
|
||||
int ret;
|
||||
int i;
|
||||
uint32_t f;
|
||||
uint32_t t;
|
||||
|
||||
/* Check if changelog enabled */
|
||||
if (!clicon_option_bool(h, "CLICON_XML_CHANGELOG"))
|
||||
goto ok;
|
||||
/* Get changelog */
|
||||
if ((xchlog = clicon_xml_changelog_get(h)) == NULL)
|
||||
goto ok;
|
||||
|
||||
/* Iterate and find relevant changelog entries in the interval:
|
||||
* - find all changelogs in the interval: [from, to]
|
||||
* - note it t=0 then no changelog is applied
|
||||
*/
|
||||
if (xpath_vec(xchlog, "module[namespace=\"%s\"]",
|
||||
&vec, &veclen, namespace) < 0)
|
||||
goto done;
|
||||
/* Get all changelogs in the interval [from,to]*/
|
||||
for (i=0; i<veclen; i++){
|
||||
xch = vec[i];
|
||||
f = t = 0;
|
||||
if ((b = xml_find_body(xch, "revfrom")) != NULL)
|
||||
if (ys_parse_date_arg(b, &f) < 0)
|
||||
goto done;
|
||||
if ((b = xml_find_body(xch, "revision")) != NULL)
|
||||
if (ys_parse_date_arg(b, &t) < 0)
|
||||
goto done;
|
||||
if ((f && from>f) || to<t)
|
||||
continue;
|
||||
if ((ret = changelog_iterate(h, xt, xch)) < 0)
|
||||
goto done;
|
||||
if (ret == 0)
|
||||
goto fail;
|
||||
}
|
||||
ok:
|
||||
retval = 1;
|
||||
done:
|
||||
if (vec)
|
||||
free(vec);
|
||||
return retval;
|
||||
fail:
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/*! Initialize module revision. read changelog, etc
|
||||
*/
|
||||
int
|
||||
clixon_xml_changelog_init(clicon_handle h)
|
||||
{
|
||||
int retval = -1;
|
||||
char *filename;
|
||||
int fd = -1;
|
||||
cxobj *xt = NULL;
|
||||
yang_spec *yspec;
|
||||
cbuf *cbret = NULL;
|
||||
int ret;
|
||||
|
||||
yspec = clicon_dbspec_yang(h);
|
||||
if ((filename = clicon_option_str(h, "CLICON_XML_CHANGELOG_FILE")) != NULL){
|
||||
if ((fd = open(filename, O_RDONLY)) < 0){
|
||||
clicon_err(OE_UNIX, errno, "open(%s)", filename);
|
||||
goto done;
|
||||
}
|
||||
if (xml_parse_file(fd, NULL, yspec, &xt) < 0)
|
||||
goto done;
|
||||
if (xml_rootchild(xt, 0, &xt) < 0)
|
||||
goto done;
|
||||
if ((cbret = cbuf_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if ((ret = xml_yang_validate_all(xt, cbret)) < 0)
|
||||
goto done;
|
||||
if (ret==1 && (ret = xml_yang_validate_add(xt, cbret)) < 0)
|
||||
goto done;
|
||||
if (ret == 0){ /* validation failed */
|
||||
clicon_err(OE_YANG, 0, "validation failed: %s", cbuf_get(cbret));
|
||||
goto done;
|
||||
}
|
||||
if (clicon_xml_changelog_set(h, xt) < 0)
|
||||
goto done;
|
||||
xt = NULL;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
if (fd != -1)
|
||||
close(fd);
|
||||
if (xt)
|
||||
xml_free(xt);
|
||||
if (cbret)
|
||||
cbuf_free(cbret);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Given a top-level XML tree and a namespace, return a vector of matching XML nodes
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] xt Top-level XML tree, with children marked with namespaces
|
||||
* @param[in] namespace The namespace to select
|
||||
* @param[out] vecp Vector containining XML nodes w namespace. Null-terminated.
|
||||
* @param[out] veclenp Length of vector
|
||||
* @note Need to free vec after use with free()
|
||||
* Example
|
||||
* xt ::= <config><a xmlns="urn:example:a"/><aaa xmlns="urn:example:a"/><a xmlns="urn:example:b"/></config
|
||||
* namespace ::= urn:example:a
|
||||
* out:
|
||||
* vec ::= [<a xmlns="urn:example:a"/>, <aaa xmlns="urn:example:a"/>, NULL]
|
||||
*/
|
||||
int
|
||||
xml_namespace_vec(clicon_handle h,
|
||||
cxobj *xt,
|
||||
char *namespace,
|
||||
cxobj ***vecp,
|
||||
size_t *veclenp)
|
||||
{
|
||||
int retval = -1;
|
||||
cxobj **xvec = NULL;
|
||||
size_t xlen;
|
||||
cxobj *xc;
|
||||
char *ns;
|
||||
int i;
|
||||
|
||||
/* Allocate upper bound on length (ie could be too large) + a NULL element
|
||||
* (event though we use veclen)
|
||||
*/
|
||||
xlen = xml_child_nr_type(xt, CX_ELMNT)+1;
|
||||
if ((xvec = calloc(xlen, sizeof(cxobj*))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "calloc");
|
||||
goto done;
|
||||
}
|
||||
/* Iterate and find xml nodes with assoctaed namespace */
|
||||
xc = NULL;
|
||||
i = 0;
|
||||
while ((xc = xml_child_each(xt, xc, CX_ELMNT)) != NULL) {
|
||||
if (xml2ns(xc, NULL, &ns) < 0) /* Get namespace of XML */
|
||||
goto done;
|
||||
if (strcmp(namespace, ns))
|
||||
continue; /* no match */
|
||||
xvec[i++] = xc;
|
||||
}
|
||||
*vecp = xvec;
|
||||
*veclenp = i;
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue