Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file netconf.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Python3 bindings for libnetconf2 (client-side) |
| 5 | * |
| 6 | * Copyright (c) 2017 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | /* Python API header */ |
| 16 | #include <Python.h> |
| 17 | |
| 18 | /* standard headers */ |
| 19 | #include <nc_client.h> |
| 20 | #include <syslog.h> |
| 21 | |
| 22 | #include "netconf.h" |
| 23 | |
| 24 | PyObject *libnetconf2Error; |
| 25 | PyObject *libnetconf2Warning; |
Radek Krejci | 0f3499a | 2017-10-13 13:39:36 +0200 | [diff] [blame] | 26 | PyObject *libnetconf2ReplyError; |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 27 | |
| 28 | /* syslog usage flag */ |
| 29 | static int syslogEnabled = 0; |
| 30 | |
| 31 | static void |
| 32 | clb_print(NC_VERB_LEVEL level, const char* msg) |
| 33 | { |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 34 | switch (level) { |
| 35 | case NC_VERB_ERROR: |
| 36 | PyErr_SetString(libnetconf2Error, msg); |
| 37 | if (syslogEnabled) { |
| 38 | syslog(LOG_ERR, "%s", msg); |
| 39 | } |
| 40 | break; |
| 41 | case NC_VERB_WARNING: |
| 42 | if (syslogEnabled) { |
| 43 | syslog(LOG_WARNING, "%s", msg); |
| 44 | } |
| 45 | PyErr_WarnEx(libnetconf2Warning, msg, 1); |
| 46 | break; |
| 47 | case NC_VERB_VERBOSE: |
| 48 | if (syslogEnabled) { |
| 49 | syslog(LOG_INFO, "%s", msg); |
| 50 | } |
| 51 | break; |
| 52 | case NC_VERB_DEBUG: |
| 53 | if (syslogEnabled) { |
| 54 | syslog(LOG_DEBUG, "%s", msg); |
| 55 | } |
| 56 | break; |
| 57 | } |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static PyObject * |
| 61 | setSyslog(PyObject *self, PyObject *args, PyObject *keywds) |
| 62 | { |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 63 | char* name = NULL; |
| 64 | static char* logname = NULL; |
| 65 | static int option = LOG_PID; |
| 66 | static int facility = LOG_USER; |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 67 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 68 | static char *kwlist[] = {"enabled", "name", "option", "facility", NULL}; |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 69 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 70 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "p|sii", kwlist, &syslogEnabled, &name, &option, &facility)) { |
| 71 | return NULL; |
| 72 | } |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 73 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 74 | if (name) { |
| 75 | free(logname); |
| 76 | logname = strdup(name); |
| 77 | } else { |
| 78 | free(logname); |
| 79 | logname = NULL; |
| 80 | } |
| 81 | closelog(); |
| 82 | openlog(logname, option, facility); |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 83 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 84 | Py_RETURN_NONE; |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static PyObject * |
| 88 | setVerbosity(PyObject *self, PyObject *args, PyObject *keywds) |
| 89 | { |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 90 | int level = NC_VERB_ERROR; /* 0 */ |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 91 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 92 | static char *kwlist[] = {"level", NULL}; |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 93 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 94 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "i", kwlist, &level)) { |
| 95 | return NULL; |
| 96 | } |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 97 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 98 | /* normalize level value if not from the enum */ |
| 99 | if (level < NC_VERB_ERROR) { |
| 100 | nc_verbosity(NC_VERB_ERROR); |
| 101 | } else if (level > NC_VERB_DEBUG) { |
| 102 | nc_verbosity(NC_VERB_DEBUG); |
| 103 | } else { |
| 104 | nc_verbosity(level); |
| 105 | } |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 106 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 107 | Py_RETURN_NONE; |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Radek Krejci | 74ca481 | 2017-09-21 13:03:33 +0200 | [diff] [blame] | 110 | static PyObject * |
| 111 | setSearchpath(PyObject *self, PyObject *args, PyObject *keywds) |
| 112 | { |
| 113 | char *path; |
| 114 | static char *kwlist[] = {"path", NULL}; |
| 115 | |
| 116 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "s", kwlist, &path)) { |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | if (nc_client_set_schema_searchpath(path)) { |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | Py_RETURN_NONE; |
| 125 | } |
| 126 | |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 127 | static PyMethodDef netconf2Methods[] = { |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 128 | {"setVerbosity", (PyCFunction)setVerbosity, METH_VARARGS | METH_KEYWORDS, |
| 129 | "setVerbosity(level)\n--\n\n" |
| 130 | "Set verbose level\n\n" |
| 131 | ":param level: Verbosity level (0 - errors, 1 - warnings, 2 - verbose, 3 - debug)\n" |
| 132 | ":type level: int\n" |
| 133 | ":returns: None\n"}, |
| 134 | {"setSyslog", (PyCFunction)setSyslog, METH_VARARGS | METH_KEYWORDS, |
| 135 | "setSyslog(enabled[, name=None][, option=LOG_PID][, facility=LOG_USER])\n--\n\n" |
| 136 | "Set application settings for syslog.\n\n" |
| 137 | ":param enabled: Flag to enable/disable logging into syslog.\n" |
| 138 | ":type enabled: bool\n" |
| 139 | ":param name: Identifier (program name is set by default).\n" |
| 140 | ":type name: string\n" |
| 141 | ":param option: ORed value of syslog options (LOG_PID by default).\n" |
| 142 | ":type option: int\n" |
| 143 | ":param facility: Type of the program logging the message (LOG_USER by default).\n" |
| 144 | ":type facility: int\n" |
| 145 | ":returns: None\n\n" |
| 146 | ".. seealso:: syslog.openlog\n"}, |
| 147 | {"setSearchpath", (PyCFunction)setSearchpath, METH_VARARGS | METH_KEYWORDS, |
| 148 | "setSearchpath(path)\n--\n\n" |
| 149 | "Set location where YANG/YIN schemas are searched and where the schemas\n" |
| 150 | "retrieved via <get-schema> opration are stored.\n\n" |
| 151 | ":param path: Search directory.\n" |
| 152 | ":type path: string\n" |
| 153 | ":returns: None\n"}, |
| 154 | {NULL, NULL, 0, NULL} |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 155 | }; |
| 156 | |
Radek Krejci | b03ebe4 | 2017-07-04 14:00:33 +0200 | [diff] [blame] | 157 | static char netconf2Docs[] = |
| 158 | "NETCONF Protocol client-side implementation using libnetconf2\n" |
| 159 | "\n" |
| 160 | "netconf2 is a wrapper around libnetconf2 functions designed for NETCONF\n" |
| 161 | "clients. it provides a higher level API than the original libnetconf2 to\n" |
| 162 | "better fit the usage in Python.\n"; |
| 163 | |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 164 | static struct PyModuleDef ncModule = { |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 165 | PyModuleDef_HEAD_INIT, |
| 166 | "netconf2", |
| 167 | netconf2Docs, |
| 168 | -1, |
| 169 | netconf2Methods, |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /* module initializer */ |
| 173 | PyMODINIT_FUNC |
| 174 | PyInit_netconf2(void) |
| 175 | { |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 176 | PyObject *nc; |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 177 | |
Radek Krejci | 9721101 | 2017-10-13 16:05:45 +0200 | [diff] [blame] | 178 | /* import libyang Python module to have it available */ |
| 179 | if (!PyImport_ImportModule("libyang")) { |
| 180 | return NULL; |
| 181 | } |
| 182 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 183 | /* initiate libnetconf2 client part */ |
| 184 | nc_client_init(); |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 185 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 186 | /* set schema searchpath |
| 187 | * nc_client_set_schema_searchpath() |
| 188 | */ |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 189 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 190 | /* set print callback */ |
| 191 | nc_set_print_clb(clb_print); |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 192 | |
| 193 | if (PyType_Ready(&ncSessionType) == -1) { |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | ncSSHType.tp_new = PyType_GenericNew; |
| 198 | if (PyType_Ready(&ncSSHType) == -1) { |
| 199 | return NULL; |
| 200 | } |
Radek Krejci | a2429d1 | 2017-10-13 13:37:24 +0200 | [diff] [blame] | 201 | /* |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 202 | ncTLSType.tp_new = PyType_GenericNew; |
| 203 | if (PyType_Ready(&ncTLSType) == -1) { |
| 204 | return NULL; |
| 205 | } |
Radek Krejci | a2429d1 | 2017-10-13 13:37:24 +0200 | [diff] [blame] | 206 | */ |
Radek Krejci | 0f3499a | 2017-10-13 13:39:36 +0200 | [diff] [blame] | 207 | ncErrType.tp_new = PyType_GenericNew; |
| 208 | if (PyType_Ready(&ncErrType) == -1) { |
| 209 | return NULL; |
| 210 | } |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 211 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 212 | /* create netconf as the Python module */ |
| 213 | nc = PyModule_Create(&ncModule); |
| 214 | if (nc == NULL) { |
| 215 | return NULL; |
| 216 | } |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 217 | |
| 218 | Py_INCREF(&ncSSHType); |
| 219 | PyModule_AddObject(nc, "SSH", (PyObject *)&ncSSHType); |
Radek Krejci | a2429d1 | 2017-10-13 13:37:24 +0200 | [diff] [blame] | 220 | /* |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 221 | Py_INCREF(&ncTLSType); |
| 222 | PyModule_AddObject(nc, "TLS", (PyObject *)&ncTLSType); |
Radek Krejci | a2429d1 | 2017-10-13 13:37:24 +0200 | [diff] [blame] | 223 | */ |
Radek Krejci | 0f3499a | 2017-10-13 13:39:36 +0200 | [diff] [blame] | 224 | Py_INCREF(&ncErrType); |
| 225 | PyModule_AddObject(nc, "ReplyErrorInfo", (PyObject *)&ncErrType); |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 226 | |
| 227 | Py_INCREF(&ncSessionType); |
| 228 | PyModule_AddObject(nc, "Session", (PyObject *)&ncSessionType); |
| 229 | |
| 230 | /* |
| 231 | Py_INCREF(&ncTLSType); |
| 232 | PyModule_AddObject(nc, "TLS", (PyObject *)&ncTLSType); |
| 233 | */ |
| 234 | /* |
| 235 | PyModule_AddStringConstant(nc, "NETCONFv1_0", NETCONF_CAP_BASE10); |
| 236 | PyModule_AddStringConstant(nc, "NETCONFv1_1", NETCONF_CAP_BASE11); |
| 237 | PyModule_AddStringConstant(nc, "TRANSPORT_SSH", NETCONF_TRANSPORT_SSH); |
| 238 | PyModule_AddStringConstant(nc, "TRANSPORT_TLS", NETCONF_TRANSPORT_TLS); |
| 239 | */ |
Radek Krejci | e0854c0 | 2017-10-10 21:11:29 +0200 | [diff] [blame] | 240 | PyModule_AddIntConstant(nc, "DATASTORE_RUNNING", NC_DATASTORE_RUNNING); |
| 241 | PyModule_AddIntConstant(nc, "DATASTORE_STARTUP", NC_DATASTORE_STARTUP); |
| 242 | PyModule_AddIntConstant(nc, "DATASTORE_CANDIDATE", NC_DATASTORE_CANDIDATE); |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 243 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 244 | /* init libnetconf exceptions for use in clb_print() */ |
| 245 | libnetconf2Error = PyErr_NewExceptionWithDoc("netconf2.Error", |
| 246 | "Error passed from the underlying libnetconf2 library.", |
| 247 | NULL, NULL); |
| 248 | Py_INCREF(libnetconf2Error); |
| 249 | PyModule_AddObject(nc, "Error", libnetconf2Error); |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 250 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 251 | libnetconf2Warning = PyErr_NewExceptionWithDoc("netconf2.Warning", |
| 252 | "Warning passed from the underlying libnetconf2 library.", |
| 253 | PyExc_Warning, NULL); |
| 254 | Py_INCREF(libnetconf2Warning); |
| 255 | PyModule_AddObject(nc, "Warning", libnetconf2Warning); |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 256 | |
Radek Krejci | 0f3499a | 2017-10-13 13:39:36 +0200 | [diff] [blame] | 257 | libnetconf2ReplyError = PyErr_NewExceptionWithDoc("netconf2.ReplyError", |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 258 | "NETCONF error returned from the server.", |
| 259 | NULL, NULL); |
Radek Krejci | 0f3499a | 2017-10-13 13:39:36 +0200 | [diff] [blame] | 260 | Py_INCREF(libnetconf2ReplyError); |
| 261 | PyModule_AddObject(nc, "ReplyError", libnetconf2ReplyError); |
| 262 | |
Radek Krejci | 0242d84 | 2017-10-13 16:12:16 +0200 | [diff] [blame^] | 263 | return nc; |
Radek Krejci | c61f0b4 | 2017-06-07 13:21:41 +0200 | [diff] [blame] | 264 | } |