|
| 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/traffic_counter/CmdConfigTrafficCounter.h" |
| 12 | + |
| 13 | +#include "fboss/cli/fboss2/CmdHandler.cpp" |
| 14 | + |
| 15 | +#include <fmt/format.h> |
| 16 | +#include <folly/String.h> |
| 17 | +#include <algorithm> |
| 18 | +#include <iostream> |
| 19 | +#include "fboss/cli/fboss2/session/ConfigSession.h" |
| 20 | + |
| 21 | +namespace facebook::fboss { |
| 22 | + |
| 23 | +namespace { |
| 24 | +constexpr std::string_view kTypePackets = "packets"; |
| 25 | +constexpr std::string_view kTypeBytes = "bytes"; |
| 26 | +constexpr std::string_view kTypeBoth = "both"; |
| 27 | + |
| 28 | +std::string typesToString(const std::vector<cfg::CounterType>& types) { |
| 29 | + bool hasPackets = |
| 30 | + std::find(types.begin(), types.end(), cfg::CounterType::PACKETS) != |
| 31 | + types.end(); |
| 32 | + bool hasBytes = |
| 33 | + std::find(types.begin(), types.end(), cfg::CounterType::BYTES) != |
| 34 | + types.end(); |
| 35 | + if (hasPackets && hasBytes) { |
| 36 | + return std::string(kTypeBoth); |
| 37 | + } |
| 38 | + if (hasBytes) { |
| 39 | + return std::string(kTypeBytes); |
| 40 | + } |
| 41 | + return std::string(kTypePackets); |
| 42 | +} |
| 43 | +} // namespace |
| 44 | + |
| 45 | +TrafficCounterArg::TrafficCounterArg(std::vector<std::string> v) { |
| 46 | + if (v.empty()) { |
| 47 | + throw std::invalid_argument( |
| 48 | + "Counter name is required, followed by an optional type (packets|bytes|both)"); |
| 49 | + } |
| 50 | + if (v.size() > 2) { |
| 51 | + throw std::invalid_argument( |
| 52 | + "Expected <name> [packets|bytes|both]; got too many arguments"); |
| 53 | + } |
| 54 | + |
| 55 | + name_ = v[0]; |
| 56 | + if (name_.empty()) { |
| 57 | + throw std::invalid_argument("Counter name must not be empty"); |
| 58 | + } |
| 59 | + data_.push_back(v[0]); |
| 60 | + |
| 61 | + if (v.size() == 2) { |
| 62 | + std::string typeStr = v[1]; |
| 63 | + folly::toLowerAscii(typeStr); |
| 64 | + if (typeStr == kTypePackets) { |
| 65 | + types_ = {cfg::CounterType::PACKETS}; |
| 66 | + } else if (typeStr == kTypeBytes) { |
| 67 | + types_ = {cfg::CounterType::BYTES}; |
| 68 | + } else if (typeStr == kTypeBoth) { |
| 69 | + types_ = {cfg::CounterType::PACKETS, cfg::CounterType::BYTES}; |
| 70 | + } else { |
| 71 | + throw std::invalid_argument( |
| 72 | + "Invalid counter type '" + v[1] + |
| 73 | + "'. Expected 'packets', 'bytes', or 'both'"); |
| 74 | + } |
| 75 | + data_.push_back(v[1]); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +CmdConfigTrafficCounterTraits::RetType CmdConfigTrafficCounter::queryClient( |
| 80 | + const HostInfo& /* hostInfo */, |
| 81 | + const ObjectArgType& trafficCounter) { |
| 82 | + auto& session = ConfigSession::getInstance(); |
| 83 | + auto& config = session.getAgentConfig(); |
| 84 | + auto& swConfig = *config.sw(); |
| 85 | + |
| 86 | + const auto& name = trafficCounter.getName(); |
| 87 | + const auto& newTypes = trafficCounter.getTypes(); |
| 88 | + |
| 89 | + auto& counters = *swConfig.trafficCounters(); |
| 90 | + auto it = std::find_if( |
| 91 | + counters.begin(), counters.end(), [&](const cfg::TrafficCounter& c) { |
| 92 | + return *c.name() == name; |
| 93 | + }); |
| 94 | + |
| 95 | + if (it != counters.end()) { |
| 96 | + if (*it->types() == newTypes) { |
| 97 | + return fmt::format( |
| 98 | + "Traffic counter '{}' is already configured with type '{}'", |
| 99 | + name, |
| 100 | + typesToString(newTypes)); |
| 101 | + } |
| 102 | + *it->types() = newTypes; |
| 103 | + session.saveConfig( |
| 104 | + cli::ServiceType::AGENT, cli::ConfigActionLevel::HITLESS); |
| 105 | + return fmt::format( |
| 106 | + "Successfully updated traffic counter '{}' to type '{}'", |
| 107 | + name, |
| 108 | + typesToString(newTypes)); |
| 109 | + } |
| 110 | + |
| 111 | + cfg::TrafficCounter newCounter; |
| 112 | + newCounter.name() = name; |
| 113 | + newCounter.types() = newTypes; |
| 114 | + counters.push_back(std::move(newCounter)); |
| 115 | + |
| 116 | + session.saveConfig(cli::ServiceType::AGENT, cli::ConfigActionLevel::HITLESS); |
| 117 | + |
| 118 | + return fmt::format( |
| 119 | + "Successfully created traffic counter '{}' with type '{}'", |
| 120 | + name, |
| 121 | + typesToString(newTypes)); |
| 122 | +} |
| 123 | + |
| 124 | +void CmdConfigTrafficCounter::printOutput(const RetType& output) { |
| 125 | + std::cout << output << std::endl; |
| 126 | +} |
| 127 | + |
| 128 | +// Explicit template instantiation |
| 129 | +template void |
| 130 | +CmdHandler<CmdConfigTrafficCounter, CmdConfigTrafficCounterTraits>::run(); |
| 131 | + |
| 132 | +} // namespace facebook::fboss |
0 commit comments