This commit is contained in:
Olof hagsand 2016-03-07 20:55:55 +01:00
parent ca18b7f49e
commit 0a812696c2
47 changed files with 1818 additions and 1783 deletions

View file

@ -173,56 +173,25 @@ detect_endtag(char *tag, char ch, int *state)
return retval;
}
/*
* target_locked
* return 1 if locked, 0 if not.
* return clientid if locked.
*/
int
target_locked(enum target_type target, int *client)
{
return 0;
}
/*
* unlock_target
*/
int
unlock_target(enum target_type target)
{
return 0;
}
int
lock_target(enum target_type target)
{
return 0;
}
/*! Get "target" attribute, return actual database given candidate or running
* Caller must do error handling
* @retval dbname Actual database file name
*/
char *
netconf_get_target(clicon_handle h, cxobj *xn, char *path)
netconf_get_target(clicon_handle h,
cxobj *xn,
char *path)
{
cxobj *x;
char *target = NULL;
char *running_db;
char *candidate_db;
if ((running_db = clicon_running_db(h)) == NULL)
goto done;
if ((candidate_db = clicon_candidate_db(h)) == NULL)
goto done;
if ((x = xpath_first(xn, path)) != NULL){
if (xpath_first(x, "candidate") != NULL)
target = candidate_db;
target = "candidate";
else
if (xpath_first(x, "running") != NULL)
target = running_db;
if (xpath_first(x, "running") != NULL)
target = "running";
}
done:
return target;
}

View file

