summaryrefslogtreecommitdiffstats
path: root/src/lib/yang/adaptor_config.h
blob: 88cfd4c3f4d831856a48d57ce181e2db013fca9d (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// 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_ADAPTOR_CONFIG_H
#define ISC_ADAPTOR_CONFIG_H 1

#include <yang/adaptor_host.h>
#include <yang/adaptor_option.h>
#include <yang/adaptor_pool.h>
#include <yang/adaptor_subnet.h>
#include <list>

namespace isc {
namespace yang {

/// @brief JSON adaptor for Kea server configurations.
///
/// Currently only from JSON to YANG for DHCPv4 and DHCPv6 available
/// as preProcess4 and preProcess6 class methods, filling some required
/// (by YANG) fields (e.g. subnet IDs, or option code and space), or
/// transforming a hand-written JSON configuration into a canonical form.
class AdaptorConfig : public AdaptorHost, public AdaptorOption,
    public AdaptorSubnet {
public:

    /// @brief Constructor.
    AdaptorConfig();

    /// @brief Destructor.
    virtual ~AdaptorConfig();

    /// @brief Pre process a DHCPv4 configuration.
    ///
    /// Assign subnet IDs, check and set defaults in options, etc.
    /// Note even though the parameter is a ConstElementPtr and is not modified,
    /// sub-structures can modify it, so if you need a copy do a deep one.
    ///
    /// @param config The configuration.
    /// @throw MissingKey when a required key is missing.
    /// @throw BadValue when null or not a map or deprecated Logging present.
    /// @note Does nothing if "Dhcp4" is not present in the map.
    static void preProcess4(isc::data::ConstElementPtr config);

    /// @brief Pre process a DHCPv6 configuration.
    ///
    /// Assign subnet IDs, check and set default in options, etc.
    /// Note even though the parameter is a ConstElementPtr and is not modified,
    /// sub-structures can modify it, so if you need a copy do a deep one.
    ///
    /// @param config The configuration.
    /// @throw MissingKey when a required key is missing.
    /// @throw BadValue when null or not a map or deprecated Logging present.
    /// @note Does nothing if "Dhcp6" is not present in the map.
    static void preProcess6(isc::data::ConstElementPtr config);

protected:
    /// @brief Collects subnet-ids on all subnets.
    ///
    /// It will go over all subnets and collect their ids. It will then return
    /// true if all subnets have ids. If the subnets list is empty, it will also
    /// return true. False will be returned if there is at least one subnet that
    /// doesn't have subnet-id.
    ///
    /// @param subnets The subnet list.
    /// @param set The reference to the set of assigned IDs.
    /// @return True if all subnets have an ID, false otherwise.
    static bool subnetsCollectID(isc::data::ConstElementPtr subnets,
                                 SubnetIDSet& set);

    /// @brief Collects subnet-ids in all subnets in all shared network list.
    ///
    /// It will go over all subnets in all shared networks specified to collect
    /// their subnet-ids. It will then return true if all subnets have ids. If
    /// the subnets list is empty, it will also return true. False will be
    /// returned if there is at least one subnet that doesn't have subnet-id.
    ///
    /// @param networks The shared network list.
    /// @param set The reference to the set of assigned IDs.
    /// @param subsel The subnet list name.
    /// @return True if all subnets have an ID, false otherwise.
    static bool sharedNetworksCollectID(isc::data::ConstElementPtr networks,
                                        SubnetIDSet& set,
                                        const std::string& subsel);

    /// @brief Assigns subnet-id to every subnet in a subnet list.
    ///
    /// Only those subnets that don't have one subnet-id assigned yet,
    /// will get a new subnet-id value.
    ///
    /// @param subnets The subnet list.
    /// @param set The reference to the set of assigned IDs.
    /// @param next The next ID.
    static void subnetsAssignID(isc::data::ConstElementPtr subnets,
                                SubnetIDSet& set, isc::dhcp::SubnetID& next);

    /// @brief Assigns subnet-id to every subnet in a shared network list.
    ///
    /// Only those subnets that don't have one subnet-id assigned yet,
    /// will get a new subnet-id value.
    ///
    /// @param networks The shared network list.
    /// @param set The reference to the set of assigned IDs.
    /// @param next The next ID.
    /// @param subsel The subnet list name.
    static void sharedNetworksAssignID(isc::data::ConstElementPtr networks,
                                       SubnetIDSet& set,
                                       isc::dhcp::SubnetID& next,
                                       const std::string& subsel);

    /// @brief Sanitizes all pools in a pools list.
    ///
    /// Goes over each pool and cleans up its definition (removes extra spaces,
    /// adds one space before and after - ).
    ///
    /// @param pools The pool list.
    static void sanitizePools(isc::data::ConstElementPtr pools);

    /// @brief Sanitizes all pools in a subnets list.
    ///
    /// @param subnets The subnet list.
    static void sanitizePoolsInSubnets(isc::data::ConstElementPtr subnets);

    /// @brief Sanitizes all pools in all subnets in a shared network list.
    ///
    /// @param networks The shared network list.
    /// @param subsel The subnet list name.
    static void sanitizePoolsInSharedNetworks(isc::data::ConstElementPtr networks,
                                              const std::string& subsel);

    /// @brief Collect option definitions from an option definition list.
    ///
    /// Collects options definitions, but also sets missing option space
    /// with default.
    ///
    /// @param defs The option definition list.
    /// @param space The default space name (missing will be filled with this)
    /// @param codes Option definitions.
    static void sanitizeOptionDefList(isc::data::ConstElementPtr defs,
                                      const std::string& space,
                                      OptionCodes& codes);

    /// @brief Set missing option codes to an option data list.
    ///
    /// @param options The option data list.
    /// @param space The default space name.
    /// @param codes Option definitions.
    static void sanitizeOptionDataList(isc::data::ConstElementPtr options,
                                       const std::string& space,
                                       const OptionCodes& codes);

    /// @brief Collect option definitions from a client class list
    /// and set missing option codes.
    ///
    /// @param classes The client class list.
    /// @param space The default space name.
    /// @param codes Option definitions.
    static void sanitizeOptionClasses(isc::data::ConstElementPtr classes,
                                      const std::string& space,
                                      OptionCodes& codes);

    /// @brief Set missing option codes to a pool list.
    ///
    /// @param pools The pool list.
    /// @param space The default space name.
    /// @param codes Option definitions.
    static void sanitizeOptionPools(isc::data::ConstElementPtr pools,
                                    const std::string& space,
                                    const OptionCodes& codes);

    /// @brief Set missing option codes to a host reservation list.
    ///
    /// @param hosts The host reservation list.
    /// @param space The default space name.
    /// @param codes Option definitions.
    static void sanitizeOptionHosts(isc::data::ConstElementPtr hosts,
                                    const std::string& space,
                                    const OptionCodes& codes);

    /// @brief Set missing option codes to a subnet list.
    ///
    /// @param subnets The subnet list.
    /// @param space The default space name.
    /// @param codes Option definitions.
    static void sanitizeOptionSubnets(isc::data::ConstElementPtr subnets,
                                      const std::string& space,
                                      const OptionCodes& codes);

    /// @brief Set missing option codes to a shared network list.
    ///
    /// @param networks The shared network list.
    /// @param space The default space name.
    /// @param codes Option definitions.
    static void sanitizeOptionSharedNetworks(isc::data::ConstElementPtr networks,
                                             const std::string& space,
                                             const OptionCodes& codes);

    /// @brief Process require client classes in a pool list.
    ///
    /// Remove empty require client class list.
    ///
    /// @param pools The pool list.
    static void sanitizeRequireClassesPools(isc::data::ConstElementPtr pools);

    /// @brief Process require client classes in a subnet list.
    ///
    /// Remove empty require client class lists.
    ///
    /// @param subnets The subnet list.
    static void sanitizeRequireClassesSubnets(isc::data::ConstElementPtr subnets);

    /// @brief Process require client classes in a shared network list.
    ///
    /// Remove empty require client class lists.
    ///
    /// @param networks The shared network list.
    /// @param subsel The subnet list name.
    static void requireClassesSharedNetworks(isc::data::ConstElementPtr networks,
                                             const std::string& subsel);

    /// @brief Process host reservation list.
    ///
    /// Quote when needed flex-id identifiers.
    ///
    /// @param hosts The host reservation list.
    static void sanitizeHostList(isc::data::ConstElementPtr hosts);

    /// @brief Process host reservations in a subnet list.
    ///
    /// Quote when needed flex-id identifiers.
    ///
    /// @param subnets The subnet list.
    static void sanitizeHostSubnets(isc::data::ConstElementPtr subnets);

    /// @brief Process host reservations in a shared network list.
    ///
    /// Quote when needed flex-id identifiers.
    ///
    /// @param networks The shared network list.
    /// @param space The default space name.
    static void SanitizeHostsInSharedNetworks(isc::data::ConstElementPtr networks,
                                              const std::string& space);

    /// @brief Sanitizes relay information in subnets in a subnet list.
    ///
    /// Force the use of ip-addresses when it finds an ip-address entry.
    ///
    /// @param subnets The subnet list.
    static void sanitizeRelaySubnets(isc::data::ConstElementPtr subnets);

    /// @brief Sanitizes relay information in a shared network list.
    ///
    /// Force the use of ip-addresses when it finds an ip-address entry.
    ///
    /// @param networks The shared network list.
    /// @param subsel The subnet list name.
    static void sanitizeRelayInSharedNetworks(isc::data::ConstElementPtr networks,
                                              const std::string& subsel);

    /// @brief Update (hosts) database.
    ///
    /// Force the use of hosts-databases vs. hosts-database.
    ///
    /// @param dhcp The DHCP server.
    static void sanitizeDatabase(isc::data::ConstElementPtr dhcp);

    /// @brief Update relay supplied options.
    ///
    /// Remove empty relay supplied option list.
    ///
    /// @param dhcp The DHCPv6 server.
    static void sanitizeRelaySuppliedOptions(isc::data::ConstElementPtr dhcp);

    /// @brief Pre process a configuration.
    ///
    /// Assign subnet IDs, check and set default in options, etc.
    ///
    /// @param dhcp The server configuration.
    /// @param subsel The subnet list name.
    /// @param space The default option space name.
    /// @throw MissingKey when a required key is missing.
    static void preProcess(isc::data::ElementPtr dhcp,
                           const std::string& subsel,
                           const std::string& space);
};

}  // end of namespace isc::yang
}  // end of namespace isc

#endif // ISC_ADAPTOR_CONFIG_H