2014-03-18 11:44:09 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-04-23 15:57:58 +02:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2014-03-18 11:44:09 +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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "config/applyrule.hpp"
|
|
|
|
#include "base/logger_fwd.hpp"
|
2014-05-11 17:14:35 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <set>
|
2014-03-18 11:44:09 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
ApplyRule::RuleMap ApplyRule::m_Rules;
|
|
|
|
ApplyRule::CallbackMap ApplyRule::m_Callbacks;
|
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
ApplyRule::ApplyRule(const String& targetType, const String& name, const AExpression::Ptr& expression,
|
2014-03-28 13:25:40 +01:00
|
|
|
const AExpression::Ptr& filter, const DebugInfo& di, const Dictionary::Ptr& scope)
|
2014-04-05 12:56:56 +02:00
|
|
|
: m_TargetType(targetType), m_Name(name), m_Expression(expression), m_Filter(filter), m_DebugInfo(di), m_Scope(scope)
|
2014-03-18 11:44:09 +01:00
|
|
|
{ }
|
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
String ApplyRule::GetTargetType(void) const
|
|
|
|
{
|
|
|
|
return m_TargetType;
|
|
|
|
}
|
|
|
|
|
2014-03-28 13:25:40 +01:00
|
|
|
String ApplyRule::GetName(void) const
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-03-28 13:25:40 +01:00
|
|
|
return m_Name;
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AExpression::Ptr ApplyRule::GetExpression(void) const
|
|
|
|
{
|
|
|
|
return m_Expression;
|
|
|
|
}
|
|
|
|
|
2014-03-28 13:25:40 +01:00
|
|
|
AExpression::Ptr ApplyRule::GetFilter(void) const
|
|
|
|
{
|
|
|
|
return m_Filter;
|
|
|
|
}
|
|
|
|
|
2014-03-18 11:44:09 +01:00
|
|
|
DebugInfo ApplyRule::GetDebugInfo(void) const
|
|
|
|
{
|
|
|
|
return m_DebugInfo;
|
|
|
|
}
|
|
|
|
|
2014-03-24 11:23:47 +01:00
|
|
|
Dictionary::Ptr ApplyRule::GetScope(void) const
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-03-24 11:23:47 +01:00
|
|
|
return m_Scope;
|
|
|
|
}
|
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
void ApplyRule::AddRule(const String& sourceType, const String& targetType, const String& name,
|
2014-03-28 13:25:40 +01:00
|
|
|
const AExpression::Ptr& expression, const AExpression::Ptr& filter,
|
|
|
|
const DebugInfo& di, const Dictionary::Ptr& scope)
|
|
|
|
{
|
2014-04-05 12:56:56 +02:00
|
|
|
m_Rules[sourceType].push_back(ApplyRule(targetType, name, expression, filter, di, scope));
|
2014-03-28 13:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ApplyRule::EvaluateFilter(const Dictionary::Ptr& scope) const
|
2014-03-24 11:23:47 +01:00
|
|
|
{
|
2014-03-30 15:04:53 +02:00
|
|
|
scope->Set("__parent", m_Scope);
|
|
|
|
bool result = m_Filter->Evaluate(scope);
|
|
|
|
scope->Remove("__parent");
|
|
|
|
return result;
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 00:23:29 +02:00
|
|
|
void ApplyRule::EvaluateRules(bool clear)
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-03-29 23:18:31 +01:00
|
|
|
std::set<String> completedTypes;
|
2014-03-18 11:44:09 +01:00
|
|
|
|
2014-03-29 23:18:31 +01:00
|
|
|
while (completedTypes.size() < m_Callbacks.size()) {
|
2014-04-05 12:56:56 +02:00
|
|
|
std::pair<String, std::pair<Callback, std::vector<String> > > kv;
|
2014-03-29 23:18:31 +01:00
|
|
|
BOOST_FOREACH(kv, m_Callbacks) {
|
|
|
|
const String& sourceType = kv.first;
|
2014-03-18 11:44:09 +01:00
|
|
|
|
2014-03-29 23:18:31 +01:00
|
|
|
if (completedTypes.find(sourceType) != completedTypes.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const Callback& callback = kv.second.first;
|
2014-04-05 12:56:56 +02:00
|
|
|
const std::vector<String>& targetTypes = kv.second.second;
|
2014-03-29 23:18:31 +01:00
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
bool cont = false;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const String& targetType, targetTypes) {
|
|
|
|
if (IsValidSourceType(targetType) && completedTypes.find(targetType) == completedTypes.end()) {
|
|
|
|
cont = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-29 23:18:31 +01:00
|
|
|
|
2014-04-07 13:23:27 +02:00
|
|
|
if (cont)
|
|
|
|
continue;
|
|
|
|
|
2014-03-29 23:18:31 +01:00
|
|
|
completedTypes.insert(sourceType);
|
|
|
|
|
|
|
|
RuleMap::const_iterator it = m_Rules.find(kv.first);
|
|
|
|
|
|
|
|
if (it == m_Rules.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
callback(it->second);
|
|
|
|
}
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
2014-03-29 23:18:31 +01:00
|
|
|
|
2014-05-02 00:23:29 +02:00
|
|
|
if (clear)
|
|
|
|
m_Rules.clear();
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
void ApplyRule::RegisterType(const String& sourceType, const std::vector<String>& targetTypes, const ApplyRule::Callback& callback)
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-04-05 12:56:56 +02:00
|
|
|
m_Callbacks[sourceType] = make_pair(callback, targetTypes);
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
bool ApplyRule::IsValidSourceType(const String& sourceType)
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-03-28 13:25:40 +01:00
|
|
|
return m_Callbacks.find(sourceType) != m_Callbacks.end();
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
2014-04-05 12:56:56 +02:00
|
|
|
|
|
|
|
bool ApplyRule::IsValidTargetType(const String& sourceType, const String& targetType)
|
|
|
|
{
|
|
|
|
CallbackMap::const_iterator it = m_Callbacks.find(sourceType);
|
|
|
|
|
|
|
|
if (it == m_Callbacks.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (it->second.second.size() == 1 && targetType == "")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const String& type, it->second.second) {
|
|
|
|
if (type == targetType)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<String> ApplyRule::GetTargetTypes(const String& sourceType)
|
|
|
|
{
|
|
|
|
CallbackMap::const_iterator it = m_Callbacks.find(sourceType);
|
|
|
|
|
|
|
|
if (it == m_Callbacks.end())
|
|
|
|
return std::vector<String>();
|
|
|
|
|
|
|
|
return it->second.second;
|
|
|
|
}
|
|
|
|
|