|
| 1 | +#include "Dataplane.h" |
| 2 | +#include "Dynmon.h" |
| 3 | + |
| 4 | +Dataplane::Dataplane(Dynmon &parent, const DataplaneJsonObject &conf) |
| 5 | + : DataplaneBase(parent) { |
| 6 | + // Setting the dataplane name |
| 7 | + if (conf.nameIsSet()) |
| 8 | + name_ = conf.getName(); |
| 9 | + else |
| 10 | + name_ = DEFAULT_NAME; |
| 11 | + |
| 12 | + // Setting the dataplane code |
| 13 | + if (conf.codeIsSet()) |
| 14 | + code_ = conf.getCode(); |
| 15 | + else |
| 16 | + code_ = DEFAULT_CODE; |
| 17 | + |
| 18 | + // Setting the dataplane metrics configuration |
| 19 | + addMetricsList(conf.getMetrics()); |
| 20 | +} |
| 21 | + |
| 22 | +Dataplane::~Dataplane() {} |
| 23 | + |
| 24 | +std::string Dataplane::getName() { |
| 25 | + return name_; |
| 26 | +} |
| 27 | + |
| 28 | +std::string Dataplane::getCode() { |
| 29 | + return code_; |
| 30 | +} |
| 31 | + |
| 32 | +std::shared_ptr<DataplaneMetrics> Dataplane::getMetrics(const std::string &name) { |
| 33 | + for (auto &it : metrics_) |
| 34 | + if (it->getName() == name) |
| 35 | + return it; |
| 36 | + throw std::runtime_error("The metric does not exist"); |
| 37 | +} |
| 38 | + |
| 39 | +std::vector<std::shared_ptr<DataplaneMetrics>> Dataplane::getMetricsList() { |
| 40 | + return metrics_; |
| 41 | +} |
| 42 | + |
| 43 | +void Dataplane::addMetrics(const std::string &name, const DataplaneMetricsJsonObject &conf) { |
| 44 | + auto entry = std::make_shared<DataplaneMetrics>(*this, conf); |
| 45 | + if (entry == nullptr) |
| 46 | + throw std::runtime_error("This should not happen"); |
| 47 | + for (auto &it : metrics_) |
| 48 | + if (it->getName() == entry->getName()) |
| 49 | + throw std::runtime_error("Cannot insert duplicate metrics entry"); |
| 50 | + metrics_.push_back(entry); |
| 51 | +} |
| 52 | + |
| 53 | +void Dataplane::addMetricsList(const std::vector<DataplaneMetricsJsonObject> &conf) { |
| 54 | + DataplaneBase::addMetricsList(conf); |
| 55 | +} |
| 56 | + |
| 57 | +void Dataplane::replaceMetrics(const std::string &name, const DataplaneMetricsJsonObject &conf) { |
| 58 | + DataplaneBase::replaceMetrics(name, conf); |
| 59 | +} |
| 60 | + |
| 61 | +void Dataplane::delMetrics(const std::string &name) { |
| 62 | + metrics_.erase( |
| 63 | + std::remove_if(metrics_.begin(), metrics_.end(), |
| 64 | + [name](const std::shared_ptr<DataplaneMetrics> &entry) { |
| 65 | + return entry->getName() == name; |
| 66 | + }), |
| 67 | + metrics_.end()); |
| 68 | +} |
| 69 | + |
| 70 | +void Dataplane::delMetricsList() { |
| 71 | + metrics_.clear(); |
| 72 | +} |
0 commit comments