Skip to content

Commit ea3064d

Browse files
authored
Merge pull request #149 from CiscoTestAutomation/paneeraj_grpc_message
Added support for grpc message length
2 parents 4dd9a4e + 5249c46 commit ea3064d

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--------------------------------------------------------------------------------
2+
New
3+
--------------------------------------------------------------------------------
4+
* yang.connector
5+
* Added support for GRPC_MAX_RECEIVE_MESSAGE_LENGTH and GRPC_MAX_SEND_MESSAGE_LENGTH attributes to pick up from testbed settings.

connector/src/yang/connector/gnmi.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def banner(string):
3131
def to_plaintext(string):
3232
return string
3333

34+
from .settings import Settings
3435

3536
# create a logger for this module
3637
log = logging.getLogger(__name__)
@@ -239,6 +240,9 @@ def __init__(self, *args, **kwargs):
239240
self.results = deque()
240241
self.metadata = None
241242

243+
# connection_info is set by BaseConnection class
244+
self.settings = self.connection_info.pop('settings', Settings())
245+
242246
@property
243247
def connected(self):
244248
"""Return True if session is connected."""
@@ -304,7 +308,12 @@ def connect(self):
304308
port = str(dev_args.get('port'))
305309
target = '{0}:{1}'.format(host, port)
306310

307-
options = [('grpc.max_receive_message_length', 1000000000)]
311+
max_receive_message_length = self.settings.get('GRPC_MAX_RECEIVE_MESSAGE_LENGTH')
312+
max_send_message_length = self.settings.get('GRPC_MAX_SEND_MESSAGE_LENGTH')
313+
314+
options = [('grpc.max_receive_message_length', max_receive_message_length),
315+
('grpc.max_send_message_length', max_send_message_length)]
316+
308317
# Gather certificate settings
309318
root = dev_args.get('root_certificate')
310319
if not root:
@@ -351,6 +360,7 @@ def connect(self):
351360
)
352361
else:
353362
self.channel = grpc.insecure_channel(target)
363+
self.channel = grpc.insecure_channel(target, options)
354364
self.metadata = [
355365
("username", username),
356366
("password", password),

connector/src/yang/connector/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ def __init__(self, *args, **kwargs):
88
self.NETCONF_SCREEN_LOGGING_MAX_LINES = 40
99
# Enable XML formatting by default
1010
self.NETCONF_LOGGING_FORMAT_XML = True
11+
# Default receive message length
12+
self.GRPC_MAX_RECEIVE_MESSAGE_LENGTH = 1000000000
13+
# Default send message length
14+
self.GRPC_MAX_SEND_MESSAGE_LENGTH = 1000000000

connector/src/yang/connector/tests/test_gnmi.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ def test_connect(self):
2525
' ip : "1.2.3.4"\n' \
2626
' port: 830\n' \
2727
' username: admin\n' \
28-
' password: admin\n'
28+
' password: admin\n' \
2929

3030
testbed = loader.load(yaml)
3131
device = testbed.devices['dummy']
3232
with patch('yang.connector.gnmi.grpc.insecure_channel') as mock_grpc:
3333
device.connect(alias='gnmi', via='Gnmi')
34-
mock_grpc.assert_called_with('1.2.3.4:830')
34+
mock_grpc.assert_called_with('1.2.3.4:830', [('grpc.max_receive_message_length', 1000000000), ('grpc.max_send_message_length', 1000000000)])
3535

3636
def test_re_connect(self):
3737

@@ -46,16 +46,16 @@ def test_re_connect(self):
4646
' ip : "1.2.3.4"\n' \
4747
' port: 830\n' \
4848
' username: admin\n' \
49-
' password: admin\n'
49+
' password: admin\n' \
5050

5151
testbed = loader.load(yaml)
5252
device = testbed.devices['dummy']
5353
with patch('yang.connector.gnmi.grpc.insecure_channel') as mock_grpc:
5454
device.connect()
55-
mock_grpc.assert_called_with('1.2.3.4:830')
55+
mock_grpc.assert_called_with('1.2.3.4:830', [('grpc.max_receive_message_length', 1000000000), ('grpc.max_send_message_length', 1000000000)])
5656
device.disconnect()
5757
device.connect(alias='gnmi', via='Gnmi')
58-
mock_grpc.assert_called_with('1.2.3.4:830')
58+
mock_grpc.assert_called_with('1.2.3.4:830', [('grpc.max_receive_message_length', 1000000000), ('grpc.max_send_message_length', 1000000000)])
5959

6060
def test_connect_proxy(self):
6161
yaml = \
@@ -91,7 +91,7 @@ def test_connect_proxy(self):
9191
mock_tunnel.side_effect = ['830']
9292
device.connections['Gnmi'].sshtunnel = AttrDict({'tunnel_ip': '4.3.2.1'})
9393
device.connect(alias='gnmi', via='Gnmi')
94-
mock_grpc.assert_called_with('4.3.2.1:830')
94+
mock_grpc.assert_called_with('4.3.2.1:830', [('grpc.max_receive_message_length', 1000000000), ('grpc.max_send_message_length', 1000000000)])
9595
request = {
9696
"namespace": {"oc-acl": "http://openconfig.net/yang/acl"},
9797
"nodes": [
@@ -159,6 +159,29 @@ def test_get_prefix(self):
159159
path = xpath_util.get_prefix('rfc7951')
160160
self.assertIsInstance(path, proto.gnmi_pb2.Path)
161161

162+
def test_connect_grcp_length(self):
163+
yaml = \
164+
'devices:\n' \
165+
' dummy:\n' \
166+
' type: dummy_device\n' \
167+
' connections:\n' \
168+
' Gnmi:\n' \
169+
' class: yang.connector.Gnmi\n' \
170+
' protocol: gnmi\n' \
171+
' ip : "1.2.3.4"\n' \
172+
' port: 830\n' \
173+
' username: admin\n' \
174+
' password: admin\n' \
175+
' settings:\n' \
176+
' GRPC_MAX_RECEIVE_MESSAGE_LENGTH: 100\n' \
177+
' GRPC_MAX_SEND_MESSAGE_LENGTH: 100\n' \
178+
179+
testbed = loader.load(yaml)
180+
device = testbed.devices['dummy']
181+
with patch('yang.connector.gnmi.grpc.insecure_channel') as mock_grpc:
182+
device.connect(alias='gnmi', via='Gnmi')
183+
mock_grpc.assert_called_with('1.2.3.4:830', [('grpc.max_receive_message_length', 100), ('grpc.max_send_message_length', 100)])
184+
162185

163186
if __name__ == '__main__':
164187
unittest.main()

0 commit comments

Comments
 (0)