def test_rpc_gen(self): """check rpc_gen""" xmlns = { 'r': utils.NETCONF_NAMESPACE, 'n': "someaction" } netconf_namespace, xmlns = utils.update_xmlns( xmlns ) self.assertEqual(netconf_namespace, "r") data = { "b": "b" } # dont have namespace in action parent = utils.rpc_gen( "some_id", 'run', netconf_namespace, data, xmlns ) rpc_string = etree.tostring(parent) example_string = ( """<r:rpc xmlns:r="urn:ietf:params:xml:ns:netconf:base:""" + """1.0" xmlns:n="someaction" message-id="some_id"><r:ru""" + """n><r:b>b</r:b></r:run></r:rpc>""" ) self.assertEqual(rpc_string, example_string) # have namespace in action parent = utils.rpc_gen( "some_id", 'n@run', netconf_namespace, data, xmlns ) rpc_string = etree.tostring(parent) example_string = ( """<r:rpc xmlns:r="urn:ietf:params:xml:ns:netconf:base:""" + """1.0" xmlns:n="someaction" message-id="some_id"><n:ru""" + """n><n:b>b</n:b></n:run></r:rpc>""" ) self.assertEqual(rpc_string, example_string)
def test_rpc_gen(self): """check rpc_gen""" xmlns = { 'r': utils.NETCONF_NAMESPACE, 'n': "someaction" } netconf_namespace, xmlns = utils.update_xmlns( xmlns ) self.assertEqual(netconf_namespace, "r") data = { "b": "b" } # dont have namespace in action parent = utils.rpc_gen( "some_id", 'run', netconf_namespace, data, xmlns ) rpc_string = etree.tostring(parent) example_string = ( """<r:rpc xmlns:n="someaction" xmlns:r="urn:ietf:""" + """params:xml:ns:netconf:base:1.0" r:message-id""" + """="some_id"><r:run><r:b>b</r:b></r:run></r:rpc>""" ) self.assertEqual(rpc_string, example_string) # have namespace in action parent = utils.rpc_gen( "some_id", 'n@run', netconf_namespace, data, xmlns ) rpc_string = etree.tostring(parent) example_string = ( """<r:rpc xmlns:n="someaction" xmlns:r="urn:ietf:""" + """params:xml:ns:netconf:base:1.0" r:message-id""" + """="some_id"><n:run><n:b>b</n:b></n:run></r:rpc>""" ) self.assertEqual(rpc_string, example_string)
def _run_one(netconf, message_id, operation, netconf_namespace, data, xmlns, strict_check=False, deep_error_check=False, log_file_name=None): """run one call by netconf connection""" # rpc ctx.logger.info("rpc call") parent = utils.rpc_gen(message_id, operation, netconf_namespace, data, xmlns) # send rpc rpc_string = etree.tostring(parent, pretty_print=True, xml_declaration=True, encoding='UTF-8') return _run_one_string(netconf, rpc_string, xmlns, netconf_namespace, strict_check, deep_error_check, log_file_name=log_file_name)
def _run_calls(netconf, message_id, netconf_namespace, xmlns, calls, back_database, dsdl, strict_check): # recheck before real send for call in calls: operation = call.get('action') if not operation: continue data = call.get('payload', {}) # gen xml for check parent = utils.rpc_gen(message_id, operation, netconf_namespace, data, xmlns) # try to validate validate_xml = call.get('validate_xml', True) if validate_xml: # validate rpc rng, sch, xpath = _gen_relaxng_with_schematron( dsdl, call.get('action')) else: xpath = None if xpath: ctx.logger.info("We have some validation rules for '{}'".format( str(xpath))) utils.xml_validate(parent, xmlns, xpath, rng, sch) # we can have several calls in one session, # like lock, edit-config, unlock for call in calls: deep_error_check = call.get('deep_error_check') operation = call.get('action') if not operation: ctx.logger.info("No operations") continue data = call.get('payload', {}) message_id = message_id + 1 if "@" not in operation: operation = "_@" + operation _update_data(data, operation, netconf_namespace, back_database) response_dict = _run_one(netconf, message_id, operation, netconf_namespace, data, xmlns, strict_check, deep_error_check) # save results to runtime properties save_to = call.get('save_to') if save_to: ctx.instance.runtime_properties[save_to] = response_dict ctx.instance.runtime_properties[save_to + "_ns"] = xmlns
action: get payload: source: running: {} filter: _@@type: subtree turing@turing-machine: turing@transition-function: {} ------------------- """ if __name__ == "__main__": if len(sys.argv) != 2: print(help_message) else: yaml_rpc = open(sys.argv[1], 'rb') with yaml_rpc: yaml_text = yaml_rpc.read() yaml_dict = yaml.load(yaml_text) data = yaml_dict.get('payload', {}) xmlns = yaml_dict.get('ns', {}) operation = yaml_dict.get('action', 'get') netconf_namespace, xmlns = utils.update_xmlns(xmlns) parent = utils.rpc_gen("some_id", operation, netconf_namespace, data, xmlns) rpc_string = etree.tostring(parent, pretty_print=True, xml_declaration=True, encoding='UTF-8') print(rpc_string)
source: running: {} filter: _@@type: subtree turing@turing-machine: turing@transition-function: {} ------------------- """ if __name__ == "__main__": if len(sys.argv) != 2: print(help_message) else: yaml_rpc = open(sys.argv[1], 'rb') with yaml_rpc: yaml_text = yaml_rpc.read() yaml_dict = yaml.load(yaml_text) data = yaml_dict.get('payload', {}) xmlns = yaml_dict.get('ns', {}) operation = yaml_dict.get('action', 'get') netconf_namespace, xmlns = utils.update_xmlns( xmlns ) parent = utils.rpc_gen( "some_id", operation, netconf_namespace, data, xmlns ) rpc_string = etree.tostring( parent, pretty_print=True, xml_declaration=True, encoding='UTF-8' ) print(rpc_string)