307 lines
8.2 KiB
C
307 lines
8.2 KiB
C
/*
|
|
*
|
|
***** BEGIN LICENSE BLOCK *****
|
|
|
|
Copyright (C) 2009-2017 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 *****
|
|
|
|
* Examples:
|
|
|
|
./datastore_client -d candidate -b /usr/local/var/routing -p /home/olof/src/clixon/datastore/keyvalue/keyvalue.so -y /usr/local/share/routing/yang -m ietf-ip get /
|
|
|
|
sudo ./datastore_client -d candidate -b /usr/local/var/routing -p /home/olof/src/clixon/datastore/keyvalue/keyvalue.so -y /usr/local/share/routing/yang -m ietf-ip put merge /interfaces/interface=eth66 '<config>eth66</config>'
|
|
|
|
sudo ./datastore_client -d candidate -b /usr/local/var/routing -p /home/olof/src/clixon/datastore/keyvalue/keyvalue.so -y /usr/local/share/routing/yang -m ietf-ip put merge / '<config><interfaces><interface><name>eth0</name><enabled>true</enabled></interface></interfaces></config>'
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "clixon_config.h" /* generated by config & autoconf */
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <stdarg.h>
|
|
#include <errno.h>
|
|
#include <signal.h>
|
|
#include <fcntl.h>
|
|
#include <time.h>
|
|
#include <syslog.h>
|
|
#include <sys/time.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/param.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
|
|
/* cligen */
|
|
#include <cligen/cligen.h>
|
|
|
|
/* clicon */
|
|
#include <clixon/clixon.h>
|
|
|
|
/* Command line options to be passed to getopt(3) */
|
|
#define DATASTORE_OPTS "hDd:p:b:y:m:"
|
|
|
|
/*! usage
|
|
*/
|
|
static void
|
|
usage(char *argv0)
|
|
{
|
|
fprintf(stderr, "usage:%s <options>* [<command>]\n"
|
|
"where options are\n"
|
|
"\t-h\t\tHelp\n"
|
|
"\t-D\t\tDebug\n"
|
|
"\t-d <db>\t\tDatabase name. Default: running. Alt: candidate,startup\n"
|
|
"\t-b <dir>\tDatabase directory. Mandatory\n"
|
|
"\t-p <plugin>\tDatastore plugin. Mandatory\n"
|
|
"\t-y <dir>\tYang directory (where modules are stored). Mandatory\n"
|
|
"\t-m <module>\tYang module. Mandatory\n"
|
|
"and command is either:\n"
|
|
"\tget <xpath>\n"
|
|
"\tput (merge|replace|create|delete|remove) <xml>\n"
|
|
"\tcopy <todb>\n"
|
|
"\tlock <pid>\n"
|
|
"\tunlock\n"
|
|
"\tunlock_all <pid>\n"
|
|
"\tislocked\n"
|
|
"\texists\n"
|
|
"\tdelete\n"
|
|
"\tinit\n"
|
|
,
|
|
argv0
|
|
);
|
|
exit(0);
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
char c;
|
|
clicon_handle h;
|
|
char *argv0;
|
|
char *db = "running";
|
|
char *plugin = NULL;
|
|
char *cmd = NULL;
|
|
yang_spec *yspec = NULL;
|
|
char *yangdir = NULL;
|
|
char *yangmodule = NULL;
|
|
char *dbdir = NULL;
|
|
int ret;
|
|
int pid;
|
|
enum operation_type op;
|
|
cxobj *xt = NULL;
|
|
|
|
/* In the startup, logs to stderr & debug flag set later */
|
|
clicon_log_init(__PROGRAM__, LOG_INFO, CLICON_LOG_STDERR);
|
|
|
|
argv0 = argv[0];
|
|
/* Defaults */
|
|
if ((h = clicon_handle_init()) == NULL)
|
|
goto done;
|
|
/* getopt in two steps, first find config-file before over-riding options. */
|
|
while ((c = getopt(argc, argv, DATASTORE_OPTS)) != -1)
|
|
switch (c) {
|
|
case '?' :
|
|
case 'h' : /* help */
|
|
usage(argv0);
|
|
break;
|
|
case 'D' : /* debug */
|
|
debug = 1;
|
|
break;
|
|
case 'd': /* db symbolic: running|candidate|startup */
|
|
if (!optarg)
|
|
usage(argv0);
|
|
db = optarg;
|
|
break;
|
|
case 'p': /* datastore plugin */
|
|
if (!optarg)
|
|
usage(argv0);
|
|
plugin = optarg;
|
|
break;
|
|
case 'b': /* db directory */
|
|
if (!optarg)
|
|
usage(argv0);
|
|
dbdir = optarg;
|
|
break;
|
|
case 'y': /* Yang directory */
|
|
if (!optarg)
|
|
usage(argv0);
|
|
yangdir = optarg;
|
|
break;
|
|
case 'm': /* Yang module */
|
|
if (!optarg)
|
|
usage(argv0);
|
|
yangmodule = optarg;
|
|
break;
|
|
}
|
|
/*
|
|
* Logs, error and debug to stderr, set debug level
|
|
*/
|
|
clicon_log_init(__PROGRAM__, debug?LOG_DEBUG:LOG_INFO, CLICON_LOG_STDERR);
|
|
clicon_debug_init(debug, NULL);
|
|
|
|
argc -= optind;
|
|
argv += optind;
|
|
if (argc < 1)
|
|
usage(argv0);
|
|
cmd = argv[0];
|
|
if (plugin == NULL){
|
|
clicon_err(OE_DB, 0, "Missing plugin -p option");
|
|
goto done;
|
|
}
|
|
if (dbdir == NULL){
|
|
clicon_err(OE_DB, 0, "Missing dbdir -b option");
|
|
goto done;
|
|
}
|
|
if (yangdir == NULL){
|
|
clicon_err(OE_YANG, 0, "Missing yangdir -y option");
|
|
goto done;
|
|
}
|
|
if (yangmodule == NULL){
|
|
clicon_err(OE_YANG, 0, "Missing yang module -m option");
|
|
goto done;
|
|
}
|
|
/* Load datastore plugin */
|
|
if (xmldb_plugin_load(h, plugin) < 0)
|
|
goto done;
|
|
/* Connect to plugin to get a handle */
|
|
if (xmldb_connect(h) < 0)
|
|
goto done;
|
|
/* Create yang spec */
|
|
if ((yspec = yspec_new()) == NULL)
|
|
goto done;
|
|
/* Parse yang spec from given file */
|
|
if (yang_parse(h, yangdir, yangmodule, NULL, yspec) < 0)
|
|
goto done;
|
|
/* Set database directory option */
|
|
if (xmldb_setopt(h, "dbdir", dbdir) < 0)
|
|
goto done;
|
|
/* Set yang spec option */
|
|
if (xmldb_setopt(h, "yangspec", yspec) < 0)
|
|
goto done;
|
|
if (strcmp(cmd, "get")==0){
|
|
if (argc != 1 && argc != 2)
|
|
usage(argv0);
|
|
if (xmldb_get(h, db, argc==2?argv[1]:"/", &xt) < 0)
|
|
goto done;
|
|
clicon_xml2file(stdout, xt, 0, 0);
|
|
|
|
fprintf(stdout, "\n");
|
|
}
|
|
else if (strcmp(cmd, "put")==0){
|
|
if (argc != 3){
|
|
clicon_err(OE_DB, 0, "Unexpected nr of args: %d", argc);
|
|
usage(argv0);
|
|
}
|
|
if (xml_operation(argv[1], &op) < 0){
|
|
clicon_err(OE_DB, 0, "Unrecognized operation: %s", argv[1]);
|
|
usage(argv0);
|
|
}
|
|
if (clicon_xml_parse_str(argv[2], &xt) < 0)
|
|
goto done;
|
|
if (xml_rootchild(xt, 0, &xt) < 0)
|
|
goto done;
|
|
if (xmldb_put(h, db, op, xt) < 0)
|
|
goto done;
|
|
}
|
|
else if (strcmp(cmd, "copy")==0){
|
|
if (argc != 2)
|
|
usage(argv0);
|
|
if (xmldb_copy(h, db, argv[1]) < 0)
|
|
goto done;
|
|
}
|
|
else if (strcmp(cmd, "lock")==0){
|
|
if (argc != 2)
|
|
usage(argv0);
|
|
pid = atoi(argv[1]);
|
|
if (xmldb_lock(h, db, pid) < 0)
|
|
goto done;
|
|
}
|
|
else if (strcmp(cmd, "unlock")==0){
|
|
if (argc != 1)
|
|
usage(argv0);
|
|
if (xmldb_unlock(h, db) < 0)
|
|
goto done;
|
|
}
|
|
else if (strcmp(cmd, "unlock_all")==0){
|
|
if (argc != 2)
|
|
usage(argv0);
|
|
pid = atoi(argv[1]);
|
|
if (xmldb_unlock_all(h, pid) < 0)
|
|
goto done;
|
|
}
|
|
else if (strcmp(cmd, "islocked")==0){
|
|
if (argc != 1)
|
|
usage(argv0);
|
|
if ((ret = xmldb_islocked(h, db)) < 0)
|
|
goto done;
|
|
fprintf(stdout, "islocked: %d\n", ret);
|
|
}
|
|
else if (strcmp(cmd, "exists")==0){
|
|
if (argc != 1)
|
|
usage(argv0);
|
|
if ((ret = xmldb_exists(h, db)) < 0)
|
|
goto done;
|
|
fprintf(stdout, "exists: %d\n", ret);
|
|
}
|
|
else if (strcmp(cmd, "delete")==0){
|
|
if (argc != 1)
|
|
usage(argv0);
|
|
if (xmldb_delete(h, db) < 0)
|
|
goto done;
|
|
}
|
|
else if (strcmp(cmd, "init")==0){
|
|
if (argc != 1)
|
|
usage(argv0);
|
|
if (xmldb_create(h, db) < 0)
|
|
goto done;
|
|
}
|
|
else{
|
|
clicon_err(OE_DB, 0, "Unrecognized command: %s", cmd);
|
|
usage(argv0);
|
|
}
|
|
if (xmldb_disconnect(h) < 0)
|
|
goto done;
|
|
if (xmldb_plugin_unload(h) < 0)
|
|
goto done;
|
|
done:
|
|
if (xt)
|
|
xml_free(xt);
|
|
if (h)
|
|
clicon_handle_exit(h);
|
|
if (yspec)
|
|
yspec_free(yspec);
|
|
return 0;
|
|
}
|
|
|