def test_xpath_split(self): for xpath, split_result in XPATH_SPLIT_EXPECTED_RESULTS.items(): if split_result is ValueError: with self.assertRaises(split_result): list(ly.xpath_split(xpath)) else: self.assertEqual(list(ly.xpath_split(xpath)), split_result)
def action_cb(xpath, input_params, event, private_data): calls.append((xpath, input_params, event, private_data)) _, _, keys = list(libyang.xpath_split(xpath))[2] _, name = keys[0] duration = input_params["duration"] return { "message": "%s alarm triggered for %s seconds" % ( name, duration, ) }
def trigger_alarm(xpath, input_params, event, private_data): print() print("========================") print("Action call: %s" % xpath) print("params: %s" % input_params) _, _, keys = list(libyang.xpath_split(xpath))[2] _, alarm_name = keys[0] seconds = input_params["duration"] out = { "message": "%s alarm triggered for %s seconds" % (alarm_name, seconds) } print("returning %s" % out) print("---------------") print() time.sleep(seconds) return out
def rpc_send( self, xpath: str, input_dict: Dict, timeout_ms: int = 0, strict: bool = False, strip_prefixes: bool = True, include_implicit_defaults: bool = False, trim_default_values: bool = False, keep_empty_containers: bool = False, ) -> Dict: """ Same as rpc_send_ly() but takes a python dictionary and a YANG module name as input arguments. :arg rpc_input: Input data tree. It is converted to a libyang struct lyd_node according to module_name. :arg module_name: The name of the YANG module used to convert the rpc input dict to a libyang.DNode object. :arg strict: If True, reject rpc_input if it contains elements without any schema definition. :arg strip_prefixes: If True, remove YANG module prefixes from dictionary keys. :arg include_implicit_defaults: Include leaves with implicit default values in the retured dict. :arg trim_default_values: Exclude leaves when their value equals the default. :arg keep_empty_containers: Include empty (non-presence) containers. :returns: A python dictionary with the RPC/action output tree. """ ctx = self.get_ly_ctx() rpc = {} libyang.xpath_set(rpc, xpath, input_dict) module_name, _, _ = next(libyang.xpath_split(xpath)) module = ctx.get_module(module_name) in_dnode = module.parse_data_dict(rpc, rpc=True, strict=strict, validate=False) try: out_dnode = self.rpc_send_ly(in_dnode, timeout_ms=timeout_ms) finally: in_dnode.free() try: out_dict = out_dnode.print_dict( strip_prefixes=strip_prefixes, absolute=False, with_siblings=False, include_implicit_defaults=include_implicit_defaults, trim_default_values=trim_default_values, keep_empty_containers=keep_empty_containers, ) # strip first item with only RPC/action name return next(iter(out_dict.values())) finally: out_dnode.free()