module_spec.cc 15.4 KB
Newer Older
1
// Copyright (C) 2010-2017 Internet Systems Consortium.
2
//
3 4 5
// 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/.
6

7
#include <config/module_spec.h>
8 9

#include <sstream>
Jelte Jansen's avatar
Jelte Jansen committed
10
#include <iostream>
Jelte Jansen's avatar
Jelte Jansen committed
11 12
#include <fstream>
#include <cerrno>
13 14 15

#include <boost/foreach.hpp>

16
// todo: add more context to thrown ModuleSpecErrors?
17

18
using namespace isc::data;
19
using namespace isc::config;
20

21
namespace {
22
//
23
// Private functions
24 25
//

26 27 28
void
check_leaf_item(ConstElementPtr spec, const std::string& name,
                Element::types type, bool mandatory)
29 30
{
    if (spec->contains(name)) {
31
        if (type == Element::any || spec->get(name)->getType() == type) {
32 33
            return;
        } else {
34 35
            isc_throw(ModuleSpecError,
                      name + " not of type " + Element::typeToName(type));
36 37 38 39 40
        }
    } else if (mandatory) {
        // todo: want parent item name, and perhaps some info about location
        // in list? or just catch and throw new...
        // or make this part non-throwing and check return value...
41
        isc_throw(ModuleSpecError, name + " missing in " + spec->str());
42 43 44
    }
}

45
void check_config_item_list(ConstElementPtr spec);
46

47 48
void
check_config_item(ConstElementPtr spec) {
49 50 51 52
    check_leaf_item(spec, "item_name", Element::string, true);
    check_leaf_item(spec, "item_type", Element::string, true);
    check_leaf_item(spec, "item_optional", Element::boolean, true);
    check_leaf_item(spec, "item_default",
53
                    Element::nameToType(spec->get("item_type")->stringValue()),
54
                    !spec->get("item_optional")->boolValue()
55 56
                   );

57
    // if list, check the list specification
58
    if (Element::nameToType(spec->get("item_type")->stringValue()) == Element::list) {
59 60 61
        check_leaf_item(spec, "list_item_spec", Element::map, true);
        check_config_item(spec->get("list_item_spec"));
    }
62 63

    if (spec->get("item_type")->stringValue() == "map") {
64
        check_leaf_item(spec, "map_item_spec", Element::list, true);
Jelte Jansen's avatar
Jelte Jansen committed
65
        check_config_item_list(spec->get("map_item_spec"));
66 67 68
    } else if (spec->get("item_type")->stringValue() == "named_set") {
        check_leaf_item(spec, "named_set_item_spec", Element::map, true);
        check_config_item(spec->get("named_set_item_spec"));
69 70 71
    }
}

72 73
void
check_config_item_list(ConstElementPtr spec) {
74
    if (spec->getType() != Element::list) {
75
        isc_throw(ModuleSpecError, "config_data is not a list of elements");
76
    }
77
    BOOST_FOREACH(ConstElementPtr item, spec->listValue()) {
78 79 80 81
        check_config_item(item);
    }
}

Naoki Kambe's avatar
Naoki Kambe committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
// checks whether the given element is a valid statistics specification
// returns false if the specification is bad
bool
check_format(ConstElementPtr value, ConstElementPtr format_name) {
    typedef std::map<std::string, std::string> format_types;
    format_types time_formats;
    // TODO: should be added other format types if necessary
    time_formats.insert(
        format_types::value_type("date-time", "%Y-%m-%dT%H:%M:%SZ") );
    time_formats.insert(
        format_types::value_type("date", "%Y-%m-%d") );
    time_formats.insert(
        format_types::value_type("time", "%H:%M:%S") );
    BOOST_FOREACH (const format_types::value_type& f, time_formats) {
        if (format_name->stringValue() == f.first) {
            struct tm tm;
Naoki Kambe's avatar
Naoki Kambe committed
98
            std::vector<char> buf(32);
99 100
            memset(&tm, 0, sizeof(tm));
            // reverse check
Naoki Kambe's avatar
Naoki Kambe committed
101
            return (strptime(value->stringValue().c_str(),
102
                             f.second.c_str(), &tm) != NULL
Naoki Kambe's avatar
Naoki Kambe committed
103
                    && strftime(&buf[0], buf.size(),
104
                                f.second.c_str(), &tm) != 0
Naoki Kambe's avatar
Naoki Kambe committed
105 106
                    && strncmp(value->stringValue().c_str(),
                               &buf[0], buf.size()) == 0);
Naoki Kambe's avatar
Naoki Kambe committed
107 108 109 110 111 112 113 114 115 116
        }
    }
    return (false);
}

void check_statistics_item_list(ConstElementPtr spec);

void
check_statistics_item_list(ConstElementPtr spec) {
    if (spec->getType() != Element::list) {
117
        isc_throw(ModuleSpecError, "statistics is not a list of elements");
Naoki Kambe's avatar
Naoki Kambe committed
118 119 120 121 122 123 124 125 126 127 128 129
    }
    BOOST_FOREACH(ConstElementPtr item, spec->listValue()) {
        check_config_item(item);
        // additional checks for statistics
        check_leaf_item(item, "item_title", Element::string, true);
        check_leaf_item(item, "item_description", Element::string, true);
        check_leaf_item(item, "item_format", Element::string, false);
        // checks name of item_format and validation of item_default
        if (item->contains("item_format")
            && item->contains("item_default")) {
            if(!check_format(item->get("item_default"),
                             item->get("item_format"))) {
130
                isc_throw(ModuleSpecError,
Naoki Kambe's avatar
Naoki Kambe committed
131 132 133 134 135 136
                    "item_default not valid type of item_format");
            }
        }
    }
}

137 138
void
check_command(ConstElementPtr spec) {
139 140 141 142 143
    check_leaf_item(spec, "command_name", Element::string, true);
    check_leaf_item(spec, "command_args", Element::list, true);
    check_config_item_list(spec->get("command_args"));
}

144 145
void
check_command_list(ConstElementPtr spec) {
146
    if (spec->getType() != Element::list) {
147
        isc_throw(ModuleSpecError, "commands is not a list of elements");
148
    }
149
    BOOST_FOREACH(ConstElementPtr item, spec->listValue()) {
150 151 152 153
        check_command(item);
    }
}

154 155
void
check_data_specification(ConstElementPtr spec) {
156
    check_leaf_item(spec, "module_name", Element::string, true);
Jelte Jansen's avatar
Jelte Jansen committed
157
    check_leaf_item(spec, "module_description", Element::string, false);
158 159
    // config_data is not mandatory; module could just define
    // commands and have no config
160 161 162 163 164 165
    if (spec->contains("config_data")) {
        check_config_item_list(spec->get("config_data"));
    }
    if (spec->contains("commands")) {
        check_command_list(spec->get("commands"));
    }
Naoki Kambe's avatar
Naoki Kambe committed
166 167 168
    if (spec->contains("statistics")) {
        check_statistics_item_list(spec->get("statistics"));
    }
169 170
}

171
// checks whether the given element is a valid module specification
172
// throws a ModuleSpecError if the specification is bad
173 174
void
check_module_specification(ConstElementPtr def) {
175 176
    try {
        check_data_specification(def);
177
    } catch (const TypeError& te) {
178
        isc_throw(ModuleSpecError, te.what());
179
    }
180
}
181
}
182

