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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (C) 2018-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 ISC_TRANSLATOR_LOGGER_H
#define ISC_TRANSLATOR_LOGGER_H 1

#include <yang/translator.h>

namespace isc {
namespace yang {

/// Logger translation between YANG and JSON
///
/// JSON syntax for all Kea servers with loggers is:
/// @code
/// {
///     "name": <name>,
///     "output-options": [ <output options> ],
///     "severity": <severity>,
///     "debuglevel": <debug level>,
///     "user-context": { <json map> },
///     "comment": <comment>
/// }
/// @endcode
///
/// JSON syntax for all Kea server for output options is:
/// @code
/// {
///    "output": <output, e.g. log file name>,
///    "maxver": <maximum file version>,
///    "maxsize": <maximum file size>,
///    "flush": <flush flag>,
///    "pattern": <custom layout>
/// }
/// @endcode
///
/// YANG syntax for loggers is:
/// @code
/// +--rw logger* [name]
///    +--rw name             string
///    +--rw output-option* [output]
///    |  +--rw output        string
///    |  +--rw flush?        boolean
///    |  +--rw maxsize?      uint32
///    |  +--rw maxver?       uint32
///    |  +--rw pattern?      string
///    +--rw debuglevel?      uint8
///    +--rw severity?        enumeration
///    +--rw user-context?    user-context
/// @endcode
///
/// An example in JSON and YANG formats:
/// @code
/// [
///     {
///         "name": "foo",
///         "severity": "WARN",
///         "output-options":
///             [
///                 {
///                     "output": "/bar",
///                     "maxver": 10
///                 }
///             ]
///     }
/// ]
/// @endcode
/// @code
///  /kea-dhcp4-server:config (container)
///  /kea-dhcp4-server:config/...
///  /kea-dhcp4-server:config/logger[name='foo'] (list instance)
///  /kea-dhcp4-server:config/logger[name='foo']/name = foo
///  /kea-dhcp4-server:config/logger[name='foo']/
///     option[output='/bar'] (list instance)
///  /kea-dhcp4-server:config/logger[name='foo']/
///     option[output='/bar']/option = /bar
///  /kea-dhcp4-server:config/logger[name='foo']/
///     option[output='/bar']/maxver = 10
///  /kea-dhcp4-server:config/logger[name='foo']/severity = WARN
/// @endcode

/// @brief A translator class for converting a logger between
/// YANG and JSON.
///
/// Currently supports all kea models:
/// - kea-dhcp4-server
/// - kea-dhcp6-server
/// - kea-dhcp-ddns
/// - kea-ctrl-agent
class TranslatorLogger : virtual public Translator {
public:
    /// @brief Constructor.
    ///
    /// @param session Sysrepo session.
    /// @param model Model name.
    TranslatorLogger(sysrepo::Session session, const std::string& model);

    /// @brief Destructor.
    virtual ~TranslatorLogger() = default;<--- Destructor in derived class<--- Virtual destructor in base class

    /// @brief Translate a logger from YANG to JSON.
    ///
    /// @param data_node the YANG node representing the logger configuration
    ///
    /// @return JSON representation of the logger.
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getLogger(libyang::DataNode const& data_node);

    /// @brief Translate and set logger from JSON to YANG.
    ///
    /// @param xpath The xpath of the logger.
    /// @param elem The JSON element.
    void setLogger(const std::string& xpath, isc::data::ConstElementPtr elem);

protected:
    /// @brief Translate an output option from YANG to JSON.
    ///
    /// @param data_node the YANG node representing the output option
    ///
    /// @return JSON representation of the output option.
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getOutputOption(libyang::DataNode const& data_node);

    /// @brief Translate output options from YANG to JSON.
    ///
    /// @param data_node the YANG node representing output options
    ///
    /// @return JSON representation of output options.
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getOutputOptions(libyang::DataNode const& data_node);

    /// @brief Translate and set an output option from JSON to YANG.
    ///
    /// @param xpath The xpath of the output option.
    /// @param elem The JSON element.
    void setOutputOption(const std::string& xpath,
                         isc::data::ConstElementPtr elem);

    /// @brief Translate and set output options from JSON to YANG.
    ///
    /// @param xpath The xpath of the output options.
    /// @param elem The JSON element.
    /// @throw BadValue on an output option without output.
    void setOutputOptions(const std::string& xpath,
                          isc::data::ConstElementPtr elem);

    /// @brief getLogger JSON for loggers.
    ///
    /// @param data_node the YANG node representing the logger configuration
    ///
    /// @return JSON representation of the logger.
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getLoggerKea(libyang::DataNode const& data_node);

    /// @brief setLogger for loggers.
    ///
    /// @param xpath The xpath of the logger.
    /// @param elem The JSON element.
    void setLoggerKea(const std::string& xpath,
                      isc::data::ConstElementPtr elem);
};  // TranslatorLogger

/// @brief A translator class for converting a logger list between
/// YANG and JSON.
///
/// Currently supports all kea servers and agents. Specific to Kea.
class TranslatorLoggers : virtual public TranslatorLogger {
public:
    /// @brief Constructor.
    ///
    /// @param session Sysrepo session.
    /// @param model Model name.
    TranslatorLoggers(sysrepo::Session session, const std::string& model);

    /// @brief Destructor.
    virtual ~TranslatorLoggers() = default;<--- Destructor in derived class

    /// @brief Translate loggers from YANG to JSON.
    ///
    /// @param data_node the YANG node representing the list of loggers
    ///
    /// @return the JSON representation of the list of loggers
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ConstElementPtr getLoggers(libyang::DataNode const& data_node);

    /// @brief Translate loggers from YANG to JSON.
    ///
    /// @param xpath The xpath of loggers.
    ///
    /// @return JSON representation of loggers.
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ConstElementPtr getLoggersFromAbsoluteXpath(std::string const& xpath);

    /// @brief Translate and set loggers from JSON to YANG.
    ///
    /// @param xpath The xpath of loggers.
    ///
    /// @param elem The JSON element.
    void setLoggers(const std::string& xpath,
                    isc::data::ConstElementPtr elem);

protected:
    /// @brief getLoggers JSON for loggers.
    ///
    /// @param data_node the YANG node representing loggers configuration
    ///
    /// @return JSON representation of loggers.
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getLoggersKea(libyang::DataNode const& data_node);

    /// @brief setLoggers for loggers.
    ///
    /// @param xpath The xpath of loggers.
    /// @param elem The JSON element.
    /// @throw BadValue on a logger without name.
    void setLoggersKea(const std::string& xpath,
                       isc::data::ConstElementPtr elem);
};  // TranslatorLoggers

}  // namespace yang
}  // namespace isc

#endif  // ISC_TRANSLATOR_LOGGER_H