Coverage Report

Created: 2025-11-16 07:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kea/src/lib/hooks/hooks_config.h
Line
Count
Source
1
// Copyright (C) 2017-2025 Internet Systems Consortium, Inc. ("ISC")
2
//
3
// This Source Code Form is subject to the terms of the Mozilla Public
4
// License, v. 2.0. If a copy of the MPL was not distributed with this
5
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7
#ifndef HOOKS_CONFIG_H
8
#define HOOKS_CONFIG_H
9
10
#include <exceptions/exceptions.h>
11
#include <cc/data.h>
12
#include <cc/cfg_to_element.h>
13
#include <hooks/libinfo.h>
14
15
namespace isc {
16
namespace hooks {
17
18
/// @brief Exception thrown when a library failed to validate
19
class InvalidHooksLibraries : public Exception {
20
 public:
21
    InvalidHooksLibraries(const char* file, size_t line, const char* what) :
22
0
        isc::Exception(file, line, what) { }
23
};
24
25
/// @brief Wrapper class that holds hooks libraries configuration
26
///
27
/// This was moved from HooksLibrariesParser
28
///
29
/// However, this class does more than just check the list of library names.
30
/// It does two other things:
31
/// # validate libraries
32
/// # load libraries
33
/// and it provides a toElement() method to unparse a configuration.
34
///
35
/// @todo add toElement() unit tests
36
class HooksConfig : public isc::data::CfgToElement {
37
public:
38
    /// @brief Default constructor.
39
    ///
40
55.0k
    HooksConfig() : libraries_() { }
41
42
    /// @brief Adds additional hooks libraries.
43
    ///
44
    /// @param libname full filename with path to the library.
45
    /// @param parameters map of parameters that configure the library.
46
    /// @param cfgname original value of `library` parameter from this
47
    /// library's configuration.
48
    void add(const std::string& libname,
49
             isc::data::ConstElementPtr parameters,
50
             const std::string& cfgname = "");
51
52
    /// @brief Provides access to the configured hooks libraries.
53
    ///
54
    /// @note The const reference returned is only valid as long as the
55
    /// object that returned it.
56
0
    const isc::hooks::HookLibsCollection& get() const {
57
0
        return libraries_;
58
0
    }
59
60
    /// @brief Removes all configured hooks libraries.
61
165
    void clear() {
62
165
        libraries_.clear();
63
165
    }
64
65
    /// @brief Compares two Hooks Config classes for equality
66
    ///
67
    /// @param other other hooksconfig to compare with
68
    bool equal(const HooksConfig& other) const;
69
70
    /// @brief Verifies that libraries stored in libraries_ are valid.
71
    ///
72
    /// This method is a smart wrapper around @ref
73
    /// isc::hooks::HooksManager::validateLibraries().
74
    /// It tries to validate all the libraries stored in libraries_.
75
    ///
76
    /// @param position position of the hooks-library map for error reporting
77
    /// @param multi_threading_enabled The flag which indicates if MT is enabled
78
    ///        (used to check hook libraries compatibility with MT).
79
    ///
80
    /// @throw InvalidHooksLibraries if any issue is discovered.
81
    void verifyLibraries(const isc::data::Element::Position& position,
82
                         bool multi_threading_enabled) const;
83
84
    /// @brief Commits hooks libraries configuration.
85
    ///
86
    /// This method calls necessary methods in HooksManager that will unload
87
    /// any libraries that may be currently loaded and will load the actual
88
    /// libraries. Providing that the specified libraries are valid and are
89
    /// different to those already loaded, this method loads the new set of
90
    /// libraries (and unloads the existing set).
91
    ///
92
    /// @param multi_threading_enabled The flag which indicates if MT is enabled
93
    ///        (used to check hook libraries compatibility with MT).
94
    ///
95
    /// @throw InvalidHooksLibraries if the call to HooksManager fails.
96
    void loadLibraries(bool multi_threading_enabled) const;
97
98
    /// @brief Unparse a configuration object
99
    ///
100
    /// Returns an element which must parse into the same object, i.e.
101
    /// @code
102
    /// for all valid config C parse(parse(C)->toElement()) == parse(C)
103
    /// @endcode
104
    ///
105
    /// @return a pointer to a configuration which can be parsed into
106
    /// the initial configuration object
107
    isc::data::ElementPtr toElement() const;
108
109
private:
110
    /// @brief List of hooks libraries with their configuration parameters
111
    isc::hooks::HookLibsCollection libraries_;
112
};
113
114
}  // namespace hooks
115
}  // namespace isc
116
117
#endif // HOOKS_CONFIG_H