json_config_parser.cc 24 KB
Newer Older
Tomek Mrugalski's avatar
Tomek Mrugalski committed
1
// Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <config/ccsession.h>
#include <dhcp4/dhcp4_log.h>
17 18
#include <dhcp/libdhcp++.h>
#include <dhcp/option_definition.h>
19
#include <dhcpsrv/cfg_option.h>
20
#include <dhcpsrv/cfgmgr.h>
21
#include <dhcp4/json_config_parser.h>
22
#include <dhcpsrv/dbaccess_parser.h>
23
#include <dhcpsrv/dhcp_parsers.h>
24
#include <dhcpsrv/option_space_container.h>
25
#include <util/encode/hex.h>
26
#include <util/strutil.h>
27

Tomek Mrugalski's avatar
Tomek Mrugalski committed
28 29 30
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
31

Tomek Mrugalski's avatar
Tomek Mrugalski committed
32
#include <limits>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
33 34 35 36
#include <iostream>
#include <vector>
#include <map>

37
using namespace std;
38 39
using namespace isc;
using namespace isc::dhcp;
40 41 42
using namespace isc::data;
using namespace isc::asiolink;

43 44
namespace {

45
/// @brief Parser for IPv4 pool definitions.
46
///
47 48 49
/// This is the IPv4 derivation of the PoolParser class and handles pool
/// definitions, i.e. a list of entries of one of two syntaxes: min-max and
/// prefix/len for IPv4 pools. Pool4 objects are created and stored in chosen
50
/// PoolStorage container.
51
///
52 53
/// It is useful for parsing Dhcp4/subnet4[X]/pool parameters.
class Pool4Parser : public PoolParser {
54 55
public:

56 57 58 59 60 61 62 63
    /// @brief Constructor.
    ///
    /// @param param_name name of the parameter. Note, it is passed through
    /// but unused, parameter is currently always "Dhcp4/subnet4[X]/pool"
    /// @param pools storage container in which to store the parsed pool
    /// upon "commit"
    Pool4Parser(const std::string& param_name,  PoolStoragePtr pools)
        :PoolParser(param_name, pools) {
64 65
    }

66 67
protected:
    /// @brief Creates a Pool4 object given a IPv4 prefix and the prefix length.
68
    ///
69 70
    /// @param addr is the IPv4 prefix of the pool.
    /// @param len is the prefix length.
71
    /// @param ignored dummy parameter to provide symmetry between the
72
    /// PoolParser derivations. The V6 derivation requires a third value.
73
    /// @return returns a PoolPtr to the new Pool4 object.
74
    PoolPtr poolMaker (IOAddress &addr, uint32_t len, int32_t) {
75 76
        return (PoolPtr(new Pool4(addr, len)));
    }
77

78 79 80 81
    /// @brief Creates a Pool4 object given starting and ending IPv4 addresses.
    ///
    /// @param min is the first IPv4 address in the pool.
    /// @param max is the last IPv4 address in the pool.
82
    /// @param ignored dummy parameter to provide symmetry between the
83
    /// PoolParser derivations. The V6 derivation requires a third value.
84
    /// @return returns a PoolPtr to the new Pool4 object.
85
    PoolPtr poolMaker (IOAddress &min, IOAddress &max, int32_t) {
86
        return (PoolPtr(new Pool4(min, max)));
87
    }
88
};
89

90 91 92 93 94 95 96 97 98 99 100 101
class Pools4ListParser : public PoolsListParser {
public:
    Pools4ListParser(const std::string& dummy, PoolStoragePtr pools)
        :PoolsListParser(dummy, pools) {
    }

protected:
    virtual ParserPtr poolParserMaker(PoolStoragePtr storage) {
        return (ParserPtr(new Pool4Parser("pool", storage)));
    }
};

102 103
/// @brief This class parses a single IPv4 subnet.
///
104 105
/// This is the IPv4 derivation of the SubnetConfigParser class and it parses
/// the whole subnet definition. It creates parsersfor received configuration
106 107 108 109
/// parameters as needed.
class Subnet4ConfigParser : public SubnetConfigParser {
public:
    /// @brief Constructor
110
    ///
111 112
    /// @param ignored first parameter
    /// stores global scope parameters, options, option defintions.
113
    Subnet4ConfigParser(const std::string&)
114
        :SubnetConfigParser("", globalContext(), IOAddress("0.0.0.0")) {
115
    }
116

117 118 119 120 121 122 123
    /// @brief Parses a single IPv4 subnet configuration and adds to the
    /// Configuration Manager.
    ///
    /// @param subnet A new subnet being configured.
    void build(ConstElementPtr subnet) {
        SubnetConfigParser::build(subnet);

124
        if (subnet_) {
125 126 127
            Subnet4Ptr sub4ptr = boost::dynamic_pointer_cast<Subnet4>(subnet_);
            if (!sub4ptr) {
                // If we hit this, it is a programming error.
128
                isc_throw(Unexpected,
129 130 131
                          "Invalid cast in Subnet4ConfigParser::commit");
            }

132 133
            // Set relay information if it was parsed
            if (relay_info_) {
134
                sub4ptr->setRelayInfo(*relay_info_);
135 136
            }

137 138 139 140 141 142 143 144 145
            // Adding a subnet to the Configuration Manager may fail if the
            // subnet id is invalid (duplicate). Thus, we catch exceptions
            // here to append a position in the configuration string.
            try {
                isc::dhcp::CfgMgr::instance().addSubnet4(sub4ptr);
            } catch (const std::exception& ex) {
                isc_throw(DhcpConfigError, ex.what() << " ("
                          << subnet->getPosition() << ")");
            }
146 147 148
        }
    }

149 150 151 152 153 154
    /// @brief Commits subnet configuration.
    ///
    /// This function is currently no-op because subnet should already
    /// be added into the Config Manager in the build().
    void commit() { }

155
protected:
156

157
    /// @brief Creates parsers for entries in subnet definition.
158
    ///
159 160 161 162 163 164 165 166 167 168
    /// @param config_id name of the entry
    ///
    /// @return parser object for specified entry name. Note the caller is
    /// responsible for deleting the parser created.
    /// @throw isc::dhcp::DhcpConfigError if trying to create a parser
    /// for unknown config element
    DhcpConfigParser* createSubnetConfigParser(const std::string& config_id) {
        DhcpConfigParser* parser = NULL;
        if ((config_id.compare("valid-lifetime") == 0)  ||
            (config_id.compare("renew-timer") == 0)  ||
169 170
            (config_id.compare("rebind-timer") == 0) ||
            (config_id.compare("id") == 0)) {
171
            parser = new Uint32Parser(config_id, uint32_values_);
172
        } else if ((config_id.compare("subnet") == 0) ||
173
                   (config_id.compare("interface") == 0) ||
174
                   (config_id.compare("client-class") == 0) ||
175
                   (config_id.compare("next-server") == 0)) {
176
            parser = new StringParser(config_id, string_values_);
177 178
        } else if (config_id.compare("pools") == 0) {
            parser = new Pools4ListParser(config_id, pools_);
179
        } else if (config_id.compare("relay") == 0) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
180
            parser = new RelayInfoParser(config_id, relay_info_, Option::V4);
181
        } else if (config_id.compare("option-data") == 0) {
182
            parser = new OptionDataListParser(config_id, options_, AF_INET);
183
        } else {
184
            isc_throw(NotImplemented, "unsupported parameter: " << config_id);
185 186
        }

187
        return (parser);
188 189
    }

190
    /// @brief Determines if the given option space name and code describe
Jeremy C. Reed's avatar
Jeremy C. Reed committed
191
    /// a standard option for the DHCP4 server.
192
    ///
193 194 195 196 197 198 199 200
    /// @param option_space is the name of the option space to consider
    /// @param code is the numeric option code to consider
    /// @return returns true if the space and code are part of the server's
    /// standard options.
    bool isServerStdOption(std::string option_space, uint32_t code) {
        return ((option_space.compare("dhcp4") == 0)
                && LibDHCP::isStandardOption(Option::V4, code));
    }
201

202 203 204 205 206 207 208
    /// @brief Returns the option definition for a given option code from
    /// the DHCP4 server's standard set of options.
    /// @param code is the numeric option code of the desired option definition.
    /// @return returns a pointer the option definition
    OptionDefinitionPtr getServerStdOptionDefinition (uint32_t code) {
        return (LibDHCP::getOptionDef(Option::V4, code));
    }
209

210
    /// @brief Issues a DHCP4 server specific warning regarding duplicate subnet
211 212
    /// options.
    ///
213
    /// @param code is the numeric option code of the duplicate option
214
    /// @param addr is the subnet address
215
    /// @todo a means to know the correct logger and perhaps a common
216
    /// message would allow this method to be emitted by the base class.
217 218 219 220 221
    virtual void duplicate_option_warning(uint32_t code,
                                         isc::asiolink::IOAddress& addr) {
        LOG_WARN(dhcp4_logger, DHCP4_CONFIG_OPTION_DUPLICATE)
            .arg(code).arg(addr.toText());
    }
222

223
    /// @brief Instantiates the IPv4 Subnet based on a given IPv4 address
224 225
    /// and prefix length.
    ///
226
    /// @param addr is IPv4 address of the subnet.
227
    /// @param len is the prefix length
228
    void initSubnet(isc::asiolink::IOAddress addr, uint8_t len) {
229 230 231 232 233 234 235 236
        // The renew-timer and rebind-timer are optional. If not set, the
        // option 58 and 59 will not be sent to a client. In this case the
        // client will use default values based on the valid-lifetime.
        Triplet<uint32_t> t1 = getOptionalParam("renew-timer");
        Triplet<uint32_t> t2 = getOptionalParam("rebind-timer");
        // The valid-lifetime is mandatory. It may be specified for a
        // particular subnet. If not, the global value should be present.
        // If there is no global value, exception is thrown.
237
        Triplet<uint32_t> valid = getParam("valid-lifetime");
238 239 240 241
        // Subnet ID is optional. If it is not supplied the value of 0 is used,
        // which means autogenerate.
        SubnetID subnet_id =
            static_cast<SubnetID>(uint32_values_->getOptionalParam("id", 0));
242

243 244 245 246 247 248 249 250 251 252
        stringstream s;
        s << addr << "/" << static_cast<int>(len) << " with params: ";
        // t1 and t2 are optional may be not specified.
        if (!t1.unspecified()) {
            s << "t1=" << t1 << ", ";
        }
        if (!t2.unspecified()) {
            s << "t2=" << t2 << ", ";
        }
        s <<"valid-lifetime=" << valid;
253

254
        LOG_INFO(dhcp4_logger, DHCP4_CONFIG_NEW_SUBNET).arg(s.str());
255

256
        Subnet4Ptr subnet4(new Subnet4(addr, len, t1, t2, valid, subnet_id));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
257
        subnet_ = subnet4;
258 259 260 261

        // Try global value first
        try {
            string next_server = globalContext()->string_values_->getParam("next-server");
Tomek Mrugalski's avatar
Tomek Mrugalski committed
262 263 264
            if (!next_server.empty()) {
                subnet4->setSiaddr(IOAddress(next_server));
            }
265
        } catch (const DhcpConfigError&) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
266
            // Don't care. next_server is optional. We can live without it
267 268 269 270
        } catch (...) {
            isc_throw(DhcpConfigError, "invalid parameter next-server ("
                      << globalContext()->string_values_->getPosition("next-server")
                      << ")");
271 272 273 274 275
        }

