/**
 * @file netconf.c
 * @author Radek Krejci <rkrejci@cesnet.cz>
 * @brief Python3 bindings for libnetconf2 (client-side)
 *
 * Copyright (c) 2017 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

/* Python API header */
#include <Python.h>

/* standard headers */
#include <nc_client.h>
#include <syslog.h>

#include "netconf.h"

PyObject *libnetconf2Error;
PyObject *libnetconf2Warning;
PyObject *libnetconf2ReplyError;

/* syslog usage flag */
static int syslogEnabled = 0;

static void
clb_print(NC_VERB_LEVEL level, const char* msg)
{
	switch (level) {
	case NC_VERB_ERROR:
		PyErr_SetString(libnetconf2Error, msg);
		if (syslogEnabled) {syslog(LOG_ERR, "%s", msg);}
		break;
	case NC_VERB_WARNING:
		if (syslogEnabled) {syslog(LOG_WARNING, "%s", msg);}
		PyErr_WarnEx(libnetconf2Warning, msg, 1);
		break;
	case NC_VERB_VERBOSE:
		if (syslogEnabled) {syslog(LOG_INFO, "%s", msg);}
		break;
	case NC_VERB_DEBUG:
		if (syslogEnabled) {syslog(LOG_DEBUG, "%s", msg);}
		break;
	}
}

static PyObject *
setSyslog(PyObject *self, PyObject *args, PyObject *keywds)
{
	char* name = NULL;
	static char* logname = NULL;
	static int option = LOG_PID;
	static int facility = LOG_USER;

	static char *kwlist[] = {"enabled", "name", "option", "facility", NULL};

	if (! PyArg_ParseTupleAndKeywords(args, keywds, "p|sii", kwlist, &syslogEnabled, &name, &option, &facility)) {
		return NULL;
	}

	if (name) {
		free(logname);
		logname = strdup(name);
	} else {
		free(logname);
		logname = NULL;
	}
	closelog();
	openlog(logname, option, facility);

	Py_RETURN_NONE;
}

static PyObject *
setVerbosity(PyObject *self, PyObject *args, PyObject *keywds)
{
	int level = NC_VERB_ERROR; /* 0 */

	static char *kwlist[] = {"level", NULL};

	if (! PyArg_ParseTupleAndKeywords(args, keywds, "i", kwlist, &level)) {
		return NULL;
	}

	/* normalize level value if not from the enum */
	if (level < NC_VERB_ERROR) {
		nc_verbosity(NC_VERB_ERROR);
	} else if (level > NC_VERB_DEBUG) {
		nc_verbosity(NC_VERB_DEBUG);
	} else {
		nc_verbosity(level);
	}

	Py_RETURN_NONE;
}

static PyObject *
setSearchpath(PyObject *self, PyObject *args, PyObject *keywds)
{
    char *path;
    static char *kwlist[] = {"path", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, keywds, "s", kwlist, &path)) {
        return NULL;
    }

    if (nc_client_set_schema_searchpath(path)) {
        return NULL;
    }

    Py_RETURN_NONE;
}

static PyMethodDef netconf2Methods[] = {
		{"setVerbosity", (PyCFunction)setVerbosity, METH_VARARGS | METH_KEYWORDS,
		 "setVerbosity(level)\n--\n\n"
		 "Set verbose level\n\n"
		 ":param level: Verbosity level (0 - errors, 1 - warnings, 2 - verbose, 3 - debug)\n"
		 ":type level: int\n"
		 ":returns: None\n"},
		{"setSyslog", (PyCFunction)setSyslog, METH_VARARGS | METH_KEYWORDS,
		 "setSyslog(enabled[, name=None][, option=LOG_PID][, facility=LOG_USER])\n--\n\n"
		 "Set application settings for syslog.\n\n"
		 ":param enabled: Flag to enable/disable logging into syslog.\n"
		 ":type enabled: bool\n"
		 ":param name: Identifier (program name is set by default).\n"
		 ":type name: string\n"
		 ":param option: ORed value of syslog options (LOG_PID by default).\n"
		 ":type option: int\n"
		 ":param facility: Type of the program logging the message (LOG_USER by default).\n"
		 ":type facility: int\n"
		 ":returns: None\n\n"
		 ".. seealso:: syslog.openlog\n"},
		{"setSearchpath", (PyCFunction)setSearchpath, METH_VARARGS | METH_KEYWORDS,
		 "setSearchpath(path)\n--\n\n"
		 "Set location where YANG/YIN schemas are searched and where the schemas\n"
		 "retrieved via <get-schema> opration are stored.\n\n"
		 ":param path: Search directory.\n"
		 ":type path: string\n"
		 ":returns: None\n"},
		{NULL, NULL, 0, NULL}
};

