]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/grpc-query.py
2 # -*- coding: utf-8 eval: (blacken-mode 1) -*-
4 # February 22 2022, Christian Hopps <chopps@labn.net>
6 # Copyright (c) 2022, LabN Consulting, L.L.C.
8 # Permission is hereby granted, free of charge, to any person obtaining a copy
9 # of this software and associated documentation files (the "Software"), to deal
10 # in the Software without restriction, including without limitation the rights
11 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 # copies of the Software, and to permit persons to whom the Software is
13 # furnished to do so, subject to the following conditions:
15 # The above copyright notice and this permission notice shall be included in all
16 # copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
35 # This is painful but works if you have installed grpc and grpc_tools would be *way*
36 # better if we actually built and installed these but ... python packaging.
41 from micronet
import commander
43 commander
.cmd_raises(f
"cp {CWD}/../../../grpc/frr-northbound.proto .")
45 f
"python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I . frr-northbound.proto"
47 except Exception as error
:
48 logging
.error("can't create proto definition modules %s", error
)
53 import frr_northbound_pb2
54 import frr_northbound_pb2_grpc
56 # Would be nice if compiling the modules internally from the source worked
57 # # import grpc_tools.protoc
58 # # proto_include = pkg_resources.resource_filename("grpc_tools", "_proto")
59 # from grpc_tools.protoc import _proto_file_to_module_name, _protos_and_services
61 # frr_northbound_pb2, frr_northbound_pb2_grpc = _protos_and_services(
62 # "frr_northbound.proto"
66 except Exception as error
:
67 logging
.error("can't import proto definition modules %s", error
)
72 def __init__(self
, server
, port
):
73 self
.channel
= grpc
.insecure_channel("{}:{}".format(server
, port
))
74 self
.stub
= frr_northbound_pb2_grpc
.NorthboundStub(self
.channel
)
76 def get_capabilities(self
):
77 request
= frr_northbound_pb2
.GetCapabilitiesRequest()
80 response
= self
.stub
.GetCapabilities(request
)
81 except Exception as error
:
82 logging
.error("Got exception from stub: %s", error
)
84 logging
.debug("GRPC Capabilities: %s", response
)
88 request
= frr_northbound_pb2
.GetRequest()
89 request
.path
.append(xpath
)
90 request
.type = frr_northbound_pb2
.GetRequest
.ALL
91 request
.encoding
= frr_northbound_pb2
.XML
93 for r
in self
.stub
.Get(request
):
94 logging
.info('GRPC Get path: "%s" value: %s', request
.path
, r
)
95 xml
+= str(r
.data
.data
)
99 def next_action(action_list
=None):
100 "Get next action from list or STDIN"
102 for action
in action_list
:
116 parser
= argparse
.ArgumentParser(description
="gRPC Client")
118 "-s", "--server", default
="localhost", help="gRPC Server Address"
121 "-p", "--port", type=int, default
=50051, help="gRPC Server TCP Port"
123 parser
.add_argument("-v", "--verbose", action
="store_true", help="be verbose")
124 parser
.add_argument("--check", action
="store_true", help="check runable")
125 parser
.add_argument("actions", nargs
="*", help="GETCAP|GET,xpath")
126 args
= parser
.parse_args(*args
)
128 level
= logging
.DEBUG
if args
.verbose
else logging
.INFO
131 format
="%(asctime)s %(levelname)s: GRPC-CLI-CLIENT: %(name)s %(message)s",
137 c
= GRPCClient(args
.server
, args
.port
)
139 for action
in next_action(args
.actions
):
140 action
= action
.casefold()
141 logging
.info("GOT ACTION: %s", action
)
142 if action
== "getcap":
143 caps
= c
.get_capabilities()
144 print("Capabilities:", caps
)
145 elif action
.startswith("get,"):
146 # Print Interface State and Config
147 _
, xpath
= action
.split(",", 1)
148 print("Get XPath: ", xpath
)
150 print("{}: {}".format(xpath
, xml
))
151 # for _ in range(0, 1):
154 if __name__
== "__main__":