module_spec.cc 10.2 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::config;
28

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

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

52
void check_config_item_list(ConstElementPtr spec);
53

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

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

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

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

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

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

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

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

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

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

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

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

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

178
bool
179 180
ModuleSpec::validate_config(ConstElementPtr data, const bool full) const {
    ConstElementPtr spec = module_specification->find("config_data");
JINMEI Tatuya's avatar
JINMEI Tatuya committed
181
    return (validate_spec_list(spec, data, full, ElementPtr()));
182 183 184
}

bool
185 186
ModuleSpec::validate_config(ConstElementPtr data, const bool full,
                            ElementPtr errors) const
187
{
188
    ConstElementPtr spec = module_specification->find("config_data");
JINMEI Tatuya's avatar
JINMEI Tatuya committed
189
    return (validate_spec_list(spec, data, full, errors));
190 191
}

192 193
ModuleSpec
moduleSpecFromFile(const std::string& file_name, const bool check)
Jelte Jansen's avatar
Jelte Jansen committed
194
                   throw(JSONError, ModuleSpecError)
195 196 197 198 199 200 201 202 203 204
{
    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());
    }

205
    ConstElementPtr module_spec_element = Element::fromJSON(file, file_name);
206
    if (module_spec_element->contains("module_spec")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
207
        return (ModuleSpec(module_spec_element->get("module_spec"), check));
208 209 210
    } else {
        throw ModuleSpecError("No module_spec in specification");
    }
211 212 213 214
}

ModuleSpec
moduleSpecFromFile(std::ifstream& in, const bool check)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
215 216
                   throw(JSONError, ModuleSpecError)
{
217
    ConstElementPtr module_spec_element = Element::fromJSON(in);
218
    if (module_spec_element->contains("module_spec")) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
219
        return (ModuleSpec(module_spec_element->get("module_spec"), check));
220 221 222
    } else {
        throw ModuleSpecError("No module_spec in specification");
    }
223 224
}

225

226
namespace {
227 228 229 230
//
// private functions
//

231 232 233
//
// helper functions for validation
//
234 235
bool
check_type(ConstElementPtr spec, ConstElementPtr element) {
236
    std::string cur_item_type;
237
    cur_item_type = spec->get("item_type")->stringValue();
238
    if (cur_item_type == "any") {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
239
        return (true);
240
    }
241
    switch (element->getType()) {
242
        case Element::integer:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
243
            return (cur_item_type == "integer");
244 245
            break;
        case Element::real:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
246
            return (cur_item_type == "real");
247 248
            break;
        case Element::boolean:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
249
            return (cur_item_type == "boolean");
250 251
            break;
        case Element::string:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
252
            return (cur_item_type == "string");
253 254
            break;
        case Element::list:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
255
            return (cur_item_type == "list");
256 257
            break;
        case Element::map:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
258
            return (cur_item_type == "map");
259 260
            break;
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
261
    return (false);
262
}
263
}
264 265

bool
266 267 268
ModuleSpec::validate_item(ConstElementPtr spec, ConstElementPtr data,
                          const bool full, ElementPtr errors) const
{
269
    if (!check_type(spec, data)) {
270 271 272
        // 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;
273 274 275
        if (errors) {
            errors->add(Element::create("Type mismatch"));
        }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
276
        return (false);
277
    }
278
    if (data->getType() == Element::list) {
279 280
        ConstElementPtr list_spec = spec->get("list_item_spec");
        BOOST_FOREACH(ConstElementPtr list_el, data->listValue()) {
281
            if (!check_type(list_spec, list_el)) {
282 283 284
                if (errors) {
                    errors->add(Element::create("Type mismatch"));
                }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
285
                return (false);
286
            }
287
            if (list_spec->get("item_type")->stringValue() == "map") {
288
                if (!validate_item(list_spec, list_el, full, errors)) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
289
                    return (false);
290 291
                }
            }
292 293
        }
    }
294
    if (data->getType() == Element::map) {
295
        if (!validate_spec_list(spec->get("map_item_spec"), data, full, errors)) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
296
            return (false);
297 298
        }
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
299
    return (true);
300 301 302 303
}

// spec is a map with item_name etc, data is a map
bool
304 305 306
ModuleSpec::validate_spec(ConstElementPtr spec, ConstElementPtr data,
                          const bool full, ElementPtr errors) const
{
307 308
    std::string item_name = spec->get("item_name")->stringValue();
    bool optional = spec->get("item_optional")->boolValue();
309
    ConstElementPtr data_el;
310
    data_el = data->get(item_name);
311
    
312
    if (data_el) {
313
        if (!validate_item(spec, data_el, full, errors)) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
314
            return (false);
315 316
        }
    } else {
317 318 319 320
        if (!optional && full) {
            if (errors) {
                errors->add(Element::create("Non-optional value missing"));
            }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
321
            return (false);
322 323
        }
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
324
    return (true);
325 326 327 328
}

// spec is a list of maps, data is a map
bool
329 330 331
ModuleSpec::validate_spec_list(ConstElementPtr spec, ConstElementPtr data,
                               const bool full, ElementPtr errors) const
{
332
    std::string cur_item_name;
333
    BOOST_FOREACH(ConstElementPtr cur_spec_el, spec->listValue()) {
334
        if (!validate_spec(cur_spec_el, data, full, errors)) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
335
            return (false);
336 337
        }
    }
JINMEI Tatuya's avatar
JINMEI Tatuya committed
338
    return (true);
339 340
}

341 342
}
}