Skip to content

Commit 923bd7e

Browse files
committed
Constrain subscription and smart origin
1 parent 355b534 commit 923bd7e

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/cisco_gnmi/xe.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def subscribe_xpaths(
222222
xpath_subscriptions,
223223
request_mode="STREAM",
224224
sub_mode="SAMPLE",
225-
encoding="PROTO",
225+
encoding="JSON_IETF",
226226
sample_interval=Client._NS_IN_S * 10,
227227
suppress_redundant=False,
228228
heartbeat_interval=None,
@@ -246,7 +246,7 @@ def subscribe_xpaths(
246246
Indicates whether STREAM to stream from target,
247247
ONCE to stream once (like a get),
248248
POLL to respond to POLL.
249-
[STREAM, ONCE, POLL]
249+
[STREAM]
250250
sub_mode : proto.gnmi_pb2.SubscriptionMode, optional
251251
The default SubscriptionMode on a per Subscription basis in the SubscriptionList.
252252
TARGET_DEFINED indicates that the target (like device/destination) should stream
@@ -256,10 +256,10 @@ def subscribe_xpaths(
256256
desired behavior.
257257
ON_CHANGE only streams updates when changes occur.
258258
SAMPLE will stream the subscription at a regular cadence/interval.
259-
[TARGET_DEFINED, ON_CHANGE, SAMPLE]
259+
[SAMPLE]
260260
encoding : proto.gnmi_pb2.Encoding, optional
261261
A member of the proto.gnmi_pb2.Encoding enum specifying desired encoding of returned data
262-
[JSON, BYTES, PROTO, ASCII, JSON_IETF]
262+
[JSON, JSON_IETF]
263263
sample_interval : int, optional
264264
Default nanoseconds for sample to occur.
265265
Defaults to 10 seconds.
@@ -274,15 +274,23 @@ def subscribe_xpaths(
274274
-------
275275
subscribe()
276276
"""
277+
supported_request_modes = ["STREAM"]
278+
supported_encodings = ["JSON", "JSON_IETF"]
279+
supported_sub_modes = ["SAMPLE"]
277280
subscription_list = proto.gnmi_pb2.SubscriptionList()
278281
subscription_list.mode = util.validate_proto_enum(
279282
"mode",
280283
request_mode,
281284
"SubscriptionList.Mode",
282285
proto.gnmi_pb2.SubscriptionList.Mode,
286+
supported_request_modes,
283287
)
284288
subscription_list.encoding = util.validate_proto_enum(
285-
"encoding", encoding, "Encoding", proto.gnmi_pb2.Encoding
289+
"encoding",
290+
encoding,
291+
"Encoding",
292+
proto.gnmi_pb2.Encoding,
293+
supported_encodings,
286294
)
287295
if isinstance(xpath_subscriptions, string_types):
288296
xpath_subscriptions = [xpath_subscriptions]
@@ -298,28 +306,24 @@ def subscribe_xpaths(
298306
sub_mode,
299307
"SubscriptionMode",
300308
proto.gnmi_pb2.SubscriptionMode,
309+
supported_sub_modes,
301310
)
302311
subscription.sample_interval = sample_interval
303-
subscription.suppress_redundant = suppress_redundant
304-
if heartbeat_interval:
305-
subscription.heartbeat_interval = heartbeat_interval
306312
elif isinstance(xpath_subscription, dict):
307313
path = self.parse_xpath_to_gnmi_path(xpath_subscription["path"])
308314
arg_dict = {
309315
"path": path,
310316
"mode": sub_mode,
311317
"sample_interval": sample_interval,
312-
"suppress_redundant": suppress_redundant,
313318
}
314-
if heartbeat_interval:
315-
arg_dict["heartbeat_interval"] = heartbeat_interval
316319
arg_dict.update(xpath_subscription)
317320
if "mode" in arg_dict:
318321
arg_dict["mode"] = util.validate_proto_enum(
319322
"sub_mode",
320323
arg_dict["mode"],
321324
"SubscriptionMode",
322325
proto.gnmi_pb2.SubscriptionMode,
326+
supported_sub_modes,
323327
)
324328
subscription = proto.gnmi_pb2.Subscription(**arg_dict)
325329
elif isinstance(xpath_subscription, proto.gnmi_pb2.Subscription):
@@ -330,15 +334,14 @@ def subscribe_xpaths(
330334
return self.subscribe([subscription_list])
331335

332336
def parse_xpath_to_gnmi_path(self, xpath, origin=None):
333-
"""No origin specified implies openconfig
334-
Otherwise origin is expected to be the module name
337+
"""Naievely tries to intelligently (non-sequitur!) origin
338+
Otherwise assume rfc7951
339+
legacy is not considered
335340
"""
336341
if origin is None:
337342
# naive but effective
338-
if xpath.startswith("openconfig") or ":" not in xpath:
339-
# openconfig
340-
origin = None
343+
if ":" not in xpath:
344+
origin = "openconfig"
341345
else:
342-
# module name
343-
origin = xpath.split(":")[0]
346+
origin = "rfc7951"
344347
return super(XEClient, self).parse_xpath_to_gnmi_path(xpath, origin)

0 commit comments

Comments
 (0)