Skip to content

Commit 44721ea

Browse files
NOS-6643..NOS-6648: fboss2 config/delete protocol bgp policy as-path-list
Adds the first BGP policy object-type command family, under a new `policy` grouping node beneath `protocol bgp` (sibling to global/neighbor/peer-group): config protocol bgp policy as-path-list <name> [description <string>] delete protocol bgp policy as-path-list <name> The list's `entry <seq-num>` level is a CLI11 subcommand of its own and lands in the next commit, so this one owns only the list level. - Follows the dispatcher shape the neighbor/peer-group families use: one factory per value shape from the shared BgpCliAttrHandlers.h, named setters that do nothing but assign the thrift field, and a registry that is one line per attribute. Writes bgp_policy.BgpPolicies.aspath_lists[] through the typed ConfigSession — no new session plumbing, since BgpConfig.policies already exists. - Lookup/create helpers live in BgpAsPathListCliUtils.h, including the entry-level helpers, so the delete command and the entry subcommand in the next commit share them rather than re-deriving the scan. - A rejected value leaves nothing staged: a list implicitly created for the failed command is rolled back before returning. - There is no per-entry delete; `delete ... as-path-list <name>` removes the whole list. - unit tests (10): CmdConfigBgpPolicyAsPathListTest (6) covering arg validation, bare create, the description round-trip, named lists staying distinct, re-reference reporting the existing list and unknown-attribute rejection, plus CmdDeleteBgpPolicyAsPathListTest (4). - integration tests (2): ConfigBgpPolicyAsPathListTest stages and commits, then asserts against bgpd's running config via the getRunningConfig RPC, confirming bgpd accepts and adopts the .policies blob end to end. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent fb46784 commit 44721ea

17 files changed

Lines changed: 1035 additions & 0 deletions

