1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2 
3 #include "config/applyrule.hpp"
4 #include "base/logger.hpp"
5 #include <set>
6 
7 using namespace icinga;
8 
9 ApplyRule::RuleMap ApplyRule::m_Rules;
10 ApplyRule::TypeMap ApplyRule::m_Types;
11 
ApplyRule(String targetType,String name,Expression::Ptr expression,Expression::Ptr filter,String package,String fkvar,String fvvar,Expression::Ptr fterm,bool ignoreOnError,DebugInfo di,Dictionary::Ptr scope)12 ApplyRule::ApplyRule(String targetType, String name, Expression::Ptr expression,
13 	Expression::Ptr filter, String package, String fkvar, String fvvar, Expression::Ptr fterm,
14 	bool ignoreOnError, DebugInfo di, Dictionary::Ptr scope)
15 	: m_TargetType(std::move(targetType)), m_Name(std::move(name)), m_Expression(std::move(expression)), m_Filter(std::move(filter)), m_Package(std::move(package)), m_FKVar(std::move(fkvar)),
16 	m_FVVar(std::move(fvvar)), m_FTerm(std::move(fterm)), m_IgnoreOnError(ignoreOnError), m_DebugInfo(std::move(di)), m_Scope(std::move(scope)), m_HasMatches(false)
17 { }
18 
GetTargetType() const19 String ApplyRule::GetTargetType() const
20 {
21 	return m_TargetType;
22 }
23 
GetName() const24 String ApplyRule::GetName() const
25 {
26 	return m_Name;
27 }
28 
GetExpression() const29 Expression::Ptr ApplyRule::GetExpression() const
30 {
31 	return m_Expression;
32 }
33 
GetFilter() const34 Expression::Ptr ApplyRule::GetFilter() const
35 {
36 	return m_Filter;
37 }
38 
GetPackage() const39 String ApplyRule::GetPackage() const
40 {
41 	return m_Package;
42 }
43 
GetFKVar() const44 String ApplyRule::GetFKVar() const
45 {
46 	return m_FKVar;
47 }
48 
GetFVVar() const49 String ApplyRule::GetFVVar() const
50 {
51 	return m_FVVar;
52 }
53 
GetFTerm() const54 Expression::Ptr ApplyRule::GetFTerm() const
55 {
56 	return m_FTerm;
57 }
58 
GetIgnoreOnError() const59 bool ApplyRule::GetIgnoreOnError() const
60 {
61 	return m_IgnoreOnError;
62 }
63 
GetDebugInfo() const64 DebugInfo ApplyRule::GetDebugInfo() const
65 {
66 	return m_DebugInfo;
67 }
68 
GetScope() const69 Dictionary::Ptr ApplyRule::GetScope() const
70 {
71 	return m_Scope;
72 }
73 
AddRule(const String & sourceType,const String & targetType,const String & name,const Expression::Ptr & expression,const Expression::Ptr & filter,const String & package,const String & fkvar,const String & fvvar,const Expression::Ptr & fterm,bool ignoreOnError,const DebugInfo & di,const Dictionary::Ptr & scope)74 void ApplyRule::AddRule(const String& sourceType, const String& targetType, const String& name,
75 	const Expression::Ptr& expression, const Expression::Ptr& filter, const String& package, const String& fkvar,
76 	const String& fvvar, const Expression::Ptr& fterm, bool ignoreOnError, const DebugInfo& di, const Dictionary::Ptr& scope)
77 {
78 	m_Rules[sourceType].push_back(ApplyRule(targetType, name, expression, filter, package, fkvar, fvvar, fterm, ignoreOnError, di, scope));
79 }
80 
EvaluateFilter(ScriptFrame & frame) const81 bool ApplyRule::EvaluateFilter(ScriptFrame& frame) const
82 {
83 	return Convert::ToBool(m_Filter->Evaluate(frame));
84 }
85 
RegisterType(const String & sourceType,const std::vector<String> & targetTypes)86 void ApplyRule::RegisterType(const String& sourceType, const std::vector<String>& targetTypes)
87 {
88 	m_Types[sourceType] = targetTypes;
89 }
90 
IsValidSourceType(const String & sourceType)91 bool ApplyRule::IsValidSourceType(const String& sourceType)
92 {
93 	return m_Types.find(sourceType) != m_Types.end();
94 }
95 
IsValidTargetType(const String & sourceType,const String & targetType)96 bool ApplyRule::IsValidTargetType(const String& sourceType, const String& targetType)
97 {
98 	auto it = m_Types.find(sourceType);
99 
100 	if (it == m_Types.end())
101 		return false;
102 
103 	if (it->second.size() == 1 && targetType == "")
104 		return true;
105 
106 	for (const String& type : it->second) {
107 		if (type == targetType)
108 			return true;
109 	}
110 
111 	return false;
112 }
113 
GetTargetTypes(const String & sourceType)114 std::vector<String> ApplyRule::GetTargetTypes(const String& sourceType)
115 {
116 	auto it = m_Types.find(sourceType);
117 
118 	if (it == m_Types.end())
119 		return std::vector<String>();
120 
121 	return it->second;
122 }
123 
AddMatch()124 void ApplyRule::AddMatch()
125 {
126 	m_HasMatches = true;
127 }
128 
HasMatches() const129 bool ApplyRule::HasMatches() const
130 {
131 	return m_HasMatches;
132 }
133 
GetRules(const String & type)134 std::vector<ApplyRule>& ApplyRule::GetRules(const String& type)
135 {
136 	auto it = m_Rules.find(type);
137 	if (it == m_Rules.end()) {
138 		static std::vector<ApplyRule> emptyList;
139 		return emptyList;
140 	}
141 	return it->second;
142 }
143 
CheckMatches(bool silent)144 void ApplyRule::CheckMatches(bool silent)
145 {
146 	for (const RuleMap::value_type& kv : m_Rules) {
147 		for (const ApplyRule& rule : kv.second) {
148 			if (!rule.HasMatches() && !silent)
149 				Log(LogWarning, "ApplyRule")
150 					<< "Apply rule '" << rule.GetName() << "' (" << rule.GetDebugInfo() << ") for type '" << kv.first << "' does not match anywhere!";
151 		}
152 	}
153 }
154