1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2 
3 #ifndef CONFIGITEM_H
4 #define CONFIGITEM_H
5 
6 #include "config/i2-config.hpp"
7 #include "config/expression.hpp"
8 #include "config/activationcontext.hpp"
9 #include "base/configobject.hpp"
10 #include "base/workqueue.hpp"
11 
12 namespace icinga
13 {
14 
15 
16 /**
17  * A configuration item. Non-abstract configuration items can be used to
18  * create configuration objects at runtime.
19  *
20  * @ingroup config
21  */
22 class ConfigItem final : public Object {
23 public:
24 	DECLARE_PTR_TYPEDEFS(ConfigItem);
25 
26 	ConfigItem(Type::Ptr type, String name, bool abstract,
27 		Expression::Ptr exprl,
28 		Expression::Ptr filter,
29 		bool defaultTmpl, bool ignoreOnError, DebugInfo debuginfo,
30 		Dictionary::Ptr scope, String zone,
31 		String package);
32 
33 	Type::Ptr GetType() const;
34 	String GetName() const;
35 	bool IsAbstract() const;
36 	bool IsDefaultTemplate() const;
37 	bool IsIgnoreOnError() const;
38 
39 	std::vector<ConfigItem::Ptr> GetParents() const;
40 
41 	Expression::Ptr GetExpression() const;
42 	Expression::Ptr GetFilter() const;
43 
44 	void Register();
45 	void Unregister();
46 
47 	DebugInfo GetDebugInfo() const;
48 	Dictionary::Ptr GetScope() const;
49 
50 	ConfigObject::Ptr GetObject() const;
51 
52 	static ConfigItem::Ptr GetByTypeAndName(const Type::Ptr& type,
53 		const String& name);
54 
55 	static bool CommitItems(const ActivationContext::Ptr& context, WorkQueue& upq, std::vector<ConfigItem::Ptr>& newItems, bool silent = false);
56 	static bool ActivateItems(const std::vector<ConfigItem::Ptr>& newItems, bool runtimeCreated = false,
57 		bool mainConfigActivation = false, bool withModAttrs = false, const Value& cookie = Empty);
58 
59 	static bool RunWithActivationContext(const Function::Ptr& function);
60 
61 	static std::vector<ConfigItem::Ptr> GetItems(const Type::Ptr& type);
62 	static std::vector<ConfigItem::Ptr> GetDefaultTemplates(const Type::Ptr& type);
63 
64 	static void RemoveIgnoredItems(const String& allowedConfigPath);
65 
66 private:
67 	Type::Ptr m_Type; /**< The object type. */
68 	String m_Name; /**< The name. */
69 	bool m_Abstract; /**< Whether this is a template. */
70 
71 	Expression::Ptr m_Expression;
72 	Expression::Ptr m_Filter;
73 	bool m_DefaultTmpl;
74 	bool m_IgnoreOnError;
75 	DebugInfo m_DebugInfo; /**< Debug information. */
76 	Dictionary::Ptr m_Scope; /**< variable scope. */
77 	String m_Zone; /**< The zone. */
78 	String m_Package;
79 	ActivationContext::Ptr m_ActivationContext;
80 
81 	ConfigObject::Ptr m_Object;
82 
83 	static std::mutex m_Mutex;
84 
85 	typedef std::map<String, ConfigItem::Ptr> ItemMap;
86 	typedef std::map<Type::Ptr, ItemMap> TypeMap;
87 	static TypeMap m_Items; /**< All registered configuration items. */
88 	static TypeMap m_DefaultTemplates;
89 
90 	typedef std::vector<ConfigItem::Ptr> ItemList;
91 	static ItemList m_UnnamedItems;
92 
93 	typedef std::vector<String> IgnoredItemList;
94 	static IgnoredItemList m_IgnoredItems;
95 
96 	static ConfigItem::Ptr GetObjectUnlocked(const String& type,
97 		const String& name);
98 
99 	ConfigObject::Ptr Commit(bool discard = true);
100 
101 	static bool CommitNewItems(const ActivationContext::Ptr& context, WorkQueue& upq, std::vector<ConfigItem::Ptr>& newItems);
102 };
103 
104 }
105 
106 #endif /* CONFIGITEM_H */
107