Skip to content

Commit

Permalink
Change hash map to vector of pair to increase readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
PINS Team authored and divyagayathri-hcl committed Aug 8, 2024
1 parent fc947e8 commit c34c6a7
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 237 deletions.
60 changes: 0 additions & 60 deletions lib/gnmi/gnmi_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include "absl/algorithm/container.h"
#include "absl/base/log_severity.h"
#include "absl/container/btree_map.h"
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
Expand Down Expand Up @@ -1653,65 +1652,6 @@ absl::StatusOr<bool> CheckLinkUp(const std::string& interface_name,
return ops_response == "\"UP\"";
}

absl::StatusOr<std::string> AppendSflowConfigIfNotPresent(
absl::string_view gnmi_config, absl::string_view agent_addr_ipv6,
const absl::flat_hash_map<std::string, int>& collector_address_to_port,
const absl::flat_hash_set<std::string>& sflow_enabled_interfaces,
const int sampling_rate, const int sampling_header_size) {
ASSIGN_OR_RETURN(auto gnmi_config_json, json_yang::ParseJson(gnmi_config));
if (gnmi_config_json.find("openconfig-sampling:sampling") !=
gnmi_config_json.end()) {
return std::string(gnmi_config);
}
if (agent_addr_ipv6.empty()) {
return absl::FailedPreconditionError(
"loopback_address parameter cannot be empty.");
}
if (sflow_enabled_interfaces.empty()) {
return absl::FailedPreconditionError(
"sflow_enabled_interfaces parameter cannot be empty.");
}
gnmi_config_json["openconfig-sampling:sampling"]
["openconfig-sampling-sflow:sflow"]["config"]["enabled"] =
true;
gnmi_config_json["openconfig-sampling:sampling"]
["openconfig-sampling-sflow:sflow"]["config"]["sample-size"] =
sampling_header_size;
gnmi_config_json["openconfig-sampling:sampling"]
["openconfig-sampling-sflow:sflow"]["config"]
["polling-interval"] = 0;
gnmi_config_json["openconfig-sampling:sampling"]
["openconfig-sampling-sflow:sflow"]["config"]
["agent-id-ipv6"] = agent_addr_ipv6;
absl::btree_map<std::string, int> sorted_collector_addresses(
collector_address_to_port.begin(), collector_address_to_port.end());
for (const auto& [address, port] : sorted_collector_addresses) {
nlohmann::basic_json<> sflow_collector_config;
sflow_collector_config["address"] = address;
sflow_collector_config["port"] = port;
sflow_collector_config["config"]["address"] = address;
sflow_collector_config["config"]["port"] = port;
gnmi_config_json["openconfig-sampling:sampling"]
["openconfig-sampling-sflow:sflow"]["collectors"]
["collector"]
.push_back(sflow_collector_config);
}
absl::btree_set<std::string> sorted_interface_names(
sflow_enabled_interfaces.begin(), sflow_enabled_interfaces.end());
for (const auto& interface_name : sorted_interface_names) {
nlohmann::basic_json<> sflow_interface_config;
sflow_interface_config["name"] = interface_name;
sflow_interface_config["config"]["name"] = interface_name;
sflow_interface_config["config"]["enabled"] = true;
sflow_interface_config["config"]["ingress-sampling-rate"] = sampling_rate;
gnmi_config_json["openconfig-sampling:sampling"]
["openconfig-sampling-sflow:sflow"]["interfaces"]
["interface"]
.push_back(sflow_interface_config);
}
return gnmi_config_json.dump();
}

