|
| 1 | +#include "redis_utils.h" |
| 2 | + |
| 3 | +#include <sstream> |
| 4 | +#include <string> |
| 5 | +#include <unordered_map> |
| 6 | +#include <unordered_set> |
| 7 | + |
| 8 | +#include "dbconnector.h" |
| 9 | +#include "notificationproducer.h" |
| 10 | +//#include "stateverification.h" |
| 11 | +#include "table.h" |
| 12 | +#include "timestamp.h" |
| 13 | +#include "warm_restart.h" |
| 14 | + |
| 15 | +namespace rebootbackend { |
| 16 | + |
| 17 | +using WarmStartState = ::swss::WarmStart::WarmStartState; |
| 18 | + |
| 19 | + |
| 20 | +void init_warm_reboot_states(const swss::DBConnector &db) { |
| 21 | + swss::Table table(&db, STATE_WARM_RESTART_TABLE_NAME); |
| 22 | + std::vector<std::string> keys; |
| 23 | + |
| 24 | + table.getKeys(keys); |
| 25 | + for (auto &key : keys) { |
| 26 | + table.hdel(key, "state"); |
| 27 | + table.hdel(key, "timestamp"); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +void set_warm_restart_enable(const swss::DBConnector &db, bool enabled) { |
| 32 | + swss::Table table(&db, STATE_WARM_RESTART_ENABLE_TABLE_NAME); |
| 33 | + table.hset("system", "enable", enabled ? "true" : "false"); |
| 34 | +} |
| 35 | + |
| 36 | +bool is_valid_key(const std::string &key, const std::string &separator) { |
| 37 | + if (separator.empty()) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + size_t pos = key.find(separator); |
| 42 | + // The separator must exist in the string, and cannot be the first or last |
| 43 | + // character. |
| 44 | + return !(pos == std::string::npos || pos == 0 || pos == key.size() - 1); |
| 45 | +} |
| 46 | + |
| 47 | +bool get_docker_app_from_key(const std::string &key, |
| 48 | + const std::string &separator, std::string &docker, |
| 49 | + std::string &app) { |
| 50 | + SWSS_LOG_ENTER(); |
| 51 | + |
| 52 | + size_t pos = key.find(separator); |
| 53 | + |
| 54 | + if (separator.empty()) { |
| 55 | + SWSS_LOG_ERROR("separator [%s] shouldn't be empty", separator.c_str()); |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + if (pos == std::string::npos) { |
| 60 | + SWSS_LOG_ERROR("key [%s] should contain separator [%s]", key.c_str(), |
| 61 | + separator.c_str()); |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + docker = key.substr(0, pos); |
| 66 | + app = key.substr(pos + separator.length(), std::string::npos); |
| 67 | + |
| 68 | + if (docker.empty()) { |
| 69 | + SWSS_LOG_ERROR("docker name shouldn't be empty, key = %s", key.c_str()); |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + if (app.empty()) { |
| 74 | + SWSS_LOG_ERROR("app name shouldn't be empty, key = %s", key.c_str()); |
| 75 | + return false; |
| 76 | + } |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace rebootbackend |
0 commit comments