183 184
namespace isc {
namespace config {
185 186 187 188
//
// Public functions
//

189
// throw ModuleSpecError
190
ModuleSpec::ModuleSpec(ConstElementPtr module_spec_element,
191 192 193 194
                       const bool check)
                       
{
    module_specification = module_spec_element;
195
    if (check) {
196 197 198 199
        check_module_specification(module_specification);
    }
}

200
ConstElementPtr
JINMEI Tatuya's avatar
JINMEI Tatuya committed
201
ModuleSpec::getCommandsSpec() const {
202
    if (module_specification->contains("commands")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
203
        return (module_specification->get("commands"));
204
    } else {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
205
        return (ElementPtr());
206 207 208
    }
}

209
ConstElementPtr
JINMEI Tatuya's avatar
JINMEI Tatuya committed
210
ModuleSpec::getConfigSpec() const {
211
    if (module_specification->contains("config_data")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
212
        return (module_specification->get("config_data"));
213
    } else {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
214
        return (ElementPtr());
215 216 217
    }
}

Naoki Kambe's avatar
Naoki Kambe committed
218 219 220 221 222 223 224 225 226
ConstElementPtr
ModuleSpec::getStatisticsSpec() const {
    if (module_specification->contains("statistics")) {
        return (module_specification->get("statistics"));
    } else {
        return (ElementPtr());
    }
}

227
const std::string
JINMEI Tatuya's avatar
JINMEI Tatuya committed
228 229
ModuleSpec::getModuleName() const {
    return (module_specification->get("module_name")->stringValue());
230 231
}

Jelte Jansen's avatar
Jelte Jansen committed
232
const std::string
JINMEI Tatuya's avatar
JINMEI Tatuya committed
233
ModuleSpec::getModuleDescription() const {
Jelte Jansen's avatar
Jelte Jansen committed
234
    if (module_specification->contains("module_description")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
235
        return (module_specification->get("module_description")->stringValue());
Jelte Jansen's avatar
Jelte Jansen committed
236
    } else {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
237
        return (std::string(""));
Jelte Jansen's avatar
Jelte Jansen committed
238 239 240
    }
}

241
bool
242
ModuleSpec::validateConfig(ConstElementPtr data, const bool full) const {
243
    ConstElementPtr spec = module_specification->find("config_data");
244
    return (validateSpecList(spec, data, full, ElementPtr()));
245 246
}

Naoki Kambe's avatar
Naoki Kambe committed
247 248 249 250 251 252
bool
ModuleSpec::validateStatistics(ConstElementPtr data, const bool full) const {
    ConstElementPtr spec = module_specification->find("statistics");
    return (validateSpecList(spec, data, full, ElementPtr()));
}

253
bool
254
ModuleSpec::validateCommand(const std::string& command,
255
                             ConstElementPtr args,
256
                             ElementPtr errors) const
257
{
258
    if (args->getType() != Element::map) {
Jelte Jansen's avatar
Jelte Jansen committed
259 260
        errors->add(Element::create("args for command " +
                                    command + " is not a map"));
261 262 263
        return (false);
    }

264
    ConstElementPtr commands_spec = module_specification->find("commands");
265 266 267 268 269 270 271 272
    if (!commands_spec) {
        // there are no commands according to the spec.
        errors->add(Element::create("The given module has no commands"));
        return (false);
    }

    BOOST_FOREACH(ConstElementPtr cur_command, commands_spec->listValue()) {
        if (cur_command->get("command_name")->stringValue() == command) {
273
            return (validateSpecList(cur_command->get("command_args"),
Jelte Jansen's avatar
Jelte Jansen committed
274
                                       args, true, errors));
275 276 277 278 279 280 281 282
        }
    }

    // this command is unknown
    errors->add(Element::create("Unknown command " + command));
    return (false);
}

283
bool
284
ModuleSpec::validateConfig(ConstElementPtr data, const bool full,
285
                            ElementPtr errors) const
286
{
287
    ConstElementPtr spec = module_specification->find("config_data");
288
    return (validateSpecList(spec, data, full, errors));
289 290
}

Naoki Kambe's avatar
Naoki Kambe committed
291 292 293 294 295 296 297 298
bool
ModuleSpec::validateStatistics(ConstElementPtr data, const bool full,
                               ElementPtr errors) const
{
    ConstElementPtr spec = module_specification->find("statistics");
    return (validateSpecList(spec, data, full, errors));
}

299
// throw JSONError and ModuleSpecError
300 301 302 303 304
ModuleSpec
moduleSpecFromFile(const std::string& file_name, const bool check)
{
    std::ifstream file;

305 306 307
    // zero out the errno to be safe
    errno = 0;

308 309 310 311
    file.open(file_name.c_str());
    if (!file) {
        std::stringstream errs;
        errs << "Error opening " << file_name << ": " << strerror(errno);
312
        isc_throw(ModuleSpecError, errs.str());
313 314
    }

315
    ConstElementPtr module_spec_element = Element::fromJSON(file, file_name);
316
    if (module_spec_element->contains("module_spec")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
317
        return (ModuleSpec(module_spec_element->get("module_spec"), check));
318
    } else {
319
        isc_throw(ModuleSpecError, "No module_spec in specification");
320
    }
321 322
}

323
// throw JSONError and ModuleSpecError
324 325
ModuleSpec
moduleSpecFromFile(std::ifstream& in, const bool check)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
326
{
327
    ConstElementPtr module_spec_element = Element::fromJSON(in);
328
    if (module_spec_element->contains("module_spec")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
329
        return (ModuleSpec(module_spec_element->get("module_spec"), check));
330
    } else {
331
        isc_throw(ModuleSpecError, "No module_spec in specification");
332
    }
333 334
}

335

336
namespace {
337 338 339 340
//
// private functions
//

341 342 343
//
// helper functions for validation
//
344 345
bool
check_type(ConstElementPtr spec, ConstElementPtr element) {
346
    std::string cur_item_type;
347
    cur_item_type = spec->get("item_type")->stringValue();
348
    if (cur_item_type == "any") {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
349
        return (true);
350
    }
351
    switch (element->getType()) {
352
        case Element::integer:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
353
            return (cur_item_type == "integer");
354 355
            break;
        case Element::real:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
356
            return (cur_item_type == "real");
357 358
            break;
        case Element::boolean:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
359
            return (cur_item_type == "boolean");
360 361
            break;
        case Element::string:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
362
            return (cur_item_type == "string");
363 364
            break;
        case Element::list:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
365
            return (cur_item_type == "list");
366 367
            break;
        case Element::map:
368
            return (cur_item_type == "map" ||
369
                    cur_item_type == "named_set");
370 371
            break;
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
372
    return (false);
373
}
374
}
375 376

bool
377
ModuleSpec::validateItem(ConstElementPtr spec, ConstElementPtr data,
378 379
                          const bool full, ElementPtr errors) const
{
380
    if (!check_type(spec, data)) {
381 382 383
        // we should do some proper error feedback here
        // std::cout << "type mismatch; not " << spec->get("item_type") << ": " << data << std::endl;
        // std::cout << spec << std::endl;
384 385 386
        if (errors) {
            errors->add(Element::create("Type mismatch"));
        }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
387
        return (false);
388
    }
389
    if (data->getType() == Element::list) {
390 391
        ConstElementPtr list_spec = spec->get("list_item_spec");
        BOOST_FOREACH(ConstElementPtr list_el, data->listValue()) {
392
            if (!check_type(list_spec, list_el)) {
393 394 395
                if (errors) {
                    errors->add(Element::create("Type mismatch"));
                }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
396
                return (false);
397
            }
398
            if (list_spec->get("item_type")->stringValue() == "map") {
399
                if (!validateItem(list_spec, list_el, full, errors)) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
400
                    return (false);
401 402
                }
            }
403 404
        }
    }
405
    if (data->getType() == Element::map) {
Jelte Jansen's avatar
Jelte Jansen committed
406 407
        // either a normal 'map' or a 'named set' (determined by which
        // subspecification it has)
408 409 410 411 412 413 414 415
        if (spec->contains("map_item_spec")) {
            if (!validateSpecList(spec->get("map_item_spec"), data, full, errors)) {
                return (false);
            }
        } else {
            typedef std::pair<std::string, ConstElementPtr> maptype;

            BOOST_FOREACH(maptype m, data->mapValue()) {
416
                if (!validateItem(spec->get("named_set_item_spec"), m.second, full, errors)) {
417 418 419
                    return (false);
                }
            }
420 421
        }
    }
Naoki Kambe's avatar
Naoki Kambe committed
422 423 424 425 426 427 428 429
    if (spec->contains("item_format")) {
        if (!check_format(data, spec->get("item_format"))) {
            if (errors) {
                errors->add(Element::create("Format mismatch"));
            }
            return (false);
        }
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
430
    return (true);
431 432 433 434
}

// spec is a map with item_name etc, data is a map
bool
435
ModuleSpec::validateSpec(ConstElementPtr spec, ConstElementPtr data,
436 437
                          const bool full, ElementPtr errors) const
{
438 439
    std::string item_name = spec->get("item_name")->stringValue();
    bool optional = spec->get("item_optional")->boolValue();
440
    ConstElementPtr data_el;
441
    data_el = data->get(item_name);
442
    
443
    if (data_el) {
444
        if (!validateItem(spec, data_el, full, errors)) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
445
            return (false);
446 447
        }
    } else {
448 449 450 451
        if (!optional && full) {
            if (errors) {
                errors->add(Element::create("Non-optional value missing"));
            }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
452
            return (false);
453 454
        }
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
455
    return (true);
456 457 458 459
}

// spec is a list of maps, data is a map
bool
460
ModuleSpec::validateSpecList(ConstElementPtr spec, ConstElementPtr data,
461 462
                               const bool full, ElementPtr errors) const
{
463
    bool validated = true;
464
    BOOST_FOREACH(ConstElementPtr cur_spec_el, spec->listValue()) {
465
        if (!validateSpec(cur_spec_el, data, full, errors)) {
466
            validated = false;
467 468
        }
    }
469 470 471 472

    typedef std::pair<std::string, ConstElementPtr> maptype;
    
    BOOST_FOREACH(maptype m, data->mapValue()) {
473
        // Ignore 'version' as a config element
474
        if (m.first.compare("version") != 0) {
475
            bool found = false;
476 477 478 479
            BOOST_FOREACH(ConstElementPtr cur_spec_el, spec->listValue()) {
                if (cur_spec_el->get("item_name")->stringValue().compare(m.first) == 0) {
                    found = true;
                }
480
            }
481 482 483 484 485
            if (!found) {
                validated = false;
                if (errors) {
                    errors->add(Element::create("Unknown item " + m.first));
                }
486 487 488 489 490
            }
        }
    }

    return (validated);
491 492
}

493 494
}
}