Jan Kundrát | cf5c636 | 2020-01-16 22:54:47 +0100 | [diff] [blame] | 1 | /* |
| 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át | e718594 | 2020-01-22 19:45:53 +0100 | [diff] [blame] | 8 | #include <pybind11/functional.h> |
Jan Kundrát | cf5c636 | 2020-01-16 22:54:47 +0100 | [diff] [blame] | 9 | #include <pybind11/pybind11.h> |
| 10 | #include <pybind11/stl.h> |
| 11 | #include "netconf_access.hpp" |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 12 | #include "netconf-client.hpp" |
Jan Kundrát | cf5c636 | 2020-01-16 22:54:47 +0100 | [diff] [blame] | 13 | |
| 14 | using namespace std::literals; |
| 15 | using namespace pybind11::literals; |
| 16 | |
| 17 | // shamelessly stolen from the docs |
| 18 | namespace 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 | |
| 33 | PYBIND11_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át | e718594 | 2020-01-22 19:45:53 +0100 | [diff] [blame] | 63 | .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át | cf5c636 | 2020-01-16 22:54:47 +0100 | [diff] [blame] | 69 | .def("getItems", &NetconfAccess::getItems, "xpath"_a) |
| 70 | .def("setLeaf", &NetconfAccess::setLeaf, "xpath"_a, "value"_a) |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 71 | .def("createItem", &NetconfAccess::createItem, "xpath"_a) |
| 72 | .def("deleteItem", &NetconfAccess::deleteItem, "xpath"_a) |
Jan Kundrát | 40735fd | 2020-05-22 15:53:57 +0200 | [diff] [blame] | 73 | .def("executeRpc", &NetconfAccess::executeRpc, "rpc"_a, "input"_a=DatastoreAccess::Tree{}) |
| 74 | .def("commitChanges", &NetconfAccess::commitChanges) |
Jan Kundrát | cf5c636 | 2020-01-16 22:54:47 +0100 | [diff] [blame] | 75 | ; |
| 76 | } |