* All uses of `api_path2xpath_cvv()` should be replaced by `api_path2xpath()`
  * `api_path2xpath()` added an `xerr` argument.
This commit is contained in:
Olof hagsand 2020-03-10 22:37:16 +01:00
parent 0e94937ccf
commit adbb683329
20 changed files with 191 additions and 153 deletions

View file

@ -152,7 +152,7 @@ expand_dbvar(void *h,
*/
if (api_path_fmt2api_path(api_path_fmt, cvv, &api_path) < 0)
goto done;
if (api_path2xpath(api_path, yspec, &xpath, &nsc) < 0)
if (api_path2xpath(api_path, yspec, &xpath, &nsc, NULL) < 0)
goto done;
/* Get configuration */
@ -714,7 +714,7 @@ cli_show_auto1(clicon_handle h,
}
if (api_path_fmt2api_path(api_path_fmt, cvv, &api_path) < 0)
goto done;
if (api_path2xpath(api_path, yspec, &xpath, &nsc) < 0)
if (api_path2xpath(api_path, yspec, &xpath, &nsc, NULL) < 0)
goto done;
/* XXX Kludge to overcome a trailing / in show, that I cannot add to
* yang2api_path_fmt_1 where it should belong.

View file

@ -708,7 +708,7 @@ restconf_insert_attributes(cxobj *xdata,
if (xml_prefix_set(xa, "yang") < 0)
goto done;
xml_type_set(xa, CX_ATTR);
if ((ret = api_path2xpath(pstr, ys_spec(y), &xpath, &nsc)) < 0)
if ((ret = api_path2xpath(pstr, ys_spec(y), &xpath, &nsc, NULL)) < 0)
goto done;
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");

View file

@ -124,11 +124,11 @@ api_data(clicon_handle h,
if (strcmp(request_method, "OPTIONS")==0)
retval = api_data_options(h, r);
else if (strcmp(request_method, "HEAD")==0)
retval = api_data_head(h, r, pcvec, pi, qvec, pretty, media_out);
retval = api_data_head(h, r, api_path, pcvec, pi, qvec, pretty, media_out);
else if (strcmp(request_method, "GET")==0)
retval = api_data_get(h, r, pcvec, pi, qvec, pretty, media_out);
retval = api_data_get(h, r, api_path, pcvec, pi, qvec, pretty, media_out);
else if (strcmp(request_method, "POST")==0)
retval = api_data_post(h, r, api_path, pcvec, pi, qvec, data, pretty, media_out);
retval = api_data_post(h, r, api_path, pi, qvec, data, pretty, media_out);
else if (strcmp(request_method, "PUT")==0)
retval = api_data_put(h, r, api_path, pcvec, pi, qvec, data, pretty, media_out);
else if (strcmp(request_method, "PATCH")==0)
@ -169,9 +169,9 @@ api_operations(clicon_handle h,
request_method = FCGX_GetParam("REQUEST_METHOD", r->envp);
clicon_debug(1, "%s method:%s", __FUNCTION__, request_method);
if (strcmp(request_method, "GET")==0)
retval = api_operations_get(h, r, path, pcvec, pi, qvec, data, pretty, media_out);
retval = api_operations_get(h, r, path, pi, qvec, data, pretty, media_out);
else if (strcmp(request_method, "POST")==0)
retval = api_operations_post(h, r, path, pcvec, pi, qvec, data,
retval = api_operations_post(h, r, path, pi, qvec, data,
pretty, media_out);
else
retval = restconf_notfound(r);

View file

@ -261,9 +261,9 @@ api_data_write(clicon_handle h,
int ret;
char *namespace = NULL;
char *dname;
cbuf *cbpath = NULL;
cvec *nsc = NULL;
enum yang_bind yb;
char *xpath = NULL;
clicon_debug(1, "%s api_path:\"%s\"", __FUNCTION__, api_path0);
clicon_debug(1, "%s data:\"%s\"", __FUNCTION__, data);
@ -272,28 +272,27 @@ api_data_write(clicon_handle h,
goto done;
}
api_path=api_path0;
/* strip /... from start */
for (i=0; i<pi; i++)
api_path = index(api_path+1, '/');
/* Check if object exists in backend.
* Translate api-path to xpath */
if ((cbpath = cbuf_new()) == NULL)
goto done;
cprintf(cbpath, "/");
if ((ret = api_path2xpath_cvv(pcvec, pi, yspec, cbpath, &nsc, &xerr)) < 0)
goto done;
if (ret == 0){
if ((xe = xpath_first(xerr, NULL, "rpc-error")) == NULL){
clicon_err(OE_XML, EINVAL, "rpc-error not found (internal error)");
if (api_path){
/* Translate api-path to xpath: xpath (cbpath) and namespace context (nsc) */
if ((ret = api_path2xpath(api_path, yspec, &xpath, &nsc, &xerr)) < 0)
goto done;
if (ret == 0){ /* validation failed */
if ((xe = xpath_first(xerr, NULL, "rpc-error")) == NULL){
clicon_err(OE_XML, EINVAL, "rpc-error not found (internal error)");
goto done;
}
if (api_return_err(h, r, xe, pretty, media_out, 0) < 0)
goto done;
goto ok;
}
if (api_return_err(h, r, xe, pretty, media_out, 0) < 0)
goto done;
goto ok;
}
xret = NULL;
if (clicon_rpc_get_config(h, clicon_nacm_recovery_user(h),
"candidate", cbuf_get(cbpath), nsc, &xret) < 0){
"candidate", xpath, nsc, &xret) < 0){
if (netconf_operation_failed_xml(&xerr, "protocol", clicon_err_reason) < 0)
goto done;
if ((xe = xpath_first(xerr, NULL, "rpc-error")) == NULL){
@ -672,10 +671,10 @@ api_data_write(clicon_handle h,
retval = 0;
done:
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
if (xpath)
free(xpath);
if (nsc)
xml_nsctx_free(nsc);
if (cbpath)
cbuf_free(cbpath);
if (xret)
xml_free(xret);
if (xerr)

View file

@ -66,6 +66,7 @@
* According to restconf
* @param[in] h Clixon handle
* @param[in] r Fastcgi request handle
* @param[in] api_path According to restconf (Sec 3.5.3.1 in rfc8040)
* @param[in] pcvec Vector of path ie DOCUMENT_URI element
* @param[in] pi Offset, where path starts
* @param[in] qvec Vector of query string (QUERY_STRING)
@ -90,17 +91,17 @@
* Netconf: <get-config>, <get>
*/
static int
api_data_get2(clicon_handle h,
FCGX_Request *r,
cvec *pcvec,
int pi,
cvec *qvec,
int pretty,
api_data_get2(clicon_handle h,
FCGX_Request *r,
char *api_path,
cvec *pcvec, /* XXX remove? */
int pi,
cvec *qvec,
int pretty,
restconf_media media_out,
int head)
int head)
{
int retval = -1;
cbuf *cbpath = NULL;
char *xpath = NULL;
cbuf *cbx = NULL;
yang_stmt *yspec;
@ -117,12 +118,41 @@ api_data_get2(clicon_handle h,
char *attr; /* attribute value string */
netconf_content content = CONTENT_ALL;
int32_t depth = -1; /* Nr of levels to print, -1 is all, 0 is none */
cxobj *xtop = NULL;
cxobj *xbot = NULL;
yang_stmt *y = NULL;
clicon_debug(1, "%s", __FUNCTION__);
if ((yspec = clicon_dbspec_yang(h)) == NULL){
clicon_err(OE_FATAL, 0, "No DB_SPEC");
goto done;
}
/* strip /... from start */
for (i=0; i<pi; i++)
api_path = index(api_path+1, '/');
if (api_path){
if ((xtop = xml_new("top", NULL, NULL)) == NULL)
goto done;
/* Translate api-path to xml, but to validate the api-path, note: strict=1
* xtop and xbot unnecessary fir this function but neede by function
*/
if ((ret = api_path2xml(api_path, yspec, xtop, YC_DATANODE, 1, &xbot, &y, &xerr)) < 0)
goto done;
/* Translate api-path to xpath: xpath (cbpath) and namespace context (nsc) */
if (ret != 0 &&
(ret = api_path2xpath(api_path, yspec, &xpath, &nsc, &xerr)) < 0)
goto done;
if (ret == 0){ /* validation failed */
if ((xe = xpath_first(xerr, NULL, "rpc-error")) == NULL){
clicon_err(OE_XML, EINVAL, "rpc-error not found (internal error)");
goto done;
}
if (api_return_err(h, r, xe, pretty, media_out, 0) < 0)
goto done;
goto ok;
}
}
/* Check for content attribute */
if ((attr = cvec_find_str(qvec, "content")) != NULL){
clicon_debug(1, "%s content=%s", __FUNCTION__, attr);
@ -162,25 +192,7 @@ api_data_get2(clicon_handle h,
}
}
}
if ((cbpath = cbuf_new()) == NULL)
goto done;
cprintf(cbpath, "/");
/* We know "data" is element pi-1.
* Translate api-path to xpath: xpath (cbpath) and namespace context (nsc)
*/
if ((ret = api_path2xpath_cvv(pcvec, pi, yspec, cbpath, &nsc, &xerr)) < 0)
goto done;
if (ret == 0){
if ((xe = xpath_first(xerr, NULL, "rpc-error")) == NULL){
clicon_err(OE_XML, EINVAL, "rpc-error not found (internal error)");
goto done;
}
if (api_return_err(h, r, xe, pretty, media_out, 0) < 0)
goto done;
goto ok;
}
xpath = cbuf_get(cbpath);
clicon_debug(1, "%s path:%s", __FUNCTION__, xpath);
switch (content){
case CONTENT_CONFIG:
@ -306,12 +318,14 @@ api_data_get2(clicon_handle h,
retval = 0;
done:
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
if (xpath)
free(xpath);
if (nsc)
xml_nsctx_free(nsc);
if (xtop)
xml_free(xtop);
if (cbx)
cbuf_free(cbx);
if (cbpath)
cbuf_free(cbpath);
if (xret)
xml_free(xret);
if (xerr)
@ -324,6 +338,7 @@ api_data_get2(clicon_handle h,
/*! REST HEAD method
* @param[in] h Clixon handle
* @param[in] r Fastcgi request handle
* @param[in] api_path According to restconf (Sec 3.5.3.1 in rfc8040)
* @param[in] pcvec Vector of path ie DOCUMENT_URI element
* @param[in] pi Offset, where path starts
* @param[in] qvec Vector of query string (QUERY_STRING)
@ -338,19 +353,21 @@ api_data_get2(clicon_handle h,
int
api_data_head(clicon_handle h,
FCGX_Request *r,
char *api_path,
cvec *pcvec,
int pi,
cvec *qvec,
int pretty,
restconf_media media_out)
{
return api_data_get2(h, r, pcvec, pi, qvec, pretty, media_out, 1);
return api_data_get2(h, r, api_path, pcvec, pi, qvec, pretty, media_out, 1);
}
/*! REST GET method
* According to restconf
* @param[in] h Clixon handle
* @param[in] r Fastcgi request handle
* @param[in] api_path According to restconf (Sec 3.5.3.1 in rfc8040)
* @param[in] pcvec Vector of path ie DOCUMENT_URI element
* @param[in] pi Offset, where path starts
* @param[in] qvec Vector of query string (QUERY_STRING)
@ -375,13 +392,14 @@ api_data_head(clicon_handle h,
int
api_data_get(clicon_handle h,
FCGX_Request *r,
char *api_path,
cvec *pcvec,
int pi,
cvec *qvec,
int pretty,
restconf_media media_out)
{
return api_data_get2(h, r, pcvec, pi, qvec, pretty, media_out, 0);
return api_data_get2(h, r, api_path, pcvec, pi, qvec, pretty, media_out, 0);
}
/*! GET restconf/operations resource
@ -413,7 +431,6 @@ int
api_operations_get(clicon_handle h,
FCGX_Request *r,
char *path,
cvec *pcvec,
int pi,
cvec *qvec,
char *data,

View file

@ -40,13 +40,12 @@
/*
* Prototypes
*/
int api_data_head(clicon_handle h, FCGX_Request *r, cvec *pcvec, int pi,
int api_data_head(clicon_handle h, FCGX_Request *r, char *api_path, cvec *pcvec, int pi,
cvec *qvec, int pretty, restconf_media media_out);
int api_data_get(clicon_handle h, FCGX_Request *r, cvec *pcvec, int pi,
int api_data_get(clicon_handle h, FCGX_Request *r, char *api_path, cvec *pcvec, int pi,
cvec *qvec, int pretty, restconf_media media_out);
int api_operations_get(clicon_handle h, FCGX_Request *r,
char *path,
cvec *pcvec, int pi, cvec *qvec, char *data,
char *api_path, int pi, cvec *qvec, char *data,
int pretty, restconf_media media_out);
#endif /* _RESTCONF_METHODS_GET_H_ */

View file

@ -100,7 +100,6 @@ int
api_data_post(clicon_handle h,
FCGX_Request *r,
char *api_path,
cvec *pcvec,
int pi,
cvec *qvec,
char *data,
@ -145,6 +144,7 @@ api_data_post(clicon_handle h,
/* Translate api_path to xtop/xbot */
xbot = xtop;
if (api_path){
/* Translate api-path to xml, side-effect: validate the api-path, note: strict=1 */
if ((ret = api_path2xml(api_path, yspec, xtop, YC_DATANODE, 1, &xbot, &ybot, &xerr)) < 0)
goto done;
if (ret == 0){ /* validation failed */
@ -609,7 +609,7 @@ api_operations_post_output(clicon_handle h,
* (1) Does not handle <ok/> properly
* (2) Uncertain how validation errors should be logged/handled
*/
if (youtput!=NULL){
if (youtput != NULL){
xml_spec_set(xoutput, youtput); /* needed for xml_spec_populate */
#if 0
if (xml_spec_populate(xoutput, yspec, NULL) < 0)
@ -675,9 +675,7 @@ api_operations_post_output(clicon_handle h,
/*! REST operation POST method
* @param[in] h CLIXON handle
* @param[in] r Fastcgi request handle
* @param[in] path According to restconf (Sec 3.5.1.1 in [draft])
* @param[in] pcvec Vector of path ie DOCUMENT_URI element
* @param[in] pi Offset, where to start pcvec
* @param[in] api_path According to restconf (Sec 3.5.3.1 in rfc8040)
* @param[in] qvec Vector of query string (QUERY_STRING)
* @param[in] data Stream input data
* @param[in] pretty Set to 1 for pretty-printed xml/json output
@ -706,8 +704,7 @@ api_operations_post_output(clicon_handle h,
int
api_operations_post(clicon_handle h,
FCGX_Request *r,
char *path,
cvec *pcvec,
char *api_path,
int pi,
cvec *qvec,
char *data,
@ -716,7 +713,7 @@ api_operations_post(clicon_handle h,
{
int retval = -1;
int i;
char *oppath = path;
char *oppath = api_path;
yang_stmt *yspec;
yang_stmt *youtput = NULL;
yang_stmt *yrpc = NULL;
@ -736,7 +733,7 @@ api_operations_post(clicon_handle h,
yang_stmt *ys = NULL;
char *namespace = NULL;
clicon_debug(1, "%s json:\"%s\" path:\"%s\"", __FUNCTION__, data, path);
clicon_debug(1, "%s json:\"%s\" path:\"%s\"", __FUNCTION__, data, api_path);
/* 1. Initialize */
if ((yspec = clicon_dbspec_yang(h)) == NULL){
clicon_err(OE_FATAL, 0, "No DB_SPEC");

View file

@ -42,14 +42,13 @@
* Prototypes
*/
int api_data_post(clicon_handle h, FCGX_Request *r, char *api_path,
cvec *pcvec, int pi,
int pi,
cvec *qvec, char *data,
int pretty,
restconf_media media_out);
int api_operations_post(clicon_handle h, FCGX_Request *r,
char *path,
cvec *pcvec, int pi, cvec *qvec, char *data,
int api_operations_post(clicon_handle h, FCGX_Request *r, char *api_path,
int pi, cvec *qvec, char *data,
int pretty, restconf_media media_out);