Kea  1.5.0
simple_parser4.cc
Go to the documentation of this file.
1 // Copyright (C) 2016-2018 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 #include <config.h>
8 
10 #include <cc/data.h>
11 #include <boost/foreach.hpp>
12 #include <iostream>
13 
14 using namespace isc::data;
15 
16 namespace isc {
17 namespace dhcp {
32 
37 const SimpleDefaults SimpleParser4::OPTION4_DEF_DEFAULTS = {
38  { "record-types", Element::string, ""},
39  { "space", Element::string, "dhcp4"},
40  { "array", Element::boolean, "false"},
41  { "encapsulate", Element::string, "" }
42 };
43 
49 const SimpleDefaults SimpleParser4::OPTION4_DEFAULTS = {
50  { "space", Element::string, "dhcp4"},
51  { "csv-format", Element::boolean, "true"},
52  { "always-send", Element::boolean, "false"}
53 };
54 
60 const SimpleDefaults SimpleParser4::GLOBAL4_DEFAULTS = {
61  { "valid-lifetime", Element::integer, "7200" },
62  { "decline-probation-period", Element::integer, "86400" }, // 24h
63  { "dhcp4o6-port", Element::integer, "0" },
64  { "echo-client-id", Element::boolean, "true" },
65  { "match-client-id", Element::boolean, "true" },
66  { "authoritative", Element::boolean, "false" },
67  { "next-server", Element::string, "0.0.0.0" },
68  { "server-hostname", Element::string, "" },
69  { "boot-file-name", Element::string, "" },
70  { "server-tag", Element::string, "" },
71  { "reservation-mode", Element::string, "all" }
72 };
73 
81 const SimpleDefaults SimpleParser4::SUBNET4_DEFAULTS = {
82  { "id", Element::integer, "0" }, // 0 means autogenerate
83  { "interface", Element::string, "" },
84  { "client-class", Element::string, "" },
85  { "4o6-interface", Element::string, "" },
86  { "4o6-interface-id", Element::string, "" },
87  { "4o6-subnet", Element::string, "" },
88 };
89 
96 const SimpleDefaults SimpleParser4::SHARED_SUBNET4_DEFAULTS = {
97  { "id", Element::integer, "0" }, // 0 means autogenerate
98  { "4o6-interface", Element::string, "" },
99  { "4o6-interface-id", Element::string, "" },
100  { "4o6-subnet", Element::string, "" },
101 };
102 
104 const SimpleDefaults SimpleParser4::SHARED_NETWORK4_DEFAULTS = {
105  { "client-class", Element::string, "" },
106  { "interface", Element::string, "" }
107 };
108 
110 const SimpleDefaults SimpleParser4::IFACE4_DEFAULTS = {
111  { "re-detect", Element::boolean, "true" }
112 };
113 
123 const ParamsList SimpleParser4::INHERIT_TO_SUBNET4 = {
124  "boot-file-name",
125  "client-class",
126  "interface",
127  "match-client-id",
128  "authoritative",
129  "next-server",
130  "rebind-timer",
131  "relay",
132  "renew-timer",
133  "reservation-mode",
134  "server-hostname",
135  "valid-lifetime"
136 };
137 
139 const SimpleDefaults SimpleParser4::DHCP_QUEUE_CONTROL4_DEFAULTS = {
140  { "enable-queue", Element::boolean, "false"},
141  { "queue-type", Element::string, "kea-ring4"},
142  { "capacity", Element::integer, "500"}
143 };
144 
145 
147 
151 
152 size_t SimpleParser4::setAllDefaults(isc::data::ElementPtr global) {
153  size_t cnt = 0;
154 
155  // Set global defaults first.
156  cnt = setDefaults(global, GLOBAL4_DEFAULTS);
157 
158  // Now set option definition defaults for each specified option definition
159  ConstElementPtr option_defs = global->get("option-def");
160  if (option_defs) {
161  BOOST_FOREACH(ElementPtr option_def, option_defs->listValue()) {
162  cnt += SimpleParser::setDefaults(option_def, OPTION4_DEF_DEFAULTS);
163  }
164  }
165 
166  // Set the defaults for option data
167  ConstElementPtr options = global->get("option-data");
168  if (options) {
169  cnt += setListDefaults(options, OPTION4_DEFAULTS);
170  }
171 
172  // Now set the defaults for defined subnets
173  ConstElementPtr subnets = global->get("subnet4");
174  if (subnets) {
175  cnt += setListDefaults(subnets, SUBNET4_DEFAULTS);
176  }
177 
178  // Set the defaults for interfaces config
179  ConstElementPtr ifaces_cfg = global->get("interfaces-config");
180  if (ifaces_cfg) {
181  ElementPtr mutable_cfg = boost::const_pointer_cast<Element>(ifaces_cfg);
182  cnt += setDefaults(mutable_cfg, IFACE4_DEFAULTS);
183  }
184 
185  // Set defaults for shared networks
186  ConstElementPtr shared = global->get("shared-networks");
187  if (shared) {
188  BOOST_FOREACH(ElementPtr net, shared->listValue()) {
189 
190  cnt += setDefaults(net, SHARED_NETWORK4_DEFAULTS);
191 
192  ConstElementPtr subs = net->get("subnet4");
193  if (subs) {
194  cnt += setListDefaults(subs, SHARED_SUBNET4_DEFAULTS);
195  }
196  }
197  }
198 
199  // Set the defaults for dhcp-queue-control. If the element isn't
200  // there we'll add it.
201  ConstElementPtr queue_control = global->get("dhcp-queue-control");
202  ElementPtr mutable_cfg;
203  if (queue_control) {
204  mutable_cfg = boost::const_pointer_cast<Element>(queue_control);
205  } else {
206  mutable_cfg = Element::createMap();
207  global->set("dhcp-queue-control", mutable_cfg);
208  }
209 
210  cnt += setDefaults(mutable_cfg, DHCP_QUEUE_CONTROL4_DEFAULTS);
211 
212  return (cnt);
213 }
214 
215 size_t SimpleParser4::deriveParameters(isc::data::ElementPtr global) {
216  size_t cnt = 0;
217 
218  // Now derive global parameters into subnets.
219  ConstElementPtr subnets = global->get("subnet4");
220  if (subnets) {
221  BOOST_FOREACH(ElementPtr single_subnet, subnets->listValue()) {
222  cnt += SimpleParser::deriveParams(global, single_subnet,
223  INHERIT_TO_SUBNET4);
224  }
225  }
226 
227  // Deriving parameters for shared networks is a bit more involved.
228  // First, the shared-network level derives from global, and then
229  // subnets within derive from it.
230  ConstElementPtr shared = global->get("shared-networks");
231  if (shared) {
232  BOOST_FOREACH(ElementPtr net, shared->listValue()) {
233  // First try to inherit the parameters from shared network,
234  // if defined there.
235  // Then try to inherit them from global.
236  cnt += SimpleParser::deriveParams(global, net,
237  INHERIT_TO_SUBNET4);
238 
239  // Now we need to go thrugh all the subnets in this net.
240  subnets = net->get("subnet4");
241  if (subnets) {
242  BOOST_FOREACH(ElementPtr single_subnet, subnets->listValue()) {
243  cnt += SimpleParser::deriveParams(net, single_subnet,
244  INHERIT_TO_SUBNET4);
245  }
246  }
247 
248  }
249  }
250 
251  return (cnt);
252 }
253 
254 };
255 };
isc::data::SimpleDefaults
std::vector< SimpleDefault > SimpleDefaults
This specifies all default values in a given scope (e.g. a subnet)
Definition: lib/cc/simple_parser.h:31
isc::data
Definition: cfg_to_element.h:25
simple_parser4.h
isc::data::Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:268
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::data::ParamsList
std::vector< std::string > ParamsList
This defines a list of all parameters that are derived (or inherited) between contexts.
Definition: lib/cc/simple_parser.h:35
isc::data::SimpleParser::deriveParams
static size_t deriveParams(isc::data::ConstElementPtr parent, isc::data::ElementPtr child, const ParamsList &params)
Derives (inherits) parameters from parent scope to a child.
Definition: lib/cc/simple_parser.cc:183
isc::data::Element::boolean
@ boolean
Definition: data.h:151
isc::data::Element::string
@ string
Definition: data.h:151
data.h
isc::data::Element::integer
@ integer
Definition: data.h:151
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::data::SimpleParser::setDefaults
static size_t setDefaults(isc::data::ElementPtr scope, const SimpleDefaults &default_values)
Sets the default values.
Definition: lib/cc/simple_parser.cc:99