/* * ***** BEGIN LICENSE BLOCK ***** Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren Copyright (C) 2017-2019 Olof Hagsand Copyright (C) 2020-2021 Olof Hagsand and Rubicon Communications, LLC(Netgate) 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 #include #include #include #include #include #include #include #include #include #include /* cligen */ #include /* 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_data.h" #include "clixon_yang_module.h" #include "clixon_yang_parse_lib.h" #include "clixon_netconf_lib.h" #include "clixon_xml_nsctx.h" #include "clixon_xml_map.h" #include "clixon_xml_io.h" #include "clixon_validate.h" #include "clixon_xml_changelog.h" #include "clixon_xpath_ctx.h" #include "clixon_xpath.h" static int changelog_rename(clicon_handle h, cxobj *xt, cxobj *xw, cvec *nsc, char *tag) { int retval = -1; xp_ctx *xctx = NULL; char *str = NULL; if (tag == NULL){ clicon_err(OE_XML, 0, "tag required"); goto done; } if (xpath_vec_ctx(xw, nsc, tag, 0, &xctx) < 0) goto done; if (ctx2string(xctx, &str) < 0) goto done; if (!strlen(str)){ clicon_err(OE_XML, 0, "invalid rename tag: \"%s\"", str); goto done; } if (xml_name_set(xw, str) < 0) goto done; // ok: retval = 1; done: if (xctx) ctx_free(xctx); if (str) free(str); return retval; // fail: retval = 0; goto done; } /* replace target XML */ static int changelog_replace(clicon_handle h, cxobj *xt, cxobj *xw, cxobj *xnew) { int retval = -1; cxobj *x; /* create a new node by parsing fttransform string and insert it at target */ if (xnew == NULL){ clicon_err(OE_XML, 0, "new required"); goto done; } /* replace: remove all children of target */ while ((x = xml_child_i(xw, 0)) != NULL) if (xml_purge(x) < 0) goto done; /* replace: first single node under */ if (xml_child_nr(xnew) != 1){ clicon_err(OE_XML, 0, "Single child to required"); goto done; } x = xml_child_i(xnew, 0); /* Copy from xnew to (now) empty target */ if (xml_copy(x, xw) < 0) goto done; retval = 1; done: return retval; } /* create a new node by parsing "new" and insert it at target */ static int changelog_insert(clicon_handle h, cxobj *xt, cxobj *xw, cxobj *xnew) { int retval = -1; cxobj *x; if (xnew == NULL){ clicon_err(OE_XML, 0, "new required"); goto done; } /* replace: add all new children to target */ while ((x = xml_child_i(xnew, 0)) != NULL) if (xml_addsub(xw, x) < 0) goto done; // ok: retval = 1; done: return retval; // fail: retval = 0; goto done; } /* delete target */ static int changelog_delete(clicon_handle h, cxobj *xt, cxobj *xw) { int retval = -1; if (xml_purge(xw) < 0) goto done; retval = 1; done: return retval; } /* Move target node to location */ static int changelog_move(clicon_handle h, cxobj *xt, cxobj *xw, cvec *nsc, char *dst) { int retval = -1; cxobj *xp; /* destination parent node */ if ((xp = xpath_first(xt, nsc, "%s", dst)) == NULL){ clicon_err(OE_XML, 0, "path required"); goto done; } if (xml_addsub(xp, xw) < 0) goto done; retval = 1; done: return retval; } /*! Perform a changelog operation * @param[in] h Clicon handle * @param[in] xt XML to upgrade * @param[in] xi Changelog item * @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 *whenxpath; /* xpath to when */ char *tag; /* xpath to extra path (move) */ char *dst; /* xpath to extra path (move) */ cxobj *xnew; /* new xml (insert, replace) */ char *wxpath; /* xpath to where (target-node) */ cxobj **wvec = NULL; /* Vector of where(target) nodes */ size_t wlen; cxobj *xw; int ret; xp_ctx *xctx = NULL; int i; cvec *nsc = NULL; /* Get namespace context from changelog item */ if (xml_nsctx_node(xi, &nsc) < 0) goto done; if ((op = xml_find_body(xi, "op")) == NULL) goto ok; /* get common variables that may be used in the operations below */ tag = xml_find_body(xi, "tag"); dst = xml_find_body(xi, "dst"); xnew = xml_find(xi, "new"); whenxpath = xml_find_body(xi, "when"); if ((wxpath = xml_find_body(xi, "where")) == NULL) goto ok; /* Get vector of target nodes meeting the where requirement */ if (xpath_vec(xt, nsc, "%s", &wvec, &wlen, wxpath) < 0) goto done; for (i=0; i..., f) || to, , NULL] */ int xml_namespace_vec(clicon_handle h, cxobj *xt, char *ns, cxobj ***vecp, size_t *veclenp) { int retval = -1; cxobj **xvec = NULL; size_t xlen; cxobj *xc; char *ns0; 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, &ns0) < 0) /* Get namespace of XML */ goto done; if (strcmp(ns, ns0)) continue; /* no match */ xvec[i++] = xc; } *vecp = xvec; *veclenp = i; retval = 0; done: return retval; }