]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[333-parser-libraries-for-servers] Added DHCPv[46] {to,from}-yang tools
authorFrancis Dupont <fdupont@isc.org>
Sat, 5 Jan 2019 14:23:55 +0000 (15:23 +0100)
committerFrancis Dupont <fdupont@isc.org>
Sat, 5 Jan 2019 14:23:55 +0000 (15:23 +0100)
src/bin/dhcp4/tools/.gitignore
src/bin/dhcp4/tools/Makefile.am
src/bin/dhcp4/tools/export_from_yang4.cc [new file with mode: 0644]
src/bin/dhcp4/tools/import_to_yang4.cc [new file with mode: 0644]
src/bin/dhcp6/tools/.gitignore
src/bin/dhcp6/tools/Makefile.am
src/bin/dhcp6/tools/export_from_yang6.cc [new file with mode: 0644]
src/bin/dhcp6/tools/import_to_yang6.cc [new file with mode: 0644]
src/bin/netconf/tools/Makefile.am

index 191faa7af8cf2836f4ea6aad4e13224a4593ffa4..42a64c465d07efb8d487a54fd4f91ee1e573bbd2 100644 (file)
@@ -1,2 +1,4 @@
 /parser4
+/export-from-yang4
+/import-to-yang4
 /tools4.8
index 59ed2a8d5b342a57151127b3724b46240da619f5..9204aa376b5adbb53e32464854e0c92deb11b2a1 100644 (file)
@@ -2,12 +2,16 @@ AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
 AM_CPPFLAGS += -I$(top_srcdir)/src -I$(top_builddir)/src
 AM_CPPFLAGS += -I$(top_srcdir)/src/bin -I$(top_builddir)/src/bin
 AM_CPPFLAGS += $(BOOST_INCLUDES)
-#AM_CPPFLAGS += $(SYSREPO_CPPFLAGS)
+if HAVE_SYSREPO
+AM_CPPFLAGS += $(SYSREPO_CPPFLAGS)
+endif
 
 CLEANFILES = *.gcno *.gcda
 
 sbin_PROGRAMS = parser4
-#sbin_PROGRAMS += to-yang from-yang
+if HAVE_SYSREPO
+sbin_PROGRAMS += import-to-yang4 export-from-yang4
+endif
 
 parser4_SOURCES = parser4.cc
 parser4_LDADD  = $(top_builddir)/src/bin/dhcp4/libparser4.la
@@ -16,5 +20,28 @@ parser4_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la
 parser4_LDADD += $(top_builddir)/src/lib/util/libkea-util.la
 parser4_LDADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la
 parser4_LDADD += $(BOOST_LIBS)
-#parser4_LDADD += $(SYSREPO_LIBS)
 parser4_LDFLAGS = $(AM_LDFLAGS)
