module_spec.cc 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2010  Internet Systems Consortium.
//
// Permission to use, copy, modify, and 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 INTERNET SYSTEMS CONSORTIUM
// DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
// INTERNET SYSTEMS CONSORTIUM 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.
15

16
#include <config/module_spec.h>
17 18

#include <sstream>
Jelte Jansen's avatar
Jelte Jansen committed
19
#include <iostream>
Jelte Jansen's avatar
Jelte Jansen committed
20 21
#include <fstream>
#include <cerrno>
22 23 24

#include <boost/foreach.hpp>

25
// todo: add more context to thrown ModuleSpecErrors?
26

27
using namespace isc::data;
28
using namespace isc::config;
29

30
namespace {
31
//
32
// Private functions
33 34
//

35 36 37
void
check_leaf_item(ConstElementPtr spec, const std::string& name,
                Element::types type, bool mandatory)
38 39
{
    if (spec->contains(name)) {
40
        if (spec->get(name)->getType() == type) {
41 42
            return;
        } else {
43
            throw ModuleSpecError(name + " not of type " + Element::typeToName(type));
44 45 46 47 48
        }
    } 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...
49
        throw ModuleSpecError(name + " missing in " + spec->str());
50 51 52
    }
}

53
void check_config_item_list(ConstElementPtr spec);
54

55 56
void
check_config_item(ConstElementPtr spec) {
57 58 59 60
    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",
61
                    Element::nameToType(spec->get("item_type")->stringValue()),
62
                    !spec->get("item_optional")->boolValue()
63 64
                   );

65
    // if list, check the list specification
66
    if (Element::nameToType(spec->get("item_type")->stringValue()) == Element::list) {
67 68 69 70
        check_leaf_item(spec, "list_item_spec", Element::map, true);
        check_config_item(spec->get("list_item_spec"));
    }
    // todo: add stuff for type map
71
    if (Element::nameToType(spec->get("item_type")->stringValue()) == Element::map) {
72
        check_leaf_item(spec, "map_item_spec", Element::list, true);
Jelte Jansen's avatar
Jelte Jansen committed
73
        check_config_item_list(spec->get("map_item_spec"));
74 75 76
    }
}

77 78
void
check_config_item_list(ConstElementPtr spec) {
79
    if (spec->getType() != Element::list) {
80
        throw ModuleSpecError("config_data is not a list of elements");
81
    }
82
    BOOST_FOREACH(ConstElementPtr item, spec->listValue()) {
83 84 85 86
        check_config_item(item);
    }
}

87 88
void
check_command(ConstElementPtr spec) {
89 90 91 92 93
    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"));
}

94 95
void
check_command_list(ConstElementPtr spec) {
96
    if (spec->getType() != Element::list) {
97
        throw ModuleSpecError("commands is not a list of elements");
98
    }
99
    BOOST_FOREACH(ConstElementPtr item, spec->listValue()) {
100 101 102 103
        check_command(item);
    }
}

104 105
void
check_data_specification(ConstElementPtr spec) {
106
    check_leaf_item(spec, "module_name", Element::string, true);
Jelte Jansen's avatar
Jelte Jansen committed
107
    check_leaf_item(spec, "module_description", Element::string, false);
108 109
    // config_data is not mandatory; module could just define
    // commands and have no config
110 111 112 113 114 115 116 117
    if (spec->contains("config_data")) {
        check_config_item_list(spec->get("config_data"));
    }
    if (spec->contains("commands")) {
        check_command_list(spec->get("commands"));
    }
}

118
// checks whether the given element is a valid module specification
119
// throws a ModuleSpecError if the specification is bad
120 121
void
check_module_specification(ConstElementPtr def) {
122 123
    try {
        check_data_specification(def);
124
    } catch (const TypeError& te) {
125 126
        throw ModuleSpecError(te.what());
    }
127
}
128
}
129

