summaryrefslogtreecommitdiffstats
path: root/src/lib/yang/translator_pool.h
blob: 90b9f352f3ebd890700f6450b5f720585d0802e4 (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// 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_POOL_H
#define ISC_TRANSLATOR_POOL_H 1

#include <yang/translator_option_data.h>

namespace isc {
namespace yang {

/// @brief A translator class for converting a pool between YANG and JSON.
///
/// Currently supports on kea-dhcp[46]-server and partially ietf-dhcpv6-server.
///
/// JSON syntax for both kea-dhcp4 and kea-dhcp6 is:
/// @code
/// {
///     "pool": "<pool prefix or start - end addresses>",
///     "option-data": [ <list of option data> ],
///     "client-class": "<guard class name>",
///     "require-client-classes": [ <list of required class names> ],
///     "user-context": { <json map> },
///     "comment": "<comment>"
/// }
/// @endcode
///
/// YANG syntax is for ietf-dhcpv6-server is with pool-id as the key:
/// @code
///  +--rw pool-id               uint32
///  +--rw pool-prefix           inet:ipv6-prefix
///  +--rw start-address         inet:ipv6-address-no-zone
///  +--rw end-address           inet:ipv6-address-no-zone
///  +--rw valid-lifetime        yang:timeticks
///  +--rw renew-time            yang:timeticks
///  +--rw rebind-time           yang:timeticks
///  +--rw preferred-lifetime    yang:timeticks
///  +--rw rapid-commit?         boolean
///  +--rw client-class?         string
///  +--rw max-address-count     threshold
///  +--rw option-set-id
///     /server/server-config/option-sets/option-set/option-set-id
/// @endcode
///
/// YANG syntax for kea-dhcp[46] is with start-address and end-address
/// as the keys:
/// @code
/// +--rw pool* [start-address end-address]
///    +--rw prefix?                   inet:ipv[46]-prefix
///    +--rw start-address             inet:ipv[46]-address
///    +--rw end-address               inet:ipv[46]-address
///    +--rw option-data* [code space]
///    +--rw client-class?             string
///    +--rw require-client-classes*   string
///    +--rw user-context?             user-context
/// @endcode
///
/// An example in JSON and YANG formats for the IETF model:
/// @code
/// [
///     {
///         "pool": "2001:db8::/112"
///     }
/// ]
/// @endcode
/// @code
///  /ietf-dhcpv6-server:server (container)
///  /ietf-dhcpv6-server:server/server-config (container)
///  /ietf-dhcpv6-server:server/server-config/network-ranges (container)
///  /ietf-dhcpv6-server:server/server-config/network-ranges/
///     network-range[network-range-id='111'] (list instance)
///  /ietf-dhcpv6-server:server/server-config/network-ranges/
///     network-range[network-range-id='111']/network-range-id = 111
///  /ietf-dhcpv6-server:server/server-config/network-ranges/
///     network-range[network-range-id='111']/network-prefix = 2001:db8::/48
///  /ietf-dhcpv6-server:server/server-config/network-ranges/
///     network-range[network-range-id='111']/address-pools (container)
///  /ietf-dhcpv6-server:server/server-config/network-ranges/
///     network-range[network-range-id='111']/address-pools/
///     address-pool[pool-id='0'] (list instance)
///  /ietf-dhcpv6-server:server/server-config/network-ranges/
///     network-range[network-range-id='111']/address-pools/
///     address-pool[pool-id='0']/pool-id = 0
///  /ietf-dhcpv6-server:server/server-config/network-ranges/
///     network-range[network-range-id='111']/address-pools/
///     address-pool[pool-id='0']/pool-prefix = 2001:db8::1:0/112
///  /ietf-dhcpv6-server:server/server-config/network-ranges/
///     network-range[network-range-id='111']/address-pools/
///     address-pool[pool-id='0']/start-address = 2001:db8::1:0
///  /ietf-dhcpv6-server:server/server-config/network-ranges/
///     network-range[network-range-id='111']/address-pools/
///     address-pool[pool-id='0']/end-address = 2001:db8::1:ffff
///  /ietf-dhcpv6-server:server/server-config/network-ranges/
///     network-range[network-range-id='111']/address-pools/
///     address-pool[pool-id='0']/max-address-count = disabled
/// @endcode
///
/// An example in JSON and YANG formats for the Kea model:
/// @code
/// [
///     {
///         "pool": "2001:db8::1 - 2001:db8::100"
///     }
/// ]
/// @endcode
/// @code
///  /kea-dhcp6-server:config (container)
///  /kea-dhcp6-server:config/subnet6[id='111'] (list instance)
///  /kea-dhcp6-server:config/subnet6[id='111']/id = 111
///  /kea-dhcp6-server:config/subnet6[id='111']/subnet = 2001:db8::/48
///  /kea-dhcp6-server:config/subnet6[id='111']/
///     pool[start-address='2001:db8::1'][end-address='2001:db8::100']
///     (list instance)
///  /kea-dhcp6-server:config/subnet6[id='111']/
///     pool[start-address='2001:db8::1'][end-address='2001:db8::100']/
///     start-address = 2001:db8::1
///  /kea-dhcp6-server:config/subnet6[id='111']/
///     pool[start-address='2001:db8::1'][end-address='2001:db8::100']/
///     end-address = 2001:db8::100
/// @endcode
class TranslatorPool : virtual public TranslatorOptionDataList {
public:
    /// @brief Constructor.
    ///
    /// @param session Sysrepo session.
    /// @param model Model name.
    TranslatorPool(sysrepo::Session session, const std::string& model);

    /// @brief Destructor.
    virtual ~TranslatorPool() = default;

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

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

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

    /// @brief Get start and end addresses from prefix.
    ///
    /// @param prefix The prefix string.
    /// @param start_address The reference to the start_address.
    /// @param end_address The reference to the end_address.
    /// @throw BadValue when the prefix is not correctly formed.
    static void getAddresses(const std::string& prefix,
                             std::string& start_address,
                             std::string& end_address);

protected:
    /// @brief getPool for ietf-dhcpv6-server.
    ///
    /// @param data_node the YANG node representing the pool configuration
    ///
    /// @return JSON representation of the pool.
    ///
    /// @throw BadValue on pool without prefix.
    isc::data::ElementPtr getPoolIetf6(libyang::DataNode const& data_node);

    /// @brief setPool for ietf-dhcpv6-server.
    ///
    /// @param xpath The xpath of the pool.
    /// @param elem The JSON element.
    /// @throw BadValue on pool without prefix and with a range which is
    /// not a prefix.
    void setPoolIetf6(const std::string& xpath,
                      isc::data::ConstElementPtr elem);

    /// @brief getPool for kea-dhcp[46]-server.
    ///
    /// @param data_node the YANG node representing the pool configuration
    ///
    /// @return JSON representation of the pool.
    ///
    /// @throw BadValue on a pool without prefix and start or end address.
    isc::data::ElementPtr getPoolKea(libyang::DataNode const& data_node);

    /// @brief setPool for kea-dhcp[46]-server.
    ///
    /// @param xpath The xpath of the pool.
    /// @param elem The JSON element.
    /// @throw BadValue on a pool without a well formed prefix.
    void setPoolKea(const std::string& xpath, isc::data::ConstElementPtr elem);
};  // TranslatorPool

/// @brief A translator class for converting pools between YANG and JSON.
///
/// Currently supports on kea-dhcp[46]-server and partially ietf-dhcpv6-server.
class TranslatorPools : virtual public TranslatorPool {
public:
    /// @brief Constructor.
    ///
    /// @param session Sysrepo session.
    /// @param model Model name.
    TranslatorPools(sysrepo::Session session, const std::string& model);

    /// @brief Destructor.
    virtual ~TranslatorPools() = default;

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

    /// @brief Translate pools from YANG to JSON.
    ///
    /// @param xpath The xpath of the pool list.
    ///
    /// @return the JSON representation of the list of pools
    ///
    /// @throw NetconfError when sysrepo raises an error.
    isc::data::ElementPtr getPoolsFromAbsoluteXpath(std::string const& xpath);

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

protected:
    /// @brief getPools for ietf-dhcpv6-server.
    ///
    /// @param data_node the YANG node representing the list of pools
    ///
    /// @return the JSON representation of the list of pools
    isc::data::ElementPtr getPoolsIetf(libyang::DataNode const& data_node);

    /// @brief getPools for kea-dhcp[46]-server.
    ///
    /// @param data_node the YANG node representing the list of pools
    ///
    /// @return the JSON representation of the list of pools
    isc::data::ElementPtr getPoolsKea(libyang::DataNode const& data_node);

    /// @brief setPools using pool-id.
    ///
    /// @param xpath The xpath of the pool list.
    /// @param elem The JSON element.
    void setPoolsById(const std::string& xpath,
                      isc::data::ConstElementPtr elem);

    /// @brief setPools using address pair.
    ///
    /// @param xpath The xpath of the pool list.
    /// @param elem The JSON element.
    /// @throw BadValue on a pool without a prefix.
    void setPoolsByAddresses(const std::string& xpath,
                             isc::data::ConstElementPtr elem);
};  // TranslatorPools

}  // namespace yang
}  // namespace isc

#endif  // ISC_TRANSLATOR_POOL_H