icinga2/lib/config/configtype.cpp

273 lines
8.1 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2013-03-17 20:19:29 +01:00
#include "config/configtype.h"
#include "config/configcompilercontext.h"
2013-03-16 21:18:53 +01:00
#include "base/objectlock.h"
#include "base/convert.h"
2013-03-18 11:02:18 +01:00
#include "base/scripttask.h"
2013-03-15 18:21:29 +01:00
#include <boost/tuple/tuple.hpp>
2013-03-16 21:18:53 +01:00
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/foreach.hpp>
using namespace icinga;
ConfigType::ConfigType(const String& name, const DebugInfo& debuginfo)
: m_Name(name), m_RuleList(boost::make_shared<TypeRuleList>()), m_DebugInfo(debuginfo)
{ }
String ConfigType::GetName(void) const
{
return m_Name;
}
String ConfigType::GetParent(void) const
{
return m_Parent;
}
void ConfigType::SetParent(const String& parent)
{
m_Parent = parent;
}
TypeRuleList::Ptr ConfigType::GetRuleList(void) const
{
return m_RuleList;
}
DebugInfo ConfigType::GetDebugInfo(void) const
{
return m_DebugInfo;
}
void ConfigType::ValidateItem(const ConfigItem::Ptr& item) const
{
2013-03-04 15:52:42 +01:00
Dictionary::Ptr attrs = item->Link();
/* Don't validate abstract items. */
if (item->IsAbstract())
return;
2013-03-16 21:18:53 +01:00
std::vector<String> locations;
2013-03-04 15:52:42 +01:00
locations.push_back("Object '" + item->GetName() + "' (Type: '" + item->GetType() + "')");
ConfigType::Ptr parent;
if (m_Parent.IsEmpty()) {
if (GetName() != "DynamicObject")
parent = ConfigCompilerContext::GetContext()->GetType("DynamicObject");
} else {
parent = ConfigCompilerContext::GetContext()->GetType(m_Parent);
}
2013-03-16 21:18:53 +01:00
std::vector<TypeRuleList::Ptr> ruleLists;
2013-03-01 12:07:52 +01:00
if (parent) {
ObjectLock plock(parent);
ruleLists.push_back(parent->m_RuleList);
2013-03-01 12:07:52 +01:00
}
ruleLists.push_back(m_RuleList);
ValidateDictionary(attrs, ruleLists, locations);
}
2013-02-17 19:14:34 +01:00
/**
* @threadsafety Always.
*/
2013-03-16 21:18:53 +01:00
String ConfigType::LocationToString(const std::vector<String>& locations)
{
bool first = true;
String stack;
BOOST_FOREACH(const String& location, locations) {
if (!first)
stack += " -> ";
else
first = false;
stack += location;
}
return stack;
}
2013-02-17 19:14:34 +01:00
/**
* @threadsafety Always.
*/
void ConfigType::ValidateDictionary(const Dictionary::Ptr& dictionary,
2013-03-16 21:18:53 +01:00
const std::vector<TypeRuleList::Ptr>& ruleLists, std::vector<String>& locations)
{
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
BOOST_FOREACH(const String& require, ruleList->GetRequires()) {
locations.push_back("Attribute '" + require + "'");
Value value = dictionary->Get(require);
if (value.IsEmpty()) {
ConfigCompilerContext::GetContext()->AddError(false,
"Required attribute is missing: " + LocationToString(locations));
}
locations.pop_back();
}
String validator = ruleList->GetValidator();
if (!validator.IsEmpty()) {
2013-03-15 11:19:52 +01:00
ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(validator);
if (!func)
2013-03-16 21:18:53 +01:00
BOOST_THROW_EXCEPTION(std::invalid_argument("Validator function '" + validator + "' does not exist."));
2013-03-16 21:18:53 +01:00
std::vector<Value> arguments;
arguments.push_back(LocationToString(locations));
arguments.push_back(dictionary);
ScriptTask::Ptr task = boost::make_shared<ScriptTask>(func, arguments);
task->Start();
2013-03-01 12:07:52 +01:00
task->GetResult();
}
}
2013-03-01 12:07:52 +01:00
ObjectLock olock(dictionary);
String key;
Value value;
2013-03-15 18:21:29 +01:00
BOOST_FOREACH(boost::tie(key, value), dictionary) {
TypeValidationResult overallResult = ValidationUnknownField;
2013-03-16 21:18:53 +01:00
std::vector<TypeRuleList::Ptr> subRuleLists;
locations.push_back("Attribute '" + key + "'");
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
TypeRuleList::Ptr subRuleList;
TypeValidationResult result = ruleList->ValidateAttribute(key, value, &subRuleList);
if (subRuleList)
subRuleLists.push_back(subRuleList);
if (overallResult == ValidationOK)
continue;
if (result == ValidationOK) {
overallResult = result;
continue;
}
if (result == ValidationInvalidType)
overallResult = result;
}
if (overallResult == ValidationUnknownField)
ConfigCompilerContext::GetContext()->AddError(true, "Unknown attribute: " + LocationToString(locations));
else if (overallResult == ValidationInvalidType)
ConfigCompilerContext::GetContext()->AddError(false, "Invalid type for attribute: " + LocationToString(locations));
2013-03-06 15:41:13 +01:00
if (!subRuleLists.empty() && value.IsObjectType<Dictionary>())
ValidateDictionary(value, subRuleLists, locations);
2013-03-14 13:24:07 +01:00
else if (!subRuleLists.empty() && value.IsObjectType<Array>())
ValidateArray(value, subRuleLists, locations);
locations.pop_back();
}
}
/**
* @threadsafety Always.
*/
void ConfigType::ValidateArray(const Array::Ptr& array,
2013-03-16 21:18:53 +01:00
const std::vector<TypeRuleList::Ptr>& ruleLists, std::vector<String>& locations)
2013-03-14 13:24:07 +01:00
{
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
BOOST_FOREACH(const String& require, ruleList->GetRequires()) {
2013-03-14 23:52:52 +01:00
size_t index = Convert::ToLong(require);
2013-03-14 13:24:07 +01:00
locations.push_back("Attribute '" + require + "'");
if (array->GetLength() < index) {
ConfigCompilerContext::GetContext()->AddError(false,
"Required array index is missing: " + LocationToString(locations));
}
locations.pop_back();
}
String validator = ruleList->GetValidator();
if (!validator.IsEmpty()) {
2013-03-15 11:19:52 +01:00
ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(validator);
2013-03-14 13:24:07 +01:00
if (!func)
2013-03-16 21:18:53 +01:00
BOOST_THROW_EXCEPTION(std::invalid_argument("Validator function '" + validator + "' does not exist."));
2013-03-14 13:24:07 +01:00
2013-03-16 21:18:53 +01:00
std::vector<Value> arguments;
2013-03-14 13:24:07 +01:00
arguments.push_back(LocationToString(locations));
arguments.push_back(array);
ScriptTask::Ptr task = boost::make_shared<ScriptTask>(func, arguments);
task->Start();
task->GetResult();
}
}
ObjectLock olock(array);
int index = 0;
String key;
BOOST_FOREACH(const Value& value, array) {
key = Convert::ToString(index);
index++;
TypeValidationResult overallResult = ValidationUnknownField;
2013-03-16 21:18:53 +01:00
std::vector<TypeRuleList::Ptr> subRuleLists;
2013-03-14 13:24:07 +01:00
locations.push_back("Attribute '" + key + "'");
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
TypeRuleList::Ptr subRuleList;
TypeValidationResult result = ruleList->ValidateAttribute(key, value, &subRuleList);
if (subRuleList)
subRuleLists.push_back(subRuleList);
if (overallResult == ValidationOK)
continue;
if (result == ValidationOK) {
overallResult = result;
continue;
}
if (result == ValidationInvalidType)
overallResult = result;
}
if (overallResult == ValidationUnknownField)
ConfigCompilerContext::GetContext()->AddError(true, "Unknown attribute: " + LocationToString(locations));
else if (overallResult == ValidationInvalidType)
ConfigCompilerContext::GetContext()->AddError(false, "Invalid type for array index: " + LocationToString(locations));
if (!subRuleLists.empty() && value.IsObjectType<Dictionary>())
ValidateDictionary(value, subRuleLists, locations);
else if (!subRuleLists.empty() && value.IsObjectType<Array>())
ValidateArray(value, subRuleLists, locations);
locations.pop_back();
}
}