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
// Copyright (C) 2013-2024 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/.

#include <config.h>

#include <cc/command_interpreter.h>
#include <dhcp/libdhcp++.h>
#include <process/d_log.h>
#include <process/d_cfg_mgr.h>
#include <process/daemon.h>
#include <process/redact_config.h>
#include <util/encode/encode.h>

#include <boost/lexical_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/algorithm/string.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <limits><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <map><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::config;
using namespace isc::dhcp;
using namespace isc::data;
using namespace isc::asiolink;

namespace isc {
namespace process {

// *********************** DCfgMgrBase  *************************

DCfgMgrBase::DCfgMgrBase(ConfigPtr context) {
    setContext(context);
}

DCfgMgrBase::~DCfgMgrBase() {
}

void
DCfgMgrBase::resetContext() {
    ConfigPtr context = createNewContext();
    setContext(context);
}

void
DCfgMgrBase::setContext(ConfigPtr& context) {<--- Parameter 'context' can be declared as reference to const
    if (!context) {
        isc_throw(DCfgMgrBaseError, "DCfgMgrBase: context cannot be NULL");
    }

    context_ = context;
}

ConstElementPtr
DCfgMgrBase::redactConfig(ConstElementPtr const& config) const {
    ConstElementPtr result(config);
    for (std::list<std::string>& json_path : jsonPathsToRedact()) {<--- Variable 'json_path' can be declared as reference to const
        result = isc::process::redactConfig(result, json_path);<--- Consider using std::accumulate algorithm instead of a raw loop.
    }
    return result;
}

list<list<string>> DCfgMgrBase::jsonPathsToRedact() const {
    static list<list<string>> const list;
    return list;
}

isc::data::ConstElementPtr
DCfgMgrBase::simpleParseConfig(isc::data::ConstElementPtr config_set,
                               bool check_only,
                               const std::function<void()>& post_config_cb) {
    if (!config_set) {
        return (isc::config::createAnswer(CONTROL_RESULT_ERROR,
                                          std::string("Can't parse NULL config")));
    }
    LOG_DEBUG(dctl_logger, isc::log::DBGLVL_COMMAND, DCTL_CONFIG_START)
        .arg(redactConfig(config_set)->str());

    // The parsers implement data inheritance by directly accessing
    // configuration context. For this reason the data parsers must store
    // the parsed data into context immediately. This may cause data
    // inconsistency if the parsing operation fails after the context has been
    // modified. We need to preserve the original context here
    // so as we can rollback changes when an error occurs.
    ConfigPtr original_context = context_;
    resetContext();
    bool rollback = false;

    // Answer will hold the result returned to the caller.
    ConstElementPtr answer;

    try {
        // Logging is common so factor it.
        Daemon::configureLogger(config_set, context_);

        // Let's call the actual implementation
        answer = parse(config_set, check_only);

        // and check the response returned.
        int code = 0;
        isc::config::parseAnswer(code, answer);

        // Everything was fine. Configuration set processed successfully.
        if (!check_only) {
            if (code == 0) {
                // Call the callback only when parsing was successful.
                if (post_config_cb) {
                    post_config_cb();
                }
                LOG_INFO(dctl_logger, DCTL_CONFIG_COMPLETE).arg(getConfigSummary(0));
                // Set the last commit timestamp.
                auto now = boost::posix_time::second_clock::universal_time();
                context_->setLastCommitTime(now);
            } else {
                rollback = true;
            }

            // Use the answer provided.
            //answer = isc::config::createAnswer(CONTROL_RESULT_SUCCESS, "Configuration committed.");
        } else {
            LOG_INFO(dctl_logger, DCTL_CONFIG_CHECK_COMPLETE)
                .arg(getConfigSummary(0))
                .arg(config::answerToText(answer));
        }

    } catch (const std::exception& ex) {
        LOG_ERROR(dctl_logger, DCTL_PARSER_FAIL).arg(ex.what());
        answer = isc::config::createAnswer(CONTROL_RESULT_ERROR, ex.what());
        rollback = true;
    }

    if (check_only) {
        // If this is a configuration check only, then don't actually apply
        // the configuration and reverse to the previous one.
        context_ = original_context;
    }

    if (rollback) {
        // An error occurred, so make sure that we restore original context.
        context_ = original_context;
    }

    return (answer);
}


void
DCfgMgrBase::setCfgDefaults(isc::data::ElementPtr) {
}

isc::data::ConstElementPtr
DCfgMgrBase::parse(isc::data::ConstElementPtr, bool) {
    isc_throw(DCfgMgrBaseError, "This class does not implement simple parser paradigm yet");
}

} // end of isc::dhcp namespace
} // end of isc namespace