2013-02-06 12:09:50 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2013-02-06 12:09:50 +01:00
|
|
|
* *
|
|
|
|
* 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-25 18:36:15 +01:00
|
|
|
#include "base/scriptfunction.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2013-02-06 12:09:50 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
ConfigType::ConfigType(const String& name, const DebugInfo& debuginfo)
|
2013-11-06 08:51:56 +01:00
|
|
|
: m_Name(name), m_RuleList(make_shared<TypeRuleList>()), m_DebugInfo(debuginfo)
|
2013-02-06 12:09:50 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-06 11:26:00 +02:00
|
|
|
void ConfigType::AddParentRules(std::vector<TypeRuleList::Ptr>& ruleLists, const ConfigType::Ptr& item)
|
|
|
|
{
|
|
|
|
ConfigType::Ptr parent;
|
|
|
|
if (item->m_Parent.IsEmpty()) {
|
|
|
|
if (item->GetName() != "DynamicObject")
|
2013-08-20 11:06:04 +02:00
|
|
|
parent = ConfigType::GetByName("DynamicObject");
|
2013-06-06 11:26:00 +02:00
|
|
|
} else {
|
2013-08-20 11:06:04 +02:00
|
|
|
parent = ConfigType::GetByName(item->m_Parent);
|
2013-06-06 11:26:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parent) {
|
|
|
|
AddParentRules(ruleLists, parent);
|
|
|
|
|
|
|
|
ObjectLock plock(parent);
|
|
|
|
ruleLists.push_back(parent->m_RuleList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigType::ValidateItem(const ConfigItem::Ptr& item)
|
2013-02-06 12:09:50 +01:00
|
|
|
{
|
|
|
|
/* Don't validate abstract items. */
|
2013-03-11 13:05:32 +01:00
|
|
|
if (item->IsAbstract())
|
2013-02-06 12:09:50 +01:00
|
|
|
return;
|
|
|
|
|
2013-12-14 23:10:37 +01:00
|
|
|
Dictionary::Ptr attrs;
|
|
|
|
DebugInfo debugInfo;
|
|
|
|
String type, name;
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(item);
|
|
|
|
|
|
|
|
attrs = item->GetProperties();
|
|
|
|
debugInfo = item->GetDebugInfo();
|
|
|
|
type = item->GetType();
|
|
|
|
name = item->GetName();
|
|
|
|
}
|
2013-03-19 07:09:06 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<String> locations;
|
2013-12-14 23:10:37 +01:00
|
|
|
locations.push_back("Object '" + name + "' (Type: '" + type + "') at " + debugInfo.Path + ":" + Convert::ToString(debugInfo.FirstLine));
|
2013-02-06 12:09:50 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<TypeRuleList::Ptr> ruleLists;
|
2013-06-06 11:26:00 +02:00
|
|
|
AddParentRules(ruleLists, GetSelf());
|
2013-02-06 12:09:50 +01:00
|
|
|
ruleLists.push_back(m_RuleList);
|
|
|
|
|
|
|
|
ValidateDictionary(attrs, ruleLists, locations);
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
String ConfigType::LocationToString(const std::vector<String>& locations)
|
2013-02-06 12:09:50 +01:00
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
String stack;
|
|
|
|
BOOST_FOREACH(const String& location, locations) {
|
|
|
|
if (!first)
|
|
|
|
stack += " -> ";
|
|
|
|
else
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
stack += location;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2013-02-06 12:09:50 +01:00
|
|
|
{
|
|
|
|
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()) {
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true,
|
2013-02-06 12:09:50 +01:00
|
|
|
"Required attribute is missing: " + LocationToString(locations));
|
|
|
|
}
|
|
|
|
|
|
|
|
locations.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
String validator = ruleList->GetValidator();
|
|
|
|
|
|
|
|
if (!validator.IsEmpty()) {
|
2014-03-30 15:04:53 +02:00
|
|
|
ScriptFunction::Ptr func = ScriptFunction::GetByName(validator);
|
2013-02-06 12:09:50 +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-02-06 12:09:50 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<Value> arguments;
|
2013-02-06 12:09:50 +01:00
|
|
|
arguments.push_back(LocationToString(locations));
|
|
|
|
arguments.push_back(dictionary);
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
func->Invoke(arguments);
|
2013-02-06 12:09:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(dictionary);
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, dictionary) {
|
2013-02-06 12:09:50 +01:00
|
|
|
TypeValidationResult overallResult = ValidationUnknownField;
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<TypeRuleList::Ptr> subRuleLists;
|
2013-05-03 10:48:28 +02:00
|
|
|
String hint;
|
2013-02-06 12:09:50 +01:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
locations.push_back("Attribute '" + kv.first + "'");
|
2013-02-06 12:09:50 +01:00
|
|
|
|
|
|
|
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
|
|
|
|
TypeRuleList::Ptr subRuleList;
|
2013-11-30 17:42:50 +01:00
|
|
|
TypeValidationResult result = ruleList->ValidateAttribute(kv.first, kv.second, &subRuleList, &hint);
|
2013-02-06 12:09:50 +01:00
|
|
|
|
|
|
|
if (subRuleList)
|
|
|
|
subRuleLists.push_back(subRuleList);
|
|
|
|
|
2013-02-06 13:21:55 +01:00
|
|
|
if (overallResult == ValidationOK)
|
|
|
|
continue;
|
|
|
|
|
2013-02-06 12:09:50 +01:00
|
|
|
if (result == ValidationOK) {
|
|
|
|
overallResult = result;
|
2013-02-06 13:21:55 +01:00
|
|
|
continue;
|
2013-02-06 12:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result == ValidationInvalidType)
|
|
|
|
overallResult = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overallResult == ValidationUnknownField)
|
2014-03-28 14:25:16 +01:00
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, "Unknown attribute: " + LocationToString(locations));
|
2013-05-03 10:48:28 +02:00
|
|
|
else if (overallResult == ValidationInvalidType) {
|
|
|
|
String message = "Invalid value for attribute: " + LocationToString(locations);
|
|
|
|
|
|
|
|
if (!hint.IsEmpty())
|
|
|
|
message += ": " + hint;
|
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, message);
|
2013-05-03 10:48:28 +02:00
|
|
|
}
|
2013-02-06 12:09:50 +01:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
if (!subRuleLists.empty() && kv.second.IsObjectType<Dictionary>())
|
|
|
|
ValidateDictionary(kv.second, subRuleLists, locations);
|
|
|
|
else if (!subRuleLists.empty() && kv.second.IsObjectType<Array>())
|
|
|
|
ValidateArray(kv.second, subRuleLists, locations);
|
2013-03-14 13:24:07 +01:00
|
|
|
|
|
|
|
locations.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true,
|
2013-03-14 13:24:07 +01:00
|
|
|
"Required array index is missing: " + LocationToString(locations));
|
|
|
|
}
|
|
|
|
|
|
|
|
locations.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
String validator = ruleList->GetValidator();
|
|
|
|
|
|
|
|
if (!validator.IsEmpty()) {
|
2014-03-30 15:04:53 +02:00
|
|
|
ScriptFunction::Ptr func = ScriptFunction::GetByName(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);
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
func->Invoke(arguments);
|
2013-03-14 13:24:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-03 10:48:28 +02:00
|
|
|
String hint;
|
2013-03-14 13:24:07 +01:00
|
|
|
|
2013-05-03 10:48:28 +02:00
|
|
|
locations.push_back("Index " + key);
|
2013-03-14 13:24:07 +01:00
|
|
|
|
|
|
|
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
|
|
|
|
TypeRuleList::Ptr subRuleList;
|
2013-05-03 10:48:28 +02:00
|
|
|
TypeValidationResult result = ruleList->ValidateAttribute(key, value, &subRuleList, &hint);
|
2013-03-14 13:24:07 +01:00
|
|
|
|
|
|
|
if (subRuleList)
|
|
|
|
subRuleLists.push_back(subRuleList);
|
|
|
|
|
|
|
|
if (overallResult == ValidationOK)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (result == ValidationOK) {
|
|
|
|
overallResult = result;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == ValidationInvalidType)
|
|
|
|
overallResult = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overallResult == ValidationUnknownField)
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(false, "Unknown attribute: " + LocationToString(locations));
|
2013-05-03 10:48:28 +02:00
|
|
|
else if (overallResult == ValidationInvalidType) {
|
|
|
|
String message = "Invalid value for array index: " + LocationToString(locations);
|
|
|
|
|
|
|
|
if (!hint.IsEmpty())
|
|
|
|
message += ": " + hint;
|
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, message);
|
2013-05-03 10:48:28 +02:00
|
|
|
}
|
2013-03-14 13:24:07 +01:00
|
|
|
|
|
|
|
if (!subRuleLists.empty() && value.IsObjectType<Dictionary>())
|
|
|
|
ValidateDictionary(value, subRuleLists, locations);
|
|
|
|
else if (!subRuleLists.empty() && value.IsObjectType<Array>())
|
|
|
|
ValidateArray(value, subRuleLists, locations);
|
2013-02-06 12:09:50 +01:00
|
|
|
|
|
|
|
locations.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
|
|
|
|
void ConfigType::Register(void)
|
|
|
|
{
|
2013-11-30 17:47:53 +01:00
|
|
|
ConfigTypeRegistry::GetInstance()->Register(GetName(), GetSelf());
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigType::Ptr ConfigType::GetByName(const String& name)
|
|
|
|
{
|
2013-11-30 17:47:53 +01:00
|
|
|
return ConfigTypeRegistry::GetInstance()->GetItem(name);
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
|
2013-11-30 17:47:53 +01:00
|
|
|
ConfigTypeRegistry::ItemMap ConfigType::GetTypes(void)
|
2013-08-20 11:06:04 +02:00
|
|
|
{
|
2013-11-30 17:47:53 +01:00
|
|
|
return ConfigTypeRegistry::GetInstance()->GetItems();
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigType::DiscardTypes(void)
|
|
|
|
{
|
2013-11-30 17:47:53 +01:00
|
|
|
ConfigTypeRegistry::GetInstance()->Clear();
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
2013-11-30 17:47:53 +01:00
|
|
|
|
|
|
|
ConfigTypeRegistry *ConfigTypeRegistry::GetInstance(void)
|
|
|
|
{
|
|
|
|
return Singleton<ConfigTypeRegistry>::GetInstance();
|
|
|
|
}
|
|
|
|
|