def run(**kwargs):
    """main entry point for all operations"""

    xmls = kwargs.get('xmls', [])
    if not xmls:
        ctx.logger.info("No xmls for translate")
        return

    xmlns = kwargs.get('xmlns', {})
    netconf_namespace, xmlns = utils.update_xmlns(
        xmlns
    )

    for xml_struct in xmls:
        raw_xml = xml_struct.get("raw")
        if not raw_xml:
            ctx.logger.info("Empty raw xml?")
            continue
        ctx.logger.info("Parsing %s..." % (raw_xml[:60]))
        xml_node = etree.XML(raw_xml)
        xml_dict = {}
        utils.generate_dict_node(
            xml_dict, xml_node,
            xmlns
        )
        # save results to runtime properties
        save_to = xml_struct.get('save_to')
        if save_to:
            ctx.instance.runtime_properties[save_to] = xml_dict
            ctx.instance.runtime_properties[save_to + "_ns"] = xmlns
Example #2
0
 def test_xml_to_dict_turing(self):
     """example from turing machine"""
     xml = """
         <rpc xmlns:turing="http://example.net/turing-machine"
             xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
             message-id="some_id">
           <get>
             <filter type="subtree">
               <turing:turing-machine>
                 <turing:transition-function/>
               </turing:turing-machine>
             </filter>
             <source>
               <running/>
             </source>
           </get>
         </rpc>
     """
     xmlns = {
         utils.DEFAULT_NCNS: utils.NETCONF_NAMESPACE,
         # require for regen namespace
         self.FAKE_NS: "a"
     }
     result = {}
     utils.generate_dict_node(result, etree.XML(xml), xmlns)
     # check dict
     self.assertEqual(self.TURING_DICT, result)
     # check xmlns
     self.assertEqual(
         {
             self.FAKE_NS: 'a',
             self.REAL_NS: 'http://example.net/turing-machine',
             utils.DEFAULT_NCNS: utils.NETCONF_NAMESPACE
         }, xmlns)
Example #3
0
def _parse_response(xmlns,
                    netconf_namespace,
                    response,
                    strict_check=False,
                    deep_error_check=False):
    """parse response from server with check to rpc-error"""
    if strict_check:
        try:
            xml_node = etree.XML(response)
        except etree.XMLSyntaxError as e:
            raise cfy_exc.NonRecoverableError("Syntax error in xml %s" %
                                              str(e))
    else:
        # for case when we recieved not fully correct xml
        parser = etree.XMLParser(recover=True)
        xml_node = etree.XML(response, parser)

    xml_dict = {}
    utils.generate_dict_node(xml_dict, xml_node, xmlns)

    try:
        if 'rpc-reply' not in xml_dict or \
                (netconf_namespace + '@rpc-reply') not in xml_dict:
            ctx.logger.error(
                'Unexpected key in response: {0}'.format(xml_dict))
        reply = \
            [v for k, v in xml_dict.viewitems()
             if 'rpc-reply' in k][0]
    except IndexError:
        raise cfy_exc.NonRecoverableError(
            'unexpected reply struct: {0}'.format(xml_dict))

    _check_reply_for_errors(reply, netconf_namespace, deep_error_check)

    return reply
