|
| 1 | +/** |
| 2 | + * \file src/plugins/output/ipfix/src/Config.cpp |
| 3 | + * \author Michal Sedlak <[email protected]> |
| 4 | + * \brief Config for ipfix output plugin |
| 5 | + * \date 2019 |
| 6 | + */ |
| 7 | + |
| 8 | +/* Copyright (C) 2019 CESNET, z.s.p.o. |
| 9 | + * |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions |
| 12 | + * are met: |
| 13 | + * 1. Redistributions of source code must retain the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer in |
| 17 | + * the documentation and/or other materials provided with the |
| 18 | + * distribution. |
| 19 | + * 3. Neither the name of the Company nor the names of its contributors |
| 20 | + * may be used to endorse or promote products derived from this |
| 21 | + * software without specific prior written permission. |
| 22 | + * |
| 23 | + * ALTERNATIVELY, provided that this notice is retained in full, this |
| 24 | + * product may be distributed under the terms of the GNU General Public |
| 25 | + * License (GPL) version 2 or later, in which case the provisions |
| 26 | + * of the GPL apply INSTEAD OF those given above. |
| 27 | + * |
| 28 | + * This software is provided ``as is'', and any express or implied |
| 29 | + * warranties, including, but not limited to, the implied warranties of |
| 30 | + * merchantability and fitness for a particular purpose are disclaimed. |
| 31 | + * In no event shall the company or contributors be liable for any |
| 32 | + * direct, indirect, incidental, special, exemplary, or consequential |
| 33 | + * damages (including, but not limited to, procurement of substitute |
| 34 | + * goods or services; loss of use, data, or profits; or business |
| 35 | + * interruption) however caused and on any theory of liability, whether |
| 36 | + * in contract, strict liability, or tort (including negligence or |
| 37 | + * otherwise) arising in any way out of the use of this software, even |
| 38 | + * if advised of the possibility of such damage. |
| 39 | + * |
| 40 | + */ |
| 41 | + |
| 42 | +#include "Config.hpp" |
| 43 | + |
| 44 | +#include <stdexcept> |
| 45 | +#include <memory> |
| 46 | + |
| 47 | +enum params_xml_nodes { |
| 48 | + PARAM_FILENAME, |
| 49 | + PARAM_USE_UTC_FOR_FILENAME_TIME, |
| 50 | + PARAM_WINDOW_SIZE, |
| 51 | + PARAM_ALIGN_WINDOWS, |
| 52 | + PARAM_SKIP_UNKNOWN_DATASETS |
| 53 | +}; |
| 54 | + |
| 55 | +static const struct fds_xml_args args_params[] = { |
| 56 | + FDS_OPTS_ROOT("params"), |
| 57 | + FDS_OPTS_ELEM(PARAM_FILENAME, "filename", FDS_OPTS_T_STRING, 0), |
| 58 | + FDS_OPTS_ELEM(PARAM_USE_UTC_FOR_FILENAME_TIME, "useUtcForFilenameTime", FDS_OPTS_T_STRING, FDS_OPTS_P_OPT), |
| 59 | + FDS_OPTS_ELEM(PARAM_WINDOW_SIZE, "windowSize", FDS_OPTS_T_UINT, FDS_OPTS_P_OPT), |
| 60 | + FDS_OPTS_ELEM(PARAM_ALIGN_WINDOWS, "alignWindows", FDS_OPTS_T_BOOL, FDS_OPTS_P_OPT), |
| 61 | + FDS_OPTS_ELEM(PARAM_SKIP_UNKNOWN_DATASETS, "skipUnknownDataSets", FDS_OPTS_T_BOOL, FDS_OPTS_P_OPT), |
| 62 | + FDS_OPTS_END |
| 63 | +}; |
| 64 | + |
| 65 | +void Config::set_defaults() { |
| 66 | + filename = ""; |
| 67 | + use_utc_for_filename_time = false; |
| 68 | + window_size = 0; |
| 69 | + align_windows = true; |
| 70 | + skip_unknown_datasets = false; |
| 71 | +} |
| 72 | + |
| 73 | +void Config::parse_params(fds_xml_ctx_t *params) |
| 74 | +{ |
| 75 | + const struct fds_xml_cont *content; |
| 76 | + while (fds_xml_next(params, &content) != FDS_EOC) { |
| 77 | + switch (content->id) { |
| 78 | + case PARAM_FILENAME: |
| 79 | + assert(content->type == FDS_OPTS_T_STRING); |
| 80 | + filename = std::string(content->ptr_string); |
| 81 | + break; |
| 82 | + case PARAM_USE_UTC_FOR_FILENAME_TIME: |
| 83 | + assert(content->type == FDS_OPTS_T_BOOL); |
| 84 | + use_utc_for_filename_time = content->val_bool; |
| 85 | + break; |
| 86 | + case PARAM_WINDOW_SIZE: |
| 87 | + assert(content->type == FDS_OPTS_T_UINT); |
| 88 | + window_size = content->val_uint; |
| 89 | + break; |
| 90 | + case PARAM_ALIGN_WINDOWS: |
| 91 | + assert(content->type == FDS_OPTS_T_BOOL); |
| 92 | + align_windows = content->val_bool; |
| 93 | + break; |
| 94 | + case PARAM_SKIP_UNKNOWN_DATASETS: |
| 95 | + assert(content->type == FDS_OPTS_T_BOOL); |
| 96 | + skip_unknown_datasets = content->val_bool; |
| 97 | + break; |
| 98 | + default: |
| 99 | + throw std::invalid_argument("Unexpected element within <params>!"); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void |
| 105 | +Config::check_validity() |
| 106 | +{ |
| 107 | + if (filename.empty()) { |
| 108 | + throw std::invalid_argument("Filename cannot be empty!"); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +Config::Config(const char *params) |
| 113 | +{ |
| 114 | + // Set default values |
| 115 | + set_defaults(); |
| 116 | + |
| 117 | + // Create XML parser |
| 118 | + std::unique_ptr<fds_xml_t, decltype(&fds_xml_destroy)> xml(fds_xml_create(), &fds_xml_destroy); |
| 119 | + if (!xml) { |
| 120 | + throw std::runtime_error("Failed to create an XML parser!"); |
| 121 | + } |
| 122 | + |
| 123 | + if (fds_xml_set_args(xml.get(), args_params) != FDS_OK) { |
| 124 | + std::string err = fds_xml_last_err(xml.get()); |
| 125 | + throw std::runtime_error("Failed to parse the description of an XML document: " + err); |
| 126 | + } |
| 127 | + |
| 128 | + fds_xml_ctx_t *params_ctx = fds_xml_parse_mem(xml.get(), params, true); |
| 129 | + if (!params_ctx) { |
| 130 | + std::string err = fds_xml_last_err(xml.get()); |
| 131 | + throw std::runtime_error("Failed to parse the configuration: " + err); |
| 132 | + } |
| 133 | + |
| 134 | + // Parse parameters and check configuration |
| 135 | + try { |
| 136 | + parse_params(params_ctx); |
| 137 | + check_validity(); |
| 138 | + } catch (std::exception &ex) { |
| 139 | + throw std::runtime_error("Failed to parse the configuration: " + std::string(ex.what())); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +Config::~Config() |
| 144 | +{ |
| 145 | + // Nothing to do |
| 146 | +} |
0 commit comments