        // Try subnet specific value if it's available
        try {
            string next_server = string_values_->getParam("next-server");
Tomek Mrugalski's avatar
Tomek Mrugalski committed
276 277 278
            if (!next_server.empty()) {
                subnet4->setSiaddr(IOAddress(next_server));
            }
279
        } catch (const DhcpConfigError&) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
280
            // Don't care. next_server is optional. We can live without it
281 282 283 284
        } catch (...) {
            isc_throw(DhcpConfigError, "invalid parameter next-server ("
                      << string_values_->getPosition("next-server")
                      << ")");
285
        }
286

287

288 289 290 291 292 293 294
        // Try setting up client class (if specified)
        try {
            string client_class = string_values_->getParam("client-class");
            subnet4->allowClientClass(client_class);
        } catch (const DhcpConfigError&) {
            // That's ok if it fails. client-class is optional.
        }
295 296 297
    }
};

298
/// @brief this class parses list of DHCP4 subnets
299 300 301 302
///
/// This is a wrapper parser that handles the whole list of Subnet4
/// definitions. It iterates over all entries and creates Subnet4ConfigParser
/// for each entry.
303
class Subnets4ListConfigParser : public DhcpConfigParser {
304 305 306 307
public:

    /// @brief constructor
    ///
308
    /// @param dummy first argument, always ignored. All parsers accept a
309
    /// string parameter "name" as their first argument.
310 311 312 313 314 315 316 317 318 319
    Subnets4ListConfigParser(const std::string&) {
    }

