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
|
// Copyright (C) 2018-2022 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>
#include <list>
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 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 TranslatorBasic {
public:
/// @brief Constructor.
///
/// @param session Sysrepo session.
/// @param model Model name.
TranslatorDatabase(sysrepo::S_Session session, const std::string& model);
/// @brief Destructor.
virtual ~TranslatorDatabase();
/// @brief Get and translate a database access from YANG to JSON.
///
/// @param xpath The xpath of the database.
/// @return JSON representation of the database.
/// @throw SysrepoError when sysrepo raises an error.
isc::data::ElementPtr getDatabase(const std::string& 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 xpath The xpath of the database.
/// @return JSON representation of the database or null if none.
/// @throw SysrepoError when sysrepo raises an error.
isc::data::ElementPtr getDatabaseKea(const std::string& xpath);
/// @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);
};
/// @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::S_Session session, const std::string& model);
/// @brief Destructor.
virtual ~TranslatorDatabases();
/// @brief Get and translate database accesses from YANG to JSON.
///
/// @param xpath The xpath of databases including the list name.
/// @return JSON representation of databases.
/// @throw SysrepoError when sysrepo raises an error.
isc::data::ConstElementPtr getDatabases(const std::string& 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 xpath The xpath of databases including the list name.
/// @return JSON representation of databases.
/// @throw SysrepoError when sysrepo raises an error.
isc::data::ElementPtr getDatabasesKea(const std::string& 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);
};
} // namespace yang
} // namespace isc
#endif // ISC_TRANSLATOR_DATABASE_H
|