cmake/CliFboss2.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,11 @@ add_library(fboss2_config_lib
880880
fboss/cli/fboss2/commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.h
881881
fboss/cli/fboss2/commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.cpp
882882
fboss/cli/fboss2/commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.h
883+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.cpp
884+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h
885+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/BgpAsPathListCliUtils.h
886+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.cpp
887+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.h
883888
fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.cpp
884889
fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h
885890
fboss/cli/fboss2/commands/config/ptp/transparent_clock/CmdConfigPtpTransparentClock.cpp
@@ -978,6 +983,10 @@ add_library(fboss2_config_lib
978983
fboss/cli/fboss2/commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h
979984
fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.cpp
980985
fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h
986+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.cpp
987+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h
988+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.cpp
989+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h
981990
fboss/cli/fboss2/commands/delete/protocol/CmdDeleteProtocol.h
982991
fboss/cli/fboss2/commands/delete/protocol/static/CmdDeleteProtocolStatic.cpp
983992
fboss/cli/fboss2/commands/delete/protocol/static/CmdDeleteProtocolStatic.h

fboss/cli/fboss2/BUCK

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,8 @@ cpp_library(
11001100
"commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.cpp",
11011101
"commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.cpp",
11021102
"commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.cpp",
1103+
"commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.cpp",
1104+
"commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.cpp",
11031105
"commands/config/protocol/static/CmdConfigProtocolStatic.cpp",
11041106
"commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.cpp",
11051107
"commands/config/ptp/CmdConfigPtp.cpp",
@@ -1149,6 +1151,8 @@ cpp_library(
11491151
"commands/delete/protocol/bgp/CmdDeleteProtocolBgp.cpp",
11501152
"commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.cpp",
11511153
"commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.cpp",
1154+
"commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.cpp",
1155+
"commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.cpp",
11521156
"commands/delete/protocol/static/CmdDeleteProtocolStatic.cpp",
11531157
"commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.cpp",
11541158
"commands/delete/qos/CmdDeleteQos.cpp",
@@ -1207,6 +1211,9 @@ cpp_library(
12071211
"commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.h",
12081212
"commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.h",
12091213
"commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.h",
1214+
"commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h",
1215+
"commands/config/protocol/bgp/policy/as-path-list/BgpAsPathListCliUtils.h",
1216+
"commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.h",
12101217
"commands/config/protocol/static/CmdConfigProtocolStatic.h",
12111218
"commands/config/protocol/static/route/StaticRouteUtils.h",
12121219
"commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.h",
@@ -1257,6 +1264,8 @@ cpp_library(
12571264
"commands/delete/protocol/bgp/CmdDeleteProtocolBgp.h",
12581265
"commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h",
12591266
"commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h",
1267+
"commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h",
1268+
"commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h",
12601269
"commands/delete/protocol/static/CmdDeleteProtocolStatic.h",
12611270
"commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.h",
12621271
"commands/delete/qos/CmdDeleteQos.h",

fboss/cli/fboss2/CmdListConfig.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#include "fboss/cli/fboss2/commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.h"
4141
#include "fboss/cli/fboss2/commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.h"
4242
#include "fboss/cli/fboss2/commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.h"
43+
#include "fboss/cli/fboss2/commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h"
44+
#include "fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.h"
4345
#include "fboss/cli/fboss2/commands/config/protocol/static/CmdConfigProtocolStatic.h"
4446
#include "fboss/cli/fboss2/commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.h"
4547
#include "fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h"
@@ -87,6 +89,8 @@
8789
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/CmdDeleteProtocolBgp.h"
8890
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h"
8991
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h"
92+
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h"
93+
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h"
9094
#include "fboss/cli/fboss2/commands/delete/protocol/static/CmdDeleteProtocolStatic.h"
9195
#include "fboss/cli/fboss2/commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.h"
9296
#include "fboss/cli/fboss2/commands/delete/qos/CmdDeleteQos.h"
@@ -378,6 +382,21 @@ const CommandTree& kConfigCommandTree() {
378382
commandHandler<CmdConfigProtocolBgpNeighbor>,
379383
argRegistrar<CmdConfigProtocolBgpNeighborTraits>,
380384
},
385+
{
386+
"policy",
387+
"Configure BGP policy objects",
388+
commandHandler<CmdConfigProtocolBgpPolicy>,
389+
argRegistrar<CmdConfigProtocolBgpPolicyTraits>,
390+
{{
391+
"as-path-list",
392+
"Configure BGP AS-path list: <name> "
393+
"[<attribute> <value> ...] (description)",
394+
commandHandler<
395+
CmdConfigProtocolBgpPolicyAsPathList>,
396+
argRegistrar<
397+
CmdConfigProtocolBgpPolicyAsPathListTraits>,
398+
}},
399+
},
381400
},
382401
},
383402
{
@@ -674,6 +693,19 @@ const CommandTree& kConfigCommandTree() {
674693
"Delete a BGP peer-group: <name>",
675694
commandHandler<CmdDeleteProtocolBgpPeerGroup>,
676695
argRegistrar<CmdDeleteProtocolBgpPeerGroupTraits>,
696+
},
697+
{
698+
"policy",
699+
"Delete BGP policy objects",
700+
commandHandler<CmdDeleteProtocolBgpPolicy>,
701+
argTypeHandler<CmdDeleteProtocolBgpPolicyTraits>,
702+
{{
703+
"as-path-list",
704+
"Delete a BGP AS-path list: <name>",
705+
commandHandler<CmdDeleteProtocolBgpPolicyAsPathList>,
706+
argRegistrar<
707+
CmdDeleteProtocolBgpPolicyAsPathListTraits>,
708+
}},
677709
}},
678710
},
679711
{
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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/CmdConfigProtocolBgpPolicy.h"
12+
13+
#include "fboss/cli/fboss2/CmdHandler.cpp"
14+
15+
namespace facebook::fboss {
16+
17+
CmdConfigProtocolBgpPolicyTraits::RetType
18+
CmdConfigProtocolBgpPolicy::queryClient(const HostInfo& /* hostInfo */) {
19+
return "BGP policy configuration. Use subcommands: as-path-list";
20+
}
21+
22+
void CmdConfigProtocolBgpPolicy::printOutput(const RetType& output) {
23+
std::cout << output << std::endl;
24+
}
25+
26+
// Explicit template instantiation
27+
template void
28+
CmdHandler<CmdConfigProtocolBgpPolicy, CmdConfigProtocolBgpPolicyTraits>::run();
29+
30+
} // namespace facebook::fboss
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 "fboss/cli/fboss2/CmdHandler.h"
14+
#include "fboss/cli/fboss2/commands/config/protocol/bgp/CmdConfigProtocolBgp.h"
15+
16+
namespace facebook::fboss {
17+
18+
// Grouping node for `config protocol bgp policy <object-type> ...`. Holds no
19+
// state of its own; the per-object-type dispatchers (as-path-list, and later
20+
// community-list/prefix-list/routing-policy) are its subcommands. Mirrors
21+
// CmdConfigProtocolBgp.
22+
struct CmdConfigProtocolBgpPolicyTraits : public WriteCommandTraits {
23+
using ParentCmd = CmdConfigProtocolBgp;
24+
using ObjectArgType = std::monostate;
25+
using RetType = std::string;
26+
};
27+
28+
class CmdConfigProtocolBgpPolicy : public CmdHandler<
29+
CmdConfigProtocolBgpPolicy,
30+
CmdConfigProtocolBgpPolicyTraits> {
31+
public:
32+
using ObjectArgType = CmdConfigProtocolBgpPolicyTraits::ObjectArgType;
33+
using RetType = CmdConfigProtocolBgpPolicyTraits::RetType;
34+
35+
RetType queryClient(const HostInfo& hostInfo);
36+
37+
void printOutput(const RetType& output);
38+
};
39+
40+
} // namespace facebook::fboss
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 as-path-list CLI family, shared between the
21+
* list-level dispatcher (CmdConfigProtocolBgpPolicyAsPathList), the entry
22+
* subcommand (CmdConfigProtocolBgpPolicyAsPathListEntry), and the delete
23+
* counterparts. An AsPathList is keyed by name; an AsPathListEntry (in
24+
* as_path_list[]) is keyed by sequence_number.
25+
*/
26+
namespace facebook::fboss::bgpcli {
27+
28+
inline bool asPathListExists(
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()->aspath_lists();
35+
return std::any_of(lists.begin(), lists.end(), [&](const auto& list) {
36+
return *list.name() == name;
37+
});
38+
}
39+
40+
// Find the as-path-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 `as-path-list <name>` creates one
43+
// explicitly. AsPathList's only key field is `name`.
44+
inline bgp::bgp_policy::AsPathList& findOrCreateAsPathList(
45+
bgp::thrift::BgpConfig& cfg,
46+
const std::string& name) {
47+
auto& lists = *cfg.policies().ensure().aspath_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 asPathListEntryExists(
60+
const bgp::bgp_policy::AsPathList& list,
61+
int64_t seqNum) {
62+
const auto& entries = *list.as_path_list();
63+
return std::any_of(entries.begin(), entries.end(), [&](const auto& entry) {
64+
return entry.sequence_number().has_value() &&
65+
*entry.sequence_number() == seqNum;
66+
});
67+
}
68+
69+
// Find the entry keyed by sequence_number within a list, creating it if
70+
// absent. sequence_number is the entry's identity.
71+
inline bgp::bgp_policy::AsPathListEntry& findOrCreateAsPathListEntry(
72+
bgp::bgp_policy::AsPathList& list,
73+
int64_t seqNum) {
74+
auto& entries = *list.as_path_list();
75+
for (auto& entry : entries) {
76+
if (entry.sequence_number().has_value() &&
77+
*entry.sequence_number() == seqNum) {
78+
return entry;
79+
}
80+
}
81+
entries.emplace_back();
82+
auto& entry = entries.back();
83+
entry.sequence_number() = seqNum;
84+
return entry;
85+
}
86+
87+
} // namespace facebook::fboss::bgpcli

0 commit comments

Comments
 (0)