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
// 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/.

#include <config.h>

#include <asiolink/io_error.h>
#include <netconf/netconf_cfg_mgr.h>
#include <netconf/netconf_log.h>
#include <exceptions/exceptions.h>

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

using namespace std;
using namespace isc::process;
using namespace isc::data;
using namespace isc::http;

namespace isc {
namespace netconf {

// *********************** CfgControlSocket  *************************

CfgControlSocket::CfgControlSocket(Type type, const string& name,
                                   const Url& url)
    : type_(type), name_(name), url_(url) {
}

CfgControlSocket::Type
CfgControlSocket::stringToType(const string& type) {
    if (type == "unix") {
        return (CfgControlSocket::Type::UNIX);
    } else if (type == "http") {
        return (CfgControlSocket::Type::HTTP);
    } else if (type == "stdout") {
        return (CfgControlSocket::Type::STDOUT);
    }

    isc_throw(BadValue, "Unknown control socket type: " << type);
}

const string
CfgControlSocket::typeToString(CfgControlSocket::Type type) {
    switch (type) {
    case CfgControlSocket::Type::UNIX:
        return ("unix");
    case CfgControlSocket::Type::HTTP:
        return ("http");
    case CfgControlSocket::Type::STDOUT:
        return ("stdout");
    default:
        isc_throw(BadValue, "Unknown control socket type: " << type);
    }
}

ElementPtr
CfgControlSocket::toElement() const {
    ElementPtr result = Element::createMap();
    // Set user-context
    contextToElement(result);
    // Set type
    result->set("socket-type", Element::create(typeToString(type_)));
    // Set name
    result->set("socket-name", Element::create(name_));
    // Set url
    result->set("socket-url", Element::create(url_.toText()));
    return (result);
}

// *********************** CfgServer  *************************
CfgServer::CfgServer(const string& model, CfgControlSocketPtr ctrl_sock)
    : model_(model), boot_update_(true), subscribe_changes_(true),
      subscribe_notifications_(true), validate_changes_(true),
      control_socket_(ctrl_sock) {
}

string
CfgServer::toText() const {
    ostringstream s;
    s << "model: " << model_ << ", control socker: ";
    if (!control_socket_) {
        s << "none";
    } else {
        switch (control_socket_->getType()) {
        case CfgControlSocket::Type::UNIX:
            s << "UNIX:'" << control_socket_->getName() << "'";
            break;
        case CfgControlSocket::Type::HTTP:
          s << "HTTP:'" << control_socket_->getUrl().toText() << "'";
            break;
        case CfgControlSocket::Type::STDOUT:
            s << "STDOUT";
            break;
        }
    }
    return (s.str());
}

ElementPtr
CfgServer::toElement() const {
    ElementPtr result = Element::createMap();
    // Set user-context
    contextToElement(result);
    // Set model
    result->set("model", Element::create(model_));
    // Set boot-update
    result->set("boot-update", Element::create(boot_update_));
    // Set subscribe-changes
    result->set("subscribe-changes", Element::create(subscribe_changes_));
    // Set validate-changes
    result->set("validate-changes", Element::create(validate_changes_));
    // Set control-socket
    if (control_socket_) {
        result->set("control-socket", control_socket_->toElement());
    }
    return (result);
}

ostream&
operator<<(ostream& os, const CfgServer& server) {
    os << server.toText();
    return (os);
}

// *************************** PARSERS ***********************************

// *********************** ControlSocketConfigParser  *************************

CfgControlSocketPtr
ControlSocketConfigParser::parse(ConstElementPtr ctrl_sock_config) {
    CfgControlSocketPtr result;
    string type_str = getString(ctrl_sock_config, "socket-type");
    string name = getString(ctrl_sock_config, "socket-name");
    string url_str = getString(ctrl_sock_config, "socket-url");
    ConstElementPtr user_context = ctrl_sock_config->get("user-context");

    // Type must be valid.
    CfgControlSocket::Type type;
    try {
        type = CfgControlSocket::stringToType(type_str);
    } catch (exception const& ex) {
        isc_throw(ConfigError, ex.what() << " '" << type_str << "' ("
                  << getPosition("socket-type", ctrl_sock_config)  << ")");
    }

    // Url must be valid.
    Url url(url_str);
    if (!url.isValid()) {
        isc_throw(ConfigError, "invalid control socket url: "
                  << url.getErrorMessage() << " '" << url_str << "' ("
                  << getPosition("socket-url", ctrl_sock_config)  << ")");
    }

    // Create the control socket.
    try {
        result.reset(new CfgControlSocket(type, name, url));
    } catch (exception const& ex) {
        isc_throw(ConfigError, ex.what() << " ("
                  << ctrl_sock_config->getPosition() << ")");
    }

    // Add user-context.
    if (user_context) {
        result->setContext(user_context);
    }

    return (result);
}

// *********************** ServerConfigParser  *************************

CfgServerPtr
ServerConfigParser::parse(ConstElementPtr server_config) {
    CfgServerPtr result;
    string model = getString(server_config, "model");
    ConstElementPtr user_context = server_config->get("user-context");
    ConstElementPtr ctrl_sock_config = server_config->get("control-socket");
    CfgControlSocketPtr ctrl_sock;
    if (ctrl_sock_config) {
        ControlSocketConfigParser parser;
        ctrl_sock = parser.parse(ctrl_sock_config);
    }
    try {
        result.reset(new CfgServer(model, ctrl_sock));
    } catch (exception const& ex) {
        isc_throw(ConfigError, ex.what() << " ("
                  << server_config->getPosition() << ")");
    }

    // Add flags.
    result->setBootUpdate(getBoolean(server_config, "boot-update"));
    result->setSubscribeChanges(getBoolean(server_config, "subscribe-changes"));
    result->setValidateChanges(getBoolean(server_config, "validate-changes"));

    // Add user-context.
    if (user_context) {
        result->setContext(user_context);
    }

    return (result);
}

}  // namespace netconf
}  // namespace isc