/* * ***** 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 ***** * "api-path" is "URI-encoded path expression" definition in RFC8040 3.5.3 * BNF: * := ("/" ( | ))* * := * := [ ":"] * := * := "=" key-value *("," key-value) * := * := * := ( | "_") ( | | "_" | "-" | ".") */ #ifdef HAVE_CONFIG_H #include "clixon_config.h" /* generated by config & autoconf */ #endif #include #include #include #include #include #include #include #include #include #include #include #include /* cligen */ #include /* clixon */ #include "clixon_string.h" #include "clixon_queue.h" #include "clixon_hash.h" #include "clixon_handle.h" #include "clixon_string.h" #include "clixon_err.h" #include "clixon_log.h" #include "clixon_options.h" #include "clixon_yang.h" #include "clixon_xml.h" #include "clixon_xml_nsctx.h" #include "clixon_netconf_lib.h" #include "clixon_xml_map.h" #include "clixon_api_path.h" /*! Given an XML node, return root node * A root node is an ancestor xr of x with one or both of the following properties * - its XML parent is NULL parent, * - its associated yang specification's parent is a yang module. * @param[in] x XML node * @param[out] xr XML root */ int xml_yang_root(cxobj *x, cxobj **xr) { int retval = -1; cxobj *xp; yang_stmt *y; yang_stmt *yp; while ((xp = xml_parent(x)) != NULL){ if ((y = xml_spec(x)) != NULL && (yp = yang_parent_get(y)) != NULL) /* Actually, maybe only the Y_MODULE clause is relevant */ if (yp==NULL || yang_keyword_get(yp) == Y_MODULE || yang_keyword_get(yp) == Y_SUBMODULE) break; /* x is the root */ x = xp; } *xr = x; retval = 0; return retval; } /*! Construct an xml key format from yang statement using wildcards for keys * Recursively construct it to the top. * Example: * yang: container a -> list b -> key c -> leaf d * xpath: /modname:a/b/%s/d * @param[in] ys Yang statement * @param[in] inclkey If set include key leaf (eg last leaf d in ex) * @param[out] cb api_path_fmt, * @retval 0 OK * @retval -1 Error * @see RFC8040 3.5.3 where "api-path" is defined as "URI-encoded path expression" */ static int yang2api_path_fmt_1(yang_stmt *ys, int inclkey, cbuf *cb) { yang_stmt *yp; /* parent */ yang_stmt *ymod; int i; cvec *cvk = NULL; /* vector of index keys */ int retval = -1; if ((yp = yang_parent_get(ys)) == NULL){ clicon_err(OE_YANG, EINVAL, "yang expected parent %s", yang_argument_get(ys)); goto done; } if (yp != NULL && /* XXX rm */ yang_keyword_get(yp) != Y_MODULE && yang_keyword_get(yp) != Y_SUBMODULE){ if (yang2api_path_fmt_1((yang_stmt *)yp, 1, cb) < 0) /* recursive call */ goto done; if (yang_keyword_get(yp) != Y_CHOICE && yang_keyword_get(yp) != Y_CASE){ #if 0 /* In some cases, such as cli_show_auto, a trailing '/' should * NOT be present if ys is a key in a list. * But in other cases (I think most), the / should be there, * so a patch is added in cli_show_auto instead. */ if (yang_keyword_get(ys) == Y_LEAF && yp && yang_keyword_get(yp) == Y_LIST && yang_key_match(yp, ys->ys_argument) == 1) ; else #endif cprintf(cb, "/"); } /* If parent namespace/module is different from child -> add child prefix */ if (ys_real_module(yp) != (ymod = ys_real_module(ys))) cprintf(cb, "%s:", yang_argument_get(ymod)); } else /* top symbol - mark with name prefix */ cprintf(cb, "/%s:", yang_argument_get(yp)); if (inclkey){ if (yang_keyword_get(ys) != Y_CHOICE && yang_keyword_get(ys) != Y_CASE) cprintf(cb, "%s", yang_argument_get(ys)); } else{ if (yang_keyword_get(ys) == Y_LEAF && yp && yang_keyword_get(yp) == Y_LIST){ if (yang_key_match(yp, yang_argument_get(ys)) == 0) cprintf(cb, "%s", yang_argument_get(ys)); /* Not if leaf and key */ } else if (yang_keyword_get(ys) != Y_CHOICE && yang_keyword_get(ys) != Y_CASE) cprintf(cb, "%s", yang_argument_get(ys)); } switch (yang_keyword_get(ys)){ case Y_LIST: cvk = yang_cvec_get(ys); /* Use Y_LIST cache, see ys_populate_list() */ if (cvec_len(cvk)) cprintf(cb, "="); /* Iterate over individual keys */ for (i=0; i list b -> key c -> leaf d * api_path: /a/b=%s/d * @param[in] ys Yang statement * @param[in] inclkey If set include key leaf (eg last leaf d in ex) * @param[out] api_path_fmt XML api path. Needs to be freed after use. * "api-path" is "URI-encoded path expression" definition in RFC8040 3.5.3 */ int yang2api_path_fmt(yang_stmt *ys, int inclkey, char **api_path_fmt) { int retval = -1; cbuf *cb = NULL; if ((cb = cbuf_new()) == NULL){ clicon_err(OE_UNIX, errno, "cbuf_new"); goto done; } if (yang2api_path_fmt_1(ys, inclkey, cb) < 0) goto done; if ((*api_path_fmt = strdup(cbuf_get(cb))) == NULL){ clicon_err(OE_UNIX, errno, "strdup"); goto done; } retval = 0; done: if (cb) cbuf_free(cb); return retval; } /*! Transform an xml key format and a vector of values to an XML key * Used for actual key, eg in clicon_rpc_change(), xmldb_put_xkey() * Example: * xmlkeyfmt: /interfaces/interface=%s/ipv4/address=%s * cvv: 0 : set interfaces interface e ipv4 address 1.2.3.4 * 1 : name = "e" * 2 : ip = "1.2.3.4" * api_path: /interfaces/interface=e/ipv4/address=1.2.3.4 * @param[in] api_path_fmt XML key format, eg /aaa/%s/name * @param[in] cvv cligen variable vector, one for every wildchar in * api_path_fmt * @param[out] api_path api_path, eg /aaa/17. Free after use * @note first and last elements of cvv are not used,.. * @see api_path_fmt2xpath * @example * api_path_fmt: /interfaces/interface=%s/name * cvv: - * api_path: /interfaces/interface/name * @example * api_path_fmt: /interfaces/interface=%s/name * cvv: e0 * api_path: /interfaces/interface=e0/name * @example * api_path_fmt: /subif-entry=%s,%s/subid * cvv: foo * api_path: /subif-entry=foo/subid * * "api-path" is "URI-encoded path expression" definition in RFC8040 3.5.3 */ int api_path_fmt2api_path(char *api_path_fmt, cvec *cvv, char **api_path) { int retval = -1; char c; int esc=0; cbuf *cb = NULL; int i; int j; char *str; char *strenc=NULL; cg_var *cv; if ((cb = cbuf_new()) == NULL){ clicon_err(OE_UNIX, errno, "cbuf_new"); goto done; } j = 1; /* j==0 is cli string */ for (i=0; i get module + change namespace */ if ((ymod = yang_find_module_by_name(yspec, prefix)) == NULL){ cprintf(cberr, "No such yang module: %s", prefix); if (netconf_invalid_value_xml(xerr, "application", cbuf_get(cberr)) < 0) goto done; goto fail; } namespace = yang_find_mynamespace(ymod); /* change namespace */ } if (i == offset && ymod) /* root */ y = yang_find_datanode(ymod, name); else y = yang_find_datanode(y, name); if (y == NULL){ if (netconf_unknown_element_xml(xerr, "application", name, "Unknown element") < 0) goto done; goto fail; } /* Get XML/xpath prefix given namespace. * note different from api-path prefix */ if (xml_nsctx_get_prefix(nsc, namespace, &xprefix) == 0){ xprefix = yang_find_myprefix(y); clicon_debug(1, "%s prefix not found add it %s", __FUNCTION__, xprefix); /* not found, add it to nsc */ if (xml_nsctx_add(nsc, xprefix, namespace) < 0) goto done; } /* Check if has value, means '=' */ if (cv2str(cv, NULL, 0) > 0){ if ((val = cv2str_dup(cv)) == NULL) goto done; switch (yang_keyword_get(y)){ case Y_LIST: /* Transform value "a,b,c" to "a" "b" "c" (nvalvec=3) * Note that vnr can be < length of cvk, due to empty or unset values */ if (valvec){ /* loop, valvec may have been used before */ free(valvec); valvec = NULL; } if ((valvec = clicon_strsep(val, ",", &nvalvec)) == NULL) goto done; cvk = yang_cvec_get(y); /* Use Y_LIST cache, see ys_populate_list() */ cvi = NULL; /* Iterate over individual yang keys */ cprintf(xpath, "/"); if (xprefix) cprintf(xpath, "%s:", xprefix); cprintf(xpath, "%s", name); vi = 0; while ((cvi = cvec_each(cvk, cvi)) != NULL && vi * xtop[out]: * foo> * * xbotp: * ybotp: Y_LEAF subid * @note "api-path" is "URI-encoded path expression" definition in RFC8040 3.5.3 * @see api_path2xpath For api-path to xml xpath translation */ int api_path2xml(char *api_path, yang_stmt *yspec, cxobj *xtop, yang_class nodeclass, int strict, cxobj **xbotp, yang_stmt **ybotp, cxobj **xerr) { int retval = -1; char **vec = NULL; int nvec; cxobj *xroot; cbuf *cberr = NULL; clicon_debug(1, "%s api_path:%s", __FUNCTION__, api_path); if ((cberr = cbuf_new()) == NULL){ clicon_err(OE_UNIX, errno, "cbuf_new"); goto done; } if (*api_path!='/'){ cprintf(cberr, "Invalid api-path: %s (must start with '/')", api_path); if (netconf_invalid_value_xml(xerr, "application", cbuf_get(cberr)) < 0) goto done; goto fail; } if ((vec = clicon_strsep(api_path, "/", &nvec)) == NULL) goto done; /* Remove trailing '/'. Like in /a/ -> /a */ if (nvec > 1 && !strlen(vec[nvec-1])) nvec--; if (nvec < 1){ cprintf(cberr, "Malformed api-path: %s: too short)", api_path); if (netconf_invalid_value_xml(xerr, "application", cbuf_get(cberr)) < 0) goto done; goto fail; } nvec--; /* NULL-terminated */ if ((retval = api_path2xml_vec(vec+1, nvec, xtop, yspec, nodeclass, strict, xbotp, ybotp, xerr)) < 1) goto done; xml_yang_root(*xbotp, &xroot); if (xmlns_assign(xroot) < 0) goto done; // ok: retval = 1; done: if (cberr) cbuf_free(cberr); if (vec) free(vec); return retval; fail: retval = 0; goto done; } /*! Construct an api_path from an XML node (single level not recursive) * @param[in] x XML node (need to be yang populated) * @param[out] cb api_path, must be initialized * @retval 0 OK * @retval -1 Error * @see yang2api_path_fmt * @see xml2xpath */ int xml2api_path_1(cxobj *x, cbuf *cb) { int retval = -1; yang_stmt *y = NULL; cvec *cvk = NULL; /* vector of index keys */ cg_var *cvi; enum rfc_6020 keyword; int i; char *keyname; cxobj *xkey; cxobj *xb; char *b; char *enc; yang_stmt *ymod; cxobj *xp; if ((y = xml_spec(x)) == NULL){ cprintf(cb, "/%s", xml_name(x)); goto ok; } ymod = ys_module(y); xp = xml_parent(x); if (ymod && xp && xml_spec(xp)==NULL) /* Add prefix only if root */ cprintf(cb, "/%s:%s", yang_argument_get(ymod), xml_name(x)); else cprintf(cb, "/%s", xml_name(x)); keyword = yang_keyword_get(y); switch (keyword){ case Y_LEAF_LIST: b = xml_body(x); enc = NULL; if (uri_percent_encode(&enc, "%s", b) < 0) goto done; cprintf(cb, "=%s", enc?enc:""); if (enc) free(enc); break; case Y_LIST: cvk = yang_cvec_get(y); /* Use Y_LIST cache, see ys_populate_list() */ if (cvec_len(cvk)) cprintf(cb, "="); /* Iterate over individual keys */ cvi = NULL; i = 0; while ((cvi = cvec_each(cvk, cvi)) != NULL) { keyname = cv_string_get(cvi); if ((xkey = xml_find(x, keyname)) == NULL) goto done; /* No key in xml */ if ((xb = xml_find(x, keyname)) == NULL) goto done; if (i++) cprintf(cb, ","); b = xml_body(xb); enc = NULL; if (uri_percent_encode(&enc, "%s", b) < 0) goto done; cprintf(cb, "%s", enc?enc:""); if (enc) free(enc); } break; default: break; } #if 0 { /* Just for testing */ cxobj *xc; if ((xc = xml_child_i_type(x, 0, CX_ELMNT)) != NULL) if (xml2api_path_1(xc, cb) < 0) goto done; } #endif ok: retval = 0; done: return retval; }