blob: eaa43496aea5306546f0ba21e87daf08b8a88372 [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>
11#include "netconf_access.hpp"
Jan Kundráte7185942020-01-22 19:45:53 +010012#include "netconf-client.h"
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)
71 .def("commitChanges", &NetconfAccess::commitChanges)
72 ;
73}