absl::StatusOr<absl::flat_hash_map<std::string, Counters>>
GetAllInterfaceCounters(gnmi::gNMI::StubInterface& gnmi_stub) {
ASSIGN_OR_RETURN(
Expand Down
11 changes: 0 additions & 11 deletions lib/gnmi/gnmi_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,17 +421,6 @@ absl::Status SetPortLoopbackMode(bool port_loopback,
absl::string_view interface_name,
gnmi::gNMI::StubInterface& gnmi_stub);

// Appends sFlow config to `gnmi_config` and returns modified config if success.
// The modified config would sort collector IPs and interface names by string
// order. This function would return original `gnmi_config` if sFlow config is
// already present in `gnmi_config`. Returns an FailedPreconditionError if
// `agent_addr_ipv6` or `sflow_enabled_interfaces` is empty.
absl::StatusOr<std::string> AppendSflowConfigIfNotPresent(
absl::string_view gnmi_config, absl::string_view agent_addr_ipv6,
const absl::flat_hash_map<std::string, int>& collector_address_to_port,
const absl::flat_hash_set<std::string>& sflow_enabled_interfaces,
const int sampling_rate, const int sampling_header_size);

// Gets counters for all interfaces.
absl::StatusOr<absl::flat_hash_map<std::string, Counters>>
GetAllInterfaceCounters(gnmi::gNMI::StubInterface& gnmi_stub);
Expand Down
166 changes: 0 additions & 166 deletions lib/gnmi/gnmi_helper_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2276,172 +2276,6 @@ TEST(BreakoutModeMatchTest, LocalFileTestDataTest) {
IsOkAndHolds(10));
}

TEST(SflowconfigTest, AppendSflowConfigSuccess) {
const std::string kOpenConfig = R"({
"openconfig-interfaces:interfaces":{
"interface":[
{
"name":"bond0",
"state":{"oper-status":"UP","openconfig-p4rt:id":1}
}
]
}
})";
const std::string kSflowConfig = R"({
"openconfig-interfaces:interfaces":{
"interface":[
{
"name":"bond0",
"state":{"oper-status":"UP","openconfig-p4rt:id":1}
}
]
},
"openconfig-sampling:sampling" : {
"openconfig-sampling-sflow:sflow" : {
"collectors" : {
"collector" : [
{
"address" : "2001:4860:f802::be",
"config" : {
"address" : "2001:4860:f802::be",
"port" : 6343
},
"port" : 6343
}
]
},
"config" : {
"agent-id-ipv6" : "8002:12::aab0",
"enabled" : true,
"polling-interval" : 0,
"sample-size" : 128
},
"interfaces" : {
"interface" : [
{
"config" : {
"enabled" : true,
"name" : "Ethernet1",
"ingress-sampling-rate" : 1000
},
"name" : "Ethernet1"
},
{
"config" : {
"enabled" : true,
"name" : "Ethernet2",
"ingress-sampling-rate" : 1000
},
"name" : "Ethernet2"
}
]
}
}
}
})";
ASSERT_OK_AND_ASSIGN(
auto json_str,
AppendSflowConfigIfNotPresent(
kOpenConfig,
/*agent_addr_ipv6=*/"8002:12::aab0",
/*collector_address_to_port=*/{{"2001:4860:f802::be", 6343}},
/*sflow_enabled_interfaces=*/{"Ethernet1", "Ethernet2"},
/*sampling_rate=*/1000,
/*sampling_header_size=*/128));
ASSERT_EQ(::nlohmann::json::parse(json_str),
::nlohmann::json::parse(kSflowConfig));
}

TEST(SflowconfigTest, AppendSflowConfigNoopIfPresent) {
const std::string kGnmiConfig = R"({
"openconfig-interfaces:interfaces":{
"interface":[
{
"name":"bond0",
"state":{"oper-status":"UP","openconfig-p4rt:id":1}
}
]
},
"openconfig-sampling:sampling" : {
"openconfig-sampling-sflow:sflow" : {
"collectors" : {
"collector" : [
{
"address" : "2001:4860:f802::be",
"config" : {
"address" : "2001:4860:f802::be",
"port" : 6343
},
"port" : 6343
}
]
},
"config" : {
"agent-id-ipv6" : "8002:12::aab0",
"enabled" : true,
"polling-interval" : 0,
"sample-size" : 128
},
"interfaces" : {
"interface" : [
{
"config" : {
"enabled" : true,
"name" : "Ethernet1",
"ingress-sampling-rate" : 1000
},
"name" : "Ethernet1"
},
{
"config" : {
"enabled" : true,
"name" : "Ethernet2",
"ingress-sampling-rate" : 1000
},
"name" : "Ethernet2"
}
]
}
}
}
})";
ASSERT_OK_AND_ASSIGN(
auto json_str,
AppendSflowConfigIfNotPresent(
kGnmiConfig,
/*agent_addr_ipv6=*/"8002:12::aab0",
/*collector_address_to_port=*/{{"12:34:56::78", 6343}},
/*sflow_enabled_interfaces=*/{"Ethernet3", "Ethernet4"},
/*sampling_rate=*/100,
/*sampling_header_size=*/12));
ASSERT_EQ(::nlohmann::json::parse(json_str),
::nlohmann::json::parse(kGnmiConfig));
}

TEST(SflowconfigTest, AppendSflowConfigWrongParameterFail) {
EXPECT_THAT(
AppendSflowConfigIfNotPresent(
/*gnmi_config=*/"",
/*agent_addr_ipv6=*/"",
/*collector_address_to_port=*/{{"2001:4860:f802::be", 6343}},
/*sflow_enabled_interfaces=*/{"Ethernet1", "Ethernet2"},
/*sampling_rate=*/1000,
/*sampling_header_size=*/128),
StatusIs(absl::StatusCode::kFailedPrecondition,
HasSubstr("loopback_address parameter cannot be empty.")));
EXPECT_THAT(
AppendSflowConfigIfNotPresent(
/*gnmi_config=*/"",
/*agent_addr_ipv6=*/"8002:12::aab0",
/*collector_address_to_port=*/{{"2001:4860:f802::be", 6343}},
/*sflow_enabled_interfaces=*/{},
/*sampling_rate=*/1000,
/*sampling_header_size=*/128),
StatusIs(
absl::StatusCode::kFailedPrecondition,
HasSubstr("sflow_enabled_interfaces parameter cannot be empty.")));
}

TEST(GetAllInterfaceCounters, Works) {
static constexpr absl::string_view kInterfaceJson = R"(
{
Expand Down

0 comments on commit c34c6a7

Please sign in to comment.