Commit d5ded106 authored by Naoki Kambe's avatar Naoki Kambe

[trac929]

addition and modification as a variant of the statistics part of module_spec.py
 - add check_format which checks whether the given element is a valid statistics specification
 - modify check_data_specification to add check of statistics specification
 - add getStatisticsSpec() which returns statistics specification
 - add two of validateStatistics which check whether specified data is valid for statistics specification
 - modify validateItem to add check of item_format in specification

update the year of copyright
parent c4ef641d
// Copyright (C) 2010 Internet Systems Consortium. // Copyright (C) 2010, 2011 Internet Systems Consortium.
// //
// Permission to use, copy, modify, and distribute this software for any // Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above // purpose with or without fee is hereby granted, provided that the above
...@@ -87,6 +87,54 @@ check_config_item_list(ConstElementPtr spec) { ...@@ -87,6 +87,54 @@ check_config_item_list(ConstElementPtr spec) {
} }
} }
// 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;
return (strptime(value->stringValue().c_str(),
f.second.c_str(), &tm) != NULL);
}
}
return (false);
}
void check_statistics_item_list(ConstElementPtr spec);
void
check_statistics_item_list(ConstElementPtr spec) {
if (spec->getType() != Element::list) {
throw ModuleSpecError("statistics is not a list of elements");
}
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"))) {
throw ModuleSpecError(
"item_default not valid type of item_format");
}
}
}
}
void void
check_command(ConstElementPtr spec) { check_command(ConstElementPtr spec) {
check_leaf_item(spec, "command_name", Element::string, true); check_leaf_item(spec, "command_name", Element::string, true);
...@@ -116,6 +164,9 @@ check_data_specification(ConstElementPtr spec) { ...@@ -116,6 +164,9 @@ check_data_specification(ConstElementPtr spec) {
if (spec->contains("commands")) { if (spec->contains("commands")) {
check_command_list(spec->get("commands")); check_command_list(spec->get("commands"));
} }
if (spec->contains("statistics")) {
check_statistics_item_list(spec->get("statistics"));
}
} }
// checks whether the given element is a valid module specification // checks whether the given element is a valid module specification
...@@ -165,6 +216,15 @@ ModuleSpec::getConfigSpec() const { ...@@ -165,6 +216,15 @@ ModuleSpec::getConfigSpec() const {
} }
} }
ConstElementPtr
ModuleSpec::getStatisticsSpec() const {
if (module_specification->contains("statistics")) {
return (module_specification->get("statistics"));
} else {
return (ElementPtr());
}
}
const std::string const std::string
ModuleSpec::getModuleName() const { ModuleSpec::getModuleName() const {
return (module_specification->get("module_name")->stringValue()); return (module_specification->get("module_name")->stringValue());
...@@ -185,6 +245,12 @@ ModuleSpec::validateConfig(ConstElementPtr data, const bool full) const { ...@@ -185,6 +245,12 @@ ModuleSpec::validateConfig(ConstElementPtr data, const bool full) const {
return (validateSpecList(spec, data, full, ElementPtr())); return (validateSpecList(spec, data, full, ElementPtr()));
} }
bool
ModuleSpec::validateStatistics(ConstElementPtr data, const bool full) const {
ConstElementPtr spec = module_specification->find("statistics");
return (validateSpecList(spec, data, full, ElementPtr()));
}
bool bool
ModuleSpec::validateCommand(const std::string& command, ModuleSpec::validateCommand(const std::string& command,
ConstElementPtr args, ConstElementPtr args,
...@@ -223,6 +289,14 @@ ModuleSpec::validateConfig(ConstElementPtr data, const bool full, ...@@ -223,6 +289,14 @@ ModuleSpec::validateConfig(ConstElementPtr data, const bool full,
return (validateSpecList(spec, data, full, errors)); return (validateSpecList(spec, data, full, errors));
} }
bool
ModuleSpec::validateStatistics(ConstElementPtr data, const bool full,
ElementPtr errors) const
{
ConstElementPtr spec = module_specification->find("statistics");
return (validateSpecList(spec, data, full, errors));
}
ModuleSpec ModuleSpec
moduleSpecFromFile(const std::string& file_name, const bool check) moduleSpecFromFile(const std::string& file_name, const bool check)
throw(JSONError, ModuleSpecError) throw(JSONError, ModuleSpecError)
...@@ -343,6 +417,14 @@ ModuleSpec::validateItem(ConstElementPtr spec, ConstElementPtr data, ...@@ -343,6 +417,14 @@ ModuleSpec::validateItem(ConstElementPtr spec, ConstElementPtr data,
} }
} }
} }
if (spec->contains("item_format")) {
if (!check_format(data, spec->get("item_format"))) {
if (errors) {
errors->add(Element::create("Format mismatch"));
}
return (false);
}
}
return (true); return (true);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment