This repository was archived by the owner on Dec 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathconfig.cpp
More file actions
1394 lines (1171 loc) · 59.8 KB
/
config.cpp
File metadata and controls
1394 lines (1171 loc) · 59.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest
#include <algorithm>
#include <cstddef>
#include <fstream>
#include <list>
#include <set>
#include <sstream>
#include <fmt/format.h>
#include <everest/exceptions.hpp>
#include <everest/logging.hpp>
#include <framework/runtime.hpp>
#include <utils/config.hpp>
#include <utils/formatter.hpp>
#include <utils/yaml_loader.hpp>
namespace Everest {
using json = nlohmann::json;
using json_uri = nlohmann::json_uri;
using json_validator = nlohmann::json_schema::json_validator;
class ConfigParseException : public std::exception {
public:
enum ParseErrorType {
MISSING_ENTRY,
SCHEMA
};
ConfigParseException(ParseErrorType err_t, const std::string& entry, const std::string& what = "") :
err_t(err_t), entry(entry), what(what){};
const ParseErrorType err_t;
const std::string entry;
const std::string what;
};
static json draft07 = R"(
{
"$ref": "http://json-schema.org/draft-07/schema#"
}
)"_json;
struct ParsedConfigMap {
json parsed_config_map{};
std::set<std::string> unknown_config_entries;
};
static void validate_config_schema(const json& config_map_schema) {
// iterate over every config entry
json_validator validator(Config::loader, Config::format_checker);
for (const auto& config_item : config_map_schema.items()) {
if (!config_item.value().contains("default")) {
continue;
}
try {
validator.set_root_schema(config_item.value());
validator.validate(config_item.value().at("default"));
} catch (const std::exception& e) {
throw std::runtime_error(fmt::format("Config item '{}' has issues:\n{}", config_item.key(), e.what()));
}
}
}
static ParsedConfigMap parse_config_map(const json& config_map_schema, const json& config_map) {
json parsed_config_map{};
std::set<std::string> unknown_config_entries;
const std::set<std::string> config_map_keys = Config::keys(config_map);
const std::set<std::string> config_map_schema_keys = Config::keys(config_map_schema);
std::set_difference(config_map_keys.begin(), config_map_keys.end(), config_map_schema_keys.begin(),
config_map_schema_keys.end(),
std::inserter(unknown_config_entries, unknown_config_entries.end()));
// validate each config entry
for (const auto& config_entry_el : config_map_schema.items()) {
const std::string& config_entry_name = config_entry_el.key();
const json& config_entry = config_entry_el.value();
// only convenience exception, would be catched by schema validation below if not thrown here
if (!config_entry.contains("default") and !config_map.contains(config_entry_name)) {
throw ConfigParseException(ConfigParseException::MISSING_ENTRY, config_entry_name);
}
json config_entry_value;
if (config_map.contains(config_entry_name)) {
config_entry_value = config_map[config_entry_name];
} else if (config_entry.contains("default")) {
config_entry_value = config_entry.at("default"); // use default value defined in manifest
}
json_validator validator(Config::loader, Config::format_checker);
validator.set_root_schema(config_entry);
try {
auto patch = validator.validate(config_entry_value);
if (!patch.is_null()) {
// extend config entry with default values
config_entry_value = config_entry_value.patch(patch);
}
} catch (const std::exception& err) {
throw ConfigParseException(ConfigParseException::SCHEMA, config_entry_name, err.what());
}
parsed_config_map[config_entry_name] = config_entry_value;
}
return {parsed_config_map, unknown_config_entries};
}
static auto get_provides_for_probe_module(const std::string& probe_module_id, const json& config,
const json& manifests) {
auto provides = json::object();
for (const auto& item : config.items()) {
if (item.key() == probe_module_id) {
// do not parse ourself
continue;
}
const auto& module_config = item.value();
const auto& connections = module_config.value("connections", json::object());
for (const auto& connection : connections.items()) {
const std::string& req_id = connection.key();
const std::string module_name = module_config.at("module");
const auto& module_manifest = manifests.at(module_name);
// FIXME (aw): in principle we would need to check here again, the listed connections are indeed specified
// in the modules manifest
const std::string requirement_interface = module_manifest.at("requires").at(req_id).at("interface");
for (const auto& req_item : connection.value().items()) {
const std::string impl_mod_id = req_item.value().at("module_id");
const std::string impl_id = req_item.value().at("implementation_id");
if (impl_mod_id != probe_module_id) {
continue;
}
if (provides.contains(impl_id) && (provides[impl_id].at("interface") != requirement_interface)) {
EVLOG_AND_THROW(EverestConfigError(
"ProbeModule can not fulfill multiple requirements for the same implementation id '" + impl_id +
"', but with different interfaces"));
} else {
provides[impl_id] = {{"interface", requirement_interface}, {"description", "none"}};
}
}
}
}
if (provides.empty()) {
provides["none"] = {{"interface", "empty"}, {"description", "none"}};
}
return provides;
}
static auto get_requirements_for_probe_module(const std::string& probe_module_id, const json& config,
const json& manifests) {
const auto& probe_module_config = config.at(probe_module_id);
auto requirements = json::object();
const auto connections_it = probe_module_config.find("connections");
if (connections_it == probe_module_config.end()) {
return requirements;
}
for (const auto& item : connections_it->items()) {
const auto& req_id = item.key();
for (const auto& ffs : item.value()) {
const std::string module_id = ffs.at("module_id");
const std::string impl_id = ffs.at("implementation_id");
const auto& module_config_it = config.find(module_id);
if (module_config_it == config.end()) {
EVLOG_AND_THROW(
EverestConfigError(fmt::format("ProbeModule refers to a non-existent module id '{}'", module_id)));
}
const auto& module_manifest = manifests.at(module_config_it->at("module"));
const auto& module_provides_it = module_manifest.find("provides");
if (module_provides_it == module_manifest.end()) {
EVLOG_AND_THROW(EverestConfigError(fmt::format(
"ProbeModule requires something from module id '{}' but it does not provide anything", module_id)));
}
const auto& provide_it = module_provides_it->find(impl_id);
if (provide_it == module_provides_it->end()) {
EVLOG_AND_THROW(EverestConfigError(
fmt::format("ProbeModule requires something from module id '{}', but it does not provide '{}'",
module_id, impl_id)));
}
const std::string interface = provide_it->at("interface");
if (requirements.contains(req_id) && (requirements[req_id].at("interface") != interface)) {
// FIXME (aw): we might need to adujst the min/max values here for possible implementations
EVLOG_AND_THROW(EverestConfigError("ProbeModule interface mismatch -- FIXME (aw)"));
} else {
requirements[req_id] = {{"interface", interface}};
}
}
}
return requirements;
}
static void setup_probe_module_manifest(const std::string& probe_module_id, const json& config, json& manifests) {
// setup basic information
auto& manifest = manifests["ProbeModule"];
manifest = {
{"description", "ProbeModule (generated)"},
{
"metadata",
{
{"license", "https://opensource.org/licenses/Apache-2.0"},
{"authors", {"everest"}},
},
},
};
manifest["provides"] = get_provides_for_probe_module(probe_module_id, config, manifests);
auto requirements = get_requirements_for_probe_module(probe_module_id, config, manifests);
if (not requirements.empty()) {
manifest["requires"] = requirements;
}
}
ImplementationInfo extract_implementation_info(const std::unordered_map<std::string, std::string>& module_names,
const json& manifests, const std::string& module_id,
const std::string& impl_id) {
BOOST_LOG_FUNCTION();
if (module_names.find(module_id) == module_names.end()) {
EVTHROW(EverestApiError(fmt::format("Module id '{}' not found in config!", module_id)));
}
ImplementationInfo info;
info.module_id = module_id;
info.module_name = module_names.at(module_id);
info.impl_id = impl_id;
if (!impl_id.empty()) {
if (not manifests.contains(info.module_name)) {
EVTHROW(EverestApiError(fmt::format("No known manifest for module name '{}'!", info.module_name)));
}
if (not manifests.at(info.module_name).at("provides").contains(impl_id)) {
EVTHROW(EverestApiError(fmt::format("Implementation id '{}' not defined in manifest of module '{}'!",
impl_id, info.module_name)));
}
info.impl_intf = manifests.at(info.module_name).at("provides").at(impl_id).at("interface");
}
return info;
}
std::string create_printable_identifier(const ImplementationInfo& info, const std::string& module_id,
const std::string& impl_id) {
BOOST_LOG_FUNCTION();
// no implementation id yet so only return this kind of string:
auto module_string = fmt::format("{}:{}", info.module_id, info.module_name);
if (impl_id.empty()) {
return module_string;
}
return fmt::format("{}->{}:{}", module_string, info.impl_id, info.impl_intf);
}
// ConfigBase
std::string ConfigBase::printable_identifier(const std::string& module_id) const {
BOOST_LOG_FUNCTION();
return printable_identifier(module_id, "");
}
std::string ConfigBase::printable_identifier(const std::string& module_id, const std::string& impl_id) const {
BOOST_LOG_FUNCTION();
const auto info = extract_implementation_info(this->module_names, this->manifests, module_id, impl_id);
return create_printable_identifier(info, module_id, impl_id);
}
std::string ConfigBase::get_module_name(const std::string& module_id) const {
return this->module_names.at(module_id);
}
std::string ConfigBase::mqtt_prefix(const std::string& module_id, const std::string& impl_id) {
BOOST_LOG_FUNCTION();
return fmt::format("{}modules/{}/impl/{}", this->mqtt_settings.everest_prefix, module_id, impl_id);
}
std::string ConfigBase::mqtt_module_prefix(const std::string& module_id) {
BOOST_LOG_FUNCTION();
return fmt::format("{}modules/{}", this->mqtt_settings.everest_prefix, module_id);
}
const json& ConfigBase::get_main_config() const {
BOOST_LOG_FUNCTION();
return this->main;
}
bool ConfigBase::contains(const std::string& module_id) const {
BOOST_LOG_FUNCTION();
return this->main.contains(module_id);
}
const json& ConfigBase::get_manifests() const {
BOOST_LOG_FUNCTION();
return this->manifests;
}
const json& ConfigBase::get_interface_definitions() const {
BOOST_LOG_FUNCTION();
return this->interface_definitions;
}
const json& ConfigBase::get_interfaces() const {
BOOST_LOG_FUNCTION();
return this->interfaces;
}
const json& ConfigBase::get_settings() const {
BOOST_LOG_FUNCTION();
return this->settings;
}
const json ConfigBase::get_schemas() const {
BOOST_LOG_FUNCTION();
return this->_schemas;
}
json ConfigBase::get_error_types() {
BOOST_LOG_FUNCTION();
return this->error_map.get_error_types();
}
const json& ConfigBase::get_types() const {
BOOST_LOG_FUNCTION();
return this->types;
}
std::unordered_map<std::string, ConfigCache> ConfigBase::get_module_config_cache() {
BOOST_LOG_FUNCTION();
return this->module_config_cache;
}
std::unordered_map<std::string, std::string> ConfigBase::get_module_names() {
return this->module_names;
}
json ConfigBase::resolve_requirement(const std::string& module_id, const std::string& requirement_id) const {
BOOST_LOG_FUNCTION();
// FIXME (aw): this function should throw, if the requirement id
// isn't even listed in the module manifest
// FIXME (aw): the following if doesn't check for the requirement id
// at all
const auto module_name_it = this->module_names.find(module_id);
if (module_name_it == this->module_names.end()) {
EVLOG_AND_THROW(EverestApiError(fmt::format("Requested requirement id '{}' of module {} not found in config!",
requirement_id, printable_identifier(module_id))));
}
// check for connections for this requirement
const auto& module_config = this->main.at(module_id);
const std::string module_name = module_name_it->second;
const auto& requirement = this->manifests.at(module_name).at("requires").at(requirement_id);
if (!module_config.at("connections").contains(requirement_id)) {
return json::array(); // return an empty array if our config does not contain any connections for this
// requirement id
}
// if only one single connection entry was required, return only this one
// callers can check with is_array() if this is a single connection (legacy) or a connection list
if (requirement.at("min_connections") == 1 && requirement.at("max_connections") == 1) {
return module_config.at("connections").at(requirement_id).at(0);
}
return module_config.at("connections").at(requirement_id);
}
std::map<Requirement, Fulfillment> ConfigBase::resolve_requirements(const std::string& module_id) const {
std::map<Requirement, Fulfillment> requirements;
const auto& module_name = get_module_name(module_id);
for (const auto& req_id : Config::keys(this->manifests.at(module_name).at("requires"))) {
const auto& resolved_req = this->resolve_requirement(module_id, req_id);
if (!resolved_req.is_array()) {
const auto& resolved_module_id = resolved_req.at("module_id");
const auto& resolved_impl_id = resolved_req.at("implementation_id");
const auto req = Requirement{req_id, 0};
requirements[req] = {resolved_module_id, resolved_impl_id, req};
} else {
for (std::size_t i = 0; i < resolved_req.size(); i++) {
const auto& resolved_module_id = resolved_req.at(i).at("module_id");
const auto& resolved_impl_id = resolved_req.at(i).at("implementation_id");
const auto req = Requirement{req_id, i};
requirements[req] = {resolved_module_id, resolved_impl_id, req};
}
}
}
return requirements;
}
std::list<Requirement> ConfigBase::get_requirements(const std::string& module_id) const {
BOOST_LOG_FUNCTION();
std::list<Requirement> res;
for (const auto& [requirement, fulfillment] : this->resolve_requirements(module_id)) {
res.push_back(requirement);
}
return res;
}
std::map<std::string, std::vector<Fulfillment>> ConfigBase::get_fulfillments(const std::string& module_id) const {
BOOST_LOG_FUNCTION();
std::map<std::string, std::vector<Fulfillment>> res;
for (const auto& [requirement, fulfillment] : this->resolve_requirements(module_id)) {
res[requirement.id].push_back(fulfillment);
}
return res;
}
std::unordered_map<std::string, ModuleTierMappings> ConfigBase::get_3_tier_model_mappings() {
return this->tier_mappings;
}
std::optional<ModuleTierMappings> ConfigBase::get_module_3_tier_model_mappings(const std::string& module_id) const {
if (this->tier_mappings.find(module_id) == this->tier_mappings.end()) {
return std::nullopt;
}
return this->tier_mappings.at(module_id);
}
std::optional<Mapping> ConfigBase::get_3_tier_model_mapping(const std::string& module_id,
const std::string& impl_id) const {
const auto module_tier_mappings = this->get_module_3_tier_model_mappings(module_id);
if (not module_tier_mappings.has_value()) {
return std::nullopt;
}
const auto& mapping = module_tier_mappings.value();
if (mapping.implementations.find(impl_id) == mapping.implementations.end()) {
// if no specific implementation mapping is given, use the module mapping
return mapping.module;
}
return mapping.implementations.at(impl_id);
}
// ManagerConfig
void ManagerConfig::load_and_validate_manifest(const std::string& module_id, const json& module_config) {
const std::string module_name = module_config.at("module");
this->module_config_cache[module_id] = ConfigCache();
this->module_names[module_id] = module_name;
EVLOG_debug << fmt::format("Found module {}, loading and verifying manifest...", printable_identifier(module_id));
// load and validate module manifest.json
const fs::path manifest_path = this->ms.runtime_settings->modules_dir / module_name / "manifest.yaml";
try {
if (module_name != "ProbeModule") {
// FIXME (aw): this is implicit logic, because we know, that the ProbeModule manifest had been set up
// manually already
EVLOG_debug << fmt::format("Loading module manifest file at: {}", fs::canonical(manifest_path).string());
this->manifests[module_name] = load_yaml(manifest_path);
}
json_validator validator(Config::loader, Config::format_checker);
validator.set_root_schema(this->_schemas.manifest);
const auto patch = validator.validate(this->manifests[module_name]);
if (!patch.is_null()) {
// extend manifest with default values
this->manifests[module_name] = this->manifests[module_name].patch(patch);
}
} catch (const std::exception& e) {
EVLOG_AND_THROW(EverestConfigError(fmt::format("Failed to load and parse manifest file {}: {}",
fs::weakly_canonical(manifest_path).string(), e.what())));
}
// validate user-defined default values for the config meta-schemas
try {
validate_config_schema(this->manifests[module_name]["config"]);
} catch (const std::exception& e) {
EVLOG_AND_THROW(EverestConfigError(
fmt::format("Failed to validate the module configuration meta-schema for module '{}'. Reason:\n{}",
module_name, e.what())));
}
for (const auto& impl : this->manifests[module_name]["provides"].items()) {
try {
validate_config_schema(impl.value().at("config"));
} catch (const std::exception& e) {
EVLOG_AND_THROW(
EverestConfigError(fmt::format("Failed to validate the implementation configuration meta-schema "
"for implementation '{}' in module '{}'. Reason:\n{}",
impl.key(), module_name, e.what())));
}
}
const std::set<std::string> provided_impls = Config::keys(this->manifests[module_name]["provides"]);
this->interfaces[module_name] = json({});
this->module_config_cache[module_name].provides_impl = provided_impls;
for (const auto& impl_id : provided_impls) {
EVLOG_debug << fmt::format("Loading interface for implementation: {}", impl_id);
auto intf_name = this->manifests[module_name]["provides"][impl_id]["interface"].get<std::string>();
auto seen_interfaces = std::set<std::string>();
this->interfaces[module_name][impl_id] = intf_name;
resolve_interface(intf_name);
this->module_config_cache[module_name].cmds[impl_id] = this->interface_definitions.at(intf_name).at("cmds");
}
// check if config only contains impl_ids listed in manifest file
std::set<std::string> unknown_impls_in_config;
const std::set<std::string> configured_impls = Config::keys(this->main[module_id]["config_implementation"]);
std::set_difference(configured_impls.begin(), configured_impls.end(), provided_impls.begin(), provided_impls.end(),
std::inserter(unknown_impls_in_config, unknown_impls_in_config.end()));
if (!unknown_impls_in_config.empty()) {
EVLOG_AND_THROW(EverestApiError(
fmt::format("Implementation id(s)[{}] mentioned in config, but not defined in manifest of module '{}'!",
fmt::join(unknown_impls_in_config, " "), module_config.at("module"))));
}
// validate config entries against manifest file
for (const auto& impl_id : provided_impls) {
EVLOG_verbose << fmt::format(
"Validating implementation config of {} against json schemas defined in module mainfest...",
printable_identifier(module_id, impl_id));
const json config_map =
module_config.value("config_implementation", json::object()).value(impl_id, json::object());
const json config_map_schema =
this->manifests[module_config.at("module").get<std::string>()]["provides"][impl_id]["config"];
try {
const auto parsed_config_map = parse_config_map(config_map_schema, config_map);
if (parsed_config_map.unknown_config_entries.size()) {
for (const auto& unknown_entry : parsed_config_map.unknown_config_entries) {
EVLOG_error << fmt::format(
"Unknown config entry '{}' of {} of module '{}' ignored, please fix your config file!",
unknown_entry, printable_identifier(module_id, impl_id), module_config.at("module"));
}
}
this->main[module_id]["config_maps"][impl_id] = parsed_config_map.parsed_config_map;
} catch (const ConfigParseException& err) {
if (err.err_t == ConfigParseException::MISSING_ENTRY) {
EVLOG_AND_THROW(EverestConfigError(fmt::format("Missing mandatory config entry '{}' in {}!", err.entry,
printable_identifier(module_id, impl_id))));
} else if (err.err_t == ConfigParseException::SCHEMA) {
EVLOG_AND_THROW(
EverestConfigError(fmt::format("Schema validation for config entry '{}' failed in {}! Reason:\n{}",
err.entry, printable_identifier(module_id, impl_id), err.what)));
} else {
throw err;
}
}
}
// validate config for !module
{
const json& config_map = module_config.at("config_module");
const json config_map_schema = this->manifests[module_config.at("module").get<std::string>()]["config"];
try {
auto parsed_config_map = parse_config_map(config_map_schema, config_map);
if (parsed_config_map.unknown_config_entries.size()) {
for (const auto& unknown_entry : parsed_config_map.unknown_config_entries) {
EVLOG_error << fmt::format(
"Unknown config entry '{}' of module '{}' ignored, please fix your config file!", unknown_entry,
module_config.at("module"));
}
}
this->main[module_id]["config_maps"]["!module"] = parsed_config_map.parsed_config_map;
} catch (const ConfigParseException& err) {
if (err.err_t == ConfigParseException::MISSING_ENTRY) {
EVLOG_AND_THROW(
EverestConfigError(fmt::format("Missing mandatory config entry '{}' for module config in module {}",
err.entry, module_config.at("module"))));
} else if (err.err_t == ConfigParseException::SCHEMA) {
EVLOG_AND_THROW(EverestConfigError(fmt::format(
"Schema validation for config entry '{}' failed for module config in module {}! Reason:\n{}",
err.entry, module_config.at("module"), err.what)));
} else {
throw err;
}
}
}
}
std::tuple<json, int64_t> ManagerConfig::load_and_validate_with_schema(const fs::path& file_path, const json& schema) {
const json json_to_validate = load_yaml(file_path);
int64_t validation_ms = 0;
const auto start_time_validate = std::chrono::system_clock::now();
json_validator validator(Config::loader, Config::format_checker);
validator.set_root_schema(schema);
validator.validate(json_to_validate);
const auto end_time_validate = std::chrono::system_clock::now();
EVLOG_debug
<< "YAML validation of " << file_path.string() << " took: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end_time_validate - start_time_validate).count()
<< "ms";
validation_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(end_time_validate - start_time_validate).count();
return {json_to_validate, validation_ms};
}
json ManagerConfig::resolve_interface(const std::string& intf_name) {
// load and validate interface.json and mark interface as seen
const auto intf_definition = load_interface_file(intf_name);
this->interface_definitions[intf_name] = intf_definition;
return intf_definition;
}
json ManagerConfig::load_interface_file(const std::string& intf_name) {
BOOST_LOG_FUNCTION();
const fs::path intf_path = this->ms.interfaces_dir / (intf_name + ".yaml");
try {
EVLOG_debug << fmt::format("Loading interface file at: {}", fs::canonical(intf_path).string());
json interface_json = load_yaml(intf_path);
// this subschema can not use allOf with the draft-07 schema because that will cause our validator to
// add all draft-07 default values which never validate (the {"not": true} default contradicts everything)
// --> validating against draft-07 will be done in an extra step below
json_validator validator(Config::loader, Config::format_checker);
validator.set_root_schema(this->_schemas.interface);
auto patch = validator.validate(interface_json);
if (!patch.is_null()) {
// extend config entry with default values
interface_json = interface_json.patch(patch);
}
interface_json = ManagerConfig::replace_error_refs(interface_json);
// erase "description"
if (interface_json.contains("description")) {
interface_json.erase("description");
}
// validate every cmd arg/result and var definition against draft-07 schema
validator.set_root_schema(draft07);
for (auto& var_entry : interface_json["vars"].items()) {
auto& var_value = var_entry.value();
// erase "description"
if (var_value.contains("description")) {
var_value.erase("description");
}
if (var_value.contains("items")) {
auto& items = var_value.at("items");
if (items.contains("description")) {
items.erase("description");
}
if (items.contains("properties")) {
for (auto& property : items.at("properties").items()) {
auto& property_value = property.value();
if (property_value.contains("description")) {
property_value.erase("description");
}
}
}
}
validator.validate(var_value);
}
for (auto& cmd_entry : interface_json["cmds"].items()) {
auto& cmd = interface_json["cmds"][cmd_entry.key()];
// erase "description"
if (cmd.contains("description")) {
cmd.erase("description");
}
for (auto& arguments_entry : cmd["arguments"].items()) {
auto& arg_entry = arguments_entry.value();
// erase "description"
if (arg_entry.contains("description")) {
arg_entry.erase("description");
}
validator.validate(arg_entry);
}
auto& result = interface_json["cmds"][cmd_entry.key()]["result"];
// erase "description"
if (result.contains("description")) {
result.erase("description");
}
validator.validate(result);
}
return interface_json;
} catch (const std::exception& e) {
EVLOG_AND_THROW(EverestConfigError(fmt::format("Failed to load and parse interface file {}: {}",
fs::weakly_canonical(intf_path).string(), e.what())));
}
}
std::list<json> ManagerConfig::resolve_error_ref(const std::string& reference) {
BOOST_LOG_FUNCTION();
const std::string ref_prefix = "/errors/";
const std::string err_ref = reference.substr(ref_prefix.length());
const auto result = err_ref.find("#/");
std::string err_namespace;
std::string err_name;
bool is_error_list;
if (result == std::string::npos) {
err_namespace = err_ref;
err_name = "";
is_error_list = true;
} else {
err_namespace = err_ref.substr(0, result);
err_name = err_ref.substr(result + 2);
is_error_list = false;
}
const fs::path path = this->ms.errors_dir / (err_namespace + ".yaml");
json error_json = load_yaml(path);
std::list<json> errors;
if (is_error_list) {
for (auto& error : error_json.at("errors")) {
error["namespace"] = err_namespace;
errors.push_back(error);
}
} else {
for (auto& error : error_json.at("errors")) {
if (error.at("name") == err_name) {
error["namespace"] = err_namespace;
errors.push_back(error);
break;
}
}
}
return errors;
}
json ManagerConfig::replace_error_refs(json& interface_json) {
BOOST_LOG_FUNCTION();
if (!interface_json.contains("errors")) {
return interface_json;
}
json errors_new = json::object();
for (auto& error_entry : interface_json.at("errors")) {
const std::list<json> errors = resolve_error_ref(error_entry.at("reference"));
for (auto& error : errors) {
if (!errors_new.contains(error.at("namespace"))) {
errors_new[error.at("namespace")] = json::object();
}
if (errors_new.at(error.at("namespace")).contains(error.at("name"))) {
EVLOG_AND_THROW(EverestConfigError(fmt::format("Error name '{}' in namespace '{}' already referenced!",
error.at("name"), error.at("namespace"))));
}
errors_new[error.at("namespace")][error.at("name")] = error;
}
}
interface_json["errors"] = errors_new;
return interface_json;
}
void ManagerConfig::resolve_all_requirements() {
BOOST_LOG_FUNCTION();
EVLOG_debug << "Resolving module requirements...";
// this whole code will not check existence of keys defined by config or
// manifest metaschemas these have already been checked by schema validation
for (auto& element : this->main.items()) {
const auto& module_id = element.key();
auto& module_config = element.value();
std::set<std::string> unknown_requirement_entries;
const std::set<std::string> module_config_connections_set = Config::keys(module_config["connections"]);
const std::set<std::string> manifest_module_requires_set =
Config::keys(this->manifests[module_config["module"].get<std::string>()]["requires"]);
std::set_difference(module_config_connections_set.begin(), module_config_connections_set.end(),
manifest_module_requires_set.begin(), manifest_module_requires_set.end(),
std::inserter(unknown_requirement_entries, unknown_requirement_entries.end()));
if (!unknown_requirement_entries.empty()) {
EVLOG_AND_THROW(EverestApiError(fmt::format("Configured connection for requirement id(s) [{}] of {} not "
"defined as requirement in manifest of module '{}'!",
fmt::join(unknown_requirement_entries, " "),
printable_identifier(module_id), module_config["module"])));
}
for (auto& element : this->manifests[module_config["module"].get<std::string>()]["requires"].items()) {
const auto& requirement_id = element.key();
const auto& requirement = element.value();
if (!module_config["connections"].contains(requirement_id)) {
if (requirement.at("min_connections") < 1) {
EVLOG_debug << fmt::format("Manifest of {} lists OPTIONAL requirement '{}' which could not be "
"fulfilled and will be ignored...",
printable_identifier(module_id), requirement_id);
continue; // stop here, there is nothing we can do
}
EVLOG_AND_THROW(EverestConfigError(fmt::format(
"Requirement '{}' of module {} not fulfilled: requirement id '{}' not listed in connections!",
requirement_id, printable_identifier(module_id), requirement_id)));
}
const json& connections = module_config["connections"][requirement_id];
// check if min_connections and max_connections are fulfilled
if (connections.size() < requirement.at("min_connections") ||
connections.size() > requirement.at("max_connections")) {
EVLOG_AND_THROW(EverestConfigError(
fmt::format("Requirement '{}' of module {} not fulfilled: requirement list does "
"not have an entry count between {} and {}!",
requirement_id, printable_identifier(module_id), requirement.at("min_connections"),
requirement.at("max_connections"))));
}
for (uint64_t connection_num = 0; connection_num < connections.size(); connection_num++) {
const auto& connection = connections[connection_num];
const std::string& connection_module_id = connection.at("module_id");
if (!this->main.contains(connection_module_id)) {
EVLOG_AND_THROW(EverestConfigError(fmt::format(
"Requirement '{}' of module {} not fulfilled: module id '{}' (configured in "
"connection {}) not loaded in config!",
requirement_id, printable_identifier(module_id), connection_module_id, connection_num)));
}
const std::string& connection_module_name = this->main[connection_module_id]["module"];
const std::string& connection_impl_id = connection.at("implementation_id");
const auto& connection_manifest = this->manifests[connection_module_name];
if (!connection_manifest.at("provides").contains(connection_impl_id)) {
EVLOG_AND_THROW(EverestConfigError(
fmt::format("Requirement '{}' of module {} not fulfilled: required module {} does not provide "
"an implementation for '{}'!",
requirement_id, printable_identifier(module_id),
printable_identifier(connection.at("module_id")), connection_impl_id)));
}
// FIXME: copy here so we can safely erase description and config entries
// FIXME: if we were to copy here this costs us a huge amount of performance during startup
// FIXME: or does it really? tests are inconclusive right now...
auto connection_provides = connection_manifest.at("provides").at(connection_impl_id);
if (connection_provides.contains("config")) {
connection_provides.erase("config");
}
if (connection_provides.contains("description")) {
connection_provides.erase("description");
}
const std::string& requirement_interface = requirement.at("interface");
// check interface requirement
if (requirement_interface != connection_provides.at("interface")) {
EVLOG_AND_THROW(EverestConfigError(fmt::format(
"Requirement '{}' of module {} not fulfilled by connection to module {}: required "
"interface "
"'{}' is not provided by this implementation! Connected implementation provides interface "
"'{}'.",
requirement_id, printable_identifier(module_id),
printable_identifier(connection.at("module_id"), connection_impl_id), requirement_interface,
connection_provides.at("interface").get<std::string>())));
}
module_config["connections"][requirement_id][connection_num]["provides"] = connection_provides;
module_config["connections"][requirement_id][connection_num]["required_interface"] =
requirement_interface;
EVLOG_debug << fmt::format(
"Manifest of {} lists requirement '{}' which will be fulfilled by {}...",
printable_identifier(module_id), requirement_id,
printable_identifier(connection.at("module_id"), connection.at("implementation_id")));
}
}
}
EVLOG_debug << "All module requirements resolved successfully...";
}
void ManagerConfig::parse(json config) {
this->main = std::move(config);
// load type files
if (this->ms.runtime_settings->validate_schema) {
int64_t total_time_validation_ms = 0, total_time_parsing_ms = 0;
for (auto const& types_entry : fs::recursive_directory_iterator(this->ms.types_dir)) {
const auto start_time = std::chrono::system_clock::now();
const auto& type_file_path = types_entry.path();
if (fs::is_regular_file(type_file_path) && type_file_path.extension() == ".yaml") {
const auto type_path =
std::string("/") + fs::relative(type_file_path, this->ms.types_dir).stem().string();
try {
// load and validate type file, store validated result in this->types
EVLOG_verbose << fmt::format("Loading type file at: {}", fs::canonical(type_file_path).c_str());
const auto [type_json, validate_ms] =
load_and_validate_with_schema(type_file_path, this->_schemas.type);
total_time_validation_ms += validate_ms;
this->types[type_path] = type_json.at("types");
} catch (const std::exception& e) {
EVLOG_AND_THROW(EverestConfigError(fmt::format(
"Failed to load and parse type file '{}', reason: {}", type_file_path.string(), e.what())));
}
}
const auto end_time = std::chrono::system_clock::now();
total_time_parsing_ms +=
std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
EVLOG_debug << "Parsing of type " << types_entry.path().string() << " took: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() << "ms";
}
EVLOG_info << "- Types loaded in [" << total_time_parsing_ms - total_time_validation_ms << "ms]";
EVLOG_info << "- Types validated [" << total_time_validation_ms << "ms]";
}
// load error files
if (this->ms.runtime_settings->validate_schema) {
int64_t total_time_validation_ms = 0, total_time_parsing_ms = 0;
for (auto const& errors_entry : fs::recursive_directory_iterator(this->ms.errors_dir)) {
const auto start_time = std::chrono::system_clock::now();
const auto& error_file_path = errors_entry.path();
if (fs::is_regular_file(error_file_path) && error_file_path.extension() == ".yaml") {
try {
// load and validate error file
EVLOG_verbose << fmt::format("Loading error file at: {}", fs::canonical(error_file_path).c_str());
const auto [error_json, validate_ms] =
load_and_validate_with_schema(error_file_path, this->_schemas.error_declaration_list);
total_time_validation_ms += validate_ms;
} catch (const std::exception& e) {
EVLOG_AND_THROW(EverestConfigError(fmt::format(
"Failed to load and parse error file '{}', reason: {}", error_file_path.string(), e.what())));
}
}
const auto end_time = std::chrono::system_clock::now();
total_time_parsing_ms +=
std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
EVLOG_debug << "Parsing of error " << errors_entry.path().string() << " took: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() << "ms";
}
EVLOG_info << "- Errors loaded in [" << total_time_parsing_ms - total_time_validation_ms << "ms]";
EVLOG_info << "- Errors validated [" << total_time_validation_ms << "ms]";
}
std::optional<std::string> probe_module_id;
// load manifest files of configured modules
for (const auto& element : this->main.items()) {
const auto& module_id = element.key();
const auto& module_config = element.value();
if (module_config.at("module") == "ProbeModule") {
if (probe_module_id) {
EVLOG_AND_THROW(EverestConfigError("Multiple instance of module type ProbeModule not supported yet"));
}
probe_module_id = module_id;
continue;
}
load_and_validate_manifest(module_id, module_config);
}
if (probe_module_id) {
setup_probe_module_manifest(*probe_module_id, this->main, this->manifests);
load_and_validate_manifest(*probe_module_id, this->main.at(*probe_module_id));
}
// load telemetry configs
for (const auto& element : this->main.items()) {
const auto& module_id = element.key();
auto& module_config = element.value();
if (module_config.contains("telemetry")) {
const auto& telemetry = module_config.at("telemetry");
if (telemetry.contains("id")) {
this->telemetry_configs[module_id].emplace(TelemetryConfig{telemetry.at("id").get<int>()});
}
}
}
resolve_all_requirements();
parse_3_tier_model_mapping();
// TODO: cleanup "descriptions" from config ?
}
void ManagerConfig::parse_3_tier_model_mapping() {
for (const auto& element : this->main.items()) {
const auto& module_id = element.key();
const auto& module_name = this->get_module_name(module_id);