|
| 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 | +#include "fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/entry/CmdConfigProtocolBgpPolicyAsPathListEntry.h" |
| 12 | + |
| 13 | +#include "fboss/cli/fboss2/CmdHandler.cpp" |
| 14 | + |
| 15 | +#include <fmt/core.h> |
| 16 | +#include <neteng/fboss/bgp/public_tld/configerator/structs/neteng/fboss/bgp/gen-cpp2/bgp_config_types.h> |
| 17 | +#include <iostream> |
| 18 | +#include <map> |
| 19 | +#include <optional> |
| 20 | +#include <ostream> |
| 21 | +#include <stdexcept> |
| 22 | +#include <string> |
| 23 | +#include <string_view> |
| 24 | +#include <vector> |
| 25 | +#include "configerator/structs/neteng/bgp_policy/thrift/gen-cpp2/bgp_policy_types.h" |
| 26 | +#include "configerator/structs/neteng/bgp_policy/thrift/gen-cpp2/routing_policy_types.h" |
| 27 | +#include "fboss/cli/fboss2/commands/config/protocol/bgp/BgpCliAttrHandlers.h" |
| 28 | +#include "fboss/cli/fboss2/commands/config/protocol/bgp/BgpCliValueParsers.h" |
| 29 | +#include "fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/BgpAsPathListCliUtils.h" |
| 30 | +#include "fboss/cli/fboss2/session/ConfigSession.h" |
| 31 | +#include "fboss/cli/fboss2/utils/CmdUtilsCommon.h" |
| 32 | +#include "fboss/cli/fboss2/utils/HostInfo.h" |
| 33 | +#include "fmt/format.h" |
| 34 | + |
| 35 | +namespace facebook::fboss { |
| 36 | + |
| 37 | +namespace { |
| 38 | + |
| 39 | +// The attribute names, exactly as documented. Kept here so the |
| 40 | +// valid-attribute set and the handler table stay in sync. |
| 41 | +constexpr std::string_view kDescription = "description"; |
| 42 | +constexpr std::string_view kAsnRegexp = "asn-regexp"; |
| 43 | +constexpr std::string_view kMatchLogic = "match-logic"; |
| 44 | + |
| 45 | +// match-logic values (routing_policy.MatchValueLogicOperator names). |
| 46 | +constexpr std::string_view kMatchLogicEqual = "EQUAL"; |
| 47 | +constexpr std::string_view kMatchLogicNotEqual = "NOT_EQUAL"; |
| 48 | + |
| 49 | +using AsPathListEntry = bgp::bgp_policy::AsPathListEntry; |
| 50 | +using MatchValueLogicOperator = bgp::routing_policy::MatchValueLogicOperator; |
| 51 | +using bgpcli::AttrHandler; |
| 52 | +using bgpcli::enumAttr; |
| 53 | +using bgpcli::joinedStringAttr; |
| 54 | +using bgpcli::ok; |
| 55 | +using bgpcli::Result; |
| 56 | + |
| 57 | +// ---- value lookups ---------------------------------------------------------- |
| 58 | + |
| 59 | +std::optional<MatchValueLogicOperator> lookupMatchLogic(const std::string& s) { |
| 60 | + if (s == kMatchLogicEqual) { |
| 61 | + return MatchValueLogicOperator::EQUAL; |
| 62 | + } |
| 63 | + if (s == kMatchLogicNotEqual) { |
| 64 | + return MatchValueLogicOperator::NOT_EQUAL; |
| 65 | + } |
| 66 | + return std::nullopt; |
| 67 | +} |
| 68 | + |
| 69 | +// ---- entry-level setters ---------------------------------------------------- |
| 70 | +// Each writes one already-parsed, already-validated value. Parsing and message |
| 71 | +// text belong to the shared factories in BgpCliAttrHandlers.h. |
| 72 | + |
| 73 | +void setDescription(AsPathListEntry& entry, const std::string& description) { |
| 74 | + entry.description() = description; |
| 75 | +} |
| 76 | + |
| 77 | +// The AS path is modelled as an AsPathType union; the CLI sets the inline |
| 78 | +// `as_path` (AsPath) arm and its regexp, matching the thrift path |
| 79 | +// .as_path.as_path.asn_regexp. |
| 80 | +void setAsnRegexp(AsPathListEntry& entry, const std::string& regexp) { |
| 81 | + entry.as_path()->set_as_path().asn_regexp() = regexp; |
| 82 | +} |
| 83 | + |
| 84 | +void setMatchLogic(AsPathListEntry& entry, MatchValueLogicOperator op) { |
| 85 | + entry.match_logic_type() = op; |
| 86 | +} |
| 87 | + |
| 88 | +// ---- entry-level attribute registry ----------------------------------------- |
| 89 | +// One line per documented attribute: its dispatch key, its value shape, and |
| 90 | +// the setter that stores it. asn-regexp is a joined string because an AS-path |
| 91 | +// regex separates ASNs with spaces (e.g. `^65000 65001$`), so its tokens must |
| 92 | +// be re-joined rather than required to be single. |
| 93 | +const std::map<std::string, AttrHandler<AsPathListEntry>, std::less<>>& |
| 94 | +entryAttrHandlers() { |
| 95 | + static const std::string kMatchLogicValues = |
| 96 | + fmt::format("{}|{}", kMatchLogicEqual, kMatchLogicNotEqual); |
| 97 | + static const std::map<std::string, AttrHandler<AsPathListEntry>, std::less<>> |
| 98 | + kHandlers = { |
| 99 | + {std::string(kAsnRegexp), |
| 100 | + joinedStringAttr<AsPathListEntry>(kAsnRegexp, setAsnRegexp)}, |
| 101 | + {std::string(kDescription), |
| 102 | + joinedStringAttr<AsPathListEntry>(kDescription, setDescription)}, |
| 103 | + {std::string(kMatchLogic), |
| 104 | + enumAttr<AsPathListEntry, MatchValueLogicOperator>( |
| 105 | + kMatchLogic, |
| 106 | + kMatchLogicValues, |
| 107 | + lookupMatchLogic, |
| 108 | + setMatchLogic)}, |
| 109 | + }; |
| 110 | + return kHandlers; |
| 111 | +} |
| 112 | + |
| 113 | +std::string validAttrList() { |
| 114 | + std::string out; |
| 115 | + for (const auto& [name, _] : entryAttrHandlers()) { |
| 116 | + if (!out.empty()) { |
| 117 | + out += ", "; |
| 118 | + } |
| 119 | + out += name; |
| 120 | + } |
| 121 | + return out; |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace |
| 125 | + |
| 126 | +// Parse + validate at construction so queryClient stays a thin dispatch. |
| 127 | +// Throwing std::invalid_argument is how the framework surfaces arg parse |
| 128 | +// errors (same mechanism as BgpAsPathListConfig). |
| 129 | +BgpAsPathListEntryConfig::BgpAsPathListEntryConfig(std::vector<std::string> v) |
| 130 | + : utils::BaseObjectArgType<std::string>(v) { |
| 131 | + if (v.empty()) { |
| 132 | + throw std::invalid_argument( |
| 133 | + "Error: entry <seq-num> is required, optionally followed by an " |
| 134 | + "<attribute> <value>"); |
| 135 | + } |
| 136 | + auto seq = bgpcli::parseNonNegInt64(v[0]); |
| 137 | + if (!seq) { |
| 138 | + throw std::invalid_argument( |
| 139 | + fmt::format( |
| 140 | + "Error: entry <seq-num> must be a non-negative integer, got '{}'", |
| 141 | + v[0])); |
| 142 | + } |
| 143 | + seqNum_ = *seq; |
| 144 | + if (v.size() == 1) { |
| 145 | + return; // bare `entry <seq-num>`: create it |
| 146 | + } |
| 147 | + |
| 148 | + attr_ = v[1]; |
| 149 | + values_.assign(v.begin() + 2, v.end()); |
| 150 | + |
| 151 | + if (entryAttrHandlers().find(attr_) == entryAttrHandlers().end()) { |
| 152 | + throw std::invalid_argument( |
| 153 | + fmt::format( |
| 154 | + "Error: unknown as-path-list entry attribute '{}'. Valid " |
| 155 | + "attributes: {}", |
| 156 | + attr_, |
| 157 | + validAttrList())); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +CmdConfigProtocolBgpPolicyAsPathListEntryTraits::RetType |
| 162 | +CmdConfigProtocolBgpPolicyAsPathListEntry::queryClient( |
| 163 | + const HostInfo& /* hostInfo */, |
| 164 | + const BgpAsPathListConfig& listArgs, |
| 165 | + const ObjectArgType& args) { |
| 166 | + // The parent parse accepts `as-path-list <name> <attr> <value> ... entry |
| 167 | + // ...`, but only the leaf (this command) runs — silently dropping the |
| 168 | + // list-level attribute would look like it was staged. Reject the mix. |
| 169 | + if (!listArgs.attr().empty()) { |
| 170 | + return fmt::format( |
| 171 | + "Error: configure as-path-list attributes and entry in separate " |
| 172 | + "commands (got as-path-list attribute '{}' alongside entry {})", |
| 173 | + listArgs.attr(), |
| 174 | + args.seqNum()); |
| 175 | + } |
| 176 | + |
| 177 | + auto& session = ConfigSession::getInstance(); |
| 178 | + auto& cfg = session.getBgpConfig(); |
| 179 | + const bool listCreated = !bgpcli::asPathListExists(cfg, listArgs.listName()); |
| 180 | + auto& list = bgpcli::findOrCreateAsPathList(cfg, listArgs.listName()); |
| 181 | + const bool entryCreated = !bgpcli::asPathListEntryExists(list, args.seqNum()); |
| 182 | + auto& entry = bgpcli::findOrCreateAsPathListEntry(list, args.seqNum()); |
| 183 | + |
| 184 | + Result result = args.attr().empty() |
| 185 | + ? ok(entryCreated |
| 186 | + ? fmt::format( |
| 187 | + "Successfully created BGP as-path-list {} entry {}", |
| 188 | + listArgs.listName(), |
| 189 | + args.seqNum()) |
| 190 | + : fmt::format( |
| 191 | + "BGP as-path-list {} entry {} already exists", |
| 192 | + listArgs.listName(), |
| 193 | + args.seqNum())) |
| 194 | + // The attribute is guaranteed valid: BgpAsPathListEntryConfig's |
| 195 | + // constructor rejects an unknown attribute before we get here. |
| 196 | + : entryAttrHandlers().find(args.attr())->second(entry, args.values()); |
| 197 | + |
| 198 | + if (result.ok) { |
| 199 | + if (!args.attr().empty()) { |
| 200 | + result.message += fmt::format( |
| 201 | + " for as-path-list {} entry {}", listArgs.listName(), args.seqNum()); |
| 202 | + } |
| 203 | + session.saveBgpConfig(); |
| 204 | + result.message += |
| 205 | + fmt::format("\nConfig saved to: {}", session.getBgpSessionConfigPath()); |
| 206 | + } else { |
| 207 | + // A rejected value must not leave a phantom entry (or a phantom list |
| 208 | + // implicitly created for it) visible to later lookups in this process. |
| 209 | + if (entryCreated) { |
| 210 | + list.as_path_list()->pop_back(); |
| 211 | + } |
| 212 | + if (listCreated) { |
| 213 | + cfg.policies()->aspath_lists()->pop_back(); |
| 214 | + } |
| 215 | + } |
| 216 | + return result.message; |
| 217 | +} |
| 218 | + |
| 219 | +void CmdConfigProtocolBgpPolicyAsPathListEntry::printOutput( |
| 220 | + const RetType& output) { |
| 221 | + std::cout << output << std::endl; |
| 222 | +} |
| 223 | + |
| 224 | +// Explicit template instantiation |
| 225 | +template void CmdHandler< |
| 226 | + CmdConfigProtocolBgpPolicyAsPathListEntry, |
| 227 | + CmdConfigProtocolBgpPolicyAsPathListEntryTraits>::run(); |
| 228 | + |
| 229 | +} // namespace facebook::fboss |
0 commit comments