]> granicus.if.org Git - icinga2/blob - lib/config/objectrule.cpp
Merge branch 'fix/apply-req-group-assign-6105' into next
[icinga2] / lib / config / objectrule.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "config/objectrule.h"
21 #include "base/logger_fwd.h"
22
23 using namespace icinga;
24
25 ObjectRule::RuleMap ObjectRule::m_Rules;
26 ObjectRule::CallbackMap ObjectRule::m_Callbacks;
27
28 ObjectRule::ObjectRule(const String& name, const AExpression::Ptr& expression,
29     const AExpression::Ptr& filter, const DebugInfo& di, const Dictionary::Ptr& scope)
30         : m_Name(name), m_Expression(expression), m_Filter(filter), m_DebugInfo(di), m_Scope(scope)
31 { }
32
33 String ObjectRule::GetName(void) const
34 {
35         return m_Name;
36 }
37
38 AExpression::Ptr ObjectRule::GetExpression(void) const
39 {
40         return m_Expression;
41 }
42
43 AExpression::Ptr ObjectRule::GetFilter(void) const
44 {
45         return m_Filter;
46 }
47
48 DebugInfo ObjectRule::GetDebugInfo(void) const
49 {
50         return m_DebugInfo;
51 }
52
53 Dictionary::Ptr ObjectRule::GetScope(void) const
54 {
55         return m_Scope;
56 }
57
58 void ObjectRule::AddRule(const String& sourceType, const String& name,
59     const AExpression::Ptr& expression, const AExpression::Ptr& filter,
60     const DebugInfo& di, const Dictionary::Ptr& scope)
61 {
62         m_Rules[sourceType].push_back(ObjectRule(name, expression, filter, di, scope));
63 }
64
65 bool ObjectRule::EvaluateFilter(const Dictionary::Ptr& scope) const
66 {
67         scope->Set("__parent", m_Scope);
68         bool result = m_Filter->Evaluate(scope);
69         scope->Remove("__parent");
70         return result;
71 }
72
73 void ObjectRule::EvaluateRules(bool clear)
74 {
75         std::pair<String, Callback> kv;
76         BOOST_FOREACH(kv, m_Callbacks) {
77                 const Callback& callback = kv.second;
78
79                 RuleMap::const_iterator it = m_Rules.find(kv.first);
80
81                 if (it == m_Rules.end())
82                         continue;
83
84                 callback(it->second);
85         }
86
87         if (clear)
88                 m_Rules.clear();
89 }
90
91 void ObjectRule::RegisterType(const String& sourceType, const ObjectRule::Callback& callback)
92 {
93         m_Callbacks[sourceType] = callback;
94 }
95
96 bool ObjectRule::IsValidSourceType(const String& sourceType)
97 {
98         return m_Callbacks.find(sourceType) != m_Callbacks.end();
99 }
100