From: Francis Dupont Date: Mon, 26 Feb 2018 12:48:55 +0000 (+0100) Subject: [5525] Added getParameterNames X-Git-Tag: trac5526_base X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b41502a0e4ce7dcf114c8b50549d27c1f4d731b0;p=thirdparty%2Fkea.git [5525] Added getParameterNames --- diff --git a/src/lib/hooks/library_handle.cc b/src/lib/hooks/library_handle.cc index 7aa7a052cc..4a661fcd54 100644 --- a/src/lib/hooks/library_handle.cc +++ b/src/lib/hooks/library_handle.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2018 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 @@ -80,7 +80,7 @@ LibraryHandle::deregisterAllCallouts(const std::string& name) { } isc::data::ConstElementPtr -LibraryHandle::getParameter(const std::string& name) { +LibraryHandle::getParameters() { HookLibsCollection libinfo = HooksManager::getHooksManager().getLibraryInfo(); int index = index_; @@ -103,8 +103,13 @@ LibraryHandle::getParameter(const std::string& name) { // * 1..numlib - indexes for actual libraries // * INT_MAX - post-user library callout + return (libinfo[index - 1].second); +} + +isc::data::ConstElementPtr +LibraryHandle::getParameter(const std::string& name) { // Try to find appropriate parameter. May return null pointer - isc::data::ConstElementPtr params = libinfo[index - 1].second; + isc::data::ConstElementPtr params = getParameters(); if (!params) { return (isc::data::ConstElementPtr()); } @@ -113,5 +118,23 @@ LibraryHandle::getParameter(const std::string& name) { return (params->get(name)); } +std::vector +LibraryHandle::getParameterNames() { + std::vector names; + // Find all parameter names. + isc::data::ConstElementPtr params = getParameters(); + if (!params || + (params->getType() != isc::data::Element::map) || + (params->size() == 0)) { + return (names); + } + auto map = params->mapValue(); + for (auto elem = map.begin(); elem != map.end(); ++elem) { + names.push_back(elem->first); + } + return (names); +} + + } // namespace util } // namespace isc diff --git a/src/lib/hooks/library_handle.h b/src/lib/hooks/library_handle.h index 5dfad2a909..983c8802a0 100644 --- a/src/lib/hooks/library_handle.h +++ b/src/lib/hooks/library_handle.h @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2018 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 @@ -198,6 +198,13 @@ public: isc::data::ConstElementPtr getParameter(const std::string& name); + /// @brief Returns names of configuration parameters for the library. + /// + /// This method returns a vector of strings reflecting names of + /// configuration parameters specified in the configuration file. + /// + std::vector getParameterNames(); + private: /// @brief Copy constructor /// @@ -218,6 +225,11 @@ private: /// @param Unused - should be the object to copy. LibraryHandle& operator=(const LibraryHandle&); + /// @brief Get configuration parameter common code. + /// + /// @return configuration parameters. + isc::data::ConstElementPtr getParameters(); + /// Back pointer to the collection object for the library CalloutManager* callout_manager_; diff --git a/src/lib/hooks/tests/callout_params_library.cc b/src/lib/hooks/tests/callout_params_library.cc index b5b569a206..3fb7ab309a 100644 --- a/src/lib/hooks/tests/callout_params_library.cc +++ b/src/lib/hooks/tests/callout_params_library.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2018 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 @@ -14,6 +14,7 @@ #include #include +using namespace std; using namespace isc::hooks; using namespace isc::data; @@ -39,6 +40,7 @@ int load(LibraryHandle& handle) { ConstElementPtr int_elem = handle.getParameter("ivalue"); ConstElementPtr bool_elem = handle.getParameter("bvalue"); ConstElementPtr nonexistent = handle.getParameter("nonexistent"); + vector names = handle.getParameterNames(); // String handling example. if (!string_elem) { @@ -51,7 +53,7 @@ int load(LibraryHandle& handle) { return (2); } - std::string str = string_elem->stringValue(); + string str = string_elem->stringValue(); if (str != "string value") { // Parameter is specified, is a string, but has unexpected value. // @@ -97,6 +99,14 @@ int load(LibraryHandle& handle) { return (9); } + // Check names. As a side effect of what maps using strings are + // implemented names are sorted in alphabetical order. + if ((names.size() != 3) || (names[0] != "bvalue") || + (names[1] != "ivalue") || (names[2] != "svalue")) { + // Expect 3 names: bvalue, ivalue, svalue. + return (10); + } + // All validation steps were successful. The library has all the parameters // it needs, so we should report a success. return (0);