+
+if HAVE_SYSREPO
+import_to_yang4_SOURCES = import_to_yang4.cc
+import_to_yang4_LDADD  = $(top_builddir)/src/bin/dhcp4/libparser4.la
+import_to_yang4_LDADD += $(top_builddir)/src/lib/yang/libkea-yang.la
+import_to_yang4_LDADD += $(top_builddir)/src/lib/cc/libkea-cc.la
+import_to_yang4_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la
+import_to_yang4_LDADD += $(top_builddir)/src/lib/util/libkea-util.la
+import_to_yang4_LDADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la
+import_to_yang4_LDADD += $(BOOST_LIBS)
+import_to_yang4_LDADD += $(SYSREPO_LIBS)
+import_to_yang4_LDFLAGS = $(AM_LDFLAGS)
+
+export_from_yang4_SOURCES = export_from_yang4.cc
+export_from_yang4_LDADD  = $(top_builddir)/src/bin/dhcp4/libparser4.la
+export_from_yang4_LDADD += $(top_builddir)/src/lib/yang/libkea-yang.la
+export_from_yang4_LDADD += $(top_builddir)/src/lib/cc/libkea-cc.la
+export_from_yang4_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la
+export_from_yang4_LDADD += $(top_builddir)/src/lib/util/libkea-util.la
+export_from_yang4_LDADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la
+export_from_yang4_LDADD += $(BOOST_LIBS)
+export_from_yang4_LDADD += $(SYSREPO_LIBS)
+export_from_yang4_LDFLAGS = $(AM_LDFLAGS)
+endif
diff --git a/src/bin/dhcp4/tools/export_from_yang4.cc b/src/bin/dhcp4/tools/export_from_yang4.cc
new file mode 100644 (file)
index 0000000..0ed0173
--- /dev/null
@@ -0,0 +1,112 @@
+// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <config.h>
+#include <kea_version.h>
+
+#include <yang/translator_config.h>
+#include <yang/sysrepo_error.h>
+
+#include <fstream>
+#include <iostream>
+
+using namespace std;
+using namespace isc;
+using namespace isc::data;
+using namespace isc::yang;
+#ifndef HAVE_PRE_0_7_6_SYSREPO
+using namespace sysrepo;
+#endif
+
+/// @file From YANG to JSON utility: the sysrepo startup datastore is
+/// exported into a configuration file.
+
+/// @brief Print export-from-yang4 usage.
+void
+usage() {
+    cerr << "Usage: export-from-yang4 [-v] [-V] -m <model> -o <output>"
+         << endl;
+}
+
+int
+main(int argc, char* argv[]) {
+    string model("kea-dhcp4-server");
+    string output_file("");
+
+    int ch;
+    while ((ch = getopt(argc, argv, "m:o:vV")) != -1) {
+        switch (ch) {
+        case 'm':
+            model = optarg;
+            break;
+
+        case 'o':
+            output_file = optarg;
+            break;
+
+        case 'v':
+            cout << VERSION << endl;
+            return (EXIT_SUCCESS);
+
+        case 'V':
+            cout << VERSION << endl << EXTENDED_VERSION << endl;
+            return (EXIT_SUCCESS);
+
+        default:
+            usage();
+            return (EXIT_FAILURE);
+        }
+    }
+
+    // Check for extraneous parameters.
+    if (argc > optind) {
+        usage();
+        return (EXIT_FAILURE);
+    }
+
+    try {
+        S_Connection conn;
+        try {
+            conn.reset(new Connection("export-from-yang4"));
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "can't connect to sysrepo: " << ex.what());
+        }
+
+        S_Session sess;
+        try {
+            sess.reset(new Session(conn, SR_DS_STARTUP));
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "can't establish a session: " << ex.what());
+        }
+
+        ConstElementPtr json;
+        try {
+            json = TranslatorConfig(sess, model).getConfig();
+        } catch (const exception& ex) {
+            isc_throw(SysrepoError, "translation failed: " << ex.what());
+        }
+
+        if (!json) {
+            isc_throw(SysrepoError, "got an emoty result");
+        }
+
+        if (output_file.empty()) {
+            cout << prettyPrint(json) << endl;
+        } else {
+            ofstream out(output_file, ios::trunc);
+            if (!out.good()) {
+                isc_throw(Unexpected, "Unable to open file " + output_file +
+                          " for writing");
+                out << prettyPrint(json) << endl;
+            }
+            out.close();
+        }
+    } catch (const std::exception& ex) {
+        cerr << "export-from-yang4 failed with " << ex.what() << endl;
+        return (EXIT_FAILURE);
+    }
+    return (EXIT_SUCCESS);
+}
diff --git a/src/bin/dhcp4/tools/import_to_yang4.cc b/src/bin/dhcp4/tools/import_to_yang4.cc
new file mode 100644 (file)
index 0000000..ac76ef5
--- /dev/null
@@ -0,0 +1,122 @@
+// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <config.h>
+#include <kea_version.h>
+
+#include <yang/translator_config.h>
+#include <yang/sysrepo_error.h>
+#include <dhcp4/parser_context.h>
+
+#include <iostream>
+
+using namespace std;
+using namespace isc;
+using namespace isc::data;
+using namespace isc::yang;
+#ifndef HAVE_PRE_0_7_6_SYSREPO
+using namespace sysrepo;
+#endif
+
+/// @file From JSON to YANG utility: the configuration file is translated
+/// and imported in the sysrepo startup datastore.
+
+/// @brief Print import-to-yang4 usage.
+void
+usage() {
+    cerr << "Usage: import-to-yang4 [-v] [-V] -i <input> -m <model>"
+         << endl;
+}
+
+int
+main(int argc, char* argv[]) {
+    string input_file("");
+    string model("kea-dhcp4-server");
+
+    int ch;
+    while ((ch = getopt(argc, argv, "i:m:vV")) != -1) {
+        switch (ch) {
+        case 'i':
+            input_file = optarg;
+            break;
+
+        case 'm':
+            model = optarg;
+            break;
+
+        case 'v':
+            cout << VERSION << endl;
+            return (EXIT_SUCCESS);
+
+        case 'V':
+            cout << VERSION << endl << EXTENDED_VERSION << endl;
+            return (EXIT_SUCCESS);
+
+        default:
+            usage();
+            return (EXIT_FAILURE);
+        }
+    }
+
+    // Check for extraneous parameters.
+    if (argc > optind) {
+        usage();
+        return (EXIT_FAILURE);
+    }
+
+    // Input file is required.
+    if (input_file.empty()) {
+        cerr << "Input file was not specified." << endl;
+        usage();
+        return (EXIT_FAILURE);
+    }
+
+    try {
+        Parser4Context parser;
+        ConstElementPtr json;
+        json = parser.parseFile(input_file, Parser4Context::PARSER_DHCP4);
+        if (!json) {
+            cerr << "No input found" << endl;
+            return (EXIT_FAILURE);
+        }
+
+        S_Connection conn;
+        try {
+            conn.reset(new Connection("import-to-yang4"));
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "can't connect to sysrepo: " << ex.what());
+        }
+
+        S_Session sess;
+        try {
+            sess.reset(new Session(conn, SR_DS_STARTUP));
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "can't establish a session: " << ex.what());
+        }
+
+        try {
+            TranslatorConfig(sess, model).setConfig(json);
+        } catch (const exception& ex) {
+            isc_throw(SysrepoError, "translation failed: " << ex.what());
+        }
+
+        try {
+            sess->validate();
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "failed to validate: " << ex.what());
+        }
+
+        try {
+            sess->commit();
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "failed to commit " << ex.what());
+        }
+    } catch (const std::exception& ex) {
+        cerr << "import-to-yang4 failed with " << ex.what() << endl;
+        return (EXIT_FAILURE);
+    }
+    return (EXIT_SUCCESS);
+}
index b4e6ce27f29a2bfd3241776efc7336a74e5b2a80..be2a14a9c0bbe028055058b02889b93441f606d4 100644 (file)
@@ -1,2 +1,4 @@
 /parser6
