| 32 | #include <tsutil/YamlCfg.h> |
| 33 | |
| 34 | NextHopStrategyFactory::NextHopStrategyFactory(const char *file) : fn(file) |
| 35 | { |
| 36 | YAML::Node config; |
| 37 | YAML::Node strategies; |
| 38 | std::stringstream doc; |
| 39 | std::unordered_set<std::string> include_once; |
| 40 | |
| 41 | // strategy policies. |
| 42 | constexpr std::string_view consistent_hash = "consistent_hash"; |
| 43 | constexpr std::string_view first_live = "first_live"; |
| 44 | constexpr std::string_view rr_strict = "rr_strict"; |
| 45 | constexpr std::string_view rr_ip = "rr_ip"; |
| 46 | constexpr std::string_view latched = "latched"; |
| 47 | |
| 48 | bool error_loading = false; |
| 49 | strategies_loaded = true; |
| 50 | const char *basename = std::string_view(fn).substr(fn.find_last_of('/') + 1).data(); |
| 51 | |
| 52 | NH_Note("%s loading ...", basename); |
| 53 | |
| 54 | struct stat sbuf; |
| 55 | if (stat(fn.c_str(), &sbuf) == -1 && errno == ENOENT) { |
| 56 | // missing config file is an acceptable runtime state |
| 57 | strategies_loaded = false; |
| 58 | NH_Note("%s doesn't exist", fn.c_str()); |
| 59 | goto done; |
| 60 | } |
| 61 | |
| 62 | // load the strategies yaml config file. |
| 63 | try { |
| 64 | loadConfigFile(fn.c_str(), doc, include_once); |
| 65 | |
| 66 | config = YAML::Load(doc); |
| 67 | if (config.IsNull()) { |
| 68 | NH_Note("No NextHop strategy configs were loaded."); |
| 69 | strategies_loaded = false; |
| 70 | } else { |
| 71 | strategies = config["strategies"]; |
| 72 | if (strategies.Type() != YAML::NodeType::Sequence) { |
| 73 | NH_Error("malformed %s file, expected a 'strategies' sequence", basename); |
| 74 | strategies_loaded = false; |
| 75 | error_loading = true; |
| 76 | } |
| 77 | } |
| 78 | // loop through the strategies document. |
| 79 | for (auto &&strategie : strategies) { |
| 80 | ts::Yaml::Map strategy{strategie}; |
| 81 | auto name = strategy["strategy"].as<std::string>(); |
| 82 | auto policy = strategy["policy"]; |
| 83 | if (!policy) { |
| 84 | NH_Error("No policy is defined for the strategy named '%s', this strategy will be ignored.", name.c_str()); |
| 85 | continue; |
| 86 | } |
| 87 | const auto &policy_value = policy.Scalar(); |
| 88 | NHPolicyType policy_type = NH_UNDEFINED; |
| 89 | |
| 90 | if (policy_value == consistent_hash) { |
| 91 | policy_type = NH_CONSISTENT_HASH; |