From: Francis Dupont Date: Sat, 5 Jan 2019 14:23:55 +0000 (+0100) Subject: [333-parser-libraries-for-servers] Added DHCPv[46] {to,from}-yang tools X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=1ed12b5862ccd2506dc59c12f24811e8d8be1d49;p=thirdparty%2Fkea.git [333-parser-libraries-for-servers] Added DHCPv[46] {to,from}-yang tools --- diff --git a/src/bin/dhcp4/tools/.gitignore b/src/bin/dhcp4/tools/.gitignore index 191faa7af8..42a64c465d 100644 --- a/src/bin/dhcp4/tools/.gitignore +++ b/src/bin/dhcp4/tools/.gitignore @@ -1,2 +1,4 @@ /parser4 +/export-from-yang4 +/import-to-yang4 /tools4.8 diff --git a/src/bin/dhcp4/tools/Makefile.am b/src/bin/dhcp4/tools/Makefile.am index 59ed2a8d5b..9204aa376b 100644 --- a/src/bin/dhcp4/tools/Makefile.am +++ b/src/bin/dhcp4/tools/Makefile.am @@ -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 index 0000000000..0ed0173c0d --- /dev/null +++ b/src/bin/dhcp4/tools/export_from_yang4.cc @@ -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 +#include + +#include +#include + +#include +#include + +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 -o " + << 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 index 0000000000..ac76ef5e78 --- /dev/null +++ b/src/bin/dhcp4/tools/import_to_yang4.cc @@ -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 +#include + +#include +#include +#include + +#include + +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 -m " + << 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); +} diff --git a/src/bin/dhcp6/tools/.gitignore b/src/bin/dhcp6/tools/.gitignore index b4e6ce27f2..be2a14a9c0 100644 --- a/src/bin/dhcp6/tools/.gitignore +++ b/src/bin/dhcp6/tools/.gitignore @@ -1,2 +1,4 @@ /parser6 +/export-from-yang6 +/import-to-yang6 /tools6.8 diff --git a/src/bin/dhcp6/tools/Makefile.am b/src/bin/dhcp6/tools/Makefile.am index 984094c335..253e735cbb 100644 --- a/src/bin/dhcp6/tools/Makefile.am +++ b/src/bin/dhcp6/tools/Makefile.am @@ -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 index 0000000000..60ceb40718 --- /dev/null +++ b/src/bin/dhcp6/tools/export_from_yang6.cc @@ -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 +#include + +#include +#include + +#include +#include + +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 -o " + << 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 index 0000000000..66d33e2e2f --- /dev/null +++ b/src/bin/dhcp6/tools/import_to_yang6.cc @@ -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 +#include + +#include +#include +#include + +#include + +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 -m " + << 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); +} diff --git a/src/bin/netconf/tools/Makefile.am b/src/bin/netconf/tools/Makefile.am index bcd7ed608b..8b1c587873 100644 --- a/src/bin/netconf/tools/Makefile.am +++ b/src/bin/netconf/tools/Makefile.am @@ -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)