+/export-from-yang6
+/import-to-yang6
 /tools6.8
index 984094c3358e694ff48b8a53570ce4902a278015..253e735cbb80e783189af2098357337081bcf966 100644 (file)
@@ -2,12 +2,16 @@ AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
 AM_CPPFLAGS += -I$(top_srcdir)/src -I$(top_builddir)/src
 AM_CPPFLAGS += -I$(top_srcdir)/src/bin -I$(top_builddir)/src/bin
 AM_CPPFLAGS += $(BOOST_INCLUDES)
-#AM_CPPFLAGS += $(SYSREPO_CPPFLAGS)
+if HAVE_SYSREPO
+AM_CPPFLAGS += $(SYSREPO_CPPFLAGS)
+endif
 
 CLEANFILES = *.gcno *.gcda
 
 sbin_PROGRAMS = parser6
-#sbin_PROGRAMS += to-yang from-yang
+if HAVE_SYSREPO
+sbin_PROGRAMS += import-to-yang6 export-from-yang6
+endif
 
 parser6_SOURCES = parser6.cc
 parser6_LDADD  = $(top_builddir)/src/bin/dhcp6/libparser6.la
@@ -16,5 +20,28 @@ parser6_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la
 parser6_LDADD += $(top_builddir)/src/lib/util/libkea-util.la
 parser6_LDADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la
 parser6_LDADD += $(BOOST_LIBS)
-#parser6_LDADD += $(SYSREPO_LIBS)
 parser6_LDFLAGS = $(AM_LDFLAGS)