    /// @brief parses contents of the list
    ///
    /// Iterates over all entries on the list and creates Subnet4ConfigParser
    /// for each entry.
    ///
    /// @param subnets_list pointer to a list of IPv4 subnets
    void build(ConstElementPtr subnets_list) {
320 321 322 323 324 325
        // @todo: Implement more subtle reconfiguration than toss
        // the old one and replace with the new one.

        // remove old subnets
        CfgMgr::instance().deleteSubnets4();

326
        BOOST_FOREACH(ConstElementPtr subnet, subnets_list->listValue()) {
327
            ParserPtr parser(new Subnet4ConfigParser("subnet"));
328 329 330 331 332 333 334
            parser->build(subnet);
            subnets_.push_back(parser);
        }
    }

    /// @brief commits subnets definitions.
    ///
335 336
    /// Iterates over all Subnet4 parsers. Each parser contains definitions of
    /// a single subnet and its parameters and commits each subnet separately.
337 338 339 340 341 342 343 344 345 346
    void commit() {
        BOOST_FOREACH(ParserPtr subnet, subnets_) {
            subnet->commit();
        }

    }

    /// @brief Returns Subnet4ListConfigParser object
    /// @param param_name name of the parameter
    /// @return Subnets4ListConfigParser object
347
    static DhcpConfigParser* factory(const std::string& param_name) {
348 349 350 351 352
        return (new Subnets4ListConfigParser(param_name));
    }

