From 11d32429ed3f0e1f32c469c2dfa188d053f0c66c Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 15:49:53 +0000 Subject: [PATCH 01/12] init --- .../RSUHealthMonitorPlugin/CMakeLists.txt | 2 +- .../RSUHealthMonitorPlugin/manifest.json | 10 +-- .../src/RSUConfigurationException.h | 19 +++++ .../src/RSUConfigurationList.cpp | 84 +++++++++++++++++++ .../src/RSUConfigurationList.h | 56 +++++++++++++ .../test/test_RSUConfigurationList.cpp | 65 ++++++++++++++ 6 files changed, 230 insertions(+), 6 deletions(-) create mode 100644 src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h create mode 100644 src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp create mode 100644 src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h create mode 100644 src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/CMakeLists.txt b/src/v2i-hub/RSUHealthMonitorPlugin/CMakeLists.txt index d863a4bf1..2bbadf66a 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/CMakeLists.txt +++ b/src/v2i-hub/RSUHealthMonitorPlugin/CMakeLists.txt @@ -13,7 +13,7 @@ TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC tmxutils jsoncpp NemaTode) ############# enable_testing() include_directories(${PROJECT_SOURCE_DIR}/src) -add_library(${PROJECT_NAME}_lib src/RSUHealthMonitorWorker.cpp) +add_library(${PROJECT_NAME}_lib src/RSUHealthMonitorWorker.cpp src/RSUConfigurationList) target_link_libraries(${PROJECT_NAME}_lib PUBLIC tmxutils NemaTode diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/manifest.json b/src/v2i-hub/RSUHealthMonitorPlugin/manifest.json index f2ee94443..c6e40ab8d 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/manifest.json +++ b/src/v2i-hub/RSUHealthMonitorPlugin/manifest.json @@ -18,7 +18,7 @@ "description": "Sending RSU SNMP GET request at every configured interval. Default every 1 second. Unit of measure: second." }, { - "key":"RSUIp", + "key":"RSUIp",//RSUConfiguration ->RSUS "default":"192.168.XX.XX", "description":"An IP address of the RSU the V2X hub is connected to." }, @@ -33,17 +33,17 @@ "description":"SNMP v3 authentication passphrase" }, { - "key":"SecurityUser", + "key":"SecurityUser", // user "default":"authOnlyUser", "description":"SNMP Security Name" }, { - "key":"SecurityLevel", - "default":"authPriv", + "key":"SecurityLevel", + "default":"authPriv", //Make this constant "description":"SNMP Security level" }, { - "key":"RSUMIBVersion", + "key":"RSUMIBVersion",//mibVersion "default":"RSU4.1", "description":"The version of RSU MIB (Management Information Base). E.G. RSU4.1 or RSU1218. Currently only support RSU4.1" } diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h new file mode 100644 index 000000000..7a7b5cf89 --- /dev/null +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h @@ -0,0 +1,19 @@ +#pragma once +#include + +namespace RSUHealthMonitor +{ + class RSUConfigurationException : public std::exception + { + private: + std::string message; + + public: + RSUConfigurationException(const char *msg) : message(msg){}; + const char *what() + { + return message.c_str(); + } + }; + +} \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp new file mode 100644 index 000000000..5b417a097 --- /dev/null +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -0,0 +1,84 @@ + +#include "RSUConfigurationList.h" + +namespace RSUHealthMonitor +{ + Json::Value RSUConfigurationList::parseJson(std::string rsuConfigsStr) + { + JSONCPP_STRING err; + Json::Value root; + auto length = static_cast(rsuConfigsStr.length()); + Json::CharReaderBuilder builder; + std::unique_ptr reader(builder.newCharReader()); + if (!reader->parse(rsuConfigsStr.c_str(), rsuConfigsStr.c_str() + length, &root, &err)) + { + std::stringstream oss; + oss << "Parse RSUs raw string error: "; + oss << err.c_str(); + throw RSUConfigurationException(oss.str().c_str()); + } + return root; + } + void RSUConfigurationList::parseRSUs(std::string rsuConfigsStr) + { + auto json = parseJson(rsuConfigsStr); + RSUConfiguration config; + auto rsuArray = json["RSUS"]; + if (!rsuArray.isArray()) + { + throw RSUConfigurationException("RSUS is not an array."); + } + for (auto i = 0; i != rsuArray.size(); i++) + { + if (rsuArray[i].isMember(RSUIpKey)) + { + config.rsuIp = rsuArray[i][RSUIpKey].asString(); + } + else + { + throw RSUConfigurationException("RSU IP does not exist."); + } + + if (rsuArray[i].isMember(SNMPPortKey)) + { + config.snmpPort = atoi(rsuArray[i][SNMPPortKey].asCString()); + } + else + { + throw RSUConfigurationException("SNMP port does not exist."); + } + + if (rsuArray[i].isMember(AuthPassPhraseKey)) + { + config.authPassPhrase = rsuArray[i][AuthPassPhraseKey].asString(); + } + else + { + throw RSUConfigurationException("Authentication pass phrase does not exist."); + } + + if (rsuArray[i].isMember(UserKey)) + { + config.user = rsuArray[i][UserKey].asString(); + } + else + { + throw RSUConfigurationException("User does not exist."); + } + + if (rsuArray[i].isMember(RSUMIBVersionKey)) + { + config.mibVersion = rsuArray[i][RSUMIBVersionKey].asString(); + } + else + { + throw RSUConfigurationException("RSU mib version does not exist."); + } + configs.push_back(config); + } + } + std::vector RSUConfigurationList::getConfigs() + { + return configs; + } +} \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h new file mode 100644 index 000000000..244ef731e --- /dev/null +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h @@ -0,0 +1,56 @@ +#pragma once +#include +#include +#include +#include +#include "RSUConfigurationException.h" + +namespace RSUHealthMonitor +{ + static constexpr const char *RSUIpKey = "RSUIp"; + static constexpr const char *SNMPPortKey = "SNMPPort"; + static constexpr const char *UserKey = "User"; + static constexpr const char *AuthPassPhraseKey = "AuthPassPhrase"; + static constexpr const char *RSUMIBVersionKey = "RSUMIBVersion"; + static constexpr const char *SecurityLevelKey = "SecurityLevel"; + struct RSUConfiguration + { + std::string rsuIp; + uint16_t snmpPort; + std::string user; + std::string authPassPhrase; + std::string securityLevel = "authPriv"; + std::string mibVersion; + friend std::ostream &operator<<(std::ostream &os, const RSUConfiguration &config) + { + os << RSUIpKey << ": " << config.rsuIp << ", " << SNMPPortKey << ": " << config.snmpPort << ", " << UserKey << ": " << config.user << ", " << AuthPassPhraseKey << ": " << config.authPassPhrase << ", " << SecurityLevelKey << ": " << config.securityLevel << ", " << RSUMIBVersionKey << ": " << config.mibVersion; + return os; + } + }; + + class RSUConfigurationList + { + private: + std::vector configs; + /*** + * @brief Parse JSON string and return the corresponding JSON value. + * @param rsuConfigsStr A JSON string includes all RSUs related configrations. + * @return JSON::Value A JSON object that includes RSUS information. + */ + Json::Value parseJson(std::string rsuConfigsStr); + + public: + RSUConfigurationList() = default; + ~RSUConfigurationList() = default; + /** + * @brief Parse RSUs configrations in JSON string representation, and update the memeber of list of RSUConfiguration struct. + * @param rsuConfigsStr A JSON string includes all RSUs related configrations. + */ + void parseRSUs(std::string rsuConfigsStr); + /** + * @brief Get a list of RSUConfiguration struct. + */ + std::vector getConfigs(); + }; + +} // namespace RSUHealthMonitor diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp new file mode 100644 index 000000000..5f8b919a2 --- /dev/null +++ b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp @@ -0,0 +1,65 @@ +#include +#include "RSUConfigurationList.h" + +namespace RSUHealthMonitor +{ + class test_RSUConfigurationList : public ::testing::Test + { + public: + std::shared_ptr rsuConfigList = std::make_shared(); + }; + + TEST_F(test_RSUConfigurationList, parseAndGetConfigs) + { + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + rsuConfigList->parseRSUs(rsuConfigsStr); + ASSERT_EQ(2, rsuConfigList->getConfigs().size()); + } + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_MalformatJSON) + { + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + std::string rsuConfigsStr = "{ \"RSUS { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + } + + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_missing_RSUS) + { + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + std::string rsuConfigsStr = "{ \"ERROR\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + } + + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_missing_SNMPPORT) + { + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort_Missing\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + } + + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_missing_AuthPassPhrase) + { + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase_Missing\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + } + + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_missing_User) + { + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User_Missing\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + } + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_missing_MibVersion) + { + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion_Missing\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + } +} \ No newline at end of file From 97ca2da04b22c08171b6dcb727291aadcb2fad8b Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 18:06:37 +0000 Subject: [PATCH 02/12] update config --- .../RSUHealthMonitorPlugin/manifest.json | 33 ++------------ .../src/RSUConfigurationException.h | 8 +--- .../src/RSUConfigurationList.cpp | 43 ++++++++++++++++--- .../src/RSUConfigurationList.h | 20 ++++++--- .../src/RSUHealthMonitorPlugin.cpp | 41 +++++++----------- .../src/RSUHealthMonitorPlugin.h | 22 +++++----- .../src/RSUHealthMonitorWorker.h | 7 +-- .../test/test_RSUConfigurationList.cpp | 19 +++++--- 8 files changed, 101 insertions(+), 92 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/manifest.json b/src/v2i-hub/RSUHealthMonitorPlugin/manifest.json index c6e40ab8d..5cbd9dff2 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/manifest.json +++ b/src/v2i-hub/RSUHealthMonitorPlugin/manifest.json @@ -17,35 +17,10 @@ "default":"1", "description": "Sending RSU SNMP GET request at every configured interval. Default every 1 second. Unit of measure: second." }, - { - "key":"RSUIp",//RSUConfiguration ->RSUS - "default":"192.168.XX.XX", - "description":"An IP address of the RSU the V2X hub is connected to." - }, - { - "key":"SNMPPort", - "default":"161", - "description":"The SNMP port for sending message or command." - }, - { - "key":"AuthPassPhrase", - "default":"dummy", - "description":"SNMP v3 authentication passphrase" - }, - { - "key":"SecurityUser", // user - "default":"authOnlyUser", - "description":"SNMP Security Name" - }, - { - "key":"SecurityLevel", - "default":"authPriv", //Make this constant - "description":"SNMP Security level" - }, - { - "key":"RSUMIBVersion",//mibVersion - "default":"RSU4.1", - "description":"The version of RSU MIB (Management Information Base). E.G. RSU4.1 or RSU1218. Currently only support RSU4.1" + { + "key":"RSUConfigurationList", + "default":"{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }", + "description":"Configurations of the RSUs the V2X hub is connected to." } ] } \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h index 7a7b5cf89..50c522e86 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h @@ -3,17 +3,13 @@ namespace RSUHealthMonitor { - class RSUConfigurationException : public std::exception + class RSUConfigurationException : public std::runtime_error { private: std::string message; public: - RSUConfigurationException(const char *msg) : message(msg){}; - const char *what() - { - return message.c_str(); - } + RSUConfigurationException(const char *msg) : runtime_error(msg){}; }; } \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp index 5b417a097..2f4e9485d 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -12,13 +12,13 @@ namespace RSUHealthMonitor std::unique_ptr reader(builder.newCharReader()); if (!reader->parse(rsuConfigsStr.c_str(), rsuConfigsStr.c_str() + length, &root, &err)) { - std::stringstream oss; - oss << "Parse RSUs raw string error: "; - oss << err.c_str(); - throw RSUConfigurationException(oss.str().c_str()); + std::stringstream ss; + ss << "Parse RSUs raw string error: " << err; + throw RSUConfigurationException(ss.str().c_str()); } return root; } + void RSUConfigurationList::parseRSUs(std::string rsuConfigsStr) { auto json = parseJson(rsuConfigsStr); @@ -68,7 +68,8 @@ namespace RSUHealthMonitor if (rsuArray[i].isMember(RSUMIBVersionKey)) { - config.mibVersion = rsuArray[i][RSUMIBVersionKey].asString(); + auto _rsuMIBVersionStr = rsuArray[i][RSUMIBVersionKey].asString(); + config.mibVersion = strToMibVersion(_rsuMIBVersionStr); } else { @@ -77,8 +78,40 @@ namespace RSUHealthMonitor configs.push_back(config); } } + + RSUMibVersion RSUConfigurationList::strToMibVersion(std::string &mibVersionStr) + { + boost::trim_left(mibVersionStr); + boost::trim_right(mibVersionStr); + // Support RSU MIB version 4.1 + if (boost::iequals(mibVersionStr, RSU4_1_str)) + { + return RSUMibVersion::RSUMIB_V_4_1; + } + else + { + std::stringstream ss; + ss << "Uknown RSU MIB version: " << mibVersionStr; + throw RSUConfigurationException(ss.str().c_str()); + } + } + std::vector RSUConfigurationList::getConfigs() { return configs; } + + std::ostream &operator<<(std::ostream &os, const RSUMibVersion &mib) + { + const std::string nameMibs[] = {"UNKOWN MIB", + "RSU 4.1", + "NTCIP 1218"}; + return os << nameMibs[mib]; + } + + std::ostream &operator<<(std::ostream &os, const RSUConfiguration &config) + { + os << RSUIpKey << ": " << config.rsuIp << ", " << SNMPPortKey << ": " << config.snmpPort << ", " << UserKey << ": " << config.user << ", " << AuthPassPhraseKey << ": " << config.authPassPhrase << ", " << SecurityLevelKey << ": " << config.securityLevel << ", " << RSUMIBVersionKey << ": " << config.mibVersion; + return os; + } } \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h index 244ef731e..2512430e8 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "RSUConfigurationException.h" namespace RSUHealthMonitor @@ -13,6 +14,16 @@ namespace RSUHealthMonitor static constexpr const char *AuthPassPhraseKey = "AuthPassPhrase"; static constexpr const char *RSUMIBVersionKey = "RSUMIBVersion"; static constexpr const char *SecurityLevelKey = "SecurityLevel"; + static constexpr const char *RSU4_1_str = "RSU4.1"; + static constexpr const char *RSU1218_str = "RSU1218"; + + enum RSUMibVersion + { + UNKOWN_MIB_V = 0, + RSUMIB_V_4_1 = 1, + RSUMIB_V_1218 = 2 + }; + struct RSUConfiguration { std::string rsuIp; @@ -20,12 +31,8 @@ namespace RSUHealthMonitor std::string user; std::string authPassPhrase; std::string securityLevel = "authPriv"; - std::string mibVersion; - friend std::ostream &operator<<(std::ostream &os, const RSUConfiguration &config) - { - os << RSUIpKey << ": " << config.rsuIp << ", " << SNMPPortKey << ": " << config.snmpPort << ", " << UserKey << ": " << config.user << ", " << AuthPassPhraseKey << ": " << config.authPassPhrase << ", " << SecurityLevelKey << ": " << config.securityLevel << ", " << RSUMIBVersionKey << ": " << config.mibVersion; - return os; - } + RSUMibVersion mibVersion; + friend std::ostream &operator<<(std::ostream &os, const RSUConfiguration &config); }; class RSUConfigurationList @@ -38,6 +45,7 @@ namespace RSUHealthMonitor * @return JSON::Value A JSON object that includes RSUS information. */ Json::Value parseJson(std::string rsuConfigsStr); + RSUMibVersion strToMibVersion(std::string &mibVersionStr); public: RSUConfigurationList() = default; diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp index 8d81a3b6c..1c0128fef 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp @@ -10,44 +10,35 @@ namespace RSUHealthMonitor { _rsuWorker = std::make_shared(); _rsuStatusTimer = make_unique(); + _rsuConfigListPtr = std::make_shared(); UpdateConfigSettings(); // Send SNMP call to RSU periodically at configurable interval. _timerThId = _rsuStatusTimer->AddPeriodicTick([this]() { - // Periodic SNMP call to get RSU status based on RSU MIB version 4.1 - auto rsuStatusJson = _rsuWorker->getRSUStatus(_rsuMibVersion, _rsuIp, _snmpPort, _securityUser, _authPassPhrase, _securityLevel, SEC_TO_MICRO); - PLOG(logINFO) << "Updating _interval: " << _interval; - //Broadcast RSU status periodically at _interval - BroadcastRSUStatus(rsuStatusJson); }, + this->monitorRSUs(); + PLOG(logINFO) << "Updating RSU _interval: " << _interval; }, std::chrono::milliseconds(_interval * SEC_TO_MILLI)); _rsuStatusTimer->Start(); } + void RSUHealthMonitorPlugin::monitorRSUs() + { + for (auto rsuConfig : _rsuConfigListPtr->getConfigs()) + { + auto rsuStatusJson = _rsuWorker->getRSUStatus(rsuConfig.mibVersion, rsuConfig.rsuIp, rsuConfig.snmpPort, rsuConfig.user, rsuConfig.authPassPhrase, rsuConfig.securityLevel, SEC_TO_MICRO); + BroadcastRSUStatus(rsuStatusJson, rsuConfig.mibVersion); + } + } + void RSUHealthMonitorPlugin::UpdateConfigSettings() { PLOG(logINFO) << "Updating configuration settings."; lock_guard lock(_configMutex); GetConfigValue("Interval", _interval); - GetConfigValue("RSUIp", _rsuIp); - GetConfigValue("SNMPPort", _snmpPort); - GetConfigValue("AuthPassPhrase", _authPassPhrase); - GetConfigValue("SecurityUser", _securityUser); - GetConfigValue("SecurityLevel", _securityLevel); - GetConfigValue("RSUMIBVersion", _rsuMIBVersionStr); - boost::trim_left(_rsuMIBVersionStr); - boost::trim_right(_rsuMIBVersionStr); - // Support RSU MIB version 4.1 - if (boost::iequals(_rsuMIBVersionStr, RSU4_1_str)) - { - _rsuMibVersion = RSUMibVersion::RSUMIB_V_4_1; - } - else - { - _rsuMibVersion = RSUMibVersion::UNKOWN_MIB_V; - PLOG(logERROR) << "Uknown RSU MIB version: " << _rsuMIBVersionStr; - } + GetConfigValue("RSUConfigurationList", _rsuConfigListStr); + _rsuConfigListPtr->parseRSUs(_rsuConfigListStr); try { @@ -65,13 +56,13 @@ namespace RSUHealthMonitor UpdateConfigSettings(); } - void RSUHealthMonitorPlugin::BroadcastRSUStatus(const Json::Value &rsuStatusJson) + void RSUHealthMonitorPlugin::BroadcastRSUStatus(const Json::Value &rsuStatusJson, const RSUMibVersion &mibVersion) { // Broadcast the RSU status info when there are RSU responses. if (!rsuStatusJson.empty() && _rsuWorker) { auto rsuStatusFields = _rsuWorker->getJsonKeys(rsuStatusJson); - auto configTbl = _rsuWorker->GetRSUStatusConfig(_rsuMibVersion); + auto configTbl = _rsuWorker->GetRSUStatusConfig(mibVersion); // Only broadcast RSU status when all required fields are present. if (_rsuWorker->validateAllRequiredFieldsPresent(configTbl, rsuStatusFields)) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h index 47d8ee1bd..1e5d21b91 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h @@ -5,6 +5,7 @@ #include #include "RSUStatusMessage.h" #include "RSUHealthMonitorWorker.h" +#include "RSUConfigurationList.h" using namespace tmx::utils; using namespace std; @@ -17,15 +18,15 @@ namespace RSUHealthMonitor private: mutex _configMutex; uint16_t _interval; - string _rsuIp; - uint16_t _snmpPort; - string _authPassPhrase; - string _securityUser; - string _securityLevel; - string _rsuMIBVersionStr; - RSUMibVersion _rsuMibVersion; - const char *RSU4_1_str = "RSU4.1"; - const char *RSU1218_str = "RSU1218"; + // string _rsuIp; + // uint16_t _snmpPort; + // string _authPassPhrase; + // string _securityUser; + // string _securityLevel; + // string _rsuMIBVersionStr; + // RSUMibVersion _rsuMibVersion; + string _rsuConfigListStr; + shared_ptr _rsuConfigListPtr; shared_ptr _rsuWorker; unique_ptr _rsuStatusTimer; uint _timerThId; @@ -35,12 +36,13 @@ namespace RSUHealthMonitor * @brief Broadcast RSU status * @param Json::Value RSU status in JSON format */ - void BroadcastRSUStatus(const Json::Value& rsuStatusJson); + void BroadcastRSUStatus(const Json::Value &rsuStatusJson, const RSUMibVersion &mibVersion); public: explicit RSUHealthMonitorPlugin(const std::string &name); void UpdateConfigSettings(); void OnConfigChanged(const char *key, const char *value) override; + void monitorRSUs(); }; } // namespace RSUHealthMonitorPlugin \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorWorker.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorWorker.h index ef7a429c5..36ebf34bf 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorWorker.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorWorker.h @@ -11,6 +11,7 @@ #include "SNMPClient.h" #include #include "RSUStatusMessage.h" +#include "RSUConfigurationList.h" using namespace std; using namespace tmx::utils; @@ -19,12 +20,6 @@ using namespace tmx::messages; namespace RSUHealthMonitor { - enum class RSUMibVersion - { - UNKOWN_MIB_V = 0, - RSUMIB_V_4_1 = 1, - RSUMIB_V_1218 = 2 - }; struct RSUFieldOIDStruct { diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp index 5f8b919a2..f048babc3 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp @@ -24,7 +24,7 @@ namespace RSUHealthMonitor ASSERT_EQ(0, rsuConfigList->getConfigs().size()); } - TEST_F(test_RSUConfigurationList, parseAndGetConfigs_missing_RSUS) + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_Missing_RSUS) { ASSERT_EQ(0, rsuConfigList->getConfigs().size()); std::string rsuConfigsStr = "{ \"ERROR\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; @@ -32,7 +32,7 @@ namespace RSUHealthMonitor ASSERT_EQ(0, rsuConfigList->getConfigs().size()); } - TEST_F(test_RSUConfigurationList, parseAndGetConfigs_missing_SNMPPORT) + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_Missing_SNMPPORT) { ASSERT_EQ(0, rsuConfigList->getConfigs().size()); std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort_Missing\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; @@ -40,7 +40,7 @@ namespace RSUHealthMonitor ASSERT_EQ(0, rsuConfigList->getConfigs().size()); } - TEST_F(test_RSUConfigurationList, parseAndGetConfigs_missing_AuthPassPhrase) + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_Missing_AuthPassPhrase) { ASSERT_EQ(0, rsuConfigList->getConfigs().size()); std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase_Missing\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; @@ -48,18 +48,27 @@ namespace RSUHealthMonitor ASSERT_EQ(0, rsuConfigList->getConfigs().size()); } - TEST_F(test_RSUConfigurationList, parseAndGetConfigs_missing_User) + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_Missing_User) { ASSERT_EQ(0, rsuConfigList->getConfigs().size()); std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User_Missing\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); ASSERT_EQ(0, rsuConfigList->getConfigs().size()); } - TEST_F(test_RSUConfigurationList, parseAndGetConfigs_missing_MibVersion) + + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_Missing_MibVersion) { ASSERT_EQ(0, rsuConfigList->getConfigs().size()); std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion_Missing\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); ASSERT_EQ(0, rsuConfigList->getConfigs().size()); } + + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_Invalid_MibVersion) + { + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"INVALID_RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + } } \ No newline at end of file From b982d26b9cd57751c2f7a58f76b0721a74288e75 Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 18:41:56 +0000 Subject: [PATCH 03/12] add more test --- .../src/RSUConfigurationList.cpp | 13 +++++++++---- .../src/RSUHealthMonitorPlugin.cpp | 6 +++++- .../src/RSUHealthMonitorPlugin.h | 7 ------- .../test/test_RSUConfigurationList.cpp | 11 +++++++++++ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp index 2f4e9485d..f2bbccf02 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -22,6 +22,7 @@ namespace RSUHealthMonitor void RSUConfigurationList::parseRSUs(std::string rsuConfigsStr) { auto json = parseJson(rsuConfigsStr); + std::vector tempConfigs; RSUConfiguration config; auto rsuArray = json["RSUS"]; if (!rsuArray.isArray()) @@ -41,11 +42,12 @@ namespace RSUHealthMonitor if (rsuArray[i].isMember(SNMPPortKey)) { - config.snmpPort = atoi(rsuArray[i][SNMPPortKey].asCString()); + auto port = atoi(rsuArray[i][SNMPPortKey].asCString()); + port != 0 ? config.snmpPort = port : throw RSUConfigurationException("Invalid port number in string format."); } else { - throw RSUConfigurationException("SNMP port does not exist."); + throw RSUConfigurationException("Either SNMP port does not exist."); } if (rsuArray[i].isMember(AuthPassPhraseKey)) @@ -75,15 +77,18 @@ namespace RSUHealthMonitor { throw RSUConfigurationException("RSU mib version does not exist."); } - configs.push_back(config); + tempConfigs.push_back(config); } + // Only update RSU configurations when all configs are processed correctly. + configs.clear(); + configs.assign(tempConfigs.begin(), tempConfigs.end()); } RSUMibVersion RSUConfigurationList::strToMibVersion(std::string &mibVersionStr) { boost::trim_left(mibVersionStr); boost::trim_right(mibVersionStr); - // Support RSU MIB version 4.1 + // Only support RSU MIB version 4.1 if (boost::iequals(mibVersionStr, RSU4_1_str)) { return RSUMibVersion::RSUMIB_V_4_1; diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp index 1c0128fef..59f9b825c 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp @@ -38,12 +38,16 @@ namespace RSUHealthMonitor lock_guard lock(_configMutex); GetConfigValue("Interval", _interval); GetConfigValue("RSUConfigurationList", _rsuConfigListStr); - _rsuConfigListPtr->parseRSUs(_rsuConfigListStr); try { + _rsuConfigListPtr->parseRSUs(_rsuConfigListStr); _rsuStatusTimer->ChangeFrequency(_timerThId, std::chrono::milliseconds(_interval * SEC_TO_MILLI)); } + catch (const RSUConfigurationException &ex) + { + PLOG(logERROR) << "Cannot update RSU configurations due to error: " << ex.what(); + } catch (const tmx::TmxException &ex) { PLOG(logERROR) << ex.what(); diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h index 1e5d21b91..e41ec8d8c 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.h @@ -18,13 +18,6 @@ namespace RSUHealthMonitor private: mutex _configMutex; uint16_t _interval; - // string _rsuIp; - // uint16_t _snmpPort; - // string _authPassPhrase; - // string _securityUser; - // string _securityLevel; - // string _rsuMIBVersionStr; - // RSUMibVersion _rsuMibVersion; string _rsuConfigListStr; shared_ptr _rsuConfigListPtr; shared_ptr _rsuWorker; diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp index f048babc3..c7d637e3f 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp @@ -15,6 +15,10 @@ namespace RSUHealthMonitor std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.XX.XX\", \"SNMPPort\": \"161\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; rsuConfigList->parseRSUs(rsuConfigsStr); ASSERT_EQ(2, rsuConfigList->getConfigs().size()); + std::stringstream ss; + ss << rsuConfigList->getConfigs()[0]; + std::string expected = "RSUIp: 192.168.XX.XX, SNMPPort: 161, User: authOnlyUser, AuthPassPhrase: dummy, SecurityLevel: authPriv, RSUMIBVersion: RSU 4.1"; + ASSERT_EQ(expected, ss.str()); } TEST_F(test_RSUConfigurationList, parseAndGetConfigs_MalformatJSON) { @@ -40,6 +44,13 @@ namespace RSUHealthMonitor ASSERT_EQ(0, rsuConfigList->getConfigs().size()); } + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_INVALID_SNMPPORT) + { + auto rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.01.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"INVALID_PORT\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); + ASSERT_EQ(0, rsuConfigList->getConfigs().size()); + } + TEST_F(test_RSUConfigurationList, parseAndGetConfigs_Missing_AuthPassPhrase) { ASSERT_EQ(0, rsuConfigList->getConfigs().size()); From c8fcc4337d07001360d018bc77b00f62f6964035 Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 18:43:54 +0000 Subject: [PATCH 04/12] update exception --- .../RSUHealthMonitorPlugin/src/RSUConfigurationException.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h index 50c522e86..3d4be81ae 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h @@ -5,11 +5,7 @@ namespace RSUHealthMonitor { class RSUConfigurationException : public std::runtime_error { - private: - std::string message; - public: RSUConfigurationException(const char *msg) : runtime_error(msg){}; }; - } \ No newline at end of file From 8e1afef999927f0721d38bc6bf15d188be2eb3ba Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 19:34:02 +0000 Subject: [PATCH 05/12] address code smell --- .../src/RSUConfigurationException.h | 2 +- .../src/RSUConfigurationList.cpp | 16 ++++++++-------- .../src/RSUConfigurationList.h | 8 ++++---- .../src/RSUHealthMonitorPlugin.cpp | 2 +- .../test/test_RSUConfigurationList.cpp | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h index 3d4be81ae..81e02a16e 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h @@ -6,6 +6,6 @@ namespace RSUHealthMonitor class RSUConfigurationException : public std::runtime_error { public: - RSUConfigurationException(const char *msg) : runtime_error(msg){}; + explicit RSUConfigurationException(const char *msg) : runtime_error(msg){}; }; } \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp index f2bbccf02..baf7f3cfb 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -3,7 +3,7 @@ namespace RSUHealthMonitor { - Json::Value RSUConfigurationList::parseJson(std::string rsuConfigsStr) + Json::Value RSUConfigurationList::parseJson(std::string &rsuConfigsStr) const { JSONCPP_STRING err; Json::Value root; @@ -19,7 +19,7 @@ namespace RSUHealthMonitor return root; } - void RSUConfigurationList::parseRSUs(std::string rsuConfigsStr) + void RSUConfigurationList::parseRSUs(std::string &rsuConfigsStr) { auto json = parseJson(rsuConfigsStr); std::vector tempConfigs; @@ -42,7 +42,7 @@ namespace RSUHealthMonitor if (rsuArray[i].isMember(SNMPPortKey)) { - auto port = atoi(rsuArray[i][SNMPPortKey].asCString()); + auto port = static_cast(atoi(rsuArray[i][SNMPPortKey].asCString())); port != 0 ? config.snmpPort = port : throw RSUConfigurationException("Invalid port number in string format."); } else @@ -84,7 +84,7 @@ namespace RSUHealthMonitor configs.assign(tempConfigs.begin(), tempConfigs.end()); } - RSUMibVersion RSUConfigurationList::strToMibVersion(std::string &mibVersionStr) + RSUMibVersion RSUConfigurationList::strToMibVersion(std::string &mibVersionStr) const { boost::trim_left(mibVersionStr); boost::trim_right(mibVersionStr); @@ -108,10 +108,10 @@ namespace RSUHealthMonitor std::ostream &operator<<(std::ostream &os, const RSUMibVersion &mib) { - const std::string nameMibs[] = {"UNKOWN MIB", - "RSU 4.1", - "NTCIP 1218"}; - return os << nameMibs[mib]; + const std::vector nameMibs = {"UNKOWN MIB", + "RSU 4.1", + "NTCIP 1218"}; + return os << nameMibs[static_cast(mib)]; } std::ostream &operator<<(std::ostream &os, const RSUConfiguration &config) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h index 2512430e8..14f9bded1 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h @@ -17,7 +17,7 @@ namespace RSUHealthMonitor static constexpr const char *RSU4_1_str = "RSU4.1"; static constexpr const char *RSU1218_str = "RSU1218"; - enum RSUMibVersion + enum class RSUMibVersion { UNKOWN_MIB_V = 0, RSUMIB_V_4_1 = 1, @@ -44,8 +44,8 @@ namespace RSUHealthMonitor * @param rsuConfigsStr A JSON string includes all RSUs related configrations. * @return JSON::Value A JSON object that includes RSUS information. */ - Json::Value parseJson(std::string rsuConfigsStr); - RSUMibVersion strToMibVersion(std::string &mibVersionStr); + Json::Value parseJson(std::string &rsuConfigsStr) const; + RSUMibVersion strToMibVersion(std::string &mibVersionStr) const; public: RSUConfigurationList() = default; @@ -54,7 +54,7 @@ namespace RSUHealthMonitor * @brief Parse RSUs configrations in JSON string representation, and update the memeber of list of RSUConfiguration struct. * @param rsuConfigsStr A JSON string includes all RSUs related configrations. */ - void parseRSUs(std::string rsuConfigsStr); + void parseRSUs(std::string &rsuConfigsStr); /** * @brief Get a list of RSUConfiguration struct. */ diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp index 59f9b825c..ea4a0ce09 100755 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorPlugin.cpp @@ -17,7 +17,7 @@ namespace RSUHealthMonitor _timerThId = _rsuStatusTimer->AddPeriodicTick([this]() { this->monitorRSUs(); - PLOG(logINFO) << "Updating RSU _interval: " << _interval; }, + PLOG(logINFO) << "Monitoring RSU at interval (second): " << _interval; }, std::chrono::milliseconds(_interval * SEC_TO_MILLI)); _rsuStatusTimer->Start(); } diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp index c7d637e3f..1ba516cdc 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp @@ -46,7 +46,7 @@ namespace RSUHealthMonitor TEST_F(test_RSUConfigurationList, parseAndGetConfigs_INVALID_SNMPPORT) { - auto rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.01.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"INVALID_PORT\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; + std::string rsuConfigsStr = "{ \"RSUS\": [ { \"RSUIp\": \"192.168.01.XX\", \"SNMPPort\": \"162\", \"AuthPassPhrase\": \"dummy\", \"User\": \"authOnlyUser\", \"RSUMIBVersion\": \"RSU4.1\" },{ \"RSUIp\": \"192.168.00.XX\", \"SNMPPort\": \"INVALID_PORT\", \"AuthPassPhrase\": \"tester\", \"User\": \"authPrivUser\", \"RSUMIBVersion\": \"RSU4.1\" }] }"; ASSERT_THROW(rsuConfigList->parseRSUs(rsuConfigsStr), RSUHealthMonitor::RSUConfigurationException); ASSERT_EQ(0, rsuConfigList->getConfigs().size()); } From f671c8d483e0308fe37274f39297033dd96d7607 Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 19:52:29 +0000 Subject: [PATCH 06/12] address code smel --- .../src/RSUConfigurationException.h | 6 +++--- .../src/RSUConfigurationList.cpp | 14 +++++++------- .../src/RSUConfigurationList.h | 9 +++++---- .../test/test_RSUConfigurationList.cpp | 2 +- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h index 81e02a16e..26c7b0c75 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h @@ -1,11 +1,11 @@ #pragma once -#include +#include namespace RSUHealthMonitor { - class RSUConfigurationException : public std::runtime_error + class RSUConfigurationException : public tmx::TmxException { public: - explicit RSUConfigurationException(const char *msg) : runtime_error(msg){}; + using TmxException::TmxException; }; } \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp index baf7f3cfb..4377dfc5c 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -3,7 +3,7 @@ namespace RSUHealthMonitor { - Json::Value RSUConfigurationList::parseJson(std::string &rsuConfigsStr) const + Json::Value RSUConfigurationList::parseJson(const std::string &rsuConfigsStr) const { JSONCPP_STRING err; Json::Value root; @@ -19,12 +19,12 @@ namespace RSUHealthMonitor return root; } - void RSUConfigurationList::parseRSUs(std::string &rsuConfigsStr) + void RSUConfigurationList::parseRSUs(const std::string &rsuConfigsStr) { auto json = parseJson(rsuConfigsStr); std::vector tempConfigs; RSUConfiguration config; - auto rsuArray = json["RSUS"]; + auto rsuArray = json[RSUSKey]; if (!rsuArray.isArray()) { throw RSUConfigurationException("RSUS is not an array."); @@ -84,7 +84,7 @@ namespace RSUHealthMonitor configs.assign(tempConfigs.begin(), tempConfigs.end()); } - RSUMibVersion RSUConfigurationList::strToMibVersion(std::string &mibVersionStr) const + RSUMibVersion RSUConfigurationList::strToMibVersion(const std::string &mibVersionStr) const { boost::trim_left(mibVersionStr); boost::trim_right(mibVersionStr); @@ -101,7 +101,7 @@ namespace RSUHealthMonitor } } - std::vector RSUConfigurationList::getConfigs() + std::vector RSUConfigurationList::getConfigs() const { return configs; } @@ -109,8 +109,8 @@ namespace RSUHealthMonitor std::ostream &operator<<(std::ostream &os, const RSUMibVersion &mib) { const std::vector nameMibs = {"UNKOWN MIB", - "RSU 4.1", - "NTCIP 1218"}; + "RSU4.1", + "NTCIP1218"}; return os << nameMibs[static_cast(mib)]; } diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h index 14f9bded1..1ebdb798a 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h @@ -8,6 +8,7 @@ namespace RSUHealthMonitor { + static constexpr const char *RSUSKey = "RSUS"; static constexpr const char *RSUIpKey = "RSUIp"; static constexpr const char *SNMPPortKey = "SNMPPort"; static constexpr const char *UserKey = "User"; @@ -44,8 +45,8 @@ namespace RSUHealthMonitor * @param rsuConfigsStr A JSON string includes all RSUs related configrations. * @return JSON::Value A JSON object that includes RSUS information. */ - Json::Value parseJson(std::string &rsuConfigsStr) const; - RSUMibVersion strToMibVersion(std::string &mibVersionStr) const; + Json::Value parseJson(const std::string &rsuConfigsStr) const; + RSUMibVersion strToMibVersion(const std::string &mibVersionStr) const; public: RSUConfigurationList() = default; @@ -54,11 +55,11 @@ namespace RSUHealthMonitor * @brief Parse RSUs configrations in JSON string representation, and update the memeber of list of RSUConfiguration struct. * @param rsuConfigsStr A JSON string includes all RSUs related configrations. */ - void parseRSUs(std::string &rsuConfigsStr); + void parseRSUs(const std::string &rsuConfigsStr); /** * @brief Get a list of RSUConfiguration struct. */ - std::vector getConfigs(); + std::vector getConfigs() const; }; } // namespace RSUHealthMonitor diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp index 1ba516cdc..8fb16e5a2 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/test/test_RSUConfigurationList.cpp @@ -17,7 +17,7 @@ namespace RSUHealthMonitor ASSERT_EQ(2, rsuConfigList->getConfigs().size()); std::stringstream ss; ss << rsuConfigList->getConfigs()[0]; - std::string expected = "RSUIp: 192.168.XX.XX, SNMPPort: 161, User: authOnlyUser, AuthPassPhrase: dummy, SecurityLevel: authPriv, RSUMIBVersion: RSU 4.1"; + std::string expected = "RSUIp: 192.168.XX.XX, SNMPPort: 161, User: authOnlyUser, AuthPassPhrase: dummy, SecurityLevel: authPriv, RSUMIBVersion: RSU4.1"; ASSERT_EQ(expected, ss.str()); } TEST_F(test_RSUConfigurationList, parseAndGetConfigs_MalformatJSON) From 036a992360d6f55bfa63ab90c371206d54498c3d Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 19:54:37 +0000 Subject: [PATCH 07/12] address code smel --- .../RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp | 4 ++-- src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp index 4377dfc5c..808dc78f8 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -19,7 +19,7 @@ namespace RSUHealthMonitor return root; } - void RSUConfigurationList::parseRSUs(const std::string &rsuConfigsStr) + void RSUConfigurationList::parseRSUs(std::string &rsuConfigsStr) { auto json = parseJson(rsuConfigsStr); std::vector tempConfigs; @@ -84,7 +84,7 @@ namespace RSUHealthMonitor configs.assign(tempConfigs.begin(), tempConfigs.end()); } - RSUMibVersion RSUConfigurationList::strToMibVersion(const std::string &mibVersionStr) const + RSUMibVersion RSUConfigurationList::strToMibVersion(std::string &mibVersionStr) const { boost::trim_left(mibVersionStr); boost::trim_right(mibVersionStr); diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h index 1ebdb798a..1cc94e32c 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h @@ -46,7 +46,7 @@ namespace RSUHealthMonitor * @return JSON::Value A JSON object that includes RSUS information. */ Json::Value parseJson(const std::string &rsuConfigsStr) const; - RSUMibVersion strToMibVersion(const std::string &mibVersionStr) const; + RSUMibVersion strToMibVersion(std::string &mibVersionStr) const; public: RSUConfigurationList() = default; @@ -55,7 +55,7 @@ namespace RSUHealthMonitor * @brief Parse RSUs configrations in JSON string representation, and update the memeber of list of RSUConfiguration struct. * @param rsuConfigsStr A JSON string includes all RSUs related configrations. */ - void parseRSUs(const std::string &rsuConfigsStr); + void parseRSUs(std::string &rsuConfigsStr); /** * @brief Get a list of RSUConfiguration struct. */ From 5a0dbcacaf7db804800841707085dca31231b8ab Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 20:39:58 +0000 Subject: [PATCH 08/12] address code smell --- .../RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp | 6 +++--- .../RSUHealthMonitorPlugin/src/RSUConfigurationList.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp index 808dc78f8..aa843ea17 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -19,7 +19,7 @@ namespace RSUHealthMonitor return root; } - void RSUConfigurationList::parseRSUs(std::string &rsuConfigsStr) + void RSUConfigurationList::parseRSUs(const std::string &rsuConfigsStr) { auto json = parseJson(rsuConfigsStr); std::vector tempConfigs; @@ -70,8 +70,8 @@ namespace RSUHealthMonitor if (rsuArray[i].isMember(RSUMIBVersionKey)) { - auto _rsuMIBVersionStr = rsuArray[i][RSUMIBVersionKey].asString(); - config.mibVersion = strToMibVersion(_rsuMIBVersionStr); + auto rsuMIBVersionStr = rsuArray[i][RSUMIBVersionKey].asString(); + config.mibVersion = strToMibVersion(rsuMIBVersionStr); } else { diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h index 1cc94e32c..5938ec7fb 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.h @@ -55,7 +55,7 @@ namespace RSUHealthMonitor * @brief Parse RSUs configrations in JSON string representation, and update the memeber of list of RSUConfiguration struct. * @param rsuConfigsStr A JSON string includes all RSUs related configrations. */ - void parseRSUs(std::string &rsuConfigsStr); + void parseRSUs(const std::string &rsuConfigsStr); /** * @brief Get a list of RSUConfiguration struct. */ From 720e4d7b6c93da64b98878b173e09f38c78b251b Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Tue, 16 Apr 2024 21:00:21 +0000 Subject: [PATCH 09/12] remove update --- .../RSUHealthMonitorPlugin/src/RSUHealthMonitorWorker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorWorker.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorWorker.cpp index 6ac62f9fc..7c972cc75 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorWorker.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUHealthMonitorWorker.cpp @@ -125,7 +125,7 @@ namespace RSUHealthMonitor try { // Create SNMP client and use SNMP V3 protocol - PLOG(logINFO) << "Update SNMP client: RSU IP: " << _rsuIp << ", RSU port: " << _snmpPort << ", User: " << _securityUser << ", auth pass phrase: " << _authPassPhrase << ", security level: " + PLOG(logINFO) << "SNMP client: RSU IP: " << _rsuIp << ", RSU port: " << _snmpPort << ", User: " << _securityUser << ", auth pass phrase: " << _authPassPhrase << ", security level: " << _securityLevel; auto _snmpClientPtr = std::make_unique(_rsuIp, _snmpPort, "", _securityUser, _securityLevel, _authPassPhrase, SNMP_VERSION_3, timeout); From 01a48eb174440616d8b083f6f3eaf635005244f1 Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Wed, 17 Apr 2024 15:15:53 +0000 Subject: [PATCH 10/12] address comments --- .../src/RSUConfigurationException.h | 5 ++--- .../src/RSUConfigurationList.cpp | 20 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h index 26c7b0c75..2b09fb593 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h @@ -1,11 +1,10 @@ #pragma once -#include namespace RSUHealthMonitor { - class RSUConfigurationException : public tmx::TmxException + class RSUConfigurationException : public std::runtime_error { public: - using TmxException::TmxException; + RSUConfigurationException(const std::string &msg) : std::runtime_error(msg){}; }; } \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp index aa843ea17..858aeff0c 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -27,7 +27,7 @@ namespace RSUHealthMonitor auto rsuArray = json[RSUSKey]; if (!rsuArray.isArray()) { - throw RSUConfigurationException("RSUS is not an array."); + throw RSUConfigurationException("RSUConfigurationList: Missing RSUS array."); } for (auto i = 0; i != rsuArray.size(); i++) { @@ -37,17 +37,20 @@ namespace RSUHealthMonitor } else { - throw RSUConfigurationException("RSU IP does not exist."); + std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: RSU IP [" + std::string(RSUIpKey) + "] is required."; + throw RSUConfigurationException(errMsg); } if (rsuArray[i].isMember(SNMPPortKey)) { auto port = static_cast(atoi(rsuArray[i][SNMPPortKey].asCString())); - port != 0 ? config.snmpPort = port : throw RSUConfigurationException("Invalid port number in string format."); + std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: Invalid SNMP port number in string format."; + port != 0 ? config.snmpPort = port : throw RSUConfigurationException(errMsg); } else { - throw RSUConfigurationException("Either SNMP port does not exist."); + std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: SNMP port [" + std::string(SNMPPortKey) + "] is required."; + throw RSUConfigurationException(errMsg); } if (rsuArray[i].isMember(AuthPassPhraseKey)) @@ -56,7 +59,8 @@ namespace RSUHealthMonitor } else { - throw RSUConfigurationException("Authentication pass phrase does not exist."); + std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: Authentication pass phrase [" + std::string(AuthPassPhraseKey) + "] is required."; + throw RSUConfigurationException(errMsg); } if (rsuArray[i].isMember(UserKey)) @@ -65,7 +69,8 @@ namespace RSUHealthMonitor } else { - throw RSUConfigurationException("User does not exist."); + std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: User [" + std::string(UserKey) + "] is required."; + throw RSUConfigurationException(errMsg); } if (rsuArray[i].isMember(RSUMIBVersionKey)) @@ -75,7 +80,8 @@ namespace RSUHealthMonitor } else { - throw RSUConfigurationException("RSU mib version does not exist."); + std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: RSU MIB version [" + std::string(RSUMIBVersionKey) + "] is required."; + throw RSUConfigurationException(errMsg); } tempConfigs.push_back(config); } From 889bb5cb6697db5a9de65cfe2bf54cd16ddf4902 Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Wed, 17 Apr 2024 15:36:38 +0000 Subject: [PATCH 11/12] address comments --- .../src/RSUConfigurationException.h | 2 +- .../src/RSUConfigurationList.cpp | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h index 2b09fb593..5c643d74b 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h @@ -5,6 +5,6 @@ namespace RSUHealthMonitor class RSUConfigurationException : public std::runtime_error { public: - RSUConfigurationException(const std::string &msg) : std::runtime_error(msg){}; + explicit RSUConfigurationException(const std::string &msg) : std::runtime_error(msg){}; }; } \ No newline at end of file diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp index 858aeff0c..058ea216d 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationList.cpp @@ -37,19 +37,19 @@ namespace RSUHealthMonitor } else { - std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: RSU IP [" + std::string(RSUIpKey) + "] is required."; + auto errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: RSU IP [" + std::string(RSUIpKey) + "] is required."; throw RSUConfigurationException(errMsg); } if (rsuArray[i].isMember(SNMPPortKey)) { auto port = static_cast(atoi(rsuArray[i][SNMPPortKey].asCString())); - std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: Invalid SNMP port number in string format."; + auto errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: Invalid SNMP port number in string format."; port != 0 ? config.snmpPort = port : throw RSUConfigurationException(errMsg); } else { - std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: SNMP port [" + std::string(SNMPPortKey) + "] is required."; + auto errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: SNMP port [" + std::string(SNMPPortKey) + "] is required."; throw RSUConfigurationException(errMsg); } @@ -59,7 +59,7 @@ namespace RSUHealthMonitor } else { - std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: Authentication pass phrase [" + std::string(AuthPassPhraseKey) + "] is required."; + auto errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: Authentication pass phrase [" + std::string(AuthPassPhraseKey) + "] is required."; throw RSUConfigurationException(errMsg); } @@ -69,7 +69,7 @@ namespace RSUHealthMonitor } else { - std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: User [" + std::string(UserKey) + "] is required."; + auto errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: User [" + std::string(UserKey) + "] is required."; throw RSUConfigurationException(errMsg); } @@ -80,7 +80,7 @@ namespace RSUHealthMonitor } else { - std::string errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: RSU MIB version [" + std::string(RSUMIBVersionKey) + "] is required."; + auto errMsg = "RSUConfigurationList [" + std::to_string(i + 1) + "]: RSU MIB version [" + std::string(RSUMIBVersionKey) + "] is required."; throw RSUConfigurationException(errMsg); } tempConfigs.push_back(config); From 43ed4c86dfe7b7893ce36fc2e6d97e2042d69a34 Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Wed, 17 Apr 2024 15:54:26 +0000 Subject: [PATCH 12/12] address codesmell --- .../RSUHealthMonitorPlugin/src/RSUConfigurationException.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h index 5c643d74b..27e5197ca 100644 --- a/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h +++ b/src/v2i-hub/RSUHealthMonitorPlugin/src/RSUConfigurationException.h @@ -5,6 +5,6 @@ namespace RSUHealthMonitor class RSUConfigurationException : public std::runtime_error { public: - explicit RSUConfigurationException(const std::string &msg) : std::runtime_error(msg){}; + using runtime_error::runtime_error; }; } \ No newline at end of file