19
19
import os
20
20
import re
21
21
from inspect import isclass
22
- from typing import Any , Iterator , List , Tuple , Union
22
+ from typing import Any , List , Tuple , Union
23
23
24
24
from pyangbind .lib import yangtypes
25
25
from pyangbind .lib .base import PybindBase
@@ -302,25 +302,30 @@ def isPathOK(xpath: str) -> bool:
302
302
return True
303
303
304
304
305
- def isPathIn (xpath : str , xpaths : List [str ]) -> bool :
306
- """Returns True if xpath in included or equal to any path in xpaths .
305
+ def isPathInRequestedPaths (xpath : str , xpaths : List [str ]) -> bool :
306
+ """Returns True if xpath in included or equal to any request path .
307
307
308
- xpaths can contain paths with wildcard '*'.
308
+ The request paths can contain paths with wildcard '*'.
309
309
310
310
Args:
311
311
xpath: Non-wildcard xpath to check.
312
312
xpaths: List of xpaths (can have wildcards) to contain xpath.
313
313
"""
314
314
def _inOrEqual (x : str ):
315
+ # Ensure the path has keys sorted
316
+ x = pathToString (parsePath (x ))
315
317
x_regex = re .escape (x ).replace (r"\*" , r"(.+)" )
316
318
return re .match (x_regex , xpath ) is not None
317
319
320
+ xpath = pathToString (parsePath (xpath ))
318
321
return any (map (_inOrEqual , xpaths ))
319
322
320
323
321
324
def pathToString (path : gnmi_pb2 .Path ) -> str :
322
325
"""Parse a gNMI Path to a URI-like string.
323
326
327
+ The keys are sorted alphabetically.
328
+
324
329
Args:
325
330
A gNMI.Path oject.
326
331
@@ -335,7 +340,7 @@ def pathToString(path: gnmi_pb2.Path) -> str:
335
340
elem = e .name
336
341
if hasattr (e , "key" ):
337
342
keys = [f"[{ k } ={ v } ]" for k , v in e .key .items ()]
338
- elem += f"{ '' .join (keys )} "
343
+ elem += f"{ '' .join (sorted ( keys ) )} "
339
344
path_elems .append (elem )
340
345
return "/" + "/" .join (path_elems )
341
346
@@ -463,47 +468,43 @@ def intersectListCmp(want: list, got: list) -> Tuple[bool, str]:
463
468
return True , ""
464
469
465
470
466
- def gNMISubscriptionOnceRequests (
471
+ def gNMISubscriptionOnceRequest (
467
472
xpaths : List [str ],
468
473
encoding : str = 'JSON_IETF'
469
- ) -> Iterator [ gnmi_pb2 .SubscribeRequest ] :
474
+ ) -> gnmi_pb2 .SubscribeRequest :
470
475
"""
471
- Returns an Iterator of gNMI Subscription ONCE requests for the xpaths.
476
+ Returns a gNMI Subscription ONCE requests for the xpaths.
472
477
473
478
Args:
474
479
xpaths: List of gNMI xpaths to subscribe to.
475
480
encoding: Encoding requested for the Updates.
476
481
"""
477
482
paths = [parsePath (xpath ) for xpath in xpaths ]
478
- for r in [
479
- gnmi_pb2 .SubscribeRequest (subscribe = gnmi_pb2 .SubscriptionList (
480
- subscription = [gnmi_pb2 .Subscription (path = path )
481
- for path in paths ],
482
- mode = gnmi_pb2 .SubscriptionList .ONCE ,
483
- encoding = encoding ))]:
484
- yield r
483
+ return gnmi_pb2 .SubscribeRequest (subscribe = gnmi_pb2 .SubscriptionList (
484
+ subscription = [gnmi_pb2 .Subscription (path = path )
485
+ for path in paths ],
486
+ mode = gnmi_pb2 .SubscriptionList .ONCE ,
487
+ encoding = encoding ))
485
488
486
489
487
- def gNMISubscriptionStreamSampleRequests (
490
+ def gNMISubscriptionStreamSampleRequest (
488
491
xpaths : List [str ],
489
492
sample_interval : int ,
490
493
encoding : str = 'JSON_IETF'
491
- ) -> Iterator [ gnmi_pb2 .SubscribeRequest ] :
494
+ ) -> gnmi_pb2 .SubscribeRequest :
492
495
"""
493
- Returns a gNMI Subscription STREAM SAMPLE request for the xpath .
496
+ Returns a gNMI Subscription STREAM SAMPLE request for the xpaths .
494
497
495
498
Args:
496
499
xpaths: gNMI xpaths to subscribe to.
497
500
sample_interval: Nanoseconds interval between Updates.
498
501
encoding: Encoding requested for the Updates.
499
502
"""
500
503
paths = [parsePath (xpath ) for xpath in xpaths ]
501
- for r in [
502
- gnmi_pb2 .SubscribeRequest (subscribe = gnmi_pb2 .SubscriptionList (
503
- subscription = [gnmi_pb2 .Subscription (
504
- path = path ,
505
- mode = gnmi_pb2 .SubscriptionMode .SAMPLE ,
506
- sample_interval = sample_interval ) for path in paths ],
507
- mode = gnmi_pb2 .SubscriptionList .STREAM ,
508
- encoding = encoding ))]:
509
- yield r
504
+ return gnmi_pb2 .SubscribeRequest (subscribe = gnmi_pb2 .SubscriptionList (
505
+ subscription = [gnmi_pb2 .Subscription (
506
+ path = path ,
507
+ mode = gnmi_pb2 .SubscriptionMode .SAMPLE ,
508
+ sample_interval = sample_interval ) for path in paths ],
509
+ mode = gnmi_pb2 .SubscriptionList .STREAM ,
510
+ encoding = encoding ))
0 commit comments