|
| 1 | +/* |
| 2 | + * Copyright (c) 2004-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include <neteng/fboss/bgp/public_tld/configerator/structs/neteng/fboss/bgp/gen-cpp2/bgp_config_types.h> |
| 14 | +#include <algorithm> |
| 15 | +#include <cstdint> |
| 16 | +#include <string> |
| 17 | +#include "configerator/structs/neteng/bgp_policy/thrift/gen-cpp2/bgp_policy_types.h" |
| 18 | + |
| 19 | +/** |
| 20 | + * Lookup/create helpers for the routing-policy CLI family, shared between the |
| 21 | + * policy-level dispatcher (CmdConfigProtocolBgpPolicyRoutingPolicy), the term |
| 22 | + * subcommand (CmdConfigProtocolBgpPolicyRoutingPolicyTerm), and their delete |
| 23 | + * counterparts. A routing-policy (BgpPolicyStatement) is keyed by name; a |
| 24 | + * term (BgpPolicyTerm in policy_entries[]) is keyed by sequence_number. |
| 25 | + */ |
| 26 | +namespace facebook::fboss::bgpcli { |
| 27 | + |
| 28 | +inline bool routingPolicyExists( |
| 29 | + const bgp::thrift::BgpConfig& cfg, |
| 30 | + const std::string& name) { |
| 31 | + if (!cfg.policies().has_value()) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + const auto& policies = *cfg.policies()->bgp_policy_statements(); |
| 35 | + return std::any_of(policies.begin(), policies.end(), [&](const auto& p) { |
| 36 | + return *p.name() == name; |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +// The routing-policy statement keyed by name, or nullptr. Non-creating; |
| 41 | +// used by the delete commands so a typo'd delete can't stage a change. |
| 42 | +inline bgp::bgp_policy::BgpPolicyStatement* findRoutingPolicy( |
| 43 | + bgp::thrift::BgpConfig& cfg, |
| 44 | + const std::string& name) { |
| 45 | + if (!cfg.policies().has_value()) { |
| 46 | + return nullptr; |
| 47 | + } |
| 48 | + auto& policies = *cfg.policies()->bgp_policy_statements(); |
| 49 | + auto it = std::find_if(policies.begin(), policies.end(), [&](const auto& p) { |
| 50 | + return *p.name() == name; |
| 51 | + }); |
| 52 | + return it == policies.end() ? nullptr : &*it; |
| 53 | +} |
| 54 | + |
| 55 | +// Find the routing-policy statement keyed by name, creating it if absent. |
| 56 | +// Setting an attribute on a not-yet-created policy implicitly creates it, so |
| 57 | +// command ordering stays forgiving; a bare `routing-policy <name>` creates one |
| 58 | +// explicitly. BgpPolicyStatement's only key field is `name`. |
| 59 | +inline bgp::bgp_policy::BgpPolicyStatement& findOrCreateRoutingPolicy( |
| 60 | + bgp::thrift::BgpConfig& cfg, |
| 61 | + const std::string& name) { |
| 62 | + auto& policies = *cfg.policies().ensure().bgp_policy_statements(); |
| 63 | + for (auto& policy : policies) { |
| 64 | + if (*policy.name() == name) { |
| 65 | + return policy; |
| 66 | + } |
| 67 | + } |
| 68 | + policies.emplace_back(); |
| 69 | + auto& policy = policies.back(); |
| 70 | + policy.name() = name; |
| 71 | + return policy; |
| 72 | +} |
| 73 | + |
| 74 | +inline bool routingPolicyTermExists( |
| 75 | + const bgp::bgp_policy::BgpPolicyStatement& policy, |
| 76 | + int64_t seqNum) { |
| 77 | + const auto& terms = *policy.policy_entries(); |
| 78 | + return std::any_of(terms.begin(), terms.end(), [&](const auto& t) { |
| 79 | + return t.sequence_number().has_value() && *t.sequence_number() == seqNum; |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +// Find the term keyed by sequence_number, creating it if absent. The name is |
| 84 | +// seeded from the seq-num so every term has a stable, unique identity — |
| 85 | +// BgpPolicyAction.next_term_id references terms by name. |
| 86 | +inline bgp::bgp_policy::BgpPolicyTerm& findOrCreateRoutingPolicyTerm( |
| 87 | + bgp::bgp_policy::BgpPolicyStatement& policy, |
| 88 | + int64_t seqNum) { |
| 89 | + auto& terms = *policy.policy_entries(); |
| 90 | + for (auto& term : terms) { |
| 91 | + if (term.sequence_number().has_value() && |
| 92 | + *term.sequence_number() == seqNum) { |
| 93 | + return term; |
| 94 | + } |
| 95 | + } |
| 96 | + terms.emplace_back(); |
| 97 | + auto& term = terms.back(); |
| 98 | + term.sequence_number() = seqNum; |
| 99 | + term.name() = std::to_string(seqNum); |
| 100 | + return term; |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace facebook::fboss::bgpcli |
0 commit comments