Skip to content

Commit

Permalink
add extended json conf
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-christophe81 committed Mar 28, 2024
1 parent 6fa22a8 commit a7f8787
Show file tree
Hide file tree
Showing 16 changed files with 805 additions and 405 deletions.
2 changes: 2 additions & 0 deletions broker/neb/precomp_inc/precomp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ namespace asio = boost::asio;
#include <spdlog/fmt/ostr.h>
#include <spdlog/spdlog.h>

#include <rapidjson/document.h>

#endif
76 changes: 76 additions & 0 deletions common/inc/com/centreon/common/rapidjson_helper.hh
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,16 @@ class rapidjson_helper {
const char* default_value) const;

double get_double(const char* field_name) const;
float get_float(const char* field_name) const;

uint64_t get_uint64_t(const char* field_name) const;
int64_t get_int64_t(const char* field_name) const;

uint32_t get_uint32_t(const char* field_name) const;
int32_t get_int32_t(const char* field_name) const;

uint16_t get_uint16_t(const char* field_name) const;
int16_t get_int16_t(const char* field_name) const;

unsigned get_unsigned(const char* field_name) const;
unsigned get_unsigned(const char* field_name, unsigned default_value) const;
Expand All @@ -275,6 +283,10 @@ class rapidjson_helper {

bool get_bool(const char* field_name, bool default_value) const;

// as overriding can't be done with returned type, we use a templated method
template <typename value_type>
value_type get(const char* field_name);

const rapidjson::Value& get_member(const char* field_name) const;

/**
Expand All @@ -296,6 +308,70 @@ class rapidjson_helper {
const std::string_view& json_content);
};

/**
* @brief by default doesn't compile
* Only following specializations are valid
*
* @tparam value_type std::string, uint64_t, double....
* @param field_name
* @return value_type
*/
template <typename value_type>
inline value_type rapidjson_helper::get(const char* field_name) {
class not_convertible {};
static_assert(std::is_convertible<value_type, not_convertible>::value);
}

template <>
inline std::string rapidjson_helper::get<std::string>(const char* field_name) {
return get_string(field_name);
}

template <>
inline double rapidjson_helper::get<double>(const char* field_name) {
return get_double(field_name);
}

template <>
inline float rapidjson_helper::get<float>(const char* field_name) {
return get_float(field_name);
}

template <>
inline uint64_t rapidjson_helper::get<uint64_t>(const char* field_name) {
return get_uint64_t(field_name);
}

template <>
inline int64_t rapidjson_helper::get<int64_t>(const char* field_name) {
return get_int64_t(field_name);
}

template <>
inline uint32_t rapidjson_helper::get<uint32_t>(const char* field_name) {
return get_uint32_t(field_name);
}

template <>
inline int32_t rapidjson_helper::get<int32_t>(const char* field_name) {
return get_int32_t(field_name);
}

template <>
inline uint16_t rapidjson_helper::get<uint16_t>(const char* field_name) {
return get_uint16_t(field_name);
}

template <>
inline int16_t rapidjson_helper::get<int16_t>(const char* field_name) {
return get_int16_t(field_name);
}

template <>
inline bool rapidjson_helper::get<bool>(const char* field_name) {
return get_bool(field_name);
}

/**
* @brief This class is helper to build json validator from a json string
* Example:
Expand Down
103 changes: 103 additions & 0 deletions common/src/rapidjson_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ double rapidjson_helper::get_double(const char* field_name) const {
&rapidjson::Value::GetDouble, &absl::SimpleAtod);
}

/**
* @brief read a float field
*
* @param field_name
* @return const char* field value
* @throw msg_fmt if member does not exist or field value is nor a float nor a
* string containing a float
*/
float rapidjson_helper::get_float(const char* field_name) const {
return get<float>(
field_name, "float",
[](const rapidjson::Value& val) {
return val.IsFloat() || val.IsInt() || val.IsUint() || val.IsInt64() ||
val.IsUint64();
},
&rapidjson::Value::GetFloat, &absl::SimpleAtof);
}

/**
* @brief read a uint64_t field
*
Expand All @@ -124,6 +142,91 @@ uint64_t rapidjson_helper::get_uint64_t(const char* field_name) const {
&rapidjson::Value::GetUint64, &absl::SimpleAtoi<uint64_t>);
}

/**
* @brief read a int64_t field
*
* @param field_name
* @return const char* field value
* @throw msg_fmt if member does not exist or field value is nor a int64_t nor
* a string containing a int64_t
*/
int64_t rapidjson_helper::get_int64_t(const char* field_name) const {
return get<int64_t>(
field_name, "int64",
[](const rapidjson::Value& val) { return val.IsInt64(); },
&rapidjson::Value::GetInt64, &absl::SimpleAtoi<int64_t>);
}

/**
* @brief read a uint32_t field
*
* @param field_name
* @return const char* field value
* @throw msg_fmt if member does not exist or field value is nor a uint32_t nor
* a string containing a uint32_t
*/
uint32_t rapidjson_helper::get_uint32_t(const char* field_name) const {
uint64_t to_test = get_uint64_t(field_name);
if (to_test > std::numeric_limits<uint32_t>::max()) {
throw exceptions::msg_fmt("field {}:uint32_t overflow {}", field_name,
to_test);
}
return to_test;
}

/**
* @brief read a int32_t field
*
* @param field_name
* @return const char* field value
* @throw msg_fmt if member does not exist or field value is nor a int32_t nor
* a string containing a int32_t
*/
int32_t rapidjson_helper::get_int32_t(const char* field_name) const {
int64_t to_test = get_int64_t(field_name);
if (to_test > std::numeric_limits<int32_t>::max() ||
to_test < std::numeric_limits<int32_t>::min()) {
throw exceptions::msg_fmt("field {}:int32_t overflow {}", field_name,
to_test);
}
return to_test;
}

/**
* @brief read a uint16_t field
*
* @param field_name
* @return const char* field value
* @throw msg_fmt if member does not exist or field value is nor a uint16_t nor
* a string containing a uint16_t
*/
uint16_t rapidjson_helper::get_uint16_t(const char* field_name) const {
uint64_t to_test = get_uint64_t(field_name);
if (to_test > std::numeric_limits<uint16_t>::max()) {
throw exceptions::msg_fmt("field {}:uint16_t overflow {}", field_name,
to_test);
}
return to_test;
}

/**
* @brief read a int16_t field
*
* @param field_name
* @return const char* field value
* @throw msg_fmt if member does not exist or field value is nor a int16_t nor
* a string containing a int16_t
*/
int16_t rapidjson_helper::get_int16_t(const char* field_name) const {
int64_t to_test = get_int64_t(field_name);
if (to_test > std::numeric_limits<int16_t>::max() ||
to_test < std::numeric_limits<int16_t>::min()) {
throw exceptions::msg_fmt("field {}:int16_t overflow {}", field_name,
to_test);
}
return to_test;
}

/**
* @brief read an unsigned integer field
*
Expand Down
4 changes: 3 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,9 @@ add_subdirectory(src/macros)
add_subdirectory(modules)
add_subdirectory(src/retention)
add_subdirectory(enginerpc)
include_directories(enginerpc ${CMAKE_SOURCE_DIR}/common/src)
include_directories(enginerpc
${CMAKE_SOURCE_DIR}/common/src
${CMAKE_SOURCE_DIR}/common/inc)

# Library engine target.
add_library(enginelog STATIC src/log_v2.cc)
Expand Down
38 changes: 20 additions & 18 deletions engine/enginerpc/engine_impl.cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@

/*
* Copyright 2022 Centreon
*
* This file is part of Centreon Engine.
*
* Centreon Engine is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* Centreon Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Centreon Engine. If not, see
* <http://www.gnu.org/licenses/>.
*/
* Copyright 2022 Centreon
*
* This file is part of Centreon Engine.
*
* Centreon Engine is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* Centreon Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Centreon Engine. If not, see
* <http://www.gnu.org/licenses/>.
*/

#include <google/protobuf/util/time_util.h>
#include <sys/types.h>
Expand All @@ -33,6 +33,8 @@ namespace asio = boost::asio;
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/spdlog.h>

#include <rapidjson/document.h>

#include "com/centreon/common/process_stat.hh"
#include "com/centreon/common/time.hh"
#include "com/centreon/engine/host.hh"
Expand Down Expand Up @@ -99,7 +101,7 @@ std::ostream& operator<<(std::ostream& str, const ServiceIdentifier& serv_id) {
return str;
}

}
} // namespace com::centreon::engine

