@@ -338,7 +338,6 @@ def get(self, prefix: str = "", path: list = None,
338
338
339
339
datatype = datatype .lower ()
340
340
type_dict = {'all' , 'config' , 'state' , 'operational' }
341
- encoding_set = {'json' , 'bytes' , 'proto' , 'ascii' , 'json_ietf' }
342
341
343
342
# Set Protobuf value for information type
344
343
pb_datatype = 0
@@ -355,20 +354,9 @@ def get(self, prefix: str = "", path: list = None,
355
354
logger .error ('The GetRequst data type is not within the defined range. Using default type \' all\' .' )
356
355
357
356
# Set Protobuf value for encoding
358
- pb_encoding = 4
359
- if encoding in encoding_set :
360
- if encoding .lower () == 'json' :
361
- pb_encoding = 0
362
- elif encoding .lower () == 'bytes' :
363
- pb_encoding = 1
364
- elif encoding .lower () == 'proto' :
365
- pb_encoding = 2
366
- elif encoding .lower () == 'ascii' :
367
- pb_encoding = 3
368
- else :
369
- pb_encoding = 4
370
- else :
371
- logger .error ('The GetRequst encoding is not within the defined range. Using default type \' json_ietf\' .' )
357
+ pb_encoding = choose_encoding (collected_capabilities = self .__capabilities ,
358
+ default_encoding = "json" ,
359
+ requested_encoding = encoding )
372
360
373
361
# Gnmi PREFIX
374
362
try :
@@ -390,12 +378,6 @@ def get(self, prefix: str = "", path: list = None,
390
378
logger .error ('Conversion of gNMI paths to the Protobuf format failed' )
391
379
raise gNMIException ('Conversion of gNMI paths to the Protobuf format failed' , e )
392
380
393
- if self .__capabilities and 'supported_encodings' in self .__capabilities :
394
- if 'json' in self .__capabilities ['supported_encodings' ]:
395
- pb_encoding = 0
396
- elif 'json_ietf' in self .__capabilities ['supported_encodings' ]:
397
- pb_encoding = 4
398
-
399
381
try :
400
382
gnmi_message_request = GetRequest (prefix = protobuf_prefix , path = protobuf_paths ,
401
383
type = pb_datatype , encoding = pb_encoding )
@@ -513,12 +495,11 @@ def set(self, delete: list = None, replace: list = None,
513
495
del_protobuf_paths = []
514
496
replace_msg = []
515
497
update_msg = []
516
- encoding_set = {'json' , 'bytes' , 'proto' , 'ascii' , 'json_ietf' }
517
498
diff_list = []
518
499
519
- if encoding not in encoding_set :
520
- logger .error (f'The encoding { encoding } is not supported. The allowed are: { ", " .join (encoding_set )} .' )
521
- raise gNMIException (f'The encoding { encoding } is not supported. The allowed are: { ", " .join (encoding_set )} .' )
500
+ if encoding . upper () not in Encoding . keys () :
501
+ logger .error (f'The encoding { encoding } is not supported. The allowed are: { ", " .join (Encoding . keys () )} .' )
502
+ raise gNMIException (f'The encoding { encoding } is not supported. The allowed are: { ", " .join (Encoding . keys () )} .' )
522
503
523
504
# Gnmi PREFIX
524
505
try :
@@ -543,57 +524,11 @@ def set(self, delete: list = None, replace: list = None,
543
524
544
525
# Replace operation
545
526
if replace :
546
- if isinstance (replace , list ):
547
- for ue in replace :
548
- if isinstance (ue , tuple ):
549
- u_path = gnmi_path_generator (ue [0 ])
550
- u_val = json .dumps (ue [1 ]).encode ('utf-8' )
551
-
552
- if encoding == 'json' :
553
- replace_msg .append (Update (path = u_path , val = TypedValue (json_val = u_val )))
554
- elif encoding == 'bytes' :
555
- replace_msg .append (Update (path = u_path , val = TypedValue (bytes_val = u_val )))
556
- elif encoding == 'proto' :
557
- replace_msg .append (Update (path = u_path , val = TypedValue (proto_bytes = u_val )))
558
- elif encoding == 'ascii' :
559
- replace_msg .append (Update (path = u_path , val = TypedValue (ascii_val = u_val [1 :- 1 ])))
560
- elif encoding == 'json_ietf' :
561
- replace_msg .append (Update (path = u_path , val = TypedValue (json_ietf_val = u_val )))
562
-
563
- else :
564
- logger .error (f'The input element for Update message must be tuple, got { ue } .' )
565
- raise gNMIException (f'The input element for Update message must be tuple, got { ue } .' )
566
-
567
- else :
568
- logger .error (f'The provided input for Set message (replace operation) is not list.' )
569
- raise gNMIException ('The provided input for Set message (replace operation) is not list.' )
527
+ replace_msg = construct_update_message (user_list = replace , encoding = encoding )
570
528
571
529
# Update operation
572
530
if update :
573
- if isinstance (update , list ):
574
- for ue in update :
575
- if isinstance (ue , tuple ):
576
- u_path = gnmi_path_generator (ue [0 ])
577
- u_val = json .dumps (ue [1 ]).encode ('utf-8' )
578
-
579
- if encoding == 'json' :
580
- update_msg .append (Update (path = u_path , val = TypedValue (json_val = u_val )))
581
- elif encoding == 'bytes' :
582
- update_msg .append (Update (path = u_path , val = TypedValue (bytes_val = u_val )))
583
- elif encoding == 'proto' :
584
- update_msg .append (Update (path = u_path , val = TypedValue (proto_bytes = u_val )))
585
- elif encoding == 'ascii' :
586
- update_msg .append (Update (path = u_path , val = TypedValue (ascii_val = u_val [1 :- 1 ])))
587
- elif encoding == 'json_ietf' :
588
- update_msg .append (Update (path = u_path , val = TypedValue (json_ietf_val = u_val )))
589
-
590
- else :
591
- logger .error (f'The input element for Update message must be tuple, got { ue } .' )
592
- raise gNMIException (f'The input element for Update message must be tuple, got { ue } .' )
593
-
594
- else :
595
- logger .error (f'The provided input for Set message (update operation) is not list.' )
596
- raise gNMIException ('The provided input for Set message (update operation) is not list.' )
531
+ update_msg = construct_update_message (user_list = update , encoding = encoding )
597
532
598
533
try :
599
534
# Adding collection of data for diff before the change
@@ -739,8 +674,9 @@ def _build_subscriptionrequest(self, subscribe: dict, target: str = None, extens
739
674
if 'encoding' not in subscribe :
740
675
subscribe .update ({'encoding' : 'proto' })
741
676
742
- if subscribe ['encoding' ].upper () not in Encoding .keys ():
743
- raise ValueError (f'Subscribe encoding { subscribe ["encoding" ]} is out of allowed ranges.' )
677
+ pb_encoding = choose_encoding (collected_capabilities = self .__capabilities ,
678
+ default_encoding = "proto" ,
679
+ requested_encoding = subscribe ['encoding' ])
744
680
745
681
# qos
746
682
if 'qos' not in subscribe or not subscribe ["qos" ]:
@@ -766,13 +702,13 @@ def _build_subscriptionrequest(self, subscribe: dict, target: str = None, extens
766
702
767
703
# Create message for eveyrhting besides subscriptions
768
704
request = SubscriptionList (prefix = gnmi_path_generator (subscribe ['prefix' ], target ),
769
- use_aliases = subscribe ['use_aliases' ],
770
- qos = subscribe ['qos' ],
771
- mode = subscribe_mode ,
772
- allow_aggregation = subscribe ['allow_aggregation' ],
773
- use_models = subscribe ['use_models' ],
774
- encoding = Encoding . Value ( subscribe [ 'encoding' ]. upper ()) ,
775
- updates_only = subscribe ['updates_only' ])
705
+ use_aliases = subscribe ['use_aliases' ],
706
+ qos = subscribe ['qos' ],
707
+ mode = subscribe_mode ,
708
+ allow_aggregation = subscribe ['allow_aggregation' ],
709
+ use_models = subscribe ['use_models' ],
710
+ encoding = pb_encoding ,
711
+ updates_only = subscribe ['updates_only' ])
776
712
777
713
# subscription
778
714
if 'subscription' not in subscribe or not subscribe ['subscription' ]:
@@ -1242,7 +1178,9 @@ def telemetryParser(in_message=None, debug: bool = False):
1242
1178
return None
1243
1179
1244
1180
1245
- def debug_gnmi_msg (is_printable , what_to_print , message_name ) -> None :
1181
+ def debug_gnmi_msg (is_printable : bool ,
1182
+ what_to_print : str ,
1183
+ message_name : str ) -> None :
1246
1184
"""This helper function prints debug output"""
1247
1185
if is_printable :
1248
1186
print (message_name )
@@ -1252,7 +1190,7 @@ def debug_gnmi_msg(is_printable, what_to_print, message_name) -> None:
1252
1190
1253
1191
1254
1192
def process_potentially_json_value (input_val ) -> Any :
1255
- """This helper method converts value from bytestream"""
1193
+ """This helper function converts value from bytestream"""
1256
1194
unprocessed_value = input_val .decode (encoding = "utf-8" )
1257
1195
1258
1196
if unprocessed_value :
@@ -1264,3 +1202,58 @@ def process_potentially_json_value(input_val) -> Any:
1264
1202
processed_val = None
1265
1203
1266
1204
return processed_val
1205
+
1206
+
1207
+ def choose_encoding (collected_capabilities : list ,
1208
+ default_encoding : str ,
1209
+ requested_encoding : str = None ) -> int :
1210
+ """This helper function chooses the needed encoding"""
1211
+ # Default encoding equals to JSON_IETF in Protobuf format
1212
+ result = 4
1213
+
1214
+ if requested_encoding :
1215
+ if requested_encoding .upper () in Encoding .keys ():
1216
+ result = Encoding .Value (name = requested_encoding .upper ())
1217
+
1218
+ else :
1219
+ raise ValueError (f'Subscribe encoding { requested_encoding } is out of allowed ranges.' )
1220
+
1221
+ else :
1222
+ if collected_capabilities and 'supported_encodings' in collected_capabilities and \
1223
+ default_encoding in collected_capabilities ['supported_encodings' ]:
1224
+ result = Encoding .Value (name = default_encoding .upper ())
1225
+
1226
+ return result
1227
+
1228
+
1229
+ def construct_update_message (user_list : list ,
1230
+ encoding : str ) -> list :
1231
+ """This is a helper method to construct the Update() GNMI message"""
1232
+ result = []
1233
+
1234
+ if isinstance (user_list , list ):
1235
+ for ue in user_list :
1236
+ if isinstance (ue , tuple ):
1237
+ u_path = gnmi_path_generator (ue [0 ])
1238
+ u_val = json .dumps (ue [1 ]).encode ('utf-8' )
1239
+
1240
+ if encoding == 'json' :
1241
+ result .append (Update (path = u_path , val = TypedValue (json_val = u_val )))
1242
+ elif encoding == 'bytes' :
1243
+ result .append (Update (path = u_path , val = TypedValue (bytes_val = u_val )))
1244
+ elif encoding == 'proto' :
1245
+ result .append (Update (path = u_path , val = TypedValue (proto_bytes = u_val )))
1246
+ elif encoding == 'ascii' :
1247
+ result .append (Update (path = u_path , val = TypedValue (ascii_val = u_val [1 :- 1 ])))
1248
+ elif encoding == 'json_ietf' :
1249
+ result .append (Update (path = u_path , val = TypedValue (json_ietf_val = u_val )))
1250
+
1251
+ else :
1252
+ logger .error (f'The input element for Update message must be tuple, got { ue } .' )
1253
+ raise gNMIException (f'The input element for Update message must be tuple, got { ue } .' )
1254
+
1255
+ else :
1256
+ logger .error (f'The provided input for Set message (replace operation) is not list.' )
1257
+ raise gNMIException ('The provided input for Set message (replace operation) is not list.' )
1258
+
1259
+ return result
0 commit comments