1 // Copyright (C) 2018-2021 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef ISC_TRANSLATOR_PD_POOL_H
8 #define ISC_TRANSLATOR_PD_POOL_H 1
9 
10 #include <yang/translator_option_data.h>
11 #include <list>
12 
13 namespace isc {
14 namespace yang {
15 
16 /// Prefix delegation pool translation between YANG and JSON
17 ///
18 /// JSON syntax for kea-dhcp6-server is:
19 /// @code
20 /// {
21 ///     "prefix": <prefix base>,
22 ///     "prefix-len": <prefix length>,
23 ///     "delegated-len": <delegated length>,
24 ///     "excluded-prefix": <excluded prefix>,
25 ///     "excluded-prefix-len": <excluded prefix length>,
26 ///     "option-data": [ <list of option data> ],
27 ///     "client-class": "<guard class name>",
28 ///     "require-client-classes": [ <list of required class names> ],
29 ///     "user-context": { <json map> },
30 ///     "comment": "<comment>"
31 /// }
32 /// @endcode
33 ///
34 /// YANG syntax for ietf-dhcpv6-server is with pool-id as the key.
35 /// @code
36 ///  +--rw pool-id                     uint32
37 ///  +--rw prefix                      inet:ipv6-prefix
38 ///  +--rw prefix-length               uint8
39 ///  +--rw valid-lifetime              yang:timeticks
40 ///  +--rw renew-time                  yang:timeticks
41 ///  +--rw rebind-time                 yang:timeticks
42 ///  +--rw preferred-lifetime          yang:timeticks
43 ///  +--rw rapid-commit?               boolean
44 ///  +--rw client-class?               string
45 ///  +--rw max-pd-space-utilization?   threshold
46 ///  +--rw option-set-id?
47 ///     /server/server-config/option-sets/option-set/option-set-id
48 /// @endcode
49 ///
50 /// YANG syntax for kea-dhcp6-server is with prefix as the key.
51 /// @code
52 ///  +--rw prefix?                  inet:ipv6-prefix
53 ///  +--rw delegated-len?           uint8
54 ///  +--rw excluded-prefix?         inet:ipv6-prefix
55 ///  +--rw option-data*
56 ///  +--rw client-class?            string
57 ///  +--rw require-client-classes*  string
58 ///  +--rw user-context?            string
59 /// @endcode
60 ///
61 /// An example in JSON and YANG formats:
62 /// @code
63 /// [
64 ///     {
65 ///         "prefix": "2001:db8:0:1000::",
66 ///         "prefix-len": 56,
67 ///         "delegated-len": 64
68 ///     }
69 /// ]
70 /// @endcode
71 /// @code
72 ///  /ietf-dhcpv6-server:server (container)
73 ///  /ietf-dhcpv6-server:server/server-config (container)
74 ///  /ietf-dhcpv6-server:server/server-config/network-ranges (container)
75 ///  /ietf-dhcpv6-server:server/server-config/network-ranges
76 ///     network-range[network-range-id='111'] (list instance)
77 ///  /ietf-dhcpv6-server:server/server-config/network-ranges
78 ///     network-range[network-range-id='111']/network-range-id = 111
79 ///  /ietf-dhcpv6-server:server/server-config/network-ranges
80 ///     network-range[network-range-id='111']/network-prefix = 2001:db8::/48
81 ///  /ietf-dhcpv6-server:server/server-config/network-ranges
82 ///     network-range[network-range-id='111']/pd-pools (container)
83 ///  /ietf-dhcpv6-server:server/server-config/network-ranges
84 ///     network-range[network-range-id='111']/pd-pools/
85 ///     pd-pool[pool-id='0'] (list instance)
86 ///  /ietf-dhcpv6-server:server/server-config/network-ranges/
87 ///     pd-pool[pool-id='0']/pool-id = 0
88 ///     network-range[network-range-id='111']/pd-pools/
89 ///     pd-pool[pool-id='0']/prefix = 2001:db8:0:1000::/56
90 ///  /ietf-dhcpv6-server:server/server-config/network-ranges
91 ///     network-range[network-range-id='111']/pd-pools/
92 ///     pd-pool[pool-id='0']/prefix-length = 56
93 ///  /ietf-dhcpv6-server:server/server-config/network-ranges
94 ///     network-range[network-range-id='111']/pd-pools/
95 ///     pd-pool[pool-id='0']/max-pd-space-utilization = disabled
96 /// @endcode
97 /// @code
98 ///  /kea-dhcp6-server:config (container)
99 ///  /kea-dhcp6-server:config/subnet6[id='111'] (list instance)
100 ///  /kea-dhcp6-server:config/subnet6[id='111']/id = 111
101 ///  /kea-dhcp6-server:config/subnet6[id='111']/subnet = 2001:db8::/48
102 ///  /kea-dhcp6-server:config/subnet6[id='111']/
103 ///     pd-pool[prefix='2001:db8:0:1000::/56' (list instance)
104 ///  /kea-dhcp6-server:config/subnet6[id='111']/
105 ///     pd-pool[prefix='2001:db8:0:1000::/56'/prefix = 2001:db8:0:1000::/56
106 ///  /kea-dhcp6-server:config/subnet6[id='111']/
107 ///     pd-pool[prefix='2001:db8:0:1000::/56'/delegated-len = 64
108 /// @endcode
109 
110 /// @brief A translator class for converting a pd-pool between
111 /// YANG and JSON.
112 ///
113 /// Currently supported models:
114 /// - kea-dhcp6-server
115 /// - ietf-dhcpv6-server (partial support)
116 class TranslatorPdPool : virtual public TranslatorOptionDataList {
117 public:
118 
119     /// @brief Constructor.
120     ///
121     /// @param session Sysrepo session.
122     /// @param model Model name.
123     TranslatorPdPool(sysrepo::S_Session session, const std::string& model);
124 
125     /// @brief Destructor.
126     virtual ~TranslatorPdPool();
127 
128     /// @brief Get and translate a pd-pool from YANG to JSON.
129     ///
130     /// @param xpath The xpath of the pd-pool.
131     /// @return JSON representation of the pd-pool.
132     /// @throw SysrepoError when sysrepo raises an error.
133     /// @throw BadValue on pd-pool without well formed prefix.
134     isc::data::ElementPtr getPdPool(const std::string& xpath);
135 
136     /// @brief Translate and set pd-pool from JSON to YANG.
137     ///
138     /// @param xpath The xpath of the pd-pool.
139     /// @param elem The JSON element.
140     void setPdPool(const std::string& xpath, isc::data::ConstElementPtr elem);
141 
142 protected:
143     /// @brief getPdPool for ietf-dhcpv6-server.
144     ///
145     /// @param xpath The xpath of the pd-pool.
146     /// @return JSON representation of the pd-pool.
147     /// @throw SysrepoError when sysrepo raises an error.
148     isc::data::ElementPtr getPdPoolIetf6(const std::string& xpath);
149 
150     /// @brief setPdPool for ietf-dhcpv6-server.
151     ///
152     /// @param xpath The xpath of the pd-pool.
153     /// @param elem The JSON element.
154     /// @throw BadValue on pd-pool without prefix or prefix length.
155     void setPdPoolIetf6(const std::string& xpath,
156                         isc::data::ConstElementPtr elem);
157 
158     /// @brief getPdPool for kea-dhcp6-server.
159     ///
160     /// @param xpath The xpath of the pd-pool.
161     /// @return JSON representation of the pd-pool.
162     /// @throw SysrepoError when sysrepo raises an error.
163     isc::data::ElementPtr getPdPoolKea(const std::string& xpath);
164 
165     /// @brief setPdPool for kea-dhcp6-server.
166     ///
167     /// @param xpath The xpath of the pd-pool.
168     /// @param elem The JSON element.
169     void setPdPoolKea(const std::string& xpath,
170                       isc::data::ConstElementPtr elem);
171 };
172 
173 /// @brief A translator class for converting a pd-pool list between
174 /// YANG and JSON.
175 ///
176 /// Currently supports the following models:
177 /// - kea-dhcp6-server
178 /// - ietf-dhcpv6-server (partial support)
179 class TranslatorPdPools : virtual public TranslatorPdPool {
180 public:
181 
182     /// @brief Constructor.
183     ///
184     /// @param session Sysrepo session.
185     /// @param model Model name.
186     TranslatorPdPools(sysrepo::S_Session session, const std::string& model);
187 
188     /// @brief Destructor.
189     virtual ~TranslatorPdPools();
190 
191     /// @brief Get and translate pd-pools from YANG to JSON.
192     ///
193     /// @param xpath The xpath of the pd-pool list.
194     /// @throw SysrepoError when sysrepo raises an error.
195     isc::data::ElementPtr getPdPools(const std::string& xpath);
196 
197     /// @brief Translate and set pd-pools from JSON to YANG.
198     ///
199     /// @param xpath The xpath of the pd-pool list.
200     /// @param elem The JSON element.
201     void setPdPools(const std::string& xpath, isc::data::ConstElementPtr elem);
202 
203 protected:
204     /// @brief getPdPools common part.
205     ///
206     /// @param xpath The xpath of the pd-pool list.
207     /// @throw SysrepoError when sysrepo raises an error.
208     isc::data::ElementPtr getPdPoolsCommon(const std::string& xpath);
209 
210     /// @brief setPdPools using pool-id.
211     ///
212     /// @param xpath The xpath of the pd-pool list.
213     /// @param elem The JSON element.
214     void setPdPoolsId(const std::string& xpath,
215                       isc::data::ConstElementPtr elem);
216 
217     /// @brief setPdPools using prefix.
218     ///
219     /// @param xpath The xpath of the pd-pool list.
220     /// @param elem The JSON element.
221     /// @throw BadValue on pd-pool without prefix or prefix length.
222     void setPdPoolsPrefix(const std::string& xpath,
223                           isc::data::ConstElementPtr elem);
224 };
225 
226 }  // namespace yang
227 }  // namespace isc
228 
229 #endif // ISC_TRANSLATOR_PD_POOL_H
230