|
| 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/routing-policy/CmdConfigProtocolBgpPolicyRoutingPolicy.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 <functional> |
| 18 | +#include <iostream> |
| 19 | +#include <map> |
| 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 "fboss/cli/fboss2/commands/config/protocol/bgp/BgpCliAttrHandlers.h" |
| 27 | +#include "fboss/cli/fboss2/commands/config/protocol/bgp/BgpCliValueParsers.h" |
| 28 | +#include "fboss/cli/fboss2/session/ConfigSession.h" |
| 29 | +#include "fboss/cli/fboss2/utils/CmdUtilsCommon.h" |
| 30 | +#include "fboss/cli/fboss2/utils/HostInfo.h" |
| 31 | +#include "fmt/format.h" |
| 32 | + |
| 33 | +namespace facebook::fboss { |
| 34 | + |
| 35 | +namespace { |
| 36 | + |
| 37 | +// The attribute names, exactly as documented. Kept here so the valid-attribute |
| 38 | +// set and the handler table stay in sync. |
| 39 | +constexpr std::string_view kObjectName = "routing-policy"; |
| 40 | +constexpr std::string_view kDescription = "description"; |
| 41 | + |
| 42 | +using BgpPolicyStatement = bgp::bgp_policy::BgpPolicyStatement; |
| 43 | +using bgpcli::AttrHandler; |
| 44 | +using bgpcli::joinedStringAttr; |
| 45 | +using bgpcli::ok; |
| 46 | +using bgpcli::Result; |
| 47 | + |
| 48 | +// ---- policy-level setters --------------------------------------------------- |
| 49 | +// Each writes one already-parsed, already-validated value. Parsing and message |
| 50 | +// text belong to the shared factories in BgpCliAttrHandlers.h. |
| 51 | + |
| 52 | +void setDescription( |
| 53 | + BgpPolicyStatement& policy, |
| 54 | + const std::string& description) { |
| 55 | + policy.description() = description; |
| 56 | +} |
| 57 | + |
| 58 | +// ---- policy-level attribute registry --------------------------------------- |
| 59 | +// One line per documented attribute: its dispatch key, its value shape, and |
| 60 | +// the setter that stores it. |
| 61 | +const std::map<std::string, AttrHandler<BgpPolicyStatement>, std::less<>>& |
| 62 | +policyAttrHandlers() { |
| 63 | + static const std:: |
| 64 | + map<std::string, AttrHandler<BgpPolicyStatement>, std::less<>> |
| 65 | + kHandlers = { |
| 66 | + {std::string(kDescription), |
| 67 | + joinedStringAttr<BgpPolicyStatement>( |
| 68 | + kDescription, setDescription)}, |
| 69 | + }; |
| 70 | + return kHandlers; |
| 71 | +} |
| 72 | + |
| 73 | +std::string validAttrList() { |
| 74 | + std::string out; |
| 75 | + for (const auto& [name, _] : policyAttrHandlers()) { |
| 76 | + if (!out.empty()) { |
| 77 | + out += ", "; |
| 78 | + } |
| 79 | + out += name; |
| 80 | + } |
| 81 | + return out; |
| 82 | +} |
| 83 | + |
| 84 | +// Find the routing-policy statement keyed by name, creating it if absent. |
| 85 | +// Setting an attribute on a not-yet-created policy implicitly creates it, so |
| 86 | +// command ordering stays forgiving; a bare `routing-policy <name>` creates one |
| 87 | +// explicitly. BgpPolicyStatement's only key field is `name`. |
| 88 | +BgpPolicyStatement& findOrCreatePolicy( |
| 89 | + bgp::thrift::BgpConfig& cfg, |
| 90 | + const std::string& name) { |
| 91 | + auto& policies = *cfg.policies().ensure().bgp_policy_statements(); |
| 92 | + for (auto& policy : policies) { |
| 93 | + if (*policy.name() == name) { |
| 94 | + return policy; |
| 95 | + } |
| 96 | + } |
| 97 | + policies.emplace_back(); |
| 98 | + auto& policy = policies.back(); |
| 99 | + policy.name() = name; |
| 100 | + return policy; |
| 101 | +} |
| 102 | + |
| 103 | +bool policyExists(const bgp::thrift::BgpConfig& cfg, const std::string& name) { |
| 104 | + if (!cfg.policies().has_value()) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + for (const auto& policy : *cfg.policies()->bgp_policy_statements()) { |
| 108 | + if (*policy.name() == name) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + } |
| 112 | + return false; |
| 113 | +} |
| 114 | + |
| 115 | +} // namespace |
| 116 | + |
| 117 | +// Parse + validate at construction so queryClient stays a thin dispatch. |
| 118 | +// Throwing std::invalid_argument is how the framework surfaces arg parse |
| 119 | +// errors (same mechanism as BgpCommunityListConfig). |
| 120 | +BgpRoutingPolicyConfig::BgpRoutingPolicyConfig(std::vector<std::string> v) |
| 121 | + : utils::BaseObjectArgType<std::string>(v) { |
| 122 | + if (v.empty()) { |
| 123 | + throw std::invalid_argument( |
| 124 | + "Error: routing-policy <name> is required, optionally followed by " |
| 125 | + "an <attribute> <value>"); |
| 126 | + } |
| 127 | + if (v[0].empty()) { |
| 128 | + throw std::invalid_argument( |
| 129 | + fmt::format("Error: {} name must not be empty", kObjectName)); |
| 130 | + } |
| 131 | + policyName_ = v[0]; |
| 132 | + if (v.size() == 1) { |
| 133 | + return; // bare `routing-policy <name>`: create it |
| 134 | + } |
| 135 | + |
| 136 | + attr_ = v[1]; |
| 137 | + values_.assign(v.begin() + 2, v.end()); |
| 138 | + |
| 139 | + if (policyAttrHandlers().find(attr_) == policyAttrHandlers().end()) { |
| 140 | + throw std::invalid_argument( |
| 141 | + fmt::format( |
| 142 | + "Error: unknown routing-policy attribute '{}'. Valid " |
| 143 | + "attributes: {}", |
| 144 | + attr_, |
| 145 | + validAttrList())); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +CmdConfigProtocolBgpPolicyRoutingPolicyTraits::RetType |
| 150 | +CmdConfigProtocolBgpPolicyRoutingPolicy::queryClient( |
| 151 | + const HostInfo& /* hostInfo */, |
| 152 | + const ObjectArgType& args) { |
| 153 | + auto& session = ConfigSession::getInstance(); |
| 154 | + auto& cfg = session.getBgpConfig(); |
| 155 | + const bool policyCreated = !policyExists(cfg, args.policyName()); |
| 156 | + auto& policy = findOrCreatePolicy(cfg, args.policyName()); |
| 157 | + |
| 158 | + Result result = args.attr().empty() |
| 159 | + ? ok(policyCreated |
| 160 | + ? fmt::format( |
| 161 | + "Successfully created BGP routing-policy {}", |
| 162 | + args.policyName()) |
| 163 | + : fmt::format( |
| 164 | + "BGP routing-policy {} already exists", args.policyName())) |
| 165 | + // The attribute is guaranteed valid: BgpRoutingPolicyConfig's |
| 166 | + // constructor rejects an unknown attribute before we get here. |
| 167 | + : policyAttrHandlers().find(args.attr())->second(policy, args.values()); |
| 168 | + |
| 169 | + if (result.ok) { |
| 170 | + if (!args.attr().empty()) { |
| 171 | + result.message += |
| 172 | + fmt::format(" for routing-policy {}", args.policyName()); |
| 173 | + } |
| 174 | + session.saveBgpConfig(); |
| 175 | + result.message += |
| 176 | + fmt::format("\nConfig saved to: {}", session.getBgpSessionConfigPath()); |
| 177 | + } else if (policyCreated) { |
| 178 | + // Drop the phantom policy so a rejected value is not visible to later |
| 179 | + // lookups in the same process. |
| 180 | + cfg.policies()->bgp_policy_statements()->pop_back(); |
| 181 | + } |
| 182 | + return result.message; |
| 183 | +} |
| 184 | + |
| 185 | +void CmdConfigProtocolBgpPolicyRoutingPolicy::printOutput( |
| 186 | + const RetType& output) { |
| 187 | + std::cout << output << std::endl; |
| 188 | +} |
| 189 | + |
| 190 | +// Explicit template instantiation |
| 191 | +template void CmdHandler< |
| 192 | + CmdConfigProtocolBgpPolicyRoutingPolicy, |
| 193 | + CmdConfigProtocolBgpPolicyRoutingPolicyTraits>::run(); |
| 194 | + |
| 195 | +} // namespace facebook::fboss |
0 commit comments