diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d6fcc56..f9de1082 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -282,8 +282,7 @@ if (ENABLE_TESTS) list(FILTER dvmfnetest_SRC EXCLUDE REGEX "/FNEMain\\.cpp$") list(FILTER dvmfnetest_SRC EXCLUDE REGEX "/ActivityLog\\.cpp$") list(FILTER dvmfnetest_SRC EXCLUDE REGEX "/HostFNE\\.cpp$") - list(FILTER dvmfnetest_SRC EXCLUDE REGEX "/fne/restapi/.*\\.cpp$") - add_executable(dvmtests ${common_INCLUDE} ${dvmhost_SRC} ${dvmfnetest_SRC} ${dvmtests_SRC}) + add_executable(dvmtests ${common_INCLUDE} ${dvmhost_SRC} ${dvmfnetest_SRC} ${dvmtests_SRC} src/remote/RESTClient.cpp) target_compile_definitions(dvmtests PUBLIC -DCATCH2_TEST_COMPILATION) target_link_libraries(dvmtests PRIVATE Catch2::Catch2WithMain common ${OPENSSL_LIBRARIES} asio::asio Threads::Threads util) target_include_directories(dvmtests PRIVATE ${OPENSSL_INCLUDE_DIR} src src/host src/fne tests) diff --git a/src/common/lookups/AdjSiteMapLookup.h b/src/common/lookups/AdjSiteMapLookup.h index ee9db8bd..0755b755 100644 --- a/src/common/lookups/AdjSiteMapLookup.h +++ b/src/common/lookups/AdjSiteMapLookup.h @@ -59,7 +59,7 @@ namespace lookups AdjPeerMapEntry() { m_active = node["active"].as(false); - m_peerId = node["peer_id"].as(0U); + m_peerId = node["peerId"].as(0U); yaml::Node& neighborList = node["neighbors"]; if (neighborList.size() > 0U) { @@ -120,6 +120,7 @@ namespace lookups newNeighbor = __INT_STR(neighbor); } } + node["neighbors"] = neighborList; } public: diff --git a/src/fne/HostFNE.cpp b/src/fne/HostFNE.cpp index 64d703a2..29692ff4 100644 --- a/src/fne/HostFNE.cpp +++ b/src/fne/HostFNE.cpp @@ -544,7 +544,7 @@ bool HostFNE::initializeRESTAPI() // initialize network remote command if (restApiEnable) { - m_RESTAPI = new RESTAPI(restApiAddress, restApiPort, restApiPassword, restApiSSLKey, restApiSSLCert, restApiEnableSSL, this, restApiDebug); + m_RESTAPI = new fne_restapi::RESTAPI(restApiAddress, restApiPort, restApiPassword, restApiSSLKey, restApiSSLCert, restApiEnableSSL, this, restApiDebug); m_RESTAPI->setLookups(m_ridLookup, m_tidLookup, m_peerListLookup, m_adjSiteMapLookup, m_cryptoLookup); m_RESTAPI->setNetwork(m_network); bool ret = m_RESTAPI->open(); diff --git a/src/fne/HostFNE.h b/src/fne/HostFNE.h index e7e99ba9..db2a7039 100644 --- a/src/fne/HostFNE.h +++ b/src/fne/HostFNE.h @@ -135,8 +135,8 @@ private: bool m_allowActivityTransfer; bool m_allowDiagnosticTransfer; - friend class RESTAPI; - RESTAPI* m_RESTAPI; + friend class fne_restapi::RESTAPI; + fne_restapi::RESTAPI* m_RESTAPI; /** * @brief Reads basic configuration parameters from the INI. diff --git a/src/fne/network/TrafficNetwork.h b/src/fne/network/TrafficNetwork.h index 36554c8b..9adb9c89 100644 --- a/src/fne/network/TrafficNetwork.h +++ b/src/fne/network/TrafficNetwork.h @@ -63,7 +63,7 @@ class FNETestHooks; #endif class HOST_SW_API HostFNE; -class HOST_SW_API RESTAPI; +namespace fne_restapi { class HOST_SW_API RESTAPI; } namespace network { namespace callhandler { class HOST_SW_API TagDMRData; } } namespace network { namespace callhandler { namespace packetdata { class HOST_SW_API DMRPacketData; } } } namespace network { namespace callhandler { class HOST_SW_API TagP25Data; } } @@ -336,7 +336,7 @@ namespace network friend class P25OTARService; P25OTARService* m_p25OTARService; - friend class ::RESTAPI; + friend class ::fne_restapi::RESTAPI; HostFNE* m_host; std::string m_address; diff --git a/src/fne/restapi/RESTAPI.cpp b/src/fne/restapi/RESTAPI.cpp index 7ab5ba99..b4c02828 100644 --- a/src/fne/restapi/RESTAPI.cpp +++ b/src/fne/restapi/RESTAPI.cpp @@ -24,6 +24,7 @@ using namespace network; using namespace restapi; using namespace restapi::http; using namespace lookups; +using namespace fne_restapi; #include #include @@ -43,6 +44,7 @@ using namespace lookups; // --------------------------------------------------------------------------- // Global Functions // --------------------------------------------------------------------------- +namespace fne_restapi { /** * @brief Helper to format string. @@ -490,6 +492,8 @@ TalkgroupRuleGroupVoice jsonToTG(json::object& req, HTTPPayload& reply) return groupVoice; } +} // namespace fne_restapi + // --------------------------------------------------------------------------- // Public Class Members // --------------------------------------------------------------------------- @@ -1810,8 +1814,8 @@ void RESTAPI::restAPI_GetReloadTGs(const HTTPPayload& request, HTTPPayload& repl json::object response = json::object(); setResponseDefaultStatus(response); - if (m_network != nullptr) { - m_network->m_tidLookup->reload(); + if (m_tidLookup != nullptr) { + m_tidLookup->reload(); } reply.payload(response); @@ -1828,8 +1832,8 @@ void RESTAPI::restAPI_GetReloadRIDs(const HTTPPayload& request, HTTPPayload& rep json::object response = json::object(); setResponseDefaultStatus(response); - if (m_network != nullptr) { - m_network->m_ridLookup->reload(); + if (m_ridLookup != nullptr) { + m_ridLookup->reload(); } reply.payload(response); @@ -1846,8 +1850,8 @@ void RESTAPI::restAPI_GetReloadPeerList(const HTTPPayload& request, HTTPPayload& json::object response = json::object(); setResponseDefaultStatus(response); - if (m_network != nullptr) { - m_network->m_peerListLookup->reload(); + if (m_peerListLookup != nullptr) { + m_peerListLookup->reload(); } reply.payload(response); diff --git a/src/fne/restapi/RESTAPI.h b/src/fne/restapi/RESTAPI.h index 49df4868..a87709f8 100644 --- a/src/fne/restapi/RESTAPI.h +++ b/src/fne/restapi/RESTAPI.h @@ -39,450 +39,453 @@ class HOST_SW_API HostFNE; namespace network { class HOST_SW_API TrafficNetwork; } -// --------------------------------------------------------------------------- -// Class Declaration -// --------------------------------------------------------------------------- - -/** - * @brief Implements the REST API server logic. - * @ingroup fne_rest - */ -class HOST_SW_API RESTAPI : private Thread { -public: - /** - * @brief Initializes a new instance of the RESTAPI class. - * @param address Network Hostname/IP address to connect to. - * @param port Network port number. - * @param password Authentication password. - * @param keyFile SSL certificate private key. - * @param certFile SSL certificate. - * @param enableSSL Flag indicating SSL should be used for HTTPS support. - * @param host Instance of the HostFNE class. - * @param debug Flag indicating verbose logging should be enabled. - */ - RESTAPI(const std::string& address, uint16_t port, const std::string& password, const std::string& keyFile, const std::string& certFile, - bool enableSSL, HostFNE* host, bool debug); - /** - * @brief Finalizes a instance of the RESTAPI class. - */ - ~RESTAPI() override; - - /** - * @brief Sets the instances of the Radio ID, Talkgroup ID and Peer List lookup tables. - * @param ridLookup Radio ID Lookup Table Instance - * @param tidLookup Talkgroup Rules Lookup Table Instance - * @param peerListLookup Peer List Lookup Table Instance - * @param adjPeerMapLookup Adjacent Site Map Lookup Table Instance - * @param cryptoLookup Crypto Container Instance - */ - void setLookups(::lookups::RadioIdLookup* ridLookup, ::lookups::TalkgroupRulesLookup* tidLookup, - ::lookups::PeerListLookup* peerListLookup, ::lookups::AdjSiteMapLookup* adjPeerMapLookup, - CryptoContainer* cryptoLookup); - /** - * @brief Sets the instance of the traffic network. - * @param network Instance of the TrafficNetwork class. - */ - void setNetwork(::network::TrafficNetwork* network); - - /** - * @brief Opens connection to the network. - * @returns bool True, if REST API services are started, otherwise false. - */ - bool open(); - - /** - * @brief Closes connection to the network. - */ - void close(); - -private: - typedef restapi::RequestDispatcher RESTDispatcherType; - typedef restapi::http::HTTPPayload HTTPPayload; - RESTDispatcherType m_dispatcher; - restapi::http::HTTPServer m_restServer; -#if defined(ENABLE_SSL) - restapi::http::SecureHTTPServer m_restSecureServer; - bool m_enableSSL; -#endif // ENABLE_SSL - - std::mt19937 m_random; - - std::string m_password; - uint8_t* m_passwordHash; - bool m_debug; - - HostFNE* m_host; - network::TrafficNetwork* m_network; - - ::lookups::RadioIdLookup* m_ridLookup; - ::lookups::TalkgroupRulesLookup* m_tidLookup; - ::lookups::PeerListLookup* m_peerListLookup; - ::lookups::AdjSiteMapLookup* m_adjSiteMapLookup; - CryptoContainer* m_cryptoLookup; - - typedef std::unordered_map::value_type AuthTokenValueType; - std::unordered_map m_authTokens; - - /** - * @brief Thread entry point. This function is provided to run the thread - * for the REST API services. - */ - void entry() override; - - /** - * @brief Helper to initialize REST API endpoints. - */ - void initializeEndpoints(); - - /** - * @brief Helper to invalidate a host token. - * @param host Host. - */ - void invalidateHostToken(const std::string host); - /** - * @brief Helper to validate authentication for REST API. - * @param request HTTP request. - * @param reply HTTP reply. - * @returns bool True, if authentication token is valid, otherwise false. - */ - bool validateAuth(const HTTPPayload& request, HTTPPayload& reply); - - /** - * @brief REST API endpoint; implements authentication request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutAuth(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements get version request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetVersion(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get status request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetStatus(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /* - ** Peer Operations - */ - - /** - * @brief REST API endpoint; implements get peer query request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetPeerQuery(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get peer count request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetPeerCount(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get peer reset request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutPeerReset(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put reset upstream peer connection request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutPeerResetConn(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements get peer list query request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetPeerList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put peer add request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutPeerAdd(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put peer delete request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutPeerDelete(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put peer list commit request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetPeerCommit(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put peer NAK request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutPeerNAKByPeerId(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put peer NAK request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutPeerNAKByAddress(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /* - ** Radio ID Operations - */ - - /** - * @brief REST API endpoint; implements get radio ID query request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetRIDQuery(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put radio ID add request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutRIDAdd(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put radio ID delete request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutRIDDelete(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put radio ID commit request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetRIDCommit(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /* - ** Talkgroup ID Operations - */ - - /** - * @brief REST API endpoint; implements get talkgroup ID query request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetTGQuery(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put talkgroup ID add request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutTGAdd(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put talkgroup ID delete request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutTGDelete(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put talkgroup ID commit request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetTGCommit(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /* - ** Adjacent Site Map Operations - */ - - /** - * @brief REST API endpoint; implements get adjacent site map list query request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetAdjMapList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put adjacent site map add request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutAdjMapAdd(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put adjacent site map delete request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutAdjMapDelete(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put adjacent site map commit request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetAdjMapCommit(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /* - ** Refresh and Statistics/Metadata Operations - */ - - /** - * @brief - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetForceUpdate(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements get reload talkgroup ID list request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetReloadTGs(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get reload radio ID list request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetReloadRIDs(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements get reload peer list request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetReloadPeerList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements get reload crypto container request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetReloadCrypto(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements get statistics request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetStats(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements put reset total calls request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetResetTotalCalls(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements put reset active calls request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetResetActiveCalls(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements put reset call collisions request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetResetCallCollisions(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements put reset call switches request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetResetCallSwitches(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements get unit registration list request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetUnitRegList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements get affiliation list request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetAffList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements get talkgroup grant list request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetGrantList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /** - * @brief REST API endpoint; implements get spanning tree list request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetSpanningTree(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /* - ** Digital Mobile Radio - */ - - /** - * @brief REST API endpoint; implements DMR RID operations request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutDMRRID(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - - /* - ** Project 25 - */ - - /** - * @brief REST API endpoint; implements P25 RID operation request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutP25RID(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); -}; +namespace fne_restapi +{ + // --------------------------------------------------------------------------- + // Class Declaration + // --------------------------------------------------------------------------- + + /** + * @brief Implements the REST API server logic. + * @ingroup fne_rest + */ + class HOST_SW_API RESTAPI : private Thread { + public: + /** + * @brief Initializes a new instance of the RESTAPI class. + * @param address Network Hostname/IP address to connect to. + * @param port Network port number. + * @param password Authentication password. + * @param keyFile SSL certificate private key. + * @param certFile SSL certificate. + * @param enableSSL Flag indicating SSL should be used for HTTPS support. + * @param host Instance of the HostFNE class. + * @param debug Flag indicating verbose logging should be enabled. + */ + RESTAPI(const std::string& address, uint16_t port, const std::string& password, const std::string& keyFile, const std::string& certFile, + bool enableSSL, HostFNE* host, bool debug); + /** + * @brief Finalizes a instance of the RESTAPI class. + */ + ~RESTAPI() override; + + /** + * @brief Sets the instances of the Radio ID, Talkgroup ID and Peer List lookup tables. + * @param ridLookup Radio ID Lookup Table Instance + * @param tidLookup Talkgroup Rules Lookup Table Instance + * @param peerListLookup Peer List Lookup Table Instance + * @param adjPeerMapLookup Adjacent Site Map Lookup Table Instance + * @param cryptoLookup Crypto Container Instance + */ + void setLookups(::lookups::RadioIdLookup* ridLookup, ::lookups::TalkgroupRulesLookup* tidLookup, + ::lookups::PeerListLookup* peerListLookup, ::lookups::AdjSiteMapLookup* adjPeerMapLookup, + CryptoContainer* cryptoLookup); + /** + * @brief Sets the instance of the traffic network. + * @param network Instance of the TrafficNetwork class. + */ + void setNetwork(::network::TrafficNetwork* network); + + /** + * @brief Opens connection to the network. + * @returns bool True, if REST API services are started, otherwise false. + */ + bool open(); + + /** + * @brief Closes connection to the network. + */ + void close(); + + private: + typedef restapi::RequestDispatcher RESTDispatcherType; + typedef restapi::http::HTTPPayload HTTPPayload; + RESTDispatcherType m_dispatcher; + restapi::http::HTTPServer m_restServer; + #if defined(ENABLE_SSL) + restapi::http::SecureHTTPServer m_restSecureServer; + bool m_enableSSL; + #endif // ENABLE_SSL + + std::mt19937 m_random; + + std::string m_password; + uint8_t* m_passwordHash; + bool m_debug; + + HostFNE* m_host; + network::TrafficNetwork* m_network; + + ::lookups::RadioIdLookup* m_ridLookup; + ::lookups::TalkgroupRulesLookup* m_tidLookup; + ::lookups::PeerListLookup* m_peerListLookup; + ::lookups::AdjSiteMapLookup* m_adjSiteMapLookup; + CryptoContainer* m_cryptoLookup; + + typedef std::unordered_map::value_type AuthTokenValueType; + std::unordered_map m_authTokens; + + /** + * @brief Thread entry point. This function is provided to run the thread + * for the REST API services. + */ + void entry() override; + + /** + * @brief Helper to initialize REST API endpoints. + */ + void initializeEndpoints(); + + /** + * @brief Helper to invalidate a host token. + * @param host Host. + */ + void invalidateHostToken(const std::string host); + /** + * @brief Helper to validate authentication for REST API. + * @param request HTTP request. + * @param reply HTTP reply. + * @returns bool True, if authentication token is valid, otherwise false. + */ + bool validateAuth(const HTTPPayload& request, HTTPPayload& reply); + + /** + * @brief REST API endpoint; implements authentication request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutAuth(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements get version request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetVersion(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get status request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetStatus(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /* + ** Peer Operations + */ + + /** + * @brief REST API endpoint; implements get peer query request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetPeerQuery(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get peer count request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetPeerCount(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get peer reset request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutPeerReset(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put reset upstream peer connection request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutPeerResetConn(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements get peer list query request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetPeerList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put peer add request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutPeerAdd(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put peer delete request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutPeerDelete(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put peer list commit request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetPeerCommit(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put peer NAK request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutPeerNAKByPeerId(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put peer NAK request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutPeerNAKByAddress(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /* + ** Radio ID Operations + */ + + /** + * @brief REST API endpoint; implements get radio ID query request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetRIDQuery(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put radio ID add request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutRIDAdd(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put radio ID delete request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutRIDDelete(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put radio ID commit request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetRIDCommit(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /* + ** Talkgroup ID Operations + */ + + /** + * @brief REST API endpoint; implements get talkgroup ID query request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetTGQuery(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put talkgroup ID add request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutTGAdd(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put talkgroup ID delete request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutTGDelete(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put talkgroup ID commit request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetTGCommit(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /* + ** Adjacent Site Map Operations + */ + + /** + * @brief REST API endpoint; implements get adjacent site map list query request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetAdjMapList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put adjacent site map add request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutAdjMapAdd(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put adjacent site map delete request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutAdjMapDelete(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put adjacent site map commit request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetAdjMapCommit(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /* + ** Refresh and Statistics/Metadata Operations + */ + + /** + * @brief + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetForceUpdate(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements get reload talkgroup ID list request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetReloadTGs(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get reload radio ID list request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetReloadRIDs(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements get reload peer list request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetReloadPeerList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements get reload crypto container request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetReloadCrypto(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements get statistics request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetStats(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements put reset total calls request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetResetTotalCalls(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements put reset active calls request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetResetActiveCalls(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements put reset call collisions request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetResetCallCollisions(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements put reset call switches request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetResetCallSwitches(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements get unit registration list request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetUnitRegList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements get affiliation list request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetAffList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements get talkgroup grant list request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetGrantList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /** + * @brief REST API endpoint; implements get spanning tree list request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetSpanningTree(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /* + ** Digital Mobile Radio + */ + + /** + * @brief REST API endpoint; implements DMR RID operations request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutDMRRID(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + + /* + ** Project 25 + */ + + /** + * @brief REST API endpoint; implements P25 RID operation request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutP25RID(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + }; +} // namespace fne_restapi #endif // __REST_API_H__ diff --git a/src/host/Host.Config.cpp b/src/host/Host.Config.cpp index 01b96b1e..ccc32994 100644 --- a/src/host/Host.Config.cpp +++ b/src/host/Host.Config.cpp @@ -1065,7 +1065,7 @@ bool Host::createNetwork() if (restApiEnable) { m_restAddress = restApiAddress; m_restPort = restApiPort; - m_RESTAPI = new RESTAPI(restApiAddress, restApiPort, restApiPassword, restApiSSLKey, restApiSSLCert, restApiEnableSSL, this, restApiDebug); + m_RESTAPI = new host_restapi::RESTAPI(restApiAddress, restApiPort, restApiPassword, restApiSSLKey, restApiSSLCert, restApiEnableSSL, this, restApiDebug); m_RESTAPI->setLookups(m_ridLookup, m_tidLookup); bool ret = m_RESTAPI->open(); if (!ret) { diff --git a/src/host/Host.h b/src/host/Host.h index 774847ce..dcf6230c 100644 --- a/src/host/Host.h +++ b/src/host/Host.h @@ -67,7 +67,7 @@ extern network::NetRPC* g_RPC; // Class Prototypes // --------------------------------------------------------------------------- -class HOST_SW_API RESTAPI; +namespace host_restapi { class HOST_SW_API RESTAPI; } // --------------------------------------------------------------------------- // Class Declaration @@ -232,10 +232,10 @@ private: static uint8_t m_activeTickDelay; static uint8_t m_idleTickDelay; - friend class RESTAPI; + friend class host_restapi::RESTAPI; std::string m_restAddress; uint16_t m_restPort; - RESTAPI* m_RESTAPI; + host_restapi::RESTAPI* m_RESTAPI; std::string m_rpcAddress; uint16_t m_rpcPort; diff --git a/src/host/modem/Modem.h b/src/host/modem/Modem.h index 44d0b181..51217470 100644 --- a/src/host/modem/Modem.h +++ b/src/host/modem/Modem.h @@ -75,10 +75,10 @@ class HOST_SW_API Host; class HOST_SW_API HostCal; -class HOST_SW_API RESTAPI; +namespace host_restapi { class HOST_SW_API RESTAPI; } class HOST_SW_API HostSetup; -#if defined(ENABLE_SETUP_TUI) +#if defined(ENABLE_SETUP_TUI) class HOST_SW_API SetupApplication; class HOST_SW_API SetupMainWnd; @@ -786,10 +786,10 @@ namespace modem protected: friend class ::Host; friend class ::HostCal; - friend class ::RESTAPI; + friend class ::host_restapi::RESTAPI; friend class ::HostSetup; -#if defined(ENABLE_SETUP_TUI) +#if defined(ENABLE_SETUP_TUI) friend class ::SetupApplication; friend class ::SetupMainWnd; diff --git a/src/host/restapi/RESTAPI.cpp b/src/host/restapi/RESTAPI.cpp index 5ebae98f..5b7ff630 100644 --- a/src/host/restapi/RESTAPI.cpp +++ b/src/host/restapi/RESTAPI.cpp @@ -25,6 +25,7 @@ using namespace network; using namespace restapi; using namespace restapi::http; using namespace modem; +using namespace host_restapi; #include #include @@ -44,6 +45,7 @@ using namespace modem; // --------------------------------------------------------------------------- // Global Functions // --------------------------------------------------------------------------- +namespace host_restapi { /** * @brief Helper to format string. @@ -130,6 +132,8 @@ bool parseRequestBody(const HTTPPayload& request, HTTPPayload& reply, json::obje return true; } +} // namespace host_restapi + // --------------------------------------------------------------------------- // Public Class Members // --------------------------------------------------------------------------- diff --git a/src/host/restapi/RESTAPI.h b/src/host/restapi/RESTAPI.h index 40a36361..e4cd6d57 100644 --- a/src/host/restapi/RESTAPI.h +++ b/src/host/restapi/RESTAPI.h @@ -38,362 +38,365 @@ namespace dmr { class HOST_SW_API Control; } namespace p25 { class HOST_SW_API Control; } namespace nxdn { class HOST_SW_API Control; } -// --------------------------------------------------------------------------- -// Class Declaration -// --------------------------------------------------------------------------- +namespace host_restapi +{ + // --------------------------------------------------------------------------- + // Class Declaration + // --------------------------------------------------------------------------- -/** - * @brief Implements the REST API server logic. - * @ingroup host_rest - */ -class HOST_SW_API RESTAPI : private Thread { -public: - /** - * @brief Initializes a new instance of the RESTAPI class. - * @param address Network Hostname/IP address to connect to. - * @param port Network port number. - * @param password Authentication password. - * @param keyFile SSL certificate private key. - * @param certFile SSL certificate. - * @param enableSSL Flag indicating SSL should be used for HTTPS support. - * @param host Instance of the Host class. - * @param debug Flag indicating verbose logging should be enabled. - */ - RESTAPI(const std::string& address, uint16_t port, const std::string& password, const std::string& keyFile, const std::string& certFile, - bool enableSSL, Host* host, bool debug); /** - * @brief Finalizes a instance of the RESTAPI class. - */ - ~RESTAPI() override; + * @brief Implements the REST API server logic. + * @ingroup host_rest + */ + class HOST_SW_API RESTAPI : private Thread { + public: + /** + * @brief Initializes a new instance of the RESTAPI class. + * @param address Network Hostname/IP address to connect to. + * @param port Network port number. + * @param password Authentication password. + * @param keyFile SSL certificate private key. + * @param certFile SSL certificate. + * @param enableSSL Flag indicating SSL should be used for HTTPS support. + * @param host Instance of the Host class. + * @param debug Flag indicating verbose logging should be enabled. + */ + RESTAPI(const std::string& address, uint16_t port, const std::string& password, const std::string& keyFile, const std::string& certFile, + bool enableSSL, Host* host, bool debug); + /** + * @brief Finalizes a instance of the RESTAPI class. + */ + ~RESTAPI() override; - /** - * @brief Sets the instances of the Radio ID and Talkgroup ID lookup tables. - * @param ridLookup Radio ID Lookup Table Instance - * @param tidLookup Talkgroup Rules Lookup Table Instance - */ - void setLookups(::lookups::RadioIdLookup* ridLookup, ::lookups::TalkgroupRulesLookup* tidLookup); - /** - * @brief Sets the instances of the digital radio protocols. - * @param dmr Instance of the DMR Control class. - * @param p25 Instance of the P25 Control class. - * @param nxdn Instance of the NXDN Control class. - */ - void setProtocols(dmr::Control* dmr, p25::Control* p25, nxdn::Control* nxdn); + /** + * @brief Sets the instances of the Radio ID and Talkgroup ID lookup tables. + * @param ridLookup Radio ID Lookup Table Instance + * @param tidLookup Talkgroup Rules Lookup Table Instance + */ + void setLookups(::lookups::RadioIdLookup* ridLookup, ::lookups::TalkgroupRulesLookup* tidLookup); + /** + * @brief Sets the instances of the digital radio protocols. + * @param dmr Instance of the DMR Control class. + * @param p25 Instance of the P25 Control class. + * @param nxdn Instance of the NXDN Control class. + */ + void setProtocols(dmr::Control* dmr, p25::Control* p25, nxdn::Control* nxdn); - /** - * @brief Opens connection to the network. - * @returns bool True, if REST API services are started, otherwise false. - */ - bool open(); + /** + * @brief Opens connection to the network. + * @returns bool True, if REST API services are started, otherwise false. + */ + bool open(); - /** - * @brief Closes connection to the network. - */ - void close(); + /** + * @brief Closes connection to the network. + */ + void close(); -private: - typedef restapi::RequestDispatcher RESTDispatcherType; - typedef restapi::http::HTTPPayload HTTPPayload; - RESTDispatcherType m_dispatcher; - restapi::http::HTTPServer m_restServer; -#if defined(ENABLE_SSL) - restapi::http::SecureHTTPServer m_restSecureServer; - bool m_enableSSL; -#endif // ENABLE_SSL + private: + typedef restapi::RequestDispatcher RESTDispatcherType; + typedef restapi::http::HTTPPayload HTTPPayload; + RESTDispatcherType m_dispatcher; + restapi::http::HTTPServer m_restServer; + #if defined(ENABLE_SSL) + restapi::http::SecureHTTPServer m_restSecureServer; + bool m_enableSSL; + #endif // ENABLE_SSL - std::mt19937 m_random; + std::mt19937 m_random; - uint8_t m_p25MFId; + uint8_t m_p25MFId; - std::string m_password; - uint8_t* m_passwordHash; - bool m_debug; + std::string m_password; + uint8_t* m_passwordHash; + bool m_debug; - Host* m_host; - dmr::Control* m_dmr; - p25::Control* m_p25; - nxdn::Control* m_nxdn; + Host* m_host; + dmr::Control* m_dmr; + p25::Control* m_p25; + nxdn::Control* m_nxdn; - ::lookups::RadioIdLookup* m_ridLookup; - ::lookups::TalkgroupRulesLookup* m_tidLookup; + ::lookups::RadioIdLookup* m_ridLookup; + ::lookups::TalkgroupRulesLookup* m_tidLookup; - typedef std::unordered_map::value_type AuthTokenValueType; - std::unordered_map m_authTokens; + typedef std::unordered_map::value_type AuthTokenValueType; + std::unordered_map m_authTokens; - /** - * @brief Thread entry point. This function is provided to run the thread - * for the REST API services. - */ - void entry() override; + /** + * @brief Thread entry point. This function is provided to run the thread + * for the REST API services. + */ + void entry() override; - /** - * @brief Helper to initialize REST API endpoints. - */ - void initializeEndpoints(); + /** + * @brief Helper to initialize REST API endpoints. + */ + void initializeEndpoints(); - /** - * @brief Helper to invalidate a host token. - * @param host Host. - */ - void invalidateHostToken(const std::string host); - /** - * @brief Helper to validate authentication for REST API. - * @param request HTTP request. - * @param reply HTTP reply. - * @returns bool True, if authentication token is valid, otherwise false. - */ - bool validateAuth(const HTTPPayload& request, HTTPPayload& reply); + /** + * @brief Helper to invalidate a host token. + * @param host Host. + */ + void invalidateHostToken(const std::string host); + /** + * @brief Helper to validate authentication for REST API. + * @param request HTTP request. + * @param reply HTTP reply. + * @returns bool True, if authentication token is valid, otherwise false. + */ + bool validateAuth(const HTTPPayload& request, HTTPPayload& reply); - /** - * @brief REST API endpoint; implements authentication request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutAuth(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements authentication request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutAuth(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get version request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetVersion(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get status request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetStatus(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get voice channels request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetVoiceCh(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get version request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetVersion(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get status request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetStatus(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get voice channels request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetVoiceCh(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put/set modem mode request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutModemMode(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements put/request modem kill request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutModemKill(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put/set modem mode request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutModemMode(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements put/request modem kill request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutModemKill(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements set supervisory mode request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutSetSupervisor(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements permit TG request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutPermitTG(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements grant TG request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutGrantTG(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements release grants request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetReleaseGrants(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements release affiliations request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetReleaseAffs(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements set supervisory mode request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutSetSupervisor(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements permit TG request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutPermitTG(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements grant TG request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutGrantTG(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements release grants request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetReleaseGrants(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements release affiliations request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetReleaseAffs(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get RID whitelist request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetRIDWhitelist(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get RID blacklist request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetRIDBlacklist(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get RID whitelist request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetRIDWhitelist(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get RID blacklist request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetRIDBlacklist(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /* - ** Digital Mobile Radio - */ + /* + ** Digital Mobile Radio + */ - /** - * @brief REST API endpoint; implements fire DMR beacon request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetDMRBeacon(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get DMR debug state request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetDMRDebug(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get DMR dump CSBK state request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetDMRDumpCSBK(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements DMR RID operations request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutDMRRID(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements toggle DMR CC enable request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetDMRCCEnable(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements toggle DMR CC broadcast request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetDMRCCBroadcast(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get DMR affiliations request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetDMRAffList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements fire DMR beacon request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetDMRBeacon(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get DMR debug state request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetDMRDebug(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get DMR dump CSBK state request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetDMRDumpCSBK(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements DMR RID operations request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutDMRRID(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements toggle DMR CC enable request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetDMRCCEnable(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements toggle DMR CC broadcast request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetDMRCCBroadcast(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get DMR affiliations request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetDMRAffList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /* - ** Project 25 - */ + /* + ** Project 25 + */ - /** - * @brief REST API endpoint; implements fire P25 CC request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetP25CC(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements P25 debug state request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetP25Debug(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements P25 dump TSBK state request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetP25DumpTSBK(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements P25 RID operation request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutP25RID(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements toggle P25 CC request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetP25CCEnable(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements toggle P25 broadcast request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetP25CCBroadcast(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements transmitting raw TSBK request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_PutP25RawTSBK(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get P25 affiliations request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetP25AffList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements fire P25 CC request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetP25CC(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements P25 debug state request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetP25Debug(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements P25 dump TSBK state request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetP25DumpTSBK(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements P25 RID operation request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutP25RID(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements toggle P25 CC request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetP25CCEnable(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements toggle P25 broadcast request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetP25CCBroadcast(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements transmitting raw TSBK request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_PutP25RawTSBK(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get P25 affiliations request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetP25AffList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /* - ** Next Generation Digital Narrowband - */ + /* + ** Next Generation Digital Narrowband + */ - /** - * @brief REST API endpoint; implements fire NXDN CC request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetNXDNCC(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements NXDN debug state request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetNXDNDebug(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements NXDN dump RCCH state request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetNXDNDumpRCCH(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements toggle NXDN CC request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetNXDNCCEnable(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); - /** - * @brief REST API endpoint; implements get NXDN affiliations request. - * @param request HTTP request. - * @param reply HTTP reply. - * @param match HTTP request matcher. - */ - void restAPI_GetNXDNAffList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); -}; + /** + * @brief REST API endpoint; implements fire NXDN CC request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetNXDNCC(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements NXDN debug state request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetNXDNDebug(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements NXDN dump RCCH state request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetNXDNDumpRCCH(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements toggle NXDN CC request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetNXDNCCEnable(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + /** + * @brief REST API endpoint; implements get NXDN affiliations request. + * @param request HTTP request. + * @param reply HTTP reply. + * @param match HTTP request matcher. + */ + void restAPI_GetNXDNAffList(const HTTPPayload& request, HTTPPayload& reply, const restapi::RequestMatch& match); + }; +} // namespace host_restapi #endif // __REST_API_H__ diff --git a/tests/fne/FNE_RESTAPI_Tests.cpp b/tests/fne/FNE_RESTAPI_Tests.cpp new file mode 100644 index 00000000..33097f4f --- /dev/null +++ b/tests/fne/FNE_RESTAPI_Tests.cpp @@ -0,0 +1,371 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Digital Voice Modem - Test Suite + * GPLv2 Open Source. Use is subject to license terms. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright (C) 2026 Bryan Biedenkapp, N2PLL + */ + +#include + +#include "common/lookups/RadioIdLookup.h" +#include "common/lookups/AdjSiteMapLookup.h" +#include "common/lookups/PeerListLookup.h" +#include "common/lookups/TalkgroupRulesLookup.h" +#include "common/restapi/http/HTTPPayload.h" +#include "fne/restapi/RESTAPI.h" +#include "fne/restapi/RESTDefines.h" +#include "remote/RESTClient.h" + +#include +#include +#include +#include +#include +#include +#include + +using namespace restapi::http; +using namespace fne_restapi; + +namespace { + // --------------------------------------------------------------------------- + // Global Variables + // --------------------------------------------------------------------------- + + constexpr uint32_t TEST_RID = 123456U; + const std::string TEST_PASSWORD = "fne-rest-test-password"; + + // --------------------------------------------------------------------------- + // Global Functions + // --------------------------------------------------------------------------- + + /** + * @brief Reserves an available loopback port. + * @returns uint16_t + */ + uint16_t reserveLoopbackPort() + { + asio::io_service ioService; + asio::ip::tcp::acceptor acceptor(ioService, + asio::ip::tcp::endpoint(asio::ip::address_v4::loopback(), 0U)); + return acceptor.local_endpoint().port(); + } + + /** + * @brief Generates a temporary file path for the ACL file. + * @returns std::string + */ + std::string temporaryACLPath() + { + const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count(); + return "/tmp/dvm-fne-rest-rid-" + std::to_string(stamp) + ".csv"; + } + + /** + * @brief Sends a REST API request to the specified endpoint with the given payload. + * @param port The port on which the REST API server is running. + * @param method The HTTP method to use for the request. + * @param endpoint The REST API endpoint to send the request to. + * @param payload The JSON payload to include in the request. + * @return The JSON response from the server. + */ + json::object sendRequest(uint16_t port, const std::string& method, + const std::string& endpoint, json::object payload = json::object()) + { + json::object response = json::object(); + REQUIRE(RESTClient::send("127.0.0.1", port, TEST_PASSWORD, method, endpoint, + payload, response, false, 1000, false) == HTTPPayload::OK); + REQUIRE(response["status"].get() == HTTPPayload::OK); + return response; + } + + /** + * @brief Creates a JSON payload for a talkgroup with the specified parameters. + * @param tgid The talkgroup ID. + * @param slot The slot number. + * @param name The name of the talkgroup. + * @return The JSON object representing the talkgroup. + */ + json::object talkgroupPayload(uint32_t tgid, uint8_t slot, const std::string& name) + { + json::object source = json::object(); + source["tgid"].set(tgid); + source["slot"].set(slot); + + json::object config = json::object(); + config["active"] = json::value(true); + config["affiliated"] = json::value(false); + config["parrot"] = json::value(false); + config["strapping"] = json::value((double)lookups::TG_STRAPPING_SELECTABLE); + config["inclusion"].set(json::array()); + config["exclusion"].set(json::array()); + config["rewrite"].set(json::array()); + config["always"].set(json::array()); + config["preferred"].set(json::array()); + config["permittedRids"].set(json::array()); + + json::object tg = json::object(); + tg["name"].set(name); + tg["alias"].set(name + " alias"); + tg["source"].set(source); + tg["config"].set(config); + return tg; + } + + // --------------------------------------------------------------------------- + // Class Declaration + // --------------------------------------------------------------------------- + + /** + * @brief Scoped wrapper for a temporary ACL file. + */ + class ScopedACLFile { + public: + /** + * @brief Initializes a new instance of the ScopedACLFile class. + */ + ScopedACLFile() : path(temporaryACLPath()) { } + /** + * @brief Finalizes a instance of the ScopedACLFile class. + */ + ~ScopedACLFile() { std::remove(path.c_str()); } + + std::string path; + }; + + // --------------------------------------------------------------------------- + // Class Declaration + // --------------------------------------------------------------------------- + + /** + * @brief Scoped wrapper for the RESTAPI instance used in tests. + */ + class ScopedRESTAPI { + public: + /** + * @brief Initializes a new instance of the ScopedRESTAPI class. + * @param port The port on which the REST API server will listen. + * @param ridLookup The Radio ID lookup instance. + * @param tgLookup The Talkgroup Rules lookup instance. + * @param peerLookup The Peer List lookup instance. + * @param adjLookup The Adjacent Site Map lookup instance. + */ + ScopedRESTAPI(uint16_t port, lookups::RadioIdLookup* ridLookup = nullptr, + lookups::TalkgroupRulesLookup* tgLookup = nullptr, + lookups::PeerListLookup* peerLookup = nullptr, + lookups::AdjSiteMapLookup* adjLookup = nullptr) : + api("127.0.0.1", port, TEST_PASSWORD, "", "", false, nullptr, false) + { + api.setLookups(ridLookup, tgLookup, peerLookup, adjLookup, nullptr); + REQUIRE(api.open()); + } + /** + * @brief Finalizes a instance of the ScopedRESTAPI class. + */ + ~ScopedRESTAPI() { api.close(); } + + fne_restapi::RESTAPI api; + }; +} // namespace + + +TEST_CASE("FNE REST RID add and commit updates the persisted ACL", "[fne][restapi][rid]") +{ + ScopedACLFile aclFile; + lookups::RadioIdLookup lookup(aclFile.path, 0U, true); + const uint16_t port = reserveLoopbackPort(); + ScopedRESTAPI server(port, &lookup); + + json::object add = json::object(); + add["rid"] = json::value((double)TEST_RID); + add["enabled"] = json::value(true); + add["alias"].set("REST test radio"); + add["canRequestKeys"] = json::value(true); + add["canRekey"] = json::value(true); + json::array allowedKIds = json::array(); + allowedKIds.push_back(json::value((double)0x1234U)); + allowedKIds.push_back(json::value((double)0xABCDU)); + add["allowedKIds"].set(allowedKIds); + + sendRequest(port, HTTP_PUT, FNE_PUT_RID_ADD, add); + + const lookups::RadioId added = lookup.find(TEST_RID); + REQUIRE_FALSE(added.radioDefault()); + CHECK(added.radioEnabled()); + CHECK(added.radioAlias() == "REST test radio"); + CHECK(added.canRequestKeys()); + CHECK(added.canRekey()); + CHECK(added.allowedKIds() == std::vector { 0x1234U, 0xABCDU }); + + sendRequest(port, HTTP_GET, FNE_GET_RID_COMMIT); + + lookups::RadioIdLookup persisted(aclFile.path, 0U, true); + REQUIRE(persisted.read()); + const lookups::RadioId saved = persisted.find(TEST_RID); + REQUIRE_FALSE(saved.radioDefault()); + CHECK(saved.radioAlias() == "REST test radio"); + CHECK(saved.allowedKIds() == std::vector { 0x1234U, 0xABCDU }); +} + +TEST_CASE("FNE REST RID delete and commit removes the RID from the ACL", "[fne][restapi][rid]") +{ + ScopedACLFile aclFile; + lookups::RadioIdLookup lookup(aclFile.path, 0U, true); + lookup.addEntry(TEST_RID, true, "radio to delete"); + lookup.commit(true); + + const uint16_t port = reserveLoopbackPort(); + ScopedRESTAPI server(port, &lookup); + + json::object remove = json::object(); + remove["rid"] = json::value((double)TEST_RID); + sendRequest(port, HTTP_PUT, FNE_PUT_RID_DELETE, remove); + CHECK(lookup.find(TEST_RID).radioDefault()); + + sendRequest(port, HTTP_GET, FNE_GET_RID_COMMIT); + + std::ifstream savedACL(aclFile.path); + REQUIRE(savedACL.good()); + const std::string contents((std::istreambuf_iterator(savedACL)), + std::istreambuf_iterator()); + CHECK(contents.find(std::to_string(TEST_RID)) == std::string::npos); +} + +TEST_CASE("FNE REST peer endpoints query, persist, and delete ACL entries", "[fne][restapi][peer]") +{ + ScopedACLFile aclFile; + lookups::PeerListLookup lookup(aclFile.path, 0U, true, false); + const uint16_t port = reserveLoopbackPort(); + ScopedRESTAPI server(port, nullptr, nullptr, &lookup); + + json::object add = json::object(); + add["peerId"] = json::value((double)9001U); + add["peerAlias"].set("REST peer"); + add["peerPassword"].set("peer secret"); + add["peerReplica"] = json::value(true); + add["canRequestKeys"] = json::value(true); + add["canIssueInhibit"] = json::value(true); + add["hasCallPriority"] = json::value(true); + sendRequest(port, HTTP_PUT, FNE_PUT_PEER_ADD, add); + + const lookups::PeerId peer = lookup.find(9001U); + CHECK(peer.peerAlias() == "REST peer"); + CHECK(peer.peerPassword() == "peer secret"); + CHECK(peer.peerReplica()); + CHECK(peer.canRequestKeys()); + CHECK(peer.canIssueInhibit()); + CHECK(peer.hasCallPriority()); + + json::object query = sendRequest(port, HTTP_GET, FNE_GET_PEER_LIST); + REQUIRE(query["peers"].get().size() == 1U); + CHECK(query["peers"].get()[0U].get()["peerPassword"].get()); + + sendRequest(port, HTTP_GET, FNE_GET_PEER_COMMIT); + lookups::PeerListLookup persisted(aclFile.path, 0U, true, false); + REQUIRE(persisted.read()); + CHECK(persisted.find(9001U).peerAlias() == "REST peer"); + + json::object remove = json::object(); + remove["peerId"] = json::value((double)9001U); + sendRequest(port, HTTP_PUT, FNE_PUT_PEER_DELETE, remove); + sendRequest(port, HTTP_GET, FNE_GET_PEER_COMMIT); + CHECK(lookup.find(9001U).peerDefault()); +} + +TEST_CASE("FNE REST talkgroup endpoints query, persist, and delete rules", "[fne][restapi][tg]") +{ + ScopedACLFile rulesFile; + lookups::TalkgroupRulesLookup lookup(rulesFile.path, 0U, true, false); + const uint16_t port = reserveLoopbackPort(); + ScopedRESTAPI server(port, nullptr, &lookup); + + sendRequest(port, HTTP_PUT, FNE_PUT_TGID_ADD, talkgroupPayload(3100U, 1U, "REST TG")); + const lookups::TalkgroupRuleGroupVoice added = lookup.find(3100U, 1U); + REQUIRE_FALSE(added.isInvalid()); + CHECK(added.name() == "REST TG"); + CHECK(added.config().active()); + + json::object query = sendRequest(port, HTTP_GET, FNE_GET_TGID_QUERY); + REQUIRE(query["tgs"].get().size() == 1U); + sendRequest(port, HTTP_GET, FNE_GET_TGID_COMMIT); + + lookups::TalkgroupRulesLookup persisted(rulesFile.path, 0U, true, false); + REQUIRE(persisted.read()); + CHECK_FALSE(persisted.find(3100U, 1U).isInvalid()); + + json::object remove = json::object(); + remove["tgid"] = json::value((double)3100U); + remove["slot"] = json::value((double)1U); + sendRequest(port, HTTP_PUT, FNE_PUT_TGID_DELETE, remove); + sendRequest(port, HTTP_GET, FNE_GET_TGID_COMMIT); + CHECK(lookup.find(3100U, 1U).isInvalid()); +} + +TEST_CASE("FNE REST adjacent-map endpoints query, persist, and delete entries", "[fne][restapi][adjmap]") +{ + ScopedACLFile mapFile; + lookups::AdjSiteMapLookup lookup(mapFile.path, 0U); + const uint16_t port = reserveLoopbackPort(); + ScopedRESTAPI server(port, nullptr, nullptr, nullptr, &lookup); + + json::object add = json::object(); + add["peerId"] = json::value((double)100U); + json::array neighbors = json::array(); + neighbors.push_back(json::value((double)101U)); + neighbors.push_back(json::value((double)102U)); + add["neighbors"].set(neighbors); + sendRequest(port, HTTP_PUT, FNE_PUT_ADJ_MAP_ADD, add); + + CHECK(lookup.find(100U).neighbors() == std::vector { 101U, 102U }); + json::object query = sendRequest(port, HTTP_GET, FNE_GET_ADJ_MAP_LIST); + REQUIRE(query["peers"].get().size() == 1U); + sendRequest(port, HTTP_GET, FNE_GET_ADJ_MAP_COMMIT); + + lookups::AdjSiteMapLookup persisted(mapFile.path, 0U); + REQUIRE(persisted.read()); + CHECK(persisted.find(100U).neighbors() == std::vector { 101U, 102U }); + + json::object remove = json::object(); + remove["peerId"] = json::value((double)100U); + sendRequest(port, HTTP_PUT, FNE_PUT_ADJ_MAP_DELETE, remove); + sendRequest(port, HTTP_GET, FNE_GET_ADJ_MAP_COMMIT); + CHECK(lookup.find(100U).neighbors().empty()); +} + +TEST_CASE("FNE REST reload endpoints refresh their configured lookup tables", "[fne][restapi][reload]") +{ + ScopedACLFile ridFile; + ScopedACLFile peerFile; + ScopedACLFile tgFile; + lookups::RadioIdLookup rids(ridFile.path, 0U, true); + lookups::PeerListLookup peers(peerFile.path, 0U, true, false); + lookups::TalkgroupRulesLookup tgs(tgFile.path, 0U, true, false); + + rids.addEntry(1001U, true, "persisted RID"); + rids.commit(true); + lookups::PeerId peer(2001U, "persisted peer", "secret", false); + peers.addEntry(2001U, peer); + peers.commit(true); + + const uint16_t port = reserveLoopbackPort(); + ScopedRESTAPI server(port, &rids, &tgs, &peers); + sendRequest(port, HTTP_PUT, FNE_PUT_TGID_ADD, talkgroupPayload(3001U, 1U, "persisted TG")); + sendRequest(port, HTTP_GET, FNE_GET_TGID_COMMIT); + + rids.addEntry(1002U, true, "memory only"); + peers.addEntry(2002U, lookups::PeerId(2002U, "memory only", "", false)); + sendRequest(port, HTTP_PUT, FNE_PUT_TGID_ADD, talkgroupPayload(3002U, 1U, "memory only")); + + sendRequest(port, HTTP_GET, FNE_GET_RELOAD_RIDS); + sendRequest(port, HTTP_GET, FNE_GET_RELOAD_PEERLIST); + sendRequest(port, HTTP_GET, FNE_GET_RELOAD_TGS); + + CHECK_FALSE(rids.find(1001U).radioDefault()); + CHECK(rids.find(1002U).radioDefault()); + CHECK_FALSE(peers.find(2001U).peerDefault()); + CHECK(peers.find(2002U).peerDefault()); + CHECK_FALSE(tgs.find(3001U, 1U).isInvalid()); + CHECK(tgs.find(3002U, 1U).isInvalid()); +} diff --git a/tests/p25/UserAlias_Tests.cpp b/tests/p25/UserAlias_Tests.cpp index c447f1bd..c29a0d3e 100644 --- a/tests/p25/UserAlias_Tests.cpp +++ b/tests/p25/UserAlias_Tests.cpp @@ -2,6 +2,10 @@ /* * Digital Voice Modem - Test Suite * GPLv2 Open Source. Use is subject to license terms. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright (C) 2026 Bryan Biedenkapp, N2PLL + * */ #include "host/Defines.h" #include "common/p25/P25Defines.h"