Skip to content

Commit 9c84065

Browse files
NOS-6643..NOS-6648: fboss2 config/delete protocol bgp policy as-path-list
1 parent c868417 commit 9c84065

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
@@ -968,6 +973,10 @@ add_library(fboss2_config_lib
968973
fboss/cli/fboss2/commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h
969974
fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.cpp
970975
fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h
976+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.cpp
977+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h
978+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.cpp
979+
fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h
971980
fboss/cli/fboss2/commands/delete/protocol/CmdDeleteProtocol.h
972981
fboss/cli/fboss2/commands/delete/protocol/static/CmdDeleteProtocolStatic.cpp
973982
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
@@ -1101,6 +1101,8 @@ cpp_library(
11011101
"commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.cpp",
11021102
"commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.cpp",
11031103
"commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.cpp",
1104+
"commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.cpp",
1105+
"commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.cpp",
11041106
"commands/config/protocol/static/CmdConfigProtocolStatic.cpp",
11051107
"commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.cpp",
11061108
"commands/config/ptp/CmdConfigPtp.cpp",
@@ -1145,6 +1147,8 @@ cpp_library(
11451147
"commands/delete/protocol/bgp/CmdDeleteProtocolBgp.cpp",
11461148
"commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.cpp",
11471149
"commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.cpp",
1150+
"commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.cpp",
1151+
"commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.cpp",
11481152
"commands/delete/protocol/static/CmdDeleteProtocolStatic.cpp",
11491153
"commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.cpp",
11501154
"commands/delete/qos/CmdDeleteQos.cpp",
@@ -1201,6 +1205,9 @@ cpp_library(
12011205
"commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.h",
12021206
"commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.h",
12031207
"commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.h",
1208+
"commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h",
1209+
"commands/config/protocol/bgp/policy/as-path-list/BgpAsPathListCliUtils.h",
1210+
"commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.h",
12041211
"commands/config/protocol/static/CmdConfigProtocolStatic.h",
12051212
"commands/config/protocol/static/route/StaticRouteUtils.h",
12061213
"commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.h",
@@ -1246,6 +1253,8 @@ cpp_library(
12461253
"commands/delete/protocol/bgp/CmdDeleteProtocolBgp.h",
12471254
"commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h",
12481255
"commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h",
1256+
"commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h",
1257+
"commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h",
12491258
"commands/delete/protocol/static/CmdDeleteProtocolStatic.h",
12501259
"commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.h",
12511260
"commands/delete/qos/CmdDeleteQos.h",

fboss/cli/fboss2/CmdListConfig.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#include "fboss/cli/fboss2/commands/config/protocol/bgp/global/CmdConfigProtocolBgpGlobal.h"
4242
#include "fboss/cli/fboss2/commands/config/protocol/bgp/neighbor/CmdConfigProtocolBgpNeighbor.h"
4343
#include "fboss/cli/fboss2/commands/config/protocol/bgp/peer-group/CmdConfigProtocolBgpPeerGroup.h"
44+
#include "fboss/cli/fboss2/commands/config/protocol/bgp/policy/CmdConfigProtocolBgpPolicy.h"
45+
#include "fboss/cli/fboss2/commands/config/protocol/bgp/policy/as-path-list/CmdConfigProtocolBgpPolicyAsPathList.h"
4446
#include "fboss/cli/fboss2/commands/config/protocol/static/CmdConfigProtocolStatic.h"
4547
#include "fboss/cli/fboss2/commands/config/protocol/static/route/add/CmdConfigProtocolStaticRouteAdd.h"
4648
#include "fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h"
@@ -84,6 +86,8 @@
8486
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/CmdDeleteProtocolBgp.h"
8587
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/neighbor/CmdDeleteProtocolBgpNeighbor.h"
8688
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/peer-group/CmdDeleteProtocolBgpPeerGroup.h"
89+
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/policy/CmdDeleteProtocolBgpPolicy.h"
90+
#include "fboss/cli/fboss2/commands/delete/protocol/bgp/policy/as-path-list/CmdDeleteProtocolBgpPolicyAsPathList.h"
8791
#include "fboss/cli/fboss2/commands/delete/protocol/static/CmdDeleteProtocolStatic.h"
8892
#include "fboss/cli/fboss2/commands/delete/protocol/static/route/CmdDeleteProtocolStaticRoute.h"
8993
#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
{
@@ -656,6 +675,19 @@ const CommandTree& kConfigCommandTree() {
656675
"Delete a BGP peer-group: <name>",
657676
commandHandler<CmdDeleteProtocolBgpPeerGroup>,
658677
argRegistrar<CmdDeleteProtocolBgpPeerGroupTraits>,
678+
},
679+
{
680+
"policy",
681+
"Delete BGP policy objects",
682+
commandHandler<CmdDeleteProtocolBgpPolicy>,
683+
argTypeHandler<CmdDeleteProtocolBgpPolicyTraits>,
684+
{{
685+
"as-path-list",
686+
"Delete a BGP AS-path list: <name>",
687+
commandHandler<CmdDeleteProtocolBgpPolicyAsPathList>,
688+
argRegistrar<
689+
CmdDeleteProtocolBgpPolicyAsPathListTraits>,
690+
}},
659691
}},
660692
},
661693
{
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)