|
| 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 | +// Accepted spellings of cfg::CounterType, matched case insensitively. |
| 25 | +constexpr std::string_view kTypePackets = "packets"; |
| 26 | +constexpr std::string_view kTypeBytes = "bytes"; |
| 27 | +constexpr std::string_view kTypeList = "PACKETS,BYTES"; |
| 28 | + |
| 29 | +std::string typesToString(const std::vector<cfg::CounterType>& types) { |
| 30 | + std::vector<std::string> names; |
| 31 | + names.reserve(types.size()); |
| 32 | + for (auto type : types) { |
| 33 | + names.push_back( |
| 34 | + type == cfg::CounterType::BYTES ? std::string("BYTES") |
| 35 | + : std::string("PACKETS")); |
| 36 | + } |
| 37 | + return folly::join(",", names); |
| 38 | +} |
| 39 | +} // namespace |
| 40 | + |
| 41 | +TrafficCounterArg::TrafficCounterArg(std::vector<std::string> v) { |
| 42 | + if (v.size() != 2) { |
| 43 | + throw std::invalid_argument( |
| 44 | + fmt::format( |
| 45 | + "Expected <name> <types>, where types is a comma separated list of {}", |
| 46 | + kTypeList)); |
| 47 | + } |
| 48 | + |
| 49 | + name_ = v[0]; |
| 50 | + if (name_.empty()) { |
| 51 | + throw std::invalid_argument("Counter name must not be empty"); |
| 52 | + } |
| 53 | + data_.push_back(v[0]); |
| 54 | + |
| 55 | + std::vector<std::string> tokens; |
| 56 | + folly::split(',', v[1], tokens); |
| 57 | + bool hasPackets = false; |
| 58 | + bool hasBytes = false; |
| 59 | + for (auto token : tokens) { |
| 60 | + folly::toLowerAscii(token); |
| 61 | + if (token == kTypePackets) { |
| 62 | + hasPackets = true; |
| 63 | + } else if (token == kTypeBytes) { |
| 64 | + hasBytes = true; |
| 65 | + } else { |
| 66 | + throw std::invalid_argument( |
| 67 | + fmt::format( |
| 68 | + "Invalid counter type '{}'. Expected a comma separated list of {}", |
| 69 | + token, |
| 70 | + kTypeList)); |
| 71 | + } |
| 72 | + } |
| 73 | + // Store in a fixed order so the config, and the already-configured |
| 74 | + // comparison below, do not depend on the order the types were typed in. |
| 75 | + if (hasPackets) { |
| 76 | + types_.push_back(cfg::CounterType::PACKETS); |
| 77 | + } |
| 78 | + if (hasBytes) { |
| 79 | + types_.push_back(cfg::CounterType::BYTES); |
| 80 | + } |
| 81 | + data_.push_back(v[1]); |
| 82 | +} |
| 83 | + |
| 84 | +CmdConfigTrafficCounterTraits::RetType CmdConfigTrafficCounter::queryClient( |
| 85 | + const HostInfo& /* hostInfo */, |
| 86 | + const ObjectArgType& trafficCounter) { |
| 87 | + auto& session = ConfigSession::getInstance(); |
| 88 | + auto& config = session.getAgentConfig(); |
| 89 | + auto& swConfig = *config.sw(); |
| 90 | + |
| 91 | + const auto& name = trafficCounter.getName(); |
| 92 | + const auto& newTypes = trafficCounter.getTypes(); |
| 93 | + |
| 94 | + auto& counters = *swConfig.trafficCounters(); |
| 95 | + auto it = std::find_if( |
| 96 | + counters.begin(), counters.end(), [&](const cfg::TrafficCounter& c) { |
| 97 | + return *c.name() == name; |
| 98 | + }); |
| 99 | + |
| 100 | + if (it != counters.end()) { |
| 101 | + if (*it->types() == newTypes) { |
| 102 | + return fmt::format( |
| 103 | + "Traffic counter '{}' is already configured with type '{}'", |
| 104 | + name, |
| 105 | + typesToString(newTypes)); |
| 106 | + } |
| 107 | + *it->types() = newTypes; |
| 108 | + session.saveConfig( |
| 109 | + cli::ServiceType::AGENT, cli::ConfigActionLevel::HITLESS); |
| 110 | + return fmt::format( |
| 111 | + "Successfully updated traffic counter '{}' to type '{}'", |
| 112 | + name, |
| 113 | + typesToString(newTypes)); |
| 114 | + } |
| 115 | + |
| 116 | + cfg::TrafficCounter newCounter; |
| 117 | + newCounter.name() = name; |
| 118 | + newCounter.types() = newTypes; |
| 119 | + counters.push_back(std::move(newCounter)); |
| 120 | + |
| 121 | + session.saveConfig(cli::ServiceType::AGENT, cli::ConfigActionLevel::HITLESS); |
| 122 | + |
| 123 | + return fmt::format( |
| 124 | + "Successfully created traffic counter '{}' with type '{}'", |
| 125 | + name, |
| 126 | + typesToString(newTypes)); |
| 127 | +} |
| 128 | + |
| 129 | +void CmdConfigTrafficCounter::printOutput(const RetType& output) { |
| 130 | + std::cout << output << std::endl; |
| 131 | +} |
| 132 | + |
| 133 | +// Explicit template instantiation |
| 134 | +template void |
| 135 | +CmdHandler<CmdConfigTrafficCounter, CmdConfigTrafficCounterTraits>::run(); |
| 136 | + |
| 137 | +} // namespace facebook::fboss |
0 commit comments