summaryrefslogtreecommitdiff
path: root/python/vyos/proto/vyconf_client.py
blob: b385f09517de0837a2a91f21dc3e48bd65bb3843 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright 2025 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.

import socket
from dataclasses import asdict

from vyos.proto import vyconf_proto
from vyos.proto import vyconf_pb2

from google.protobuf.json_format import MessageToDict
from google.protobuf.json_format import ParseDict

socket_path = '/var/run/vyconfd.sock'


def send_socket(msg: bytearray) -> bytes:
    data = bytes()
    client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    client.connect(socket_path)
    client.sendall(msg)

    data_length = client.recv(4)
    if data_length:
        length = int.from_bytes(data_length)
        data = client.recv(length)

    client.close()

    return data


def request_to_msg(req: vyconf_proto.RequestEnvelope) -> vyconf_pb2.RequestEnvelope:
    # pylint: disable=no-member

    msg = vyconf_pb2.RequestEnvelope()
    msg = ParseDict(asdict(req), msg, ignore_unknown_fields=True)
    return msg


def msg_to_response(msg: vyconf_pb2.Response) -> vyconf_proto.Response:
    # pylint: disable=no-member

    d = MessageToDict(
        msg, preserving_proto_field_name=True, use_integers_for_enums=True
    )

    response = vyconf_proto.Response(**d)
    return response


def write_request(req: vyconf_proto.RequestEnvelope) -> bytearray:
    req_msg = request_to_msg(req)
    encoded_data = req_msg.SerializeToString()
    byte_size = req_msg.ByteSize()
    length_bytes = byte_size.to_bytes(4)
    arr = bytearray(length_bytes)
    arr.extend(encoded_data)

    return arr


def read_response(msg: bytes) -> vyconf_proto.Response:
    response_msg = vyconf_pb2.Response()  # pylint: disable=no-member
    response_msg.ParseFromString(msg)
    response = msg_to_response(response_msg)

    return response


def send_request(name, *args, **kwargs):
    func = getattr(vyconf_proto, f'set_request_{name}')
    request_env = func(*args, **kwargs)
    msg = write_request(request_env)
    response_msg = send_socket(msg)
    response = read_response(response_msg)

    return response