@ -61,11 +61,11 @@
/*! Process incoming packet
* @param[in] h Clicon handle
* @param[in] xf Packet buffer
* @param[in] cb Packet buffer
*/
static int
process_incoming_packet(clicon_handle h,
cbuf *xf)
cbuf *cb)
{
char *str;
char *str0;
@ -76,23 +76,23 @@ process_incoming_packet(clicon_handle h,
cbuf *xf1;
clicon_debug(1, "RECV");
clicon_debug(2, "%s: RCV: \"%s\"", __FUNCTION__, cbuf_get(xf));
if ((str0 = strdup(cbuf_get(xf))) == NULL){
clicon_debug(2, "%s: RCV: \"%s\"", __FUNCTION__, cbuf_get(cb));
if ((str0 = strdup(cbuf_get(cb))) == NULL){
clicon_log(LOG_ERR, "%s: strdup: %s", __FUNCTION__, strerror(errno));
return -1;
}
str = str0;
/* Parse incoming XML message */
if (clicon_xml_parse_string(&str, &xml_req) < 0){
if ((xf = cbuf_new()) == NULL){
netconf_create_rpc_error(xf, NULL,
if ((cb = cbuf_new()) == NULL){
netconf_create_rpc_error(cb, NULL,
"operation-failed",
"rpc", "error",
NULL,
NULL);
netconf_output(1, xf, "rpc-error");
cbuf_free(xf);
netconf_output(1, cb, "rpc-error");
cbuf_free(cb);
}
else
clicon_log(LOG_ERR, "%s: cbuf_new", __FUNCTION__);
@ -181,12 +181,12 @@ netconf_input_cb(int s,
unsigned char buf[BUFSIZ];
int i;
int len;
static cbuf *xf; /* XXX: should use ce state? */
static cbuf *cb; /* XXX: should use ce state? */
int xml_state = 0;
int retval = -1;
if (xf == NULL)
if ((xf = cbuf_new()) == NULL){
if (cb == NULL)
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "%s: cbuf_new", __FUNCTION__);
return retval;
}
@ -209,22 +209,22 @@ netconf_input_cb(int s,
for (i=0; i<len; i++){
if (buf[i] == 0)
continue; /* Skip NULL chars (eg from terminals) */
cprintf(xf, "%c", buf[i]);
cprintf(cb, "%c", buf[i]);
if (detect_endtag("]]>]]>",
buf[i],
&xml_state)) {
/* OK, we have an xml string from a client */
if (process_incoming_packet(h, xf) < 0){
if (process_incoming_packet(h, cb) < 0){
goto done;
}
if (cc_closed)
break;
cbuf_reset(xf);
cbuf_reset(cb);
}
}
retval = 0;
done:
// cbuf_free(xf);
// cbuf_free(cb);
if (cc_closed)
retval = -1;
return retval;
@ -255,16 +255,15 @@ send_hello(int s)
return retval;
}
/* from init_candidate_db() and clicon_rpc_copy() */
/*! Initialize candidate database */
static int
init_candidate_db(clicon_handle h, char *running_db, char *candidate_db)
init_candidate_db(clicon_handle h)
{
struct stat sb;
int retval = -1;
/* init shared candidate */
if (lstat(candidate_db, &sb) < 0){
if (clicon_rpc_copy(h, running_db, candidate_db) < 0)
if (xmldb_exists(h, "candidate") != 1){
if (xmldb_copy(h, "running", "candidate") < 0)
goto done;
}
retval = 0;
@ -316,8 +315,6 @@ main(int argc, char **argv)
int quiet = 0;
clicon_handle h;
int use_syslog;
char *running_db;
char *candidate_db;
/* Defaults */
use_syslog = 0;
@ -390,15 +387,7 @@ main(int argc, char **argv)
if (netconf_plugin_load(h) < 0)
return -1;
if ((running_db = clicon_running_db(h)) == NULL){
clicon_err(OE_FATAL, 0, "running db not set");
goto done;
}
if ((candidate_db = clicon_candidate_db(h)) == NULL){
clicon_err(OE_FATAL, 0, "candidate db not set");
goto done;
}
if (init_candidate_db(h, running_db, candidate_db) < 0)
if (init_candidate_db(h) < 0)
return -1;
/* Call start function is all plugins before we go interactive */
tmp = *(argv-1);

View file

@ -108,14 +108,13 @@ netconf_filter_xmldb(clicon_handle h,
cxobj *xc;
cxobj *xfilterconf = NULL;
int retval = -1;
yang_spec *ys = clicon_dbspec_yang(h);
char *selector;
/* Default subtree filter */
switch (foption){
case FILTER_SUBTREE:
/* Get the whole database as xml */
if (xmldb_get(source, NULL, ys, &xdb) < 0){
if (xmldb_get(h, source, NULL, 0, &xdb, NULL, NULL) < 0){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"application",
@ -154,7 +153,7 @@ netconf_filter_xmldb(clicon_handle h,
break;
case FILTER_XPATH:
selector = xml_find_value(xfilter, "select");
if (xmldb_get(source, selector, ys, &xdb) < 0){
if (xmldb_get(h, source, selector, 0, &xdb, NULL, NULL) < 0){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"application",
@ -202,13 +201,13 @@ netconf_filter_xmldb(clicon_handle h,
<get-config>
<source>
<( candidate | running )/>
<candidate/> | <running/>
</source>
</get-config>
<get-config>
<source>
<( candidate | running )/>
<candidate/> | <running/>
</source>
<filter type="subtree">
<configuration>
@ -220,24 +219,6 @@ netconf_filter_xmldb(clicon_handle h,
<rpc><get-config><source><running /></source>
<filter type="xpath" select="//SenderTwampIpv4"/>
</get-config></rpc>]]>]]>
* Call graph, client to backend and formats
* netconf_input_cb # client
* read
* process_incoming_packet #
* clicon_xml_parse_string #
* netconf_rpc_dispatch
* netconf_get_config #
* xpath_first
* netconf_filter #
* db2xml_key # "^.*$"
* clicon_dbitems # (db)
* db_regexp # (lvec)
* lvec2cvec # (cvec)
* cvec2xml # (xml)
* clicon_xml2cbuf # (xml->char*)
* wanted:
* netconf_get_config
* xpath(db, filter)
*/
int
netconf_get_config(clicon_handle h,
@ -276,7 +257,7 @@ netconf_get_config(clicon_handle h,
/*! Get options from netconf edit-config
* <edit-config>
* <config>XXX</config>
* <config>...</config>
* <default-operation>(merge | none | replace)</default-operation>
* <error-option>(stop-on-error | continue-on-error )</error-option>
* <test-option>(set | test-then-set | test-only)</test-option>
@ -418,20 +399,12 @@ netconf_edit_config(clicon_handle h,
cxobj *xc; /* config */
cxobj *xcc; /* child of config */
char *target; /* db */
char *candidate_db;
cbuf *cbxml = NULL;
char *xmlstr;
if ((candidate_db = clicon_candidate_db(h)) == NULL){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"protocol", "error",
NULL, "Internal error");
goto done;
}
/* must have target, and it should be candidate */
if ((target = netconf_get_target(h, xn, "target")) == NULL ||
strcmp(target, candidate_db)){
strcmp(target, "candidate")){
netconf_create_rpc_error(cb_err, xorig,
"missing-element",
"protocol",
@ -532,7 +505,7 @@ netconf_copy_config(clicon_handle h,
goto done;
}
#endif
if (clicon_rpc_copy(h, source, target) < 0){
if (xmldb_copy(h, source, target) < 0){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"protocol", "error",
@ -546,7 +519,7 @@ netconf_copy_config(clicon_handle h,
return retval;
}
/*
/*! Delete configuration
<delete-config>
<target>
<candidate/>
@ -564,15 +537,6 @@ netconf_delete_config(clicon_handle h,
{
char *target; /* filenames */
int retval = -1;
char *candidate_db;
if ((candidate_db = clicon_candidate_db(h)) == NULL){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"protocol", "error",
NULL, "Internal error");
goto done;
}
if ((target = netconf_get_target(h, xn, "target")) == NULL){
netconf_create_rpc_error(cb_err, xorig,
@ -583,7 +547,7 @@ netconf_delete_config(clicon_handle h,
"<bad-element>target</bad-element>");
goto done;
}
if (strcmp(target, candidate_db)){
if (strcmp(target, "candidate")){
netconf_create_rpc_error(cb_err, xorig,
"bad-element",
"protocol",
@ -592,14 +556,14 @@ netconf_delete_config(clicon_handle h,
"<bad-element>target</bad-element>");
goto done;
}
if (clicon_rpc_rm(h, target) < 0){
if (xmldb_delete(h, target) < 0){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"protocol", "error",
NULL, "Internal error");
goto done;
}
if (clicon_rpc_initdb(h, target) < 0){
if (xmldb_init(h, target) < 0){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"protocol", "error",
@ -612,7 +576,7 @@ netconf_delete_config(clicon_handle h,
return retval;
}
/*
/*! Close a (user) session
<close-session/>
*/
int
@ -626,12 +590,13 @@ netconf_close_session(cxobj *xn,
return 0;
}
/*
/*! Lock a database
<lock>
<target>
<candidate/>
</target>
</lock>
XXX
*/
int
netconf_lock(clicon_handle h,
@ -652,40 +617,20 @@ netconf_lock(clicon_handle h,
"<bad-element>source</bad-element>");
goto done;
}
#ifdef notyet
if (target_locked(&client) > 0){
snprintf(info, 64, "<session-id>%d</session-id>", client);
netconf_create_rpc_error(cb_err, xorig,
"lock-denied",
"protocol",
"error",
"Lock failed, lock is already held",
info);
return -1;
}
if (lock_target(target) < 0){
netconf_create_rpc_error(cb_err, xorig,
"lock-denied",
"protocol",
"error",
"Lock failed, lock is already held",
NULL);
return -1;
}
#endif
netconf_ok_set(1);
retval = 0;
done:
return retval;
}
/*
/*! Unlock a database
<unlock>
<target>
<candidate/>
</target>
</unlock>
XXX
*/
int
netconf_unlock(clicon_handle h,
@ -705,47 +650,18 @@ netconf_unlock(clicon_handle h,
"<bad-element>target</bad-element>");
goto done;
}
#ifdef notyet
if (target_locked(&client) == 0){
netconf_create_rpc_error(cb_err, xorig,
"lock-denied",
"protocol",
"error",
"Unlock failed, lock is not held",
NULL);
return -1;
}
if (client != ce_nr){
snprintf(info, 64, "<session-id>%d</session-id>", client);
netconf_create_rpc_error(cb_err, xorig,
"lock-denied",
"protocol",
"error",
"Unlock failed, lock is held by other entity",
info);
return -1;
}
if (unlock_target(target) < 0){
netconf_create_rpc_error(cb_err, xorig,
"lock-denied",
"protocol",
"error",
"Unlock failed, unkown reason",
NULL);
return -1;
}
#endif /* notyet */
/* XXX notyet */
netconf_ok_set(1);
retval = 0;
done:
return retval;
}
/*
/*! Kill other user sessions
<kill-session>
<session-id>PID</session-id>
</kill-session>
XXX
*/
int
netconf_kill_session(cxobj *xn, cbuf *cb, cbuf *cb_err, cxobj *xorig)
@ -793,7 +709,7 @@ netconf_kill_session(cxobj *xn, cbuf *cb, cbuf *cb_err, cxobj *xorig)
return 0;
}
/*
/*! Commit candidate -> running
<commit/>
:candidate
*/
@ -805,25 +721,8 @@ netconf_commit(clicon_handle h,
cxobj *xorig)
{
int retval = -1;
char *candidate_db;
char *running_db;
if ((candidate_db = clicon_candidate_db(h)) == NULL){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"protocol", "error",
NULL, "Internal error: candidate not set");
goto done;
}
if ((running_db = clicon_running_db(h)) == NULL){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"protocol", "error",
NULL, "Internal error: running not set");
goto done;
}
if (clicon_rpc_commit(h, candidate_db,
running_db,
if (clicon_rpc_commit(h, "candidate", "running",
1, 1) < 0){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
@ -838,7 +737,7 @@ netconf_commit(clicon_handle h,
return retval;
}
/*
/*! Discard all changes in candidate / revert to running
<discard-changes/>
:candidate
*/
@ -850,24 +749,8 @@ netconf_discard_changes(clicon_handle h,
cxobj *xorig)
{
int retval = -1;
char *running_db;
char *candidate_db;
if ((running_db = clicon_running_db(h)) == NULL){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"protocol", "error",
NULL, "Internal error: running not set");
goto done;
}
if ((candidate_db = clicon_candidate_db(h)) == NULL){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"protocol", "error",
NULL, "Internal error");
goto done;
}
if (clicon_rpc_copy(h, running_db, candidate_db) < 0){
if (xmldb_copy(h, "running", "candidate") < 0){
netconf_create_rpc_error(cb_err, xorig,
"operation-failed",
"protocol", "error",
@ -881,7 +764,7 @@ netconf_discard_changes(clicon_handle h,
return retval;
}
/*
/*! Check the semantic consistency of candidate
<validate/>
:validate
*/