+
+if HAVE_SYSREPO
+import_to_yang6_SOURCES = import_to_yang6.cc
+import_to_yang6_LDADD  = $(top_builddir)/src/bin/dhcp6/libparser6.la
+import_to_yang6_LDADD += $(top_builddir)/src/lib/yang/libkea-yang.la
+import_to_yang6_LDADD += $(top_builddir)/src/lib/cc/libkea-cc.la
+import_to_yang6_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la
+import_to_yang6_LDADD += $(top_builddir)/src/lib/util/libkea-util.la
+import_to_yang6_LDADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la
+import_to_yang6_LDADD += $(BOOST_LIBS)
+import_to_yang6_LDADD += $(SYSREPO_LIBS)
+import_to_yang6_LDFLAGS = $(AM_LDFLAGS)
+
+export_from_yang6_SOURCES = export_from_yang6.cc
+export_from_yang6_LDADD  = $(top_builddir)/src/bin/dhcp6/libparser6.la
+export_from_yang6_LDADD += $(top_builddir)/src/lib/yang/libkea-yang.la
+export_from_yang6_LDADD += $(top_builddir)/src/lib/cc/libkea-cc.la
+export_from_yang6_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la
+export_from_yang6_LDADD += $(top_builddir)/src/lib/util/libkea-util.la
+export_from_yang6_LDADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la
+export_from_yang6_LDADD += $(BOOST_LIBS)
+export_from_yang6_LDADD += $(SYSREPO_LIBS)
+export_from_yang6_LDFLAGS = $(AM_LDFLAGS)
+endif
diff --git a/src/bin/dhcp6/tools/export_from_yang6.cc b/src/bin/dhcp6/tools/export_from_yang6.cc
new file mode 100644 (file)
index 0000000..60ceb40
--- /dev/null
@@ -0,0 +1,112 @@
+// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <config.h>
+#include <kea_version.h>
+
+#include <yang/translator_config.h>
+#include <yang/sysrepo_error.h>
+
+#include <fstream>
+#include <iostream>
+
+using namespace std;
+using namespace isc;
+using namespace isc::data;
+using namespace isc::yang;
+#ifndef HAVE_PRE_0_7_6_SYSREPO
+using namespace sysrepo;
+#endif
+
+/// @file From YANG to JSON utility: the sysrepo startup datastore is
+/// exported into a configuration file.
+
+/// @brief Print export-from-yang6 usage.
+void
+usage() {
+    cerr << "Usage: export-from-yang6 [-v] [-V] -m <model> -o <output>"
+         << endl;
+}
+
+int
+main(int argc, char* argv[]) {
+    string model("kea-dhcp6-server");
+    string output_file("");
+
+    int ch;
+    while ((ch = getopt(argc, argv, "m:o:vV")) != -1) {
+        switch (ch) {
+        case 'm':
+            model = optarg;
+            break;
+
+        case 'o':
+            output_file = optarg;
+            break;
+
+        case 'v':
+            cout << VERSION << endl;
+            return (EXIT_SUCCESS);
+
+        case 'V':
+            cout << VERSION << endl << EXTENDED_VERSION << endl;
+            return (EXIT_SUCCESS);
+
+        default:
+            usage();
+            return (EXIT_FAILURE);
+        }
+    }
+
+    // Check for extraneous parameters.
+    if (argc > optind) {
+        usage();
+        return (EXIT_FAILURE);
+    }
+
+    try {
+        S_Connection conn;
+        try {
+            conn.reset(new Connection("export-from-yang6"));
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "can't connect to sysrepo: " << ex.what());
+        }
+
+        S_Session sess;
+        try {
+            sess.reset(new Session(conn, SR_DS_STARTUP));
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "can't establish a session: " << ex.what());
+        }
+
+        ConstElementPtr json;
+        try {
+            json = TranslatorConfig(sess, model).getConfig();
+        } catch (const exception& ex) {
+            isc_throw(SysrepoError, "translation failed: " << ex.what());
+        }
+
+        if (!json) {
+            isc_throw(SysrepoError, "got an emoty result");
+        }
+
+        if (output_file.empty()) {
+            cout << prettyPrint(json) << endl;
+        } else {
+            ofstream out(output_file, ios::trunc);
+            if (!out.good()) {
+                isc_throw(Unexpected, "Unable to open file " + output_file +
+                          " for writing");
+                out << prettyPrint(json) << endl;
+            }
+            out.close();
+        }
+    } catch (const std::exception& ex) {
+        cerr << "export-from-yang6 failed with " << ex.what() << endl;
+        return (EXIT_FAILURE);
+    }
+    return (EXIT_SUCCESS);
+}
diff --git a/src/bin/dhcp6/tools/import_to_yang6.cc b/src/bin/dhcp6/tools/import_to_yang6.cc
new file mode 100644 (file)
index 0000000..66d33e2
--- /dev/null
@@ -0,0 +1,122 @@
+// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <config.h>
+#include <kea_version.h>
+
+#include <yang/translator_config.h>
+#include <yang/sysrepo_error.h>
+#include <dhcp6/parser_context.h>
+
+#include <iostream>
+
+using namespace std;
+using namespace isc;
+using namespace isc::data;
+using namespace isc::yang;
+#ifndef HAVE_PRE_0_7_6_SYSREPO
+using namespace sysrepo;
+#endif
+
+/// @file From JSON to YANG utility: the configuration file is translated
+/// and imported in the sysrepo startup datastore.
+
+/// @brief Print import-to-yang6 usage.
+void
+usage() {
+    cerr << "Usage: import-to-yang6 [-v] [-V] -i <input> -m <model>"
+         << endl;
+}
+
+int
+main(int argc, char* argv[]) {
+    string input_file("");
+    string model("kea-dhcp6-server");
+
+    int ch;
+    while ((ch = getopt(argc, argv, "i:m:vV")) != -1) {
+        switch (ch) {
+        case 'i':
+            input_file = optarg;
+            break;
+
+        case 'm':
+            model = optarg;
+            break;
+
+        case 'v':
+            cout << VERSION << endl;
+            return (EXIT_SUCCESS);
+
+        case 'V':
+            cout << VERSION << endl << EXTENDED_VERSION << endl;
+            return (EXIT_SUCCESS);
+
+        default:
+            usage();
+            return (EXIT_FAILURE);
+        }
+    }
+
+    // Check for extraneous parameters.
+    if (argc > optind) {
+        usage();
+        return (EXIT_FAILURE);
+    }
+
+    // Input file is required.
+    if (input_file.empty()) {
+        cerr << "Input file was not specified." << endl;
+        usage();
+        return (EXIT_FAILURE);
+    }
+
+    try {
+        Parser6Context parser;
+        ConstElementPtr json;
+        json = parser.parseFile(input_file, Parser6Context::PARSER_DHCP6);
+        if (!json) {
+            cerr << "No input found" << endl;
+            return (EXIT_FAILURE);
+        }
+
+        S_Connection conn;
+        try {
+            conn.reset(new Connection("import-to-yang6"));
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "can't connect to sysrepo: " << ex.what());
+        }
+
+        S_Session sess;
+        try {
+            sess.reset(new Session(conn, SR_DS_STARTUP));
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "can't establish a session: " << ex.what());
+        }
+
+        try {
+            TranslatorConfig(sess, model).setConfig(json);
+        } catch (const exception& ex) {
+            isc_throw(SysrepoError, "translation failed: " << ex.what());
+        }
+
+        try {
+            sess->validate();
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "failed to validate: " << ex.what());
+        }
+
+        try {
+            sess->commit();
+        } catch (const sysrepo_exception& ex) {
+            isc_throw(SysrepoError, "failed to commit " << ex.what());
+        }
+    } catch (const std::exception& ex) {
+        cerr << "import-to-yang6 failed with " << ex.what() << endl;
+        return (EXIT_FAILURE);
+    }
+    return (EXIT_SUCCESS);
+}
index bcd7ed608b39c5621641dc7a7943ecf1df63c663..8b1c587873b010a727a00f810d78476697d8434d 100644 (file)
@@ -2,12 +2,10 @@ AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
 AM_CPPFLAGS += -I$(top_srcdir)/src -I$(top_builddir)/src
 AM_CPPFLAGS += -I$(top_srcdir)/src/bin -I$(top_builddir)/src/bin
 AM_CPPFLAGS += $(BOOST_INCLUDES)
-#AM_CPPFLAGS += $(SYSREPO_CPPFLAGS)
 
 CLEANFILES = *.gcno *.gcda
 
 sbin_PROGRAMS = parser-nc
-#sbin_PROGRAMS += to-yang from-yang
 
 parser_nc_SOURCES = parser-nc.cc
 parser_nc_LDADD  = $(top_builddir)/src/bin/netconf/libparser-nc.la
@@ -16,5 +14,4 @@ parser_nc_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la
 parser_nc_LDADD += $(top_builddir)/src/lib/util/libkea-util.la
 parser_nc_LDADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la
 parser_nc_LDADD += $(BOOST_LIBS)
-#parser_nc_LDADD += $(SYSREPO_LIBS)
 parser_nc_LDFLAGS = $(AM_LDFLAGS)