Skip to content

Instantly share code, notes, and snippets.

@plajjan
Last active June 25, 2018 21:46
Show Gist options
  • Save plajjan/5a0680ebb7ec3ca1e88bf45844a9ce30 to your computer and use it in GitHub Desktop.
Save plajjan/5a0680ebb7ec3ca1e88bf45844a9ce30 to your computer and use it in GitHub Desktop.
NCS live-status get example
class Selftest(Action):
@Action.action
def cb_action(self, uinfo, name, kp, action_input, action_output):
out = action_output.somewhere
with ncs.maapi.single_write_trans('python-state-backbone-interface-write', 'system', db=ncs.OPERATIONAL) as t:
root = ncs.maagic.get_root(t)
service = ncs.maagic.get_node(t, kp)
dev = devices.device[service.interface]
intf = dev.live_status.interfaces.interface[service.interface]
# each assignment will generate a NETCONF get request to the device
# asking for one leaf per request, first tx_bytes, then rx_bytes etc
out.tx_bytes = intf.tx_bytes
out.rx_bytes = intf.rx_bytes
out.tx_packets = intf.tx_packets
out.rx_packets = intf.rx_packets
class Selftest(Action):
@Action.action
def cb_action(self, uinfo, name, kp, action_input, action_output):
out = action_output.somewhere
with ncs.maapi.single_write_trans('python-state-backbone-interface-write', 'system', db=ncs.OPERATIONAL) as t:
root = ncs.maagic.get_root(t)
service = ncs.maagic.get_node(t, kp)
dev = devices.device[service.interface]
# bulk get the whole interface
# we can use maagic to sort of express the path (this would still
# lead to a NETCONF request but it's a clean way to express the path
# (internally you can access it with _path on the maagic node object
# already today))
intf_path = dev.live_status.interfaces.interface[service.interface]
# now ask for a get query, use helper function to generate a content
# match filter based on a maagic node object (accessing its path)
intf = dev.get(path_to_content_match(intf_path))
# the intf object now holds a maagic structure with "cached" data,
# i.e. trying to access it will not lead to further requests to the device
# thus the following assignments are super cheap, just copying data
# from one local object to another
out.tx_bytes = intf.tx_bytes
out.rx_bytes = intf.rx_bytes
out.tx_packets = intf.tx_packets
out.rx_packets = intf.rx_packets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment