ifw-ccf 5.0.2
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
15
16
17namespace ifw::ccf::common {
18
22 class Setup: public Base {
23 public:
24
27
29 static Setup& Instance();
30
31
32 Setup();
33
34 ~Setup();
35
37 //elt::configng::CiiConfigInstanceNode& GetNode(const std::vector<std::string>& names);
38
40 elt::configng::CiiConfigInstanceNode& GetStagingNode(const std::vector<std::string>& names);
41
43 elt::configng::CiiConfigInstanceNode& GetNode(const elt::configng::CiiConfigDocument* doc,
44 const std::vector<std::string>& names);
45
47 const std::string& GetRootNodeName() const;
48
50 template <class TYPE>
51 bool HasCurrentPar(const std::vector<std::string>& names, TYPE& value) {
52 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
53 //auto tmp_names = CheckNodeNames(names);
54 try {
55 //auto tmp_node = GetNode(tmp_names);
56 //value = tmp_node.As<TYPE>();
57 value = GetCurrentValue<TYPE>(names);
58 return true;
59 } catch (...) {
60 return false;
61 }
62 }
63
66 template <class TYPE>
67 bool HasPar(const std::vector<std::string>& names, TYPE& value) {
68 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
69 auto tmp_names = CheckNodeNames(names);
70 if (HasStagingPar(tmp_names, value)) {
71 return true;
72 } else {
73 try {
74 // auto tmp_node = GetNode(tmp_names);
75 // value = tmp_node.As<TYPE>();
76 value = GetCurrentValue<TYPE>(tmp_names);
77 return true;
78 } catch (...) {
79 return false;
80 }
81 }
82 }
83
86 bool ParDefined(const std::string& name);
87
90 template <class TYPE>
91 bool HasStagingPar(const std::vector<std::string>& names, TYPE& value) {
92 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
93 auto tmp_names = CheckNodeNames(names);
94
95 // Try first in the Staging Par map:
96 std::lock_guard<std::recursive_mutex> lock(m_staging_setup_mtx);
97 auto tmp_name = BuildKey(tmp_names);
98 auto it = s_staging_setup_map.find(tmp_name);
99 if (it != s_staging_setup_map.end()) {
100 ifw::core::utils::conversion::Convert(it->second, value);
101 return true;
102 }
103 // - then in the CII Cfg Service staging pars doc:
104 if (m_staging_setup_doc.get() == nullptr) {
105 return false;
106 }
107 try {
108 if (m_staging_setup_doc.get() == nullptr) {
109 return false;
110 }
111 auto tmp_node = GetStagingNode(tmp_names);
112 value = tmp_node.As<TYPE>();
113 return true;
114 } catch (...) {
115 return false;
116 }
117 }
118
121 template <class TYPE>
122 bool HasStagingParKey(const std::string& name, TYPE& value) {
123 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
124 auto tmp_name = CheckNodeName(name);
125 std::lock_guard<std::recursive_mutex> lock(m_staging_setup_mtx);
126 auto it = s_staging_setup_map.find(tmp_name);
127 if (it != s_staging_setup_map.end()) {
128 ifw::core::utils::conversion::Convert(it->second, value);
129 return true;
130 } else {
131 try {
132 if (m_staging_setup_doc.get() == nullptr) {
133 return false;
134 }
135 auto tmp_node = GetStagingNode(SplitKey(tmp_name));
136 value = tmp_node.As<TYPE>();
137 return true;
138 } catch (...) {
139 return false;
140 }
141 return false;
142 }
143 }
144
146 void ResetStagingSetup();
147
150 template <class TYPE>
151 void AddStagingPar(const std::string& name, const TYPE value) {
152 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
153 std::string tmp_name = CheckNodeName(name);
154 LOG4CPLUS_INFO(Logger(), fmt::format("{}: Handling Staging Setup Parameter: |{}|=|{}|...",
155 IFWLOC, tmp_name, value));
156 // Check if parameter is defined in the 'reference setup document'.
157 if (m_ref_setup_doc.get() == nullptr) {
158 CCFTHROW("Must load an Init Setup as reference before submitting setup parameters");
159 }
160 // 1: Check if keyword is defined in the reference setup.
161 try {
162 GetNode(m_ref_setup_doc.get(), SplitKey(name));
163 } catch (...) {
164 auto err = boost::current_exception_diagnostic_information();
165 CCFTHROW(fmt::format("{}: Issue handling parameter: |{}|: {}", IFWLOC, name, err));
166 }
167 // 2: Check if keyword value is valid according to the schema.
168 // TODO: This is a workaround. the ngconfig document class does not provide a
169 // "CheckKeyword()", which could be used for this; see also
170 std::stringstream tmp_val;
171 tmp_val << value;
172 auto node = GetNode(m_ref_setup_doc.get(), SplitKey(name));
173 node = tmp_val.str();
174 m_ref_setup_doc.get()->Check();
175 auto issues = m_ref_setup_doc.get()->Check();
176 if (issues) {
177 // Reload the Reference Staging Setup to be ready for the next check to be performed.
178 m_ref_setup_doc = std::make_unique<elt::configng::CiiConfigDocument>
179 (elt::configng::CiiConfigClient::Load(m_staging_setup_file));
180 for (auto issue : issues) {
181 auto err = fmt::format("Value: {} provided with parameter: {} not valid",
182 tmp_val.str(), name);
183 //, issue.GetMessageWithOrigin());
184 LOG4CPLUS_WARN(Logger(), err);
185 CCFTHROW(err);
186 }
187 }
188
189 {
190 std::lock_guard<std::recursive_mutex> lock(m_staging_setup_mtx);
191 StoreStagingPar(tmp_name, tmp_val.str());
192 }
193 LOG4CPLUS_INFO(Logger(), fmt::format("Added Staging Setup Parameter: |{}|=|{}|", tmp_name,
194 tmp_val.str()));
195 }
196
198 std::vector<std::string> GetSetupCmdStagingPars();
199
201 void GetPars(std::map<std::string, std::string>& pars,
202 const std::string& pattern = "*");
203
204 template <class TYPE>
205 TYPE GetCurrentValue(const std::string& name) {
206 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
207 std::lock_guard<std::recursive_mutex> lock(m_current_setup_mtx);
208 auto it = m_current_setup_map.find(name);
209 if (it != m_current_setup_map.end()) {
210 TYPE tmp_value;
211 ifw::core::utils::conversion::Convert(it->second, tmp_value);
212 return tmp_value;
213 } else {
214 CCFTHROW(fmt::format("{}: Parameter: |{}| not defined among Current Setup Parameters",
215 IFWLOC, name));
216 }
217 }
218
220 template <class TYPE>
221 TYPE GetCurrentValue(const std::vector<std::string>& names) {
222 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
223 return GetCurrentValue<TYPE>(BuildKey(CheckNodeNames(names)));
224 }
225
227 template <class TYPE>
228 TYPE GetValue(const std::vector<std::string>& names) {
229 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
230 auto tmp_names = CheckNodeNames(names);
231 TYPE tmp_value;
232 if (HasPar(tmp_names, tmp_value)) {
233 return tmp_value;
234 } else {
235 CCFTHROW("Setup parameter requested not defined: " + BuildKey(tmp_names));
236 }
237 }
238
240 template <class TYPE>
241 TYPE GetValueKey(const std::string& name) {
242 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
243 auto tmp_name = CheckNodeName(name);
244 TYPE tmp_value;
245 if (HasPar(SplitKey(tmp_name), tmp_value)) {
246 return tmp_value;
247 } else {
248 CCFTHROW("Setup parameter requested not defined: " + tmp_name);
249 }
250 }
251
253 void LoadStagingSetup(const std::string& filename);
254
256 void AcceptStagingSetup();
257
259 std::string StagingSetupToString() const;
260
262 std::string SetupToString() const;
263
264 std::vector<std::string> CheckNodeNames(const std::vector<std::string>& names) const;
265 std::string CheckNodeName(const std::string& name) const;
266
267 const elt::configng::CiiConfigDocument& GetRefSetup() const;
268
269 protected:
270 void StoreStagingPar(const std::string& name, const std::string& value);
271
272 private:
273 std::string m_root_node_name;
274 std::string m_staging_setup_file;
275
276 // Reference setup document, used as a 'dictionary' to check parameters submitted
277 // in SPF format.
278 std::unique_ptr<elt::configng::CiiConfigDocument> m_ref_setup_doc;
279
280 // The Staging Setup contains the parameters during the handling.
281
282 // => Used when the Staging Setup is loaded from a file. <=
283 // Note: Not really needed as the parameters from a setup file are loaded into the
284 // "s_staging_setup_map".
285 std::unique_ptr<elt::configng::CiiConfigDocument> m_staging_setup_doc;
286
287 // => Used when Staging Setup Parameters provided in SPF via the Setup command. <=
288 // Keys are represented as SPF ("<key el1>.<key el2>....") in the map.
289 // TODO: In principle it should not be necessary to declare this map as static, but if it is
290 // a normal member it doesn't work...
291 std::recursive_mutex m_staging_setup_mtx;
292 static std::map<std::string, std::string> s_staging_setup_map;
293 // Used to preserve the order in which the Staging Parameters were added in the object.
294 std::vector<std::string> m_staging_setup_sequence;
295
296 // The Setup object contains the current setup, accepted and installed (in use).
297 //std::unique_ptr<elt::configng::CiiConfigDocument> m_setup_doc;
298 std::recursive_mutex m_current_setup_mtx;
299 std::map<std::string, std::string> m_current_setup_map;
300 };
301
302}
303
304namespace ifw::ccf {
309}
310
311#endif // CCF_COMMON_SETUP_HPP_H_
#define CCFTHROW(msg)
Definition base.hpp:366
Class to be used as parent all CCF classes.
Definition base.hpp:120
Global setup class for CCF applications. An Initialisation Setup, containing all the parameters shall...
Definition setup.hpp:22
TYPE GetValue(const std::vector< std::string > &names)
Get value from either the 1) Staging Setup, or 2) Installed Setup.
Definition setup.hpp:228
static Setup & Instance()
Return reference to unique instance of the application class.
Definition setup.cpp:13
bool HasCurrentPar(const std::vector< std::string > &names, TYPE &value)
Probe if a given parameter has been processed and accepted already.
Definition setup.hpp:51
elt::configng::CiiConfigInstanceNode & GetNode(const elt::configng::CiiConfigDocument *doc, const std::vector< std::string > &names)
Get the reference to a specific node in the referenced CII Cfg Service doc object.
Definition setup.cpp:60
std::string CheckNodeName(const std::string &name) const
Definition setup.cpp:268
void StoreStagingPar(const std::string &name, const std::string &value)
Definition setup.cpp:158
elt::configng::CiiConfigInstanceNode & GetStagingNode(const std::vector< std::string > &names)
Get the reference to a specific node in the Installed Setup.
Definition setup.cpp:55
bool ParDefined(const std::string &name)
Check if the given parameter is defined in the Reference Setup.
Definition setup.cpp:277
Setup()
Definition setup.cpp:22
bool HasStagingPar(const std::vector< std::string > &names, TYPE &value)
Definition setup.hpp:91
void AddStagingPar(const std::string &name, const TYPE value)
Definition setup.hpp:151
void LoadStagingSetup(const std::string &filename)
Load a Setup File into the Staging Setup object.
Definition setup.cpp:89
std::vector< std::string > GetSetupCmdStagingPars()
Return list of Staging Setup Pars, normally provided via a Setup Command.
Definition setup.cpp:220
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:241
TYPE GetCurrentValue(const std::string &name)
Definition setup.hpp:205
TYPE GetCurrentValue(const std::vector< std::string > &names)
Get value from the Installed Setup.
Definition setup.hpp:221
const elt::configng::CiiConfigDocument & GetRefSetup() const
Definition setup.cpp:317
void ResetStagingSetup()
Reset the internal Staging Setup.
Definition setup.cpp:81
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:190
bool HasStagingParKey(const std::string &name, TYPE &value)
Definition setup.hpp:122
bool HasPar(const std::vector< std::string > &names, TYPE &value)
Definition setup.hpp:67
std::string SetupToString() const
Return string representation of the Setup loaded.
Definition setup.cpp:298
void AcceptStagingSetup()
Merge the parameters in the Staging Setup into the Installed Setup set of parameters.
Definition setup.cpp:227
const std::string & GetRootNodeName() const
Return the name of the setup document root node name.
Definition setup.cpp:312
std::vector< std::string > CheckNodeNames(const std::vector< std::string > &names) const
Definition setup.cpp:254
std::string StagingSetupToString() const
Return string representation of the Staging Setup loaded.
Definition setup.cpp:289
~Setup()
Definition setup.cpp:28
static Setup * s_instance
Singleton instance.
Definition setup.hpp:26
Definition appBase.cpp:9
Definition appBase.cpp:9
log4cplus::Logger & Logger()
Definition base.cpp:23
ifw::ccf::common::Setup & GetSetup()
Return the reference to the CCF DB Singleton instance.
Definition setup.hpp:306
std::vector< std::string > SplitKey(const std::string &key)
Split up a concatenated key.
Definition base.cpp:244
std::string BuildKey(const std::vector< std::string > &elements)
Build a concatenated key from a number of elements (<el1>.<el2.>...).
Definition base.cpp:240