def _parse_response(xmlns,
                    netconf_namespace,
                    response,
                    strict_check=False,
                    deep_error_check=False):
    """parse response from server with check to rpc-error"""
    if strict_check:
        try:
            xml_node = etree.XML(response)
        except etree.XMLSyntaxError as e:
            raise cfy_exc.NonRecoverableError("Syntax error in xml %s" %
                                              str(e))
    else:
        # for case when we recieved not fully correct xml
        parser = etree.XMLParser(recover=True)
        xml_node = etree.XML(response, parser)

    xml_dict = {}
    utils.generate_dict_node(xml_dict, xml_node, xmlns)
    reply = None
    if 'rpc-reply' in xml_dict:
        reply = xml_dict['rpc-reply']
    elif (netconf_namespace + '@rpc-reply') in xml_dict:
        # default namespace can't be not netconf 1.0
        reply = xml_dict[netconf_namespace + '@rpc-reply']
    if not reply:
        raise cfy_exc.NonRecoverableError("unexpected reply struct")

    _check_reply_for_errors(reply, netconf_namespace, deep_error_check)

    return reply
 def test_xml_to_dict_net_namespace(self):
     """test create new namespace shortname"""
     xml = """
         <a
             xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
         >
             <b xmlns="something">b</b>
         </a>
     """
     xmlns = {
         "_": utils.NETCONF_NAMESPACE
     }
     result = {}
     utils.generate_dict_node(result, etree.XML(xml), xmlns)
     # check dict
     self.assertEqual(
         {'a': {'_something@b': 'b'}},
         result
     )
     # check xmlns
     self.assertEqual(
         {
             '_': utils.NETCONF_NAMESPACE,
             '_something': 'something'
         }, xmlns
     )
 def test_xml_to_dict(self):
     """test minimal struct with tag list and attibutes"""
     xml = """
         <a
             xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
             xmlns:nm="s"
         >
             <b m="g">
                 b
             </b>
             <c nm:nm="update">
                 <d>1</d>
                 <d>2</d>
                 <d>3</d>
             </c>
         </a>
     """
     xmlns = {
         "_": utils.NETCONF_NAMESPACE
     }
     result = {}
     utils.generate_dict_node(result, etree.XML(xml), xmlns)
     # check dict
     self.assertEqual(
         self.SIMPLE_DICT,
         result
     )
     # check xmlns
     self.assertEqual(
         {
             '_': utils.NETCONF_NAMESPACE,
             'nm': 's'
         }, xmlns
     )
 def test_xml_to_dict_turing(self):
     """example from turing machine"""
     xml = """
         <rpc xmlns:turing="http://example.net/turing-machine"
             xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
             message-id="some_id">
           <get>
             <filter type="subtree">
               <turing:turing-machine>
                 <turing:transition-function/>
               </turing:turing-machine>
             </filter>
             <source>
               <running/>
             </source>
           </get>
         </rpc>
     """
     xmlns = {
         utils.DEFAULT_NCNS: utils.NETCONF_NAMESPACE,
         # require for regen namespace
         self.FAKE_NS: "a"
     }
     result = {}
     utils.generate_dict_node(result, etree.XML(xml), xmlns)
     # check dict
     self.assertEqual(self.TURING_DICT, result)
     # check xmlns
     self.assertEqual(
         {
             self.FAKE_NS: 'a',
             self.REAL_NS: 'http://example.net/turing-machine',
             utils.DEFAULT_NCNS: utils.NETCONF_NAMESPACE
         }, xmlns
     )
 def test_xml_to_dict(self):
     """test minimal struct with tag list and attibutes"""
     xml = """
         <a
             xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
             xmlns:nm="s"
         >
             <b m="g">
                 b
             </b>
             <c nm:nm="update">
                 <d>1</d>
                 <d>2</d>
                 <d>3</d>
             </c>
         </a>
     """
     xmlns = {
         "_": utils.NETCONF_NAMESPACE
     }
     result = {}
     utils.generate_dict_node(result, etree.XML(xml), xmlns)
     # check dict
     self.assertDictEqual(
         self.SIMPLE_DICT,
         result
     )
     # check xmlns
     self.assertEqual(
         {
             '_': utils.NETCONF_NAMESPACE,
             'nm': 's'
         }, xmlns
     )
 def test_xml_to_dict_net_namespace(self):
     """test create new namespace shortname"""
     xml = """
         <a
             xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"
         >
             <b xmlns="something">b</b>
             <!-- Comment, ignore it -->
         </a>
     """
     xmlns = {
         "_": utils.NETCONF_NAMESPACE
     }
     result = {}
     utils.generate_dict_node(result, etree.XML(xml), xmlns)
     # check dict
     self.assertEqual(
         {'a': {'_something@b': 'b'}},
         result
     )
     # check xmlns
     self.assertEqual(
         {
             '_': utils.NETCONF_NAMESPACE,
             '_something': 'something'
         }, xmlns
     )
