ifw-ccf 4.0.0
Loading...
Searching...
No Matches
setup.hpp
Go to the documentation of this file.
1
5#ifndef CCF_COMMON_SETUP_HPP_H_
6#define CCF_COMMON_SETUP_HPP_H_
7
8#include <boost/exception/diagnostic_information.hpp>
9
10#include <yaml-cpp/yaml.h>
11
12#include <config-ng/ciiConfigApi.hpp>
13
14#include <ccf/common/base.hpp>
15
16// TODO: Check correct usage of const in all methods.
17
18namespace ccf::common {
19
23 class Setup: public Base {
24 public:
25
28
30 static Setup& Instance();
31
32
33 Setup();
34
35 ~Setup();
36
38 elt::configng::CiiConfigInstanceNode& GetNode(const std::vector<std::string>& names);
39
41 elt::configng::CiiConfigInstanceNode& GetStagingNode(const std::vector<std::string>& names);
42
44 elt::configng::CiiConfigInstanceNode& GetNode(const elt::configng::CiiConfigDocument* doc,
45 const std::vector<std::string>& names);
46
48 const std::string& GetRootNodeName() const;
49
52 template <class TYPE>
53 bool HasPar(const std::vector<std::string>& names, TYPE& value) {
54 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
55 auto tmp_names = CheckNodeNames(names);
56 if (HasStagingPar(tmp_names, value)) {
57 return true;
58 } else {
59 try {
60 auto tmp_node = GetNode(tmp_names);
61 value = tmp_node.As<TYPE>();
62 return true;
63 } catch (...) {
64 return false;
65 }
66 }
67 }
68
71 bool ParDefined(const std::string& name);
72
75 template <class TYPE>
76 bool HasStagingPar(const std::vector<std::string>& names, TYPE& value) {
77 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
78 auto tmp_names = CheckNodeNames(names);
79
80 // Try first in the Staging Par map:
81 auto tmp_name = BuildKey(tmp_names);
82 auto it = s_staging_setup_map.find(tmp_name);
83 if (it != s_staging_setup_map.end()) {
84 core::utils::conversion::Convert(it->second, value);
85 return true;
86 }
87 // - then in the CII Cfg Service doc:
88 try {
89 auto tmp_node = GetStagingNode(tmp_names);
90 value = tmp_node.As<TYPE>();
91 return true;
92 } catch (...) {
93 return false;
94 }
95 }
96
98 bool HasStagingNodeKey(const std::string& name);
99
102 template <class TYPE>
103 bool HasStagingParKey(const std::string& name, TYPE& value) {
104 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
105 auto tmp_name = CheckNodeName(name);
106 auto it = s_staging_setup_map.find(tmp_name);
107 if (it != s_staging_setup_map.end()) {
108 core::utils::conversion::Convert(it->second, value);
109 return true;
110 } else {
111 try {
112 auto tmp_node = GetStagingNode(SplitKey(tmp_name));
113 value = tmp_node.As<TYPE>();
114 return true;
115 } catch (...) {
116 return false;
117 }
118 return false;
119 }
120 }
121
123 void ResetStagingSetup();
124
127 template <class TYPE>
128 void AddStagingPar(const std::string& name, const TYPE value) {
129 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
130 std::string tmp_name = CheckNodeName(name);
131 LOG4CPLUS_INFO(Logger(), fmt::format("{}: Handling Staging Setup Parameter: |{}|=|{}|...",
132 CCFLOC, tmp_name, value));
133 // Check if parameter is defined in the 'reference setup document'.
134 if (m_ref_setup_doc.get() == nullptr) {
135 CCFTHROW("Must load an Init Setup as reference before submitting setup parameters");
136 }
137 // 1: Check if keyword is defined in the reference setup.
138 try {
139 GetNode(m_ref_setup_doc.get(), SplitKey(name));
140 } catch (...) {
141 auto err = boost::current_exception_diagnostic_information();
142 CCFTHROW(fmt::format("{}: Issue handling parameter: |{}|: {}", CCFLOC, name, err));
143 }
144 // 2: Check if keyword value is valid according to the schema.
145 // TODO: This is a workaround. the ngconfig document class does not provide a
146 // "CheckKeyword()", which could be used for this; see also
147 std::stringstream tmp_val;
148 tmp_val << value;
149 auto node = GetNode(m_ref_setup_doc.get(), SplitKey(name));
150 node = tmp_val.str();
151 m_ref_setup_doc.get()->Check();
152 auto issues = m_ref_setup_doc.get()->Check();
153 if (issues) {
154 // Reload the Reference Staging Setup to be ready for the next check to be performed.
155 m_ref_setup_doc = std::make_unique<elt::configng::CiiConfigDocument>
156 (elt::configng::CiiConfigClient::Load(m_staging_setup_file));
157 for (auto issue : issues) {
158 auto err = fmt::format("Value: {} provided with parameter: {} not valid",
159 tmp_val.str(), name);
160 //, issue.GetMessageWithOrigin());
161 LOG4CPLUS_WARN(Logger(), err);
162 CCFTHROW(err);
163 }
164 }
165
166 s_staging_setup_map[tmp_name] = tmp_val.str();
167 LOG4CPLUS_INFO(Logger(), fmt::format("Added Staging Setup Parameter: |{}|=|{}|", tmp_name,
168 tmp_val.str()));
169 }
170
172 const std::map<std::string, std::string>& GetSetupCmdStagingPars();
173
175 void GetPars(std::map<std::string, std::string>& pars,
176 const std::string& pattern = "*");
177
179 template <class TYPE>
180 TYPE GetValue(const std::vector<std::string>& names) {
181 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
182 auto tmp_names = CheckNodeNames(names);
183 TYPE tmp_value;
184 if (HasPar(tmp_names, tmp_value)) {
185 return tmp_value;
186 } else {
187 CCFTHROW("Setup parameter requested not defined: " + BuildKey(tmp_names));
188 }
189 }
190
192 template <class TYPE>
193 TYPE GetValueKey(const std::string& name) {
194 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
195 auto tmp_name = CheckNodeName(name);
196 TYPE tmp_value;
197 if (HasPar(SplitKey(tmp_name), tmp_value)) {
198 return tmp_value;
199 } else {
200 CCFTHROW("Setup parameter requested not defined: " + tmp_name);
201 }
202 }
203
205 void LoadStagingSetup(const std::string& filename);
206
208 void AcceptStagingSetup();
209
211 std::string SetupFileToString() const;
212
213 std::vector<std::string> CheckNodeNames(const std::vector<std::string>& names) const;
214 std::string CheckNodeName(const std::string& name) const;
215
216 const elt::configng::CiiConfigDocument& GetRefSetup() const;
217
218 protected:
219
220 private:
221 std::string m_root_node_name;
222 std::string m_staging_setup_file;
223
224 // Reference setup document, used as a 'dictionary' to check parameters submitted
225 // in SPF format.
226 std::unique_ptr<elt::configng::CiiConfigDocument> m_ref_setup_doc;
227
228 // The Staging Setup contains the parameters during the handling.
229
230 // => Used when the Staging Setup is loaded from a file. <=
231 // Note: Not really needed as the parameters from a setup file are loaded into the
232 // "s_staging_setup_map".
233 std::unique_ptr<elt::configng::CiiConfigDocument> m_staging_setup_doc;
234
235 // => Used when Staging Setup Parameters provided in SPF via the Setup command. <=
236 // Keys are represented as SPF ("<key el1>.<key el2>....") in the map.
237 // TODO: In principle it should not be necessary to declare this map as static, but if it is
238 // a normal member it doesn't work...
239 static std::map<std::string, std::string> s_staging_setup_map;
240
241 // The Setup object contains the current setup, accepted and installed (in use).
242 std::unique_ptr<elt::configng::CiiConfigDocument> m_setup_doc;
243 };
244
245}
246
247namespace ccf {
251 }
252}
253
254#endif // CCF_COMMON_SETUP_HPP_H_
#define CCFLOC
Macro generating a location identifier: "<file>:<line>:<function>:<thread>".
Definition: base.hpp:387
#define CCFTHROW(msg)
Definition: base.hpp:416
Class to be used as parent all CCF classes.
Definition: base.hpp:107
Global setup class for CCF applications. An Initialisation Setup, containing all the parameters shall...
Definition: setup.hpp:23
bool ParDefined(const std::string &name)
Check if the given parameter is defined in the Reference Setup.
Definition: setup.cpp:200
std::vector< std::string > CheckNodeNames(const std::vector< std::string > &names) const
Definition: setup.cpp:177
const std::map< std::string, std::string > & GetSetupCmdStagingPars()
Return list of staging pars, provided via the Setup command.
Definition: setup.cpp:152
bool HasStagingParKey(const std::string &name, TYPE &value)
Definition: setup.hpp:103
bool HasStagingNodeKey(const std::string &name)
Probe if a given parameter is defined among the Setup Staging Parameters.
Definition: setup.cpp:212
std::string CheckNodeName(const std::string &name) const
Definition: setup.cpp:191
bool HasPar(const std::vector< std::string > &names, TYPE &value)
Definition: setup.hpp:53
static Setup * s_instance
Singleton instance.
Definition: setup.hpp:27
static Setup & Instance()
Return reference to unique instance of the application class.
Definition: setup.cpp:13
TYPE GetValueKey(const std::string &name)
Get value from either the 1) Staging Setup, or 2) Installed Setup from the SPF key.
Definition: setup.hpp:193
void AcceptStagingSetup()
Merge the parameters in the Staging Setup into the Installed Setup set of parameters.
Definition: setup.cpp:157
void AddStagingPar(const std::string &name, const TYPE value)
Definition: setup.hpp:128
const elt::configng::CiiConfigDocument & GetRefSetup() const
Definition: setup.cpp:236
void ResetStagingSetup()
Reset the internal Staging Setup.
Definition: setup.cpp:62
TYPE GetValue(const std::vector< std::string > &names)
Get value from either the 1) Staging Setup, or 2) Installed Setup.
Definition: setup.hpp:180
std::string SetupFileToString() const
Return string representation of the setup file loaded.
Definition: setup.cpp:222
Setup()
Definition: setup.cpp:22
bool HasStagingPar(const std::vector< std::string > &names, TYPE &value)
Definition: setup.hpp:76
elt::configng::CiiConfigInstanceNode & GetStagingNode(const std::vector< std::string > &names)
Get the reference to a specific node in the Staging Setup.
Definition: setup.cpp:37
const std::string & GetRootNodeName() const
Return the name of the setup document root node name.
Definition: setup.cpp:231
elt::configng::CiiConfigInstanceNode & GetNode(const std::vector< std::string > &names)
Get the reference to a specific node in the Installed Setup.
Definition: setup.cpp:32
void GetPars(std::map< std::string, std::string > &pars, const std::string &pattern="*")
Return map with all parameters in the object. Staging pars take precedence.
Definition: setup.cpp:128
~Setup()
Definition: setup.cpp:28
void LoadStagingSetup(const std::string &filename)
Load a Setup File into the Staging Setup object.
Definition: setup.cpp:68
Definition: appBase.cpp:8
Definition: appBase.cpp:8
std::string BuildKey(const std::vector< std::string > &elements)
Build a concatenated key from a number of elements (<el1>.<el2.>...).
Definition: base.cpp:238
log4cplus::Logger & Logger()
Definition: base.cpp:11
std::vector< std::string > SplitKey(const std::string &key)
Split up a concatenated key.
Definition: base.cpp:242
ccf::common::Setup & GetSetup()
Return the reference to the CCF DB Singleton instance.
Definition: setup.hpp:249