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
234
235
236
237
238
239
240
241
242
243
244
// Copyright (C) 2018-2024 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_DATABASE_H
#define ISC_TRANSLATOR_DATABASE_H 1

#include <yang/translator.h>

namespace isc {
namespace yang {

/// Database access translation between YANG and JSON
///
/// JSON syntax for all Kea servers with database access is:
/// @code
/// {
///     "type": <type>, /// required
///     "user": <user>,
///     "password": <password>,
///     "host": <host>,
///     "name": <name>,
///     "persist": <persist flag>,
///     "port": <port>,
///     "lfc-interval": <lfc interval>,
///     "readonly": <readonly flag>,
///     "trust-anchor": <trust anchor>,
///     "cert-file": <cert file>,
///     "key-file": <key file>,
///     "cipher-list": <cipher list>,
///     "connect-timeout": <connect timeout>,
///     "max-reconnect-tries": <maximum reconnect tries>,
///     "reconnect-wait-time": <reconnect wait time>,
///     "max-row-errors": <maximum row errors>,
///     "user-context": { <json map> },
///     "comment": <comment>
/// }
/// @endcode
///
/// YANG syntax for kea-dhcp[46] is using database-type as the list key:
/// @code
/// +--rw database
///    +--rw database-type          string
///    +--rw user?                  string
///    +--rw password?              string
///    +--rw host?                  string
///    +--rw name?                  string
///    +--rw persist?               boolean
///    +--rw port?                  uint16
///    +--rw lfc-interval?          uint32
///    +--rw readonly?              boolean
///    +--rw trust-anchor?          string
///    +--rw cert-file?             string
///    +--rw key-file?              string
///    +--rw cipher-list?           string
///    +--rw connect-timeout?       uint32
///    +--rw max-reconnect-tries?   uint32
///    +--rw reconnect-wait-time?   uint32
///    +--rw max-row-errors?        uint32
///    +--rw on-fail?               string
///    +--rw retry-on-startup?      boolean
///    +--rw user-context?          user-context
/// @endcode
///
/// An example in JSON and YANG formats:
/// @code
/// [
///     {
///         "type": "mysql",
///         "name": "kea",
///         "user": "kea",
///         "password": "kea",
///         "host": "localhost",
///         "port": 3306
///     }
/// ]
/// @endcode
/// @code
/// /kea-dhcp6-server:config (container)
/// /kea-dhcp6-server:config/
///    hosts-database[database-type='mysql'] (list instance)
/// /kea-dhcp6-server:config/
///    hosts-database[database-type='mysql']/type = mysql
/// /kea-dhcp6-server:config/
///    hosts-database[database-type='mysql']/name = kea
/// /kea-dhcp6-server:config/
///    hosts-database[database-type='mysql']/user = kea
/// /kea-dhcp6-server:config/
///    hosts-database[database-type='mysql']/password = kea
/// /kea-dhcp6-server:config/
///    hosts-database[database-type='mysql']/host = localhost
/// /kea-dhcp6-server:config/
///    hosts-database[database-type='mysql']/port = 3306
/// @endcode

/// @brief A translator class for converting a database access parameters
/// between YANG and JSON.
///
/// Supports the following models:
/// - kea-dhcp4-server
/// - kea-dhcp6-server
class TranslatorDatabase : virtual public Translator {
public:
    /// @brief Constructor.
    ///
    /// @param session Sysrepo session.
    /// @param model Model name.
    TranslatorDatabase(sysrepo::Session session, const std::string& model);

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

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

    /// @brief Translate a database access from YANG to JSON.
    ///
    /// @note This is a computationally expensive operation that makes a lookup in the sysrepo
    /// datastore by calling Session::getData(). It should be used sparingly in production code,
    /// mainly to get an initial data node to work with. It may be used at will in unit tests.
    /// Use getDatabase(libyang::DataNode) as a scalable alternative.
    ///
    /// @param xpath The xpath of the database.
    ///
    /// @return JSON representation of the database.
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getDatabaseFromAbsoluteXpath(std::string const& xpath);

    /// @brief Translate and set database access from JSON to YANG.
    ///
    /// Null elem argument removes the database entry.
    ///
    /// @param xpath The xpath of the database access.
    /// @param elem The JSON element.
    /// @param skip The skip type field flag.
    void setDatabase(const std::string& xpath,
                     isc::data::ConstElementPtr elem,
                     bool skip = false);

protected:
    /// @brief getDatabase JSON for kea-dhcp[46]-server models.
    ///
    /// @param data_node the YANG node representing the database configuration
    ///
    /// @return JSON representation of the database
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getDatabaseKea(libyang::DataNode const& data_node);

    /// @brief setDatabase for kea-dhcp[46]-server models.
    ///
    /// @param xpath The xpath of the database access.
    /// @param elem The JSON element.
    /// @param skip The skip type field flag.
    /// @throw BadValue on database without type,
    void setDatabaseKea(const std::string& xpath,
                        isc::data::ConstElementPtr elem,
                        bool skip);
};  // TranslatorDatabase

/// @brief A translator class for converting a database access list between
/// YANG and JSON.
///
/// Supports kea-dhcp[46]-server, does not exist in ietf-dhcpv6-server.
class TranslatorDatabases : virtual public TranslatorDatabase {
public:
    /// @brief Constructor.
    ///
    /// @param session Sysrepo session.
    /// @param model Model name.
    TranslatorDatabases(sysrepo::Session session, const std::string& model);

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

    /// @brief Translate database accesses from YANG to JSON.
    ///
    /// @param data_node the YANG node representing the databases
    /// @param xpath the xpath of databases relative to {data_node}
    ///
    /// @return the JSON representation of the list of databases
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getDatabases(libyang::DataNode const& data_node,
                                       std::string const& xpath);

    /// @brief Translate database accesses from YANG to JSON.
    ///
    /// @note This is a computationally expensive operation that makes a lookup in the sysrepo
    /// datastore by calling Session::getData(). It should be used sparingly in production code,
    /// mainly to get an initial data node to work with. It may be used at will in unit tests.
    /// Use getDatabases(libyang::DataNode, std::string) as a scalable alternative.
    ///
    /// @param xpath The xpath of databases including the list name.
    ///
    /// @return JSON representation of databases.
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getDatabasesFromAbsoluteXpath(std::string const& xpath);

    /// @brief Translate and set database accesses from JSON to YANG.
    ///
    /// Null elem argument removes the database list.
    ///
    /// @param xpath The xpath of databases including the list name.
    /// @param elem The JSON element.
    void setDatabases(const std::string& xpath,
                      isc::data::ConstElementPtr elem);

protected:
    /// @brief getDatabases JSON for kea-dhcp[46]-server models.
    ///
    /// @param data_node the YANG node representing the databases
    /// @param xpath the xpath of databases relative to {data_node}
    ///
    /// @return JSON representation of databases.
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getDatabasesKea(libyang::DataNode const& data_node,
                                          std::string const& xpath);

    /// @brief setDatabases for kea-dhcp[46]-server models.
    ///
    /// @param xpath The xpath of databases including the list name.
    /// @param elem The JSON element.
    ///
    /// @throw BadValue on database without type,
    void setDatabasesKea(const std::string& xpath,
                         isc::data::ConstElementPtr elem);
};  // TranslatorDatabases

}  // namespace yang
}  // namespace isc

#endif  // ISC_TRANSLATOR_DATABASE_H