2014-04-23 12:44:36 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-04-23 15:57:58 +02:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2014-04-23 12:44:36 +02: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/objectrule.hpp"
|
2014-05-11 17:14:35 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <set>
|
2014-04-23 12:44:36 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
ObjectRule::RuleMap ObjectRule::m_Rules;
|
|
|
|
ObjectRule::CallbackMap ObjectRule::m_Callbacks;
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
ObjectRule::ObjectRule(const String& name, const boost::shared_ptr<Expression>& filter,
|
2014-11-10 09:52:27 +01:00
|
|
|
const DebugInfo& di, const Object::Ptr& scope)
|
|
|
|
: m_Name(name), m_Filter(filter), m_DebugInfo(di), m_Scope(scope)
|
2014-04-23 12:44:36 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
String ObjectRule::GetName(void) const
|
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
boost::shared_ptr<Expression> ObjectRule::GetFilter(void) const
|
2014-04-23 12:44:36 +02:00
|
|
|
{
|
|
|
|
return m_Filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugInfo ObjectRule::GetDebugInfo(void) const
|
|
|
|
{
|
|
|
|
return m_DebugInfo;
|
|
|
|
}
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
Object::Ptr ObjectRule::GetScope(void) const
|
2014-04-23 12:44:36 +02:00
|
|
|
{
|
|
|
|
return m_Scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectRule::AddRule(const String& sourceType, const String& name,
|
2014-11-09 19:48:28 +01:00
|
|
|
const boost::shared_ptr<Expression>& filter, const DebugInfo& di, const Object::Ptr& scope)
|
2014-04-23 12:44:36 +02:00
|
|
|
{
|
2014-11-10 09:52:27 +01:00
|
|
|
m_Rules[sourceType].push_back(ObjectRule(name, filter, di, scope));
|
2014-04-23 12:44:36 +02:00
|
|
|
}
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
bool ObjectRule::EvaluateFilter(const Object::Ptr& scope) const
|
2014-04-23 12:44:36 +02:00
|
|
|
{
|
2014-11-10 12:05:45 +01:00
|
|
|
return m_Filter->Evaluate(scope).ToBool();
|
2014-04-23 12:44:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 00:23:29 +02:00
|
|
|
void ObjectRule::EvaluateRules(bool clear)
|
2014-04-23 12:44:36 +02:00
|
|
|
{
|
|
|
|
std::pair<String, Callback> kv;
|
|
|
|
BOOST_FOREACH(kv, m_Callbacks) {
|
|
|
|
const Callback& callback = kv.second;
|
|
|
|
|
|
|
|
RuleMap::const_iterator it = m_Rules.find(kv.first);
|
|
|
|
|
|
|
|
if (it == m_Rules.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
callback(it->second);
|
|
|
|
}
|
|
|
|
|
2014-05-02 00:23:29 +02:00
|
|
|
if (clear)
|
|
|
|
m_Rules.clear();
|
2014-04-23 12:44:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectRule::RegisterType(const String& sourceType, const ObjectRule::Callback& callback)
|
|
|
|
{
|
|
|
|
m_Callbacks[sourceType] = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjectRule::IsValidSourceType(const String& sourceType)
|
|
|
|
{
|
|
|
|
return m_Callbacks.find(sourceType) != m_Callbacks.end();
|
|
|
|
}
|
|
|
|
|