14
14
import grpc
15
15
from pygnmi .spec .v080 .gnmi_pb2_grpc import gNMIStub
16
16
from pygnmi .spec .v080 .gnmi_pb2 import (CapabilityRequest , Encoding , GetRequest ,\
17
- SetRequest , Update , TypedValue , SubscribeRequest , Poll , SubscriptionList ,\
17
+ SetRequest , Subscription , Update , TypedValue , SubscribeRequest , Poll , SubscriptionList ,\
18
18
SubscriptionMode , AliasList , UpdateResult )
19
19
20
20
@@ -234,13 +234,17 @@ def wait_for_connect(self, timeout: int):
234
234
grpc .channel_ready_future (self .__channel ).result (timeout = timeout )
235
235
236
236
except grpc .FutureTimeoutError :
237
- logger .error ("Failed to setup gRPC channel, trying change cipher" )
237
+ if not self .__insecure :
238
+ logger .error ("Failed to setup gRPC channel, trying change cipher" )
238
239
239
- try :
240
- os .environ ["GRPC_SSL_CIPHER_SUITES" ] = "HIGH"
241
- grpc .channel_ready_future (self .__channel ).result (timeout = timeout )
240
+ try :
241
+ os .environ ["GRPC_SSL_CIPHER_SUITES" ] = "HIGH"
242
+ grpc .channel_ready_future (self .__channel ).result (timeout = timeout )
243
+
244
+ except grpc .FutureTimeoutError :
245
+ raise
242
246
243
- except grpc . FutureTimeoutError :
247
+ else :
244
248
raise
245
249
246
250
def capabilities (self ):
@@ -693,16 +697,13 @@ def _build_subscriptionrequest(self, subscribe: dict, target: str = None, extens
693
697
if not isinstance (subscribe , dict ):
694
698
raise ValueError ('Subscribe subscribe request is specified, but the value is not dict.' )
695
699
696
- request = SubscriptionList ()
697
700
gnmi_extension = get_gnmi_extension (ext = extension )
698
701
699
702
# use_alias
700
703
if 'use_aliases' not in subscribe :
701
704
subscribe .update ({'use_aliases' : False })
702
705
703
- if isinstance (subscribe ['use_aliases' ], bool ):
704
- request .use_aliases = subscribe ['use_aliases' ]
705
- else :
706
+ if not isinstance (subscribe ['use_aliases' ], bool ):
706
707
raise ValueError ('Subsricbe use_aliases should have boolean type.' )
707
708
708
709
# mode
@@ -711,47 +712,45 @@ def _build_subscriptionrequest(self, subscribe: dict, target: str = None, extens
711
712
712
713
if subscribe ['mode' ].lower () in {'stream' , 'once' , 'poll' }:
713
714
if subscribe ['mode' ].lower () == 'stream' :
714
- request . mode = 0
715
+ subscribe_mode = 0
715
716
elif subscribe ['mode' ].lower () == 'once' :
716
- request . mode = 1
717
+ subscribe_mode = 1
717
718
elif subscribe ['mode' ].lower () == 'poll' :
718
- request . mode = 2
719
+ subscribe_mode = 2
719
720
else :
720
721
raise ValueError ('Subscribe mode is out of allowed ranges.' )
721
722
722
723
# allow_aggregation
723
724
if 'allow_aggregation' not in subscribe :
724
725
subscribe .update ({'allow_aggregation' : False })
725
726
726
- if isinstance (subscribe ['allow_aggregation' ], bool ):
727
- request .allow_aggregation = subscribe ['allow_aggregation' ]
728
- else :
727
+ if not isinstance (subscribe ['allow_aggregation' ], bool ):
729
728
raise ValueError ('Subsricbe allow_aggregation should have boolean type.' )
730
729
731
730
# updates_only
732
731
if 'updates_only' not in subscribe :
733
732
subscribe .update ({'updates_only' : False })
734
733
735
- if isinstance (subscribe ['updates_only' ], bool ):
736
- request .updates_only = subscribe ['updates_only' ]
737
- else :
734
+ if not isinstance (subscribe ['updates_only' ], bool ):
738
735
raise ValueError ('Subsricbe updates_only should have boolean type.' )
739
736
740
737
# encoding
741
738
if 'encoding' not in subscribe :
742
739
subscribe .update ({'encoding' : 'proto' })
743
740
744
- if subscribe ['encoding' ].upper () in Encoding .keys ():
745
- request .encoding = Encoding .Value (subscribe ['encoding' ].upper ())
746
- else :
741
+ if subscribe ['encoding' ].upper () not in Encoding .keys ():
747
742
raise ValueError (f'Subscribe encoding { subscribe ["encoding" ]} is out of allowed ranges.' )
748
743
749
744
# qos
750
- if 'qos' not in subscribe :
751
- subscribe .update ({'qos' : 0 })
745
+ if 'qos' not in subscribe or not subscribe [ "qos" ] :
746
+ subscribe .update ({'qos' : { 'marking' : 0 } })
752
747
753
- # if subscribe['qos'] >= 0 and subscribe['qos'] <= 64:
754
- # request.qos = QOSMarking(marking=subscribe['qos'])
748
+ else :
749
+ if not (isinstance (subscribe ["qos" ], dict ) and \
750
+ "marking" in subscribe ["qos" ] and \
751
+ isinstance (subscribe ["qos" ]["marking" ], int ) and \
752
+ subscribe ["qos" ]["marking" ] in list (range (0 , 65 ))):
753
+ raise ValueError (f'Subscribe qos/marking { subscribe ["qos" ]["marking" ]} is out of allowed ranges.' )
755
754
756
755
# use_models
757
756
if 'use_models' not in subscribe :
@@ -764,9 +763,15 @@ def _build_subscriptionrequest(self, subscribe: dict, target: str = None, extens
764
763
if 'prefix' not in subscribe :
765
764
subscribe .update ({'prefix' : "" })
766
765
767
- # It is weird that it is not possible to assign prefix directly as earlier
768
- request .prefix .target = gnmi_path_generator (subscribe ['prefix' ], target ).target
769
- request .prefix .origin = gnmi_path_generator (subscribe ['prefix' ], target ).origin
766
+ # Create message for eveyrhting besides subscriptions
767
+ request = SubscriptionList (prefix = gnmi_path_generator (subscribe ['prefix' ], target ),
768
+ use_aliases = subscribe ['use_aliases' ],
769
+ qos = subscribe ['qos' ],
770
+ mode = subscribe_mode ,
771
+ allow_aggregation = subscribe ['allow_aggregation' ],
772
+ use_models = subscribe ['use_models' ],
773
+ encoding = Encoding .Value (subscribe ['encoding' ].upper ()),
774
+ updates_only = subscribe ['updates_only' ])
770
775
771
776
# subscription
772
777
if 'subscription' not in subscribe or not subscribe ['subscription' ]:
@@ -802,8 +807,11 @@ def _build_subscriptionrequest(self, subscribe: dict, target: str = None, extens
802
807
else :
803
808
se_heartbeat_interval = 0
804
809
805
- request .subscription .add (path = se_path , mode = se_mode , sample_interval = se_sample_interval ,
806
- suppress_redundant = se_suppress_redundant , heartbeat_interval = se_heartbeat_interval )
810
+ request .subscription .add (path = se_path ,
811
+ mode = se_mode ,
812
+ sample_interval = se_sample_interval ,
813
+ suppress_redundant = se_suppress_redundant ,
814
+ heartbeat_interval = se_heartbeat_interval )
807
815
808
816
if gnmi_extension :
809
817
return SubscribeRequest (subscribe = request , extension = [gnmi_extension ])
@@ -886,7 +894,7 @@ def subscribe_poll(self, subscribe: dict, target: str = None, extension: list =
886
894
subscribe ['mode' ] = 'POLL'
887
895
gnmi_message_request = self ._build_subscriptionrequest (subscribe , target , extension )
888
896
debug_gnmi_msg (self .__debug , gnmi_message_request , "gNMI request" )
889
-
897
+
890
898
return PollSubscriber (self .__channel , gnmi_message_request , self .__metadata )
891
899
892
900
def subscribe_once (self , subscribe : dict , target : str = None , extension : list = None ):
0 commit comments