module_spec.cc 10 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
namespace isc {
28
namespace config {
29

30 31 32 33
//
// static functions
//

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

static void check_config_item_list(const ElementPtr& spec);

static void
check_config_item(const ElementPtr& spec) {
    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",
59
                    Element::nameToType(spec->get("item_type")->stringValue()),
60
                    !spec->get("item_optional")->boolValue()
61 62
                   );

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

static void
check_config_item_list(const ElementPtr& spec) {
77
    if (spec->getType() != Element::list) {
78
        throw ModuleSpecError("config_data is not a list of elements");
79
    }
80
    BOOST_FOREACH(ElementPtr item, spec->listValue()) {
81 82 83 84 85 86 87 88 89 90 91 92 93
        check_config_item(item);
    }
}

static void
check_command(const ElementPtr& spec) {
    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"));
}

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

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

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

128 129 130 131
//
// Public functions
//

132 133 134 135 136 137
ModuleSpec::ModuleSpec(ElementPtr module_spec_element,
                       const bool check)
                       throw(ModuleSpecError)
                       
{
    module_specification = module_spec_element;
138
    if (check) {
139 140 141 142 143
        check_module_specification(module_specification);
    }
}

const ElementPtr
144
ModuleSpec::getCommandsSpec() const
145 146 147 148 149 150 151 152 153
{
    if (module_specification->contains("commands")) {
        return module_specification->get("commands");
    } else {
        return ElementPtr();
    }
}

const ElementPtr
154
ModuleSpec::getConfigSpec() const
155 156 157 158 159
{
    if (module_specification->contains("config_data")) {
        return module_specification->get("config_data");
    } else {
        return ElementPtr();
160 161 162
    }
}

163
const std::string
164
ModuleSpec::getModuleName() const
165 166 167 168
{
    return module_specification->get("module_name")->stringValue();
}

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

179
bool
180
ModuleSpec::validate_config(const ElementPtr data, const bool full)
181
{
182
    ElementPtr spec = module_specification->find("config_data");
183 184 185 186 187 188
    return validate_spec_list(spec, data, full, ElementPtr());
}

bool
ModuleSpec::validate_config(const ElementPtr data, const bool full, ElementPtr errors)
{
189
    ElementPtr spec = module_specification->find("config_data");
190
    return validate_spec_list(spec, data, full, errors);
191 192
}

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

206
    ElementPtr module_spec_element = Element::fromJSON(file, file_name);
207 208 209 210 211
    if (module_spec_element->contains("module_spec")) {
        return ModuleSpec(module_spec_element->get("module_spec"), check);
    } else {
        throw ModuleSpecError("No module_spec in specification");
    }
212 213 214 215
}

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

225 226 227 228 229

//
// private functions
//

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

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

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

// spec is a list of maps, data is a map
bool
324
ModuleSpec::validate_spec_list(const ElementPtr spec, const ElementPtr data, const bool full, ElementPtr errors) {
325 326
    ElementPtr cur_data_el;
    std::string cur_item_name;
327
    BOOST_FOREACH(ElementPtr cur_spec_el, spec->listValue()) {
328
        if (!validate_spec(cur_spec_el, data, full, errors)) {
329 330 331 332 333 334
            return false;
        }
    }
    return true;
}

335 336
}
}