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