static char netconf2Docs[] =
    "NETCONF Protocol client-side implementation using libnetconf2\n"
    "\n"
    "netconf2 is a wrapper around libnetconf2 functions designed for NETCONF\n"
    "clients. it provides a higher level API than the original libnetconf2 to\n"
    "better fit the usage in Python.\n";

static struct PyModuleDef ncModule = {
		PyModuleDef_HEAD_INIT,
		"netconf2",
		netconf2Docs,
		-1,
		netconf2Methods,
};

/* module initializer */
PyMODINIT_FUNC
PyInit_netconf2(void)
{
	PyObject *nc;

    /* import libyang Python module to have it available */
    if (!PyImport_ImportModule("libyang")) {
        return NULL;
    }

	/* initiate libnetconf2 client part */
	nc_client_init();

	/* set schema searchpath
	 * nc_client_set_schema_searchpath()
	 */

	/* set print callback */
	nc_set_print_clb(clb_print);

    if (PyType_Ready(&ncSessionType) == -1) {
        return NULL;
    }

    ncSSHType.tp_new = PyType_GenericNew;
    if (PyType_Ready(&ncSSHType) == -1) {
        return NULL;
    }
/*
    ncTLSType.tp_new = PyType_GenericNew;
    if (PyType_Ready(&ncTLSType) == -1) {
        return NULL;
    }
*/
    ncErrType.tp_new = PyType_GenericNew;
    if (PyType_Ready(&ncErrType) == -1) {
        return NULL;
    }

	/* create netconf as the Python module */
	nc = PyModule_Create(&ncModule);
	if (nc == NULL) {
		return NULL;
	}

    Py_INCREF(&ncSSHType);
    PyModule_AddObject(nc, "SSH", (PyObject *)&ncSSHType);
/*
    Py_INCREF(&ncTLSType);
    PyModule_AddObject(nc, "TLS", (PyObject *)&ncTLSType);
*/
    Py_INCREF(&ncErrType);
    PyModule_AddObject(nc, "ReplyErrorInfo", (PyObject *)&ncErrType);

    Py_INCREF(&ncSessionType);
    PyModule_AddObject(nc, "Session", (PyObject *)&ncSessionType);

/*
    Py_INCREF(&ncTLSType);
    PyModule_AddObject(nc, "TLS", (PyObject *)&ncTLSType);
*/
/*
    PyModule_AddStringConstant(nc, "NETCONFv1_0", NETCONF_CAP_BASE10);
    PyModule_AddStringConstant(nc, "NETCONFv1_1", NETCONF_CAP_BASE11);
    PyModule_AddStringConstant(nc, "TRANSPORT_SSH", NETCONF_TRANSPORT_SSH);
    PyModule_AddStringConstant(nc, "TRANSPORT_TLS", NETCONF_TRANSPORT_TLS);
*/
    PyModule_AddIntConstant(nc, "DATASTORE_RUNNING", NC_DATASTORE_RUNNING);
    PyModule_AddIntConstant(nc, "DATASTORE_STARTUP", NC_DATASTORE_STARTUP);
    PyModule_AddIntConstant(nc, "DATASTORE_CANDIDATE", NC_DATASTORE_CANDIDATE);

	/* init libnetconf exceptions for use in clb_print() */
	libnetconf2Error = PyErr_NewExceptionWithDoc("netconf2.Error",
	                                             "Error passed from the underlying libnetconf2 library.",
	                                             NULL, NULL);
	Py_INCREF(libnetconf2Error);
	PyModule_AddObject(nc, "Error", libnetconf2Error);

	libnetconf2Warning = PyErr_NewExceptionWithDoc("netconf2.Warning",
	                                               "Warning passed from the underlying libnetconf2 library.",
	                                               PyExc_Warning, NULL);
	Py_INCREF(libnetconf2Warning);
	PyModule_AddObject(nc, "Warning", libnetconf2Warning);

    libnetconf2ReplyError = PyErr_NewExceptionWithDoc("netconf2.ReplyError",
                                                      "NETCONF error returned from the server.",
                                                      NULL, NULL);
    Py_INCREF(libnetconf2ReplyError);
    PyModule_AddObject(nc, "ReplyError", libnetconf2ReplyError);

	return nc;
}
