Beispiel #1
0
 def test_load_relaxng_includes_without_file(self):
     """check update relaxng with include"""
     xmlns = utils.default_xmlns()
     main_node = etree.XML(self.RELAXNG_MAIN)
     utils.load_relaxng_includes(
         main_node, xmlns,
         {"relaxng-lib.rng": etree.XML(self.RELAXNG_SLAVE)})
     self.assertEqual(etree.tostring(main_node), self.RELAXNG_RESULT)
Beispiel #2
0
 def test_load_relaxng_includes(self):
     """check update relaxng with include"""
     xmlns = utils.default_xmlns()
     main_node = etree.XML(self.RELAXNG_MAIN)
     fake_file = mock.mock_open(read_data=self.RELAXNG_SLAVE)
     with mock.patch('__builtin__.open', fake_file):
         utils.load_relaxng_includes(main_node, xmlns)
     self.assertEqual(etree.tostring(main_node), self.RELAXNG_RESULT)
 def test_load_relaxng_includes(self):
     """check update relaxng with include"""
     xmlns = utils.default_xmlns()
     main_node = etree.XML(self.RELAXNG_MAIN)
     fake_file = mock.mock_open(read_data=self.RELAXNG_SLAVE)
     with mock.patch(
         '__builtin__.open', fake_file
     ):
         utils.load_relaxng_includes(main_node, xmlns)
     self.assertEqual(
         etree.tostring(main_node),
         self.RELAXNG_RESULT
     )
 def test_load_relaxng_includes_without_file(self):
     """check update relaxng with include"""
     xmlns = utils.default_xmlns()
     main_node = etree.XML(self.RELAXNG_MAIN)
     utils.load_relaxng_includes(
         main_node, xmlns, {
             "relaxng-lib.rng": etree.XML(self.RELAXNG_SLAVE)
         }
     )
     self.assertEqual(
         etree.tostring(main_node),
         self.RELAXNG_RESULT
     )
Beispiel #5
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))
def _gen_relaxng_with_schematron(dsdl, operation=None):
    """generate validation rules by dsdl"""
    # call that will be called without validation
    skiped_actions = [
        "rfc6020@get", "rfc6020@get-config", "rfc6020@lock", "rfc6020@unlock",
        "rfc6020@copy-config"
    ]

    rng_txt = None
    sch_txt = None
    xpath = None
    if not dsdl:
        return rng_txt, sch_txt, xpath
    if operation == "rfc6020@edit-config":
        operation_type = "config"
        xpath = "/rfc6020:rpc/rfc6020:edit-config/rfc6020:config"
    elif operation in skiped_actions:
        return rng_txt, sch_txt, xpath
    else:
        # return rng_txt, sch_txt, xpath
        operation_type = "rpc"
        xpath = "/rfc6020:rpc"

    dsdl = etree.XML(str(dsdl))

    # search base directory, hack for cloudify manager:
    # we have different path to installed package and virtualenv
    virrual_env_path = os.path.dirname(__file__) + "/../../../../"
    if not os.path.isfile(virrual_env_path +
                          '/share/netconf/xslt/gen-relaxng.xsl'):
        # more correct way
        # get path from virtual env or use root as base directory
        virrual_env_path = os.environ.get("VIRTUAL_ENV", "/")

    # relaxng xslt
    rng_rpc = open(virrual_env_path + '/share/netconf/xslt/gen-relaxng.xsl',
                   'rb')
    with rng_rpc:
        main_module = operation_type + "-parent"

        xslt_root = etree.parse(rng_rpc)
        transform = etree.XSLT(xslt_root)

        # generate includes for relaxng
        transformed = transform(
            dsdl, **{
                "schema-dir":
                "'" + virrual_env_path + "/share/netconf/schema'",
                "gdefs-only": "1"
            })

        # save includes to file dictionary
        # will be used in reintegrate includes to validation
        if operation_type == 'config':
            base_dict = {
                main_module + "-gdefs-" + operation_type + ".rng":
                etree.XML(str(transformed))
            }
        else:
            base_dict = {
                main_module + "-gdefs.rng": etree.XML(str(transformed))
            }

        # validation for currect action
        transformed = transform(
            dsdl, **{
                "schema-dir":
                "'" + virrual_env_path + "/share/netconf/schema'",
                "gdefs-only": "0",
                "target": "'" + operation_type + "'",
                "basename": "'" + main_module + "'"
            })

        # remerge everything
        xmlns = utils.default_xmlns()
        rng = etree.XML(str(transformed))
        utils.load_relaxng_includes(rng, xmlns, base_dict)
        rng_txt = etree.tostring(rng, pretty_print=True)

    # generate schematron
    sch_rpc = open(virrual_env_path + '/share/netconf/xslt/gen-schematron.xsl',
                   'rb')
    with sch_rpc:
        # generated broken schematron for non config nodes
        if operation_type == 'config':
            xslt_root = etree.parse(sch_rpc)

            transform = etree.XSLT(xslt_root)
            transformed = transform(
                dsdl, **{
                    "schema-dir":
                    ("'" + virrual_env_path + "/share/netconf/schema'"),
                    "gdefs-only":
                    "1",
                    "target":
                    "'" + operation_type + "'"
                })
            sch_txt = str(transformed)

    return rng_txt, sch_txt, xpath
        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))