    /// @brief collection of subnet parsers.
    ParserCollection subnets_;
353

354 355
};

356 357 358 359 360
} // anonymous namespace

namespace isc {
namespace dhcp {

361 362 363 364 365 366 367
/// @brief creates global parsers
///
/// This method creates global parsers that parse global parameters, i.e.
/// those that take format of Dhcp4/param1, Dhcp4/param2 and so forth.
///
/// @param config_id pointer to received global configuration entry
/// @return parser for specified global DHCPv4 parameter
368
/// @throw NotImplemented if trying to create a parser for unknown
369
/// config element
370 371
    DhcpConfigParser* createGlobalDhcp4ConfigParser(const std::string& config_id,
                                                    ConstElementPtr element) {
372
    DhcpConfigParser* parser = NULL;
373 374 375
    if ((config_id.compare("valid-lifetime") == 0)  ||
        (config_id.compare("renew-timer") == 0)  ||
        (config_id.compare("rebind-timer") == 0))  {
376
        parser = new Uint32Parser(config_id,
377
                                 globalContext()->uint32_values_);
378
    } else if (config_id.compare("interfaces") == 0) {
379
        parser = new InterfaceListConfigParser(config_id, globalContext());
380
    } else if (config_id.compare("subnet4") == 0) {
381
        parser = new Subnets4ListConfigParser(config_id);
382
    } else if (config_id.compare("option-data") == 0) {
383
        parser = new OptionDataListParser(config_id, CfgOptionPtr(), AF_INET);
384
    } else if (config_id.compare("option-def") == 0) {
385
        parser  = new OptionDefListParser(config_id, globalContext());
386 387
    } else if ((config_id.compare("version") == 0) ||
               (config_id.compare("next-server") == 0)) {
388
        parser  = new StringParser(config_id,
389
                                    globalContext()->string_values_);
390
    } else if (config_id.compare("lease-database") == 0) {
391
        parser = new DbAccessParser(config_id, *globalContext());
392 393
    } else if (config_id.compare("hooks-libraries") == 0) {
        parser = new HooksLibrariesParser(config_id);
394 395
    } else if (config_id.compare("echo-client-id") == 0) {
        parser = new BooleanParser(config_id, globalContext()->boolean_values_);
396 397
    } else if (config_id.compare("dhcp-ddns") == 0) {
        parser = new D2ClientConfigParser(config_id);
398
    } else {
399 400 401
        isc_throw(DhcpConfigError,
                "unsupported global configuration parameter: "
                  << config_id << " (" << element->getPosition() << ")");
402
    }
403 404

    return (parser);
405 406
}

407
void commitGlobalOptions() {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
408 409
    // Although the function is modest for now, it is certain that the number
    // of global switches will increase over time, hence the name.
410 411 412 413 414 415 416 417 418 419 420

    // Set whether v4 server is supposed to echo back client-id (yes = RFC6842
    // compatible, no = backward compatibility)
    try {
        bool echo_client_id = globalContext()->boolean_values_->getParam("echo-client-id");
        CfgMgr::instance().echoClientId(echo_client_id);
    } catch (...) {
        // Ignore errors. This flag is optional
    }
}

421
isc::data::ConstElementPtr
422
configureDhcp4Server(Dhcpv4Srv&, isc::data::ConstElementPtr config_set) {
423
    if (!config_set) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
424 425 426
        ConstElementPtr answer = isc::config::createAnswer(1,
                                 string("Can't parse NULL config"));
        return (answer);
427 428
    }

429
    LOG_DEBUG(dhcp4_logger, DBG_DHCP4_COMMAND,
430
              DHCP4_CONFIG_START).arg(config_set->str());
431

432 433 434 435
    // Before starting any subnet operations, let's reset the subnet-id counter,
    // so newly recreated configuration starts with first subnet-id equal 1.
    Subnet::resetSubnetID();

436
    // Some of the values specified in the configuration depend on
437 438 439
    // other values. Typically, the values in the subnet4 structure
    // depend on the global values. Also, option values configuration
    // must be performed after the option definitions configurations.
440 441 442
    // Thus we group parsers and will fire them in the right order:
    // all parsers other than subnet4 and option-data parser,
    // option-data parser, subnet4 parser.
443
    ParserCollection independent_parsers;
444 445
    ParserPtr subnet_parser;
    ParserPtr option_parser;
446
    ParserPtr iface_parser;
447

448 449
    // Some of the parsers alter the state of the system in a way that can't
    // easily be undone. (Or alter it in a way such that undoing the change has
450
    // the same risk of failure as doing the change.)
451
    ParserPtr hooks_parser;
452

453
    // The subnet parsers implement data inheritance by directly
454
    // accessing global storage. For this reason the global data
455
    // parsers must store the parsed data into global storages
456 457 458 459
    // immediately. This may cause data inconsistency if the
    // parsing operation fails after the global storage has been
    // modified. We need to preserve the original global data here
    // so as we can rollback changes when an error occurs.
460
    ParserContext original_context(*globalContext());
461

462
    // Answer will hold the result.
463
    ConstElementPtr answer;
464
    // Rollback informs whether error occured and original data
465 466
    // have to be restored to global storages.
    bool rollback = false;
467
    // config_pair holds the details of the current parser when iterating over
468 469
    // the parsers.  It is declared outside the loops so in case of an error,
    // the name of the failing parser can be retrieved in the "catch" clause.
470
    ConfigPair config_pair;
471
    try {
472
        // Make parsers grouping.
473
        const std::map<std::string, ConstElementPtr>& values_map =
474
                                                        config_set->mapValue();
475
        BOOST_FOREACH(config_pair, values_map) {
476 477
            ParserPtr parser(createGlobalDhcp4ConfigParser(config_pair.first,
                                                           config_pair.second));
478 479
            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PARSER_CREATED)
                      .arg(config_pair.first);
480 481 482 483
            if (config_pair.first == "subnet4") {
                subnet_parser = parser;
            } else if (config_pair.first == "option-data") {
                option_parser = parser;
484 485 486 487 488
            } else if (config_pair.first == "interfaces") {
                // The interface parser is independent from any other
                // parser and can be run here before any other parsers.
                iface_parser = parser;
                parser->build(config_pair.second);
489 490 491 492
            } else if (config_pair.first == "hooks-libraries") {
                // Executing commit will alter currently-loaded hooks
                // libraries.  Check if the supplied libraries are valid,
                // but defer the commit until everything else has committed.
493
                hooks_parser = parser;
494
                parser->build(config_pair.second);
495
            } else {
496 497
                // Those parsers should be started before other
                // parsers so we can call build straight away.
498 499 500 501 502 503 504 505
                independent_parsers.push_back(parser);
                parser->build(config_pair.second);
                // The commit operation here may modify the global storage
                // but we need it so as the subnet6 parser can access the
                // parsed data.
                parser->commit();
            }
        }
506

507
        // The option values parser is the next one to be run.
508 509 510
        std::map<std::string, ConstElementPtr>::const_iterator option_config =
            values_map.find("option-data");
        if (option_config != values_map.end()) {
511
            config_pair.first = "option-data";
512 513 514 515
            option_parser->build(option_config->second);
            option_parser->commit();
        }

516
        // The subnet parser is the last one to be run.
517 518 519
        std::map<std::string, ConstElementPtr>::const_iterator subnet_config =
            values_map.find("subnet4");
        if (subnet_config != values_map.end()) {
520
            config_pair.first = "subnet4";
521
            subnet_parser->build(subnet_config->second);
522
        }
523

524
    } catch (const isc::Exception& ex) {
525
        LOG_ERROR(dhcp4_logger, DHCP4_PARSER_FAIL)
526
                  .arg(config_pair.first).arg(ex.what());
527
        answer = isc::config::createAnswer(1, ex.what());
528 529 530 531

        // An error occured, so make sure that we restore original data.
        rollback = true;

532
    } catch (...) {
533
        // For things like bad_cast in boost::lexical_cast
534
        LOG_ERROR(dhcp4_logger, DHCP4_PARSER_EXCEPTION).arg(config_pair.first);
535 536
        answer = isc::config::createAnswer(1, "undefined configuration"
                                           " processing error");
537 538 539

        // An error occured, so make sure that we restore original data.
        rollback = true;
540 541
    }

542 543 544 545 546 547
    // So far so good, there was no parsing error so let's commit the
    // configuration. This will add created subnets and option values into
    // the server's configuration.
    // This operation should be exception safe but let's make sure.
    if (!rollback) {
        try {
548 549
            if (subnet_parser) {
                subnet_parser->commit();
550
            }
551

552 553
            // No need to commit interface names as this is handled by the
            // CfgMgr::commit() function.
554

555 556 557
            // Apply global options
            commitGlobalOptions();

558 559 560
            // This occurs last as if it succeeds, there is no easy way
            // revert it.  As a result, the failure to commit a subsequent
            // change causes problems when trying to roll back.
561 562
            if (hooks_parser) {
                hooks_parser->commit();
563
            }
564 565
        }
        catch (const isc::Exception& ex) {
566
            LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_FAIL).arg(ex.what());
567
            answer = isc::config::createAnswer(2, ex.what());
568 569
            rollback = true;
        } catch (...) {
570
            // For things like bad_cast in boost::lexical_cast
571
            LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_EXCEPTION);
572 573
            answer = isc::config::createAnswer(2, "undefined configuration"
                                               " parsing error");
574
            rollback = true;
575 576
        }
    }
577 578 579

    // Rollback changes as the configuration parsing failed.
    if (rollback) {
580
        globalContext().reset(new ParserContext(original_context));
581 582 583
        return (answer);
    }

584
    LOG_INFO(dhcp4_logger, DHCP4_CONFIG_COMPLETE)
585
        .arg(CfgMgr::instance().getCurrentCfg()->
586
             getConfigSummary(SrvConfig::CFGSEL_ALL4));
587

588
    // Everything was fine. Configuration is successful.
589
    answer = isc::config::createAnswer(0, "Configuration successful.");
590 591 592
    return (answer);
}

593
ParserContextPtr& globalContext() {
594 595
    static ParserContextPtr global_context_ptr(new ParserContext(Option::V4));
    return (global_context_ptr);
596 597
}

598 599


600 601
}; // end of isc::dhcp namespace
}; // end of isc namespace