1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2017-2023 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/.

#ifndef HOOKS_CONFIG_H
#define HOOKS_CONFIG_H

#include <exceptions/exceptions.h>
#include <cc/data.h>
#include <cc/cfg_to_element.h>
#include <hooks/libinfo.h>

namespace isc {
namespace hooks {

/// @brief Exception thrown when a library failed to validate
class InvalidHooksLibraries : public Exception {
 public:
    InvalidHooksLibraries(const char* file, size_t line, const char* what) :
        isc::Exception(file, line, what) { };
};

/// @brief Wrapper class that holds hooks libraries configuration
///
/// This was moved from HooksLibrariesParser
///
/// However, this class does more than just check the list of library names.
/// It does two other things:
/// # validate libraries
/// # load libraries
/// and it provides a toElement() method to unparse a configuration.
///
/// @todo add toElement() unit tests
class HooksConfig : public isc::data::CfgToElement {
public:
    /// @brief Default constructor.
    ///
    HooksConfig() : libraries_() { }

    /// @brief Adds additional hooks libraries.
    ///
    /// @param libname full filename with path to the library.
    /// @param parameters map of parameters that configure the library.
    void add(std::string libname, isc::data::ConstElementPtr parameters) {
        libraries_.push_back(make_pair(libname, parameters));
    }

    /// @brief Provides access to the configured hooks libraries.
    ///
    /// @note The const reference returned is only valid as long as the
    /// object that returned it.
    const isc::hooks::HookLibsCollection& get() const {
        return libraries_;
    }

    /// @brief Removes all configured hooks libraries.
    void clear() {
        libraries_.clear();
    }

    /// @brief Compares two Hooks Config classes for equality
    ///
    /// @param other other hooksconfig to compare with
    bool equal(const HooksConfig& other) const;

    /// @brief Verifies that libraries stored in libraries_ are valid.
    ///
    /// This method is a smart wrapper around @ref
    /// isc::hooks::HooksManager::validateLibraries().
    /// It tries to validate all the libraries stored in libraries_.
    ///
    /// @param position position of the hooks-library map for error reporting
    /// @param multi_threading_enabled The flag which indicates if MT is enabled
    ///        (used to check hook libraries compatibility with MT).
    ///
    /// @throw InvalidHooksLibraries if any issue is discovered.
    void verifyLibraries(const isc::data::Element::Position& position,
                         bool multi_threading_enabled) const;

    /// @brief Commits hooks libraries configuration.
    ///
    /// This method calls necessary methods in HooksManager that will unload
    /// any libraries that may be currently loaded and will load the actual
    /// libraries. Providing that the specified libraries are valid and are
    /// different to those already loaded, this method loads the new set of
    /// libraries (and unloads the existing set).
    ///
    /// @param multi_threading_enabled The flag which indicates if MT is enabled
    ///        (used to check hook libraries compatibility with MT).
    ///
    /// @throw InvalidHooksLibraries if the call to HooksManager fails.
    void loadLibraries(bool multi_threading_enabled) const;

    /// @brief Unparse a configuration object
    ///
    /// Returns an element which must parse into the same object, i.e.
    /// @code
    /// for all valid config C parse(parse(C)->toElement()) == parse(C)
    /// @endcode
    ///
    /// @return a pointer to a configuration which can be parsed into
    /// the initial configuration object
    isc::data::ElementPtr toElement() const;<--- Function in derived class

private:
    /// @brief List of hooks libraries with their configuration parameters
    isc::hooks::HookLibsCollection libraries_;
};

}  // namespace hooks
}  // namespace isc

#endif // HOOKS_CONFIG_H