Skip to content

Commit 72662c2

Browse files
[Nexthop] 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 210fe06 commit 72662c2

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
@@ -882,6 +882,11 @@ add_library(fboss2_config_lib
882882
fboss/cli/fboss2/commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.h
883883
fboss/cli/fboss2/commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.cpp
884884
fboss/cli/fboss2/commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.h
885+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.cpp
886+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h
887+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/BgpAsPathListCliUtils.h
888+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.cpp
889+
fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.h
885890
fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.cpp
886891
fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h
887892
fboss/cli/fboss2/commands/config/ptp/transparent_clock/CmdConfigPtpTransparentClock.cpp
@@ -990,6 +995,10 @@ add_library(fboss2_config_lib
990995
fboss/cli/fboss2/commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h
991996
fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.cpp
992997
fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h
998+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.cpp
999+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h
1000+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.cpp
1001+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h
9931002
fboss/cli/fboss2/commands/delete/protocol/CmdDeleteProtocol.h
9941003
fboss/cli/fboss2/commands/delete/protocol/static/CmdDeleteProtocolStatic.cpp
9951004
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
@@ -1108,6 +1108,8 @@ cpp_library(
11081108
"commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.cpp",
11091109
"commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.cpp",
11101110
"commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.cpp",
1111+
"commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.cpp",
1112+
"commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.cpp",
11111113
"commands/config/protocol/static/CmdConfigProtocolStatic.cpp",
11121114
"commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.cpp",
11131115
"commands/config/ptp/CmdConfigPtp.cpp",
@@ -1162,6 +1164,8 @@ cpp_library(
11621164
"commands/delete/protocol/bgp/CmdDeleteProtocolBgp.cpp",
11631165
"commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.cpp",
11641166
"commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.cpp",
1167+
"commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.cpp",
1168+
"commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.cpp",
11651169
"commands/delete/protocol/static/CmdDeleteProtocolStatic.cpp",
11661170
"commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.cpp",
11671171
"commands/delete/qos/CmdDeleteQos.cpp",
@@ -1222,6 +1226,9 @@ cpp_library(
12221226
"commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.h",
12231227
"commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.h",
12241228
"commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.h",
1229+
"commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h",
1230+
"commands/config/protocol/bgp/policy/as-path-list/BgpAsPathListCliUtils.h",
1231+
"commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.h",
12251232
"commands/config/protocol/static/CmdConfigProtocolStatic.h",
12261233
"commands/config/protocol/static/route/StaticRouteUtils.h",
12271234
"commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.h",
@@ -1277,6 +1284,8 @@ cpp_library(
12771284
"commands/delete/protocol/bgp/CmdDeleteProtocolBgp.h",
12781285
"commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h",
12791286
"commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h",
1287+
"commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h",
1288+
"commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h",
12801289
"commands/delete/protocol/static/CmdDeleteProtocolStatic.h",
12811290
"commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.h",
12821291
"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"
@@ -90,6 +92,8 @@
9092
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/CmdDeleteProtocolBgp.h"
9193
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h"
9294
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h"
95+
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h"
96+
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h"
9397
#include "fboss/cli/fboss2/commands/delete/protocol/static/CmdDeleteProtocolStatic.h"
9498
#include "fboss/cli/fboss2/commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.h"
9599
#include "fboss/cli/fboss2/commands/delete/qos/CmdDeleteQos.h"
@@ -383,6 +387,21 @@ const CommandTree& kConfigCommandTree() {
383387
commandHandler<CmdConfigProtocolBgpNeighbor>,
384388
argRegistrar<CmdConfigProtocolBgpNeighborTraits>,
385389
},
390+
{
391+
"policy",
392+
"Configure BGP policy objects",
393+
commandHandler<CmdConfigProtocolBgpPolicy>,
394+
argRegistrar<CmdConfigProtocolBgpPolicyTraits>,
395+
{{
396+
"as-path-list",
397+
"Configure BGP AS-path list: <name> "
398+
"[<attribute> <value> ...] (description)",
399+
commandHandler<
400+
CmdConfigProtocolBgpPolicyAsPathList>,
401+
argRegistrar<
402+
CmdConfigProtocolBgpPolicyAsPathListTraits>,
403+
}},
404+
},
386405
},
387406
},
388407
{
@@ -711,6 +730,19 @@ const CommandTree& kConfigCommandTree() {
711730
"Delete a BGP peer-group: <name>",
712731
commandHandler<CmdDeleteProtocolBgpPeerGroup>,
713732
argRegistrar<CmdDeleteProtocolBgpPeerGroupTraits>,
733+
},
734+
{
735+
"policy",
736+
"Delete BGP policy objects",
737+
commandHandler<CmdDeleteProtocolBgpPolicy>,
738+
argTypeHandler<CmdDeleteProtocolBgpPolicyTraits>,
739+
{{
740+
"as-path-list",
741+
"Delete a BGP AS-path list: <name>",
742+
commandHandler<CmdDeleteProtocolBgpPolicyAsPathList>,
743+
argRegistrar<
744+
CmdDeleteProtocolBgpPolicyAsPathListTraits>,
745+
}},
714746
}},
715747
},
716748
{
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)