def _server_support_1_1(xmlns, netconf_namespace, response):
    xml_node = etree.XML(response)
    xpath = ("/%s:hello/%s:capabilities/%s:capability" %
             (netconf_namespace, netconf_namespace, netconf_namespace))
    capabilities = xml_node.xpath(xpath, namespaces=xmlns)
    for node in capabilities:
        xml_dict = {}
        utils.generate_dict_node(xml_dict, node, xmlns)
        value = xml_dict.get(netconf_namespace + '@capability')
        if value == netconf_connection.NETCONF_1_1_CAPABILITY:
            return True
    return False
 def test_xml_to_dict_turing(self):
     """example from turing machine"""
     xmlns = {
         utils.DEFAULT_NCNS: utils.NETCONF_NAMESPACE,
         # require for regen namespace
         self.FAKE_NS: "a"
     }
     result = utils.generate_dict_node(etree.XML(self.TURING_STRIPPED),
                                       xmlns)
     # check dict
     self.assertEqual(self.TURING_DICT, result)
     # check xmlns
     self.assertEqual(
         {
             self.FAKE_NS: 'a',
             self.REAL_NS: 'http://example.net/turing-machine',
             utils.DEFAULT_NCNS: utils.NETCONF_NAMESPACE
         }, xmlns)
Example #12
0
"""
if __name__ == "__main__":
    if len(sys.argv) < 2 and len(sys.argv) > 4:
        print(help_message)
    else:
        xmlns = utils.default_xmlns()
        xml_node = utils.load_xml(sys.argv[1])
        rng = None
        if len(sys.argv) > 2:
            rng = utils.load_xml(sys.argv[2])
        if rng is not None:
            relaxng = etree.RelaxNG(rng)
            if not relaxng.validate(xml_node):
                print("You have issues with relaxng")
        sch = None
        if len(sys.argv) > 3:
            sch = utils.load_xml(sys.argv[3])
        if sch is not None:
            schematron = isoschematron.Schematron(sch)
            if not schematron.validate(xml_node):
                print("You have issues with Schematron")
        xml_dict = {}
        utils.generate_dict_node(xml_dict, xml_node, xmlns)
        result = {'payload': xml_dict, 'ns': xmlns}
        if rng is not None:
            utils.load_relaxng_includes(rng, xmlns)
            result['rng'] = etree.tostring(rng, pretty_print=False)
        if sch is not None:
            result['sch'] = etree.tostring(sch, pretty_print=False)
        print(yaml.dump(result))
 if len(sys.argv) > 2:
     rng = utils.load_xml(sys.argv[2])
 if rng is not None:
     relaxng = etree.RelaxNG(rng)
     if not relaxng.validate(xml_node):
         print ("You have issues with relaxng")
 sch = None
 if len(sys.argv) > 3:
     sch = utils.load_xml(sys.argv[3])
 if sch is not None:
     schematron = isoschematron.Schematron(sch)
     if not schematron.validate(xml_node):
         print ("You have issues with Schematron")
 xml_dict = {}
 utils.generate_dict_node(
     xml_dict, xml_node,
     xmlns
 )
 result = {
     'payload': xml_dict,
     'ns': xmlns
 }
 if rng is not None:
     utils.load_relaxng_includes(rng, xmlns)
     result['rng'] = etree.tostring(
         rng, pretty_print=False
     )
 if sch is not None:
     result['sch'] = etree.tostring(
         sch, pretty_print=False
     )
 print(yaml.dump(result))