blob: c6e3c440c0fad9441fd8ec7244626620c8f284b2 [file] [log] [blame]
Jan Kundrátcf5c6362020-01-16 22:54:47 +01001/*
2 * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Jan Kundrát <jan.kundrat@cesnet.cz>
5 *
6*/
7
Jan Kundráte7185942020-01-22 19:45:53 +01008#include <pybind11/functional.h>
Jan Kundrátcf5c6362020-01-16 22:54:47 +01009#include <pybind11/pybind11.h>
10#include <pybind11/stl.h>
Václav Kubernát26b56082020-02-03 18:28:56 +010011#include "netconf-client.hpp"
Václav Kubernátb4e5b182020-11-16 19:55:09 +010012#include "netconf_access.hpp"
Jan Kundrátcf5c6362020-01-16 22:54:47 +010013
14using namespace std::literals;
15using namespace pybind11::literals;
16
17// shamelessly stolen from the docs
18namespace pybind11::detail {
19 template <typename... Ts>
20 struct type_caster<boost::variant<Ts...>> : variant_caster<boost::variant<Ts...>> {};
21
22 // Specifies the function used to visit the variant -- `apply_visitor` instead of `visit`
23 template <>
24 struct visit_helper<boost::variant> {
25 template <typename... Args>
26 static auto call(Args &&...args) -> decltype(boost::apply_visitor(args...)) {
27 return boost::apply_visitor(args...);
28 }
29 };
30}
31
32
33PYBIND11_MODULE(netconf_cli_py, m) {
34 m.doc() = "Python bindings for accessing NETCONF servers";
35
36 pybind11::class_<special_>(m, "YangSpecial")
37 .def("__repr__",
38 [](const special_ s) {
39 return "<netconf_cli_py.YangSpecial " + specialValueToString(s) + ">";
40 });
41
42 pybind11::class_<enum_>(m, "YangEnum")
43 .def("__repr__",
44 [](const enum_ v) {
45 return "<netconf_cli_py.YangEnum '" + v.m_value + "'>";
46 });
47
48 pybind11::class_<binary_>(m, "YangBinary")
49 .def("__repr__",
50 [](const binary_ v) {
51 return "<netconf_cli_py.YangBinary '" + v.m_value + "'>";
52 });
53
54 pybind11::class_<identityRef_>(m, "YangIdentityRef")
55 .def("__repr__",
56 [](const identityRef_ v) {
57 return "<netconf_cli_py.YangIdentityRef '"s
58 + (v.m_prefix ? v.m_prefix->m_name + ":" : ""s) + v.m_value + "'>";
59 });
60
61 pybind11::class_<NetconfAccess>(m, "NetconfAccess")
62 .def(pybind11::init<const std::string&>(), "socketPath"_a)
Jan Kundráte7185942020-01-22 19:45:53 +010063 .def(pybind11::init(
64 [](const std::string& host, const uint16_t port, const std::string& user, const libnetconf::client::KbdInteractiveCb interactiveAuth) {
65 auto session = libnetconf::client::Session::connectKbdInteractive(host, port, user, interactiveAuth);
66 return std::make_unique<NetconfAccess>(std::move(session));
67 }),
68 "server"_a, "port"_a=830, "username"_a, "interactive_auth"_a)
Jan Kundrátcf5c6362020-01-16 22:54:47 +010069 .def("getItems", &NetconfAccess::getItems, "xpath"_a)
70 .def("setLeaf", &NetconfAccess::setLeaf, "xpath"_a, "value"_a)
Jan Kundrátcbf288b2020-06-18 20:44:39 +020071 .def("createItem", &NetconfAccess::createItem, "xpath"_a)
72 .def("deleteItem", &NetconfAccess::deleteItem, "xpath"_a)
Václav Kubernátb3960f82020-12-01 03:21:48 +010073 .def("execute", &NetconfAccess::execute, "rpc"_a, "input"_a=DatastoreAccess::Tree{})
Jan Kundrát40735fd2020-05-22 15:53:57 +020074 .def("commitChanges", &NetconfAccess::commitChanges)
Jan Kundrátcf5c6362020-01-16 22:54:47 +010075 ;
76}