130 131
namespace isc {
namespace config {
132 133 134 135
//
// Public functions
//

136
ModuleSpec::ModuleSpec(ConstElementPtr module_spec_element,
137 138 139 140 141
                       const bool check)
                       throw(ModuleSpecError)
                       
{
    module_specification = module_spec_element;
142
    if (check) {
143 144 145 146
        check_module_specification(module_specification);
    }
}

147
ConstElementPtr
JINMEI Tatuya's avatar
JINMEI Tatuya committed
148
ModuleSpec::getCommandsSpec() const {
149
    if (module_specification->contains("commands")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
150
        return (module_specification->get("commands"));
151
    } else {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
152
        return (ElementPtr());
153 154 155
    }
}

156
ConstElementPtr
JINMEI Tatuya's avatar
JINMEI Tatuya committed
157
ModuleSpec::getConfigSpec() const {
158
    if (module_specification->contains("config_data")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
159
        return (module_specification->get("config_data"));
160
    } else {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
161
        return (ElementPtr());
162 163 164
    }
}

165
const std::string
JINMEI Tatuya's avatar
JINMEI Tatuya committed
166 167
ModuleSpec::getModuleName() const {
    return (module_specification->get("module_name")->stringValue());
168 169
}

Jelte Jansen's avatar
Jelte Jansen committed
170
const std::string
JINMEI Tatuya's avatar
JINMEI Tatuya committed
171
ModuleSpec::getModuleDescription() const {
Jelte Jansen's avatar
Jelte Jansen committed
172
    if (module_specification->contains("module_description")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
173
        return (module_specification->get("module_description")->stringValue());
Jelte Jansen's avatar
Jelte Jansen committed
174
    } else {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
175
        return (std::string(""));
Jelte Jansen's avatar
Jelte Jansen committed
176 177 178
    }
}

179
bool
180
ModuleSpec::validateConfig(ConstElementPtr data, const bool full) const {
181
    ConstElementPtr spec = module_specification->find("config_data");
182
    return (validateSpecList(spec, data, full, ElementPtr()));
183 184
}

185
bool
186
ModuleSpec::validateCommand(const std::string& command,
187
                             ConstElementPtr args,
188
                             ElementPtr errors) const
189
{
190
    if (args->getType() != Element::map) {
Jelte Jansen's avatar
Jelte Jansen committed
191 192
        errors->add(Element::create("args for command " +
                                    command + " is not a map"));
193 194 195
        return (false);
    }

196
    ConstElementPtr commands_spec = module_specification->find("commands");
197 198 199 200 201 202 203 204
    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) {
205
            return (validateSpecList(cur_command->get("command_args"),
Jelte Jansen's avatar
Jelte Jansen committed
206
                                       args, true, errors));
207 208 209 210 211 212 213 214
        }
    }

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

215
bool
216
ModuleSpec::validateConfig(ConstElementPtr data, const bool full,
217
                            ElementPtr errors) const
218
{
219
    ConstElementPtr spec = module_specification->find("config_data");
220
    return (validateSpecList(spec, data, full, errors));
221 222
}

223 224
ModuleSpec
moduleSpecFromFile(const std::string& file_name, const bool check)
Jelte Jansen's avatar
Jelte Jansen committed
225
                   throw(JSONError, ModuleSpecError)
226 227 228 229 230 231 232 233 234 235
{
    std::ifstream file;

    file.open(file_name.c_str());
    if (!file) {
        std::stringstream errs;
        errs << "Error opening " << file_name << ": " << strerror(errno);
        throw ModuleSpecError(errs.str());
    }

236
    ConstElementPtr module_spec_element = Element::fromJSON(file, file_name);
237
    if (module_spec_element->contains("module_spec")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
238
        return (ModuleSpec(module_spec_element->get("module_spec"), check));
239 240 241
    } else {
        throw ModuleSpecError("No module_spec in specification");
    }
242 243 244 245
}

ModuleSpec
moduleSpecFromFile(std::ifstream& in, const bool check)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
246 247
                   throw(JSONError, ModuleSpecError)
{
248
    ConstElementPtr module_spec_element = Element::fromJSON(in);
249
    if (module_spec_element->contains("module_spec")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
250
        return (ModuleSpec(module_spec_element->get("module_spec"), check));
251 252 253
    } else {
        throw ModuleSpecError("No module_spec in specification");
    }
254 255
}

256

257
namespace {
258 259 260 261
//
// private functions
//

262 263 264
//
// helper functions for validation
//
265 266
bool
check_type(ConstElementPtr spec, ConstElementPtr element) {
267
    std::string cur_item_type;
268
    cur_item_type = spec->get("item_type")->stringValue();
269
    if (cur_item_type == "any") {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
270
        return (true);
271
    }
272
    switch (element->getType()) {
273
        case Element::integer:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
274
            return (cur_item_type == "integer");
275 276
            break;
        case Element::real:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
277
            return (cur_item_type == "real");
278 279
            break;
        case Element::boolean:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
280
            return (cur_item_type == "boolean");
281 282
            break;
        case Element::string:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
283
            return (cur_item_type == "string");
284 285
            break;
        case Element::list:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
286
            return (cur_item_type == "list");
287 288
            break;
        case Element::map:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
289
            return (cur_item_type == "map");
290 291
            break;
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
292
    return (false);
293
}
294
}
295 296

bool
297
ModuleSpec::validateItem(ConstElementPtr spec, ConstElementPtr data,
298 299
                          const bool full, ElementPtr errors) const
{
300
    if (!check_type(spec, data)) {
301 302 303
        // 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;
304 305 306
        if (errors) {
            errors->add(Element::create("Type mismatch"));
        }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
307
        return (false);
308
    }
309
    if (data->getType() == Element::list) {
310 311
        ConstElementPtr list_spec = spec->get("list_item_spec");
        BOOST_FOREACH(ConstElementPtr list_el, data->listValue()) {
312
            if (!check_type(list_spec, list_el)) {
313 314 315
                if (errors) {
                    errors->add(Element::create("Type mismatch"));
                }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
316
                return (false);
317
            }
318
            if (list_spec->get("item_type")->stringValue() == "map") {
319
                if (!validateItem(list_spec, list_el, full, errors)) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
320
                    return (false);
321 322
                }
            }
323 324
        }
    }
325
    if (data->getType() == Element::map) {
326
        if (!validateSpecList(spec->get("map_item_spec"), data, full, errors)) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
327
            return (false);
328 329
        }
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
330
    return (true);
331 332 333 334
}

// spec is a map with item_name etc, data is a map
bool
335
ModuleSpec::validateSpec(ConstElementPtr spec, ConstElementPtr data,
336 337
                          const bool full, ElementPtr errors) const
{
338 339
    std::string item_name = spec->get("item_name")->stringValue();
    bool optional = spec->get("item_optional")->boolValue();
340
    ConstElementPtr data_el;
341
    data_el = data->get(item_name);
342
    
343
    if (data_el) {
344
        if (!validateItem(spec, data_el, full, errors)) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
345
            return (false);
346 347
        }
    } else {
348 349 350 351
        if (!optional && full) {
            if (errors) {
                errors->add(Element::create("Non-optional value missing"));
            }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
352
            return (false);
353 354
        }
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
355
    return (true);
356 357 358 359
}

// spec is a list of maps, data is a map
bool
360
ModuleSpec::validateSpecList(ConstElementPtr spec, ConstElementPtr data,
361 362
                               const bool full, ElementPtr errors) const
{
363
    bool validated = true;
364
    std::string cur_item_name;
365
    BOOST_FOREACH(ConstElementPtr cur_spec_el, spec->listValue()) {
366
        if (!validateSpec(cur_spec_el, data, full, errors)) {
367
            validated = false;
368 369
        }
    }
370 371 372 373 374

    typedef std::pair<std::string, ConstElementPtr> maptype;
    
    BOOST_FOREACH(maptype m, data->mapValue()) {
        bool found = false;
375 376 377 378 379 380 381 382
        // Ignore 'version' as a config element
        if (m.first.compare("version") == 0) {
            found = true;
        } else {
            BOOST_FOREACH(ConstElementPtr cur_spec_el, spec->listValue()) {
                if (cur_spec_el->get("item_name")->stringValue().compare(m.first) == 0) {
                    found = true;
                }
383 384 385 386 387 388 389 390 391 392 393
            }
        }
        if (!found) {
            validated = false;
            if (errors) {
                errors->add(Element::create("Unknown item " + m.first));
            }
        }
    }

    return (validated);
394 395
}

396 397
}
}