|
| 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/routing_policy_types.h" |
| 18 | + |
| 19 | +/** |
| 20 | + * Lookup/create helpers for the prefix-list CLI family, shared between the |
| 21 | + * list-level dispatcher (CmdConfigProtocolBgpPolicyPrefixList), the entry |
| 22 | + * subcommand (CmdConfigProtocolBgpPolicyPrefixListEntry), and the delete |
| 23 | + * counterparts. A PrefixList is keyed by name; a PrefixListEntry (in |
| 24 | + * prefixes[]) is keyed by seq_num. |
| 25 | + */ |
| 26 | +namespace facebook::fboss::bgpcli { |
| 27 | + |
| 28 | +inline bool prefixListExists( |
| 29 | + const bgp::thrift::BgpConfig& cfg, |
| 30 | + const std::string& name) { |
| 31 | + if (!cfg.policies().has_value()) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + const auto& lists = *cfg.policies()->prefix_lists(); |
| 35 | + return std::any_of(lists.begin(), lists.end(), [&](const auto& list) { |
| 36 | + return *list.name() == name; |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +// Find the prefix-list keyed by name, creating it if absent. Setting an |
| 41 | +// attribute on a not-yet-created list implicitly creates it, so command |
| 42 | +// ordering stays forgiving; a bare `prefix-list <name>` creates one |
| 43 | +// explicitly. PrefixList's only key field is `name`. |
| 44 | +inline bgp::routing_policy::PrefixList& findOrCreatePrefixList( |
| 45 | + bgp::thrift::BgpConfig& cfg, |
| 46 | + const std::string& name) { |
| 47 | + auto& lists = *cfg.policies().ensure().prefix_lists(); |
| 48 | + for (auto& list : lists) { |
| 49 | + if (*list.name() == name) { |
| 50 | + return list; |
| 51 | + } |
| 52 | + } |
| 53 | + lists.emplace_back(); |
| 54 | + auto& list = lists.back(); |
| 55 | + list.name() = name; |
| 56 | + return list; |
| 57 | +} |
| 58 | + |
| 59 | +inline bool prefixListEntryExists( |
| 60 | + const bgp::routing_policy::PrefixList& list, |
| 61 | + int32_t seqNum) { |
| 62 | + const auto& entries = *list.prefixes(); |
| 63 | + return std::any_of(entries.begin(), entries.end(), [&](const auto& entry) { |
| 64 | + return entry.seq_num().has_value() && *entry.seq_num() == seqNum; |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +// Find the entry keyed by seq_num within a list's prefixes[], creating it if |
| 69 | +// absent. seq_num is the entry's identity. |
| 70 | +inline bgp::routing_policy::PrefixListEntry& findOrCreatePrefixListEntry( |
| 71 | + bgp::routing_policy::PrefixList& list, |
| 72 | + int32_t seqNum) { |
| 73 | + auto& entries = *list.prefixes(); |
| 74 | + for (auto& entry : entries) { |
| 75 | + if (entry.seq_num().has_value() && *entry.seq_num() == seqNum) { |
| 76 | + return entry; |
| 77 | + } |
| 78 | + } |
| 79 | + entries.emplace_back(); |
| 80 | + auto& entry = entries.back(); |
| 81 | + entry.seq_num() = seqNum; |
| 82 | + return entry; |
| 83 | +} |
| 84 | + |
| 85 | +} // namespace facebook::fboss::bgpcli |
0 commit comments