namespace fmt {
template <>
Expand Down
74 changes: 74 additions & 0 deletions engine/inc/com/centreon/engine/configuration/extended_conf.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
** Copyright 2024 Centreon
**
** This file is part of Centreon Engine.
**
** Centreon Engine is free software: you can redistribute it and/or
** modify it under the terms of the GNU General Public License version 2
** as published by the Free Software Foundation.
**
** Centreon Engine is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Centreon Engine. If not, see
** <http://www.gnu.org/licenses/>.
*/

#ifndef CCE_CONFIGURATION_EXTENDED_STATE_HH
#define CCE_CONFIGURATION_EXTENDED_STATE_HH

#include "com/centreon/common/rapidjson_helper.hh"

namespace com::centreon::engine::configuration {

class state;

/**
* @brief contain json data of a config file passed in param to centengine
* command line
*
*/
class extended_conf {
std::string _path;
struct stat _file_info;
rapidjson::Document _content;

static std::list<std::unique_ptr<extended_conf>> _confs;

public:
extended_conf(const std::string& path);
~extended_conf() = default;
extended_conf(const extended_conf&) = delete;
extended_conf& operator=(const extended_conf&) = delete;
void reload();

static void apply_all_to_state(state& dest);

template <class file_path_iterator>
static void load_all(file_path_iterator begin, file_path_iterator);
};

/**
* @brief try to load all extra configuration files
* if one or more fail, we continue
*
* @tparam file_path_iterator
* @param begin
* @param end
*/
template <class file_path_iterator>
void extended_conf::load_all(file_path_iterator begin, file_path_iterator end) {
for (; begin != end; ++begin) {
try {
_confs.emplace_back(std::make_unique<extended_conf>(*begin));
} catch (const std::exception&) {
}
}
}

} // namespace com::centreon::engine::configuration

#endif
Loading

0 comments on commit a7f8787

Please sign in to comment.