From ac34383dfaa2503ef06a95d12277d9ba8533e1b4 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 21 Feb 2023 13:41:37 -0800 Subject: [PATCH] https://telecominfraproject.atlassian.net/browse/WIFI-12068 Signed-off-by: stephb9959 --- .clang-format | 178 ++ build | 2 +- src/ConfigMaker.cpp | 565 ++-- src/ConfigMaker.h | 28 +- src/Daemon.cpp | 64 +- src/Daemon.h | 53 +- src/Dashboard.cpp | 6 +- src/Dashboard.h | 18 +- src/RESTAPI/RESTAPI_action_handler.cpp | 109 +- src/RESTAPI/RESTAPI_action_handler.h | 49 +- src/RESTAPI/RESTAPI_claim_handler.cpp | 45 +- src/RESTAPI/RESTAPI_claim_handler.h | 38 +- src/RESTAPI/RESTAPI_mfa_handler.cpp | 16 +- src/RESTAPI/RESTAPI_mfa_handler.h | 39 +- src/RESTAPI/RESTAPI_oauth2_handler.cpp | 25 +- src/RESTAPI/RESTAPI_oauth2_handler.h | 43 +- src/RESTAPI/RESTAPI_routers.cpp | 61 +- src/RESTAPI/RESTAPI_signup_handler.cpp | 68 +- src/RESTAPI/RESTAPI_signup_handler.h | 60 +- src/RESTAPI/RESTAPI_stats_handler.cpp | 44 +- src/RESTAPI/RESTAPI_stats_handler.h | 36 +- src/RESTAPI/RESTAPI_subscriber_handler.cpp | 579 ++-- src/RESTAPI/RESTAPI_subscriber_handler.h | 42 +- src/RESTAPI/RESTAPI_wifiClients_handler.cpp | 209 +- src/RESTAPI/RESTAPI_wifiClients_handler.h | 51 +- src/RESTAPI/RESTAPI_wiredClients_handler.cpp | 206 +- src/RESTAPI/RESTAPI_wiredClients_handler.h | 51 +- src/RESTObjects/RESTAPI_AnalyticsObjects.cpp | 1121 ++++---- src/RESTObjects/RESTAPI_AnalyticsObjects.h | 742 +++-- src/RESTObjects/RESTAPI_CertObjects.cpp | 384 +-- src/RESTObjects/RESTAPI_CertObjects.h | 204 +- src/RESTObjects/RESTAPI_FMSObjects.cpp | 532 ++-- src/RESTObjects/RESTAPI_FMSObjects.h | 248 +- src/RESTObjects/RESTAPI_GWobjects.cpp | 612 ++--- src/RESTObjects/RESTAPI_GWobjects.h | 219 +- src/RESTObjects/RESTAPI_OWLSobjects.cpp | 170 +- src/RESTObjects/RESTAPI_OWLSobjects.h | 111 +- src/RESTObjects/RESTAPI_ProvObjects.cpp | 2409 ++++++++--------- src/RESTObjects/RESTAPI_ProvObjects.h | 1442 +++++----- src/RESTObjects/RESTAPI_SecurityObjects.cpp | 980 +++---- src/RESTObjects/RESTAPI_SecurityObjects.h | 599 ++-- src/RESTObjects/RESTAPI_SubObjects.cpp | 1088 ++++---- src/RESTObjects/RESTAPI_SubObjects.h | 522 ++-- src/StatsSvr.cpp | 207 +- src/StatsSvr.h | 195 +- src/StorageService.cpp | 22 +- src/StorageService.h | 38 +- src/SubscriberCache.cpp | 43 +- src/SubscriberCache.h | 41 +- src/framework/ALBserver.cpp | 43 +- src/framework/ALBserver.h | 38 +- src/framework/API_Proxy.h | 156 +- src/framework/AppServiceRegistry.h | 49 +- src/framework/AuthClient.cpp | 143 +- src/framework/AuthClient.h | 65 +- src/framework/CIDR.h | 8 +- src/framework/ConfigurationValidator.cpp | 125 +- src/framework/ConfigurationValidator.h | 64 +- src/framework/CountryCodes.h | 516 ++-- src/framework/EventBusManager.cpp | 29 +- src/framework/EventBusManager.h | 11 +- src/framework/KafkaManager.cpp | 303 ++- src/framework/KafkaManager.h | 92 +- src/framework/KafkaTopics.h | 9 +- src/framework/MicroService.cpp | 617 +++-- src/framework/MicroService.h | 209 +- src/framework/MicroServiceErrorHandler.h | 259 +- src/framework/MicroServiceExtra.h | 102 +- src/framework/MicroServiceFuncs.cpp | 160 +- src/framework/MicroServiceFuncs.h | 37 +- src/framework/MicroServiceNames.h | 18 +- src/framework/OpenAPIRequests.cpp | 455 ++-- src/framework/OpenAPIRequests.h | 125 +- src/framework/OpenWifiTypes.h | 109 +- src/framework/RESTAPI_ExtServer.cpp | 13 +- src/framework/RESTAPI_ExtServer.h | 64 +- .../RESTAPI_GenericServerAccounting.h | 74 +- src/framework/RESTAPI_Handler.cpp | 3 +- src/framework/RESTAPI_Handler.h | 678 ++--- src/framework/RESTAPI_IntServer.h | 69 +- src/framework/RESTAPI_PartHandler.h | 45 +- src/framework/RESTAPI_RateLimiter.h | 55 +- src/framework/RESTAPI_SystemCommand.h | 55 +- src/framework/RESTAPI_SystemConfiguration.h | 21 +- src/framework/RESTAPI_WebSocketServer.h | 54 +- src/framework/RESTAPI_utils.h | 322 ++- src/framework/StorageClass.h | 200 +- src/framework/SubSystemServer.cpp | 169 +- src/framework/SubSystemServer.h | 64 +- .../UI_WebSocketClientNotifications.cpp | 5 +- .../UI_WebSocketClientNotifications.h | 50 +- src/framework/UI_WebSocketClientServer.cpp | 291 +- src/framework/UI_WebSocketClientServer.h | 120 +- src/framework/WebSocketLogger.h | 90 +- src/framework/orm.h | 2015 +++++++------- src/framework/ow_constants.h | 1068 ++++---- src/framework/utils.cpp | 1044 +++---- src/framework/utils.h | 123 +- src/sdks/SDK_fms.cpp | 32 +- src/sdks/SDK_fms.h | 12 +- src/sdks/SDK_gw.cpp | 405 +-- src/sdks/SDK_gw.h | 39 +- src/sdks/SDK_prov.cpp | 337 +-- src/sdks/SDK_prov.h | 47 +- src/sdks/SDK_sec.cpp | 118 +- src/sdks/SDK_sec.h | 25 +- src/storage/storage_subscriber_info.cpp | 308 +-- src/storage/storage_subscriber_info.h | 43 +- 108 files changed, 13401 insertions(+), 13013 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..631f8b9 --- /dev/null +++ b/.clang-format @@ -0,0 +1,178 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveMacros: None +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: None +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortEnumsOnASingleLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: true +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 100 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseLabels: false +IndentCaseBlocks: false +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentRequires: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: All +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PenaltyIndentedWhitespace: 0 +PointerAlignment: Right +PPIndentWidth: -1 +ReferenceAlignment: Pointer +ReflowComments: true +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceAroundPointerQualifiers: Default +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +BitFieldColonSpacing: Both +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseCRLF: false +UseTab: Always +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE + - NS_SWIFT_NAME + - CF_SWIFT_NAME +... + diff --git a/build b/build index 7813681..62f9457 100644 --- a/build +++ b/build @@ -1 +1 @@ -5 \ No newline at end of file +6 \ No newline at end of file diff --git a/src/ConfigMaker.cpp b/src/ConfigMaker.cpp index 5706367..b37fdc9 100644 --- a/src/ConfigMaker.cpp +++ b/src/ConfigMaker.cpp @@ -5,68 +5,76 @@ #include "ConfigMaker.h" #include "RESTObjects/RESTAPI_SubObjects.h" #include "StorageService.h" -#include "sdks/SDK_prov.h" -#include "sdks/SDK_gw.h" #include "framework/utils.h" #include "nlohmann/json.hpp" +#include "sdks/SDK_gw.h" +#include "sdks/SDK_prov.h" namespace OpenWifi { - static std::string ConvertBand(const std::string &B) { - if(B=="2G") return "2G"; - if(B=="6G") return "6G"; - if(B=="5GU") return "5G-upper"; - if(B=="5GL") return "5G-lower"; - return B; - } + static std::string ConvertBand(const std::string &B) { + if (B == "2G") + return "2G"; + if (B == "6G") + return "6G"; + if (B == "5GU") + return "5G-upper"; + if (B == "5GL") + return "5G-lower"; + return B; + } - static std::vector ConvertBands(const std::vector &Bs) { - std::vector R; - for(const auto &i:Bs) - R.emplace_back(ConvertBand(i)); - return R; - } + static std::vector ConvertBands(const std::vector &Bs) { + std::vector R; + for (const auto &i : Bs) + R.emplace_back(ConvertBand(i)); + return R; + } - void CreateDHCPInfo( std::string &Subnet, const std::string &First, const std::string &Last, uint64_t & DHCPFirst, uint64_t & HowMany) { - Poco::Net::IPAddress SubnetAddr, FirstAddress, LastAddress; - auto Tokens = Poco::StringTokenizer(Subnet,"/"); - if(!Poco::Net::IPAddress::tryParse(Tokens[0],SubnetAddr) || !Poco::Net::IPAddress::tryParse(First,FirstAddress) || !Poco::Net::IPAddress::tryParse(Last,LastAddress)) { - Subnet = "192.168.1.1/24"; - DHCPFirst = 10; - HowMany = 100; - return; - } + void CreateDHCPInfo(std::string &Subnet, const std::string &First, const std::string &Last, + uint64_t &DHCPFirst, uint64_t &HowMany) { + Poco::Net::IPAddress SubnetAddr, FirstAddress, LastAddress; + auto Tokens = Poco::StringTokenizer(Subnet, "/"); + if (!Poco::Net::IPAddress::tryParse(Tokens[0], SubnetAddr) || + !Poco::Net::IPAddress::tryParse(First, FirstAddress) || + !Poco::Net::IPAddress::tryParse(Last, LastAddress)) { + Subnet = "192.168.1.1/24"; + DHCPFirst = 10; + HowMany = 100; + return; + } - if(LastAddress(FirstAddress.addr())}, - LA{*static_cast(LastAddress.addr())}; + struct in_addr FA { + *static_cast(FirstAddress.addr()) + }, LA{*static_cast(LastAddress.addr())}; - HowMany = htonl(LA.s_addr) - htonl(FA.s_addr); - auto SubNetBits = std::stoull(Tokens[1],nullptr,10); - uint64_t SubNetBitMask; - if(SubNetBits==8) - SubNetBitMask = 0x000000ff ; - else if(SubNetBits==16) - SubNetBitMask = 0x0000ffff ; - else - SubNetBitMask = 0x000000ff; - DHCPFirst = htonl(FA.s_addr) & SubNetBitMask; - } + HowMany = htonl(LA.s_addr) - htonl(FA.s_addr); + auto SubNetBits = std::stoull(Tokens[1], nullptr, 10); + uint64_t SubNetBitMask; + if (SubNetBits == 8) + SubNetBitMask = 0x000000ff; + else if (SubNetBits == 16) + SubNetBitMask = 0x0000ffff; + else + SubNetBitMask = 0x000000ff; + DHCPFirst = htonl(FA.s_addr) & SubNetBitMask; + } -// #define __DBG__ std::cout << __LINE__ << std::endl ; -// #define __DBG__ - bool ConfigMaker::Prepare() { + // #define __DBG__ std::cout << __LINE__ << std::endl ; + // #define __DBG__ + bool ConfigMaker::Prepare() { - SubObjects::SubscriberInfo SI; - if(!StorageService()->SubInfoDB().GetRecord("id", id_, SI)) { - bad_ = true; - return false; - } + SubObjects::SubscriberInfo SI; + if (!StorageService()->SubInfoDB().GetRecord("id", id_, SI)) { + bad_ = true; + return false; + } - // We need to create the basic sections - auto metrics = R"( + // We need to create the basic sections + auto metrics = R"( { "metrics": { "dhcp-snooping": { @@ -109,7 +117,7 @@ namespace OpenWifi { } )"_json; - auto services = R"( + auto services = R"( { "services": { "lldp": { @@ -124,254 +132,253 @@ namespace OpenWifi { } } )"_json; - for(auto &i:SI.accessPoints.list) { + for (auto &i : SI.accessPoints.list) { - nlohmann::json Interfaces; - nlohmann::json UpstreamInterface; - nlohmann::json DownstreamInterface; - nlohmann::json radios; + nlohmann::json Interfaces; + nlohmann::json UpstreamInterface; + nlohmann::json DownstreamInterface; + nlohmann::json radios; - if(i.macAddress.empty()) - continue; + if (i.macAddress.empty()) + continue; - Logger_.information(fmt::format("{}: Generating configuration.",i.macAddress)); + Logger_.information(fmt::format("{}: Generating configuration.", i.macAddress)); - UpstreamInterface["name"] = "WAN"; - UpstreamInterface["role"] = "upstream"; - UpstreamInterface["services"].push_back("lldp"); + UpstreamInterface["name"] = "WAN"; + UpstreamInterface["role"] = "upstream"; + UpstreamInterface["services"].push_back("lldp"); - std::vector AllBands; - for(const auto &rr:i.radios) - AllBands.emplace_back(ConvertBand(rr.band)); + std::vector AllBands; + for (const auto &rr : i.radios) + AllBands.emplace_back(ConvertBand(rr.band)); - nlohmann::json UpstreamPort, DownstreamPort; - if(i.internetConnection.type=="manual") { - UpstreamInterface["addressing"] = "static"; - UpstreamInterface["subnet"] = i.internetConnection.subnetMask; - UpstreamInterface["gateway"] = i.internetConnection.defaultGateway; - UpstreamInterface["send-hostname"] = i.internetConnection.sendHostname; - UpstreamInterface["use-dns"].push_back(i.internetConnection.primaryDns); - if(!i.internetConnection.secondaryDns.empty()) - UpstreamInterface["use-dns"].push_back(i.internetConnection.secondaryDns); - } else if(i.internetConnection.type=="pppoe") { - nlohmann::json Port; - Port["select-ports"].push_back("WAN*"); - UpstreamInterface["ethernet"].push_back(Port); - UpstreamInterface["broad-band"]["protocol"] = "pppoe"; - UpstreamInterface["broad-band"]["user-name"] = i.internetConnection.username; - UpstreamInterface["broad-band"]["password"] = i.internetConnection.password; - UpstreamInterface["ipv4"]["addressing"] = "dynamic"; - if(i.internetConnection.ipV6Support) - UpstreamInterface["ipv6"]["addressing"] = "dynamic"; - } else if(i.internetConnection.type=="automatic") { - nlohmann::json Port; - Port["select-ports"].push_back("WAN*"); - if(i.deviceMode.type=="bridge") - Port["select-ports"].push_back("LAN*"); - UpstreamInterface["ethernet"].push_back(Port); - UpstreamInterface["ipv4"]["addressing"] = "dynamic"; - if(i.internetConnection.ipV6Support) - UpstreamInterface["ipv6"]["addressing"] = "dynamic"; - } + nlohmann::json UpstreamPort, DownstreamPort; + if (i.internetConnection.type == "manual") { + UpstreamInterface["addressing"] = "static"; + UpstreamInterface["subnet"] = i.internetConnection.subnetMask; + UpstreamInterface["gateway"] = i.internetConnection.defaultGateway; + UpstreamInterface["send-hostname"] = i.internetConnection.sendHostname; + UpstreamInterface["use-dns"].push_back(i.internetConnection.primaryDns); + if (!i.internetConnection.secondaryDns.empty()) + UpstreamInterface["use-dns"].push_back(i.internetConnection.secondaryDns); + } else if (i.internetConnection.type == "pppoe") { + nlohmann::json Port; + Port["select-ports"].push_back("WAN*"); + UpstreamInterface["ethernet"].push_back(Port); + UpstreamInterface["broad-band"]["protocol"] = "pppoe"; + UpstreamInterface["broad-band"]["user-name"] = i.internetConnection.username; + UpstreamInterface["broad-band"]["password"] = i.internetConnection.password; + UpstreamInterface["ipv4"]["addressing"] = "dynamic"; + if (i.internetConnection.ipV6Support) + UpstreamInterface["ipv6"]["addressing"] = "dynamic"; + } else if (i.internetConnection.type == "automatic") { + nlohmann::json Port; + Port["select-ports"].push_back("WAN*"); + if (i.deviceMode.type == "bridge") + Port["select-ports"].push_back("LAN*"); + UpstreamInterface["ethernet"].push_back(Port); + UpstreamInterface["ipv4"]["addressing"] = "dynamic"; + if (i.internetConnection.ipV6Support) + UpstreamInterface["ipv6"]["addressing"] = "dynamic"; + } - if(i.deviceMode.type=="bridge") { - UpstreamPort["select-ports"].push_back("LAN*"); - UpstreamPort["select-ports"].push_back("WAN*"); - } else if(i.deviceMode.type=="manual") { - UpstreamPort.push_back("WAN*"); - DownstreamPort.push_back("LAN*"); - DownstreamInterface["name"] = "LAN"; - DownstreamInterface["role"] = "downstream"; - DownstreamInterface["services"].push_back("lldp"); - DownstreamInterface["services"].push_back("ssh"); - DownstreamInterface["ipv4"]["addressing"] = "static"; - uint64_t HowMany=0; - uint64_t FirstIPInRange; - CreateDHCPInfo(i.deviceMode.subnet,i.deviceMode.startIP,i.deviceMode.endIP,FirstIPInRange,HowMany); - DownstreamInterface["ipv4"]["subnet"] = i.deviceMode.subnet; - DownstreamInterface["ipv4"]["dhcp"]["lease-first"] = FirstIPInRange; - DownstreamInterface["ipv4"]["dhcp"]["lease-count"] = HowMany; - DownstreamInterface["ipv4"]["dhcp"]["lease-time"] = i.deviceMode.leaseTime.empty() ? "24h" : i.deviceMode.leaseTime; - } else if(i.deviceMode.type=="nat") { - UpstreamPort["select-ports"].push_back("WAN*"); - DownstreamPort["select-ports"].push_back("LAN*"); - DownstreamInterface["name"] = "LAN"; - DownstreamInterface["role"] = "downstream"; - DownstreamInterface["services"].push_back("lldp"); - DownstreamInterface["services"].push_back("ssh"); - DownstreamInterface["ipv4"]["addressing"] = "static"; - uint64_t HowMany=0; - uint64_t FirstIPInRange; - CreateDHCPInfo(i.deviceMode.subnet,i.deviceMode.startIP,i.deviceMode.endIP,FirstIPInRange,HowMany); - DownstreamInterface["ipv4"]["subnet"] = i.deviceMode.subnet; - DownstreamInterface["ipv4"]["dhcp"]["lease-first"] = FirstIPInRange; - DownstreamInterface["ipv4"]["dhcp"]["lease-count"] = HowMany; - DownstreamInterface["ipv4"]["dhcp"]["lease-time"] = i.deviceMode.leaseTime.empty() ? "24h" : i.deviceMode.leaseTime; - } - bool hasGuest=false; - nlohmann::json main_ssids, guest_ssids; - for(const auto &j:i.wifiNetworks.wifiNetworks) { - nlohmann::json ssid; - ssid["name"] = j.name ; - if(j.bands[0]=="all") { - ssid["wifi-bands"] = AllBands; - } else { - ssid["wifi-bands"] = ConvertBands(j.bands); - } - ssid["bss-mode"] = "ap"; - if(j.encryption=="wpa1-personal") { - ssid["encryption"]["proto"] = "psk"; - ssid["encryption"]["ieee80211w"] = "disabled"; - } else if(j.encryption=="wpa2-personal") { - ssid["encryption"]["proto"] = "psk2"; - ssid["encryption"]["ieee80211w"] = "disabled"; - } else if(j.encryption=="wpa3-personal") { - ssid["encryption"]["proto"] = "sae"; - ssid["encryption"]["ieee80211w"] = "required"; - } else if (j.encryption=="wpa1/2-personal") { - ssid["encryption"]["proto"] = "psk-mixed"; - ssid["encryption"]["ieee80211w"] = "disabled"; - } else if (j.encryption=="wpa2/3-personal") { - ssid["encryption"]["proto"] = "sae-mixed"; - ssid["encryption"]["ieee80211w"] = "disabled"; - } - ssid["encryption"]["key"] = j.password; - if(j.type=="main") { - main_ssids.push_back(ssid); - } - else { - hasGuest = true; - ssid["isolate-clients"] = true; - guest_ssids.push_back(ssid); - } - } + if (i.deviceMode.type == "bridge") { + UpstreamPort["select-ports"].push_back("LAN*"); + UpstreamPort["select-ports"].push_back("WAN*"); + } else if (i.deviceMode.type == "manual") { + UpstreamPort.push_back("WAN*"); + DownstreamPort.push_back("LAN*"); + DownstreamInterface["name"] = "LAN"; + DownstreamInterface["role"] = "downstream"; + DownstreamInterface["services"].push_back("lldp"); + DownstreamInterface["services"].push_back("ssh"); + DownstreamInterface["ipv4"]["addressing"] = "static"; + uint64_t HowMany = 0; + uint64_t FirstIPInRange; + CreateDHCPInfo(i.deviceMode.subnet, i.deviceMode.startIP, i.deviceMode.endIP, + FirstIPInRange, HowMany); + DownstreamInterface["ipv4"]["subnet"] = i.deviceMode.subnet; + DownstreamInterface["ipv4"]["dhcp"]["lease-first"] = FirstIPInRange; + DownstreamInterface["ipv4"]["dhcp"]["lease-count"] = HowMany; + DownstreamInterface["ipv4"]["dhcp"]["lease-time"] = + i.deviceMode.leaseTime.empty() ? "24h" : i.deviceMode.leaseTime; + } else if (i.deviceMode.type == "nat") { + UpstreamPort["select-ports"].push_back("WAN*"); + DownstreamPort["select-ports"].push_back("LAN*"); + DownstreamInterface["name"] = "LAN"; + DownstreamInterface["role"] = "downstream"; + DownstreamInterface["services"].push_back("lldp"); + DownstreamInterface["services"].push_back("ssh"); + DownstreamInterface["ipv4"]["addressing"] = "static"; + uint64_t HowMany = 0; + uint64_t FirstIPInRange; + CreateDHCPInfo(i.deviceMode.subnet, i.deviceMode.startIP, i.deviceMode.endIP, + FirstIPInRange, HowMany); + DownstreamInterface["ipv4"]["subnet"] = i.deviceMode.subnet; + DownstreamInterface["ipv4"]["dhcp"]["lease-first"] = FirstIPInRange; + DownstreamInterface["ipv4"]["dhcp"]["lease-count"] = HowMany; + DownstreamInterface["ipv4"]["dhcp"]["lease-time"] = + i.deviceMode.leaseTime.empty() ? "24h" : i.deviceMode.leaseTime; + } + bool hasGuest = false; + nlohmann::json main_ssids, guest_ssids; + for (const auto &j : i.wifiNetworks.wifiNetworks) { + nlohmann::json ssid; + ssid["name"] = j.name; + if (j.bands[0] == "all") { + ssid["wifi-bands"] = AllBands; + } else { + ssid["wifi-bands"] = ConvertBands(j.bands); + } + ssid["bss-mode"] = "ap"; + if (j.encryption == "wpa1-personal") { + ssid["encryption"]["proto"] = "psk"; + ssid["encryption"]["ieee80211w"] = "disabled"; + } else if (j.encryption == "wpa2-personal") { + ssid["encryption"]["proto"] = "psk2"; + ssid["encryption"]["ieee80211w"] = "disabled"; + } else if (j.encryption == "wpa3-personal") { + ssid["encryption"]["proto"] = "sae"; + ssid["encryption"]["ieee80211w"] = "required"; + } else if (j.encryption == "wpa1/2-personal") { + ssid["encryption"]["proto"] = "psk-mixed"; + ssid["encryption"]["ieee80211w"] = "disabled"; + } else if (j.encryption == "wpa2/3-personal") { + ssid["encryption"]["proto"] = "sae-mixed"; + ssid["encryption"]["ieee80211w"] = "disabled"; + } + ssid["encryption"]["key"] = j.password; + if (j.type == "main") { + main_ssids.push_back(ssid); + } else { + hasGuest = true; + ssid["isolate-clients"] = true; + guest_ssids.push_back(ssid); + } + } - if(i.deviceMode.type=="bridge") - UpstreamInterface["ssids"] = main_ssids; - else - DownstreamInterface["ssids"] = main_ssids; + if (i.deviceMode.type == "bridge") + UpstreamInterface["ssids"] = main_ssids; + else + DownstreamInterface["ssids"] = main_ssids; - nlohmann::json UpStreamEthernet, - DownStreamEthernet; - if(!UpstreamPort.empty()) { - UpStreamEthernet.push_back(UpstreamPort); - } - if(!DownstreamPort.empty()) { - DownStreamEthernet.push_back(DownstreamPort); - } + nlohmann::json UpStreamEthernet, DownStreamEthernet; + if (!UpstreamPort.empty()) { + UpStreamEthernet.push_back(UpstreamPort); + } + if (!DownstreamPort.empty()) { + DownStreamEthernet.push_back(DownstreamPort); + } - if(i.deviceMode.type=="bridge") { - UpstreamInterface["ethernet"] = UpStreamEthernet; - Interfaces.push_back(UpstreamInterface); - } else { - UpstreamInterface["ethernet"] = UpStreamEthernet; - DownstreamInterface["ethernet"] = DownStreamEthernet; - Interfaces.push_back(UpstreamInterface); - Interfaces.push_back(DownstreamInterface); - } + if (i.deviceMode.type == "bridge") { + UpstreamInterface["ethernet"] = UpStreamEthernet; + Interfaces.push_back(UpstreamInterface); + } else { + UpstreamInterface["ethernet"] = UpStreamEthernet; + DownstreamInterface["ethernet"] = DownStreamEthernet; + Interfaces.push_back(UpstreamInterface); + Interfaces.push_back(DownstreamInterface); + } - if(hasGuest) { - nlohmann::json GuestInterface; - GuestInterface["name"] = "Guest"; - GuestInterface["role"] = "downstream"; - GuestInterface["isolate-hosts"] = true; - GuestInterface["ipv4"]["addressing"] = "static"; - GuestInterface["ipv4"]["subnet"] = "192.168.10.1/24"; - GuestInterface["ipv4"]["dhcp"]["lease-first"] = (uint64_t )10; - GuestInterface["ipv4"]["dhcp"]["lease-count"] = (uint64_t )100; - GuestInterface["ipv4"]["dhcp"]["lease-time"] = "6h"; - GuestInterface["ssids"] = guest_ssids; - Interfaces.push_back(GuestInterface); - } + if (hasGuest) { + nlohmann::json GuestInterface; + GuestInterface["name"] = "Guest"; + GuestInterface["role"] = "downstream"; + GuestInterface["isolate-hosts"] = true; + GuestInterface["ipv4"]["addressing"] = "static"; + GuestInterface["ipv4"]["subnet"] = "192.168.10.1/24"; + GuestInterface["ipv4"]["dhcp"]["lease-first"] = (uint64_t)10; + GuestInterface["ipv4"]["dhcp"]["lease-count"] = (uint64_t)100; + GuestInterface["ipv4"]["dhcp"]["lease-time"] = "6h"; + GuestInterface["ssids"] = guest_ssids; + Interfaces.push_back(GuestInterface); + } - for(const auto &k:i.radios) { - nlohmann::json radio; + for (const auto &k : i.radios) { + nlohmann::json radio; - radio["band"] = ConvertBand(k.band); - radio["bandwidth"] = k.bandwidth; + radio["band"] = ConvertBand(k.band); + radio["bandwidth"] = k.bandwidth; - if(k.channel==0) - radio["channel"] = "auto"; - else - radio["channel"] = k.channel; - if(k.country.size()==2) - radio["country"] = k.country; + if (k.channel == 0) + radio["channel"] = "auto"; + else + radio["channel"] = k.channel; + if (k.country.size() == 2) + radio["country"] = k.country; - radio["channel-mode"] = k.channelMode; - radio["channel-width"] = k.channelWidth; - if(!k.requireMode.empty()) - radio["require-mode"] = k.requireMode; - if(k.txpower>0) - radio["tx-power"] = k.txpower; - if(k.allowDFS) - radio["allow-dfs"] = true; - if(!k.mimo.empty()) - radio["mimo"] = k.mimo; - radio["legacy-rates"] = k.legacyRates; - radio["beacon-interval"] = k.beaconInterval; - radio["dtim-period"] = k.dtimPeriod; - radio["maximum-clients"] = k.maximumClients; - radio["rates"]["beacon"] = k.rates.beacon; - radio["rates"]["multicast"] = k.rates.multicast; - radio["he-settings"]["multiple-bssid"] = k.he.multipleBSSID; - radio["he-settings"]["ema"] = k.he.ema; - radio["he-settings"]["bss-color"] = k.he.bssColor; - radios.push_back(radio); - } + radio["channel-mode"] = k.channelMode; + radio["channel-width"] = k.channelWidth; + if (!k.requireMode.empty()) + radio["require-mode"] = k.requireMode; + if (k.txpower > 0) + radio["tx-power"] = k.txpower; + if (k.allowDFS) + radio["allow-dfs"] = true; + if (!k.mimo.empty()) + radio["mimo"] = k.mimo; + radio["legacy-rates"] = k.legacyRates; + radio["beacon-interval"] = k.beaconInterval; + radio["dtim-period"] = k.dtimPeriod; + radio["maximum-clients"] = k.maximumClients; + radio["rates"]["beacon"] = k.rates.beacon; + radio["rates"]["multicast"] = k.rates.multicast; + radio["he-settings"]["multiple-bssid"] = k.he.multipleBSSID; + radio["he-settings"]["ema"] = k.he.ema; + radio["he-settings"]["bss-color"] = k.he.bssColor; + radios.push_back(radio); + } - ProvObjects::DeviceConfigurationElement Metrics{ - .name = "metrics", - .description = "default metrics", - .weight = 0, - .configuration = to_string(metrics) - }; + ProvObjects::DeviceConfigurationElement Metrics{.name = "metrics", + .description = "default metrics", + .weight = 0, + .configuration = to_string(metrics)}; - ProvObjects::DeviceConfigurationElement Services{ - .name = "services", - .description = "default services", - .weight = 0, - .configuration = to_string(services) - }; + ProvObjects::DeviceConfigurationElement Services{.name = "services", + .description = "default services", + .weight = 0, + .configuration = to_string(services)}; - nlohmann::json InterfaceSection; - InterfaceSection["interfaces"] = Interfaces; - ProvObjects::DeviceConfigurationElement InterfacesList{ - .name = "interfaces", - .description = "default interfaces", - .weight = 0, - .configuration = to_string(InterfaceSection) - }; + nlohmann::json InterfaceSection; + InterfaceSection["interfaces"] = Interfaces; + ProvObjects::DeviceConfigurationElement InterfacesList{ + .name = "interfaces", + .description = "default interfaces", + .weight = 0, + .configuration = to_string(InterfaceSection)}; - nlohmann::json RadiosSection; - RadiosSection["radios"] = radios; - ProvObjects::DeviceConfigurationElement RadiosList{ - .name = "radios", - .description = "default radios", - .weight = 0, - .configuration = to_string(RadiosSection) - }; + nlohmann::json RadiosSection; + RadiosSection["radios"] = radios; + ProvObjects::DeviceConfigurationElement RadiosList{.name = "radios", + .description = "default radios", + .weight = 0, + .configuration = + to_string(RadiosSection)}; - ProvObjects::SubscriberDevice SubDevice; + ProvObjects::SubscriberDevice SubDevice; - if(SDK::Prov::Subscriber::GetDevice(nullptr,i.serialNumber, SubDevice)) { - SubDevice.configuration.clear(); - SubDevice.configuration.push_back(Metrics); - SubDevice.configuration.push_back(Services); - SubDevice.configuration.push_back(InterfacesList); - SubDevice.configuration.push_back(RadiosList); - SubDevice.deviceRules.firmwareUpgrade = i.automaticUpgrade ? "yes" : "no"; - if(SDK::Prov::Subscriber::SetDevice(nullptr, SubDevice)) { - Logger_.information(fmt::format("Updating configuration for {}", i.serialNumber)); - } else { - Logger_.information(fmt::format("Cannot update configuration for {}", i.serialNumber)); - } - } else { - Logger_.information(fmt::format("Could not find Subscriber device in provisioning for {}", i.serialNumber)); - } - SDK::GW::Device::SetSubscriber(nullptr, i.serialNumber, SI.id); - } - SI.modified = Utils::Now(); - return StorageService()->SubInfoDB().UpdateRecord("id",id_,SI); - } + if (SDK::Prov::Subscriber::GetDevice(nullptr, i.serialNumber, SubDevice)) { + SubDevice.configuration.clear(); + SubDevice.configuration.push_back(Metrics); + SubDevice.configuration.push_back(Services); + SubDevice.configuration.push_back(InterfacesList); + SubDevice.configuration.push_back(RadiosList); + SubDevice.deviceRules.firmwareUpgrade = i.automaticUpgrade ? "yes" : "no"; + if (SDK::Prov::Subscriber::SetDevice(nullptr, SubDevice)) { + Logger_.information( + fmt::format("Updating configuration for {}", i.serialNumber)); + } else { + Logger_.information( + fmt::format("Cannot update configuration for {}", i.serialNumber)); + } + } else { + Logger_.information(fmt::format( + "Could not find Subscriber device in provisioning for {}", i.serialNumber)); + } + SDK::GW::Device::SetSubscriber(nullptr, i.serialNumber, SI.id); + } + SI.modified = Utils::Now(); + return StorageService()->SubInfoDB().UpdateRecord("id", id_, SI); + } -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/ConfigMaker.h b/src/ConfigMaker.h index d35e3e4..d7f396f 100644 --- a/src/ConfigMaker.h +++ b/src/ConfigMaker.h @@ -4,21 +4,19 @@ #pragma once -#include "RESTObjects/RESTAPI_ProvObjects.h" #include "Poco/Logger.h" +#include "RESTObjects/RESTAPI_ProvObjects.h" namespace OpenWifi { - class ConfigMaker { - public: - explicit ConfigMaker(Poco::Logger &L, const std::string &Id): - Logger_(L), - id_(Id) { - } - bool Prepare(); - bool Push(); - private: - Poco::Logger &Logger_; - const std::string id_; - bool bad_=false; - }; -} + class ConfigMaker { + public: + explicit ConfigMaker(Poco::Logger &L, const std::string &Id) : Logger_(L), id_(Id) {} + bool Prepare(); + bool Push(); + + private: + Poco::Logger &Logger_; + const std::string id_; + bool bad_ = false; + }; +} // namespace OpenWifi diff --git a/src/Daemon.cpp b/src/Daemon.cpp index 9e63dbb..d593da6 100644 --- a/src/Daemon.cpp +++ b/src/Daemon.cpp @@ -7,9 +7,9 @@ // #include "Daemon.h" +#include "StatsSvr.h" #include "StorageService.h" #include "SubscriberCache.h" -#include "StatsSvr.h" #include "Poco/Net/SSLManager.h" #include "framework/UI_WebSocketClientServer.h" @@ -19,49 +19,41 @@ namespace OpenWifi { class Daemon *Daemon::instance() { if (instance_ == nullptr) { - instance_ = new Daemon(vDAEMON_PROPERTIES_FILENAME, - vDAEMON_ROOT_ENV_VAR, - vDAEMON_CONFIG_ENV_VAR, - vDAEMON_APP_NAME, - vDAEMON_BUS_TIMER, - SubSystemVec{ - StorageService(), - SubscriberCache(), - StatsSvr(), - UI_WebSocketClientServer() - }); + instance_ = new Daemon(vDAEMON_PROPERTIES_FILENAME, vDAEMON_ROOT_ENV_VAR, + vDAEMON_CONFIG_ENV_VAR, vDAEMON_APP_NAME, vDAEMON_BUS_TIMER, + SubSystemVec{StorageService(), SubscriberCache(), StatsSvr(), + UI_WebSocketClientServer()}); } return instance_; } - void Daemon::PostInitialization([[maybe_unused]] Poco::Util::Application &self) { - } + void Daemon::PostInitialization([[maybe_unused]] Poco::Util::Application &self) {} - void DaemonPostInitialization(Poco::Util::Application &self) { - Daemon()->PostInitialization(self); - } -} + void DaemonPostInitialization(Poco::Util::Application &self) { + Daemon()->PostInitialization(self); + } +} // namespace OpenWifi int main(int argc, char **argv) { - int ExitCode; - try { - Poco::Net::SSLManager::instance().initializeServer(nullptr, nullptr, nullptr); - auto App = OpenWifi::Daemon::instance(); - ExitCode = App->run(argc, argv); - Poco::Net::SSLManager::instance().shutdown(); - } catch (Poco::Exception &exc) { - ExitCode = Poco::Util::Application::EXIT_SOFTWARE; - std::cout << exc.displayText() << std::endl; - } catch (std::exception &exc) { - ExitCode = Poco::Util::Application::EXIT_TEMPFAIL; - std::cout << exc.what() << std::endl; - } catch (...) { - ExitCode = Poco::Util::Application::EXIT_TEMPFAIL; - std::cout << "Exception on closure" << std::endl; - } + int ExitCode; + try { + Poco::Net::SSLManager::instance().initializeServer(nullptr, nullptr, nullptr); + auto App = OpenWifi::Daemon::instance(); + ExitCode = App->run(argc, argv); + Poco::Net::SSLManager::instance().shutdown(); + } catch (Poco::Exception &exc) { + ExitCode = Poco::Util::Application::EXIT_SOFTWARE; + std::cout << exc.displayText() << std::endl; + } catch (std::exception &exc) { + ExitCode = Poco::Util::Application::EXIT_TEMPFAIL; + std::cout << exc.what() << std::endl; + } catch (...) { + ExitCode = Poco::Util::Application::EXIT_TEMPFAIL; + std::cout << "Exception on closure" << std::endl; + } - std::cout << "Exitcode: " << ExitCode << std::endl; - return ExitCode; + std::cout << "Exitcode: " << ExitCode << std::endl; + return ExitCode; } // end of namespace \ No newline at end of file diff --git a/src/Daemon.h b/src/Daemon.h index 521c595..109412a 100644 --- a/src/Daemon.h +++ b/src/Daemon.h @@ -9,43 +9,40 @@ #pragma once #include -#include #include -#include +#include #include +#include +#include "Dashboard.h" #include "framework/MicroService.h" #include "framework/MicroServiceNames.h" -#include "Dashboard.h" namespace OpenWifi { - [[maybe_unused]] static const char * vDAEMON_PROPERTIES_FILENAME = "owsub.properties"; - [[maybe_unused]] static const char * vDAEMON_ROOT_ENV_VAR = "OWSUB_ROOT"; - [[maybe_unused]] static const char * vDAEMON_CONFIG_ENV_VAR = "OWSUB_CONFIG"; - [[maybe_unused]] static const char * vDAEMON_APP_NAME = uSERVICE_SUBCRIBER.c_str() ; - [[maybe_unused]] static const uint64_t vDAEMON_BUS_TIMER = 10000; + [[maybe_unused]] static const char *vDAEMON_PROPERTIES_FILENAME = "owsub.properties"; + [[maybe_unused]] static const char *vDAEMON_ROOT_ENV_VAR = "OWSUB_ROOT"; + [[maybe_unused]] static const char *vDAEMON_CONFIG_ENV_VAR = "OWSUB_CONFIG"; + [[maybe_unused]] static const char *vDAEMON_APP_NAME = uSERVICE_SUBCRIBER.c_str(); + [[maybe_unused]] static const uint64_t vDAEMON_BUS_TIMER = 10000; - class Daemon : public MicroService { - public: - explicit Daemon(const std::string & PropFile, - const std::string & RootEnv, - const std::string & ConfigEnv, - const std::string & AppName, - uint64_t BusTimer, - const SubSystemVec & SubSystems) : - MicroService( PropFile, RootEnv, ConfigEnv, AppName, BusTimer, SubSystems) {}; + class Daemon : public MicroService { + public: + explicit Daemon(const std::string &PropFile, const std::string &RootEnv, + const std::string &ConfigEnv, const std::string &AppName, uint64_t BusTimer, + const SubSystemVec &SubSystems) + : MicroService(PropFile, RootEnv, ConfigEnv, AppName, BusTimer, SubSystems){}; - void PostInitialization(Poco::Util::Application &self); - static Daemon *instance(); - inline OpenWifi::SubDashboard & GetDashboard() { return DB_; } - Poco::Logger & Log() { return Poco::Logger::get(AppName()); } - private: - static Daemon *instance_; - OpenWifi::SubDashboard DB_{}; + void PostInitialization(Poco::Util::Application &self); + static Daemon *instance(); + inline OpenWifi::SubDashboard &GetDashboard() { return DB_; } + Poco::Logger &Log() { return Poco::Logger::get(AppName()); } - }; + private: + static Daemon *instance_; + OpenWifi::SubDashboard DB_{}; + }; - inline Daemon * Daemon() { return Daemon::instance(); } - void DaemonPostInitialization(Poco::Util::Application &self); -} + inline Daemon *Daemon() { return Daemon::instance(); } + void DaemonPostInitialization(Poco::Util::Application &self); +} // namespace OpenWifi diff --git a/src/Dashboard.cpp b/src/Dashboard.cpp index 501f0dc..f28024b 100644 --- a/src/Dashboard.cpp +++ b/src/Dashboard.cpp @@ -9,11 +9,11 @@ #include "framework/utils.h" namespace OpenWifi { - void SubDashboard::Create() { + void SubDashboard::Create() { uint64_t Now = Utils::Now(); - if(LastRun_==0 || (Now-LastRun_)>120) { + if (LastRun_ == 0 || (Now - LastRun_) > 120) { DB_.reset(); LastRun_ = Now; } } -} +} // namespace OpenWifi diff --git a/src/Dashboard.h b/src/Dashboard.h index 329c31c..877999e 100644 --- a/src/Dashboard.h +++ b/src/Dashboard.h @@ -8,19 +8,23 @@ #ifndef UCENTRALGW_DASHBOARD_H #define UCENTRALGW_DASHBOARD_H -#include "framework/OpenWifiTypes.h" #include "RESTObjects/RESTAPI_ProvObjects.h" +#include "framework/OpenWifiTypes.h" namespace OpenWifi { class SubDashboard { public: - void Create(); - [[nodiscard]] const ProvObjects::Report & Report() const { return DB_;} - inline void Reset() { LastRun_=0; DB_.reset(); } + void Create(); + [[nodiscard]] const ProvObjects::Report &Report() const { return DB_; } + inline void Reset() { + LastRun_ = 0; + DB_.reset(); + } + private: - ProvObjects::Report DB_{}; - uint64_t LastRun_=0; + ProvObjects::Report DB_{}; + uint64_t LastRun_ = 0; }; -} +} // namespace OpenWifi #endif // UCENTRALGW_DASHBOARD_H diff --git a/src/RESTAPI/RESTAPI_action_handler.cpp b/src/RESTAPI/RESTAPI_action_handler.cpp index 2ecfcc4..e7d7955 100644 --- a/src/RESTAPI/RESTAPI_action_handler.cpp +++ b/src/RESTAPI/RESTAPI_action_handler.cpp @@ -3,66 +3,67 @@ // #include "RESTAPI_action_handler.h" -#include "SubscriberCache.h" -#include "StorageService.h" -#include "sdks/SDK_gw.h" #include "ConfigMaker.h" +#include "StorageService.h" +#include "SubscriberCache.h" +#include "sdks/SDK_gw.h" namespace OpenWifi { - void RESTAPI_action_handler::DoPost() { - auto Command = GetParameter("action",""); + void RESTAPI_action_handler::DoPost() { + auto Command = GetParameter("action", ""); - if(Command.empty()) { - return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); - } + if (Command.empty()) { + return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); + } - const auto & Body = ParsedBody_; - std::string Mac, ImageName,Pattern{"blink"}; - AssignIfPresent(Body,"mac",Mac); + const auto &Body = ParsedBody_; + std::string Mac, ImageName, Pattern{"blink"}; + AssignIfPresent(Body, "mac", Mac); - Poco::toLowerInPlace(Mac); - Poco::trimInPlace(Mac); - if(Mac.empty()) { - return BadRequest(RESTAPI::Errors::MissingSerialNumber); - } - uint64_t When=0, Duration = 30; - bool keepRedirector=true; - AssignIfPresent(Body, "when",When); - AssignIfPresent(Body, "duration", Duration); - AssignIfPresent(Body, "uri", ImageName); - AssignIfPresent(Body, "pattern", Pattern); - AssignIfPresent(Body, "keepRedirector",keepRedirector); + Poco::toLowerInPlace(Mac); + Poco::trimInPlace(Mac); + if (Mac.empty()) { + return BadRequest(RESTAPI::Errors::MissingSerialNumber); + } + uint64_t When = 0, Duration = 30; + bool keepRedirector = true; + AssignIfPresent(Body, "when", When); + AssignIfPresent(Body, "duration", Duration); + AssignIfPresent(Body, "uri", ImageName); + AssignIfPresent(Body, "pattern", Pattern); + AssignIfPresent(Body, "keepRedirector", keepRedirector); - Poco::SharedPtr SubInfo; - auto UserFound = SubscriberCache()->GetSubInfo(UserInfo_.userinfo.id,SubInfo); - if(!UserFound) { - SubObjects::SubscriberInfo SI; - if(!StorageService()->SubInfoDB().GetRecord("id",UserInfo_.userinfo.id,SI)) - return BadRequest(RESTAPI::Errors::SubNoDeviceActivated); - } + Poco::SharedPtr SubInfo; + auto UserFound = SubscriberCache()->GetSubInfo(UserInfo_.userinfo.id, SubInfo); + if (!UserFound) { + SubObjects::SubscriberInfo SI; + if (!StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id, SI)) + return BadRequest(RESTAPI::Errors::SubNoDeviceActivated); + } - for(const auto &i:SubInfo->accessPoints.list) { - if(i.macAddress == Mac) { - if(Command == "reboot") { - return SDK::GW::Device::Reboot(this, i.serialNumber, When); - } else if(Command == "blink") { - return SDK::GW::Device::LEDs(this, i.serialNumber, When, Duration, Pattern); - } else if(Command == "upgrade") { - return SDK::GW::Device::Upgrade(this, i.serialNumber, When, i.latestFirmwareURI, keepRedirector); - } else if(Command == "factory") { - return SDK::GW::Device::Factory(this, i.serialNumber, When, keepRedirector); - } else if(Command == "refresh") { - ConfigMaker InitialConfig(Logger(),UserInfo_.userinfo.id); - if(InitialConfig.Prepare()) - return OK(); - else - return InternalError(RESTAPI::Errors::SubConfigNotRefreshed); - } else { - return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); - } - } - } - return NotFound(); - } -} \ No newline at end of file + for (const auto &i : SubInfo->accessPoints.list) { + if (i.macAddress == Mac) { + if (Command == "reboot") { + return SDK::GW::Device::Reboot(this, i.serialNumber, When); + } else if (Command == "blink") { + return SDK::GW::Device::LEDs(this, i.serialNumber, When, Duration, Pattern); + } else if (Command == "upgrade") { + return SDK::GW::Device::Upgrade(this, i.serialNumber, When, i.latestFirmwareURI, + keepRedirector); + } else if (Command == "factory") { + return SDK::GW::Device::Factory(this, i.serialNumber, When, keepRedirector); + } else if (Command == "refresh") { + ConfigMaker InitialConfig(Logger(), UserInfo_.userinfo.id); + if (InitialConfig.Prepare()) + return OK(); + else + return InternalError(RESTAPI::Errors::SubConfigNotRefreshed); + } else { + return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); + } + } + } + return NotFound(); + } +} // namespace OpenWifi \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_action_handler.h b/src/RESTAPI/RESTAPI_action_handler.h index e84346c..b7bab6a 100644 --- a/src/RESTAPI/RESTAPI_action_handler.h +++ b/src/RESTAPI/RESTAPI_action_handler.h @@ -7,32 +7,31 @@ #include "framework/RESTAPI_Handler.h" namespace OpenWifi { - class RESTAPI_action_handler : public RESTAPIHandler { - public: - RESTAPI_action_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId, bool Internal) - : RESTAPIHandler(bindings, L, - std::vector{ - Poco::Net::HTTPRequest::HTTP_POST, - Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Server, - TransactionId, - Internal,true, false, RateLimit{}, true){} + class RESTAPI_action_handler : public RESTAPIHandler { + public: + RESTAPI_action_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, + bool Internal) + : RESTAPIHandler(bindings, L, + std::vector{Poco::Net::HTTPRequest::HTTP_POST, + Poco::Net::HTTPRequest::HTTP_OPTIONS}, + Server, TransactionId, Internal, true, false, RateLimit{}, true) {} - static auto PathName() { return std::list{"/api/v1/action"}; }; + static auto PathName() { return std::list{"/api/v1/action"}; }; - inline bool RoleIsAuthorized(std::string & Reason) { - if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { - Reason = "User must be a subscriber"; - return false; - } - return true; - } + inline bool RoleIsAuthorized(std::string &Reason) { + if (UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } - void DoGet() final {}; - void DoPost() final; - void DoPut() final {}; - void DoDelete() final {}; - private: + void DoGet() final{}; + void DoPost() final; + void DoPut() final{}; + void DoDelete() final{}; - }; -} + private: + }; +} // namespace OpenWifi diff --git a/src/RESTAPI/RESTAPI_claim_handler.cpp b/src/RESTAPI/RESTAPI_claim_handler.cpp index 876cae8..094ea02 100644 --- a/src/RESTAPI/RESTAPI_claim_handler.cpp +++ b/src/RESTAPI/RESTAPI_claim_handler.cpp @@ -8,33 +8,32 @@ namespace OpenWifi { - void RESTAPI_claim_handler::DoPut() { - std::string SerialNumber, Id; + void RESTAPI_claim_handler::DoPut() { + std::string SerialNumber, Id; - if(!HasParameter("serialNumber",SerialNumber) || !HasParameter("id",Id)) { - return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); - } + if (!HasParameter("serialNumber", SerialNumber) || !HasParameter("id", Id)) { + return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); + } - if(!Utils::ValidSerialNumber(SerialNumber)) { - return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); - } + if (!Utils::ValidSerialNumber(SerialNumber)) { + return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); + } - std::string EndPoint{"/api/v1/inventory"}; - Poco::JSON::Object Body; - OpenAPIRequestPut API(uSERVICE_PROVISIONING, EndPoint, - { { "serialNumber" , SerialNumber }, - { "claimer" , UserInfo_.userinfo.id }}, - Body, 20000); + std::string EndPoint{"/api/v1/inventory"}; + Poco::JSON::Object Body; + OpenAPIRequestPut API(uSERVICE_PROVISIONING, EndPoint, + {{"serialNumber", SerialNumber}, {"claimer", UserInfo_.userinfo.id}}, + Body, 20000); - Poco::JSON::Object::Ptr CallResponse; + Poco::JSON::Object::Ptr CallResponse; - auto StatusCode = API.Do(CallResponse, UserInfo_.userinfo.id); + auto StatusCode = API.Do(CallResponse, UserInfo_.userinfo.id); - if(StatusCode == Poco::Net::HTTPServerResponse::HTTP_OK) { - return ReturnObject(*CallResponse); - } else { - return ReturnStatus(StatusCode); - } - } + if (StatusCode == Poco::Net::HTTPServerResponse::HTTP_OK) { + return ReturnObject(*CallResponse); + } else { + return ReturnStatus(StatusCode); + } + } -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_claim_handler.h b/src/RESTAPI/RESTAPI_claim_handler.h index b8a7846..4b433a4 100644 --- a/src/RESTAPI/RESTAPI_claim_handler.h +++ b/src/RESTAPI/RESTAPI_claim_handler.h @@ -7,24 +7,24 @@ #include "framework/RESTAPI_Handler.h" namespace OpenWifi { - class RESTAPI_claim_handler : public RESTAPIHandler { - public: - RESTAPI_claim_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId, bool Internal) - : RESTAPIHandler(bindings, L, - std::vector{ - Poco::Net::HTTPRequest::HTTP_GET, - Poco::Net::HTTPRequest::HTTP_PUT, - Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Server, - TransactionId, - Internal){} + class RESTAPI_claim_handler : public RESTAPIHandler { + public: + RESTAPI_claim_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, + bool Internal) + : RESTAPIHandler(bindings, L, + std::vector{Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_PUT, + Poco::Net::HTTPRequest::HTTP_OPTIONS}, + Server, TransactionId, Internal) {} - static auto PathName() { return std::list{"/api/v1/claim"}; }; + static auto PathName() { return std::list{"/api/v1/claim"}; }; - void DoGet() final {}; - void DoPost() final {}; - void DoPut() final; - void DoDelete() final {}; - private: - }; -} + void DoGet() final{}; + void DoPost() final{}; + void DoPut() final; + void DoDelete() final{}; + + private: + }; +} // namespace OpenWifi diff --git a/src/RESTAPI/RESTAPI_mfa_handler.cpp b/src/RESTAPI/RESTAPI_mfa_handler.cpp index e11c5da..261a6ac 100644 --- a/src/RESTAPI/RESTAPI_mfa_handler.cpp +++ b/src/RESTAPI/RESTAPI_mfa_handler.cpp @@ -8,12 +8,14 @@ namespace OpenWifi { - void RESTAPI_mfa_handler::DoPut() { - API_Proxy(Logger_, Request, Response, OpenWifi::uSERVICE_SECURITY.c_str(),"/api/v1/submfa"); - } + void RESTAPI_mfa_handler::DoPut() { + API_Proxy(Logger_, Request, Response, OpenWifi::uSERVICE_SECURITY.c_str(), + "/api/v1/submfa"); + } - void RESTAPI_mfa_handler::DoGet() { - API_Proxy(Logger_, Request, Response, OpenWifi::uSERVICE_SECURITY.c_str(),"/api/v1/submfa"); - } + void RESTAPI_mfa_handler::DoGet() { + API_Proxy(Logger_, Request, Response, OpenWifi::uSERVICE_SECURITY.c_str(), + "/api/v1/submfa"); + } -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_mfa_handler.h b/src/RESTAPI/RESTAPI_mfa_handler.h index b755357..4fa6d17 100644 --- a/src/RESTAPI/RESTAPI_mfa_handler.h +++ b/src/RESTAPI/RESTAPI_mfa_handler.h @@ -6,24 +6,25 @@ #include "framework/RESTAPI_Handler.h" namespace OpenWifi { - class RESTAPI_mfa_handler : public RESTAPIHandler { - public: - RESTAPI_mfa_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId, bool Internal) - : RESTAPIHandler(bindings, L, - std::vector{ - Poco::Net::HTTPRequest::HTTP_GET, - Poco::Net::HTTPRequest::HTTP_PUT, - Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Server, - TransactionId, - Internal, true, false, RateLimit{.Interval=1000,.MaxCalls=10}, true){} + class RESTAPI_mfa_handler : public RESTAPIHandler { + public: + RESTAPI_mfa_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, + bool Internal) + : RESTAPIHandler(bindings, L, + std::vector{Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_PUT, + Poco::Net::HTTPRequest::HTTP_OPTIONS}, + Server, TransactionId, Internal, true, false, + RateLimit{.Interval = 1000, .MaxCalls = 10}, true) {} - static auto PathName() { return std::list{"/api/v1/submfa"}; }; + static auto PathName() { return std::list{"/api/v1/submfa"}; }; - void DoGet() final; - void DoPost() final {}; - void DoPut() final; - void DoDelete() final {}; - private: - }; -} + void DoGet() final; + void DoPost() final{}; + void DoPut() final; + void DoDelete() final{}; + + private: + }; +} // namespace OpenWifi diff --git a/src/RESTAPI/RESTAPI_oauth2_handler.cpp b/src/RESTAPI/RESTAPI_oauth2_handler.cpp index f7e2434..f83b103 100644 --- a/src/RESTAPI/RESTAPI_oauth2_handler.cpp +++ b/src/RESTAPI/RESTAPI_oauth2_handler.cpp @@ -3,21 +3,24 @@ // #include "RESTAPI_oauth2_handler.h" -#include "framework/MicroServiceNames.h" #include "framework/API_Proxy.h" +#include "framework/MicroServiceNames.h" namespace OpenWifi { - void RESTAPI_oauth2_handler::DoPost() { - API_Proxy(Logger_, Request, Response, OpenWifi::uSERVICE_SECURITY.c_str(),"/api/v1/suboauth2"); - } + void RESTAPI_oauth2_handler::DoPost() { + API_Proxy(Logger_, Request, Response, OpenWifi::uSERVICE_SECURITY.c_str(), + "/api/v1/suboauth2"); + } - void RESTAPI_oauth2_handler::DoGet() { - API_Proxy(Logger_, Request, Response, OpenWifi::uSERVICE_SECURITY.c_str(),"/api/v1/suboauth2"); - } + void RESTAPI_oauth2_handler::DoGet() { + API_Proxy(Logger_, Request, Response, OpenWifi::uSERVICE_SECURITY.c_str(), + "/api/v1/suboauth2"); + } - void RESTAPI_oauth2_handler::DoDelete() { - API_Proxy(Logger_, Request, Response, OpenWifi::uSERVICE_SECURITY.c_str(),"/api/v1/suboauth2"); - } + void RESTAPI_oauth2_handler::DoDelete() { + API_Proxy(Logger_, Request, Response, OpenWifi::uSERVICE_SECURITY.c_str(), + "/api/v1/suboauth2"); + } -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_oauth2_handler.h b/src/RESTAPI/RESTAPI_oauth2_handler.h index e6feebe..6fe6ece 100644 --- a/src/RESTAPI/RESTAPI_oauth2_handler.h +++ b/src/RESTAPI/RESTAPI_oauth2_handler.h @@ -7,26 +7,27 @@ #include "framework/RESTAPI_Handler.h" namespace OpenWifi { - class RESTAPI_oauth2_handler : public RESTAPIHandler { - public: - RESTAPI_oauth2_handler(const RESTAPIHandler::BindingMap &bindings, - Poco::Logger &L, - RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId, bool Internal) - : RESTAPIHandler(bindings, L, - std::vector{Poco::Net::HTTPRequest::HTTP_POST, - Poco::Net::HTTPRequest::HTTP_DELETE, - Poco::Net::HTTPRequest::HTTP_GET, - Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Server, - TransactionId, - Internal, false, true , RateLimit{.Interval=1000,.MaxCalls=10}) {} - static auto PathName() { return std::list{"/api/v1/oauth2/{token}","/api/v1/oauth2"}; }; + class RESTAPI_oauth2_handler : public RESTAPIHandler { + public: + RESTAPI_oauth2_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, + bool Internal) + : RESTAPIHandler(bindings, L, + std::vector{Poco::Net::HTTPRequest::HTTP_POST, + Poco::Net::HTTPRequest::HTTP_DELETE, + Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_OPTIONS}, + Server, TransactionId, Internal, false, true, + RateLimit{.Interval = 1000, .MaxCalls = 10}) {} + static auto PathName() { + return std::list{"/api/v1/oauth2/{token}", "/api/v1/oauth2"}; + }; - void DoGet() final; - void DoPost() final; - void DoPut() final {}; - void DoDelete() final; - private: - }; -} + void DoGet() final; + void DoPost() final; + void DoPut() final{}; + void DoDelete() final; + private: + }; +} // namespace OpenWifi diff --git a/src/RESTAPI/RESTAPI_routers.cpp b/src/RESTAPI/RESTAPI_routers.cpp index e3dfdf8..c7d4935 100644 --- a/src/RESTAPI/RESTAPI_routers.cpp +++ b/src/RESTAPI/RESTAPI_routers.cpp @@ -2,53 +2,38 @@ // Created by stephane bourque on 2021-10-23. // -#include "RESTAPI/RESTAPI_wifiClients_handler.h" #include "RESTAPI//RESTAPI_wiredClients_handler.h" -#include "RESTAPI/RESTAPI_subscriber_handler.h" -#include "RESTAPI/RESTAPI_oauth2_handler.h" #include "RESTAPI/RESTAPI_action_handler.h" -#include "RESTAPI/RESTAPI_mfa_handler.h" #include "RESTAPI/RESTAPI_claim_handler.h" +#include "RESTAPI/RESTAPI_mfa_handler.h" +#include "RESTAPI/RESTAPI_oauth2_handler.h" #include "RESTAPI/RESTAPI_signup_handler.h" #include "RESTAPI/RESTAPI_stats_handler.h" +#include "RESTAPI/RESTAPI_subscriber_handler.h" +#include "RESTAPI/RESTAPI_wifiClients_handler.h" #include "framework/RESTAPI_SystemCommand.h" #include "framework/RESTAPI_WebSocketServer.h" namespace OpenWifi { - Poco::Net::HTTPRequestHandler * RESTAPI_ExtRouter(const std::string &Path, RESTAPIHandler::BindingMap &Bindings, - Poco::Logger & L, RESTAPI_GenericServerAccounting & S, - uint64_t TransactionId) { - return RESTAPI_Router< - RESTAPI_wifiClients_handler, - RESTAPI_wiredClients_handler, - RESTAPI_subscriber_handler, - RESTAPI_oauth2_handler, - RESTAPI_action_handler, - RESTAPI_mfa_handler, - RESTAPI_claim_handler, - RESTAPI_signup_handler, - RESTAPI_system_command, - RESTAPI_stats_handler, - RESTAPI_webSocketServer - >(Path, Bindings, L, S, TransactionId); - } + Poco::Net::HTTPRequestHandler * + RESTAPI_ExtRouter(const std::string &Path, RESTAPIHandler::BindingMap &Bindings, + Poco::Logger &L, RESTAPI_GenericServerAccounting &S, uint64_t TransactionId) { + return RESTAPI_Router(Path, Bindings, L, S, TransactionId); + } - Poco::Net::HTTPRequestHandler * RESTAPI_IntRouter(const std::string &Path, RESTAPIHandler::BindingMap &Bindings, - Poco::Logger & L, RESTAPI_GenericServerAccounting & S, - uint64_t TransactionId) { - return RESTAPI_Router_I< - RESTAPI_wifiClients_handler, - RESTAPI_wiredClients_handler, - RESTAPI_subscriber_handler, - RESTAPI_oauth2_handler, - RESTAPI_action_handler, - RESTAPI_mfa_handler, - RESTAPI_claim_handler, - RESTAPI_signup_handler, - RESTAPI_system_command, - RESTAPI_stats_handler - >(Path, Bindings, L, S, TransactionId); - } + Poco::Net::HTTPRequestHandler * + RESTAPI_IntRouter(const std::string &Path, RESTAPIHandler::BindingMap &Bindings, + Poco::Logger &L, RESTAPI_GenericServerAccounting &S, uint64_t TransactionId) { + return RESTAPI_Router_I(Path, Bindings, L, S, TransactionId); + } -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_signup_handler.cpp b/src/RESTAPI/RESTAPI_signup_handler.cpp index d21977e..f5221a8 100644 --- a/src/RESTAPI/RESTAPI_signup_handler.cpp +++ b/src/RESTAPI/RESTAPI_signup_handler.cpp @@ -8,45 +8,49 @@ namespace OpenWifi { - void RESTAPI_signup_handler::DoPost() { - // do dome basic checking before we send this over. - auto UserName = GetParameter("email"); - Poco::toLowerInPlace(UserName); - Poco::trimInPlace(UserName); + void RESTAPI_signup_handler::DoPost() { + // do dome basic checking before we send this over. + auto UserName = GetParameter("email"); + Poco::toLowerInPlace(UserName); + Poco::trimInPlace(UserName); - auto SerialNumber = GetParameter("macAddress"); - Poco::toLowerInPlace(SerialNumber); - Poco::trimInPlace(SerialNumber); + auto SerialNumber = GetParameter("macAddress"); + Poco::toLowerInPlace(SerialNumber); + Poco::trimInPlace(SerialNumber); - auto registrationId = GetParameter("registrationId"); - Poco::toLowerInPlace(registrationId); - Poco::trimInPlace(registrationId); + auto registrationId = GetParameter("registrationId"); + Poco::toLowerInPlace(registrationId); + Poco::trimInPlace(registrationId); - if(!Utils::ValidSerialNumber(SerialNumber)) { - return BadRequest(RESTAPI::Errors::InvalidSerialNumber); - } + if (!Utils::ValidSerialNumber(SerialNumber)) { + return BadRequest(RESTAPI::Errors::InvalidSerialNumber); + } - if(!Utils::ValidEMailAddress(UserName)) { - return BadRequest(RESTAPI::Errors::InvalidEmailAddress); - } + if (!Utils::ValidEMailAddress(UserName)) { + return BadRequest(RESTAPI::Errors::InvalidEmailAddress); + } - if(registrationId.empty()) { - return BadRequest(RESTAPI::Errors::InvalidRegistrationOperatorName); - } + if (registrationId.empty()) { + return BadRequest(RESTAPI::Errors::InvalidRegistrationOperatorName); + } - return API_Proxy(Logger(), Request, Response, uSERVICE_PROVISIONING.c_str(), "/api/v1/signup", 60000); - } + return API_Proxy(Logger(), Request, Response, uSERVICE_PROVISIONING.c_str(), + "/api/v1/signup", 60000); + } - void RESTAPI_signup_handler::DoPut() { - return API_Proxy(Logger(), Request, Response, uSERVICE_PROVISIONING.c_str(), "/api/v1/signup", 60000); - } + void RESTAPI_signup_handler::DoPut() { + return API_Proxy(Logger(), Request, Response, uSERVICE_PROVISIONING.c_str(), + "/api/v1/signup", 60000); + } - void RESTAPI_signup_handler::DoGet() { - return API_Proxy(Logger(), Request, Response, uSERVICE_PROVISIONING.c_str(), "/api/v1/signup", 60000); - } + void RESTAPI_signup_handler::DoGet() { + return API_Proxy(Logger(), Request, Response, uSERVICE_PROVISIONING.c_str(), + "/api/v1/signup", 60000); + } - void RESTAPI_signup_handler::DoDelete() { - return API_Proxy(Logger(), Request, Response, uSERVICE_PROVISIONING.c_str(), "/api/v1/signup", 60000); - } + void RESTAPI_signup_handler::DoDelete() { + return API_Proxy(Logger(), Request, Response, uSERVICE_PROVISIONING.c_str(), + "/api/v1/signup", 60000); + } -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_signup_handler.h b/src/RESTAPI/RESTAPI_signup_handler.h index 779a174..f8f466f 100644 --- a/src/RESTAPI/RESTAPI_signup_handler.h +++ b/src/RESTAPI/RESTAPI_signup_handler.h @@ -7,38 +7,34 @@ #include "framework/RESTAPI_Handler.h" namespace OpenWifi { - class RESTAPI_signup_handler : public RESTAPIHandler { - public: - RESTAPI_signup_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId, bool Internal) - : RESTAPIHandler(bindings, L, - std::vector{ - Poco::Net::HTTPRequest::HTTP_POST, - Poco::Net::HTTPRequest::HTTP_OPTIONS, - Poco::Net::HTTPRequest::HTTP_PUT, - Poco::Net::HTTPRequest::HTTP_GET, - Poco::Net::HTTPRequest::HTTP_DELETE - }, - Server, - TransactionId, - Internal, false, true ) { + class RESTAPI_signup_handler : public RESTAPIHandler { + public: + RESTAPI_signup_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, + bool Internal) + : RESTAPIHandler(bindings, L, + std::vector{Poco::Net::HTTPRequest::HTTP_POST, + Poco::Net::HTTPRequest::HTTP_OPTIONS, + Poco::Net::HTTPRequest::HTTP_PUT, + Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_DELETE}, + Server, TransactionId, Internal, false, true) {} - } + static auto PathName() { return std::list{"/api/v1/signup"}; }; - static auto PathName() { return std::list{"/api/v1/signup"}; }; + /* inline bool RoleIsAuthorized(std::string & Reason) { + if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } + */ + void DoGet() final; + void DoPost() final; + void DoPut() final; + void DoDelete() final; -/* inline bool RoleIsAuthorized(std::string & Reason) { - if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { - Reason = "User must be a subscriber"; - return false; - } - return true; - } -*/ - void DoGet() final; - void DoPost() final; - void DoPut() final ; - void DoDelete() final; - private: - - }; -} + private: + }; +} // namespace OpenWifi diff --git a/src/RESTAPI/RESTAPI_stats_handler.cpp b/src/RESTAPI/RESTAPI_stats_handler.cpp index fd2576a..64864bc 100644 --- a/src/RESTAPI/RESTAPI_stats_handler.cpp +++ b/src/RESTAPI/RESTAPI_stats_handler.cpp @@ -4,31 +4,31 @@ #include "RESTAPI_stats_handler.h" #include "RESTObjects/RESTAPI_SubObjects.h" -#include "StorageService.h" #include "StatsSvr.h" +#include "StorageService.h" namespace OpenWifi { - void RESTAPI_stats_handler::DoGet() { - auto MAC = GetBinding("mac",""); + void RESTAPI_stats_handler::DoGet() { + auto MAC = GetBinding("mac", ""); - if(MAC.empty() || !Utils::ValidSerialNumber(MAC)) { - return BadRequest(RESTAPI::Errors::InvalidSerialNumber); - } + if (MAC.empty() || !Utils::ValidSerialNumber(MAC)) { + return BadRequest(RESTAPI::Errors::InvalidSerialNumber); + } - SubObjects::SubscriberInfo SI; - if(!StorageService()->SubInfoDB().GetRecord("id",UserInfo_.userinfo.id,SI)) { - return NotFound(); - } + SubObjects::SubscriberInfo SI; + if (!StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id, SI)) { + return NotFound(); + } - SubObjects::StatsBlock SB; - for(const auto &device:SI.accessPoints.list) { - if(device.macAddress==MAC) { - StatsSvr()->Get(device.serialNumber,SB); - break; - } - } - Poco::JSON::Object Answer; - SB.to_json(Answer); - return ReturnObject(Answer); - } -} + SubObjects::StatsBlock SB; + for (const auto &device : SI.accessPoints.list) { + if (device.macAddress == MAC) { + StatsSvr()->Get(device.serialNumber, SB); + break; + } + } + Poco::JSON::Object Answer; + SB.to_json(Answer); + return ReturnObject(Answer); + } +} // namespace OpenWifi diff --git a/src/RESTAPI/RESTAPI_stats_handler.h b/src/RESTAPI/RESTAPI_stats_handler.h index 528ef0d..e120fd1 100644 --- a/src/RESTAPI/RESTAPI_stats_handler.h +++ b/src/RESTAPI/RESTAPI_stats_handler.h @@ -11,23 +11,23 @@ #include "framework/RESTAPI_Handler.h" namespace OpenWifi { - class RESTAPI_stats_handler : public RESTAPIHandler { - public: - RESTAPI_stats_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId, bool Internal) - : RESTAPIHandler(bindings, L, - std::vector{ - Poco::Net::HTTPRequest::HTTP_GET, - Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Server, - TransactionId, - Internal, true, false, RateLimit{.Interval=1000,.MaxCalls=10}, true){} + class RESTAPI_stats_handler : public RESTAPIHandler { + public: + RESTAPI_stats_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, + bool Internal) + : RESTAPIHandler(bindings, L, + std::vector{Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_OPTIONS}, + Server, TransactionId, Internal, true, false, + RateLimit{.Interval = 1000, .MaxCalls = 10}, true) {} - static auto PathName() { return std::list{"/api/v1/stats/{mac}"}; }; + static auto PathName() { return std::list{"/api/v1/stats/{mac}"}; }; - private: - void DoGet() final; - void DoPost() final {}; - void DoPut() final {}; - void DoDelete() final {}; - }; -} + private: + void DoGet() final; + void DoPost() final{}; + void DoPut() final{}; + void DoDelete() final{}; + }; +} // namespace OpenWifi diff --git a/src/RESTAPI/RESTAPI_subscriber_handler.cpp b/src/RESTAPI/RESTAPI_subscriber_handler.cpp index 17066c2..5893093 100644 --- a/src/RESTAPI/RESTAPI_subscriber_handler.cpp +++ b/src/RESTAPI/RESTAPI_subscriber_handler.cpp @@ -3,331 +3,348 @@ // #include "RESTAPI_subscriber_handler.h" -#include "StorageService.h" -#include "RESTObjects/RESTAPI_SubObjects.h" -#include "SubscriberCache.h" -#include "sdks/SDK_prov.h" -#include "sdks/SDK_gw.h" -#include "sdks/SDK_fms.h" -#include "sdks/SDK_sec.h" #include "ConfigMaker.h" -#include "nlohmann/json.hpp" +#include "RESTObjects/RESTAPI_SubObjects.h" +#include "StorageService.h" +#include "SubscriberCache.h" #include "framework/utils.h" +#include "nlohmann/json.hpp" +#include "sdks/SDK_fms.h" +#include "sdks/SDK_gw.h" +#include "sdks/SDK_prov.h" +#include "sdks/SDK_sec.h" namespace OpenWifi { - template void AssignIfModified(T & Var, const T & Value, int &Mods) { - if(Var!=Value) { - Var = Value; - Mods++; - } - } + template void AssignIfModified(T &Var, const T &Value, int &Mods) { + if (Var != Value) { + Var = Value; + Mods++; + } + } - void RESTAPI_subscriber_handler::DoGet() { + void RESTAPI_subscriber_handler::DoGet() { - if(UserInfo_.userinfo.id.empty()) { - return NotFound(); - } + if (UserInfo_.userinfo.id.empty()) { + return NotFound(); + } - Logger().information(fmt::format("{}: Get basic info.", UserInfo_.userinfo.email)); - SubObjects::SubscriberInfo SI; - if(StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id,SI)) { + Logger().information(fmt::format("{}: Get basic info.", UserInfo_.userinfo.email)); + SubObjects::SubscriberInfo SI; + if (StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id, SI)) { - int Mods=0; + int Mods = 0; - // we need to get the stats for each AP - for(auto &i:SI.accessPoints.list) { - if(i.macAddress.empty()) - continue; - Poco::JSON::Object::Ptr LastStats; - if(SDK::GW::Device::GetLastStats(nullptr,i.serialNumber,LastStats)) { - std::ostringstream OS; - LastStats->stringify(OS); - try { - nlohmann::json LA = nlohmann::json::parse(OS.str()); - for (const auto &j: LA["interfaces"]) { - if (j.contains("location") && j["location"].get()=="/interfaces/0" && j.contains("ipv4")) { + // we need to get the stats for each AP + for (auto &i : SI.accessPoints.list) { + if (i.macAddress.empty()) + continue; + Poco::JSON::Object::Ptr LastStats; + if (SDK::GW::Device::GetLastStats(nullptr, i.serialNumber, LastStats)) { + std::ostringstream OS; + LastStats->stringify(OS); + try { + nlohmann::json LA = nlohmann::json::parse(OS.str()); + for (const auto &j : LA["interfaces"]) { + if (j.contains("location") && + j["location"].get() == "/interfaces/0" && + j.contains("ipv4")) { - if( j["ipv4"]["addresses"].is_array() - && !j["ipv4"]["addresses"].empty() ) { - auto IPParts = Poco::StringTokenizer(j["ipv4"]["addresses"][0].get(), "/"); - AssignIfModified(i.internetConnection.ipAddress,IPParts[0],Mods); - i.internetConnection.subnetMask = IPParts[1]; - } - if (j["ipv4"].contains("dhcp_server")) - AssignIfModified(i.internetConnection.defaultGateway,j["ipv4"]["dhcp_server"].get(),Mods); - else - AssignIfModified(i.internetConnection.defaultGateway,std::string{"---"},Mods); + if (j["ipv4"]["addresses"].is_array() && + !j["ipv4"]["addresses"].empty()) { + auto IPParts = Poco::StringTokenizer( + j["ipv4"]["addresses"][0].get(), "/"); + AssignIfModified(i.internetConnection.ipAddress, IPParts[0], + Mods); + i.internetConnection.subnetMask = IPParts[1]; + } + if (j["ipv4"].contains("dhcp_server")) + AssignIfModified(i.internetConnection.defaultGateway, + j["ipv4"]["dhcp_server"].get(), + Mods); + else + AssignIfModified(i.internetConnection.defaultGateway, + std::string{"---"}, Mods); - if (j.contains("dns_servers") && j["dns_servers"].is_array()) { - auto dns = j["dns_servers"]; - if (!dns.empty()) - AssignIfModified(i.internetConnection.primaryDns,dns[0].get(),Mods); - else - AssignIfModified(i.internetConnection.primaryDns,std::string{"---"},Mods); - if (dns.size() > 1) - AssignIfModified(i.internetConnection.secondaryDns, dns[1].get(),Mods); - else - AssignIfModified(i.internetConnection.secondaryDns, std::string{"---"},Mods); - } - } - } - } catch(...) { - AssignIfModified(i.internetConnection.ipAddress, std::string{"--"}, Mods); - i.internetConnection.subnetMask = "--"; - i.internetConnection.defaultGateway = "--"; - i.internetConnection.primaryDns = "--"; - i.internetConnection.secondaryDns = "--"; - } - } else { - AssignIfModified(i.internetConnection.ipAddress, std::string{"-"}, Mods); - i.internetConnection.subnetMask = "-"; - i.internetConnection.defaultGateway = "-"; - i.internetConnection.primaryDns = "-"; - i.internetConnection.secondaryDns = "-"; - } + if (j.contains("dns_servers") && j["dns_servers"].is_array()) { + auto dns = j["dns_servers"]; + if (!dns.empty()) + AssignIfModified(i.internetConnection.primaryDns, + dns[0].get(), Mods); + else + AssignIfModified(i.internetConnection.primaryDns, + std::string{"---"}, Mods); + if (dns.size() > 1) + AssignIfModified(i.internetConnection.secondaryDns, + dns[1].get(), Mods); + else + AssignIfModified(i.internetConnection.secondaryDns, + std::string{"---"}, Mods); + } + } + } + } catch (...) { + AssignIfModified(i.internetConnection.ipAddress, std::string{"--"}, Mods); + i.internetConnection.subnetMask = "--"; + i.internetConnection.defaultGateway = "--"; + i.internetConnection.primaryDns = "--"; + i.internetConnection.secondaryDns = "--"; + } + } else { + AssignIfModified(i.internetConnection.ipAddress, std::string{"-"}, Mods); + i.internetConnection.subnetMask = "-"; + i.internetConnection.defaultGateway = "-"; + i.internetConnection.primaryDns = "-"; + i.internetConnection.secondaryDns = "-"; + } - FMSObjects::DeviceInformation DI; - if(SDK::FMS::Firmware::GetDeviceInformation(nullptr,i.serialNumber,DI)) { - AssignIfModified(i.currentFirmwareDate, DI.currentFirmwareDate, Mods); - AssignIfModified(i.currentFirmware, DI.currentFirmware, Mods); - AssignIfModified(i.latestFirmwareDate, DI.latestFirmwareDate, Mods); - AssignIfModified(i.latestFirmware, DI.latestFirmware, Mods); - AssignIfModified(i.newFirmwareAvailable, DI.latestFirmwareAvailable, Mods); - AssignIfModified(i.latestFirmwareURI, DI.latestFirmwareURI, Mods); - } - } + FMSObjects::DeviceInformation DI; + if (SDK::FMS::Firmware::GetDeviceInformation(nullptr, i.serialNumber, DI)) { + AssignIfModified(i.currentFirmwareDate, DI.currentFirmwareDate, Mods); + AssignIfModified(i.currentFirmware, DI.currentFirmware, Mods); + AssignIfModified(i.latestFirmwareDate, DI.latestFirmwareDate, Mods); + AssignIfModified(i.latestFirmware, DI.latestFirmware, Mods); + AssignIfModified(i.newFirmwareAvailable, DI.latestFirmwareAvailable, Mods); + AssignIfModified(i.latestFirmwareURI, DI.latestFirmwareURI, Mods); + } + } - if(Mods) { - auto now = Utils::Now(); - if(SI.modified==now) - SI.modified++; - else - SI.modified=now; - StorageService()->SubInfoDB().UpdateRecord("id", UserInfo_.userinfo.id,SI); - } + if (Mods) { + auto now = Utils::Now(); + if (SI.modified == now) + SI.modified++; + else + SI.modified = now; + StorageService()->SubInfoDB().UpdateRecord("id", UserInfo_.userinfo.id, SI); + } - Poco::JSON::Object Answer; - SI.to_json(Answer); - return ReturnObject(Answer); - } + Poco::JSON::Object Answer; + SI.to_json(Answer); + return ReturnObject(Answer); + } - // if the user does not have a device, we cannot continue. - ProvObjects::SubscriberDeviceList Devices; + // if the user does not have a device, we cannot continue. + ProvObjects::SubscriberDeviceList Devices; - if(!SDK::Prov::Subscriber::GetDevices(this,UserInfo_.userinfo.id,UserInfo_.userinfo.owner,Devices)) { - return BadRequest(RESTAPI::Errors::ProvServiceNotAvailable); - } + if (!SDK::Prov::Subscriber::GetDevices(this, UserInfo_.userinfo.id, + UserInfo_.userinfo.owner, Devices)) { + return BadRequest(RESTAPI::Errors::ProvServiceNotAvailable); + } - if(Devices.subscriberDevices.empty() ) { - return BadRequest(RESTAPI::Errors::SubNoDeviceActivated); - } + if (Devices.subscriberDevices.empty()) { + return BadRequest(RESTAPI::Errors::SubNoDeviceActivated); + } - Logger().information(fmt::format("{}: Creating default user information.", UserInfo_.userinfo.email)); - StorageService()->SubInfoDB().CreateDefaultSubscriberInfo(UserInfo_, SI, Devices); - Logger().information(fmt::format("{}: Creating default configuration.", UserInfo_.userinfo.email)); - StorageService()->SubInfoDB().CreateRecord(SI); + Logger().information( + fmt::format("{}: Creating default user information.", UserInfo_.userinfo.email)); + StorageService()->SubInfoDB().CreateDefaultSubscriberInfo(UserInfo_, SI, Devices); + Logger().information( + fmt::format("{}: Creating default configuration.", UserInfo_.userinfo.email)); + StorageService()->SubInfoDB().CreateRecord(SI); - Logger().information(fmt::format("{}: Generating initial configuration.", UserInfo_.userinfo.email)); - ConfigMaker InitialConfig(Logger(),SI.id); - InitialConfig.Prepare(); - StorageService()->SubInfoDB().GetRecord("id", SI.id, SI); + Logger().information( + fmt::format("{}: Generating initial configuration.", UserInfo_.userinfo.email)); + ConfigMaker InitialConfig(Logger(), SI.id); + InitialConfig.Prepare(); + StorageService()->SubInfoDB().GetRecord("id", SI.id, SI); - Poco::JSON::Object Answer; - SI.to_json(Answer); - ReturnObject(Answer); - } + Poco::JSON::Object Answer; + SI.to_json(Answer); + ReturnObject(Answer); + } - static bool ValidIPv4(const std::string &IP) { - if(IP.empty()) - return false; - Poco::Net::IPAddress A; + static bool ValidIPv4(const std::string &IP) { + if (IP.empty()) + return false; + Poco::Net::IPAddress A; - if(!Poco::Net::IPAddress::tryParse(IP,A) || A.family()!=Poco::Net::AddressFamily::IPv4) - return false; + if (!Poco::Net::IPAddress::tryParse(IP, A) || A.family() != Poco::Net::AddressFamily::IPv4) + return false; - return true; - } + return true; + } - [[maybe_unused]] static bool ValidIPv6(const std::string &IP) { - if(IP.empty()) - return false; - Poco::Net::IPAddress A; + [[maybe_unused]] static bool ValidIPv6(const std::string &IP) { + if (IP.empty()) + return false; + Poco::Net::IPAddress A; - if(!Poco::Net::IPAddress::tryParse(IP,A) || A.family()!=Poco::Net::AddressFamily::IPv6) - return false; + if (!Poco::Net::IPAddress::tryParse(IP, A) || A.family() != Poco::Net::AddressFamily::IPv6) + return false; - return true; - } + return true; + } - [[maybe_unused]] static bool ValidIPv4v6(const std::string &IP) { - if(IP.empty()) - return false; - Poco::Net::IPAddress A; + [[maybe_unused]] static bool ValidIPv4v6(const std::string &IP) { + if (IP.empty()) + return false; + Poco::Net::IPAddress A; - if(!Poco::Net::IPAddress::tryParse(IP,A)) - return false; + if (!Poco::Net::IPAddress::tryParse(IP, A)) + return false; - return true; - } + return true; + } - static bool ValidateIPv4Subnet(const std::string &IP) { - auto IPParts = Poco::StringTokenizer(IP,"/"); - if(IPParts.count()!=2) { - return false; - } - if(!ValidIPv4(IPParts[0])) { - return false; - } - auto X=std::atoll(IPParts[1].c_str()); - if(X<8 || X>24) { - return false; - } - return true; - } + static bool ValidateIPv4Subnet(const std::string &IP) { + auto IPParts = Poco::StringTokenizer(IP, "/"); + if (IPParts.count() != 2) { + return false; + } + if (!ValidIPv4(IPParts[0])) { + return false; + } + auto X = std::atoll(IPParts[1].c_str()); + if (X < 8 || X > 24) { + return false; + } + return true; + } - void RESTAPI_subscriber_handler::DoPut() { + void RESTAPI_subscriber_handler::DoPut() { - auto ConfigChanged = GetParameter("configChanged","true") == "true"; - auto ApplyConfigOnly = GetParameter("applyConfigOnly","true") == "true"; + auto ConfigChanged = GetParameter("configChanged", "true") == "true"; + auto ApplyConfigOnly = GetParameter("applyConfigOnly", "true") == "true"; - if(UserInfo_.userinfo.id.empty()) { - return NotFound(); - } + if (UserInfo_.userinfo.id.empty()) { + return NotFound(); + } - SubObjects::SubscriberInfo Existing; - if(!StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id, Existing)) { - return NotFound(); - } + SubObjects::SubscriberInfo Existing; + if (!StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id, Existing)) { + return NotFound(); + } - if(ApplyConfigOnly) { - ConfigMaker InitialConfig(Logger(),UserInfo_.userinfo.id); - if(InitialConfig.Prepare()) - return OK(); - else - return InternalError(RESTAPI::Errors::SubConfigNotRefreshed); - } + if (ApplyConfigOnly) { + ConfigMaker InitialConfig(Logger(), UserInfo_.userinfo.id); + if (InitialConfig.Prepare()) + return OK(); + else + return InternalError(RESTAPI::Errors::SubConfigNotRefreshed); + } - const auto & Body = ParsedBody_; - SubObjects::SubscriberInfo Changes; - if(!Changes.from_json(Body)) { - return BadRequest(RESTAPI::Errors::InvalidJSONDocument); - } + const auto &Body = ParsedBody_; + SubObjects::SubscriberInfo Changes; + if (!Changes.from_json(Body)) { + return BadRequest(RESTAPI::Errors::InvalidJSONDocument); + } - auto Now = Utils::Now(); - if(Body->has("firstName")) - Existing.firstName = Changes.firstName; - if(Body->has("initials")) - Existing.initials = Changes.initials; - if(Body->has("lastName")) - Existing.lastName = Changes.lastName; - if(Body->has("secondaryEmail") && Utils::ValidEMailAddress(Changes.secondaryEmail)) - Existing.secondaryEmail = Changes.secondaryEmail; - if(Body->has("serviceAddress")) - Existing.serviceAddress = Changes.serviceAddress; - if(Body->has("billingAddress")) - Existing.billingAddress = Changes.billingAddress; - if(Body->has("phoneNumber")) - Existing.phoneNumber = Changes.phoneNumber; - Existing.modified = Now; + auto Now = Utils::Now(); + if (Body->has("firstName")) + Existing.firstName = Changes.firstName; + if (Body->has("initials")) + Existing.initials = Changes.initials; + if (Body->has("lastName")) + Existing.lastName = Changes.lastName; + if (Body->has("secondaryEmail") && Utils::ValidEMailAddress(Changes.secondaryEmail)) + Existing.secondaryEmail = Changes.secondaryEmail; + if (Body->has("serviceAddress")) + Existing.serviceAddress = Changes.serviceAddress; + if (Body->has("billingAddress")) + Existing.billingAddress = Changes.billingAddress; + if (Body->has("phoneNumber")) + Existing.phoneNumber = Changes.phoneNumber; + Existing.modified = Now; - // Look at the access points - for (auto &NewAP: Changes.accessPoints.list) { - for (auto &ExistingAP: Existing.accessPoints.list) { - if (NewAP.macAddress == ExistingAP.macAddress) { - for(const auto &ssid:NewAP.wifiNetworks.wifiNetworks) { - if( ssid.password.length()<8 || - ssid.password.length()>32 ) { - return BadRequest(RESTAPI::Errors::SSIDInvalidPassword); - } - } - if(NewAP.deviceMode.type=="nat") { - if(!ValidIPv4(NewAP.deviceMode.startIP) || !ValidIPv4(NewAP.deviceMode.endIP)) { - return BadRequest(RESTAPI::Errors::InvalidStartingIPAddress); - } - if(!ValidateIPv4Subnet(NewAP.deviceMode.subnet)) { - return BadRequest(RESTAPI::Errors::SubnetFormatError); - } - } else if(NewAP.deviceMode.type=="bridge") { + // Look at the access points + for (auto &NewAP : Changes.accessPoints.list) { + for (auto &ExistingAP : Existing.accessPoints.list) { + if (NewAP.macAddress == ExistingAP.macAddress) { + for (const auto &ssid : NewAP.wifiNetworks.wifiNetworks) { + if (ssid.password.length() < 8 || ssid.password.length() > 32) { + return BadRequest(RESTAPI::Errors::SSIDInvalidPassword); + } + } + if (NewAP.deviceMode.type == "nat") { + if (!ValidIPv4(NewAP.deviceMode.startIP) || + !ValidIPv4(NewAP.deviceMode.endIP)) { + return BadRequest(RESTAPI::Errors::InvalidStartingIPAddress); + } + if (!ValidateIPv4Subnet(NewAP.deviceMode.subnet)) { + return BadRequest(RESTAPI::Errors::SubnetFormatError); + } + } else if (NewAP.deviceMode.type == "bridge") { - } else if(NewAP.deviceMode.type=="manual") { - if(!ValidateIPv4Subnet(NewAP.deviceMode.subnet)) { - return BadRequest(RESTAPI::Errors::DeviceModeError); - } - if(!ValidIPv4(NewAP.deviceMode.startIP)) { - return BadRequest(RESTAPI::Errors::DeviceModeError); - } - if(!ValidIPv4(NewAP.deviceMode.endIP)) { - return BadRequest(RESTAPI::Errors::DeviceModeError); - } - } else { - return BadRequest(RESTAPI::Errors::BadDeviceMode); - } + } else if (NewAP.deviceMode.type == "manual") { + if (!ValidateIPv4Subnet(NewAP.deviceMode.subnet)) { + return BadRequest(RESTAPI::Errors::DeviceModeError); + } + if (!ValidIPv4(NewAP.deviceMode.startIP)) { + return BadRequest(RESTAPI::Errors::DeviceModeError); + } + if (!ValidIPv4(NewAP.deviceMode.endIP)) { + return BadRequest(RESTAPI::Errors::DeviceModeError); + } + } else { + return BadRequest(RESTAPI::Errors::BadDeviceMode); + } - if(NewAP.internetConnection.type=="manual") { - if(!ValidateIPv4Subnet(NewAP.internetConnection.subnetMask)) { - return BadRequest(RESTAPI::Errors::SubnetFormatError); - } - if(!ValidIPv4(NewAP.internetConnection.defaultGateway)) { - return BadRequest(RESTAPI::Errors::DefaultGatewayFormat); - } - if(!ValidIPv4(NewAP.internetConnection.primaryDns)) { - return BadRequest(RESTAPI::Errors::PrimaryDNSFormat); - } - if(!NewAP.internetConnection.secondaryDns.empty() && !ValidIPv4(NewAP.internetConnection.secondaryDns)) { - return BadRequest(RESTAPI::Errors::SecondaryDNSFormat); - } - } else if(NewAP.internetConnection.type=="pppoe") { + if (NewAP.internetConnection.type == "manual") { + if (!ValidateIPv4Subnet(NewAP.internetConnection.subnetMask)) { + return BadRequest(RESTAPI::Errors::SubnetFormatError); + } + if (!ValidIPv4(NewAP.internetConnection.defaultGateway)) { + return BadRequest(RESTAPI::Errors::DefaultGatewayFormat); + } + if (!ValidIPv4(NewAP.internetConnection.primaryDns)) { + return BadRequest(RESTAPI::Errors::PrimaryDNSFormat); + } + if (!NewAP.internetConnection.secondaryDns.empty() && + !ValidIPv4(NewAP.internetConnection.secondaryDns)) { + return BadRequest(RESTAPI::Errors::SecondaryDNSFormat); + } + } else if (NewAP.internetConnection.type == "pppoe") { - } else if(NewAP.internetConnection.type=="automatic") { + } else if (NewAP.internetConnection.type == "automatic") { - } else { - return BadRequest(RESTAPI::Errors::BadConnectionType); - } + } else { + return BadRequest(RESTAPI::Errors::BadConnectionType); + } - ExistingAP = NewAP; - ExistingAP.internetConnection.modified = Now; - ExistingAP.deviceMode.modified = Now; - ExistingAP.wifiNetworks.modified = Now; - ExistingAP.subscriberDevices.modified = Now; - } - } - Changes.modified = Utils::Now(); - } + ExistingAP = NewAP; + ExistingAP.internetConnection.modified = Now; + ExistingAP.deviceMode.modified = Now; + ExistingAP.wifiNetworks.modified = Now; + ExistingAP.subscriberDevices.modified = Now; + } + } + Changes.modified = Utils::Now(); + } - if(StorageService()->SubInfoDB().UpdateRecord("id",UserInfo_.userinfo.id, Existing)) { - if(ConfigChanged) { - ConfigMaker InitialConfig(Logger(),UserInfo_.userinfo.id); - InitialConfig.Prepare(); - } - SubObjects::SubscriberInfo Modified; - StorageService()->SubInfoDB().GetRecord("id",UserInfo_.userinfo.id,Modified); - SubscriberCache()->UpdateSubInfo(UserInfo_.userinfo.id,Modified); - Poco::JSON::Object Answer; - Modified.to_json(Answer); - return ReturnObject(Answer); - } - return InternalError(RESTAPI::Errors::RecordNotUpdated); - } + if (StorageService()->SubInfoDB().UpdateRecord("id", UserInfo_.userinfo.id, Existing)) { + if (ConfigChanged) { + ConfigMaker InitialConfig(Logger(), UserInfo_.userinfo.id); + InitialConfig.Prepare(); + } + SubObjects::SubscriberInfo Modified; + StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id, Modified); + SubscriberCache()->UpdateSubInfo(UserInfo_.userinfo.id, Modified); + Poco::JSON::Object Answer; + Modified.to_json(Answer); + return ReturnObject(Answer); + } + return InternalError(RESTAPI::Errors::RecordNotUpdated); + } - void RESTAPI_subscriber_handler::DoDelete() { - auto id = GetParameter("id",""); - if(!id.empty()) { - StorageService()->SubInfoDB().DeleteRecord("id",id); - return OK(); - } + void RESTAPI_subscriber_handler::DoDelete() { + auto id = GetParameter("id", ""); + if (!id.empty()) { + StorageService()->SubInfoDB().DeleteRecord("id", id); + return OK(); + } - SubObjects::SubscriberInfo SI; - if(StorageService()->SubInfoDB().GetRecord("id",UserInfo_.userinfo.id,SI)) { - for(const auto &i:SI.accessPoints.list) { - if(!i.serialNumber.empty()) { - SDK::Prov::Subscriber::ReturnDeviceToInventory(nullptr, UserInfo_.userinfo.id, i.serialNumber); - } - } - SDK::Sec::Subscriber::Delete(nullptr, UserInfo_.userinfo.id); - StorageService()->SubInfoDB().DeleteRecord("id", UserInfo_.userinfo.id); - return OK(); - } - return NotFound(); - } -} \ No newline at end of file + SubObjects::SubscriberInfo SI; + if (StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id, SI)) { + for (const auto &i : SI.accessPoints.list) { + if (!i.serialNumber.empty()) { + SDK::Prov::Subscriber::ReturnDeviceToInventory(nullptr, UserInfo_.userinfo.id, + i.serialNumber); + } + } + SDK::Sec::Subscriber::Delete(nullptr, UserInfo_.userinfo.id); + StorageService()->SubInfoDB().DeleteRecord("id", UserInfo_.userinfo.id); + return OK(); + } + return NotFound(); + } +} // namespace OpenWifi \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_subscriber_handler.h b/src/RESTAPI/RESTAPI_subscriber_handler.h index 3644b25..787c0fe 100644 --- a/src/RESTAPI/RESTAPI_subscriber_handler.h +++ b/src/RESTAPI/RESTAPI_subscriber_handler.h @@ -7,26 +7,26 @@ #include "framework/RESTAPI_Handler.h" namespace OpenWifi { - class RESTAPI_subscriber_handler : public RESTAPIHandler { - public: - RESTAPI_subscriber_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting & Server, - uint64_t TransactionId, bool Internal) - : RESTAPIHandler(bindings, L, - std::vector{ - Poco::Net::HTTPRequest::HTTP_GET, - Poco::Net::HTTPRequest::HTTP_PUT, - Poco::Net::HTTPRequest::HTTP_DELETE, - Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Server, - TransactionId, - Internal, true, false, RateLimit{.Interval=1000,.MaxCalls=10}, true){} + class RESTAPI_subscriber_handler : public RESTAPIHandler { + public: + RESTAPI_subscriber_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, + bool Internal) + : RESTAPIHandler(bindings, L, + std::vector{Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_PUT, + Poco::Net::HTTPRequest::HTTP_DELETE, + Poco::Net::HTTPRequest::HTTP_OPTIONS}, + Server, TransactionId, Internal, true, false, + RateLimit{.Interval = 1000, .MaxCalls = 10}, true) {} - static auto PathName() { return std::list{"/api/v1/subscriber"}; }; + static auto PathName() { return std::list{"/api/v1/subscriber"}; }; - void DoGet() final; - void DoPost() final {}; - void DoPut() final; - void DoDelete() final; - private: - }; -} + void DoGet() final; + void DoPost() final{}; + void DoPut() final; + void DoDelete() final; + + private: + }; +} // namespace OpenWifi diff --git a/src/RESTAPI/RESTAPI_wifiClients_handler.cpp b/src/RESTAPI/RESTAPI_wifiClients_handler.cpp index 368490d..ce4ef32 100644 --- a/src/RESTAPI/RESTAPI_wifiClients_handler.cpp +++ b/src/RESTAPI/RESTAPI_wifiClients_handler.cpp @@ -5,115 +5,126 @@ #include "RESTAPI_wifiClients_handler.h" #include "RESTObjects/RESTAPI_SubObjects.h" #include "StorageService.h" -#include "sdks/SDK_gw.h" -#include "nlohmann/json.hpp" #include "framework/utils.h" +#include "nlohmann/json.hpp" +#include "sdks/SDK_gw.h" // #define __DBG__ std::cout << __LINE__ << std::endl ; #define __DBG__ namespace OpenWifi { - static void AddManufacturers(SubObjects::AssociationList &List) { - std::vector> MacList; - for(const auto &i:List.associations) { - MacList.push_back(std::make_pair(i.macAddress,"")); - } + static void AddManufacturers(SubObjects::AssociationList &List) { + std::vector> MacList; + for (const auto &i : List.associations) { + MacList.push_back(std::make_pair(i.macAddress, "")); + } - if(SDK::GW::Device::GetOUIs(nullptr,MacList)) { - for(const auto &i:MacList) - for(auto &j:List.associations) - if(j.macAddress==i.first) { - // std::cout << i.first << " :: " << i.second << std::endl; - j.manufacturer = i.second; - } - } - } + if (SDK::GW::Device::GetOUIs(nullptr, MacList)) { + for (const auto &i : MacList) + for (auto &j : List.associations) + if (j.macAddress == i.first) { + // std::cout << i.first << " :: " << i.second << std::endl; + j.manufacturer = i.second; + } + } + } - void RESTAPI_wifiClients_handler::DoGet() { - auto SerialNumber = GetParameter("serialNumber",""); - if(SerialNumber.empty()) { - return BadRequest(RESTAPI::Errors::MissingSerialNumber); - } + void RESTAPI_wifiClients_handler::DoGet() { + auto SerialNumber = GetParameter("serialNumber", ""); + if (SerialNumber.empty()) { + return BadRequest(RESTAPI::Errors::MissingSerialNumber); + } - Logger().information(fmt::format("{}: Getting list of wireless clients.",SerialNumber)); + Logger().information(fmt::format("{}: Getting list of wireless clients.", SerialNumber)); - SubObjects::SubscriberInfo SI; - if(!StorageService()->SubInfoDB().GetRecord("id",UserInfo_.userinfo.id,SI)) { - return NotFound(); - } + SubObjects::SubscriberInfo SI; + if (!StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id, SI)) { + return NotFound(); + } - for(const auto &i:SI.accessPoints.list) { - if(i.macAddress.empty()) - continue; - if(SerialNumber==i.macAddress) { - Poco::JSON::Object::Ptr LastStats; - Poco::JSON::Object Answer; - if(SDK::GW::Device::GetLastStats(nullptr,i.serialNumber,LastStats)){ - uint64_t Now = Utils::Now(); - SubObjects::AssociationList AssocList; - AssocList.modified = AssocList.created = Now; - std::stringstream SS; - LastStats->stringify(SS); - try { - auto stats = nlohmann::json::parse(SS.str()); - if(stats.contains("interfaces") && stats["interfaces"].is_array()) { - auto ifs = stats["interfaces"]; - for (const auto &cur_interface: ifs) { - // create a map of MAC -> IP for clients - std::map> IPs; - if (cur_interface.contains("clients") && cur_interface["clients"].is_array()) { - auto clients = cur_interface["clients"]; - for(const auto &cur_client:clients) { - if(cur_client.contains("mac")) { - std::string ipv4,ipv6; - if( cur_client.contains("ipv6_addresses") && - cur_client["ipv6_addresses"].is_array() && - !cur_client["ipv6_addresses"].empty()) { - ipv6 = cur_client["ipv6_addresses"][0].get(); - } - if( cur_client.contains("ipv4_addresses") && - cur_client["ipv4_addresses"].is_array() && - !cur_client["ipv4_addresses"].empty()) { - ipv4 = cur_client["ipv4_addresses"][0].get(); - } - IPs[cur_client["mac"].get()] = std::make_pair(ipv4,ipv6); - } - } - } + for (const auto &i : SI.accessPoints.list) { + if (i.macAddress.empty()) + continue; + if (SerialNumber == i.macAddress) { + Poco::JSON::Object::Ptr LastStats; + Poco::JSON::Object Answer; + if (SDK::GW::Device::GetLastStats(nullptr, i.serialNumber, LastStats)) { + uint64_t Now = Utils::Now(); + SubObjects::AssociationList AssocList; + AssocList.modified = AssocList.created = Now; + std::stringstream SS; + LastStats->stringify(SS); + try { + auto stats = nlohmann::json::parse(SS.str()); + if (stats.contains("interfaces") && stats["interfaces"].is_array()) { + auto ifs = stats["interfaces"]; + for (const auto &cur_interface : ifs) { + // create a map of MAC -> IP for clients + std::map> IPs; + if (cur_interface.contains("clients") && + cur_interface["clients"].is_array()) { + auto clients = cur_interface["clients"]; + for (const auto &cur_client : clients) { + if (cur_client.contains("mac")) { + std::string ipv4, ipv6; + if (cur_client.contains("ipv6_addresses") && + cur_client["ipv6_addresses"].is_array() && + !cur_client["ipv6_addresses"].empty()) { + ipv6 = cur_client["ipv6_addresses"][0] + .get(); + } + if (cur_client.contains("ipv4_addresses") && + cur_client["ipv4_addresses"].is_array() && + !cur_client["ipv4_addresses"].empty()) { + ipv4 = cur_client["ipv4_addresses"][0] + .get(); + } + IPs[cur_client["mac"].get()] = + std::make_pair(ipv4, ipv6); + } + } + } - if (cur_interface.contains("ssids") && cur_interface["ssids"].is_array() && !cur_interface["ssids"].empty()) { - for (const auto &cur_ssid: cur_interface["ssids"]) { - if (cur_ssid.contains("associations") && cur_ssid["associations"].is_array() && !cur_ssid["associations"].empty()) { - for (const auto &cur_client: cur_ssid["associations"]) { - SubObjects::Association Assoc; - Assoc.ssid = cur_ssid["ssid"].get(); - Assoc.macAddress = cur_client["station"].get(); - Assoc.rssi = cur_client["rssi"].get(); - Assoc.rx = cur_client["rx_bytes"].get(); - Assoc.tx = cur_client["tx_bytes"].get(); - Assoc.power = 0; - Assoc.name = cur_client["station"].get(); - auto which_ips = IPs.find(Assoc.macAddress); - if(which_ips != IPs.end()) { - Assoc.ipv4 = which_ips->second.first; - Assoc.ipv6 = which_ips->second.second; - } - AssocList.associations.push_back(Assoc); - } - } - } - } - } - } - AddManufacturers(AssocList); - AssocList.to_json(Answer); - } catch (...) { - } - } - return ReturnObject(Answer); - } - } - return NotFound(); - } + if (cur_interface.contains("ssids") && + cur_interface["ssids"].is_array() && + !cur_interface["ssids"].empty()) { + for (const auto &cur_ssid : cur_interface["ssids"]) { + if (cur_ssid.contains("associations") && + cur_ssid["associations"].is_array() && + !cur_ssid["associations"].empty()) { + for (const auto &cur_client : + cur_ssid["associations"]) { + SubObjects::Association Assoc; + Assoc.ssid = cur_ssid["ssid"].get(); + Assoc.macAddress = + cur_client["station"].get(); + Assoc.rssi = cur_client["rssi"].get(); + Assoc.rx = cur_client["rx_bytes"].get(); + Assoc.tx = cur_client["tx_bytes"].get(); + Assoc.power = 0; + Assoc.name = + cur_client["station"].get(); + auto which_ips = IPs.find(Assoc.macAddress); + if (which_ips != IPs.end()) { + Assoc.ipv4 = which_ips->second.first; + Assoc.ipv6 = which_ips->second.second; + } + AssocList.associations.push_back(Assoc); + } + } + } + } + } + } + AddManufacturers(AssocList); + AssocList.to_json(Answer); + } catch (...) { + } + } + return ReturnObject(Answer); + } + } + return NotFound(); + } -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_wifiClients_handler.h b/src/RESTAPI/RESTAPI_wifiClients_handler.h index 7f5fa9c..b433135 100644 --- a/src/RESTAPI/RESTAPI_wifiClients_handler.h +++ b/src/RESTAPI/RESTAPI_wifiClients_handler.h @@ -7,31 +7,32 @@ #include "framework/RESTAPI_Handler.h" namespace OpenWifi { - class RESTAPI_wifiClients_handler : public RESTAPIHandler { - public: - RESTAPI_wifiClients_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId, bool Internal) - : RESTAPIHandler(bindings, L, - std::vector{ - Poco::Net::HTTPRequest::HTTP_GET, - Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Server, - TransactionId, - Internal,true, false, RateLimit{.Interval=1000,.MaxCalls=10}, true){} + class RESTAPI_wifiClients_handler : public RESTAPIHandler { + public: + RESTAPI_wifiClients_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, + bool Internal) + : RESTAPIHandler(bindings, L, + std::vector{Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_OPTIONS}, + Server, TransactionId, Internal, true, false, + RateLimit{.Interval = 1000, .MaxCalls = 10}, true) {} - static auto PathName() { return std::list{"/api/v1/wificlients"}; }; + static auto PathName() { return std::list{"/api/v1/wificlients"}; }; - inline bool RoleIsAuthorized(std::string & Reason) { - if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { - Reason = "User must be a subscriber"; - return false; - } - return true; - } + inline bool RoleIsAuthorized(std::string &Reason) { + if (UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } - void DoGet() final; - void DoPost() final {}; - void DoPut() final {}; - void DoDelete() final {}; - private: - }; -} + void DoGet() final; + void DoPost() final{}; + void DoPut() final{}; + void DoDelete() final{}; + + private: + }; +} // namespace OpenWifi diff --git a/src/RESTAPI/RESTAPI_wiredClients_handler.cpp b/src/RESTAPI/RESTAPI_wiredClients_handler.cpp index 067bb39..0c6ccfe 100644 --- a/src/RESTAPI/RESTAPI_wiredClients_handler.cpp +++ b/src/RESTAPI/RESTAPI_wiredClients_handler.cpp @@ -4,119 +4,127 @@ #include "RESTAPI_wiredClients_handler.h" #include "RESTObjects/RESTAPI_SubObjects.h" -#include "nlohmann/json.hpp" #include "StorageService.h" -#include "sdks/SDK_gw.h" #include "framework/utils.h" +#include "nlohmann/json.hpp" +#include "sdks/SDK_gw.h" namespace OpenWifi { - static void AddManufacturers(SubObjects::ClientList &List) { - std::vector> MacList; - for(const auto &i:List.clients) { - MacList.push_back(std::make_pair(i.macAddress,"")); - } + static void AddManufacturers(SubObjects::ClientList &List) { + std::vector> MacList; + for (const auto &i : List.clients) { + MacList.push_back(std::make_pair(i.macAddress, "")); + } - if(SDK::GW::Device::GetOUIs(nullptr,MacList)) { - for(const auto &i:MacList) - for(auto &j:List.clients) - if(j.macAddress==i.first) { - // std::cout << i.first << " :: " << i.second << std::endl; - j.manufacturer = i.second; - } - } - } + if (SDK::GW::Device::GetOUIs(nullptr, MacList)) { + for (const auto &i : MacList) + for (auto &j : List.clients) + if (j.macAddress == i.first) { + // std::cout << i.first << " :: " << i.second << std::endl; + j.manufacturer = i.second; + } + } + } - void RESTAPI_wiredClients_handler::DoGet() { - auto SerialNumber = GetParameter("serialNumber", ""); - if (SerialNumber.empty()) { - return BadRequest(RESTAPI::Errors::MissingSerialNumber); - } + void RESTAPI_wiredClients_handler::DoGet() { + auto SerialNumber = GetParameter("serialNumber", ""); + if (SerialNumber.empty()) { + return BadRequest(RESTAPI::Errors::MissingSerialNumber); + } - Logger().information(fmt::format("{}: Getting list of wired clients.",SerialNumber)); + Logger().information(fmt::format("{}: Getting list of wired clients.", SerialNumber)); - SubObjects::SubscriberInfo SI; - if (!StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id, SI)) { - return NotFound(); - } + SubObjects::SubscriberInfo SI; + if (!StorageService()->SubInfoDB().GetRecord("id", UserInfo_.userinfo.id, SI)) { + return NotFound(); + } - for (const auto &i: SI.accessPoints.list) { - if (SerialNumber == i.macAddress) { + for (const auto &i : SI.accessPoints.list) { + if (SerialNumber == i.macAddress) { - Poco::JSON::Object Answer; - uint64_t Now = Utils::Now(); - Answer.set("created", Now); - Answer.set("modified", Now); - SubObjects::ClientList CList; - CList.modified = CList.created = Now; - Poco::JSON::Object::Ptr LastStats; + Poco::JSON::Object Answer; + uint64_t Now = Utils::Now(); + Answer.set("created", Now); + Answer.set("modified", Now); + SubObjects::ClientList CList; + CList.modified = CList.created = Now; + Poco::JSON::Object::Ptr LastStats; - if(SDK::GW::Device::GetLastStats(nullptr,i.serialNumber,LastStats)){ + if (SDK::GW::Device::GetLastStats(nullptr, i.serialNumber, LastStats)) { - SubObjects::AssociationList AssocList; - AssocList.modified = AssocList.created = Now; - std::stringstream SS; - LastStats->stringify(SS); - try { - auto Stats = nlohmann::json::parse(SS.str()); - if(Stats.contains("interfaces") && Stats["interfaces"].is_array()) { - auto interfaces = Stats["interfaces"]; - for (const auto &cur_interface: interfaces) { - std::set WifiMacs; - if (cur_interface.contains("ssids") && cur_interface["ssids"].is_array() && !cur_interface["ssids"].empty()) { - for (const auto &cur_ssid: cur_interface["ssids"]) { - if (cur_ssid.contains("associations") && cur_ssid["associations"].is_array() && !cur_ssid["associations"].empty()) { - for (const auto &cur_client: cur_ssid["associations"]) { - WifiMacs.insert(cur_client["station"].get()); - } - } - } - } + SubObjects::AssociationList AssocList; + AssocList.modified = AssocList.created = Now; + std::stringstream SS; + LastStats->stringify(SS); + try { + auto Stats = nlohmann::json::parse(SS.str()); + if (Stats.contains("interfaces") && Stats["interfaces"].is_array()) { + auto interfaces = Stats["interfaces"]; + for (const auto &cur_interface : interfaces) { + std::set WifiMacs; + if (cur_interface.contains("ssids") && + cur_interface["ssids"].is_array() && + !cur_interface["ssids"].empty()) { + for (const auto &cur_ssid : cur_interface["ssids"]) { + if (cur_ssid.contains("associations") && + cur_ssid["associations"].is_array() && + !cur_ssid["associations"].empty()) { + for (const auto &cur_client : + cur_ssid["associations"]) { + WifiMacs.insert( + cur_client["station"].get()); + } + } + } + } - if(cur_interface.contains("clients") && cur_interface["clients"].is_array() && !cur_interface["clients"].empty()) { - auto clients = cur_interface["clients"]; - for (const auto &cur_client: clients) { + if (cur_interface.contains("clients") && + cur_interface["clients"].is_array() && + !cur_interface["clients"].empty()) { + auto clients = cur_interface["clients"]; + for (const auto &cur_client : clients) { - std::string Mac = cur_client["mac"]; - if(WifiMacs.find(Mac)==WifiMacs.end()) { + std::string Mac = cur_client["mac"]; + if (WifiMacs.find(Mac) == WifiMacs.end()) { - SubObjects::Client C; + SubObjects::Client C; - C.macAddress = Mac; - if (cur_client.contains("ipv6_addresses") && - cur_client["ipv6_addresses"].is_array() && - !cur_client["ipv6_addresses"].empty()) { - auto ipv6addresses = cur_client["ipv6_addresses"]; - for (const auto &cur_addr: ipv6addresses) { - C.ipv6 = cur_addr; - break; - } - } - if (cur_client.contains("ipv4_addresses") && - cur_client["ipv4_addresses"].is_array() && - !cur_client["ipv4_addresses"].empty()) { - auto ipv4addresses = cur_client["ipv4_addresses"]; - for (const auto &cur_addr: ipv4addresses) { - C.ipv4 = cur_addr; - } - } - C.tx = C.rx = 0; - C.speed = "auto"; - C.mode = "auto"; - CList.clients.push_back(C); - } - } - } - } - } - AddManufacturers(CList); - CList.to_json(Answer); - } catch (...) { - } - } - return ReturnObject(Answer); - } - } - return NotFound(); - } -} \ No newline at end of file + C.macAddress = Mac; + if (cur_client.contains("ipv6_addresses") && + cur_client["ipv6_addresses"].is_array() && + !cur_client["ipv6_addresses"].empty()) { + auto ipv6addresses = cur_client["ipv6_addresses"]; + for (const auto &cur_addr : ipv6addresses) { + C.ipv6 = cur_addr; + break; + } + } + if (cur_client.contains("ipv4_addresses") && + cur_client["ipv4_addresses"].is_array() && + !cur_client["ipv4_addresses"].empty()) { + auto ipv4addresses = cur_client["ipv4_addresses"]; + for (const auto &cur_addr : ipv4addresses) { + C.ipv4 = cur_addr; + } + } + C.tx = C.rx = 0; + C.speed = "auto"; + C.mode = "auto"; + CList.clients.push_back(C); + } + } + } + } + } + AddManufacturers(CList); + CList.to_json(Answer); + } catch (...) { + } + } + return ReturnObject(Answer); + } + } + return NotFound(); + } +} // namespace OpenWifi \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_wiredClients_handler.h b/src/RESTAPI/RESTAPI_wiredClients_handler.h index f172331..17d5c48 100644 --- a/src/RESTAPI/RESTAPI_wiredClients_handler.h +++ b/src/RESTAPI/RESTAPI_wiredClients_handler.h @@ -7,31 +7,32 @@ #include "framework/RESTAPI_Handler.h" namespace OpenWifi { - class RESTAPI_wiredClients_handler : public RESTAPIHandler { - public: - RESTAPI_wiredClients_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId, bool Internal) - : RESTAPIHandler(bindings, L, - std::vector{ - Poco::Net::HTTPRequest::HTTP_GET, - Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Server, - TransactionId, - Internal,true, false, RateLimit{.Interval=1000,.MaxCalls=10}, true){} + class RESTAPI_wiredClients_handler : public RESTAPIHandler { + public: + RESTAPI_wiredClients_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, + uint64_t TransactionId, bool Internal) + : RESTAPIHandler(bindings, L, + std::vector{Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_OPTIONS}, + Server, TransactionId, Internal, true, false, + RateLimit{.Interval = 1000, .MaxCalls = 10}, true) {} - static auto PathName() { return std::list{"/api/v1/wiredclients"}; }; + static auto PathName() { return std::list{"/api/v1/wiredclients"}; }; - inline bool RoleIsAuthorized(std::string & Reason) { - if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { - Reason = "User must be a subscriber"; - return false; - } - return true; - } + inline bool RoleIsAuthorized(std::string &Reason) { + if (UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) { + Reason = "User must be a subscriber"; + return false; + } + return true; + } - void DoGet() final; - void DoPost() final {}; - void DoPut() final {}; - void DoDelete() final {}; - private: - }; -} + void DoGet() final; + void DoPost() final{}; + void DoPut() final{}; + void DoDelete() final{}; + + private: + }; +} // namespace OpenWifi diff --git a/src/RESTObjects/RESTAPI_AnalyticsObjects.cpp b/src/RESTObjects/RESTAPI_AnalyticsObjects.cpp index 6e095e3..82991c2 100644 --- a/src/RESTObjects/RESTAPI_AnalyticsObjects.cpp +++ b/src/RESTObjects/RESTAPI_AnalyticsObjects.cpp @@ -6,619 +6,600 @@ #include "RESTAPI_ProvObjects.h" #include "framework/RESTAPI_utils.h" -using OpenWifi::RESTAPI_utils::field_to_json; using OpenWifi::RESTAPI_utils::field_from_json; +using OpenWifi::RESTAPI_utils::field_to_json; namespace OpenWifi::AnalyticsObjects { - void Report::reset() { - } + void Report::reset() {} - void Report::to_json([[maybe_unused]] Poco::JSON::Object &Obj) const { - } + void Report::to_json([[maybe_unused]] Poco::JSON::Object &Obj) const {} - void VenueInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id",id); - field_to_json(Obj,"name",name); - field_to_json(Obj,"description",description); - field_to_json(Obj,"retention",retention); - field_to_json(Obj,"interval",interval); - field_to_json(Obj,"monitorSubVenues",monitorSubVenues); - } + void VenueInfo::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "retention", retention); + field_to_json(Obj, "interval", interval); + field_to_json(Obj, "monitorSubVenues", monitorSubVenues); + } - bool VenueInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id",id); - field_from_json(Obj,"name",name); - field_from_json(Obj,"description",description); - field_from_json(Obj,"retention",retention); - field_from_json(Obj,"interval",interval); - field_from_json(Obj,"monitorSubVenues",monitorSubVenues); - return true; - } catch(...) { + bool VenueInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "retention", retention); + field_from_json(Obj, "interval", interval); + field_from_json(Obj, "monitorSubVenues", monitorSubVenues); + return true; + } catch (...) { + } + return false; + } - } - return false; - } + void BoardInfo::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "venueList", venueList); + } - void BoardInfo::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json(Obj,"venueList",venueList); - } + bool BoardInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "venueList", venueList); + return true; + } catch (...) { + } + return false; + } - bool BoardInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json(Obj,"venueList",venueList); - return true; - } catch(...) { + void DeviceInfo::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "boardId", boardId); + field_to_json(Obj, "type", type); + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "deviceType", deviceType); + field_to_json(Obj, "lastContact", lastContact); + field_to_json(Obj, "lastPing", lastPing); + field_to_json(Obj, "lastState", lastState); + field_to_json(Obj, "lastFirmware", lastFirmware); + field_to_json(Obj, "lastFirmwareUpdate", lastFirmwareUpdate); + field_to_json(Obj, "lastConnection", lastConnection); + field_to_json(Obj, "lastDisconnection", lastDisconnection); + field_to_json(Obj, "pings", pings); + field_to_json(Obj, "states", states); + field_to_json(Obj, "connected", connected); + field_to_json(Obj, "connectionIp", connectionIp); + field_to_json(Obj, "associations_2g", associations_2g); + field_to_json(Obj, "associations_5g", associations_5g); + field_to_json(Obj, "associations_6g", associations_6g); + field_to_json(Obj, "health", health); + field_to_json(Obj, "lastHealth", lastHealth); + field_to_json(Obj, "locale", locale); + field_to_json(Obj, "uptime", uptime); + field_to_json(Obj, "memory", memory); + } - } - return false; - } + bool DeviceInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "boardId", boardId); + field_from_json(Obj, "type", type); + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "deviceType", deviceType); + field_from_json(Obj, "lastContact", lastContact); + field_from_json(Obj, "lastPing", lastPing); + field_from_json(Obj, "lastState", lastState); + field_from_json(Obj, "lastFirmware", lastFirmware); + field_from_json(Obj, "lastFirmwareUpdate", lastFirmwareUpdate); + field_from_json(Obj, "lastConnection", lastConnection); + field_from_json(Obj, "lastDisconnection", lastDisconnection); + field_from_json(Obj, "pings", pings); + field_from_json(Obj, "states", states); + field_from_json(Obj, "connected", connected); + field_from_json(Obj, "connectionIp", connectionIp); + field_from_json(Obj, "associations_2g", associations_2g); + field_from_json(Obj, "associations_5g", associations_5g); + field_from_json(Obj, "associations_6g", associations_6g); + field_from_json(Obj, "health", health); + field_from_json(Obj, "lastHealth", lastHealth); + field_from_json(Obj, "locale", locale); + field_from_json(Obj, "uptime", uptime); + field_from_json(Obj, "memory", memory); + return true; + } catch (...) { + } + return false; + } - void DeviceInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"boardId",boardId); - field_to_json(Obj,"type",type); - field_to_json(Obj,"serialNumber",serialNumber); - field_to_json(Obj,"deviceType",deviceType); - field_to_json(Obj,"lastContact",lastContact); - field_to_json(Obj,"lastPing",lastPing); - field_to_json(Obj,"lastState",lastState); - field_to_json(Obj,"lastFirmware",lastFirmware); - field_to_json(Obj,"lastFirmwareUpdate",lastFirmwareUpdate); - field_to_json(Obj,"lastConnection",lastConnection); - field_to_json(Obj,"lastDisconnection",lastDisconnection); - field_to_json(Obj,"pings",pings); - field_to_json(Obj,"states",states); - field_to_json(Obj,"connected",connected); - field_to_json(Obj,"connectionIp",connectionIp); - field_to_json(Obj,"associations_2g",associations_2g); - field_to_json(Obj,"associations_5g",associations_5g); - field_to_json(Obj,"associations_6g",associations_6g); - field_to_json(Obj,"health",health); - field_to_json(Obj,"lastHealth",lastHealth); - field_to_json(Obj,"locale",locale); - field_to_json(Obj,"uptime",uptime); - field_to_json(Obj,"memory",memory); - } + void DeviceInfoList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "devices", devices); + } - bool DeviceInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"boardId",boardId); - field_from_json(Obj,"type",type); - field_from_json(Obj,"serialNumber",serialNumber); - field_from_json(Obj,"deviceType",deviceType); - field_from_json(Obj,"lastContact",lastContact); - field_from_json(Obj,"lastPing",lastPing); - field_from_json(Obj,"lastState",lastState); - field_from_json(Obj,"lastFirmware",lastFirmware); - field_from_json(Obj,"lastFirmwareUpdate",lastFirmwareUpdate); - field_from_json(Obj,"lastConnection",lastConnection); - field_from_json(Obj,"lastDisconnection",lastDisconnection); - field_from_json(Obj,"pings",pings); - field_from_json(Obj,"states",states); - field_from_json(Obj,"connected",connected); - field_from_json(Obj,"connectionIp",connectionIp); - field_from_json(Obj,"associations_2g",associations_2g); - field_from_json(Obj,"associations_5g",associations_5g); - field_from_json(Obj,"associations_6g",associations_6g); - field_from_json(Obj,"health",health); - field_from_json(Obj,"lastHealth",lastHealth); - field_from_json(Obj,"locale",locale); - field_from_json(Obj,"uptime",uptime); - field_from_json(Obj,"memory",memory); - return true; - } catch(...) { + bool DeviceInfoList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "devices", devices); + return true; + } catch (...) { + } + return false; + } - } - return false; - } + void UE_rate::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "bitrate", bitrate); + field_to_json(Obj, "mcs", mcs); + field_to_json(Obj, "nss", nss); + field_to_json(Obj, "ht", ht); + field_to_json(Obj, "sgi", sgi); + field_to_json(Obj, "chwidth", chwidth); + } - void DeviceInfoList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"devices",devices); - } + bool UE_rate::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "bitrate", bitrate); + field_from_json(Obj, "mcs", mcs); + field_from_json(Obj, "nss", nss); + field_from_json(Obj, "ht", ht); + field_from_json(Obj, "sgi", sgi); + field_from_json(Obj, "chwidth", chwidth); + return true; + } catch (...) { + } + return false; + } - bool DeviceInfoList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"devices",devices); - return true; - } catch(...) { + void UETimePoint::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "station", station); + field_to_json(Obj, "rssi", rssi); + field_to_json(Obj, "tx_bytes", tx_bytes); + field_to_json(Obj, "rx_bytes", rx_bytes); + field_to_json(Obj, "tx_duration", tx_duration); + field_to_json(Obj, "rx_packets", rx_packets); + field_to_json(Obj, "tx_packets", tx_packets); + field_to_json(Obj, "tx_retries", tx_retries); + field_to_json(Obj, "tx_failed", tx_failed); + field_to_json(Obj, "connected", connected); + field_to_json(Obj, "inactive", inactive); + field_to_json(Obj, "tx_rate", tx_rate); + field_to_json(Obj, "rx_rate", rx_rate); + // field_to_json(Obj, "tidstats", tidstats); - } - return false; - } + field_to_json(Obj, "tx_bytes_bw", tx_bytes_bw); + field_to_json(Obj, "rx_bytes_bw", rx_bytes_bw); + field_to_json(Obj, "tx_packets_bw", tx_packets_bw); + field_to_json(Obj, "rx_packets_bw", rx_packets_bw); + field_to_json(Obj, "tx_failed_pct", tx_failed_pct); + field_to_json(Obj, "tx_retries_pct", tx_retries_pct); + field_to_json(Obj, "tx_duration_pct", tx_duration_pct); - void UE_rate::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"bitrate",bitrate); - field_to_json(Obj,"mcs",mcs); - field_to_json(Obj,"nss",nss); - field_to_json(Obj,"ht",ht); - field_to_json(Obj,"sgi",sgi); - field_to_json(Obj,"chwidth",chwidth); - } + field_to_json(Obj, "tx_bytes_delta", tx_bytes_delta); + field_to_json(Obj, "rx_bytes_delta", rx_bytes_delta); + field_to_json(Obj, "tx_packets_delta", tx_packets_delta); + field_to_json(Obj, "rx_packets_delta", rx_packets_delta); + field_to_json(Obj, "tx_failed_delta", tx_failed_delta); + field_to_json(Obj, "tx_retries_delta", tx_retries_delta); + field_to_json(Obj, "tx_duration_delta", tx_duration_delta); + } - bool UE_rate::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"bitrate",bitrate); - field_from_json(Obj,"mcs",mcs); - field_from_json(Obj,"nss",nss); - field_from_json(Obj,"ht",ht); - field_from_json(Obj,"sgi",sgi); - field_from_json(Obj,"chwidth",chwidth); - return true; - } catch(...) { + bool UETimePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "station", station); + field_from_json(Obj, "rssi", rssi); + field_from_json(Obj, "tx_bytes", tx_bytes); + field_from_json(Obj, "rx_bytes", rx_bytes); + field_from_json(Obj, "tx_duration", tx_duration); + field_from_json(Obj, "rx_packets", rx_packets); + field_from_json(Obj, "tx_packets", tx_packets); + field_from_json(Obj, "tx_retries", tx_retries); + field_from_json(Obj, "tx_failed", tx_failed); + field_from_json(Obj, "connected", connected); + field_from_json(Obj, "inactive", inactive); + field_from_json(Obj, "tx_rate", tx_rate); + field_from_json(Obj, "rx_rate", rx_rate); + // field_from_json(Obj,"tidstats",tidstats); + field_from_json(Obj, "tx_bytes_bw", tx_bytes_bw); + field_from_json(Obj, "rx_bytes_bw", rx_bytes_bw); + field_from_json(Obj, "tx_packets_bw", tx_packets_bw); + field_from_json(Obj, "rx_packets_bw", rx_packets_bw); + field_from_json(Obj, "tx_failed_pct", tx_failed_pct); + field_from_json(Obj, "tx_retries_pct", tx_retries_pct); + field_from_json(Obj, "tx_duration_pct", tx_duration_pct); + field_from_json(Obj, "tx_bytes_delta", tx_bytes_delta); + field_from_json(Obj, "rx_bytes_delta", rx_bytes_delta); + field_from_json(Obj, "tx_packets_delta", tx_packets_delta); + field_from_json(Obj, "rx_packets_delta", rx_packets_delta); + field_from_json(Obj, "tx_failed_delta", tx_failed_delta); + field_from_json(Obj, "tx_retries_delta", tx_retries_delta); + field_from_json(Obj, "tx_duration_delta", tx_duration_delta); + return true; + } catch (...) { + } + return false; + } - } - return false; - } + void APTimePoint::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "collisions", collisions); + field_to_json(Obj, "multicast", multicast); + field_to_json(Obj, "rx_bytes", rx_bytes); + field_to_json(Obj, "rx_dropped", rx_dropped); + field_to_json(Obj, "rx_errors", rx_errors); + field_to_json(Obj, "rx_packets", rx_packets); + field_to_json(Obj, "tx_bytes", tx_bytes); + field_to_json(Obj, "tx_packets", tx_packets); + field_to_json(Obj, "tx_dropped", tx_dropped); + field_to_json(Obj, "tx_errors", tx_errors); + field_to_json(Obj, "tx_packets", tx_packets); - void UETimePoint::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"station",station); - field_to_json(Obj,"rssi",rssi); - field_to_json(Obj,"tx_bytes",tx_bytes); - field_to_json(Obj,"rx_bytes",rx_bytes); - field_to_json(Obj,"tx_duration",tx_duration); - field_to_json(Obj,"rx_packets",rx_packets); - field_to_json(Obj,"tx_packets",tx_packets); - field_to_json(Obj,"tx_retries",tx_retries); - field_to_json(Obj,"tx_failed",tx_failed); - field_to_json(Obj,"connected",connected); - field_to_json(Obj,"inactive",inactive); - field_to_json(Obj,"tx_rate",tx_rate); - field_to_json(Obj,"rx_rate",rx_rate); -// field_to_json(Obj, "tidstats", tidstats); + field_to_json(Obj, "tx_bytes_bw", tx_bytes_bw); + field_to_json(Obj, "rx_bytes_bw", rx_bytes_bw); + field_to_json(Obj, "rx_dropped_pct", rx_dropped_pct); + field_to_json(Obj, "tx_dropped_pct", tx_dropped_pct); + field_to_json(Obj, "rx_packets_bw", rx_packets_bw); + field_to_json(Obj, "tx_packets_bw", tx_packets_bw); + field_to_json(Obj, "rx_errors_pct", rx_errors_pct); + field_to_json(Obj, "tx_errors_pct", tx_errors_pct); - field_to_json(Obj,"tx_bytes_bw",tx_bytes_bw); - field_to_json(Obj,"rx_bytes_bw",rx_bytes_bw); - field_to_json(Obj,"tx_packets_bw",tx_packets_bw); - field_to_json(Obj,"rx_packets_bw",rx_packets_bw); - field_to_json(Obj,"tx_failed_pct",tx_failed_pct); - field_to_json(Obj,"tx_retries_pct",tx_retries_pct); - field_to_json(Obj,"tx_duration_pct",tx_duration_pct); + field_to_json(Obj, "tx_bytes_delta", tx_bytes_delta); + field_to_json(Obj, "rx_bytes_delta", rx_bytes_delta); + field_to_json(Obj, "rx_dropped_delta", rx_dropped_delta); + field_to_json(Obj, "tx_dropped_delta", tx_dropped_delta); + field_to_json(Obj, "rx_packets_delta", rx_packets_delta); + field_to_json(Obj, "tx_packets_delta", tx_packets_delta); + field_to_json(Obj, "rx_errors_delta", rx_errors_delta); + field_to_json(Obj, "tx_errors_delta", tx_errors_delta); + } - field_to_json(Obj,"tx_bytes_delta",tx_bytes_delta); - field_to_json(Obj,"rx_bytes_delta",rx_bytes_delta); - field_to_json(Obj,"tx_packets_delta",tx_packets_delta); - field_to_json(Obj,"rx_packets_delta",rx_packets_delta); - field_to_json(Obj,"tx_failed_delta",tx_failed_delta); - field_to_json(Obj,"tx_retries_delta",tx_retries_delta); - field_to_json(Obj,"tx_duration_delta",tx_duration_delta); - } + bool APTimePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "collisions", collisions); + field_from_json(Obj, "multicast", multicast); + field_from_json(Obj, "rx_bytes", rx_bytes); + field_from_json(Obj, "rx_dropped", rx_dropped); + field_from_json(Obj, "rx_errors", rx_errors); + field_from_json(Obj, "rx_packets", rx_packets); + field_from_json(Obj, "tx_bytes", tx_bytes); + field_from_json(Obj, "tx_packets", tx_packets); + field_from_json(Obj, "tx_dropped", tx_dropped); + field_from_json(Obj, "tx_errors", tx_errors); + field_from_json(Obj, "tx_packets", tx_packets); - bool UETimePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"station",station); - field_from_json(Obj,"rssi",rssi); - field_from_json(Obj,"tx_bytes",tx_bytes); - field_from_json(Obj,"rx_bytes",rx_bytes); - field_from_json(Obj,"tx_duration",tx_duration); - field_from_json(Obj,"rx_packets",rx_packets); - field_from_json(Obj,"tx_packets",tx_packets); - field_from_json(Obj,"tx_retries",tx_retries); - field_from_json(Obj,"tx_failed",tx_failed); - field_from_json(Obj,"connected",connected); - field_from_json(Obj,"inactive",inactive); - field_from_json(Obj,"tx_rate",tx_rate); - field_from_json(Obj,"rx_rate",rx_rate); -// field_from_json(Obj,"tidstats",tidstats); - field_from_json(Obj,"tx_bytes_bw",tx_bytes_bw); - field_from_json(Obj,"rx_bytes_bw",rx_bytes_bw); - field_from_json(Obj,"tx_packets_bw",tx_packets_bw); - field_from_json(Obj,"rx_packets_bw",rx_packets_bw); - field_from_json(Obj,"tx_failed_pct",tx_failed_pct); - field_from_json(Obj,"tx_retries_pct",tx_retries_pct); - field_from_json(Obj,"tx_duration_pct",tx_duration_pct); - field_from_json(Obj,"tx_bytes_delta",tx_bytes_delta); - field_from_json(Obj,"rx_bytes_delta",rx_bytes_delta); - field_from_json(Obj,"tx_packets_delta",tx_packets_delta); - field_from_json(Obj,"rx_packets_delta",rx_packets_delta); - field_from_json(Obj,"tx_failed_delta",tx_failed_delta); - field_from_json(Obj,"tx_retries_delta",tx_retries_delta); - field_from_json(Obj,"tx_duration_delta",tx_duration_delta); - return true; - } catch(...) { + field_from_json(Obj, "tx_bytes_bw", tx_bytes_bw); + field_from_json(Obj, "rx_bytes_bw", rx_bytes_bw); + field_from_json(Obj, "rx_dropped_pct", rx_dropped_pct); + field_from_json(Obj, "tx_dropped_pct", tx_dropped_pct); + field_from_json(Obj, "rx_packets_bw", rx_packets_bw); + field_from_json(Obj, "tx_packets_bw", tx_packets_bw); + field_from_json(Obj, "rx_errors_pct", rx_errors_pct); + field_from_json(Obj, "tx_errors_pct", tx_errors_pct); - } - return false; - } + field_from_json(Obj, "tx_bytes_delta", tx_bytes_delta); + field_from_json(Obj, "rx_bytes_delta", rx_bytes_delta); + field_from_json(Obj, "rx_dropped_delta", rx_dropped_delta); + field_from_json(Obj, "tx_dropped_delta", tx_dropped_delta); + field_from_json(Obj, "rx_packets_delta", rx_packets_delta); + field_from_json(Obj, "tx_packets_delta", tx_packets_delta); + field_from_json(Obj, "rx_errors_delta", rx_errors_delta); + field_from_json(Obj, "tx_errors_delta", tx_errors_delta); - void APTimePoint::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"collisions",collisions); - field_to_json(Obj,"multicast",multicast); - field_to_json(Obj,"rx_bytes",rx_bytes); - field_to_json(Obj,"rx_dropped",rx_dropped); - field_to_json(Obj,"rx_errors",rx_errors); - field_to_json(Obj,"rx_packets",rx_packets); - field_to_json(Obj,"tx_bytes",tx_bytes); - field_to_json(Obj,"tx_packets",tx_packets); - field_to_json(Obj,"tx_dropped",tx_dropped); - field_to_json(Obj,"tx_errors",tx_errors); - field_to_json(Obj,"tx_packets",tx_packets); + return true; + } catch (...) { + } + return false; + } - field_to_json(Obj,"tx_bytes_bw",tx_bytes_bw); - field_to_json(Obj,"rx_bytes_bw",rx_bytes_bw); - field_to_json(Obj,"rx_dropped_pct",rx_dropped_pct); - field_to_json(Obj,"tx_dropped_pct",tx_dropped_pct); - field_to_json(Obj,"rx_packets_bw",rx_packets_bw); - field_to_json(Obj,"tx_packets_bw",tx_packets_bw); - field_to_json(Obj,"rx_errors_pct",rx_errors_pct); - field_to_json(Obj,"tx_errors_pct",tx_errors_pct); + void TIDstat_entry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "rx_msdu", rx_msdu); + field_to_json(Obj, "tx_msdu", tx_msdu); + field_to_json(Obj, "tx_msdu_failed", tx_msdu_failed); + field_to_json(Obj, "tx_msdu_retries", tx_msdu_retries); + } - field_to_json(Obj,"tx_bytes_delta",tx_bytes_delta); - field_to_json(Obj,"rx_bytes_delta",rx_bytes_delta); - field_to_json(Obj,"rx_dropped_delta",rx_dropped_delta); - field_to_json(Obj,"tx_dropped_delta",tx_dropped_delta); - field_to_json(Obj,"rx_packets_delta",rx_packets_delta); - field_to_json(Obj,"tx_packets_delta",tx_packets_delta); - field_to_json(Obj,"rx_errors_delta",rx_errors_delta); - field_to_json(Obj,"tx_errors_delta",tx_errors_delta); - } + bool TIDstat_entry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "rx_msdu", rx_msdu); + field_from_json(Obj, "tx_msdu", tx_msdu); + field_from_json(Obj, "tx_msdu_failed", tx_msdu_failed); + field_from_json(Obj, "tx_msdu_retries", tx_msdu_retries); + return true; + } catch (...) { + } + return false; + } - bool APTimePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"collisions",collisions); - field_from_json(Obj,"multicast",multicast); - field_from_json(Obj,"rx_bytes",rx_bytes); - field_from_json(Obj,"rx_dropped",rx_dropped); - field_from_json(Obj,"rx_errors",rx_errors); - field_from_json(Obj,"rx_packets",rx_packets); - field_from_json(Obj,"tx_bytes",tx_bytes); - field_from_json(Obj,"tx_packets",tx_packets); - field_from_json(Obj,"tx_dropped",tx_dropped); - field_from_json(Obj,"tx_errors",tx_errors); - field_from_json(Obj,"tx_packets",tx_packets); + void RadioTimePoint::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "band", band); + field_to_json(Obj, "channel_width", channel_width); + field_to_json(Obj, "active_ms", active_ms); + field_to_json(Obj, "busy_ms", busy_ms); + field_to_json(Obj, "receive_ms", receive_ms); + field_to_json(Obj, "transmit_ms", transmit_ms); + field_to_json(Obj, "tx_power", tx_power); + field_to_json(Obj, "channel", channel); + field_to_json(Obj, "temperature", temperature); + field_to_json(Obj, "noise", noise); + field_to_json(Obj, "active_pct", active_pct); + field_to_json(Obj, "busy_pct", busy_pct); + field_to_json(Obj, "receive_pct", receive_pct); + field_to_json(Obj, "transmit_pct", transmit_pct); + } - field_from_json(Obj,"tx_bytes_bw",tx_bytes_bw); - field_from_json(Obj,"rx_bytes_bw",rx_bytes_bw); - field_from_json(Obj,"rx_dropped_pct",rx_dropped_pct); - field_from_json(Obj,"tx_dropped_pct",tx_dropped_pct); - field_from_json(Obj,"rx_packets_bw",rx_packets_bw); - field_from_json(Obj,"tx_packets_bw",tx_packets_bw); - field_from_json(Obj,"rx_errors_pct",rx_errors_pct); - field_from_json(Obj,"tx_errors_pct",tx_errors_pct); + bool RadioTimePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "band", band); + field_from_json(Obj, "channel_width", channel_width); + field_from_json(Obj, "active_ms", active_ms); + field_from_json(Obj, "busy_ms", busy_ms); + field_from_json(Obj, "receive_ms", receive_ms); + field_from_json(Obj, "transmit_ms", transmit_ms); + field_from_json(Obj, "tx_power", tx_power); + field_from_json(Obj, "channel", channel); + field_from_json(Obj, "temperature", temperature); + field_from_json(Obj, "noise", noise); + field_from_json(Obj, "active_pct", active_pct); + field_from_json(Obj, "busy_pct", busy_pct); + field_from_json(Obj, "receive_pct", receive_pct); + field_from_json(Obj, "transmit_pct", transmit_pct); + return true; + } catch (...) { + } + return false; + } - field_from_json(Obj,"tx_bytes_delta",tx_bytes_delta); - field_from_json(Obj,"rx_bytes_delta",rx_bytes_delta); - field_from_json(Obj,"rx_dropped_delta",rx_dropped_delta); - field_from_json(Obj,"tx_dropped_delta",tx_dropped_delta); - field_from_json(Obj,"rx_packets_delta",rx_packets_delta); - field_from_json(Obj,"tx_packets_delta",tx_packets_delta); - field_from_json(Obj,"rx_errors_delta",rx_errors_delta); - field_from_json(Obj,"tx_errors_delta",tx_errors_delta); + void AveragePoint::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "min", min); + field_to_json(Obj, "max", max); + field_to_json(Obj, "avg", avg); + } - return true; - } catch(...) { + bool AveragePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "min", min); + field_from_json(Obj, "max", max); + field_from_json(Obj, "avg", avg); + return true; + } catch (...) { + } + return false; + } - } - return false; - } + void SSIDTimePoint::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "bssid", bssid); + field_to_json(Obj, "mode", mode); + field_to_json(Obj, "ssid", ssid); + field_to_json(Obj, "band", band); + field_to_json(Obj, "channel", channel); + field_to_json(Obj, "associations", associations); + field_to_json(Obj, "tx_bytes_bw", tx_bytes_bw); + field_to_json(Obj, "rx_bytes_bw", rx_bytes_bw); + field_to_json(Obj, "tx_packets_bw", tx_packets_bw); + field_to_json(Obj, "rx_packets_bw", rx_packets_bw); + field_to_json(Obj, "tx_failed_pct", tx_failed_pct); + field_to_json(Obj, "tx_retries_pct", tx_retries_pct); + field_to_json(Obj, "tx_duration_pct", tx_duration_pct); + } - void TIDstat_entry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"rx_msdu",rx_msdu); - field_to_json(Obj,"tx_msdu",tx_msdu); - field_to_json(Obj,"tx_msdu_failed",tx_msdu_failed); - field_to_json(Obj,"tx_msdu_retries",tx_msdu_retries); - } + bool SSIDTimePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "bssid", bssid); + field_from_json(Obj, "mode", mode); + field_from_json(Obj, "ssid", ssid); + field_from_json(Obj, "band", band); + field_from_json(Obj, "channel", channel); + field_from_json(Obj, "associations", associations); + field_from_json(Obj, "tx_bytes_bw", tx_bytes_bw); + field_from_json(Obj, "rx_bytes_bw", rx_bytes_bw); + field_from_json(Obj, "tx_packets_bw", tx_packets_bw); + field_from_json(Obj, "rx_packets_bw", rx_packets_bw); + field_from_json(Obj, "tx_failed_pct", tx_failed_pct); + field_from_json(Obj, "tx_retries_pct", tx_retries_pct); + field_from_json(Obj, "tx_duration_pct", tx_duration_pct); + return true; + } catch (...) { + } + return false; + } - bool TIDstat_entry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"rx_msdu",rx_msdu); - field_from_json(Obj,"tx_msdu",tx_msdu); - field_from_json(Obj,"tx_msdu_failed",tx_msdu_failed); - field_from_json(Obj,"tx_msdu_retries",tx_msdu_retries); - return true; - } catch(...) { + void DeviceTimePoint::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "boardId", boardId); + field_to_json(Obj, "timestamp", timestamp); + field_to_json(Obj, "ap_data", ap_data); + field_to_json(Obj, "ssid_data", ssid_data); + field_to_json(Obj, "radio_data", radio_data); + field_to_json(Obj, "device_info", device_info); + field_to_json(Obj, "serialNumber", serialNumber); + } - } - return false; - } + bool DeviceTimePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "boardId", boardId); + field_from_json(Obj, "timestamp", timestamp); + field_from_json(Obj, "ap_data", ap_data); + field_from_json(Obj, "ssid_data", ssid_data); + field_from_json(Obj, "radio_data", radio_data); + field_from_json(Obj, "device_info", device_info); + field_from_json(Obj, "serialNumber", serialNumber); + return true; + } catch (...) { + } + return false; + } - void RadioTimePoint::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"band",band); - field_to_json(Obj,"channel_width",channel_width); - field_to_json(Obj,"active_ms",active_ms); - field_to_json(Obj,"busy_ms",busy_ms); - field_to_json(Obj,"receive_ms",receive_ms); - field_to_json(Obj,"transmit_ms",transmit_ms); - field_to_json(Obj,"tx_power",tx_power); - field_to_json(Obj,"channel",channel); - field_to_json(Obj,"temperature",temperature); - field_to_json(Obj,"noise",noise); - field_to_json(Obj,"active_pct",active_pct); - field_to_json(Obj,"busy_pct",busy_pct); - field_to_json(Obj,"receive_pct",receive_pct); - field_to_json(Obj,"transmit_pct",transmit_pct); - } + void DeviceTimePointAnalysis::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "noise", noise); + field_to_json(Obj, "temperature", temperature); + field_to_json(Obj, "active_pct", active_pct); + field_to_json(Obj, "busy_pct", busy_pct); + field_to_json(Obj, "receive_pct", receive_pct); + field_to_json(Obj, "transmit_pct", transmit_pct); + field_to_json(Obj, "tx_power", tx_power); + field_to_json(Obj, "tx_bytes_bw", tx_bytes_bw); + field_to_json(Obj, "rx_bytes_bw", rx_bytes_bw); + field_to_json(Obj, "rx_dropped_pct", rx_dropped_pct); + field_to_json(Obj, "tx_dropped_pct", tx_dropped_pct); + field_to_json(Obj, "rx_packets_bw", rx_packets_bw); + field_to_json(Obj, "tx_packets_bw", tx_packets_bw); + field_to_json(Obj, "rx_errors_pct", rx_errors_pct); + field_to_json(Obj, "tx_errors_pct", tx_errors_pct); + } - bool RadioTimePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"band",band); - field_from_json(Obj,"channel_width",channel_width); - field_from_json(Obj,"active_ms",active_ms); - field_from_json(Obj,"busy_ms",busy_ms); - field_from_json(Obj,"receive_ms",receive_ms); - field_from_json(Obj,"transmit_ms",transmit_ms); - field_from_json(Obj,"tx_power",tx_power); - field_from_json(Obj,"channel",channel); - field_from_json(Obj,"temperature",temperature); - field_from_json(Obj,"noise",noise); - field_from_json(Obj,"active_pct",active_pct); - field_from_json(Obj,"busy_pct",busy_pct); - field_from_json(Obj,"receive_pct",receive_pct); - field_from_json(Obj,"transmit_pct",transmit_pct); - return true; - } catch(...) { + bool DeviceTimePointAnalysis::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "noise", noise); + field_from_json(Obj, "temperature", temperature); + field_from_json(Obj, "active_pct", active_pct); + field_from_json(Obj, "busy_pct", busy_pct); + field_from_json(Obj, "receive_pct", receive_pct); + field_from_json(Obj, "transmit_pct", transmit_pct); + field_from_json(Obj, "tx_power", tx_power); + field_from_json(Obj, "tx_bytes_bw", tx_bytes_bw); + field_from_json(Obj, "rx_bytes_bw", rx_bytes_bw); + field_from_json(Obj, "rx_dropped_pct", rx_dropped_pct); + field_from_json(Obj, "tx_dropped_pct", tx_dropped_pct); + field_from_json(Obj, "rx_packets_bw", rx_packets_bw); + field_from_json(Obj, "tx_packets_bw", tx_packets_bw); + field_from_json(Obj, "rx_errors_pct", rx_errors_pct); + field_from_json(Obj, "tx_errors_pct", tx_errors_pct); + return true; + } catch (...) { + } + return false; + } - } - return false; - } + void DeviceTimePointList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "points", points); + field_to_json(Obj, "stats", stats); + } - void AveragePoint::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"min",min); - field_to_json(Obj,"max",max); - field_to_json(Obj,"avg",avg); - } + bool DeviceTimePointList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "points", points); + field_from_json(Obj, "stats", stats); + return true; + } catch (...) { + } + return false; + } - bool AveragePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"min",min); - field_from_json(Obj,"max",max); - field_from_json(Obj,"avg",avg); - return true; - } catch(...) { + void DeviceTimePointStats::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "firstPoint", firstPoint); + field_to_json(Obj, "lastPoint", lastPoint); + field_to_json(Obj, "count", count); + } - } - return false; - } + bool DeviceTimePointStats::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "firstPoint", firstPoint); + field_from_json(Obj, "lastPoint", lastPoint); + field_from_json(Obj, "count", count); + return true; + } catch (...) { + } + return false; + } - void SSIDTimePoint::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"bssid",bssid); - field_to_json(Obj,"mode",mode); - field_to_json(Obj,"ssid",ssid); - field_to_json(Obj,"band",band); - field_to_json(Obj,"channel",channel); - field_to_json(Obj,"associations",associations); - field_to_json(Obj,"tx_bytes_bw",tx_bytes_bw); - field_to_json(Obj,"rx_bytes_bw",rx_bytes_bw); - field_to_json(Obj,"tx_packets_bw",tx_packets_bw); - field_to_json(Obj,"rx_packets_bw",rx_packets_bw); - field_to_json(Obj,"tx_failed_pct",tx_failed_pct); - field_to_json(Obj,"tx_retries_pct",tx_retries_pct); - field_to_json(Obj,"tx_duration_pct",tx_duration_pct); - } + void WifiClientRate::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "bitrate", bitrate); + field_to_json(Obj, "chwidth", chwidth); + field_to_json(Obj, "mcs", mcs); + field_to_json(Obj, "nss", nss); + field_to_json(Obj, "vht", vht); + } - bool SSIDTimePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"bssid",bssid); - field_from_json(Obj,"mode",mode); - field_from_json(Obj,"ssid",ssid); - field_from_json(Obj,"band",band); - field_from_json(Obj,"channel",channel); - field_from_json(Obj,"associations",associations); - field_from_json(Obj,"tx_bytes_bw",tx_bytes_bw); - field_from_json(Obj,"rx_bytes_bw",rx_bytes_bw); - field_from_json(Obj,"tx_packets_bw",tx_packets_bw); - field_from_json(Obj,"rx_packets_bw",rx_packets_bw); - field_from_json(Obj,"tx_failed_pct",tx_failed_pct); - field_from_json(Obj,"tx_retries_pct",tx_retries_pct); - field_from_json(Obj,"tx_duration_pct",tx_duration_pct); - return true; - } catch(...) { + bool WifiClientRate::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "bitrate", bitrate); + field_from_json(Obj, "chwidth", chwidth); + field_from_json(Obj, "mcs", mcs); + field_from_json(Obj, "nss", nss); + field_from_json(Obj, "vht", vht); + return true; + } catch (...) { + } + return false; + } - } - return false; - } + void WifiClientHistory::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "timestamp", timestamp); + field_to_json(Obj, "station_id", station_id); + field_to_json(Obj, "bssid", bssid); + field_to_json(Obj, "ssid", ssid); + field_to_json(Obj, "rssi", rssi); + field_to_json(Obj, "rx_bitrate", rx_bitrate); + field_to_json(Obj, "rx_chwidth", rx_chwidth); + field_to_json(Obj, "rx_mcs", rx_mcs); + field_to_json(Obj, "rx_nss", rx_nss); + field_to_json(Obj, "rx_vht", rx_vht); + field_to_json(Obj, "tx_bitrate", tx_bitrate); + field_to_json(Obj, "tx_chwidth", tx_chwidth); + field_to_json(Obj, "tx_mcs", tx_mcs); + field_to_json(Obj, "tx_nss", tx_nss); + field_to_json(Obj, "tx_vht", tx_vht); + field_to_json(Obj, "rx_bytes", rx_bytes); + field_to_json(Obj, "tx_bytes", tx_bytes); + field_to_json(Obj, "rx_duration", rx_duration); + field_to_json(Obj, "tx_duration", tx_duration); + field_to_json(Obj, "rx_packets", rx_packets); + field_to_json(Obj, "tx_packets", tx_packets); + field_to_json(Obj, "ipv4", ipv4); + field_to_json(Obj, "ipv6", ipv6); + field_to_json(Obj, "channel_width", channel_width); + field_to_json(Obj, "noise", noise); + field_to_json(Obj, "tx_power", tx_power); + field_to_json(Obj, "channel", channel); + field_to_json(Obj, "active_ms", active_ms); + field_to_json(Obj, "busy_ms", busy_ms); + field_to_json(Obj, "receive_ms", receive_ms); + field_to_json(Obj, "mode", mode); + field_to_json(Obj, "ack_signal", ack_signal); + field_to_json(Obj, "ack_signal_avg", ack_signal_avg); + field_to_json(Obj, "connected", connected); + field_to_json(Obj, "inactive", inactive); + field_to_json(Obj, "tx_retries", tx_retries); + field_to_json(Obj, "venue_id", venue_id); + } - void DeviceTimePoint::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id",id); - field_to_json(Obj,"boardId",boardId); - field_to_json(Obj,"timestamp",timestamp); - field_to_json(Obj,"ap_data",ap_data); - field_to_json(Obj,"ssid_data",ssid_data); - field_to_json(Obj,"radio_data",radio_data); - field_to_json(Obj,"device_info",device_info); - field_to_json(Obj,"serialNumber",serialNumber); - } - - bool DeviceTimePoint::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id",id); - field_from_json(Obj,"boardId",boardId); - field_from_json(Obj,"timestamp",timestamp); - field_from_json(Obj,"ap_data",ap_data); - field_from_json(Obj,"ssid_data",ssid_data); - field_from_json(Obj,"radio_data",radio_data); - field_from_json(Obj,"device_info",device_info); - field_from_json(Obj,"serialNumber",serialNumber); - return true; - } catch(...) { - - } - return false; - } - - void DeviceTimePointAnalysis::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"noise",noise); - field_to_json(Obj,"temperature",temperature); - field_to_json(Obj,"active_pct",active_pct); - field_to_json(Obj,"busy_pct",busy_pct); - field_to_json(Obj,"receive_pct",receive_pct); - field_to_json(Obj,"transmit_pct",transmit_pct); - field_to_json(Obj,"tx_power",tx_power); - field_to_json(Obj,"tx_bytes_bw",tx_bytes_bw); - field_to_json(Obj,"rx_bytes_bw",rx_bytes_bw); - field_to_json(Obj,"rx_dropped_pct",rx_dropped_pct); - field_to_json(Obj,"tx_dropped_pct",tx_dropped_pct); - field_to_json(Obj,"rx_packets_bw",rx_packets_bw); - field_to_json(Obj,"tx_packets_bw",tx_packets_bw); - field_to_json(Obj,"rx_errors_pct",rx_errors_pct); - field_to_json(Obj,"tx_errors_pct",tx_errors_pct); - } - - bool DeviceTimePointAnalysis::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"noise",noise); - field_from_json(Obj,"temperature",temperature); - field_from_json(Obj,"active_pct",active_pct); - field_from_json(Obj,"busy_pct",busy_pct); - field_from_json(Obj,"receive_pct",receive_pct); - field_from_json(Obj,"transmit_pct",transmit_pct); - field_from_json(Obj,"tx_power",tx_power); - field_from_json(Obj,"tx_bytes_bw",tx_bytes_bw); - field_from_json(Obj,"rx_bytes_bw",rx_bytes_bw); - field_from_json(Obj,"rx_dropped_pct",rx_dropped_pct); - field_from_json(Obj,"tx_dropped_pct",tx_dropped_pct); - field_from_json(Obj,"rx_packets_bw",rx_packets_bw); - field_from_json(Obj,"tx_packets_bw",tx_packets_bw); - field_from_json(Obj,"rx_errors_pct",rx_errors_pct); - field_from_json(Obj,"tx_errors_pct",tx_errors_pct); - return true; - } catch(...) { - - } - return false; - } - - void DeviceTimePointList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"points",points); - field_to_json(Obj,"stats",stats); - } - - bool DeviceTimePointList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"points",points); - field_from_json(Obj,"stats",stats); - return true; - } catch(...) { - - } - return false; - } - - void DeviceTimePointStats::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"firstPoint",firstPoint); - field_to_json(Obj,"lastPoint",lastPoint); - field_to_json(Obj,"count",count); - } - - bool DeviceTimePointStats::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"firstPoint",firstPoint); - field_from_json(Obj,"lastPoint",lastPoint); - field_from_json(Obj,"count",count); - return true; - } catch(...) { - - } - return false; - } - - void WifiClientRate::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"bitrate",bitrate); - field_to_json(Obj,"chwidth",chwidth); - field_to_json(Obj,"mcs",mcs); - field_to_json(Obj,"nss",nss); - field_to_json(Obj,"vht",vht); - } - - bool WifiClientRate::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"bitrate",bitrate); - field_from_json(Obj,"chwidth",chwidth); - field_from_json(Obj,"mcs",mcs); - field_from_json(Obj,"nss",nss); - field_from_json(Obj,"vht",vht); - return true; - } catch(...) { - - } - return false; - } - - void WifiClientHistory::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"timestamp",timestamp); - field_to_json(Obj,"station_id",station_id); - field_to_json(Obj,"bssid",bssid); - field_to_json(Obj,"ssid",ssid); - field_to_json(Obj,"rssi",rssi); - field_to_json(Obj,"rx_bitrate",rx_bitrate); - field_to_json(Obj,"rx_chwidth",rx_chwidth); - field_to_json(Obj,"rx_mcs",rx_mcs); - field_to_json(Obj,"rx_nss",rx_nss); - field_to_json(Obj,"rx_vht",rx_vht); - field_to_json(Obj,"tx_bitrate",tx_bitrate); - field_to_json(Obj,"tx_chwidth",tx_chwidth); - field_to_json(Obj,"tx_mcs",tx_mcs); - field_to_json(Obj,"tx_nss",tx_nss); - field_to_json(Obj,"tx_vht",tx_vht); - field_to_json(Obj,"rx_bytes",rx_bytes); - field_to_json(Obj,"tx_bytes",tx_bytes); - field_to_json(Obj,"rx_duration",rx_duration); - field_to_json(Obj,"tx_duration",tx_duration); - field_to_json(Obj,"rx_packets",rx_packets); - field_to_json(Obj,"tx_packets",tx_packets); - field_to_json(Obj,"ipv4",ipv4); - field_to_json(Obj,"ipv6",ipv6); - field_to_json(Obj,"channel_width",channel_width); - field_to_json(Obj,"noise",noise); - field_to_json(Obj,"tx_power",tx_power); - field_to_json(Obj,"channel",channel); - field_to_json(Obj,"active_ms",active_ms); - field_to_json(Obj,"busy_ms",busy_ms); - field_to_json(Obj,"receive_ms",receive_ms); - field_to_json(Obj,"mode",mode); - field_to_json(Obj,"ack_signal",ack_signal); - field_to_json(Obj,"ack_signal_avg",ack_signal_avg); - field_to_json(Obj,"connected",connected); - field_to_json(Obj,"inactive",inactive); - field_to_json(Obj,"tx_retries",tx_retries); - field_to_json(Obj,"venue_id",venue_id); - } - - bool WifiClientHistory::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"timestamp",timestamp); - field_from_json(Obj,"station_id",station_id); - field_from_json(Obj,"bssid",bssid); - field_from_json(Obj,"ssid",ssid); - field_from_json(Obj,"rssi",rssi); - field_from_json(Obj,"rx_bitrate",rx_bitrate); - field_from_json(Obj,"rx_chwidth",rx_chwidth); - field_from_json(Obj,"rx_mcs",rx_mcs); - field_from_json(Obj,"rx_nss",rx_nss); - field_from_json(Obj,"rx_vht",rx_vht); - field_from_json(Obj,"tx_bitrate",tx_bitrate); - field_from_json(Obj,"tx_chwidth",tx_chwidth); - field_from_json(Obj,"tx_mcs",tx_mcs); - field_from_json(Obj,"tx_nss",tx_nss); - field_from_json(Obj,"tx_vht",tx_vht); - field_from_json(Obj,"rx_bytes",rx_bytes); - field_from_json(Obj,"tx_bytes",tx_bytes); - field_from_json(Obj,"rx_duration",rx_duration); - field_from_json(Obj,"tx_duration",tx_duration); - field_from_json(Obj,"rx_packets",rx_packets); - field_from_json(Obj,"tx_packets",tx_packets); - field_from_json(Obj,"ipv4",ipv4); - field_from_json(Obj,"ipv6",ipv6); - field_from_json(Obj,"channel_width",channel_width); - field_from_json(Obj,"noise",noise); - field_from_json(Obj,"tx_power",tx_power); - field_from_json(Obj,"channel",channel); - field_from_json(Obj,"active_ms",active_ms); - field_from_json(Obj,"busy_ms",busy_ms); - field_from_json(Obj,"receive_ms",receive_ms); - field_from_json(Obj,"mode",mode); - field_from_json(Obj,"ack_signal",ack_signal); - field_from_json(Obj,"ack_signal_avg",ack_signal_avg); - field_from_json(Obj,"connected",connected); - field_from_json(Obj,"inactive",inactive); - field_from_json(Obj,"tx_retries",tx_retries); - field_from_json(Obj,"venue_id",venue_id); - return true; - } catch(...) { - - } - return false; - } -} \ No newline at end of file + bool WifiClientHistory::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "timestamp", timestamp); + field_from_json(Obj, "station_id", station_id); + field_from_json(Obj, "bssid", bssid); + field_from_json(Obj, "ssid", ssid); + field_from_json(Obj, "rssi", rssi); + field_from_json(Obj, "rx_bitrate", rx_bitrate); + field_from_json(Obj, "rx_chwidth", rx_chwidth); + field_from_json(Obj, "rx_mcs", rx_mcs); + field_from_json(Obj, "rx_nss", rx_nss); + field_from_json(Obj, "rx_vht", rx_vht); + field_from_json(Obj, "tx_bitrate", tx_bitrate); + field_from_json(Obj, "tx_chwidth", tx_chwidth); + field_from_json(Obj, "tx_mcs", tx_mcs); + field_from_json(Obj, "tx_nss", tx_nss); + field_from_json(Obj, "tx_vht", tx_vht); + field_from_json(Obj, "rx_bytes", rx_bytes); + field_from_json(Obj, "tx_bytes", tx_bytes); + field_from_json(Obj, "rx_duration", rx_duration); + field_from_json(Obj, "tx_duration", tx_duration); + field_from_json(Obj, "rx_packets", rx_packets); + field_from_json(Obj, "tx_packets", tx_packets); + field_from_json(Obj, "ipv4", ipv4); + field_from_json(Obj, "ipv6", ipv6); + field_from_json(Obj, "channel_width", channel_width); + field_from_json(Obj, "noise", noise); + field_from_json(Obj, "tx_power", tx_power); + field_from_json(Obj, "channel", channel); + field_from_json(Obj, "active_ms", active_ms); + field_from_json(Obj, "busy_ms", busy_ms); + field_from_json(Obj, "receive_ms", receive_ms); + field_from_json(Obj, "mode", mode); + field_from_json(Obj, "ack_signal", ack_signal); + field_from_json(Obj, "ack_signal_avg", ack_signal_avg); + field_from_json(Obj, "connected", connected); + field_from_json(Obj, "inactive", inactive); + field_from_json(Obj, "tx_retries", tx_retries); + field_from_json(Obj, "venue_id", venue_id); + return true; + } catch (...) { + } + return false; + } +} // namespace OpenWifi::AnalyticsObjects \ No newline at end of file diff --git a/src/RESTObjects/RESTAPI_AnalyticsObjects.h b/src/RESTObjects/RESTAPI_AnalyticsObjects.h index d5585f5..dac46c7 100644 --- a/src/RESTObjects/RESTAPI_AnalyticsObjects.h +++ b/src/RESTObjects/RESTAPI_AnalyticsObjects.h @@ -10,414 +10,334 @@ namespace OpenWifi { - namespace AnalyticsObjects { - - struct Report { - uint64_t snapShot = 0; - - void reset(); - - void to_json(Poco::JSON::Object &Obj) const; - }; - - struct VenueInfo { - OpenWifi::Types::UUID_t id; - std::string name; - std::string description; - uint64_t retention = 0; - uint64_t interval = 0; - bool monitorSubVenues = false; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct BoardInfo { - ProvObjects::ObjectInfo info; - std::vector venueList; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - - inline bool operator<(const BoardInfo &bb) const { - return info.id < bb.info.id; - } - - inline bool operator==(const BoardInfo &bb) const { - return info.id == bb.info.id; - } - }; - - struct DeviceInfo { - std::string boardId; - std::string type; - std::string serialNumber; - std::string deviceType; - uint64_t lastContact = 0 ; - uint64_t lastPing = 0; - uint64_t lastState = 0; - std::string lastFirmware; - uint64_t lastFirmwareUpdate = 0; - uint64_t lastConnection = 0; - uint64_t lastDisconnection = 0; - uint64_t pings = 0; - uint64_t states = 0; - bool connected = false; - std::string connectionIp; - uint64_t associations_2g = 0; - uint64_t associations_5g = 0; - uint64_t associations_6g = 0; - uint64_t health = 0; - uint64_t lastHealth = 0; - std::string locale; - uint64_t uptime = 0; - double memory = 0.0; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct DeviceInfoList { - std::vector devices; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - enum wifi_band { - band_2g = 0, band_5g = 1, band_6g = 2 - }; - - struct TIDstat_entry { - uint64_t rx_msdu = 0, - tx_msdu = 0, - tx_msdu_failed = 0, - tx_msdu_retries = 0; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct UE_rate { - uint64_t bitrate=0; - uint64_t mcs=0; - uint64_t nss=0; - bool ht=false; - bool sgi=false; - uint64_t chwidth=0; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct AveragePoint { - double min = 0.0, - max = 0.0, - avg = 0.0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct UETimePoint { - std::string station; - int64_t rssi = 0; - uint64_t tx_bytes = 0, - rx_bytes = 0, - tx_duration = 0, - rx_packets = 0, - tx_packets = 0, - tx_retries = 0, - tx_failed = 0, - connected = 0, - inactive = 0; - - double tx_bytes_bw = 0.0 , - rx_bytes_bw = 0.0 , - tx_packets_bw = 0.0 , - rx_packets_bw = 0.0 , - tx_failed_pct = 0.0 , - tx_retries_pct = 0.0 , - tx_duration_pct = 0.0; - - uint64_t tx_bytes_delta = 0, - rx_bytes_delta = 0, - tx_duration_delta = 0, - rx_packets_delta = 0, - tx_packets_delta = 0, - tx_retries_delta = 0, - tx_failed_delta = 0; - - UE_rate tx_rate, - rx_rate; - std::vector tidstats; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - enum SSID_MODES { - unknown = 0, - ap, - mesh, - sta, - wds_ap, - wds_sta, - wds_repeater - }; - - inline SSID_MODES SSID_Mode(const std::string &m) { - if (m == "ap") - return ap; - if (m == "sta") - return sta; - if (m == "mesh") - return mesh; - if (m == "wds-ap") - return wds_ap; - if (m == "wds-sta") - return wds_sta; - if (m == "wds-repeater") - return wds_repeater; - return unknown; - } - - struct SSIDTimePoint { - std::string bssid, - mode, - ssid; - uint64_t band=0, - channel=0; - std::vector associations; - - AveragePoint tx_bytes_bw, - rx_bytes_bw, - tx_packets_bw, - rx_packets_bw, - tx_failed_pct, - tx_retries_pct, - tx_duration_pct; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - - struct APTimePoint { - uint64_t collisions = 0, - multicast = 0, - rx_bytes = 0, - rx_dropped = 0, - rx_errors = 0, - rx_packets = 0, - tx_bytes = 0, - tx_dropped = 0, - tx_errors = 0, - tx_packets = 0; - - double tx_bytes_bw = 0.0 , - rx_bytes_bw = 0.0 , - rx_dropped_pct = 0.0, - tx_dropped_pct = 0.0, - rx_packets_bw = 0.0, - tx_packets_bw = 0.0, - rx_errors_pct = 0.0 , - tx_errors_pct = 0.0; - - uint64_t tx_bytes_delta = 0, - rx_bytes_delta = 0 , - rx_dropped_delta = 0, - tx_dropped_delta = 0, - rx_packets_delta = 0, - tx_packets_delta = 0, - rx_errors_delta = 0, - tx_errors_delta = 0; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct RadioTimePoint { - uint64_t band = 0, - channel_width = 0; - uint64_t active_ms = 0, - busy_ms = 0, - receive_ms = 0, - transmit_ms = 0, - tx_power = 0, - channel = 0; - int64_t temperature = 0, - noise = 0; - - double active_pct = 0.0 , - busy_pct = 0.0, - receive_pct = 0.0, - transmit_pct = 0.0; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - - struct DeviceTimePoint { - std::string id; - std::string boardId; - uint64_t timestamp = 0; - APTimePoint ap_data; - std::vector ssid_data; - std::vector radio_data; - AnalyticsObjects::DeviceInfo device_info; - std::string serialNumber; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - - inline bool operator<(const DeviceTimePoint &rhs) const { - if(timestamp < rhs.timestamp) - return true; - if(timestamp > rhs.timestamp) - return false; - if(device_info.serialNumber < rhs.device_info.serialNumber) - return true; - return false; - } - - inline bool operator==(const DeviceTimePoint &rhs) const { - return timestamp==rhs.timestamp && device_info.serialNumber==rhs.device_info.serialNumber; - } - - inline bool operator>(const DeviceTimePoint &rhs) const { - if(timestamp > rhs.timestamp) - return true; - if(timestamp < rhs.timestamp) - return false; - if(device_info.serialNumber > rhs.device_info.serialNumber) - return true; - return false; - } - - }; - - struct DeviceTimePointAnalysis { - uint64_t timestamp; - - AveragePoint noise; - AveragePoint temperature; - AveragePoint active_pct; - AveragePoint busy_pct; - AveragePoint receive_pct; - AveragePoint transmit_pct; - AveragePoint tx_power; - - AveragePoint tx_bytes_bw; - AveragePoint rx_bytes_bw; - AveragePoint rx_dropped_pct; - AveragePoint tx_dropped_pct; - AveragePoint rx_packets_bw; - AveragePoint tx_packets_bw; - AveragePoint rx_errors_pct; - AveragePoint tx_errors_pct; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - - }; - - struct DeviceTimePointList { - std::vector points; - std::vector stats; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct BandwidthAnalysisEntry { - uint64_t timestamp = 0; - - }; - - struct BandwidthAnalysis { - - }; - - struct AverageValueSigned { - int64_t peak=0, avg=0, low=0; - }; - - struct AverageValueUnsigned { - uint64_t peak=0, avg=0, low=0; - }; - - struct RadioAnalysis { - uint64_t timestamp=0; - AverageValueSigned noise, temperature; - AverageValueUnsigned active_ms, - busy_ms, - transmit_ms, - receive_ms; - }; - - struct DeviceTimePointStats { - uint64_t firstPoint=0; - uint64_t lastPoint=0; - uint64_t count=0; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct WifiClientRate { - uint32_t bitrate=0; - uint32_t chwidth=0; - uint16_t mcs=0; - uint16_t nss=0; - bool vht=false; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct WifiClientHistory { - uint64_t timestamp=Utils::Now(); - std::string station_id; - std::string bssid; - std::string ssid; - int64_t rssi=0; - uint32_t rx_bitrate=0; - uint32_t rx_chwidth=0; - uint16_t rx_mcs=0; - uint16_t rx_nss=0; - bool rx_vht=false; - uint32_t tx_bitrate=0; - uint32_t tx_chwidth=0; - uint16_t tx_mcs=0; - uint16_t tx_nss=0; - bool tx_vht=false; - uint64_t rx_bytes=0; - uint64_t tx_bytes=0; - uint64_t rx_duration=0; - uint64_t tx_duration=0; - uint64_t rx_packets=0; - uint64_t tx_packets=0; - std::string ipv4; - std::string ipv6; - uint64_t channel_width=0; - int64_t noise=0; - uint64_t tx_power=0; - uint64_t channel=0; - uint64_t active_ms=0; - uint64_t busy_ms=0; - uint64_t receive_ms=0; - std::string mode; - int64_t ack_signal=0; - int64_t ack_signal_avg=0; - uint64_t connected=0; - uint64_t inactive=0; - uint64_t tx_retries=0; - std::string venue_id; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - } - -} \ No newline at end of file + namespace AnalyticsObjects { + + struct Report { + uint64_t snapShot = 0; + + void reset(); + + void to_json(Poco::JSON::Object &Obj) const; + }; + + struct VenueInfo { + OpenWifi::Types::UUID_t id; + std::string name; + std::string description; + uint64_t retention = 0; + uint64_t interval = 0; + bool monitorSubVenues = false; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct BoardInfo { + ProvObjects::ObjectInfo info; + std::vector venueList; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + + inline bool operator<(const BoardInfo &bb) const { return info.id < bb.info.id; } + + inline bool operator==(const BoardInfo &bb) const { return info.id == bb.info.id; } + }; + + struct DeviceInfo { + std::string boardId; + std::string type; + std::string serialNumber; + std::string deviceType; + uint64_t lastContact = 0; + uint64_t lastPing = 0; + uint64_t lastState = 0; + std::string lastFirmware; + uint64_t lastFirmwareUpdate = 0; + uint64_t lastConnection = 0; + uint64_t lastDisconnection = 0; + uint64_t pings = 0; + uint64_t states = 0; + bool connected = false; + std::string connectionIp; + uint64_t associations_2g = 0; + uint64_t associations_5g = 0; + uint64_t associations_6g = 0; + uint64_t health = 0; + uint64_t lastHealth = 0; + std::string locale; + uint64_t uptime = 0; + double memory = 0.0; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct DeviceInfoList { + std::vector devices; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + enum wifi_band { band_2g = 0, band_5g = 1, band_6g = 2 }; + + struct TIDstat_entry { + uint64_t rx_msdu = 0, tx_msdu = 0, tx_msdu_failed = 0, tx_msdu_retries = 0; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct UE_rate { + uint64_t bitrate = 0; + uint64_t mcs = 0; + uint64_t nss = 0; + bool ht = false; + bool sgi = false; + uint64_t chwidth = 0; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct AveragePoint { + double min = 0.0, max = 0.0, avg = 0.0; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct UETimePoint { + std::string station; + int64_t rssi = 0; + uint64_t tx_bytes = 0, rx_bytes = 0, tx_duration = 0, rx_packets = 0, tx_packets = 0, + tx_retries = 0, tx_failed = 0, connected = 0, inactive = 0; + + double tx_bytes_bw = 0.0, rx_bytes_bw = 0.0, tx_packets_bw = 0.0, rx_packets_bw = 0.0, + tx_failed_pct = 0.0, tx_retries_pct = 0.0, tx_duration_pct = 0.0; + + uint64_t tx_bytes_delta = 0, rx_bytes_delta = 0, tx_duration_delta = 0, + rx_packets_delta = 0, tx_packets_delta = 0, tx_retries_delta = 0, + tx_failed_delta = 0; + + UE_rate tx_rate, rx_rate; + std::vector tidstats; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + enum SSID_MODES { unknown = 0, ap, mesh, sta, wds_ap, wds_sta, wds_repeater }; + + inline SSID_MODES SSID_Mode(const std::string &m) { + if (m == "ap") + return ap; + if (m == "sta") + return sta; + if (m == "mesh") + return mesh; + if (m == "wds-ap") + return wds_ap; + if (m == "wds-sta") + return wds_sta; + if (m == "wds-repeater") + return wds_repeater; + return unknown; + } + + struct SSIDTimePoint { + std::string bssid, mode, ssid; + uint64_t band = 0, channel = 0; + std::vector associations; + + AveragePoint tx_bytes_bw, rx_bytes_bw, tx_packets_bw, rx_packets_bw, tx_failed_pct, + tx_retries_pct, tx_duration_pct; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct APTimePoint { + uint64_t collisions = 0, multicast = 0, rx_bytes = 0, rx_dropped = 0, rx_errors = 0, + rx_packets = 0, tx_bytes = 0, tx_dropped = 0, tx_errors = 0, tx_packets = 0; + + double tx_bytes_bw = 0.0, rx_bytes_bw = 0.0, rx_dropped_pct = 0.0, tx_dropped_pct = 0.0, + rx_packets_bw = 0.0, tx_packets_bw = 0.0, rx_errors_pct = 0.0, + tx_errors_pct = 0.0; + + uint64_t tx_bytes_delta = 0, rx_bytes_delta = 0, rx_dropped_delta = 0, + tx_dropped_delta = 0, rx_packets_delta = 0, tx_packets_delta = 0, + rx_errors_delta = 0, tx_errors_delta = 0; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct RadioTimePoint { + uint64_t band = 0, channel_width = 0; + uint64_t active_ms = 0, busy_ms = 0, receive_ms = 0, transmit_ms = 0, tx_power = 0, + channel = 0; + int64_t temperature = 0, noise = 0; + + double active_pct = 0.0, busy_pct = 0.0, receive_pct = 0.0, transmit_pct = 0.0; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct DeviceTimePoint { + std::string id; + std::string boardId; + uint64_t timestamp = 0; + APTimePoint ap_data; + std::vector ssid_data; + std::vector radio_data; + AnalyticsObjects::DeviceInfo device_info; + std::string serialNumber; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + + inline bool operator<(const DeviceTimePoint &rhs) const { + if (timestamp < rhs.timestamp) + return true; + if (timestamp > rhs.timestamp) + return false; + if (device_info.serialNumber < rhs.device_info.serialNumber) + return true; + return false; + } + + inline bool operator==(const DeviceTimePoint &rhs) const { + return timestamp == rhs.timestamp && + device_info.serialNumber == rhs.device_info.serialNumber; + } + + inline bool operator>(const DeviceTimePoint &rhs) const { + if (timestamp > rhs.timestamp) + return true; + if (timestamp < rhs.timestamp) + return false; + if (device_info.serialNumber > rhs.device_info.serialNumber) + return true; + return false; + } + }; + + struct DeviceTimePointAnalysis { + uint64_t timestamp; + + AveragePoint noise; + AveragePoint temperature; + AveragePoint active_pct; + AveragePoint busy_pct; + AveragePoint receive_pct; + AveragePoint transmit_pct; + AveragePoint tx_power; + + AveragePoint tx_bytes_bw; + AveragePoint rx_bytes_bw; + AveragePoint rx_dropped_pct; + AveragePoint tx_dropped_pct; + AveragePoint rx_packets_bw; + AveragePoint tx_packets_bw; + AveragePoint rx_errors_pct; + AveragePoint tx_errors_pct; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct DeviceTimePointList { + std::vector points; + std::vector stats; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct BandwidthAnalysisEntry { + uint64_t timestamp = 0; + }; + + struct BandwidthAnalysis {}; + + struct AverageValueSigned { + int64_t peak = 0, avg = 0, low = 0; + }; + + struct AverageValueUnsigned { + uint64_t peak = 0, avg = 0, low = 0; + }; + + struct RadioAnalysis { + uint64_t timestamp = 0; + AverageValueSigned noise, temperature; + AverageValueUnsigned active_ms, busy_ms, transmit_ms, receive_ms; + }; + + struct DeviceTimePointStats { + uint64_t firstPoint = 0; + uint64_t lastPoint = 0; + uint64_t count = 0; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct WifiClientRate { + uint32_t bitrate = 0; + uint32_t chwidth = 0; + uint16_t mcs = 0; + uint16_t nss = 0; + bool vht = false; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct WifiClientHistory { + uint64_t timestamp = Utils::Now(); + std::string station_id; + std::string bssid; + std::string ssid; + int64_t rssi = 0; + uint32_t rx_bitrate = 0; + uint32_t rx_chwidth = 0; + uint16_t rx_mcs = 0; + uint16_t rx_nss = 0; + bool rx_vht = false; + uint32_t tx_bitrate = 0; + uint32_t tx_chwidth = 0; + uint16_t tx_mcs = 0; + uint16_t tx_nss = 0; + bool tx_vht = false; + uint64_t rx_bytes = 0; + uint64_t tx_bytes = 0; + uint64_t rx_duration = 0; + uint64_t tx_duration = 0; + uint64_t rx_packets = 0; + uint64_t tx_packets = 0; + std::string ipv4; + std::string ipv6; + uint64_t channel_width = 0; + int64_t noise = 0; + uint64_t tx_power = 0; + uint64_t channel = 0; + uint64_t active_ms = 0; + uint64_t busy_ms = 0; + uint64_t receive_ms = 0; + std::string mode; + int64_t ack_signal = 0; + int64_t ack_signal_avg = 0; + uint64_t connected = 0; + uint64_t inactive = 0; + uint64_t tx_retries = 0; + std::string venue_id; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + } // namespace AnalyticsObjects + +} // namespace OpenWifi \ No newline at end of file diff --git a/src/RESTObjects/RESTAPI_CertObjects.cpp b/src/RESTObjects/RESTAPI_CertObjects.cpp index 6b30005..a1a9d6d 100644 --- a/src/RESTObjects/RESTAPI_CertObjects.cpp +++ b/src/RESTObjects/RESTAPI_CertObjects.cpp @@ -5,208 +5,208 @@ #include "RESTAPI_CertObjects.h" #include "framework/RESTAPI_utils.h" -using OpenWifi::RESTAPI_utils::field_to_json; using OpenWifi::RESTAPI_utils::field_from_json; +using OpenWifi::RESTAPI_utils::field_to_json; namespace OpenWifi::CertObjects { - void CertificateEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id", id); - field_to_json(Obj,"entity", entity); - field_to_json(Obj,"creator", creator); - field_to_json(Obj,"type", type); - field_to_json(Obj,"status", status); - field_to_json(Obj,"certificate", certificate); - field_to_json(Obj,"key", key); - field_to_json(Obj,"devid", devid); - field_to_json(Obj,"cas", cas); - field_to_json(Obj,"manufacturer", manufacturer); - field_to_json(Obj,"model", model); - field_to_json(Obj,"redirector", redirector); - field_to_json(Obj,"commonName", commonName); - field_to_json(Obj,"certificateId", certificateId); - field_to_json(Obj,"batch", batch); - field_to_json(Obj,"created", created); - field_to_json(Obj,"modified", modified); - field_to_json(Obj,"revoked", revoked); - field_to_json(Obj,"revokeCount", revokeCount); - field_to_json(Obj,"synched", synched); - field_to_json(Obj,"expiryDate", expiryDate); - } + void CertificateEntry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "creator", creator); + field_to_json(Obj, "type", type); + field_to_json(Obj, "status", status); + field_to_json(Obj, "certificate", certificate); + field_to_json(Obj, "key", key); + field_to_json(Obj, "devid", devid); + field_to_json(Obj, "cas", cas); + field_to_json(Obj, "manufacturer", manufacturer); + field_to_json(Obj, "model", model); + field_to_json(Obj, "redirector", redirector); + field_to_json(Obj, "commonName", commonName); + field_to_json(Obj, "certificateId", certificateId); + field_to_json(Obj, "batch", batch); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + field_to_json(Obj, "revoked", revoked); + field_to_json(Obj, "revokeCount", revokeCount); + field_to_json(Obj, "synched", synched); + field_to_json(Obj, "expiryDate", expiryDate); + } - bool CertificateEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id", id); - field_from_json(Obj,"entity", entity); - field_from_json(Obj,"creator", creator); - field_from_json(Obj,"type", type); - field_from_json(Obj,"status", status); - field_from_json(Obj,"certificate", certificate); - field_from_json(Obj,"key", key); - field_from_json(Obj,"devid", devid); - field_from_json(Obj,"cas", cas); - field_from_json(Obj,"manufacturer", manufacturer); - field_from_json(Obj,"model", model); - field_from_json(Obj,"redirector", redirector); - field_from_json(Obj,"commonName", commonName); - field_from_json(Obj,"certificateId", certificateId); - field_from_json(Obj,"batch", batch); - field_from_json(Obj,"created", created); - field_from_json(Obj,"modified", modified); - field_from_json(Obj,"revoked", revoked); - field_from_json(Obj,"revokeCount", revokeCount); - field_from_json(Obj,"synched", synched); - field_from_json(Obj,"expiryDate", expiryDate); - return true; - } catch (...) { - } - return false; - } + bool CertificateEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "creator", creator); + field_from_json(Obj, "type", type); + field_from_json(Obj, "status", status); + field_from_json(Obj, "certificate", certificate); + field_from_json(Obj, "key", key); + field_from_json(Obj, "devid", devid); + field_from_json(Obj, "cas", cas); + field_from_json(Obj, "manufacturer", manufacturer); + field_from_json(Obj, "model", model); + field_from_json(Obj, "redirector", redirector); + field_from_json(Obj, "commonName", commonName); + field_from_json(Obj, "certificateId", certificateId); + field_from_json(Obj, "batch", batch); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + field_from_json(Obj, "revoked", revoked); + field_from_json(Obj, "revokeCount", revokeCount); + field_from_json(Obj, "synched", synched); + field_from_json(Obj, "expiryDate", expiryDate); + return true; + } catch (...) { + } + return false; + } - void EntityEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id", id); - field_to_json(Obj,"creator", creator); - field_to_json(Obj,"name", name); - field_to_json(Obj,"description", description); - field_to_json(Obj,"defaultRedirector", defaultRedirector); - field_to_json(Obj,"apiKey", apiKey); - field_to_json(Obj,"serverEnrollmentProfile", serverEnrollmentProfile); - field_to_json(Obj,"clientEnrollmentProfile", clientEnrollmentProfile); - field_to_json(Obj,"organization", organization); - field_to_json(Obj,"created", created); - field_to_json(Obj,"modified", modified); - field_to_json(Obj,"suspended", suspended); - field_to_json(Obj,"deleted", deleted); - field_to_json(Obj,"notes", notes); - } + void EntityEntry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "creator", creator); + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "defaultRedirector", defaultRedirector); + field_to_json(Obj, "apiKey", apiKey); + field_to_json(Obj, "serverEnrollmentProfile", serverEnrollmentProfile); + field_to_json(Obj, "clientEnrollmentProfile", clientEnrollmentProfile); + field_to_json(Obj, "organization", organization); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + field_to_json(Obj, "suspended", suspended); + field_to_json(Obj, "deleted", deleted); + field_to_json(Obj, "notes", notes); + } - bool EntityEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id", id); - field_from_json(Obj,"creator", creator); - field_from_json(Obj,"name", name); - field_from_json(Obj,"description", description); - field_from_json(Obj,"defaultRedirector", defaultRedirector); - field_from_json(Obj,"apiKey", apiKey); - field_from_json(Obj,"serverEnrollmentProfile", serverEnrollmentProfile); - field_from_json(Obj,"clientEnrollmentProfile", clientEnrollmentProfile); - field_from_json(Obj,"organization", organization); - field_from_json(Obj,"created", created); - field_from_json(Obj,"modified", modified); - field_from_json(Obj,"suspended", suspended); - field_from_json(Obj,"deleted", deleted); - field_from_json(Obj,"notes", notes); - return true; - } catch (...) { - } - return false; - } + bool EntityEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "creator", creator); + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "defaultRedirector", defaultRedirector); + field_from_json(Obj, "apiKey", apiKey); + field_from_json(Obj, "serverEnrollmentProfile", serverEnrollmentProfile); + field_from_json(Obj, "clientEnrollmentProfile", clientEnrollmentProfile); + field_from_json(Obj, "organization", organization); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + field_from_json(Obj, "suspended", suspended); + field_from_json(Obj, "deleted", deleted); + field_from_json(Obj, "notes", notes); + return true; + } catch (...) { + } + return false; + } - void BatchEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id", id); - field_to_json(Obj,"entity", entity); - field_to_json(Obj,"creator", creator); - field_to_json(Obj,"name", name); - field_to_json(Obj,"description", description); - field_to_json(Obj,"manufacturer", manufacturer); - field_to_json(Obj,"model", model); - field_to_json(Obj,"redirector", redirector); - field_to_json(Obj,"commonNames", commonNames); - field_to_json(Obj,"jobHistory", jobHistory); - field_to_json(Obj,"notes", notes); - field_to_json(Obj,"submitted", submitted); - field_to_json(Obj,"started", started); - field_to_json(Obj,"completed", completed); - field_to_json(Obj,"modified", modified); - } + void BatchEntry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "creator", creator); + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "manufacturer", manufacturer); + field_to_json(Obj, "model", model); + field_to_json(Obj, "redirector", redirector); + field_to_json(Obj, "commonNames", commonNames); + field_to_json(Obj, "jobHistory", jobHistory); + field_to_json(Obj, "notes", notes); + field_to_json(Obj, "submitted", submitted); + field_to_json(Obj, "started", started); + field_to_json(Obj, "completed", completed); + field_to_json(Obj, "modified", modified); + } - bool BatchEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id", id); - field_from_json(Obj,"entity", entity); - field_from_json(Obj,"creator", creator); - field_from_json(Obj,"name", name); - field_from_json(Obj,"description", description); - field_from_json(Obj,"manufacturer", manufacturer); - field_from_json(Obj,"model", model); - field_from_json(Obj,"redirector", redirector); - field_from_json(Obj,"commonNames", commonNames); - field_from_json(Obj,"jobHistory", jobHistory); - field_from_json(Obj,"notes", notes); - field_from_json(Obj,"submitted", submitted); - field_from_json(Obj,"started", started); - field_from_json(Obj,"completed", completed); - field_from_json(Obj,"modified", modified); - return true; - } catch (...) { - } - return false; - } + bool BatchEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "creator", creator); + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "manufacturer", manufacturer); + field_from_json(Obj, "model", model); + field_from_json(Obj, "redirector", redirector); + field_from_json(Obj, "commonNames", commonNames); + field_from_json(Obj, "jobHistory", jobHistory); + field_from_json(Obj, "notes", notes); + field_from_json(Obj, "submitted", submitted); + field_from_json(Obj, "started", started); + field_from_json(Obj, "completed", completed); + field_from_json(Obj, "modified", modified); + return true; + } catch (...) { + } + return false; + } - void JobEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id", id); - field_to_json(Obj,"entity", entity); - field_to_json(Obj,"creator", creator); - field_to_json(Obj,"batch", batch); - field_to_json(Obj,"commonNames", commonNames); - field_to_json(Obj,"completedNames", completedNames); - field_to_json(Obj,"errorNames", errorNames); - field_to_json(Obj,"status", status); - field_to_json(Obj,"command", command); - field_to_json(Obj,"parameters", parameters); - field_to_json(Obj,"submitted", submitted); - field_to_json(Obj,"started", started); - field_to_json(Obj,"completed", completed); - field_to_json(Obj,"requesterUsername", requesterUsername); - } + void JobEntry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "creator", creator); + field_to_json(Obj, "batch", batch); + field_to_json(Obj, "commonNames", commonNames); + field_to_json(Obj, "completedNames", completedNames); + field_to_json(Obj, "errorNames", errorNames); + field_to_json(Obj, "status", status); + field_to_json(Obj, "command", command); + field_to_json(Obj, "parameters", parameters); + field_to_json(Obj, "submitted", submitted); + field_to_json(Obj, "started", started); + field_to_json(Obj, "completed", completed); + field_to_json(Obj, "requesterUsername", requesterUsername); + } - bool JobEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id", id); - field_from_json(Obj,"entity", entity); - field_from_json(Obj,"creator", creator); - field_from_json(Obj,"batch", batch); - field_from_json(Obj,"commonNames", commonNames); - field_from_json(Obj,"completedNames", completedNames); - field_from_json(Obj,"errorNames", errorNames); - field_from_json(Obj,"status", status); - field_from_json(Obj,"command", command); - field_from_json(Obj,"parameters", parameters); - field_from_json(Obj,"submitted", submitted); - field_from_json(Obj,"started", started); - field_from_json(Obj,"completed", completed); - field_from_json(Obj,"requesterUsername", requesterUsername); - return true; - } catch (...) { - } - return false; - } + bool JobEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "creator", creator); + field_from_json(Obj, "batch", batch); + field_from_json(Obj, "commonNames", commonNames); + field_from_json(Obj, "completedNames", completedNames); + field_from_json(Obj, "errorNames", errorNames); + field_from_json(Obj, "status", status); + field_from_json(Obj, "command", command); + field_from_json(Obj, "parameters", parameters); + field_from_json(Obj, "submitted", submitted); + field_from_json(Obj, "started", started); + field_from_json(Obj, "completed", completed); + field_from_json(Obj, "requesterUsername", requesterUsername); + return true; + } catch (...) { + } + return false; + } - void DashBoardYearlyStats::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "year", year); - field_to_json(Obj, "activeCerts", activeCerts); - field_to_json(Obj, "revokedCerts", revokedCerts); - } + void DashBoardYearlyStats::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "year", year); + field_to_json(Obj, "activeCerts", activeCerts); + field_to_json(Obj, "revokedCerts", revokedCerts); + } - void Dashboard::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"snapshot", snapshot); - field_to_json(Obj,"numberOfIssuedCerts", numberOfIssuedCerts); - field_to_json(Obj,"numberOfRevokedCerts", numberOfRevokedCerts); - field_to_json(Obj,"activeCertsPerOrganization", activeCertsPerOrganization); - field_to_json(Obj,"revokedCertsPerOrganization", revokedCertsPerOrganization); - field_to_json(Obj,"numberOfRedirectors", numberOfRedirectors); - field_to_json(Obj,"deviceTypes", deviceTypes); - field_to_json(Obj,"monthlyNumberOfCerts", monthlyNumberOfCerts); - field_to_json(Obj,"monthlyNumberOfCertsPerOrgPerYear", monthlyNumberOfCertsPerOrgPerYear); - } + void Dashboard::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "snapshot", snapshot); + field_to_json(Obj, "numberOfIssuedCerts", numberOfIssuedCerts); + field_to_json(Obj, "numberOfRevokedCerts", numberOfRevokedCerts); + field_to_json(Obj, "activeCertsPerOrganization", activeCertsPerOrganization); + field_to_json(Obj, "revokedCertsPerOrganization", revokedCertsPerOrganization); + field_to_json(Obj, "numberOfRedirectors", numberOfRedirectors); + field_to_json(Obj, "deviceTypes", deviceTypes); + field_to_json(Obj, "monthlyNumberOfCerts", monthlyNumberOfCerts); + field_to_json(Obj, "monthlyNumberOfCertsPerOrgPerYear", monthlyNumberOfCertsPerOrgPerYear); + } - void Dashboard::reset() { - snapshot=0; - numberOfRevokedCerts = numberOfIssuedCerts = 0; - activeCertsPerOrganization.clear(); - revokedCertsPerOrganization.clear(); - numberOfRedirectors.clear(); - deviceTypes.clear(); - monthlyNumberOfCerts.clear(); - monthlyNumberOfCertsPerOrgPerYear.clear(); - } -} \ No newline at end of file + void Dashboard::reset() { + snapshot = 0; + numberOfRevokedCerts = numberOfIssuedCerts = 0; + activeCertsPerOrganization.clear(); + revokedCertsPerOrganization.clear(); + numberOfRedirectors.clear(); + deviceTypes.clear(); + monthlyNumberOfCerts.clear(); + monthlyNumberOfCertsPerOrgPerYear.clear(); + } +} // namespace OpenWifi::CertObjects \ No newline at end of file diff --git a/src/RESTObjects/RESTAPI_CertObjects.h b/src/RESTObjects/RESTAPI_CertObjects.h index 4baeafa..672116b 100644 --- a/src/RESTObjects/RESTAPI_CertObjects.h +++ b/src/RESTObjects/RESTAPI_CertObjects.h @@ -4,121 +4,121 @@ #pragma once -#include -#include "framework/OpenWifiTypes.h" #include "RESTObjects/RESTAPI_SecurityObjects.h" +#include "framework/OpenWifiTypes.h" +#include namespace OpenWifi::CertObjects { - struct CertificateEntry { - OpenWifi::Types::UUID_t id; - OpenWifi::Types::UUID_t entity; - OpenWifi::Types::UUID_t creator; - std::string type; - std::string status; - std::string certificate; - std::string key; - std::string devid; - std::string cas; - std::string manufacturer; - std::string model; - std::string redirector; - std::string commonName; - std::string certificateId; - OpenWifi::Types::UUID_t batch; - uint64_t created = 0; - uint64_t modified = 0; - uint64_t revoked = 0; - uint64_t revokeCount = 0; - uint64_t synched = 0; - uint64_t expiryDate = 0 ; + struct CertificateEntry { + OpenWifi::Types::UUID_t id; + OpenWifi::Types::UUID_t entity; + OpenWifi::Types::UUID_t creator; + std::string type; + std::string status; + std::string certificate; + std::string key; + std::string devid; + std::string cas; + std::string manufacturer; + std::string model; + std::string redirector; + std::string commonName; + std::string certificateId; + OpenWifi::Types::UUID_t batch; + uint64_t created = 0; + uint64_t modified = 0; + uint64_t revoked = 0; + uint64_t revokeCount = 0; + uint64_t synched = 0; + uint64_t expiryDate = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct EntityEntry { - OpenWifi::Types::UUID_t id; - OpenWifi::Types::UUID_t creator; - std::string name; - std::string description; - std::string defaultRedirector; - std::string apiKey; - std::string serverEnrollmentProfile; - std::string clientEnrollmentProfile; - std::string organization; - SecurityObjects::NoteInfoVec notes; - bool suspended=false; - bool deleted=false; - uint64_t created = 0 ; - uint64_t modified = 0 ; + struct EntityEntry { + OpenWifi::Types::UUID_t id; + OpenWifi::Types::UUID_t creator; + std::string name; + std::string description; + std::string defaultRedirector; + std::string apiKey; + std::string serverEnrollmentProfile; + std::string clientEnrollmentProfile; + std::string organization; + SecurityObjects::NoteInfoVec notes; + bool suspended = false; + bool deleted = false; + uint64_t created = 0; + uint64_t modified = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct BatchEntry { - OpenWifi::Types::UUID_t id; - OpenWifi::Types::UUID_t entity; - OpenWifi::Types::UUID_t creator; - std::string name; - std::string description; - std::string manufacturer; - std::string model; - std::string redirector; - std::vector commonNames; - std::vector jobHistory; - SecurityObjects::NoteInfoVec notes; - uint64_t submitted = 0 ; - uint64_t started = 0 ; - uint64_t completed = 0 ; - uint64_t modified = 0 ; + struct BatchEntry { + OpenWifi::Types::UUID_t id; + OpenWifi::Types::UUID_t entity; + OpenWifi::Types::UUID_t creator; + std::string name; + std::string description; + std::string manufacturer; + std::string model; + std::string redirector; + std::vector commonNames; + std::vector jobHistory; + SecurityObjects::NoteInfoVec notes; + uint64_t submitted = 0; + uint64_t started = 0; + uint64_t completed = 0; + uint64_t modified = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct JobEntry { - OpenWifi::Types::UUID_t id; - OpenWifi::Types::UUID_t entity; - OpenWifi::Types::UUID_t creator; - OpenWifi::Types::UUID_t batch; - std::string command; - OpenWifi::Types::StringVec commonNames; - OpenWifi::Types::StringVec completedNames; - OpenWifi::Types::StringVec errorNames; - Types::StringPairVec parameters; - std::string status; - uint64_t submitted=0; - uint64_t started=0; - uint64_t completed=0; - std::string requesterUsername; + struct JobEntry { + OpenWifi::Types::UUID_t id; + OpenWifi::Types::UUID_t entity; + OpenWifi::Types::UUID_t creator; + OpenWifi::Types::UUID_t batch; + std::string command; + OpenWifi::Types::StringVec commonNames; + OpenWifi::Types::StringVec completedNames; + OpenWifi::Types::StringVec errorNames; + Types::StringPairVec parameters; + std::string status; + uint64_t submitted = 0; + uint64_t started = 0; + uint64_t completed = 0; + std::string requesterUsername; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct DashBoardYearlyStats { - uint64_t year=0; - OpenWifi::Types::Counted3DMapSII activeCerts; - OpenWifi::Types::Counted3DMapSII revokedCerts; + struct DashBoardYearlyStats { + uint64_t year = 0; + OpenWifi::Types::Counted3DMapSII activeCerts; + OpenWifi::Types::Counted3DMapSII revokedCerts; - void to_json(Poco::JSON::Object &Obj) const; - }; + void to_json(Poco::JSON::Object &Obj) const; + }; - struct Dashboard { - uint64_t snapshot=0; - uint64_t numberOfIssuedCerts=0; - uint64_t numberOfRevokedCerts=0; - OpenWifi::Types::CountedMap activeCertsPerOrganization; - OpenWifi::Types::CountedMap revokedCertsPerOrganization; - OpenWifi::Types::CountedMap numberOfRedirectors; - OpenWifi::Types::CountedMap deviceTypes; - OpenWifi::Types::CountedMap monthlyNumberOfCerts; - std::vector monthlyNumberOfCertsPerOrgPerYear; + struct Dashboard { + uint64_t snapshot = 0; + uint64_t numberOfIssuedCerts = 0; + uint64_t numberOfRevokedCerts = 0; + OpenWifi::Types::CountedMap activeCertsPerOrganization; + OpenWifi::Types::CountedMap revokedCertsPerOrganization; + OpenWifi::Types::CountedMap numberOfRedirectors; + OpenWifi::Types::CountedMap deviceTypes; + OpenWifi::Types::CountedMap monthlyNumberOfCerts; + std::vector monthlyNumberOfCertsPerOrgPerYear; - void to_json(Poco::JSON::Object &Obj) const; - void reset(); - }; + void to_json(Poco::JSON::Object &Obj) const; + void reset(); + }; -} \ No newline at end of file +} // namespace OpenWifi::CertObjects \ No newline at end of file diff --git a/src/RESTObjects/RESTAPI_FMSObjects.cpp b/src/RESTObjects/RESTAPI_FMSObjects.cpp index 5c42da9..ff46f83 100644 --- a/src/RESTObjects/RESTAPI_FMSObjects.cpp +++ b/src/RESTObjects/RESTAPI_FMSObjects.cpp @@ -6,305 +6,293 @@ #include "framework/RESTAPI_utils.h" #include "framework/utils.h" -using OpenWifi::RESTAPI_utils::field_to_json; using OpenWifi::RESTAPI_utils::field_from_json; +using OpenWifi::RESTAPI_utils::field_to_json; namespace OpenWifi::FMSObjects { - void Firmware::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "id", id); - field_to_json(Obj, "release", release); - field_to_json(Obj, "deviceType", deviceType); - field_to_json(Obj, "description", description); - field_to_json(Obj, "revision", revision); - field_to_json(Obj, "uri", uri); - field_to_json(Obj, "image", image); - field_to_json(Obj, "imageDate", imageDate); - field_to_json(Obj, "size", size); - field_to_json(Obj, "downloadCount", downloadCount); - field_to_json(Obj, "firmwareHash", firmwareHash); - field_to_json(Obj, "owner", owner); - field_to_json(Obj, "location", location); - field_to_json(Obj, "uploader", uploader); - field_to_json(Obj, "digest", digest); - field_to_json(Obj, "latest", latest); - field_to_json(Obj, "notes", notes); - field_to_json(Obj, "created", created); - }; + void Firmware::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "release", release); + field_to_json(Obj, "deviceType", deviceType); + field_to_json(Obj, "description", description); + field_to_json(Obj, "revision", revision); + field_to_json(Obj, "uri", uri); + field_to_json(Obj, "image", image); + field_to_json(Obj, "imageDate", imageDate); + field_to_json(Obj, "size", size); + field_to_json(Obj, "downloadCount", downloadCount); + field_to_json(Obj, "firmwareHash", firmwareHash); + field_to_json(Obj, "owner", owner); + field_to_json(Obj, "location", location); + field_to_json(Obj, "uploader", uploader); + field_to_json(Obj, "digest", digest); + field_to_json(Obj, "latest", latest); + field_to_json(Obj, "notes", notes); + field_to_json(Obj, "created", created); + }; - bool Firmware::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "id", id); - field_from_json(Obj, "release", release); - field_from_json(Obj, "deviceType", deviceType); - field_from_json(Obj, "description", description); - field_from_json(Obj, "revision", revision); - field_from_json(Obj, "uri", uri); - field_from_json(Obj, "image", image); - field_from_json(Obj, "imageDate", imageDate); - field_from_json(Obj, "size", size); - field_from_json(Obj, "downloadCount", downloadCount); - field_from_json(Obj, "firmwareHash", firmwareHash); - field_from_json(Obj, "owner", owner); - field_from_json(Obj, "location", location); - field_from_json(Obj, "uploader", uploader); - field_from_json(Obj, "digest", digest); - field_from_json(Obj, "latest", latest); - field_from_json(Obj, "notes", notes); - field_from_json(Obj, "created", created); - return true; - } catch (...) { + bool Firmware::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "release", release); + field_from_json(Obj, "deviceType", deviceType); + field_from_json(Obj, "description", description); + field_from_json(Obj, "revision", revision); + field_from_json(Obj, "uri", uri); + field_from_json(Obj, "image", image); + field_from_json(Obj, "imageDate", imageDate); + field_from_json(Obj, "size", size); + field_from_json(Obj, "downloadCount", downloadCount); + field_from_json(Obj, "firmwareHash", firmwareHash); + field_from_json(Obj, "owner", owner); + field_from_json(Obj, "location", location); + field_from_json(Obj, "uploader", uploader); + field_from_json(Obj, "digest", digest); + field_from_json(Obj, "latest", latest); + field_from_json(Obj, "notes", notes); + field_from_json(Obj, "created", created); + return true; + } catch (...) { + } + return true; + } - } - return true; - } + void FirmwareList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "firmwares", firmwares); + } - void FirmwareList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"firmwares",firmwares); - } + bool FirmwareList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "firmwares", firmwares); + return true; + } catch (...) { + } + return false; + } - bool FirmwareList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "firmwares", firmwares); - return true; - } catch (...) { + void DeviceType::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "deviceType", deviceType); + field_to_json(Obj, "manufacturer", manufacturer); + field_to_json(Obj, "model", model); + field_to_json(Obj, "policy", policy); + field_to_json(Obj, "notes", notes); + field_to_json(Obj, "lastUpdate", lastUpdate); + field_to_json(Obj, "created", created); + field_to_json(Obj, "id", id); + field_to_json(Obj, "id", id); + field_to_json(Obj, "id", id); + } - } - return false; - } + bool DeviceType::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "deviceType", deviceType); + field_from_json(Obj, "manufacturer", manufacturer); + field_from_json(Obj, "model", model); + field_from_json(Obj, "policy", policy); + field_from_json(Obj, "notes", notes); + field_from_json(Obj, "lastUpdate", lastUpdate); + field_from_json(Obj, "created", created); + field_from_json(Obj, "id", id); + field_from_json(Obj, "id", id); + field_from_json(Obj, "id", id); + return true; + } catch (...) { + } + return false; + } - void DeviceType::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "id", id); - field_to_json(Obj, "deviceType", deviceType); - field_to_json(Obj, "manufacturer", manufacturer); - field_to_json(Obj, "model", model); - field_to_json(Obj, "policy", policy); - field_to_json(Obj, "notes", notes); - field_to_json(Obj, "lastUpdate", lastUpdate); - field_to_json(Obj, "created", created); - field_to_json(Obj, "id", id); - field_to_json(Obj, "id", id); - field_to_json(Obj, "id", id); - } + void DeviceTypeList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "deviceTypes", deviceTypes); + } - bool DeviceType::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "id", id); - field_from_json(Obj, "deviceType", deviceType); - field_from_json(Obj, "manufacturer", manufacturer); - field_from_json(Obj, "model", model); - field_from_json(Obj, "policy", policy); - field_from_json(Obj, "notes", notes); - field_from_json(Obj, "lastUpdate", lastUpdate); - field_from_json(Obj, "created", created); - field_from_json(Obj, "id", id); - field_from_json(Obj, "id", id); - field_from_json(Obj, "id", id); - return true; - } catch (...) { + bool DeviceTypeList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "deviceTypes", deviceTypes); + return true; + } catch (...) { + } + return false; + } - } - return false; - } + void RevisionHistoryEntry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "fromRelease", fromRelease); + field_to_json(Obj, "toRelease", toRelease); + field_to_json(Obj, "commandUUID", commandUUID); + field_to_json(Obj, "revisionId", revisionId); + field_to_json(Obj, "upgraded", upgraded); + } - void DeviceTypeList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"deviceTypes", deviceTypes); - } + bool RevisionHistoryEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "fromRelease", fromRelease); + field_from_json(Obj, "toRelease", toRelease); + field_from_json(Obj, "commandUUID", commandUUID); + field_from_json(Obj, "revisionId", revisionId); + field_from_json(Obj, "upgraded", upgraded); + return true; + } catch (...) { + } + return false; + } - bool DeviceTypeList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"deviceTypes", deviceTypes); - return true; - } catch(...) { + void RevisionHistoryEntryList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "deviceTypes", history); + } - } - return false; - } + bool RevisionHistoryEntryList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "deviceTypes", history); + return true; + } catch (...) { + } + return false; + } - void RevisionHistoryEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "id", id); - field_to_json(Obj, "serialNumber", serialNumber); - field_to_json(Obj, "fromRelease", fromRelease); - field_to_json(Obj, "toRelease", toRelease); - field_to_json(Obj, "commandUUID", commandUUID); - field_to_json(Obj, "revisionId", revisionId); - field_to_json(Obj, "upgraded", upgraded); - } + void FirmwareAgeDetails::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "latestId", latestId); + field_to_json(Obj, "image", image); + field_to_json(Obj, "imageDate", imageDate); + field_to_json(Obj, "revision", revision); + field_to_json(Obj, "uri", uri); + field_to_json(Obj, "age", age); + field_to_json(Obj, "latest", latest); + } - bool RevisionHistoryEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "id", id); - field_from_json(Obj, "serialNumber", serialNumber); - field_from_json(Obj, "fromRelease", fromRelease); - field_from_json(Obj, "toRelease", toRelease); - field_from_json(Obj, "commandUUID", commandUUID); - field_from_json(Obj, "revisionId", revisionId); - field_from_json(Obj, "upgraded", upgraded); - return true; - } catch(...) { + bool FirmwareAgeDetails::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "latestId", latestId); + field_from_json(Obj, "image", image); + field_from_json(Obj, "imageDate", imageDate); + field_from_json(Obj, "revision", revision); + field_from_json(Obj, "uri", uri); + field_from_json(Obj, "age", age); + field_from_json(Obj, "latest", latest); + return true; + } catch (...) { + } + return false; + } - } - return false; - } + void DeviceConnectionInformation::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "revision", revision); + field_to_json(Obj, "deviceType", deviceType); + field_to_json(Obj, "endPoint", endPoint); + field_to_json(Obj, "lastUpdate", lastUpdate); + field_to_json(Obj, "status", status); + } - void RevisionHistoryEntryList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"deviceTypes", history); - } + bool DeviceConnectionInformation::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "revision", revision); + field_from_json(Obj, "deviceType", deviceType); + field_from_json(Obj, "endPoint", endPoint); + field_from_json(Obj, "lastUpdate", lastUpdate); + field_from_json(Obj, "status", status); + return true; + } catch (...) { + } + return false; + } - bool RevisionHistoryEntryList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"deviceTypes", history); - return true; - } catch(...) { + void DeviceReport::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "ouis", OUI_); + field_to_json(Obj, "revisions", Revisions_); + field_to_json(Obj, "deviceTypes", DeviceTypes_); + field_to_json(Obj, "status", Status_); + field_to_json(Obj, "endPoints", EndPoints_); + field_to_json(Obj, "usingLatest", UsingLatest_); + field_to_json(Obj, "unknownFirmwares", UnknownFirmwares_); + field_to_json(Obj, "snapshot", snapshot); + field_to_json(Obj, "numberOfDevices", numberOfDevices); + field_to_json(Obj, "totalSecondsOld", totalSecondsOld_); + } - } - return false; - } + void DeviceReport::reset() { + OUI_.clear(); + Revisions_.clear(); + DeviceTypes_.clear(); + Status_.clear(); + EndPoints_.clear(); + UsingLatest_.clear(); + UnknownFirmwares_.clear(); + totalSecondsOld_.clear(); + numberOfDevices = 0; + snapshot = Utils::Now(); + } - void FirmwareAgeDetails::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"latestId", latestId); - field_to_json(Obj,"image", image); - field_to_json(Obj,"imageDate", imageDate); - field_to_json(Obj,"revision", revision); - field_to_json(Obj,"uri", uri); - field_to_json(Obj,"age", age); - field_to_json(Obj,"latest",latest); - } + bool DeviceReport::from_json([[maybe_unused]] const Poco::JSON::Object::Ptr &Obj) { + try { - bool FirmwareAgeDetails::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"latestId", latestId); - field_from_json(Obj,"image", image); - field_from_json(Obj,"imageDate", imageDate); - field_from_json(Obj,"revision", revision); - field_from_json(Obj,"uri", uri); - field_from_json(Obj,"age", age); - field_from_json(Obj,"latest", latest); - return true; - } catch(...) { + return true; + } catch (...) { + } + return false; + } - } - return false; - } + void DeviceInformation::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "history", history); + field_to_json(Obj, "currentFirmware", currentFirmware); + field_to_json(Obj, "currentFirmwareDate", currentFirmwareDate); + field_to_json(Obj, "latestFirmware", latestFirmware); + field_to_json(Obj, "latestFirmwareDate", latestFirmwareDate); + field_to_json(Obj, "latestFirmwareAvailable", latestFirmwareAvailable); + field_to_json(Obj, "latestFirmwareURI", latestFirmwareURI); + } - void DeviceConnectionInformation::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "serialNumber", serialNumber); - field_to_json(Obj, "revision", revision); - field_to_json(Obj, "deviceType", deviceType); - field_to_json(Obj, "endPoint", endPoint); - field_to_json(Obj, "lastUpdate", lastUpdate); - field_to_json(Obj, "status", status); - } + bool DeviceInformation::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "history", history); + field_from_json(Obj, "currentFirmware", currentFirmware); + field_from_json(Obj, "currentFirmwareDate", currentFirmwareDate); + field_from_json(Obj, "latestFirmware", latestFirmware); + field_from_json(Obj, "latestFirmwareDate", latestFirmwareDate); + field_from_json(Obj, "latestFirmwareAvailable", latestFirmwareAvailable); + field_from_json(Obj, "latestFirmwareURI", latestFirmwareURI); + return true; + } catch (...) { + } + return false; + } - bool DeviceConnectionInformation::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "serialNumber", serialNumber); - field_from_json(Obj, "revision", revision); - field_from_json(Obj, "deviceType", deviceType); - field_from_json(Obj, "endPoint", endPoint); - field_from_json(Obj, "lastUpdate", lastUpdate); - field_from_json(Obj, "status", status); - return true; - } catch(...) { + void DeviceCurrentInfo::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "revision", revision); + field_to_json(Obj, "upgraded", upgraded); + } - } - return false; - } + bool DeviceCurrentInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "revision", revision); + field_from_json(Obj, "upgraded", upgraded); + return true; + } catch (...) { + } + return false; + } - void DeviceReport::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "ouis",OUI_); - field_to_json(Obj, "revisions", Revisions_); - field_to_json(Obj, "deviceTypes", DeviceTypes_); - field_to_json(Obj, "status", Status_); - field_to_json(Obj, "endPoints", EndPoints_); - field_to_json(Obj, "usingLatest", UsingLatest_); - field_to_json(Obj, "unknownFirmwares", UnknownFirmwares_); - field_to_json(Obj,"snapshot",snapshot); - field_to_json(Obj,"numberOfDevices",numberOfDevices); - field_to_json(Obj, "totalSecondsOld", totalSecondsOld_); - } + void DeviceCurrentInfoList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "devices", devices); + } - void DeviceReport::reset() { - OUI_.clear(); - Revisions_.clear(); - DeviceTypes_.clear(); - Status_.clear(); - EndPoints_.clear(); - UsingLatest_.clear(); - UnknownFirmwares_.clear(); - totalSecondsOld_.clear(); - numberOfDevices = 0 ; - snapshot = Utils::Now(); - } + bool DeviceCurrentInfoList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "devices", devices); + return true; + } catch (...) { + } + return false; + } - bool DeviceReport::from_json([[maybe_unused]] const Poco::JSON::Object::Ptr &Obj) { - try { - - return true; - } catch (...) { - - } - return false; - } - - void DeviceInformation::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "serialNumber",serialNumber); - field_to_json(Obj, "history", history); - field_to_json(Obj, "currentFirmware", currentFirmware); - field_to_json(Obj, "currentFirmwareDate", currentFirmwareDate); - field_to_json(Obj, "latestFirmware", latestFirmware); - field_to_json(Obj, "latestFirmwareDate", latestFirmwareDate); - field_to_json(Obj, "latestFirmwareAvailable",latestFirmwareAvailable); - field_to_json(Obj, "latestFirmwareURI",latestFirmwareURI); - } - - bool DeviceInformation::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "serialNumber",serialNumber); - field_from_json(Obj, "history", history); - field_from_json(Obj, "currentFirmware", currentFirmware); - field_from_json(Obj, "currentFirmwareDate", currentFirmwareDate); - field_from_json(Obj, "latestFirmware", latestFirmware); - field_from_json(Obj, "latestFirmwareDate", latestFirmwareDate); - field_from_json(Obj, "latestFirmwareAvailable",latestFirmwareAvailable); - field_from_json(Obj, "latestFirmwareURI",latestFirmwareURI); - return true; - } catch(...) { - - } - return false; - } - - void DeviceCurrentInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "serialNumber",serialNumber); - field_to_json(Obj, "revision", revision); - field_to_json(Obj, "upgraded", upgraded); - } - - bool DeviceCurrentInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "serialNumber",serialNumber); - field_from_json(Obj, "revision", revision); - field_from_json(Obj, "upgraded", upgraded); - return true; - } catch(...) { - - } - return false; - } - - void DeviceCurrentInfoList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "devices",devices); - } - - bool DeviceCurrentInfoList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "devices",devices); - return true; - } catch(...) { - - } - return false; - } - -} +} // namespace OpenWifi::FMSObjects diff --git a/src/RESTObjects/RESTAPI_FMSObjects.h b/src/RESTObjects/RESTAPI_FMSObjects.h index 60aea5f..705ccc8 100644 --- a/src/RESTObjects/RESTAPI_FMSObjects.h +++ b/src/RESTObjects/RESTAPI_FMSObjects.h @@ -11,149 +11,149 @@ namespace OpenWifi::FMSObjects { - struct Firmware { - std::string id; - std::string release; - std::string deviceType; - std::string description; - std::string revision; - std::string uri; - std::string image; - uint64_t imageDate=0; - uint64_t size=0; - uint64_t downloadCount=0; - std::string firmwareHash; - std::string owner; - std::string location; - std::string uploader; - std::string digest; - bool latest=false; - SecurityObjects::NoteInfoVec notes; - uint64_t created=0; + struct Firmware { + std::string id; + std::string release; + std::string deviceType; + std::string description; + std::string revision; + std::string uri; + std::string image; + uint64_t imageDate = 0; + uint64_t size = 0; + uint64_t downloadCount = 0; + std::string firmwareHash; + std::string owner; + std::string location; + std::string uploader; + std::string digest; + bool latest = false; + SecurityObjects::NoteInfoVec notes; + uint64_t created = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector FirmwareVec; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector FirmwareVec; - struct FirmwareList { - FirmwareVec firmwares; + struct FirmwareList { + FirmwareVec firmwares; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct DeviceType { - std::string id; - std::string deviceType; - std::string manufacturer; - std::string model; - std::string policy; - SecurityObjects::NoteInfoVec notes; - uint64_t lastUpdate=0; - uint64_t created=0; + struct DeviceType { + std::string id; + std::string deviceType; + std::string manufacturer; + std::string model; + std::string policy; + SecurityObjects::NoteInfoVec notes; + uint64_t lastUpdate = 0; + uint64_t created = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector DeviceTypeVec; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector DeviceTypeVec; - struct DeviceTypeList { - DeviceTypeVec deviceTypes; + struct DeviceTypeList { + DeviceTypeVec deviceTypes; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct RevisionHistoryEntry { - std::string id; - std::string serialNumber; - std::string fromRelease; - std::string toRelease; - std::string commandUUID; - std::string revisionId; - uint64_t upgraded; + struct RevisionHistoryEntry { + std::string id; + std::string serialNumber; + std::string fromRelease; + std::string toRelease; + std::string commandUUID; + std::string revisionId; + uint64_t upgraded; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector RevisionHistoryEntryVec; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector RevisionHistoryEntryVec; - struct RevisionHistoryEntryList { - RevisionHistoryEntryVec history; + struct RevisionHistoryEntryList { + RevisionHistoryEntryVec history; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct FirmwareAgeDetails { - std::string latestId; - std::string image; - uint64_t imageDate; - std::string revision; - std::string uri; - uint64_t age=0; - bool latest=true; + struct FirmwareAgeDetails { + std::string latestId; + std::string image; + uint64_t imageDate; + std::string revision; + std::string uri; + uint64_t age = 0; + bool latest = true; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct DeviceConnectionInformation { - std::string serialNumber; - std::string revision; - std::string deviceType; - std::string endPoint; - uint64_t lastUpdate; - std::string status; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct DeviceConnectionInformation { + std::string serialNumber; + std::string revision; + std::string deviceType; + std::string endPoint; + uint64_t lastUpdate; + std::string status; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct DeviceReport { - uint64_t snapshot=0; - uint64_t numberOfDevices=0; - Types::CountedMap OUI_; - Types::CountedMap Revisions_; - Types::CountedMap DeviceTypes_; - Types::CountedMap Status_; - Types::CountedMap EndPoints_; - Types::CountedMap UsingLatest_; - Types::CountedMap UnknownFirmwares_; - Types::CountedMap totalSecondsOld_; - void to_json(Poco::JSON::Object &Obj) const; - void reset(); - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct DeviceReport { + uint64_t snapshot = 0; + uint64_t numberOfDevices = 0; + Types::CountedMap OUI_; + Types::CountedMap Revisions_; + Types::CountedMap DeviceTypes_; + Types::CountedMap Status_; + Types::CountedMap EndPoints_; + Types::CountedMap UsingLatest_; + Types::CountedMap UnknownFirmwares_; + Types::CountedMap totalSecondsOld_; + void to_json(Poco::JSON::Object &Obj) const; + void reset(); + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct DeviceInformation { - std::string serialNumber; - RevisionHistoryEntryList history; - std::string currentFirmware; - uint64_t currentFirmwareDate=0; - std::string latestFirmware; - uint64_t latestFirmwareDate=0; - bool latestFirmwareAvailable; - std::string latestFirmwareURI; + struct DeviceInformation { + std::string serialNumber; + RevisionHistoryEntryList history; + std::string currentFirmware; + uint64_t currentFirmwareDate = 0; + std::string latestFirmware; + uint64_t latestFirmwareDate = 0; + bool latestFirmwareAvailable; + std::string latestFirmwareURI; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct DeviceCurrentInfo { - std::string serialNumber; - std::string revision; - uint64_t upgraded=0; + struct DeviceCurrentInfo { + std::string serialNumber; + std::string revision; + uint64_t upgraded = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct DeviceCurrentInfoList { - std::vector devices; + struct DeviceCurrentInfoList { + std::vector devices; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; -} +} // namespace OpenWifi::FMSObjects diff --git a/src/RESTObjects/RESTAPI_GWobjects.cpp b/src/RESTObjects/RESTAPI_GWobjects.cpp index 228dd0f..57ab524 100644 --- a/src/RESTObjects/RESTAPI_GWobjects.cpp +++ b/src/RESTObjects/RESTAPI_GWobjects.cpp @@ -10,7 +10,7 @@ #include "Poco/JSON/Stringifier.h" #include "Daemon.h" -#ifdef TIP_GATEWAY_SERVICE +#ifdef TIP_GATEWAY_SERVICE #include "AP_WS_Server.h" #include "CapabilitiesCache.h" #endif @@ -19,41 +19,41 @@ #include "framework/RESTAPI_utils.h" #include "framework/utils.h" -using OpenWifi::RESTAPI_utils::field_to_json; -using OpenWifi::RESTAPI_utils::field_from_json; using OpenWifi::RESTAPI_utils::EmbedDocument; +using OpenWifi::RESTAPI_utils::field_from_json; +using OpenWifi::RESTAPI_utils::field_to_json; namespace OpenWifi::GWObjects { void Device::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"serialNumber", SerialNumber); + field_to_json(Obj, "serialNumber", SerialNumber); #ifdef TIP_GATEWAY_SERVICE - field_to_json(Obj,"deviceType", CapabilitiesCache::instance()->GetPlatform(Compatible)); + field_to_json(Obj, "deviceType", CapabilitiesCache::instance()->GetPlatform(Compatible)); #endif - field_to_json(Obj,"macAddress", MACAddress); - field_to_json(Obj,"manufacturer", Manufacturer); - field_to_json(Obj,"UUID", UUID); + field_to_json(Obj, "macAddress", MACAddress); + field_to_json(Obj, "manufacturer", Manufacturer); + field_to_json(Obj, "UUID", UUID); EmbedDocument("configuration", Obj, Configuration); - field_to_json(Obj,"notes", Notes); - field_to_json(Obj,"createdTimestamp", CreationTimestamp); - field_to_json(Obj,"lastConfigurationChange", LastConfigurationChange); - field_to_json(Obj,"lastConfigurationDownload", LastConfigurationDownload); - field_to_json(Obj,"lastFWUpdate", LastFWUpdate); - field_to_json(Obj,"owner", Owner); - field_to_json(Obj,"location", Location); - field_to_json(Obj,"venue", Venue); - field_to_json(Obj,"firmware", Firmware); - field_to_json(Obj,"compatible", Compatible); - field_to_json(Obj,"fwUpdatePolicy", FWUpdatePolicy); - field_to_json(Obj,"devicePassword", DevicePassword); - field_to_json(Obj,"subscriber", subscriber); - field_to_json(Obj,"entity", entity); - field_to_json(Obj,"modified", modified); - field_to_json(Obj,"locale", locale); - field_to_json(Obj,"restrictedDevice", restrictedDevice); - field_to_json(Obj,"pendingConfiguration", pendingConfiguration); - field_to_json(Obj,"pendingConfigurationCmd", pendingConfigurationCmd); - field_to_json(Obj,"restrictionDetails", restrictionDetails); + field_to_json(Obj, "notes", Notes); + field_to_json(Obj, "createdTimestamp", CreationTimestamp); + field_to_json(Obj, "lastConfigurationChange", LastConfigurationChange); + field_to_json(Obj, "lastConfigurationDownload", LastConfigurationDownload); + field_to_json(Obj, "lastFWUpdate", LastFWUpdate); + field_to_json(Obj, "owner", Owner); + field_to_json(Obj, "location", Location); + field_to_json(Obj, "venue", Venue); + field_to_json(Obj, "firmware", Firmware); + field_to_json(Obj, "compatible", Compatible); + field_to_json(Obj, "fwUpdatePolicy", FWUpdatePolicy); + field_to_json(Obj, "devicePassword", DevicePassword); + field_to_json(Obj, "subscriber", subscriber); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "modified", modified); + field_to_json(Obj, "locale", locale); + field_to_json(Obj, "restrictedDevice", restrictedDevice); + field_to_json(Obj, "pendingConfiguration", pendingConfiguration); + field_to_json(Obj, "pendingConfigurationCmd", pendingConfigurationCmd); + field_to_json(Obj, "restrictionDetails", restrictionDetails); } void Device::to_json_with_status(Poco::JSON::Object &Obj) const { @@ -65,39 +65,39 @@ namespace OpenWifi::GWObjects { if (AP_WS_Server()->GetState(SerialNumber, ConState)) { ConState.to_json(Obj); } else { - field_to_json(Obj,"ipAddress", ""); - field_to_json(Obj,"txBytes", (uint64_t) 0); - field_to_json(Obj,"rxBytes", (uint64_t )0); - field_to_json(Obj,"messageCount", (uint64_t )0); - field_to_json(Obj,"connected", false); - field_to_json(Obj,"lastContact", ""); - field_to_json(Obj,"verifiedCertificate", "NO_CERTIFICATE"); - field_to_json(Obj,"associations_2G", (uint64_t) 0); - field_to_json(Obj,"associations_5G", (uint64_t) 0); - field_to_json(Obj,"associations_6G", (uint64_t) 0); + field_to_json(Obj, "ipAddress", ""); + field_to_json(Obj, "txBytes", (uint64_t)0); + field_to_json(Obj, "rxBytes", (uint64_t)0); + field_to_json(Obj, "messageCount", (uint64_t)0); + field_to_json(Obj, "connected", false); + field_to_json(Obj, "lastContact", ""); + field_to_json(Obj, "verifiedCertificate", "NO_CERTIFICATE"); + field_to_json(Obj, "associations_2G", (uint64_t)0); + field_to_json(Obj, "associations_5G", (uint64_t)0); + field_to_json(Obj, "associations_6G", (uint64_t)0); } #endif } bool Device::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"serialNumber",SerialNumber); - field_from_json(Obj,"deviceType",DeviceType); - field_from_json(Obj,"macAddress",MACAddress); - field_from_json(Obj,"configuration",Configuration); - field_from_json(Obj,"notes",Notes); - field_from_json(Obj,"manufacturer",Manufacturer); - field_from_json(Obj,"owner",Owner); - field_from_json(Obj,"location",Location); - field_from_json(Obj,"venue",Venue); - field_from_json(Obj,"compatible",Compatible); - field_from_json(Obj,"subscriber", subscriber); - field_from_json(Obj,"entity", entity); - field_from_json(Obj,"locale", locale); - field_from_json(Obj,"restrictedDevice", restrictedDevice); - field_from_json(Obj,"pendingConfiguration", pendingConfiguration); - field_from_json(Obj,"pendingConfigurationCmd", pendingConfigurationCmd); - field_from_json(Obj,"restrictionDetails", restrictionDetails); + field_from_json(Obj, "serialNumber", SerialNumber); + field_from_json(Obj, "deviceType", DeviceType); + field_from_json(Obj, "macAddress", MACAddress); + field_from_json(Obj, "configuration", Configuration); + field_from_json(Obj, "notes", Notes); + field_from_json(Obj, "manufacturer", Manufacturer); + field_from_json(Obj, "owner", Owner); + field_from_json(Obj, "location", Location); + field_from_json(Obj, "venue", Venue); + field_from_json(Obj, "compatible", Compatible); + field_from_json(Obj, "subscriber", subscriber); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "locale", locale); + field_from_json(Obj, "restrictedDevice", restrictedDevice); + field_from_json(Obj, "pendingConfiguration", pendingConfiguration); + field_from_json(Obj, "pendingConfigurationCmd", pendingConfigurationCmd); + field_from_json(Obj, "restrictionDetails", restrictionDetails); return true; } catch (const Poco::Exception &E) { } @@ -105,73 +105,74 @@ namespace OpenWifi::GWObjects { } void Device::Print() const { - std::cout << "Device: " << SerialNumber << " DeviceType:" << DeviceType << " MACAddress:" << MACAddress << " Manufacturer:" - << Manufacturer << " " << Configuration << std::endl; + std::cout << "Device: " << SerialNumber << " DeviceType:" << DeviceType + << " MACAddress:" << MACAddress << " Manufacturer:" << Manufacturer << " " + << Configuration << std::endl; } void Statistics::to_json(Poco::JSON::Object &Obj) const { EmbedDocument("data", Obj, Data); - field_to_json(Obj,"UUID", UUID); - field_to_json(Obj,"recorded", Recorded); + field_to_json(Obj, "UUID", UUID); + field_to_json(Obj, "recorded", Recorded); } void Capabilities::to_json(Poco::JSON::Object &Obj) const { EmbedDocument("capabilities", Obj, Capabilities); - field_to_json(Obj,"firstUpdate", FirstUpdate); - field_to_json(Obj,"lastUpdate", LastUpdate); + field_to_json(Obj, "firstUpdate", FirstUpdate); + field_to_json(Obj, "lastUpdate", LastUpdate); } void DeviceLog::to_json(Poco::JSON::Object &Obj) const { EmbedDocument("data", Obj, Data); - field_to_json(Obj,"log", Log); - field_to_json(Obj,"severity", Severity); - field_to_json(Obj,"recorded", Recorded); - field_to_json(Obj,"logType", LogType); - field_to_json(Obj,"UUID", UUID); + field_to_json(Obj, "log", Log); + field_to_json(Obj, "severity", Severity); + field_to_json(Obj, "recorded", Recorded); + field_to_json(Obj, "logType", LogType); + field_to_json(Obj, "UUID", UUID); } void HealthCheck::to_json(Poco::JSON::Object &Obj) const { EmbedDocument("values", Obj, Data); - field_to_json(Obj,"UUID", UUID); - field_to_json(Obj,"sanity", Sanity); - field_to_json(Obj,"recorded", Recorded); + field_to_json(Obj, "UUID", UUID); + field_to_json(Obj, "sanity", Sanity); + field_to_json(Obj, "recorded", Recorded); } void DefaultConfiguration::to_json(Poco::JSON::Object &Obj) const { EmbedDocument("configuration", Obj, Configuration); - field_to_json(Obj,"name", Name); - field_to_json(Obj,"modelIds", Models); - field_to_json(Obj,"description", Description); - field_to_json(Obj,"created", Created); - field_to_json(Obj,"lastModified", LastModified); + field_to_json(Obj, "name", Name); + field_to_json(Obj, "modelIds", Models); + field_to_json(Obj, "description", Description); + field_to_json(Obj, "created", Created); + field_to_json(Obj, "lastModified", LastModified); } void CommandDetails::to_json(Poco::JSON::Object &Obj) const { EmbedDocument("details", Obj, Details); EmbedDocument("results", Obj, Results); - field_to_json(Obj,"UUID", UUID); - field_to_json(Obj,"serialNumber", SerialNumber); - field_to_json(Obj,"command", Command); - field_to_json(Obj,"errorText", ErrorText); - field_to_json(Obj,"submittedBy", SubmittedBy); - field_to_json(Obj,"status", Status); - field_to_json(Obj,"submitted", Submitted); - field_to_json(Obj,"executed", Executed); - field_to_json(Obj,"completed", Completed); - field_to_json(Obj,"when", RunAt); - field_to_json(Obj,"errorCode", ErrorCode); - field_to_json(Obj,"custom", Custom); - field_to_json(Obj,"waitingForFile", WaitingForFile); - field_to_json(Obj,"attachFile", AttachDate); - field_to_json(Obj,"executionTime", executionTime); + field_to_json(Obj, "UUID", UUID); + field_to_json(Obj, "serialNumber", SerialNumber); + field_to_json(Obj, "command", Command); + field_to_json(Obj, "errorText", ErrorText); + field_to_json(Obj, "submittedBy", SubmittedBy); + field_to_json(Obj, "status", Status); + field_to_json(Obj, "submitted", Submitted); + field_to_json(Obj, "executed", Executed); + field_to_json(Obj, "completed", Completed); + field_to_json(Obj, "when", RunAt); + field_to_json(Obj, "errorCode", ErrorCode); + field_to_json(Obj, "custom", Custom); + field_to_json(Obj, "waitingForFile", WaitingForFile); + field_to_json(Obj, "attachFile", AttachDate); + field_to_json(Obj, "executionTime", executionTime); } bool DefaultConfiguration::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"name",Name); - field_from_json(Obj,"configuration",Configuration); - field_from_json(Obj,"modelIds",Models); - field_from_json(Obj,"description",Description); + field_from_json(Obj, "name", Name); + field_from_json(Obj, "configuration", Configuration); + field_from_json(Obj, "modelIds", Models); + field_from_json(Obj, "description", Description); return true; } catch (const Poco::Exception &E) { } @@ -179,18 +180,18 @@ namespace OpenWifi::GWObjects { } void BlackListedDevice::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"serialNumber", serialNumber); - field_to_json(Obj,"author", author); - field_to_json(Obj,"reason", reason); - field_to_json(Obj,"created", created); + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "author", author); + field_to_json(Obj, "reason", reason); + field_to_json(Obj, "created", created); } bool BlackListedDevice::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"serialNumber",serialNumber); - field_from_json(Obj,"author",author); - field_from_json(Obj,"reason",reason); - field_from_json(Obj,"created",created); + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "author", author); + field_from_json(Obj, "reason", reason); + field_from_json(Obj, "created", created); return true; } catch (const Poco::Exception &E) { } @@ -198,53 +199,58 @@ namespace OpenWifi::GWObjects { } void ConnectionState::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"ipAddress", Address); - field_to_json(Obj,"txBytes", TX); - field_to_json(Obj,"rxBytes", RX); - field_to_json(Obj,"messageCount", MessageCount); - field_to_json(Obj,"UUID", UUID); - field_to_json(Obj,"connected", Connected); - field_to_json(Obj,"firmware", Firmware); - field_to_json(Obj,"lastContact", LastContact); - field_to_json(Obj,"associations_2G", Associations_2G); - field_to_json(Obj,"associations_5G", Associations_5G); - field_to_json(Obj,"associations_6G", Associations_6G); - field_to_json(Obj,"webSocketClients", webSocketClients); - field_to_json(Obj,"websocketPackets", websocketPackets); - field_to_json(Obj,"kafkaClients", kafkaClients); - field_to_json(Obj,"kafkaPackets", kafkaPackets); - field_to_json(Obj,"locale", locale); - field_to_json(Obj,"started", started); - field_to_json(Obj,"sessionId", sessionId); - field_to_json(Obj,"connectionCompletionTime", connectionCompletionTime); - field_to_json(Obj,"totalConnectionTime", Utils::Now() - started); - field_to_json(Obj,"certificateExpiryDate", certificateExpiryDate); + field_to_json(Obj, "ipAddress", Address); + field_to_json(Obj, "txBytes", TX); + field_to_json(Obj, "rxBytes", RX); + field_to_json(Obj, "messageCount", MessageCount); + field_to_json(Obj, "UUID", UUID); + field_to_json(Obj, "connected", Connected); + field_to_json(Obj, "firmware", Firmware); + field_to_json(Obj, "lastContact", LastContact); + field_to_json(Obj, "associations_2G", Associations_2G); + field_to_json(Obj, "associations_5G", Associations_5G); + field_to_json(Obj, "associations_6G", Associations_6G); + field_to_json(Obj, "webSocketClients", webSocketClients); + field_to_json(Obj, "websocketPackets", websocketPackets); + field_to_json(Obj, "kafkaClients", kafkaClients); + field_to_json(Obj, "kafkaPackets", kafkaPackets); + field_to_json(Obj, "locale", locale); + field_to_json(Obj, "started", started); + field_to_json(Obj, "sessionId", sessionId); + field_to_json(Obj, "connectionCompletionTime", connectionCompletionTime); + field_to_json(Obj, "totalConnectionTime", Utils::Now() - started); + field_to_json(Obj, "certificateExpiryDate", certificateExpiryDate); - switch(VerifiedCertificate) { - case NO_CERTIFICATE: - field_to_json(Obj,"verifiedCertificate", "NO_CERTIFICATE"); break; - case VALID_CERTIFICATE: - field_to_json(Obj,"verifiedCertificate", "VALID_CERTIFICATE"); break; - case MISMATCH_SERIAL: - field_to_json(Obj,"verifiedCertificate", "MISMATCH_SERIAL"); break; - case VERIFIED: - field_to_json(Obj,"verifiedCertificate", "VERIFIED"); break; - default: - field_to_json(Obj,"verifiedCertificate", "NO_CERTIFICATE"); break; + switch (VerifiedCertificate) { + case NO_CERTIFICATE: + field_to_json(Obj, "verifiedCertificate", "NO_CERTIFICATE"); + break; + case VALID_CERTIFICATE: + field_to_json(Obj, "verifiedCertificate", "VALID_CERTIFICATE"); + break; + case MISMATCH_SERIAL: + field_to_json(Obj, "verifiedCertificate", "MISMATCH_SERIAL"); + break; + case VERIFIED: + field_to_json(Obj, "verifiedCertificate", "VERIFIED"); + break; + default: + field_to_json(Obj, "verifiedCertificate", "NO_CERTIFICATE"); + break; } } void DeviceConnectionStatistics::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"averageConnectionTime", averageConnectionTime); - field_to_json(Obj,"connectedDevices", connectedDevices ); - field_to_json(Obj,"connectingDevices", connectingDevices ); + field_to_json(Obj, "averageConnectionTime", averageConnectionTime); + field_to_json(Obj, "connectedDevices", connectedDevices); + field_to_json(Obj, "connectingDevices", connectingDevices); } bool DeviceConnectionStatistics::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"averageConnectionTime", averageConnectionTime); - field_from_json(Obj,"connectedDevices", connectedDevices ); - field_from_json(Obj,"connectingDevices", connectingDevices ); + field_from_json(Obj, "averageConnectionTime", averageConnectionTime); + field_from_json(Obj, "connectedDevices", connectedDevices); + field_from_json(Obj, "connectingDevices", connectingDevices); return true; } catch (const Poco::Exception &E) { } @@ -252,37 +258,37 @@ namespace OpenWifi::GWObjects { } void RttySessionDetails::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"serialNumber", SerialNumber); - field_to_json(Obj,"server", Server); - field_to_json(Obj,"port", Port); - field_to_json(Obj,"token",Token); - field_to_json(Obj,"timeout", TimeOut); - field_to_json(Obj,"connectionId",ConnectionId); - field_to_json(Obj,"commandUUID",CommandUUID); - field_to_json(Obj,"started", Started); - field_to_json(Obj,"viewport",ViewPort); - field_to_json(Obj,"password",DevicePassword); + field_to_json(Obj, "serialNumber", SerialNumber); + field_to_json(Obj, "server", Server); + field_to_json(Obj, "port", Port); + field_to_json(Obj, "token", Token); + field_to_json(Obj, "timeout", TimeOut); + field_to_json(Obj, "connectionId", ConnectionId); + field_to_json(Obj, "commandUUID", CommandUUID); + field_to_json(Obj, "started", Started); + field_to_json(Obj, "viewport", ViewPort); + field_to_json(Obj, "password", DevicePassword); } void Dashboard::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"commands",commands); - field_to_json(Obj,"upTimes",upTimes); - field_to_json(Obj,"memoryUsed",memoryUsed); - field_to_json(Obj,"load1",load1); - field_to_json(Obj,"load5",load5); - field_to_json(Obj,"load15",load15); - field_to_json(Obj,"vendors",vendors); - field_to_json(Obj,"status",status); - field_to_json(Obj,"deviceType",deviceType); - field_to_json(Obj,"healths",healths); - field_to_json(Obj,"certificates",certificates); - field_to_json(Obj,"lastContact",lastContact); - field_to_json(Obj,"associations",associations); - field_to_json(Obj,"snapshot",snapshot); - field_to_json(Obj,"numberOfDevices",numberOfDevices); + field_to_json(Obj, "commands", commands); + field_to_json(Obj, "upTimes", upTimes); + field_to_json(Obj, "memoryUsed", memoryUsed); + field_to_json(Obj, "load1", load1); + field_to_json(Obj, "load5", load5); + field_to_json(Obj, "load15", load15); + field_to_json(Obj, "vendors", vendors); + field_to_json(Obj, "status", status); + field_to_json(Obj, "deviceType", deviceType); + field_to_json(Obj, "healths", healths); + field_to_json(Obj, "certificates", certificates); + field_to_json(Obj, "lastContact", lastContact); + field_to_json(Obj, "associations", associations); + field_to_json(Obj, "snapshot", snapshot); + field_to_json(Obj, "numberOfDevices", numberOfDevices); } - void Dashboard::reset() { + void Dashboard::reset() { commands.clear(); upTimes.clear(); memoryUsed.clear(); @@ -296,38 +302,38 @@ namespace OpenWifi::GWObjects { certificates.clear(); lastContact.clear(); associations.clear(); - numberOfDevices = 0 ; + numberOfDevices = 0; snapshot = Utils::Now(); } - void CapabilitiesModel::to_json(Poco::JSON::Object &Obj) const{ - field_to_json(Obj,"deviceType", deviceType); - field_to_json(Obj,"capabilities", capabilities); + void CapabilitiesModel::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "deviceType", deviceType); + field_to_json(Obj, "capabilities", capabilities); }; void ScriptRequest::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"serialNumber",serialNumber); - field_to_json(Obj,"timeout",timeout); - field_to_json(Obj,"type",type); - field_to_json(Obj,"scriptId",scriptId); - field_to_json(Obj,"script",script); - field_to_json(Obj,"when",when); - field_to_json(Obj,"signature", signature); - field_to_json(Obj,"deferred", deferred); - field_to_json(Obj,"uri", uri); + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "timeout", timeout); + field_to_json(Obj, "type", type); + field_to_json(Obj, "scriptId", scriptId); + field_to_json(Obj, "script", script); + field_to_json(Obj, "when", when); + field_to_json(Obj, "signature", signature); + field_to_json(Obj, "deferred", deferred); + field_to_json(Obj, "uri", uri); } bool ScriptRequest::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"serialNumber",serialNumber); - field_from_json(Obj,"timeout",timeout); - field_from_json(Obj,"type",type); - field_from_json(Obj,"script",script); - field_from_json(Obj,"scriptId",scriptId); - field_from_json(Obj,"when",when); - field_from_json(Obj,"signature", signature); - field_from_json(Obj,"deferred", deferred); - field_from_json(Obj,"uri", uri); + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "timeout", timeout); + field_from_json(Obj, "type", type); + field_from_json(Obj, "script", script); + field_from_json(Obj, "scriptId", scriptId); + field_from_json(Obj, "when", when); + field_from_json(Obj, "signature", signature); + field_from_json(Obj, "deferred", deferred); + field_from_json(Obj, "uri", uri); return true; } catch (const Poco::Exception &E) { } @@ -335,12 +341,12 @@ namespace OpenWifi::GWObjects { } void RadiusProxyPoolList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"pools",pools); + field_to_json(Obj, "pools", pools); } bool RadiusProxyPoolList::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"pools",pools); + field_from_json(Obj, "pools", pools); return true; } catch (const Poco::Exception &E) { } @@ -348,22 +354,22 @@ namespace OpenWifi::GWObjects { } void RadiusProxyPool::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"name",name); - field_to_json(Obj,"description",description); - field_to_json(Obj,"authConfig",authConfig); - field_to_json(Obj,"acctConfig",acctConfig); - field_to_json(Obj,"coaConfig",coaConfig); - field_to_json(Obj,"useByDefault",useByDefault); + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "authConfig", authConfig); + field_to_json(Obj, "acctConfig", acctConfig); + field_to_json(Obj, "coaConfig", coaConfig); + field_to_json(Obj, "useByDefault", useByDefault); } bool RadiusProxyPool::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"name",name); - field_from_json(Obj,"description",description); - field_from_json(Obj,"authConfig",authConfig); - field_from_json(Obj,"acctConfig",acctConfig); - field_from_json(Obj,"coaConfig",coaConfig); - field_from_json(Obj,"useByDefault",useByDefault); + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "authConfig", authConfig); + field_from_json(Obj, "acctConfig", acctConfig); + field_from_json(Obj, "coaConfig", coaConfig); + field_from_json(Obj, "useByDefault", useByDefault); return true; } catch (const Poco::Exception &E) { } @@ -371,20 +377,20 @@ namespace OpenWifi::GWObjects { } void RadiusProxyServerConfig::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"strategy",strategy); - field_to_json(Obj,"monitor",monitor); - field_to_json(Obj,"monitorMethod",monitorMethod); - field_to_json(Obj,"methodParameters",methodParameters); - field_to_json(Obj,"servers",servers); + field_to_json(Obj, "strategy", strategy); + field_to_json(Obj, "monitor", monitor); + field_to_json(Obj, "monitorMethod", monitorMethod); + field_to_json(Obj, "methodParameters", methodParameters); + field_to_json(Obj, "servers", servers); } bool RadiusProxyServerConfig::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"strategy",strategy); - field_from_json(Obj,"monitor",monitor); - field_from_json(Obj,"monitorMethod",monitorMethod); - field_from_json(Obj,"methodParameters",methodParameters); - field_from_json(Obj,"servers",servers); + field_from_json(Obj, "strategy", strategy); + field_from_json(Obj, "monitor", monitor); + field_from_json(Obj, "monitorMethod", monitorMethod); + field_from_json(Obj, "methodParameters", methodParameters); + field_from_json(Obj, "servers", servers); return true; } catch (const Poco::Exception &E) { } @@ -392,40 +398,40 @@ namespace OpenWifi::GWObjects { } void RadiusProxyServerEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"name",name); - field_to_json(Obj,"ip",ip); - field_to_json(Obj,"port",port); - field_to_json(Obj,"weight",weight); - field_to_json(Obj,"secret",secret); - field_to_json(Obj,"certificate",certificate); - field_to_json(Obj,"radsec",radsec); - field_to_json(Obj,"allowSelfSigned",allowSelfSigned); - field_to_json(Obj,"radsecPort",radsecPort); - field_to_json(Obj,"radsecSecret",radsecSecret); - field_to_json(Obj,"radsecCacerts",radsecCacerts); - field_to_json(Obj,"radsecCert",radsecCert); - field_to_json(Obj,"radsecKey",radsecKey); - field_to_json(Obj,"radsecRealms",radsecRealms); - field_to_json(Obj,"ignore",ignore); + field_to_json(Obj, "name", name); + field_to_json(Obj, "ip", ip); + field_to_json(Obj, "port", port); + field_to_json(Obj, "weight", weight); + field_to_json(Obj, "secret", secret); + field_to_json(Obj, "certificate", certificate); + field_to_json(Obj, "radsec", radsec); + field_to_json(Obj, "allowSelfSigned", allowSelfSigned); + field_to_json(Obj, "radsecPort", radsecPort); + field_to_json(Obj, "radsecSecret", radsecSecret); + field_to_json(Obj, "radsecCacerts", radsecCacerts); + field_to_json(Obj, "radsecCert", radsecCert); + field_to_json(Obj, "radsecKey", radsecKey); + field_to_json(Obj, "radsecRealms", radsecRealms); + field_to_json(Obj, "ignore", ignore); } bool RadiusProxyServerEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"name",name); - field_from_json(Obj,"ip",ip); - field_from_json(Obj,"port",port); - field_from_json(Obj,"weight",weight); - field_from_json(Obj,"secret",secret); - field_from_json(Obj,"certificate",certificate); - field_from_json(Obj,"radsec",radsec); - field_from_json(Obj,"allowSelfSigned",allowSelfSigned); - field_from_json(Obj,"radsecSecret",radsecSecret); - field_from_json(Obj,"radsecPort",radsecPort); - field_from_json(Obj,"radsecCacerts",radsecCacerts); - field_from_json(Obj,"radsecCert",radsecCert); - field_from_json(Obj,"radsecKey",radsecKey); - field_from_json(Obj,"radsecRealms",radsecRealms); - field_from_json(Obj,"ignore",ignore); + field_from_json(Obj, "name", name); + field_from_json(Obj, "ip", ip); + field_from_json(Obj, "port", port); + field_from_json(Obj, "weight", weight); + field_from_json(Obj, "secret", secret); + field_from_json(Obj, "certificate", certificate); + field_from_json(Obj, "radsec", radsec); + field_from_json(Obj, "allowSelfSigned", allowSelfSigned); + field_from_json(Obj, "radsecSecret", radsecSecret); + field_from_json(Obj, "radsecPort", radsecPort); + field_from_json(Obj, "radsecCacerts", radsecCacerts); + field_from_json(Obj, "radsecCert", radsecCert); + field_from_json(Obj, "radsecKey", radsecKey); + field_from_json(Obj, "radsecRealms", radsecRealms); + field_from_json(Obj, "ignore", ignore); return true; } catch (const Poco::Exception &E) { } @@ -433,38 +439,38 @@ namespace OpenWifi::GWObjects { } void ScriptEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id", id); - field_to_json(Obj,"name", name); - field_to_json(Obj,"description", description); - field_to_json(Obj,"uri", uri); - field_to_json(Obj,"content", content); - field_to_json(Obj,"version", version); - field_to_json(Obj,"type", type); - field_to_json(Obj,"created", created); - field_to_json(Obj,"modified", modified); - field_to_json(Obj,"author", author); - field_to_json(Obj,"restricted", restricted); - field_to_json(Obj,"deferred", deferred); - field_to_json(Obj,"timeout", timeout); - field_to_json(Obj,"defaultUploadURI", defaultUploadURI); + field_to_json(Obj, "id", id); + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "uri", uri); + field_to_json(Obj, "content", content); + field_to_json(Obj, "version", version); + field_to_json(Obj, "type", type); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + field_to_json(Obj, "author", author); + field_to_json(Obj, "restricted", restricted); + field_to_json(Obj, "deferred", deferred); + field_to_json(Obj, "timeout", timeout); + field_to_json(Obj, "defaultUploadURI", defaultUploadURI); } bool ScriptEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"id", id); - field_from_json(Obj,"name", name); - field_from_json(Obj,"description", description); - field_from_json(Obj,"uri", uri); - field_from_json(Obj,"content", content); - field_from_json(Obj,"version", version); - field_from_json(Obj,"type", type); - field_from_json(Obj,"created", created); - field_from_json(Obj,"modified", modified); - field_from_json(Obj,"author", author); - field_from_json(Obj,"restricted", restricted); - field_from_json(Obj,"deferred", deferred); - field_from_json(Obj,"timeout", timeout); - field_from_json(Obj,"defaultUploadURI", defaultUploadURI); + field_from_json(Obj, "id", id); + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "uri", uri); + field_from_json(Obj, "content", content); + field_from_json(Obj, "version", version); + field_from_json(Obj, "type", type); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + field_from_json(Obj, "author", author); + field_from_json(Obj, "restricted", restricted); + field_from_json(Obj, "deferred", deferred); + field_from_json(Obj, "timeout", timeout); + field_from_json(Obj, "defaultUploadURI", defaultUploadURI); return true; } catch (const Poco::Exception &E) { } @@ -472,12 +478,12 @@ namespace OpenWifi::GWObjects { } void ScriptEntryList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"scripts",scripts); + field_to_json(Obj, "scripts", scripts); } bool ScriptEntryList::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"scripts",scripts); + field_from_json(Obj, "scripts", scripts); return true; } catch (const Poco::Exception &E) { } @@ -485,63 +491,57 @@ namespace OpenWifi::GWObjects { } void DeviceRestrictionsKeyInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"vendor", vendor); - field_to_json(Obj,"algo", algo); + field_to_json(Obj, "vendor", vendor); + field_to_json(Obj, "algo", algo); } bool DeviceRestrictionsKeyInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"vendor", vendor); - field_from_json(Obj,"algo", algo); + field_from_json(Obj, "vendor", vendor); + field_from_json(Obj, "algo", algo); return true; } catch (const Poco::Exception &E) { } return false; - } void DeviceRestrictions::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"dfs", dfs); - field_to_json(Obj,"ssh", ssh); - field_to_json(Obj,"rtty", rtty); - field_to_json(Obj,"tty", tty); - field_to_json(Obj,"developer", developer); - field_to_json(Obj,"upgrade", upgrade); - field_to_json(Obj,"commands", commands); - field_to_json(Obj,"country", country); - field_to_json(Obj,"key_info", key_info); + field_to_json(Obj, "dfs", dfs); + field_to_json(Obj, "ssh", ssh); + field_to_json(Obj, "rtty", rtty); + field_to_json(Obj, "tty", tty); + field_to_json(Obj, "developer", developer); + field_to_json(Obj, "upgrade", upgrade); + field_to_json(Obj, "commands", commands); + field_to_json(Obj, "country", country); + field_to_json(Obj, "key_info", key_info); } bool DeviceRestrictions::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"dfs", dfs); - field_from_json(Obj,"ssh", ssh); - field_from_json(Obj,"rtty", rtty); - field_from_json(Obj,"tty", tty); - field_from_json(Obj,"developer", developer); - field_from_json(Obj,"upgrade", upgrade); - field_from_json(Obj,"commands", commands); - field_from_json(Obj,"country", country); - field_from_json(Obj,"key_info", key_info); + field_from_json(Obj, "dfs", dfs); + field_from_json(Obj, "ssh", ssh); + field_from_json(Obj, "rtty", rtty); + field_from_json(Obj, "tty", tty); + field_from_json(Obj, "developer", developer); + field_from_json(Obj, "upgrade", upgrade); + field_from_json(Obj, "commands", commands); + field_from_json(Obj, "country", country); + field_from_json(Obj, "key_info", key_info); return true; } catch (const Poco::Exception &E) { } return false; } - bool DeviceRestrictionsKeyInfo::operator!=(const OpenWifi::GWObjects::DeviceRestrictionsKeyInfo &T) const { - return (T.algo!=algo) || (T.vendor!=vendor); + bool DeviceRestrictionsKeyInfo::operator!=( + const OpenWifi::GWObjects::DeviceRestrictionsKeyInfo &T) const { + return (T.algo != algo) || (T.vendor != vendor); } bool DeviceRestrictions::operator!=(const OpenWifi::GWObjects::DeviceRestrictions &T) const { - return ( (T.dfs!=dfs) || - (T.rtty!=rtty) || - (T.upgrade!=upgrade) || - (T.commands != commands) || - (T.developer != developer) || - (T.ssh !=ssh) || - (T.key_info != key_info) || - (T.country != country) ); + return ((T.dfs != dfs) || (T.rtty != rtty) || (T.upgrade != upgrade) || + (T.commands != commands) || (T.developer != developer) || (T.ssh != ssh) || + (T.key_info != key_info) || (T.country != country)); } -} - +} // namespace OpenWifi::GWObjects diff --git a/src/RESTObjects/RESTAPI_GWobjects.h b/src/RESTObjects/RESTAPI_GWobjects.h index 3e0e404..ed7b264 100644 --- a/src/RESTObjects/RESTAPI_GWobjects.h +++ b/src/RESTObjects/RESTAPI_GWobjects.h @@ -13,62 +13,57 @@ namespace OpenWifi::GWObjects { - enum CertificateValidation { - NO_CERTIFICATE, - VALID_CERTIFICATE, - MISMATCH_SERIAL, - VERIFIED - }; + enum CertificateValidation { NO_CERTIFICATE, VALID_CERTIFICATE, MISMATCH_SERIAL, VERIFIED }; struct ConnectionState { - uint64_t MessageCount = 0 ; + uint64_t MessageCount = 0; std::string Address; - uint64_t UUID = 0 ; - uint64_t PendingUUID = 0 ; + uint64_t UUID = 0; + uint64_t PendingUUID = 0; uint64_t TX = 0, RX = 0; - uint64_t Associations_2G=0; - uint64_t Associations_5G=0; - uint64_t Associations_6G=0; + uint64_t Associations_2G = 0; + uint64_t Associations_5G = 0; + uint64_t Associations_6G = 0; bool Connected = false; - uint64_t LastContact=0; + uint64_t LastContact = 0; std::string Firmware; CertificateValidation VerifiedCertificate = NO_CERTIFICATE; - std::string Compatible; - uint64_t kafkaClients=0; - uint64_t webSocketClients=0; - uint64_t kafkaPackets=0; - uint64_t websocketPackets=0; - std::string locale; - uint64_t started=0; - uint64_t sessionId=0; - double connectionCompletionTime=0.0; - std::uint64_t certificateExpiryDate=0; + std::string Compatible; + uint64_t kafkaClients = 0; + uint64_t webSocketClients = 0; + uint64_t kafkaPackets = 0; + uint64_t websocketPackets = 0; + std::string locale; + uint64_t started = 0; + uint64_t sessionId = 0; + double connectionCompletionTime = 0.0; + std::uint64_t certificateExpiryDate = 0; void to_json(Poco::JSON::Object &Obj) const; }; struct DeviceRestrictionsKeyInfo { - std::string vendor; - std::string algo; + std::string vendor; + std::string algo; - bool operator !=(const DeviceRestrictionsKeyInfo &b) const; + bool operator!=(const DeviceRestrictionsKeyInfo &b) const; void to_json(Poco::JSON::Object &Obj) const; bool from_json(const Poco::JSON::Object::Ptr &Obj); }; struct DeviceRestrictions { - bool dfs = false; - bool ssh = false; - bool rtty = false; - bool tty = false; - bool developer = false; - bool upgrade = false; - bool commands = false; - std::vector country; - DeviceRestrictionsKeyInfo key_info; + bool dfs = false; + bool ssh = false; + bool rtty = false; + bool tty = false; + bool developer = false; + bool upgrade = false; + bool commands = false; + std::vector country; + DeviceRestrictionsKeyInfo key_info; - bool operator !=(const DeviceRestrictions &D) const; + bool operator!=(const DeviceRestrictions &D) const; void to_json(Poco::JSON::Object &Obj) const; bool from_json(const Poco::JSON::Object::Ptr &Obj); @@ -80,27 +75,27 @@ namespace OpenWifi::GWObjects { std::string MACAddress; std::string Manufacturer; std::string Configuration; - SecurityObjects::NoteInfoVec Notes; + SecurityObjects::NoteInfoVec Notes; std::string Owner; std::string Location; std::string Firmware; std::string Compatible; std::string FWUpdatePolicy; - uint64_t UUID = 0 ; - uint64_t CreationTimestamp = 0 ; - uint64_t LastConfigurationChange = 0 ; - uint64_t LastConfigurationDownload = 0 ; - uint64_t LastFWUpdate = 0 ; + uint64_t UUID = 0; + uint64_t CreationTimestamp = 0; + uint64_t LastConfigurationChange = 0; + uint64_t LastConfigurationDownload = 0; + uint64_t LastFWUpdate = 0; std::string Venue; std::string DevicePassword; std::string subscriber; std::string entity; - uint64_t modified=0; + uint64_t modified = 0; std::string locale; - bool restrictedDevice=false; + bool restrictedDevice = false; std::string pendingConfiguration; std::string pendingConfigurationCmd; - DeviceRestrictions restrictionDetails; + DeviceRestrictions restrictionDetails; void to_json(Poco::JSON::Object &Obj) const; void to_json_with_status(Poco::JSON::Object &Obj) const; @@ -119,26 +114,26 @@ namespace OpenWifi::GWObjects { struct Statistics { std::string SerialNumber; - uint64_t UUID = 0 ; + uint64_t UUID = 0; std::string Data; - uint64_t Recorded = 0; + uint64_t Recorded = 0; void to_json(Poco::JSON::Object &Obj) const; }; struct HealthCheck { std::string SerialNumber; - uint64_t UUID = 0 ; + uint64_t UUID = 0; std::string Data; - uint64_t Recorded = 0 ; - uint64_t Sanity = 0 ; + uint64_t Recorded = 0; + uint64_t Sanity = 0; void to_json(Poco::JSON::Object &Obj) const; }; struct Capabilities { std::string Capabilities; - uint64_t FirstUpdate = 0 ; - uint64_t LastUpdate = 0 ; - void to_json(Poco::JSON::Object &Obj) const; + uint64_t FirstUpdate = 0; + uint64_t LastUpdate = 0; + void to_json(Poco::JSON::Object &Obj) const; }; struct DeviceLog { @@ -155,11 +150,11 @@ namespace OpenWifi::GWObjects { std::string SerialNumber; std::string Log; std::string Data; - uint64_t Severity = 0 ; - uint64_t Recorded = 0 ; - uint64_t LogType = 0 ; - uint64_t UUID = 0 ; - void to_json(Poco::JSON::Object &Obj) const; + uint64_t Severity = 0; + uint64_t Recorded = 0; + uint64_t LogType = 0; + uint64_t UUID = 0; + void to_json(Poco::JSON::Object &Obj) const; }; struct DefaultConfiguration { @@ -167,10 +162,10 @@ namespace OpenWifi::GWObjects { std::string Configuration; Types::StringVec Models; std::string Description; - uint64_t Created; - uint64_t LastModified; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); + uint64_t Created; + uint64_t LastModified; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); }; struct CommandDetails { @@ -184,15 +179,15 @@ namespace OpenWifi::GWObjects { std::string ErrorText; uint64_t Submitted = time(nullptr); uint64_t Executed = 0; - uint64_t Completed = 0 ; - uint64_t RunAt = 0 ; - uint64_t ErrorCode = 0 ; - uint64_t Custom = 0 ; - uint64_t WaitingForFile = 0 ; - uint64_t AttachDate = 0 ; - uint64_t AttachSize = 0 ; + uint64_t Completed = 0; + uint64_t RunAt = 0; + uint64_t ErrorCode = 0; + uint64_t Custom = 0; + uint64_t WaitingForFile = 0; + uint64_t AttachDate = 0; + uint64_t AttachSize = 0; std::string AttachType; - double executionTime = 0.0; + double executionTime = 0.0; void to_json(Poco::JSON::Object &Obj) const; }; @@ -208,20 +203,20 @@ namespace OpenWifi::GWObjects { struct RttySessionDetails { std::string SerialNumber; std::string Server; - uint64_t Port = 0 ; + uint64_t Port = 0; std::string Token; - uint64_t TimeOut = 0 ; + uint64_t TimeOut = 0; std::string ConnectionId; - uint64_t Started = 0 ; + uint64_t Started = 0; std::string CommandUUID; - uint64_t ViewPort = 0 ; + uint64_t ViewPort = 0; std::string DevicePassword; void to_json(Poco::JSON::Object &Obj) const; }; struct Dashboard { - uint64_t snapshot = 0 ; - uint64_t numberOfDevices = 0 ; + uint64_t snapshot = 0; + uint64_t numberOfDevices = 0; Types::CountedMap commands; Types::CountedMap upTimes; Types::CountedMap memoryUsed; @@ -247,27 +242,27 @@ namespace OpenWifi::GWObjects { }; struct ScriptEntry { - std::string id; - std::string name; - std::string description; - std::string uri; - std::string content; - std::string version; - std::string type; - std::uint64_t created; - std::uint64_t modified; - std::string author; - Types::StringVec restricted; - bool deferred=false; - std::uint64_t timeout=30; - std::string defaultUploadURI; + std::string id; + std::string name; + std::string description; + std::string uri; + std::string content; + std::string version; + std::string type; + std::uint64_t created; + std::uint64_t modified; + std::string author; + Types::StringVec restricted; + bool deferred = false; + std::uint64_t timeout = 30; + std::string defaultUploadURI; void to_json(Poco::JSON::Object &Obj) const; bool from_json(const Poco::JSON::Object::Ptr &Obj); }; struct ScriptEntryList { - std::vector scripts; + std::vector scripts; void to_json(Poco::JSON::Object &Obj) const; bool from_json(const Poco::JSON::Object::Ptr &Obj); @@ -275,7 +270,7 @@ namespace OpenWifi::GWObjects { struct ScriptRequest { std::string serialNumber; - uint64_t timeout=30; + uint64_t timeout = 30; std::string type; std::string script; std::string scriptId; @@ -291,52 +286,52 @@ namespace OpenWifi::GWObjects { struct RadiusProxyServerEntry { std::string name; std::string ip; - uint16_t port=0; - uint64_t weight=0; + uint16_t port = 0; + uint64_t weight = 0; std::string secret; std::string certificate; - bool radsec=false; - bool allowSelfSigned=false; - uint16_t radsecPort=2083; + bool radsec = false; + bool allowSelfSigned = false; + uint16_t radsecPort = 2083; std::string radsecSecret; std::string radsecKey; std::string radsecCert; - std::vector radsecCacerts; - std::vector radsecRealms; - bool ignore=false; + std::vector radsecCacerts; + std::vector radsecRealms; + bool ignore = false; void to_json(Poco::JSON::Object &Obj) const; bool from_json(const Poco::JSON::Object::Ptr &Obj); }; struct RadiusProxyServerConfig { - std::string strategy; - bool monitor=false; - std::string monitorMethod; - std::vector methodParameters; - std::vector servers; + std::string strategy; + bool monitor = false; + std::string monitorMethod; + std::vector methodParameters; + std::vector servers; void to_json(Poco::JSON::Object &Obj) const; bool from_json(const Poco::JSON::Object::Ptr &Obj); }; - struct RadiusProxyPool { + struct RadiusProxyPool { std::string name; std::string description; - RadiusProxyServerConfig authConfig; - RadiusProxyServerConfig acctConfig; - RadiusProxyServerConfig coaConfig; - bool useByDefault=false; + RadiusProxyServerConfig authConfig; + RadiusProxyServerConfig acctConfig; + RadiusProxyServerConfig coaConfig; + bool useByDefault = false; void to_json(Poco::JSON::Object &Obj) const; bool from_json(const Poco::JSON::Object::Ptr &Obj); }; struct RadiusProxyPoolList { - std::vector pools; + std::vector pools; void to_json(Poco::JSON::Object &Obj) const; bool from_json(const Poco::JSON::Object::Ptr &Obj); }; -} +} // namespace OpenWifi::GWObjects diff --git a/src/RESTObjects/RESTAPI_OWLSobjects.cpp b/src/RESTObjects/RESTAPI_OWLSobjects.cpp index 15e5f0a..0a420b7 100644 --- a/src/RESTObjects/RESTAPI_OWLSobjects.cpp +++ b/src/RESTObjects/RESTAPI_OWLSobjects.cpp @@ -4,9 +4,9 @@ #include "framework/RESTAPI_utils.h" -using OpenWifi::RESTAPI_utils::field_to_json; -using OpenWifi::RESTAPI_utils::field_from_json; using OpenWifi::RESTAPI_utils::EmbedDocument; +using OpenWifi::RESTAPI_utils::field_from_json; +using OpenWifi::RESTAPI_utils::field_to_json; #include "RESTAPI_OWLSobjects.h" @@ -14,97 +14,89 @@ using OpenWifi::RESTAPI_utils::EmbedDocument; namespace OpenWifi::OWLSObjects { - void SimulationDetails::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id", id); - field_to_json(Obj,"name", name); - field_to_json(Obj,"gateway", gateway); - field_to_json(Obj,"certificate", certificate); - field_to_json(Obj,"key", key); - field_to_json(Obj,"macPrefix", macPrefix); - field_to_json(Obj,"deviceType", deviceType); - field_to_json(Obj,"devices", devices); - field_to_json(Obj,"healthCheckInterval", healthCheckInterval); - field_to_json(Obj,"stateInterval", stateInterval); - field_to_json(Obj,"minAssociations", minAssociations); - field_to_json(Obj,"maxAssociations", maxAssociations); - field_to_json(Obj,"minClients", minClients); - field_to_json(Obj,"maxClients", maxClients); - field_to_json(Obj,"simulationLength", simulationLength); - field_to_json(Obj,"threads", threads); - field_to_json(Obj,"clientInterval", clientInterval); - field_to_json(Obj,"keepAlive", keepAlive); - field_to_json(Obj,"reconnectInterval", reconnectInterval); - field_to_json(Obj,"concurrentDevices", concurrentDevices); - } + void SimulationDetails::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "name", name); + field_to_json(Obj, "gateway", gateway); + field_to_json(Obj, "certificate", certificate); + field_to_json(Obj, "key", key); + field_to_json(Obj, "macPrefix", macPrefix); + field_to_json(Obj, "deviceType", deviceType); + field_to_json(Obj, "devices", devices); + field_to_json(Obj, "healthCheckInterval", healthCheckInterval); + field_to_json(Obj, "stateInterval", stateInterval); + field_to_json(Obj, "minAssociations", minAssociations); + field_to_json(Obj, "maxAssociations", maxAssociations); + field_to_json(Obj, "minClients", minClients); + field_to_json(Obj, "maxClients", maxClients); + field_to_json(Obj, "simulationLength", simulationLength); + field_to_json(Obj, "threads", threads); + field_to_json(Obj, "clientInterval", clientInterval); + field_to_json(Obj, "keepAlive", keepAlive); + field_to_json(Obj, "reconnectInterval", reconnectInterval); + field_to_json(Obj, "concurrentDevices", concurrentDevices); + } - bool SimulationDetails::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id", id); - field_from_json(Obj,"name", name); - field_from_json(Obj,"gateway", gateway); - field_from_json(Obj,"certificate", certificate); - field_from_json(Obj,"key", key); - field_from_json(Obj,"macPrefix", macPrefix); - field_from_json(Obj,"deviceType", deviceType); - field_from_json(Obj,"devices", devices); - field_from_json(Obj,"healthCheckInterval", healthCheckInterval); - field_from_json(Obj,"stateInterval", stateInterval); - field_from_json(Obj,"minAssociations", minAssociations); - field_from_json(Obj,"maxAssociations", maxAssociations); - field_from_json(Obj,"minClients", minClients); - field_from_json(Obj,"maxClients", maxClients); - field_from_json(Obj,"simulationLength", simulationLength); - field_from_json(Obj,"threads", threads); - field_from_json(Obj,"clientInterval", clientInterval); - field_from_json(Obj,"keepAlive", keepAlive); - field_from_json(Obj,"reconnectInterval", reconnectInterval); - field_from_json(Obj,"concurrentDevices", concurrentDevices); - return true; - } catch(...) { + bool SimulationDetails::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "name", name); + field_from_json(Obj, "gateway", gateway); + field_from_json(Obj, "certificate", certificate); + field_from_json(Obj, "key", key); + field_from_json(Obj, "macPrefix", macPrefix); + field_from_json(Obj, "deviceType", deviceType); + field_from_json(Obj, "devices", devices); + field_from_json(Obj, "healthCheckInterval", healthCheckInterval); + field_from_json(Obj, "stateInterval", stateInterval); + field_from_json(Obj, "minAssociations", minAssociations); + field_from_json(Obj, "maxAssociations", maxAssociations); + field_from_json(Obj, "minClients", minClients); + field_from_json(Obj, "maxClients", maxClients); + field_from_json(Obj, "simulationLength", simulationLength); + field_from_json(Obj, "threads", threads); + field_from_json(Obj, "clientInterval", clientInterval); + field_from_json(Obj, "keepAlive", keepAlive); + field_from_json(Obj, "reconnectInterval", reconnectInterval); + field_from_json(Obj, "concurrentDevices", concurrentDevices); + return true; + } catch (...) { + } + return false; + } - } - return false; - } + void SimulationDetailsList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "list", list); + } - void SimulationDetailsList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"list", list); - } + bool SimulationDetailsList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "list", list); + return true; + } catch (...) { + } + return false; + } - bool SimulationDetailsList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"list", list); - return true; - } catch(...) { + void SimulationStatus::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "simulationId", simulationId); + field_to_json(Obj, "state", state); + field_to_json(Obj, "tx", tx); + field_to_json(Obj, "rx", rx); + field_to_json(Obj, "msgsTx", msgsTx); + field_to_json(Obj, "msgsRx", msgsRx); + field_to_json(Obj, "liveDevices", liveDevices); + field_to_json(Obj, "timeToFullDevices", timeToFullDevices); + field_to_json(Obj, "startTime", startTime); + field_to_json(Obj, "endTime", endTime); + field_to_json(Obj, "errorDevices", errorDevices); + field_to_json(Obj, "owner", owner); + } - } - return false; - } + void Dashboard::to_json([[maybe_unused]] Poco::JSON::Object &Obj) const {} - void SimulationStatus::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id", id); - field_to_json(Obj,"simulationId", simulationId); - field_to_json(Obj,"state", state); - field_to_json(Obj,"tx", tx); - field_to_json(Obj,"rx", rx); - field_to_json(Obj,"msgsTx", msgsTx); - field_to_json(Obj,"msgsRx", msgsRx); - field_to_json(Obj,"liveDevices", liveDevices); - field_to_json(Obj,"timeToFullDevices", timeToFullDevices); - field_to_json(Obj,"startTime", startTime); - field_to_json(Obj,"endTime", endTime); - field_to_json(Obj,"errorDevices", errorDevices); - field_to_json(Obj,"owner", owner); - } + bool Dashboard::from_json([[maybe_unused]] const Poco::JSON::Object::Ptr &Obj) { return true; } - void Dashboard::to_json([[maybe_unused]] Poco::JSON::Object &Obj) const { - - } - - bool Dashboard::from_json([[maybe_unused]] const Poco::JSON::Object::Ptr &Obj) { - return true; - } - - void Dashboard::reset() { - - } -} + void Dashboard::reset() {} +} // namespace OpenWifi::OWLSObjects diff --git a/src/RESTObjects/RESTAPI_OWLSobjects.h b/src/RESTObjects/RESTAPI_OWLSobjects.h index 4dadcb6..92623cb 100644 --- a/src/RESTObjects/RESTAPI_OWLSobjects.h +++ b/src/RESTObjects/RESTAPI_OWLSobjects.h @@ -5,73 +5,70 @@ #ifndef UCENTRALSIM_RESTAPI_OWLSOBJECTS_H #define UCENTRALSIM_RESTAPI_OWLSOBJECTS_H -#include #include "Poco/JSON/Object.h" +#include namespace OpenWifi::OWLSObjects { - struct SimulationDetails { - std::string id; - std::string name; - std::string gateway; - std::string certificate; - std::string key; - std::string macPrefix; - std::string deviceType; - uint64_t devices = 5; - uint64_t healthCheckInterval = 60; - uint64_t stateInterval = 60 ; - uint64_t minAssociations = 1; - uint64_t maxAssociations = 3; - uint64_t minClients = 1 ; - uint64_t maxClients = 3; - uint64_t simulationLength = 60 * 60; - uint64_t threads = 16; - uint64_t clientInterval = 1; - uint64_t keepAlive = 300; - uint64_t reconnectInterval = 30 ; - uint64_t concurrentDevices = 5; + struct SimulationDetails { + std::string id; + std::string name; + std::string gateway; + std::string certificate; + std::string key; + std::string macPrefix; + std::string deviceType; + uint64_t devices = 5; + uint64_t healthCheckInterval = 60; + uint64_t stateInterval = 60; + uint64_t minAssociations = 1; + uint64_t maxAssociations = 3; + uint64_t minClients = 1; + uint64_t maxClients = 3; + uint64_t simulationLength = 60 * 60; + uint64_t threads = 16; + uint64_t clientInterval = 1; + uint64_t keepAlive = 300; + uint64_t reconnectInterval = 30; + uint64_t concurrentDevices = 5; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct SimulationDetailsList { - std::vector list; + struct SimulationDetailsList { + std::vector list; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct SimulationStatus { - std::string id; - std::string simulationId; - std::string state; - uint64_t tx; - uint64_t rx; - uint64_t msgsTx; - uint64_t msgsRx; - uint64_t liveDevices; - uint64_t timeToFullDevices; - uint64_t startTime; - uint64_t endTime; - uint64_t errorDevices; - std::string owner; + struct SimulationStatus { + std::string id; + std::string simulationId; + std::string state; + uint64_t tx; + uint64_t rx; + uint64_t msgsTx; + uint64_t msgsRx; + uint64_t liveDevices; + uint64_t timeToFullDevices; + uint64_t startTime; + uint64_t endTime; + uint64_t errorDevices; + std::string owner; - void to_json(Poco::JSON::Object &Obj) const; - }; + void to_json(Poco::JSON::Object &Obj) const; + }; + struct Dashboard { + int O; - struct Dashboard { - int O; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + void reset(); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - void reset(); +} // namespace OpenWifi::OWLSObjects - }; - -} - - -#endif //UCENTRALSIM_RESTAPI_OWLSOBJECTS_H +#endif // UCENTRALSIM_RESTAPI_OWLSOBJECTS_H diff --git a/src/RESTObjects/RESTAPI_ProvObjects.cpp b/src/RESTObjects/RESTAPI_ProvObjects.cpp index c5387a8..533d0f4 100644 --- a/src/RESTObjects/RESTAPI_ProvObjects.cpp +++ b/src/RESTObjects/RESTAPI_ProvObjects.cpp @@ -6,1239 +6,1192 @@ // Arilia Wireless Inc. // - #include "RESTAPI_ProvObjects.h" -#include "framework/RESTAPI_utils.h" #include "framework/MicroServiceFuncs.h" +#include "framework/RESTAPI_utils.h" #include "framework/utils.h" -using OpenWifi::RESTAPI_utils::field_to_json; using OpenWifi::RESTAPI_utils::field_from_json; +using OpenWifi::RESTAPI_utils::field_to_json; namespace OpenWifi::ProvObjects { - void ObjectInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id",id); - field_to_json(Obj,"name",name); - field_to_json(Obj,"description",description); - field_to_json(Obj,"created",created); - field_to_json(Obj,"modified",modified); - field_to_json(Obj,"notes",notes); - field_to_json(Obj,"tags",tags); - } - - bool ObjectInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id",id); - field_from_json(Obj,"name",name); - field_from_json(Obj,"description",description); - field_from_json(Obj,"created",created); - field_from_json(Obj,"modified",modified); - field_from_json(Obj,"notes",notes); - field_from_json(Obj,"tags",tags); - return true; - } catch(...) { - - } - return false; - } - - void ManagementPolicyEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"users",users); - field_to_json( Obj,"resources",resources); - field_to_json( Obj,"access",access); - field_to_json( Obj,"policy",policy); - } - - bool ManagementPolicyEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"users",users); - field_from_json( Obj,"resources",resources); - field_from_json( Obj,"access",access); - field_from_json( Obj,"policy",policy); - return true; - } catch(...) { - - } - return false; - } - - void ManagementPolicy::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json(Obj, "entries", entries); - field_to_json(Obj, "inUse", inUse); - field_to_json(Obj, "entity", entity); - } - - bool ManagementPolicy::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json(Obj, "entries", entries); - field_from_json(Obj, "inUse", inUse); - field_from_json(Obj, "entity", entity); - return true; - } catch(...) { - - } - return false; - } - - void Entity::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"parent",parent); - field_to_json( Obj,"venues",venues); - field_to_json( Obj,"children",children); - field_to_json( Obj,"contacts",contacts); - field_to_json( Obj,"locations",locations); - field_to_json( Obj,"managementPolicy",managementPolicy); - field_to_json( Obj,"deviceConfiguration",deviceConfiguration); - field_to_json( Obj,"devices",devices); - field_to_json( Obj,"deviceRules",deviceRules); - field_to_json( Obj,"sourceIP",sourceIP); - field_to_json( Obj,"variables", variables); - field_to_json( Obj,"managementPolicies", managementPolicies); - field_to_json( Obj,"managementRoles", managementRoles); - field_to_json( Obj,"maps", maps); - field_to_json( Obj,"configurations", configurations); - } - - bool Entity::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"parent",parent); - field_from_json( Obj,"venues",venues); - field_from_json( Obj,"children",children); - field_from_json( Obj,"contacts",contacts); - field_from_json( Obj,"locations",locations); - field_from_json( Obj,"managementPolicy",managementPolicy); - field_from_json( Obj,"deviceConfiguration",deviceConfiguration); - field_from_json( Obj,"devices",devices); - field_from_json( Obj,"deviceRules",deviceRules); - field_from_json( Obj,"sourceIP",sourceIP); - field_from_json( Obj,"variables", variables); - field_from_json( Obj,"managementPolicies", managementPolicies); - field_from_json( Obj,"managementRoles", managementRoles); - field_from_json( Obj,"maps", maps); - field_from_json( Obj,"configurations", configurations); - return true; - } catch(...) { - - } - return false; - } - - void DiGraphEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"parent",parent); - field_to_json( Obj,"child",child); - } - - bool DiGraphEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"parent",parent); - field_from_json( Obj,"child",child); - return true; - } catch (...) { - - } - return false; - } - - void Venue::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"parent",parent); - field_to_json( Obj,"entity",entity); - field_to_json( Obj,"children",children); - field_to_json( Obj,"devices",devices); - field_to_json( Obj,"topology",topology); - field_to_json( Obj,"parent",parent); - field_to_json( Obj,"design",design); - field_to_json( Obj,"managementPolicy",managementPolicy); - field_to_json( Obj,"deviceConfiguration",deviceConfiguration); - field_to_json( Obj,"contacts",contacts); - field_to_json( Obj,"location",location); - field_to_json( Obj,"deviceRules",deviceRules); - field_to_json( Obj,"sourceIP",sourceIP); - field_to_json( Obj,"variables", variables); - field_to_json( Obj,"managementPolicies", managementPolicies); - field_to_json( Obj,"managementRoles", managementRoles); - field_to_json( Obj,"maps", maps); - field_to_json( Obj,"configurations", configurations); - field_to_json( Obj,"boards", boards); - } - - bool Venue::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"parent",parent); - field_from_json( Obj,"entity",entity); - field_from_json( Obj,"children",children); - field_from_json( Obj,"devices",devices); - field_from_json( Obj,"topology",topology); - field_from_json( Obj,"parent",parent); - field_from_json( Obj,"design",design); - field_from_json( Obj,"managementPolicy",managementPolicy); - field_from_json( Obj,"deviceConfiguration",deviceConfiguration); - field_from_json( Obj,"contacts",contacts); - field_from_json( Obj,"location",location); - field_from_json( Obj,"deviceRules",deviceRules); - field_from_json( Obj,"sourceIP",sourceIP); - field_from_json( Obj,"variables", variables); - field_from_json( Obj,"managementPolicies", managementPolicies); - field_from_json( Obj,"managementRoles", managementRoles); - field_from_json( Obj,"maps", maps); - field_from_json( Obj,"configurations", configurations); - field_from_json( Obj,"boards", boards); - return true; - } catch (...) { - - } - return false; - } - - void Operator::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"managementPolicy",managementPolicy); - field_to_json( Obj,"managementRoles",managementRoles); - field_to_json( Obj,"deviceRules",deviceRules); - field_to_json( Obj,"variables",variables); - field_to_json( Obj,"defaultOperator",defaultOperator); - field_to_json( Obj,"sourceIP",sourceIP); - field_to_json( Obj,"registrationId",registrationId); - } - - bool Operator::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"managementPolicy",managementPolicy); - field_from_json( Obj,"managementRoles",managementRoles); - field_from_json( Obj,"deviceRules",deviceRules); - field_from_json( Obj,"variables",variables); - field_from_json( Obj,"defaultOperator",defaultOperator); - field_from_json( Obj,"sourceIP",sourceIP); - field_from_json( Obj,"registrationId",registrationId); - return true; - } catch(...) { - } - return false; - } - - void OperatorList::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"operators",operators); - } - - bool OperatorList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"operators",operators); - return true; - } catch(...) { - } - return false; - } - - void ServiceClass::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"operatorId",operatorId); - field_to_json( Obj,"managementPolicy",managementPolicy); - field_to_json( Obj,"cost",cost); - field_to_json( Obj,"currency",currency); - field_to_json( Obj,"period",period); - field_to_json( Obj,"billingCode",billingCode); - field_to_json( Obj,"variables",variables); - field_to_json( Obj,"defaultService",defaultService); - } - - bool ServiceClass::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"operatorId",operatorId); - field_from_json( Obj,"managementPolicy",managementPolicy); - field_from_json( Obj,"cost",cost); - field_from_json( Obj,"currency",currency); - field_from_json( Obj,"period",period); - field_from_json( Obj,"billingCode",billingCode); - field_from_json( Obj,"variables",variables); - field_from_json( Obj,"defaultService",defaultService); - return true; - } catch(...) { - } - return false; - - } - - void ServiceClassList::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"serviceClasses",serviceClasses); - } - - bool ServiceClassList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"serviceClasses",serviceClasses); - return true; - } catch(...) { - } - return false; - } - - void UserInfoDigest::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"id",id); - field_to_json( Obj,"entity",loginId); - field_to_json( Obj,"children",userType); - } - - bool UserInfoDigest::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"id",id); - field_from_json( Obj,"entity",loginId); - field_from_json( Obj,"children",userType); - return true; - } catch(...) { - } - return false; - } - - void ManagementRole::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"managementPolicy",managementPolicy); - field_to_json( Obj,"users",users); - field_to_json( Obj,"entity",entity); - field_to_json( Obj,"venue",venue); - } - - bool ManagementRole::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"managementPolicy",managementPolicy); - field_from_json( Obj,"users",users); - field_from_json( Obj,"entity",entity); - field_from_json( Obj,"venue",venue); - return true; - } catch(...) { - } - return false; - } - - void Location::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"type",OpenWifi::ProvObjects::to_string(type)); - field_to_json( Obj,"buildingName",buildingName); - field_to_json( Obj,"addressLines",addressLines); - field_to_json( Obj,"city",city); - field_to_json( Obj,"state",state); - field_to_json( Obj,"postal",postal); - field_to_json( Obj,"country",country); - field_to_json( Obj,"phones",phones); - field_to_json( Obj,"mobiles",mobiles); - field_to_json( Obj,"geoCode",geoCode); - field_to_json( Obj,"inUse",inUse); - field_to_json( Obj,"entity",entity); - field_to_json( Obj,"managementPolicy",managementPolicy); - } - - bool Location::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - std::string tmp_type; - field_from_json( Obj,"type", tmp_type); - type = location_from_string(tmp_type); - field_from_json( Obj,"buildingName",buildingName); - field_from_json( Obj,"addressLines",addressLines); - field_from_json( Obj,"city",city); - field_from_json( Obj,"state",state); - field_from_json( Obj,"postal",postal); - field_from_json( Obj,"country",country); - field_from_json( Obj,"phones",phones); - field_from_json( Obj,"mobiles",mobiles); - field_from_json( Obj,"geoCode",geoCode); - field_from_json( Obj,"inUse",inUse); - field_from_json( Obj,"entity",entity); - field_from_json( Obj,"managementPolicy",managementPolicy); - return true; - } catch (...) { - - } - return false; - } - - void OperatorLocation::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"type",type); - field_to_json( Obj,"buildingName",buildingName); - field_to_json( Obj,"addressLines",addressLines); - field_to_json( Obj,"city",city); - field_to_json( Obj,"state",state); - field_to_json( Obj,"postal",postal); - field_to_json( Obj,"country",country); - field_to_json( Obj,"phones",phones); - field_to_json( Obj,"mobiles",mobiles); - field_to_json( Obj,"geoCode",geoCode); - field_to_json( Obj,"operatorId",operatorId); - field_to_json( Obj,"subscriberDeviceId",subscriberDeviceId); - field_to_json( Obj,"managementPolicy",managementPolicy); - } - - bool OperatorLocation::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"type", type); - field_from_json( Obj,"buildingName",buildingName); - field_from_json( Obj,"addressLines",addressLines); - field_from_json( Obj,"city",city); - field_from_json( Obj,"state",state); - field_from_json( Obj,"postal",postal); - field_from_json( Obj,"country",country); - field_from_json( Obj,"phones",phones); - field_from_json( Obj,"mobiles",mobiles); - field_from_json( Obj,"geoCode",geoCode); - field_from_json( Obj,"operatorId",operatorId); - field_from_json( Obj,"subscriberDeviceId",subscriberDeviceId); - field_from_json( Obj,"managementPolicy",managementPolicy); - return true; - } catch (...) { - - } - return false; - } - - void SubLocation::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"type",type); - field_to_json( Obj,"buildingName",buildingName); - field_to_json( Obj,"addressLines",addressLines); - field_to_json( Obj,"city",city); - field_to_json( Obj,"state",state); - field_to_json( Obj,"postal",postal); - field_to_json( Obj,"country",country); - field_to_json( Obj,"phones",phones); - field_to_json( Obj,"mobiles",mobiles); - field_to_json( Obj,"geoCode",geoCode); - } - - bool SubLocation::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"type", type); - field_from_json( Obj,"buildingName",buildingName); - field_from_json( Obj,"addressLines",addressLines); - field_from_json( Obj,"city",city); - field_from_json( Obj,"state",state); - field_from_json( Obj,"postal",postal); - field_from_json( Obj,"country",country); - field_from_json( Obj,"phones",phones); - field_from_json( Obj,"mobiles",mobiles); - field_from_json( Obj,"geoCode",geoCode); - return true; - } catch (...) { - - } - return false; - } - - void OperatorLocationList::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj, "locations", locations); - } - - bool OperatorLocationList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj, "locations", locations); - return true; - } catch(...) { - - } - return false; - } - - void Contact::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"type", to_string(type)); - field_to_json( Obj,"title",title); - field_to_json( Obj,"salutation",salutation); - field_to_json( Obj,"firstname",firstname); - field_to_json( Obj,"lastname",lastname); - field_to_json( Obj,"initials",initials); - field_to_json( Obj,"visual",visual); - field_to_json( Obj,"mobiles",mobiles); - field_to_json( Obj,"phones",phones); - field_to_json( Obj,"primaryEmail",primaryEmail); - field_to_json( Obj,"secondaryEmail",secondaryEmail); - field_to_json( Obj,"accessPIN",accessPIN); - field_to_json( Obj,"inUse",inUse); - field_to_json( Obj,"entity",entity); - field_to_json( Obj,"managementPolicy",managementPolicy); - } - - bool Contact::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - std::string tmp_type; - field_from_json( Obj,"type", tmp_type); - type = contact_from_string(tmp_type); - field_from_json( Obj,"title",title); - field_from_json( Obj,"salutation",salutation); - field_from_json( Obj,"firstname",firstname); - field_from_json( Obj,"lastname",lastname); - field_from_json( Obj,"initials",initials); - field_from_json( Obj,"visual",visual); - field_from_json( Obj,"mobiles",mobiles); - field_from_json( Obj,"phones",phones); - field_from_json( Obj,"primaryEmail",primaryEmail); - field_from_json( Obj,"secondaryEmail",secondaryEmail); - field_from_json( Obj,"accessPIN",accessPIN); - field_from_json( Obj,"inUse",inUse); - field_from_json( Obj,"entity",entity); - field_from_json( Obj,"managementPolicy",managementPolicy); - return true; - } catch (...) { - - } - return false; - } - - void OperatorContact::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"type", type); - field_to_json( Obj,"title",title); - field_to_json( Obj,"salutation",salutation); - field_to_json( Obj,"firstname",firstname); - field_to_json( Obj,"lastname",lastname); - field_to_json( Obj,"initials",initials); - field_to_json( Obj,"visual",visual); - field_to_json( Obj,"mobiles",mobiles); - field_to_json( Obj,"phones",phones); - field_to_json( Obj,"primaryEmail",primaryEmail); - field_to_json( Obj,"secondaryEmail",secondaryEmail); - field_to_json( Obj,"accessPIN",accessPIN); - field_to_json( Obj,"operatorId",operatorId); - field_to_json( Obj,"subscriberDeviceId",subscriberDeviceId); - field_to_json( Obj,"managementPolicy",managementPolicy); - } - - bool OperatorContact::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"type", type); - field_from_json( Obj,"title",title); - field_from_json( Obj,"salutation",salutation); - field_from_json( Obj,"firstname",firstname); - field_from_json( Obj,"lastname",lastname); - field_from_json( Obj,"initials",initials); - field_from_json( Obj,"visual",visual); - field_from_json( Obj,"mobiles",mobiles); - field_from_json( Obj,"phones",phones); - field_from_json( Obj,"primaryEmail",primaryEmail); - field_from_json( Obj,"secondaryEmail",secondaryEmail); - field_from_json( Obj,"accessPIN",accessPIN); - field_from_json( Obj,"operatorId",operatorId); - field_from_json( Obj,"subscriberDeviceId",subscriberDeviceId); - field_from_json( Obj,"managementPolicy",managementPolicy); - return true; - } catch (...) { - - } - return false; - } - - void SubContact::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"type", type); - field_to_json( Obj,"title",title); - field_to_json( Obj,"salutation",salutation); - field_to_json( Obj,"firstname",firstname); - field_to_json( Obj,"lastname",lastname); - field_to_json( Obj,"initials",initials); - field_to_json( Obj,"visual",visual); - field_to_json( Obj,"mobiles",mobiles); - field_to_json( Obj,"phones",phones); - field_to_json( Obj,"primaryEmail",primaryEmail); - field_to_json( Obj,"secondaryEmail",secondaryEmail); - field_to_json( Obj,"accessPIN",accessPIN); - } - - bool SubContact::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"type", type); - field_from_json( Obj,"title",title); - field_from_json( Obj,"salutation",salutation); - field_from_json( Obj,"firstname",firstname); - field_from_json( Obj,"lastname",lastname); - field_from_json( Obj,"initials",initials); - field_from_json( Obj,"visual",visual); - field_from_json( Obj,"mobiles",mobiles); - field_from_json( Obj,"phones",phones); - field_from_json( Obj,"primaryEmail",primaryEmail); - field_from_json( Obj,"secondaryEmail",secondaryEmail); - field_from_json( Obj,"accessPIN",accessPIN); - return true; - } catch (...) { - - } - return false; - } - - void OperatorContactList::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj, "contacts", contacts); - } - - bool OperatorContactList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj, "contacts", contacts); - return true; - } catch(...) { - - } - return false; - } - - void InventoryTag::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj, "serialNumber", serialNumber); - field_to_json( Obj, "venue", venue); - field_to_json( Obj, "entity", entity); - field_to_json( Obj, "subscriber", subscriber); - field_to_json( Obj, "deviceType", deviceType); - field_to_json( Obj, "qrCode", qrCode); - field_to_json( Obj, "geoCode", geoCode); - field_to_json( Obj, "location", location); - field_to_json( Obj, "contact", contact); - field_to_json( Obj, "deviceConfiguration",deviceConfiguration); - field_to_json( Obj,"deviceRules",deviceRules); - field_to_json( Obj, "managementPolicy",managementPolicy); - field_to_json( Obj, "state",state); - field_to_json( Obj, "devClass",devClass); - field_to_json( Obj, "locale",locale); - field_to_json( Obj, "realMacAddress",realMacAddress); - field_to_json( Obj, "doNotAllowOverrides",doNotAllowOverrides); - } - - bool InventoryTag::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"serialNumber",serialNumber); - field_from_json( Obj,"venue",venue); - field_from_json( Obj,"entity",entity); - field_from_json( Obj,"subscriber",subscriber); - field_from_json( Obj,"deviceType",deviceType); - field_from_json( Obj,"qrCode", qrCode); - field_from_json( Obj,"geoCode",geoCode); - field_from_json( Obj,"location",location); - field_from_json( Obj,"contact",contact); - field_from_json( Obj,"deviceConfiguration",deviceConfiguration); - field_from_json( Obj,"deviceRules",deviceRules); - field_from_json( Obj,"managementPolicy",managementPolicy); - field_from_json( Obj,"state",state); - field_from_json( Obj,"devClass",devClass); - field_from_json( Obj,"locale",locale); - field_from_json( Obj,"realMacAddress",realMacAddress); - field_from_json( Obj, "doNotAllowOverrides",doNotAllowOverrides); - return true; - } catch(...) { - - } - return false; - } - - void InventoryConfigApplyResult::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj, "appliedConfiguration", appliedConfiguration); - field_to_json( Obj, "warnings", warnings); - field_to_json( Obj, "errors", errors); - field_to_json( Obj, "errorCode", errorCode); - } - - bool InventoryConfigApplyResult::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj, "appliedConfiguration", appliedConfiguration); - field_from_json( Obj, "warnings", warnings); - field_from_json( Obj, "errors", errors); - field_from_json( Obj, "errorCode", errorCode); - return true; - } catch (...) { - - } - return false; - } - - void InventoryTagList::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"taglist",taglist); - } - - bool InventoryTagList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"taglist",taglist); - return true; - } catch (...) { - - } - return false; - }; - - void DeviceConfigurationElement::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"name", name); - field_to_json( Obj,"description", description); - field_to_json( Obj,"weight", weight); - field_to_json( Obj,"configuration", configuration); - } - - bool DeviceConfigurationElement::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"name",name); - field_from_json( Obj,"description",description); - field_from_json( Obj,"weight",weight); - field_from_json( Obj,"configuration",configuration); - return true; - } catch(...) { - - } - return false; - } - - void DeviceConfiguration::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"managementPolicy",managementPolicy); - field_to_json( Obj,"deviceTypes",deviceTypes); - field_to_json( Obj,"subscriberOnly",subscriberOnly); - field_to_json( Obj,"entity", entity); - field_to_json( Obj,"venue", venue); - field_to_json( Obj,"subscriber", subscriber); - field_to_json( Obj,"configuration",configuration); - field_to_json( Obj,"inUse",inUse); - field_to_json( Obj,"variables",variables); - field_to_json( Obj,"deviceRules",deviceRules); - } - - bool DeviceConfiguration::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"managementPolicy",managementPolicy); - field_from_json( Obj,"deviceTypes",deviceTypes); - field_from_json( Obj,"inUse",inUse); - field_from_json( Obj,"variables",variables); - field_from_json( Obj,"subscriberOnly",subscriberOnly); - field_from_json( Obj,"entity", entity); - field_from_json( Obj,"venue", venue); - field_from_json( Obj,"subscriber", subscriber); - field_from_json( Obj,"configuration",configuration); - field_from_json( Obj,"deviceRules",deviceRules); - return true; - } catch(...) { - - } - return false; - } - - void Report::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "snapshot", snapShot); - field_to_json(Obj, "devices", tenants); - }; - - void Report::reset() { - tenants.clear(); - } - - void ExpandedUseEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "uuid", uuid); - field_to_json(Obj, "name", name); - field_to_json(Obj, "description", description); - } - - bool ExpandedUseEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"uuid",uuid); - field_from_json( Obj,"name",name); - field_from_json( Obj,"description",description); - return true; - } catch(...) { - - } - return false; - } - - void ExpandedUseEntryList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "type", type); - field_to_json(Obj, "entries", entries); - } - - bool ExpandedUseEntryList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"type",type); - field_from_json( Obj,"entries",entries); - return true; - } catch(...) { - - } - return false; - } - - void ExpandedUseEntryMapList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "entries", entries); - } - - bool ExpandedUseEntryMapList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"entries",entries); - return true; - } catch(...) { - - } - return false; - } - - void UuidList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "list", list); - } - - bool UuidList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "list", list); - return true; - } catch(...) { - - } - return false; - } - - void ObjectACL::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "users", users); - field_to_json(Obj, "roles", roles); - field_to_json(Obj, "access", access); - } - - bool ObjectACL::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "users", users); - field_from_json(Obj, "roles", roles); - field_from_json(Obj, "access", access); - return true; - } catch(...) { - - } - return false; - } - - void ObjectACLList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "list", list); - } - - bool ObjectACLList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "list", list); - return true; - } catch(...) { - - } - return false; - } - - void Map::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"data",data); - field_to_json( Obj,"entity",entity); - field_to_json( Obj,"creator",creator); - field_to_json( Obj,"visibility",visibility); - field_to_json( Obj,"access",access); - field_to_json( Obj,"managementPolicy", managementPolicy); - field_to_json( Obj,"venue", venue); - } - - bool Map::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - RESTAPI_utils::field_from_json( Obj,"data",data); - RESTAPI_utils::field_from_json( Obj,"entity",entity); - RESTAPI_utils::field_from_json( Obj,"creator",creator); - RESTAPI_utils::field_from_json( Obj,"visibility",visibility); - RESTAPI_utils::field_from_json( Obj,"access",access); - RESTAPI_utils::field_from_json( Obj,"managementPolicy", managementPolicy); - RESTAPI_utils::field_from_json( Obj,"venue", venue); - return true; - } catch(...) { - - } - return false; - } - - void SerialNumberList::to_json(Poco::JSON::Object &Obj) const { - RESTAPI_utils::field_to_json( Obj,"serialNumbers",serialNumbers); - } - - bool SerialNumberList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - RESTAPI_utils::field_from_json( Obj,"serialNumbers",serialNumbers); - return true; - } catch(...) { - - } - return false; - } - - void MapList::to_json(Poco::JSON::Object &Obj) const { - RESTAPI_utils::field_to_json( Obj,"list",list); - } - - bool MapList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - RESTAPI_utils::field_from_json( Obj,"list",list); - return true; - } catch(...) { - - } - return false; - } - - void SignupEntry::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"email", email); - field_to_json( Obj,"userId", userId); - field_to_json( Obj,"macAddress", macAddress); - field_to_json( Obj,"serialNumber", serialNumber); - field_to_json( Obj,"submitted", submitted); - field_to_json( Obj,"completed", completed); - field_to_json( Obj,"status", status); - field_to_json( Obj,"error", error); - field_to_json( Obj,"statusCode", statusCode); - field_to_json( Obj,"deviceID", deviceID); - field_to_json( Obj,"registrationId",registrationId); - field_to_json( Obj,"operatorId",operatorId); - } - - bool SignupEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"email", email); - field_from_json( Obj,"userId", userId); - field_from_json( Obj,"macAddress", macAddress); - field_from_json( Obj,"serialNumber", serialNumber); - field_from_json( Obj,"submitted", submitted); - field_from_json( Obj,"completed", completed); - field_from_json( Obj,"status", status); - field_from_json( Obj,"error", error); - field_from_json( Obj,"statusCode", statusCode); - field_from_json( Obj,"deviceID", deviceID); - field_from_json( Obj,"registrationId",registrationId); - field_from_json( Obj,"operatorId",operatorId); - return true; - } catch(...) { - - } - return false; - } - - void Variable::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"type", type); - field_to_json( Obj,"weight", weight); - field_to_json( Obj,"prefix", prefix); - field_to_json( Obj,"value", value); - } - - bool Variable::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"type", type); - field_from_json( Obj,"weight", weight); - field_from_json( Obj,"prefix", prefix); - field_from_json( Obj,"value", value); - return true; - } catch(...) { - - } - return false; - } - - void VariableList::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"variables", variables); - } - - bool VariableList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"variables", variables); - return true; - } catch(...) { - - } - return false; - } - - void VariableBlock::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"variables", variables); - field_to_json( Obj,"entity", entity); - field_to_json( Obj,"venue", venue); - field_to_json( Obj,"subscriber", subscriber); - field_to_json( Obj,"inventory", inventory); - field_to_json( Obj,"configurations", configurations); - field_to_json( Obj,"managementPolicy", managementPolicy); - } - - bool VariableBlock::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"variables", variables); - field_from_json( Obj,"entity", entity); - field_from_json( Obj,"venue", venue); - field_from_json( Obj,"subscriber", subscriber); - field_from_json( Obj,"inventory", inventory); - field_from_json( Obj,"configurations", configurations); - field_from_json( Obj,"managementPolicy", managementPolicy); - return true; - } catch(...) { - } - return false; - } - - void VariableBlockList::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"variableBlocks", variableBlocks); - } - - bool VariableBlockList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"variableBlocks", variableBlocks); - return true; - } catch(...) { - - } - return false; - } - - void ConfigurationDetails::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"configuration", configuration); - field_to_json( Obj,"rrm", rrm); - field_to_json( Obj,"firmwareRCOnly", firmwareRCOnly); - field_to_json( Obj,"firmwareUpgrade", firmwareUpgrade); - } - - bool ConfigurationDetails::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"configuration", configuration); - field_from_json( Obj,"rrm", rrm); - field_from_json( Obj,"firmwareRCOnly", firmwareRCOnly); - field_from_json( Obj,"firmwareUpgrade", firmwareUpgrade); - return true; - } catch(...) { - - } - return false; - } - - void SubscriberDevice::to_json(Poco::JSON::Object &Obj) const { - info.to_json(Obj); - field_to_json( Obj,"serialNumber", serialNumber); - field_to_json( Obj,"deviceType", deviceType); - field_to_json( Obj,"operatorId", operatorId); - field_to_json( Obj,"subscriberId", subscriberId); - field_to_json( Obj,"location", location); - field_to_json( Obj,"contact", contact); - field_to_json( Obj,"managementPolicy", managementPolicy); - field_to_json( Obj,"serviceClass", serviceClass); - field_to_json( Obj,"qrCode", qrCode); - field_to_json( Obj,"geoCode", geoCode); - field_to_json( Obj,"deviceRules",deviceRules); - field_to_json( Obj,"state", state); - field_to_json( Obj,"locale", locale); - field_to_json( Obj,"billingCode", billingCode); - field_to_json( Obj,"configuration", configuration); - field_to_json( Obj,"suspended", suspended); - field_to_json( Obj,"realMacAddress", realMacAddress); - } - - bool SubscriberDevice::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - info.from_json(Obj); - field_from_json( Obj,"serialNumber", serialNumber); - field_from_json( Obj,"deviceType", deviceType); - field_from_json( Obj,"operatorId", operatorId); - field_from_json( Obj,"subscriberId", subscriberId); - field_from_json( Obj,"location", location); - field_from_json( Obj,"contact", contact); - field_from_json( Obj,"managementPolicy", managementPolicy); - field_from_json( Obj,"serviceClass", serviceClass); - field_from_json( Obj,"qrCode", qrCode); - field_from_json( Obj,"geoCode", geoCode); - field_from_json( Obj,"deviceRules",deviceRules); - field_from_json( Obj,"state", state); - field_from_json( Obj,"locale", locale); - field_from_json( Obj,"billingCode", billingCode); - field_from_json( Obj,"configuration", configuration); - field_from_json( Obj,"suspended", suspended); - field_from_json( Obj,"realMacAddress", realMacAddress); - return true; - } catch(...) { - - } - return false; - } - - void SubscriberDeviceList::to_json(Poco::JSON::Object &Obj) const { - field_to_json( Obj,"subscriberDevices", subscriberDevices); - } - - bool SubscriberDeviceList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json( Obj,"subscriberDevices", subscriberDevices); - return true; - } catch(...) { - - } - return false; - } - - void VenueDeviceList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id",id); - field_to_json(Obj,"name",name); - field_to_json(Obj,"description",description); - field_to_json(Obj,"devices",devices); - } - - bool VenueDeviceList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id",id); - field_from_json(Obj,"name",name); - field_from_json(Obj,"description",description); - field_from_json(Obj,"devices",devices); - return true; - } catch(...) { - - } - return false; - } - - bool UpdateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, ObjectInfo &I) { - uint64_t Now = Utils::Now(); - if(O->has("name")) - I.name = O->get("name").toString(); - - if(I.name.empty()) - return false; - - if(O->has("description")) - I.description = O->get("description").toString(); - SecurityObjects::MergeNotes(O,U,I.notes); - SecurityObjects::NoteInfoVec N; - for(auto &i:I.notes) { - if(i.note.empty()) - continue; - N.push_back(SecurityObjects::NoteInfo{.created=Now,.createdBy=U.email,.note=i.note}); - } - I.modified = Now; - return true; - } - - bool CreateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, ObjectInfo &I) { - uint64_t Now = Utils::Now(); - if(O->has("name")) - I.name = O->get("name").toString(); - - if(I.name.empty()) - return false; - - if(O->has("description")) - I.description = O->get("description").toString(); - - SecurityObjects::NoteInfoVec N; - for(auto &i:I.notes) { - if(i.note.empty()) - continue; - N.push_back(SecurityObjects::NoteInfo{.created=Now,.createdBy=U.email,.note=i.note}); - } - I.notes = N; - I.modified = I.created = Now; - I.id = MicroServiceCreateUUID(); - - return true; - } - - bool CreateObjectInfo([[maybe_unused]] const SecurityObjects::UserInfo &U, ObjectInfo &I) { - I.modified = I.created = Utils::Now(); - I.id = MicroServiceCreateUUID(); - return true; - } - - void DeviceRules::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"rcOnly",rcOnly); - field_to_json(Obj,"rrm",rrm); - field_to_json(Obj,"firmwareUpgrade",firmwareUpgrade); - } - - bool DeviceRules::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"rcOnly",rcOnly); - field_from_json(Obj,"rrm",rrm); - field_from_json(Obj,"firmwareUpgrade",firmwareUpgrade); - return true; - } catch(...) { - - } - return false; - } - - void RRMAlgorithmDetails::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"name",name); - field_to_json(Obj,"parameters",parameters); - } - - bool RRMAlgorithmDetails::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"name",name); - field_from_json(Obj,"parameters",parameters); - return true; - } catch(...) { - - } - return false; - } - - void RRMDetails::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"vendor",vendor); - field_to_json(Obj,"schedule",schedule); - field_to_json(Obj,"algorithms",algorithms); - } - - bool RRMDetails::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"vendor",vendor); - field_from_json(Obj,"schedule",schedule); - field_from_json(Obj,"algorithms",algorithms); - return true; - } catch(...) { - - } - return false; - } - - void ConfigurationOverride::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"source",source); - field_to_json(Obj,"reason",reason); - field_to_json(Obj,"parameterName",parameterName); - field_to_json(Obj,"parameterType",parameterType); - field_to_json(Obj,"parameterValue",parameterValue); - field_to_json(Obj,"modified",modified); - } - - bool ConfigurationOverride::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"source",source); - field_from_json(Obj,"reason",reason); - field_from_json(Obj,"parameterName",parameterName); - field_from_json(Obj,"parameterType",parameterType); - field_from_json(Obj,"parameterValue",parameterValue); - field_from_json(Obj,"modified",modified); - return true; - } catch(...) { - - } - return false; - } - - void ConfigurationOverrideList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"serialNumber",serialNumber); - field_to_json(Obj,"managementPolicy",managementPolicy); - field_to_json(Obj,"overrides",overrides); - } - - bool ConfigurationOverrideList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"serialNumber",serialNumber); - field_from_json(Obj,"managementPolicy",managementPolicy); - field_from_json(Obj,"overrides",overrides); - return true; - } catch(...) { - - } - return false; - } - -} - - + void ObjectInfo::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + field_to_json(Obj, "notes", notes); + field_to_json(Obj, "tags", tags); + } + + bool ObjectInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + field_from_json(Obj, "notes", notes); + field_from_json(Obj, "tags", tags); + return true; + } catch (...) { + } + return false; + } + + void ManagementPolicyEntry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "users", users); + field_to_json(Obj, "resources", resources); + field_to_json(Obj, "access", access); + field_to_json(Obj, "policy", policy); + } + + bool ManagementPolicyEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "users", users); + field_from_json(Obj, "resources", resources); + field_from_json(Obj, "access", access); + field_from_json(Obj, "policy", policy); + return true; + } catch (...) { + } + return false; + } + + void ManagementPolicy::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "entries", entries); + field_to_json(Obj, "inUse", inUse); + field_to_json(Obj, "entity", entity); + } + + bool ManagementPolicy::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "entries", entries); + field_from_json(Obj, "inUse", inUse); + field_from_json(Obj, "entity", entity); + return true; + } catch (...) { + } + return false; + } + + void Entity::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "parent", parent); + field_to_json(Obj, "venues", venues); + field_to_json(Obj, "children", children); + field_to_json(Obj, "contacts", contacts); + field_to_json(Obj, "locations", locations); + field_to_json(Obj, "managementPolicy", managementPolicy); + field_to_json(Obj, "deviceConfiguration", deviceConfiguration); + field_to_json(Obj, "devices", devices); + field_to_json(Obj, "deviceRules", deviceRules); + field_to_json(Obj, "sourceIP", sourceIP); + field_to_json(Obj, "variables", variables); + field_to_json(Obj, "managementPolicies", managementPolicies); + field_to_json(Obj, "managementRoles", managementRoles); + field_to_json(Obj, "maps", maps); + field_to_json(Obj, "configurations", configurations); + } + + bool Entity::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "parent", parent); + field_from_json(Obj, "venues", venues); + field_from_json(Obj, "children", children); + field_from_json(Obj, "contacts", contacts); + field_from_json(Obj, "locations", locations); + field_from_json(Obj, "managementPolicy", managementPolicy); + field_from_json(Obj, "deviceConfiguration", deviceConfiguration); + field_from_json(Obj, "devices", devices); + field_from_json(Obj, "deviceRules", deviceRules); + field_from_json(Obj, "sourceIP", sourceIP); + field_from_json(Obj, "variables", variables); + field_from_json(Obj, "managementPolicies", managementPolicies); + field_from_json(Obj, "managementRoles", managementRoles); + field_from_json(Obj, "maps", maps); + field_from_json(Obj, "configurations", configurations); + return true; + } catch (...) { + } + return false; + } + + void DiGraphEntry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "parent", parent); + field_to_json(Obj, "child", child); + } + + bool DiGraphEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "parent", parent); + field_from_json(Obj, "child", child); + return true; + } catch (...) { + } + return false; + } + + void Venue::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "parent", parent); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "children", children); + field_to_json(Obj, "devices", devices); + field_to_json(Obj, "topology", topology); + field_to_json(Obj, "parent", parent); + field_to_json(Obj, "design", design); + field_to_json(Obj, "managementPolicy", managementPolicy); + field_to_json(Obj, "deviceConfiguration", deviceConfiguration); + field_to_json(Obj, "contacts", contacts); + field_to_json(Obj, "location", location); + field_to_json(Obj, "deviceRules", deviceRules); + field_to_json(Obj, "sourceIP", sourceIP); + field_to_json(Obj, "variables", variables); + field_to_json(Obj, "managementPolicies", managementPolicies); + field_to_json(Obj, "managementRoles", managementRoles); + field_to_json(Obj, "maps", maps); + field_to_json(Obj, "configurations", configurations); + field_to_json(Obj, "boards", boards); + } + + bool Venue::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "parent", parent); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "children", children); + field_from_json(Obj, "devices", devices); + field_from_json(Obj, "topology", topology); + field_from_json(Obj, "parent", parent); + field_from_json(Obj, "design", design); + field_from_json(Obj, "managementPolicy", managementPolicy); + field_from_json(Obj, "deviceConfiguration", deviceConfiguration); + field_from_json(Obj, "contacts", contacts); + field_from_json(Obj, "location", location); + field_from_json(Obj, "deviceRules", deviceRules); + field_from_json(Obj, "sourceIP", sourceIP); + field_from_json(Obj, "variables", variables); + field_from_json(Obj, "managementPolicies", managementPolicies); + field_from_json(Obj, "managementRoles", managementRoles); + field_from_json(Obj, "maps", maps); + field_from_json(Obj, "configurations", configurations); + field_from_json(Obj, "boards", boards); + return true; + } catch (...) { + } + return false; + } + + void Operator::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "managementPolicy", managementPolicy); + field_to_json(Obj, "managementRoles", managementRoles); + field_to_json(Obj, "deviceRules", deviceRules); + field_to_json(Obj, "variables", variables); + field_to_json(Obj, "defaultOperator", defaultOperator); + field_to_json(Obj, "sourceIP", sourceIP); + field_to_json(Obj, "registrationId", registrationId); + } + + bool Operator::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "managementPolicy", managementPolicy); + field_from_json(Obj, "managementRoles", managementRoles); + field_from_json(Obj, "deviceRules", deviceRules); + field_from_json(Obj, "variables", variables); + field_from_json(Obj, "defaultOperator", defaultOperator); + field_from_json(Obj, "sourceIP", sourceIP); + field_from_json(Obj, "registrationId", registrationId); + return true; + } catch (...) { + } + return false; + } + + void OperatorList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "operators", operators); + } + + bool OperatorList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "operators", operators); + return true; + } catch (...) { + } + return false; + } + + void ServiceClass::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "operatorId", operatorId); + field_to_json(Obj, "managementPolicy", managementPolicy); + field_to_json(Obj, "cost", cost); + field_to_json(Obj, "currency", currency); + field_to_json(Obj, "period", period); + field_to_json(Obj, "billingCode", billingCode); + field_to_json(Obj, "variables", variables); + field_to_json(Obj, "defaultService", defaultService); + } + + bool ServiceClass::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "operatorId", operatorId); + field_from_json(Obj, "managementPolicy", managementPolicy); + field_from_json(Obj, "cost", cost); + field_from_json(Obj, "currency", currency); + field_from_json(Obj, "period", period); + field_from_json(Obj, "billingCode", billingCode); + field_from_json(Obj, "variables", variables); + field_from_json(Obj, "defaultService", defaultService); + return true; + } catch (...) { + } + return false; + } + + void ServiceClassList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "serviceClasses", serviceClasses); + } + + bool ServiceClassList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "serviceClasses", serviceClasses); + return true; + } catch (...) { + } + return false; + } + + void UserInfoDigest::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "entity", loginId); + field_to_json(Obj, "children", userType); + } + + bool UserInfoDigest::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "entity", loginId); + field_from_json(Obj, "children", userType); + return true; + } catch (...) { + } + return false; + } + + void ManagementRole::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "managementPolicy", managementPolicy); + field_to_json(Obj, "users", users); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "venue", venue); + } + + bool ManagementRole::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "managementPolicy", managementPolicy); + field_from_json(Obj, "users", users); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "venue", venue); + return true; + } catch (...) { + } + return false; + } + + void Location::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "type", OpenWifi::ProvObjects::to_string(type)); + field_to_json(Obj, "buildingName", buildingName); + field_to_json(Obj, "addressLines", addressLines); + field_to_json(Obj, "city", city); + field_to_json(Obj, "state", state); + field_to_json(Obj, "postal", postal); + field_to_json(Obj, "country", country); + field_to_json(Obj, "phones", phones); + field_to_json(Obj, "mobiles", mobiles); + field_to_json(Obj, "geoCode", geoCode); + field_to_json(Obj, "inUse", inUse); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "managementPolicy", managementPolicy); + } + + bool Location::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + std::string tmp_type; + field_from_json(Obj, "type", tmp_type); + type = location_from_string(tmp_type); + field_from_json(Obj, "buildingName", buildingName); + field_from_json(Obj, "addressLines", addressLines); + field_from_json(Obj, "city", city); + field_from_json(Obj, "state", state); + field_from_json(Obj, "postal", postal); + field_from_json(Obj, "country", country); + field_from_json(Obj, "phones", phones); + field_from_json(Obj, "mobiles", mobiles); + field_from_json(Obj, "geoCode", geoCode); + field_from_json(Obj, "inUse", inUse); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "managementPolicy", managementPolicy); + return true; + } catch (...) { + } + return false; + } + + void OperatorLocation::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "type", type); + field_to_json(Obj, "buildingName", buildingName); + field_to_json(Obj, "addressLines", addressLines); + field_to_json(Obj, "city", city); + field_to_json(Obj, "state", state); + field_to_json(Obj, "postal", postal); + field_to_json(Obj, "country", country); + field_to_json(Obj, "phones", phones); + field_to_json(Obj, "mobiles", mobiles); + field_to_json(Obj, "geoCode", geoCode); + field_to_json(Obj, "operatorId", operatorId); + field_to_json(Obj, "subscriberDeviceId", subscriberDeviceId); + field_to_json(Obj, "managementPolicy", managementPolicy); + } + + bool OperatorLocation::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "type", type); + field_from_json(Obj, "buildingName", buildingName); + field_from_json(Obj, "addressLines", addressLines); + field_from_json(Obj, "city", city); + field_from_json(Obj, "state", state); + field_from_json(Obj, "postal", postal); + field_from_json(Obj, "country", country); + field_from_json(Obj, "phones", phones); + field_from_json(Obj, "mobiles", mobiles); + field_from_json(Obj, "geoCode", geoCode); + field_from_json(Obj, "operatorId", operatorId); + field_from_json(Obj, "subscriberDeviceId", subscriberDeviceId); + field_from_json(Obj, "managementPolicy", managementPolicy); + return true; + } catch (...) { + } + return false; + } + + void SubLocation::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "type", type); + field_to_json(Obj, "buildingName", buildingName); + field_to_json(Obj, "addressLines", addressLines); + field_to_json(Obj, "city", city); + field_to_json(Obj, "state", state); + field_to_json(Obj, "postal", postal); + field_to_json(Obj, "country", country); + field_to_json(Obj, "phones", phones); + field_to_json(Obj, "mobiles", mobiles); + field_to_json(Obj, "geoCode", geoCode); + } + + bool SubLocation::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "type", type); + field_from_json(Obj, "buildingName", buildingName); + field_from_json(Obj, "addressLines", addressLines); + field_from_json(Obj, "city", city); + field_from_json(Obj, "state", state); + field_from_json(Obj, "postal", postal); + field_from_json(Obj, "country", country); + field_from_json(Obj, "phones", phones); + field_from_json(Obj, "mobiles", mobiles); + field_from_json(Obj, "geoCode", geoCode); + return true; + } catch (...) { + } + return false; + } + + void OperatorLocationList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "locations", locations); + } + + bool OperatorLocationList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "locations", locations); + return true; + } catch (...) { + } + return false; + } + + void Contact::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "type", to_string(type)); + field_to_json(Obj, "title", title); + field_to_json(Obj, "salutation", salutation); + field_to_json(Obj, "firstname", firstname); + field_to_json(Obj, "lastname", lastname); + field_to_json(Obj, "initials", initials); + field_to_json(Obj, "visual", visual); + field_to_json(Obj, "mobiles", mobiles); + field_to_json(Obj, "phones", phones); + field_to_json(Obj, "primaryEmail", primaryEmail); + field_to_json(Obj, "secondaryEmail", secondaryEmail); + field_to_json(Obj, "accessPIN", accessPIN); + field_to_json(Obj, "inUse", inUse); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "managementPolicy", managementPolicy); + } + + bool Contact::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + std::string tmp_type; + field_from_json(Obj, "type", tmp_type); + type = contact_from_string(tmp_type); + field_from_json(Obj, "title", title); + field_from_json(Obj, "salutation", salutation); + field_from_json(Obj, "firstname", firstname); + field_from_json(Obj, "lastname", lastname); + field_from_json(Obj, "initials", initials); + field_from_json(Obj, "visual", visual); + field_from_json(Obj, "mobiles", mobiles); + field_from_json(Obj, "phones", phones); + field_from_json(Obj, "primaryEmail", primaryEmail); + field_from_json(Obj, "secondaryEmail", secondaryEmail); + field_from_json(Obj, "accessPIN", accessPIN); + field_from_json(Obj, "inUse", inUse); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "managementPolicy", managementPolicy); + return true; + } catch (...) { + } + return false; + } + + void OperatorContact::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "type", type); + field_to_json(Obj, "title", title); + field_to_json(Obj, "salutation", salutation); + field_to_json(Obj, "firstname", firstname); + field_to_json(Obj, "lastname", lastname); + field_to_json(Obj, "initials", initials); + field_to_json(Obj, "visual", visual); + field_to_json(Obj, "mobiles", mobiles); + field_to_json(Obj, "phones", phones); + field_to_json(Obj, "primaryEmail", primaryEmail); + field_to_json(Obj, "secondaryEmail", secondaryEmail); + field_to_json(Obj, "accessPIN", accessPIN); + field_to_json(Obj, "operatorId", operatorId); + field_to_json(Obj, "subscriberDeviceId", subscriberDeviceId); + field_to_json(Obj, "managementPolicy", managementPolicy); + } + + bool OperatorContact::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "type", type); + field_from_json(Obj, "title", title); + field_from_json(Obj, "salutation", salutation); + field_from_json(Obj, "firstname", firstname); + field_from_json(Obj, "lastname", lastname); + field_from_json(Obj, "initials", initials); + field_from_json(Obj, "visual", visual); + field_from_json(Obj, "mobiles", mobiles); + field_from_json(Obj, "phones", phones); + field_from_json(Obj, "primaryEmail", primaryEmail); + field_from_json(Obj, "secondaryEmail", secondaryEmail); + field_from_json(Obj, "accessPIN", accessPIN); + field_from_json(Obj, "operatorId", operatorId); + field_from_json(Obj, "subscriberDeviceId", subscriberDeviceId); + field_from_json(Obj, "managementPolicy", managementPolicy); + return true; + } catch (...) { + } + return false; + } + + void SubContact::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "type", type); + field_to_json(Obj, "title", title); + field_to_json(Obj, "salutation", salutation); + field_to_json(Obj, "firstname", firstname); + field_to_json(Obj, "lastname", lastname); + field_to_json(Obj, "initials", initials); + field_to_json(Obj, "visual", visual); + field_to_json(Obj, "mobiles", mobiles); + field_to_json(Obj, "phones", phones); + field_to_json(Obj, "primaryEmail", primaryEmail); + field_to_json(Obj, "secondaryEmail", secondaryEmail); + field_to_json(Obj, "accessPIN", accessPIN); + } + + bool SubContact::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "type", type); + field_from_json(Obj, "title", title); + field_from_json(Obj, "salutation", salutation); + field_from_json(Obj, "firstname", firstname); + field_from_json(Obj, "lastname", lastname); + field_from_json(Obj, "initials", initials); + field_from_json(Obj, "visual", visual); + field_from_json(Obj, "mobiles", mobiles); + field_from_json(Obj, "phones", phones); + field_from_json(Obj, "primaryEmail", primaryEmail); + field_from_json(Obj, "secondaryEmail", secondaryEmail); + field_from_json(Obj, "accessPIN", accessPIN); + return true; + } catch (...) { + } + return false; + } + + void OperatorContactList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "contacts", contacts); + } + + bool OperatorContactList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "contacts", contacts); + return true; + } catch (...) { + } + return false; + } + + void InventoryTag::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "venue", venue); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "subscriber", subscriber); + field_to_json(Obj, "deviceType", deviceType); + field_to_json(Obj, "qrCode", qrCode); + field_to_json(Obj, "geoCode", geoCode); + field_to_json(Obj, "location", location); + field_to_json(Obj, "contact", contact); + field_to_json(Obj, "deviceConfiguration", deviceConfiguration); + field_to_json(Obj, "deviceRules", deviceRules); + field_to_json(Obj, "managementPolicy", managementPolicy); + field_to_json(Obj, "state", state); + field_to_json(Obj, "devClass", devClass); + field_to_json(Obj, "locale", locale); + field_to_json(Obj, "realMacAddress", realMacAddress); + field_to_json(Obj, "doNotAllowOverrides", doNotAllowOverrides); + } + + bool InventoryTag::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "venue", venue); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "subscriber", subscriber); + field_from_json(Obj, "deviceType", deviceType); + field_from_json(Obj, "qrCode", qrCode); + field_from_json(Obj, "geoCode", geoCode); + field_from_json(Obj, "location", location); + field_from_json(Obj, "contact", contact); + field_from_json(Obj, "deviceConfiguration", deviceConfiguration); + field_from_json(Obj, "deviceRules", deviceRules); + field_from_json(Obj, "managementPolicy", managementPolicy); + field_from_json(Obj, "state", state); + field_from_json(Obj, "devClass", devClass); + field_from_json(Obj, "locale", locale); + field_from_json(Obj, "realMacAddress", realMacAddress); + field_from_json(Obj, "doNotAllowOverrides", doNotAllowOverrides); + return true; + } catch (...) { + } + return false; + } + + void InventoryConfigApplyResult::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "appliedConfiguration", appliedConfiguration); + field_to_json(Obj, "warnings", warnings); + field_to_json(Obj, "errors", errors); + field_to_json(Obj, "errorCode", errorCode); + } + + bool InventoryConfigApplyResult::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "appliedConfiguration", appliedConfiguration); + field_from_json(Obj, "warnings", warnings); + field_from_json(Obj, "errors", errors); + field_from_json(Obj, "errorCode", errorCode); + return true; + } catch (...) { + } + return false; + } + + void InventoryTagList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "taglist", taglist); + } + + bool InventoryTagList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "taglist", taglist); + return true; + } catch (...) { + } + return false; + }; + + void DeviceConfigurationElement::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "weight", weight); + field_to_json(Obj, "configuration", configuration); + } + + bool DeviceConfigurationElement::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "weight", weight); + field_from_json(Obj, "configuration", configuration); + return true; + } catch (...) { + } + return false; + } + + void DeviceConfiguration::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "managementPolicy", managementPolicy); + field_to_json(Obj, "deviceTypes", deviceTypes); + field_to_json(Obj, "subscriberOnly", subscriberOnly); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "venue", venue); + field_to_json(Obj, "subscriber", subscriber); + field_to_json(Obj, "configuration", configuration); + field_to_json(Obj, "inUse", inUse); + field_to_json(Obj, "variables", variables); + field_to_json(Obj, "deviceRules", deviceRules); + } + + bool DeviceConfiguration::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "managementPolicy", managementPolicy); + field_from_json(Obj, "deviceTypes", deviceTypes); + field_from_json(Obj, "inUse", inUse); + field_from_json(Obj, "variables", variables); + field_from_json(Obj, "subscriberOnly", subscriberOnly); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "venue", venue); + field_from_json(Obj, "subscriber", subscriber); + field_from_json(Obj, "configuration", configuration); + field_from_json(Obj, "deviceRules", deviceRules); + return true; + } catch (...) { + } + return false; + } + + void Report::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "snapshot", snapShot); + field_to_json(Obj, "devices", tenants); + }; + + void Report::reset() { tenants.clear(); } + + void ExpandedUseEntry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "uuid", uuid); + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + } + + bool ExpandedUseEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "uuid", uuid); + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + return true; + } catch (...) { + } + return false; + } + + void ExpandedUseEntryList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "type", type); + field_to_json(Obj, "entries", entries); + } + + bool ExpandedUseEntryList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "type", type); + field_from_json(Obj, "entries", entries); + return true; + } catch (...) { + } + return false; + } + + void ExpandedUseEntryMapList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "entries", entries); + } + + bool ExpandedUseEntryMapList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "entries", entries); + return true; + } catch (...) { + } + return false; + } + + void UuidList::to_json(Poco::JSON::Object &Obj) const { field_to_json(Obj, "list", list); } + + bool UuidList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "list", list); + return true; + } catch (...) { + } + return false; + } + + void ObjectACL::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "users", users); + field_to_json(Obj, "roles", roles); + field_to_json(Obj, "access", access); + } + + bool ObjectACL::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "users", users); + field_from_json(Obj, "roles", roles); + field_from_json(Obj, "access", access); + return true; + } catch (...) { + } + return false; + } + + void ObjectACLList::to_json(Poco::JSON::Object &Obj) const { field_to_json(Obj, "list", list); } + + bool ObjectACLList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "list", list); + return true; + } catch (...) { + } + return false; + } + + void Map::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "data", data); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "creator", creator); + field_to_json(Obj, "visibility", visibility); + field_to_json(Obj, "access", access); + field_to_json(Obj, "managementPolicy", managementPolicy); + field_to_json(Obj, "venue", venue); + } + + bool Map::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + RESTAPI_utils::field_from_json(Obj, "data", data); + RESTAPI_utils::field_from_json(Obj, "entity", entity); + RESTAPI_utils::field_from_json(Obj, "creator", creator); + RESTAPI_utils::field_from_json(Obj, "visibility", visibility); + RESTAPI_utils::field_from_json(Obj, "access", access); + RESTAPI_utils::field_from_json(Obj, "managementPolicy", managementPolicy); + RESTAPI_utils::field_from_json(Obj, "venue", venue); + return true; + } catch (...) { + } + return false; + } + + void SerialNumberList::to_json(Poco::JSON::Object &Obj) const { + RESTAPI_utils::field_to_json(Obj, "serialNumbers", serialNumbers); + } + + bool SerialNumberList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + RESTAPI_utils::field_from_json(Obj, "serialNumbers", serialNumbers); + return true; + } catch (...) { + } + return false; + } + + void MapList::to_json(Poco::JSON::Object &Obj) const { + RESTAPI_utils::field_to_json(Obj, "list", list); + } + + bool MapList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + RESTAPI_utils::field_from_json(Obj, "list", list); + return true; + } catch (...) { + } + return false; + } + + void SignupEntry::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "email", email); + field_to_json(Obj, "userId", userId); + field_to_json(Obj, "macAddress", macAddress); + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "submitted", submitted); + field_to_json(Obj, "completed", completed); + field_to_json(Obj, "status", status); + field_to_json(Obj, "error", error); + field_to_json(Obj, "statusCode", statusCode); + field_to_json(Obj, "deviceID", deviceID); + field_to_json(Obj, "registrationId", registrationId); + field_to_json(Obj, "operatorId", operatorId); + } + + bool SignupEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "email", email); + field_from_json(Obj, "userId", userId); + field_from_json(Obj, "macAddress", macAddress); + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "submitted", submitted); + field_from_json(Obj, "completed", completed); + field_from_json(Obj, "status", status); + field_from_json(Obj, "error", error); + field_from_json(Obj, "statusCode", statusCode); + field_from_json(Obj, "deviceID", deviceID); + field_from_json(Obj, "registrationId", registrationId); + field_from_json(Obj, "operatorId", operatorId); + return true; + } catch (...) { + } + return false; + } + + void Variable::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "type", type); + field_to_json(Obj, "weight", weight); + field_to_json(Obj, "prefix", prefix); + field_to_json(Obj, "value", value); + } + + bool Variable::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "type", type); + field_from_json(Obj, "weight", weight); + field_from_json(Obj, "prefix", prefix); + field_from_json(Obj, "value", value); + return true; + } catch (...) { + } + return false; + } + + void VariableList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "variables", variables); + } + + bool VariableList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "variables", variables); + return true; + } catch (...) { + } + return false; + } + + void VariableBlock::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "variables", variables); + field_to_json(Obj, "entity", entity); + field_to_json(Obj, "venue", venue); + field_to_json(Obj, "subscriber", subscriber); + field_to_json(Obj, "inventory", inventory); + field_to_json(Obj, "configurations", configurations); + field_to_json(Obj, "managementPolicy", managementPolicy); + } + + bool VariableBlock::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "variables", variables); + field_from_json(Obj, "entity", entity); + field_from_json(Obj, "venue", venue); + field_from_json(Obj, "subscriber", subscriber); + field_from_json(Obj, "inventory", inventory); + field_from_json(Obj, "configurations", configurations); + field_from_json(Obj, "managementPolicy", managementPolicy); + return true; + } catch (...) { + } + return false; + } + + void VariableBlockList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "variableBlocks", variableBlocks); + } + + bool VariableBlockList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "variableBlocks", variableBlocks); + return true; + } catch (...) { + } + return false; + } + + void ConfigurationDetails::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "configuration", configuration); + field_to_json(Obj, "rrm", rrm); + field_to_json(Obj, "firmwareRCOnly", firmwareRCOnly); + field_to_json(Obj, "firmwareUpgrade", firmwareUpgrade); + } + + bool ConfigurationDetails::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "configuration", configuration); + field_from_json(Obj, "rrm", rrm); + field_from_json(Obj, "firmwareRCOnly", firmwareRCOnly); + field_from_json(Obj, "firmwareUpgrade", firmwareUpgrade); + return true; + } catch (...) { + } + return false; + } + + void SubscriberDevice::to_json(Poco::JSON::Object &Obj) const { + info.to_json(Obj); + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "deviceType", deviceType); + field_to_json(Obj, "operatorId", operatorId); + field_to_json(Obj, "subscriberId", subscriberId); + field_to_json(Obj, "location", location); + field_to_json(Obj, "contact", contact); + field_to_json(Obj, "managementPolicy", managementPolicy); + field_to_json(Obj, "serviceClass", serviceClass); + field_to_json(Obj, "qrCode", qrCode); + field_to_json(Obj, "geoCode", geoCode); + field_to_json(Obj, "deviceRules", deviceRules); + field_to_json(Obj, "state", state); + field_to_json(Obj, "locale", locale); + field_to_json(Obj, "billingCode", billingCode); + field_to_json(Obj, "configuration", configuration); + field_to_json(Obj, "suspended", suspended); + field_to_json(Obj, "realMacAddress", realMacAddress); + } + + bool SubscriberDevice::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + info.from_json(Obj); + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "deviceType", deviceType); + field_from_json(Obj, "operatorId", operatorId); + field_from_json(Obj, "subscriberId", subscriberId); + field_from_json(Obj, "location", location); + field_from_json(Obj, "contact", contact); + field_from_json(Obj, "managementPolicy", managementPolicy); + field_from_json(Obj, "serviceClass", serviceClass); + field_from_json(Obj, "qrCode", qrCode); + field_from_json(Obj, "geoCode", geoCode); + field_from_json(Obj, "deviceRules", deviceRules); + field_from_json(Obj, "state", state); + field_from_json(Obj, "locale", locale); + field_from_json(Obj, "billingCode", billingCode); + field_from_json(Obj, "configuration", configuration); + field_from_json(Obj, "suspended", suspended); + field_from_json(Obj, "realMacAddress", realMacAddress); + return true; + } catch (...) { + } + return false; + } + + void SubscriberDeviceList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "subscriberDevices", subscriberDevices); + } + + bool SubscriberDeviceList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "subscriberDevices", subscriberDevices); + return true; + } catch (...) { + } + return false; + } + + void VenueDeviceList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "devices", devices); + } + + bool VenueDeviceList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "devices", devices); + return true; + } catch (...) { + } + return false; + } + + bool UpdateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, + ObjectInfo &I) { + uint64_t Now = Utils::Now(); + if (O->has("name")) + I.name = O->get("name").toString(); + + if (I.name.empty()) + return false; + + if (O->has("description")) + I.description = O->get("description").toString(); + SecurityObjects::MergeNotes(O, U, I.notes); + SecurityObjects::NoteInfoVec N; + for (auto &i : I.notes) { + if (i.note.empty()) + continue; + N.push_back( + SecurityObjects::NoteInfo{.created = Now, .createdBy = U.email, .note = i.note}); + } + I.modified = Now; + return true; + } + + bool CreateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, + ObjectInfo &I) { + uint64_t Now = Utils::Now(); + if (O->has("name")) + I.name = O->get("name").toString(); + + if (I.name.empty()) + return false; + + if (O->has("description")) + I.description = O->get("description").toString(); + + SecurityObjects::NoteInfoVec N; + for (auto &i : I.notes) { + if (i.note.empty()) + continue; + N.push_back( + SecurityObjects::NoteInfo{.created = Now, .createdBy = U.email, .note = i.note}); + } + I.notes = N; + I.modified = I.created = Now; + I.id = MicroServiceCreateUUID(); + + return true; + } + + bool CreateObjectInfo([[maybe_unused]] const SecurityObjects::UserInfo &U, ObjectInfo &I) { + I.modified = I.created = Utils::Now(); + I.id = MicroServiceCreateUUID(); + return true; + } + + void DeviceRules::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "rcOnly", rcOnly); + field_to_json(Obj, "rrm", rrm); + field_to_json(Obj, "firmwareUpgrade", firmwareUpgrade); + } + + bool DeviceRules::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "rcOnly", rcOnly); + field_from_json(Obj, "rrm", rrm); + field_from_json(Obj, "firmwareUpgrade", firmwareUpgrade); + return true; + } catch (...) { + } + return false; + } + + void RRMAlgorithmDetails::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "name", name); + field_to_json(Obj, "parameters", parameters); + } + + bool RRMAlgorithmDetails::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "name", name); + field_from_json(Obj, "parameters", parameters); + return true; + } catch (...) { + } + return false; + } + + void RRMDetails::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "vendor", vendor); + field_to_json(Obj, "schedule", schedule); + field_to_json(Obj, "algorithms", algorithms); + } + + bool RRMDetails::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "vendor", vendor); + field_from_json(Obj, "schedule", schedule); + field_from_json(Obj, "algorithms", algorithms); + return true; + } catch (...) { + } + return false; + } + + void ConfigurationOverride::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "source", source); + field_to_json(Obj, "reason", reason); + field_to_json(Obj, "parameterName", parameterName); + field_to_json(Obj, "parameterType", parameterType); + field_to_json(Obj, "parameterValue", parameterValue); + field_to_json(Obj, "modified", modified); + } + + bool ConfigurationOverride::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "source", source); + field_from_json(Obj, "reason", reason); + field_from_json(Obj, "parameterName", parameterName); + field_from_json(Obj, "parameterType", parameterType); + field_from_json(Obj, "parameterValue", parameterValue); + field_from_json(Obj, "modified", modified); + return true; + } catch (...) { + } + return false; + } + + void ConfigurationOverrideList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "managementPolicy", managementPolicy); + field_to_json(Obj, "overrides", overrides); + } + + bool ConfigurationOverrideList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "managementPolicy", managementPolicy); + field_from_json(Obj, "overrides", overrides); + return true; + } catch (...) { + } + return false; + } + +} // namespace OpenWifi::ProvObjects diff --git a/src/RESTObjects/RESTAPI_ProvObjects.h b/src/RESTObjects/RESTAPI_ProvObjects.h index 3c0ed3e..9f4edf3 100644 --- a/src/RESTObjects/RESTAPI_ProvObjects.h +++ b/src/RESTObjects/RESTAPI_ProvObjects.h @@ -12,710 +12,738 @@ namespace OpenWifi::ProvObjects { - enum FIRMWARE_UPGRADE_RULES { - dont_upgrade, - upgrade_inherit, - upgrade_release_only, - upgrade_latest - }; - - struct ObjectInfo { - Types::UUID_t id; - std::string name; - std::string description; - SecurityObjects::NoteInfoVec notes; - uint64_t created=0; - uint64_t modified=0; - Types::TagList tags; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct SerialNumberList { - Types::UUIDvec_t serialNumbers; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ManagementPolicyEntry { - Types::UUIDvec_t users; - Types::UUIDvec_t resources; - Types::StringVec access; - std::string policy; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ManagementPolicy { - ObjectInfo info; - std::vector entries; - Types::StringVec inUse; - Types::UUID_t entity; - Types::UUID_t venue; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector ManagementPolicyVec; - - struct RRMAlgorithmDetails { - std::string name; - std::string parameters; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct RRMDetails { - std::string vendor; - std::string schedule; - std::vector algorithms; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct DeviceRules { - std::string rcOnly{"inherit"}; - std::string rrm{"inherit"}; - std::string firmwareUpgrade{"inherit"}; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct Entity { - ObjectInfo info; - Types::UUID_t parent; - Types::UUIDvec_t children; - Types::UUIDvec_t venues; - Types::UUIDvec_t contacts; // all contacts associated in this entity - Types::UUIDvec_t locations; // all locations associated in this entity - Types::UUID_t managementPolicy; - Types::UUIDvec_t deviceConfiguration; - Types::UUIDvec_t devices; - DeviceRules deviceRules; - Types::StringVec sourceIP; - Types::UUIDvec_t variables; - Types::UUIDvec_t managementPolicies; - Types::UUIDvec_t managementRoles; - Types::UUIDvec_t maps; - Types::UUIDvec_t configurations; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector EntityVec; - - struct DiGraphEntry { - Types::UUID_t parent; - Types::UUID_t child; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - typedef std::vector DiGraph; - - struct Venue { - ObjectInfo info; - Types::UUID_t entity; - Types::UUID_t parent; - Types::UUIDvec_t children; - Types::UUID_t managementPolicy; - Types::UUIDvec_t devices; - DiGraph topology; - std::string design; - Types::UUIDvec_t deviceConfiguration; - Types::UUIDvec_t contacts; - std::string location; - DeviceRules deviceRules; - Types::StringVec sourceIP; - Types::UUIDvec_t variables; - Types::UUIDvec_t configurations; - Types::UUIDvec_t maps; - Types::UUIDvec_t managementPolicies; - Types::UUIDvec_t managementRoles; - Types::UUIDvec_t boards; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector VenueVec; - - struct UserInfoDigest { - std::string id; - std::string loginId; - std::string userType; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ManagementRole { - ObjectInfo info; - Types::UUID_t managementPolicy; - Types::UUIDvec_t users; - Types::StringVec inUse; - Types::UUID_t entity; - Types::UUID_t venue; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector ManagementRoleVec; - - enum LocationType { - LT_SERVICE, LT_EQUIPMENT, LT_AUTO, LT_MANUAL, - LT_SPECIAL, LT_UNKNOWN, LT_CORPORATE - }; - - inline std::string to_string(LocationType L) { - switch(L) { - case LT_SERVICE: return "SERVICE"; - case LT_EQUIPMENT: return "EQUIPMENT"; - case LT_AUTO: return "AUTO"; - case LT_MANUAL: return "MANUAL"; - case LT_SPECIAL: return "SPECIAL"; - case LT_UNKNOWN: return "UNKNOWN"; - case LT_CORPORATE: return "CORPORATE"; - default: return "UNKNOWN"; - } - } - - inline LocationType location_from_string(const std::string &S) { - if(!Poco::icompare(S,"SERVICE")) - return LT_SERVICE; - else if(!Poco::icompare(S,"EQUIPMENT")) - return LT_EQUIPMENT; - else if(!Poco::icompare(S,"AUTO")) - return LT_AUTO; - else if(!Poco::icompare(S,"MANUAL")) - return LT_MANUAL; - else if(!Poco::icompare(S,"SPECIAL")) - return LT_SPECIAL; - else if(!Poco::icompare(S,"UNKNOWN")) - return LT_UNKNOWN; - else if(!Poco::icompare(S,"CORPORATE")) - return LT_CORPORATE; - return LT_UNKNOWN; - } - - struct Location { - ObjectInfo info; - LocationType type; - std::string buildingName; - Types::StringVec addressLines; - std::string city; - std::string state; - std::string postal; - std::string country; - Types::StringVec phones; - Types::StringVec mobiles; - std::string geoCode; - Types::StringVec inUse; - Types::UUID_t entity; - Types::UUID_t managementPolicy; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector LocationVec; - - struct OperatorLocation { - ObjectInfo info; - std::string type; - std::string buildingName; - Types::StringVec addressLines; - std::string city; - std::string state; - std::string postal; - std::string country; - Types::StringVec phones; - Types::StringVec mobiles; - std::string geoCode; - Types::UUID_t operatorId; - Types::UUID_t subscriberDeviceId; - Types::UUID_t managementPolicy; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector LocationVec; - - struct SubLocation { - std::string type; - std::string buildingName; - Types::StringVec addressLines; - std::string city; - std::string state; - std::string postal; - std::string country; - Types::StringVec phones; - Types::StringVec mobiles; - std::string geoCode; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct OperatorLocationList { - std::vector locations; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - - enum ContactType { - CT_SUBSCRIBER, CT_USER, CT_INSTALLER, CT_CSR, CT_MANAGER, - CT_BUSINESSOWNER, CT_TECHNICIAN, CT_CORPORATE, CT_UNKNOWN - }; - - inline std::string to_string(ContactType L) { - switch(L) { - case CT_SUBSCRIBER: return "SUBSCRIBER"; - case CT_USER: return "USER"; - case CT_INSTALLER: return "INSTALLER"; - case CT_CSR: return "CSR"; - case CT_MANAGER: return "MANAGER"; - case CT_BUSINESSOWNER: return "BUSINESSOWNER"; - case CT_TECHNICIAN: return "TECHNICIAN"; - case CT_CORPORATE: return "CORPORATE"; - case CT_UNKNOWN: return "UNKNOWN"; - default: return "UNKNOWN"; - } - } - - inline ContactType contact_from_string(const std::string &S) { - if(!Poco::icompare(S,"SUBSCRIBER")) - return CT_SUBSCRIBER; - else if(!Poco::icompare(S,"USER")) - return CT_USER; - else if(!Poco::icompare(S,"INSTALLER")) - return CT_INSTALLER; - else if(!Poco::icompare(S,"CSR")) - return CT_CSR; - else if(!Poco::icompare(S,"BUSINESSOWNER")) - return CT_BUSINESSOWNER; - else if(!Poco::icompare(S,"TECHNICIAN")) - return CT_TECHNICIAN; - else if(!Poco::icompare(S,"CORPORATE")) - return CT_CORPORATE; - else if(!Poco::icompare(S,"UNKNOWN")) - return CT_UNKNOWN; - return CT_UNKNOWN; - } - - struct Contact { - ObjectInfo info; - ContactType type=CT_USER; - std::string title; - std::string salutation; - std::string firstname; - std::string lastname; - std::string initials; - std::string visual; - Types::StringVec mobiles; - Types::StringVec phones; - std::string primaryEmail; - std::string secondaryEmail; - std::string accessPIN; - Types::StringVec inUse; - Types::UUID_t entity; - Types::UUID_t managementPolicy; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector ContactVec; - - struct OperatorContact { - ObjectInfo info; - std::string type; - std::string title; - std::string salutation; - std::string firstname; - std::string lastname; - std::string initials; - std::string visual; - Types::StringVec mobiles; - Types::StringVec phones; - std::string primaryEmail; - std::string secondaryEmail; - std::string accessPIN; - Types::UUID_t operatorId; - Types::UUID_t subscriberDeviceId; - Types::UUID_t managementPolicy; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct SubContact { - std::string type; - std::string title; - std::string salutation; - std::string firstname; - std::string lastname; - std::string initials; - std::string visual; - Types::StringVec mobiles; - Types::StringVec phones; - std::string primaryEmail; - std::string secondaryEmail; - std::string accessPIN; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct OperatorContactList { - std::vector contacts; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - typedef std::vector OperatorContactVec; - - struct DeviceConfigurationElement { - std::string name; - std::string description; - uint64_t weight; - std::string configuration; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector DeviceConfigurationElementVec; - - struct DeviceConfiguration { - ObjectInfo info; - Types::UUID_t managementPolicy; - Types::StringVec deviceTypes; - DeviceConfigurationElementVec configuration; - Types::StringVec inUse; - Types::UUIDvec_t variables; - DeviceRules deviceRules; - bool subscriberOnly=false; - std::string venue; - std::string entity; - std::string subscriber; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector DeviceConfigurationVec; - - - struct InventoryTag { - ObjectInfo info; - std::string serialNumber; - std::string venue; - std::string entity; - std::string subscriber; - std::string deviceType; - std::string qrCode; - std::string geoCode; - std::string location; - std::string contact; - std::string deviceConfiguration; - DeviceRules deviceRules; - Types::UUID_t managementPolicy; - std::string state; - std::string devClass; - std::string locale; - std::string realMacAddress; - bool doNotAllowOverrides=false; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - typedef std::vector InventoryTagVec; - - struct InventoryTagList { - InventoryTagVec taglist; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct InventoryConfigApplyResult { - std::string appliedConfiguration; - Types::StringVec errors; - Types::StringVec warnings; - uint64_t errorCode; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct Report { - uint64_t snapShot=0; - Types::CountedMap tenants; - - void reset(); - void to_json(Poco::JSON::Object &Obj) const; - }; - - struct ExpandedUseEntry { - std::string uuid; - std::string name; - std::string description; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ExpandedUseEntryList { - std::string type; - std::vector entries; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ExpandedUseEntryMapList { - std::vector entries; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct UuidList { - Types::UUIDvec_t list; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - enum ACLACCESS { - NONE = 0, READ=1, MODIFY=2, CREATE=3, DELETE=4 - }; - - struct ObjectACL { - UuidList users; - UuidList roles; - uint64_t access = (uint64_t) NONE; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ObjectACLList { - std::vector list; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct Map { - ObjectInfo info; - std::string data; - std::string entity; - std::string creator; - std::string visibility{"private"}; - ObjectACLList access; - Types::UUID_t managementPolicy; - std::string venue; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct MapList { - std::vector list; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - enum SignupStatusCodes { - SignupCreated = 0 , - SignupWaitingForEmail, - SignupWaitingForDevice, - SignupSuccess, - SignupFailure, - SignupCanceled, - SignupTimedOut - }; - - struct SignupEntry { - ObjectInfo info; - std::string email; - std::string userId; - std::string macAddress; - std::string serialNumber; - uint64_t submitted = 0 ; - uint64_t completed = 0 ; - std::string status; - uint64_t error=0; - uint64_t statusCode=0; - std::string deviceID; - std::string registrationId; - std::string operatorId; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct Variable { - std::string type; - uint64_t weight=0; - std::string prefix; - std::string value; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct VariableList { - std::vector variables; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct VariableBlock { - ObjectInfo info; - std::vector variables; - std::string entity; - std::string venue; - std::string subscriber; - std::string inventory; - Types::UUIDvec_t configurations; - Types::UUID_t managementPolicy; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct VariableBlockList { - std::vector variableBlocks; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct Operator { - ObjectInfo info; - Types::UUID_t managementPolicy; - Types::UUIDvec_t managementRoles; - DeviceRules deviceRules; - std::vector variables; - bool defaultOperator=false; - Types::StringVec sourceIP; - std::string registrationId; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct OperatorList { - std::vector operators; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct VenueDeviceList { - std::string id; - std::string name; - std::string description; - Types::UUIDvec_t devices; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ServiceClass { - ObjectInfo info; - Types::UUID_t operatorId; - Types::UUID_t managementPolicy; - double cost=0.0; - std::string currency; - std::string period; - std::string billingCode; - std::vector variables; - bool defaultService=false; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ServiceClassList { - std::vector serviceClasses; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ConfigurationDetails { - DeviceConfigurationElementVec configuration; - std::string rrm{"inherit"}; - std::string firmwareUpgrade{"inherit"}; - std::string firmwareRCOnly{"inherit"}; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct SubscriberDevice { - ObjectInfo info; - std::string serialNumber; - std::string deviceType; - Types::UUID_t operatorId; - Types::UUID_t subscriberId; - SubLocation location; - SubContact contact; - Types::UUID_t managementPolicy; - Types::UUID_t serviceClass; - std::string qrCode; - std::string geoCode; - DeviceRules deviceRules; - std::string state; - std::string locale; - std::string billingCode; - DeviceConfigurationElementVec configuration; - bool suspended=false; - std::string realMacAddress; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct SubscriberDeviceList { - std::vector subscriberDevices; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ConfigurationOverride { - std::string source; - std::string reason; - std::string parameterName; - std::string parameterType; - std::string parameterValue; - std::uint64_t modified; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct ConfigurationOverrideList { - std::string serialNumber; - Types::UUID_t managementPolicy; - std::vector overrides; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - bool UpdateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, ObjectInfo &I); - bool CreateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, ObjectInfo &I); - bool CreateObjectInfo(const SecurityObjects::UserInfo &U, ObjectInfo &I); -}; + enum FIRMWARE_UPGRADE_RULES { + dont_upgrade, + upgrade_inherit, + upgrade_release_only, + upgrade_latest + }; + + struct ObjectInfo { + Types::UUID_t id; + std::string name; + std::string description; + SecurityObjects::NoteInfoVec notes; + uint64_t created = 0; + uint64_t modified = 0; + Types::TagList tags; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct SerialNumberList { + Types::UUIDvec_t serialNumbers; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ManagementPolicyEntry { + Types::UUIDvec_t users; + Types::UUIDvec_t resources; + Types::StringVec access; + std::string policy; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ManagementPolicy { + ObjectInfo info; + std::vector entries; + Types::StringVec inUse; + Types::UUID_t entity; + Types::UUID_t venue; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector ManagementPolicyVec; + + struct RRMAlgorithmDetails { + std::string name; + std::string parameters; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct RRMDetails { + std::string vendor; + std::string schedule; + std::vector algorithms; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct DeviceRules { + std::string rcOnly{"inherit"}; + std::string rrm{"inherit"}; + std::string firmwareUpgrade{"inherit"}; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct Entity { + ObjectInfo info; + Types::UUID_t parent; + Types::UUIDvec_t children; + Types::UUIDvec_t venues; + Types::UUIDvec_t contacts; // all contacts associated in this entity + Types::UUIDvec_t locations; // all locations associated in this entity + Types::UUID_t managementPolicy; + Types::UUIDvec_t deviceConfiguration; + Types::UUIDvec_t devices; + DeviceRules deviceRules; + Types::StringVec sourceIP; + Types::UUIDvec_t variables; + Types::UUIDvec_t managementPolicies; + Types::UUIDvec_t managementRoles; + Types::UUIDvec_t maps; + Types::UUIDvec_t configurations; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector EntityVec; + + struct DiGraphEntry { + Types::UUID_t parent; + Types::UUID_t child; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + typedef std::vector DiGraph; + + struct Venue { + ObjectInfo info; + Types::UUID_t entity; + Types::UUID_t parent; + Types::UUIDvec_t children; + Types::UUID_t managementPolicy; + Types::UUIDvec_t devices; + DiGraph topology; + std::string design; + Types::UUIDvec_t deviceConfiguration; + Types::UUIDvec_t contacts; + std::string location; + DeviceRules deviceRules; + Types::StringVec sourceIP; + Types::UUIDvec_t variables; + Types::UUIDvec_t configurations; + Types::UUIDvec_t maps; + Types::UUIDvec_t managementPolicies; + Types::UUIDvec_t managementRoles; + Types::UUIDvec_t boards; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector VenueVec; + + struct UserInfoDigest { + std::string id; + std::string loginId; + std::string userType; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ManagementRole { + ObjectInfo info; + Types::UUID_t managementPolicy; + Types::UUIDvec_t users; + Types::StringVec inUse; + Types::UUID_t entity; + Types::UUID_t venue; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector ManagementRoleVec; + + enum LocationType { + LT_SERVICE, + LT_EQUIPMENT, + LT_AUTO, + LT_MANUAL, + LT_SPECIAL, + LT_UNKNOWN, + LT_CORPORATE + }; + + inline std::string to_string(LocationType L) { + switch (L) { + case LT_SERVICE: + return "SERVICE"; + case LT_EQUIPMENT: + return "EQUIPMENT"; + case LT_AUTO: + return "AUTO"; + case LT_MANUAL: + return "MANUAL"; + case LT_SPECIAL: + return "SPECIAL"; + case LT_UNKNOWN: + return "UNKNOWN"; + case LT_CORPORATE: + return "CORPORATE"; + default: + return "UNKNOWN"; + } + } + + inline LocationType location_from_string(const std::string &S) { + if (!Poco::icompare(S, "SERVICE")) + return LT_SERVICE; + else if (!Poco::icompare(S, "EQUIPMENT")) + return LT_EQUIPMENT; + else if (!Poco::icompare(S, "AUTO")) + return LT_AUTO; + else if (!Poco::icompare(S, "MANUAL")) + return LT_MANUAL; + else if (!Poco::icompare(S, "SPECIAL")) + return LT_SPECIAL; + else if (!Poco::icompare(S, "UNKNOWN")) + return LT_UNKNOWN; + else if (!Poco::icompare(S, "CORPORATE")) + return LT_CORPORATE; + return LT_UNKNOWN; + } + + struct Location { + ObjectInfo info; + LocationType type; + std::string buildingName; + Types::StringVec addressLines; + std::string city; + std::string state; + std::string postal; + std::string country; + Types::StringVec phones; + Types::StringVec mobiles; + std::string geoCode; + Types::StringVec inUse; + Types::UUID_t entity; + Types::UUID_t managementPolicy; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector LocationVec; + + struct OperatorLocation { + ObjectInfo info; + std::string type; + std::string buildingName; + Types::StringVec addressLines; + std::string city; + std::string state; + std::string postal; + std::string country; + Types::StringVec phones; + Types::StringVec mobiles; + std::string geoCode; + Types::UUID_t operatorId; + Types::UUID_t subscriberDeviceId; + Types::UUID_t managementPolicy; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector LocationVec; + + struct SubLocation { + std::string type; + std::string buildingName; + Types::StringVec addressLines; + std::string city; + std::string state; + std::string postal; + std::string country; + Types::StringVec phones; + Types::StringVec mobiles; + std::string geoCode; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct OperatorLocationList { + std::vector locations; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + enum ContactType { + CT_SUBSCRIBER, + CT_USER, + CT_INSTALLER, + CT_CSR, + CT_MANAGER, + CT_BUSINESSOWNER, + CT_TECHNICIAN, + CT_CORPORATE, + CT_UNKNOWN + }; + + inline std::string to_string(ContactType L) { + switch (L) { + case CT_SUBSCRIBER: + return "SUBSCRIBER"; + case CT_USER: + return "USER"; + case CT_INSTALLER: + return "INSTALLER"; + case CT_CSR: + return "CSR"; + case CT_MANAGER: + return "MANAGER"; + case CT_BUSINESSOWNER: + return "BUSINESSOWNER"; + case CT_TECHNICIAN: + return "TECHNICIAN"; + case CT_CORPORATE: + return "CORPORATE"; + case CT_UNKNOWN: + return "UNKNOWN"; + default: + return "UNKNOWN"; + } + } + + inline ContactType contact_from_string(const std::string &S) { + if (!Poco::icompare(S, "SUBSCRIBER")) + return CT_SUBSCRIBER; + else if (!Poco::icompare(S, "USER")) + return CT_USER; + else if (!Poco::icompare(S, "INSTALLER")) + return CT_INSTALLER; + else if (!Poco::icompare(S, "CSR")) + return CT_CSR; + else if (!Poco::icompare(S, "BUSINESSOWNER")) + return CT_BUSINESSOWNER; + else if (!Poco::icompare(S, "TECHNICIAN")) + return CT_TECHNICIAN; + else if (!Poco::icompare(S, "CORPORATE")) + return CT_CORPORATE; + else if (!Poco::icompare(S, "UNKNOWN")) + return CT_UNKNOWN; + return CT_UNKNOWN; + } + + struct Contact { + ObjectInfo info; + ContactType type = CT_USER; + std::string title; + std::string salutation; + std::string firstname; + std::string lastname; + std::string initials; + std::string visual; + Types::StringVec mobiles; + Types::StringVec phones; + std::string primaryEmail; + std::string secondaryEmail; + std::string accessPIN; + Types::StringVec inUse; + Types::UUID_t entity; + Types::UUID_t managementPolicy; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector ContactVec; + + struct OperatorContact { + ObjectInfo info; + std::string type; + std::string title; + std::string salutation; + std::string firstname; + std::string lastname; + std::string initials; + std::string visual; + Types::StringVec mobiles; + Types::StringVec phones; + std::string primaryEmail; + std::string secondaryEmail; + std::string accessPIN; + Types::UUID_t operatorId; + Types::UUID_t subscriberDeviceId; + Types::UUID_t managementPolicy; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct SubContact { + std::string type; + std::string title; + std::string salutation; + std::string firstname; + std::string lastname; + std::string initials; + std::string visual; + Types::StringVec mobiles; + Types::StringVec phones; + std::string primaryEmail; + std::string secondaryEmail; + std::string accessPIN; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct OperatorContactList { + std::vector contacts; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + typedef std::vector OperatorContactVec; + + struct DeviceConfigurationElement { + std::string name; + std::string description; + uint64_t weight; + std::string configuration; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector DeviceConfigurationElementVec; + + struct DeviceConfiguration { + ObjectInfo info; + Types::UUID_t managementPolicy; + Types::StringVec deviceTypes; + DeviceConfigurationElementVec configuration; + Types::StringVec inUse; + Types::UUIDvec_t variables; + DeviceRules deviceRules; + bool subscriberOnly = false; + std::string venue; + std::string entity; + std::string subscriber; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector DeviceConfigurationVec; + + struct InventoryTag { + ObjectInfo info; + std::string serialNumber; + std::string venue; + std::string entity; + std::string subscriber; + std::string deviceType; + std::string qrCode; + std::string geoCode; + std::string location; + std::string contact; + std::string deviceConfiguration; + DeviceRules deviceRules; + Types::UUID_t managementPolicy; + std::string state; + std::string devClass; + std::string locale; + std::string realMacAddress; + bool doNotAllowOverrides = false; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + typedef std::vector InventoryTagVec; + + struct InventoryTagList { + InventoryTagVec taglist; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct InventoryConfigApplyResult { + std::string appliedConfiguration; + Types::StringVec errors; + Types::StringVec warnings; + uint64_t errorCode; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct Report { + uint64_t snapShot = 0; + Types::CountedMap tenants; + + void reset(); + void to_json(Poco::JSON::Object &Obj) const; + }; + + struct ExpandedUseEntry { + std::string uuid; + std::string name; + std::string description; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ExpandedUseEntryList { + std::string type; + std::vector entries; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ExpandedUseEntryMapList { + std::vector entries; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct UuidList { + Types::UUIDvec_t list; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + enum ACLACCESS { NONE = 0, READ = 1, MODIFY = 2, CREATE = 3, DELETE = 4 }; + + struct ObjectACL { + UuidList users; + UuidList roles; + uint64_t access = (uint64_t)NONE; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ObjectACLList { + std::vector list; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct Map { + ObjectInfo info; + std::string data; + std::string entity; + std::string creator; + std::string visibility{"private"}; + ObjectACLList access; + Types::UUID_t managementPolicy; + std::string venue; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct MapList { + std::vector list; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + enum SignupStatusCodes { + SignupCreated = 0, + SignupWaitingForEmail, + SignupWaitingForDevice, + SignupSuccess, + SignupFailure, + SignupCanceled, + SignupTimedOut + }; + + struct SignupEntry { + ObjectInfo info; + std::string email; + std::string userId; + std::string macAddress; + std::string serialNumber; + uint64_t submitted = 0; + uint64_t completed = 0; + std::string status; + uint64_t error = 0; + uint64_t statusCode = 0; + std::string deviceID; + std::string registrationId; + std::string operatorId; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct Variable { + std::string type; + uint64_t weight = 0; + std::string prefix; + std::string value; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct VariableList { + std::vector variables; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct VariableBlock { + ObjectInfo info; + std::vector variables; + std::string entity; + std::string venue; + std::string subscriber; + std::string inventory; + Types::UUIDvec_t configurations; + Types::UUID_t managementPolicy; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct VariableBlockList { + std::vector variableBlocks; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct Operator { + ObjectInfo info; + Types::UUID_t managementPolicy; + Types::UUIDvec_t managementRoles; + DeviceRules deviceRules; + std::vector variables; + bool defaultOperator = false; + Types::StringVec sourceIP; + std::string registrationId; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct OperatorList { + std::vector operators; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct VenueDeviceList { + std::string id; + std::string name; + std::string description; + Types::UUIDvec_t devices; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ServiceClass { + ObjectInfo info; + Types::UUID_t operatorId; + Types::UUID_t managementPolicy; + double cost = 0.0; + std::string currency; + std::string period; + std::string billingCode; + std::vector variables; + bool defaultService = false; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ServiceClassList { + std::vector serviceClasses; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ConfigurationDetails { + DeviceConfigurationElementVec configuration; + std::string rrm{"inherit"}; + std::string firmwareUpgrade{"inherit"}; + std::string firmwareRCOnly{"inherit"}; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct SubscriberDevice { + ObjectInfo info; + std::string serialNumber; + std::string deviceType; + Types::UUID_t operatorId; + Types::UUID_t subscriberId; + SubLocation location; + SubContact contact; + Types::UUID_t managementPolicy; + Types::UUID_t serviceClass; + std::string qrCode; + std::string geoCode; + DeviceRules deviceRules; + std::string state; + std::string locale; + std::string billingCode; + DeviceConfigurationElementVec configuration; + bool suspended = false; + std::string realMacAddress; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct SubscriberDeviceList { + std::vector subscriberDevices; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ConfigurationOverride { + std::string source; + std::string reason; + std::string parameterName; + std::string parameterType; + std::string parameterValue; + std::uint64_t modified; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + struct ConfigurationOverrideList { + std::string serialNumber; + Types::UUID_t managementPolicy; + std::vector overrides; + + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + bool UpdateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, + ObjectInfo &I); + bool CreateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, + ObjectInfo &I); + bool CreateObjectInfo(const SecurityObjects::UserInfo &U, ObjectInfo &I); +}; // namespace OpenWifi::ProvObjects diff --git a/src/RESTObjects/RESTAPI_SecurityObjects.cpp b/src/RESTObjects/RESTAPI_SecurityObjects.cpp index c02df59..583de45 100644 --- a/src/RESTObjects/RESTAPI_SecurityObjects.cpp +++ b/src/RESTObjects/RESTAPI_SecurityObjects.cpp @@ -9,84 +9,106 @@ #include "Poco/JSON/Parser.h" #include "Poco/JSON/Stringifier.h" -#include "framework/RESTAPI_utils.h" #include "RESTAPI_SecurityObjects.h" +#include "framework/RESTAPI_utils.h" -using OpenWifi::RESTAPI_utils::field_to_json; using OpenWifi::RESTAPI_utils::field_from_json; +using OpenWifi::RESTAPI_utils::field_to_json; namespace OpenWifi::SecurityObjects { void AclTemplate::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"Read",Read_); - field_to_json(Obj,"ReadWrite",ReadWrite_); - field_to_json(Obj,"ReadWriteCreate",ReadWriteCreate_); - field_to_json(Obj,"Delete",Delete_); - field_to_json(Obj,"PortalLogin",PortalLogin_); + field_to_json(Obj, "Read", Read_); + field_to_json(Obj, "ReadWrite", ReadWrite_); + field_to_json(Obj, "ReadWriteCreate", ReadWriteCreate_); + field_to_json(Obj, "Delete", Delete_); + field_to_json(Obj, "PortalLogin", PortalLogin_); } ResourceAccessType ResourceAccessTypeFromString(const std::string &s) { - if(!Poco::icompare(s,"READ")) return READ; - if(!Poco::icompare(s,"MODIFY")) return MODIFY; - if(!Poco::icompare(s,"DELETE")) return DELETE; - if(!Poco::icompare(s,"CREATE")) return CREATE; - if(!Poco::icompare(s,"TEST")) return TEST; - if(!Poco::icompare(s,"MOVE")) return MOVE; + if (!Poco::icompare(s, "READ")) + return READ; + if (!Poco::icompare(s, "MODIFY")) + return MODIFY; + if (!Poco::icompare(s, "DELETE")) + return DELETE; + if (!Poco::icompare(s, "CREATE")) + return CREATE; + if (!Poco::icompare(s, "TEST")) + return TEST; + if (!Poco::icompare(s, "MOVE")) + return MOVE; return NONE; } - std::string ResourceAccessTypeToString(const ResourceAccessType & T) { - switch(T) { - case READ: return "READ"; - case MODIFY: return "MODIFY"; - case DELETE: return "DELETE"; - case CREATE: return "CREATE"; - case TEST: return "TEST"; - case MOVE: return "MOVE"; - default: return "NONE"; + std::string ResourceAccessTypeToString(const ResourceAccessType &T) { + switch (T) { + case READ: + return "READ"; + case MODIFY: + return "MODIFY"; + case DELETE: + return "DELETE"; + case CREATE: + return "CREATE"; + case TEST: + return "TEST"; + case MOVE: + return "MOVE"; + default: + return "NONE"; } } - USER_ROLE UserTypeFromString(const std::string &U) { - if (!Poco::icompare(U,"root")) - return ROOT; - else if (!Poco::icompare(U,"admin")) - return ADMIN; - else if (!Poco::icompare(U,"subscriber")) - return SUBSCRIBER; - else if (!Poco::icompare(U,"partner")) - return PARTNER; - else if (!Poco::icompare(U,"csr")) - return CSR; - else if (!Poco::icompare(U, "system")) - return SYSTEM; - else if (!Poco::icompare(U, "installer")) - return INSTALLER; - else if (!Poco::icompare(U, "noc")) - return NOC; - else if (!Poco::icompare(U, "accounting")) - return ACCOUNTING; - return UNKNOWN; - } + USER_ROLE UserTypeFromString(const std::string &U) { + if (!Poco::icompare(U, "root")) + return ROOT; + else if (!Poco::icompare(U, "admin")) + return ADMIN; + else if (!Poco::icompare(U, "subscriber")) + return SUBSCRIBER; + else if (!Poco::icompare(U, "partner")) + return PARTNER; + else if (!Poco::icompare(U, "csr")) + return CSR; + else if (!Poco::icompare(U, "system")) + return SYSTEM; + else if (!Poco::icompare(U, "installer")) + return INSTALLER; + else if (!Poco::icompare(U, "noc")) + return NOC; + else if (!Poco::icompare(U, "accounting")) + return ACCOUNTING; + return UNKNOWN; + } - std::string UserTypeToString(USER_ROLE U) { - switch(U) { - case ROOT: return "root"; - case ADMIN: return "admin"; - case SUBSCRIBER: return "subscriber"; - case PARTNER: return "partner"; - case CSR: return "csr"; - case SYSTEM: return "system"; - case INSTALLER: return "installer"; - case NOC: return "noc"; - case ACCOUNTING: return "accounting"; - case UNKNOWN: - default: - return "unknown"; - } - } + std::string UserTypeToString(USER_ROLE U) { + switch (U) { + case ROOT: + return "root"; + case ADMIN: + return "admin"; + case SUBSCRIBER: + return "subscriber"; + case PARTNER: + return "partner"; + case CSR: + return "csr"; + case SYSTEM: + return "system"; + case INSTALLER: + return "installer"; + case NOC: + return "noc"; + case ACCOUNTING: + return "accounting"; + case UNKNOWN: + default: + return "unknown"; + } + } - bool AclTemplate::from_json(const Poco::JSON::Object::Ptr &Obj) { + bool AclTemplate::from_json(const Poco::JSON::Object::Ptr &Obj) { try { field_from_json(Obj, "Read", Read_); field_from_json(Obj, "ReadWrite", ReadWrite_); @@ -94,27 +116,27 @@ namespace OpenWifi::SecurityObjects { field_from_json(Obj, "Delete", Delete_); field_from_json(Obj, "PortalLogin", PortalLogin_); return true; - } catch(...) { - std::cout << "Cannot parse: AclTemplate" << std::endl; + } catch (...) { + std::cout << "Cannot parse: AclTemplate" << std::endl; } return false; } - void WebToken::to_json(Poco::JSON::Object & Obj) const { - Poco::JSON::Object AclTemplateObj; + void WebToken::to_json(Poco::JSON::Object &Obj) const { + Poco::JSON::Object AclTemplateObj; acl_template_.to_json(AclTemplateObj); - field_to_json(Obj,"access_token",access_token_); - field_to_json(Obj,"refresh_token",refresh_token_); - field_to_json(Obj,"token_type",token_type_); - field_to_json(Obj,"expires_in",expires_in_); - field_to_json(Obj,"idle_timeout",idle_timeout_); - field_to_json(Obj,"created",created_); - field_to_json(Obj,"username",username_); - field_to_json(Obj,"userMustChangePassword",userMustChangePassword); - field_to_json(Obj,"errorCode", errorCode); - Obj.set("aclTemplate",AclTemplateObj); - field_to_json(Obj,"errorCode", errorCode); - field_to_json(Obj,"lastRefresh", lastRefresh_); + field_to_json(Obj, "access_token", access_token_); + field_to_json(Obj, "refresh_token", refresh_token_); + field_to_json(Obj, "token_type", token_type_); + field_to_json(Obj, "expires_in", expires_in_); + field_to_json(Obj, "idle_timeout", idle_timeout_); + field_to_json(Obj, "created", created_); + field_to_json(Obj, "username", username_); + field_to_json(Obj, "userMustChangePassword", userMustChangePassword); + field_to_json(Obj, "errorCode", errorCode); + Obj.set("aclTemplate", AclTemplateObj); + field_to_json(Obj, "errorCode", errorCode); + field_to_json(Obj, "lastRefresh", lastRefresh_); } bool WebToken::from_json(const Poco::JSON::Object::Ptr &Obj) { @@ -130,236 +152,235 @@ namespace OpenWifi::SecurityObjects { field_from_json(Obj, "idle_timeout", idle_timeout_); field_from_json(Obj, "created", created_); field_from_json(Obj, "username", username_); - field_from_json(Obj, "userMustChangePassword",userMustChangePassword); - field_from_json(Obj,"lastRefresh", lastRefresh_); + field_from_json(Obj, "userMustChangePassword", userMustChangePassword); + field_from_json(Obj, "lastRefresh", lastRefresh_); return true; } catch (...) { - std::cout << "Cannot parse: WebToken" << std::endl; + std::cout << "Cannot parse: WebToken" << std::endl; } return false; } void MobilePhoneNumber::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"number", number); - field_to_json(Obj,"verified", verified); - field_to_json(Obj,"primary", primary); + field_to_json(Obj, "number", number); + field_to_json(Obj, "verified", verified); + field_to_json(Obj, "primary", primary); } bool MobilePhoneNumber::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"number",number); - field_from_json(Obj,"verified",verified); - field_from_json(Obj,"primary",primary); - return true; - } catch (...) { - std::cout << "Cannot parse: MobilePhoneNumber" << std::endl; - } - return false; + try { + field_from_json(Obj, "number", number); + field_from_json(Obj, "verified", verified); + field_from_json(Obj, "primary", primary); + return true; + } catch (...) { + std::cout << "Cannot parse: MobilePhoneNumber" << std::endl; + } + return false; }; void MfaAuthInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"enabled", enabled); - field_to_json(Obj,"method", method); + field_to_json(Obj, "enabled", enabled); + field_to_json(Obj, "method", method); } bool MfaAuthInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"enabled",enabled); - field_from_json(Obj,"method",method); - return true; - } catch (...) { - std::cout << "Cannot parse: MfaAuthInfo" << std::endl; - } - return false; + try { + field_from_json(Obj, "enabled", enabled); + field_from_json(Obj, "method", method); + return true; + } catch (...) { + std::cout << "Cannot parse: MfaAuthInfo" << std::endl; + } + return false; } void UserLoginLoginExtensions::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "mobiles", mobiles); - field_to_json(Obj, "mfa", mfa); - field_to_json(Obj, "authenticatorSecret", authenticatorSecret); + field_to_json(Obj, "mobiles", mobiles); + field_to_json(Obj, "mfa", mfa); + field_to_json(Obj, "authenticatorSecret", authenticatorSecret); } bool UserLoginLoginExtensions::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "mobiles",mobiles); - field_from_json(Obj, "mfa",mfa); - field_from_json(Obj, "authenticatorSecret", authenticatorSecret); - return true; - } catch (...) { - std::cout << "Cannot parse: UserLoginLoginExtensions" << std::endl; - } - return false; + try { + field_from_json(Obj, "mobiles", mobiles); + field_from_json(Obj, "mfa", mfa); + field_from_json(Obj, "authenticatorSecret", authenticatorSecret); + return true; + } catch (...) { + std::cout << "Cannot parse: UserLoginLoginExtensions" << std::endl; + } + return false; } - void MFAChallengeRequest::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "uuid", uuid); - field_to_json(Obj, "question", question); - field_to_json(Obj, "created", created); - field_to_json(Obj, "method", method); - } + void MFAChallengeRequest::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "uuid", uuid); + field_to_json(Obj, "question", question); + field_to_json(Obj, "created", created); + field_to_json(Obj, "method", method); + } - bool MFAChallengeRequest::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"uuid",uuid); - field_from_json(Obj,"question",question); - field_from_json(Obj,"created",created); - field_from_json(Obj,"method",method); - return true; - } catch (...) { - std::cout << "Cannot parse: MFAChallengeRequest" << std::endl; - } - return false; + bool MFAChallengeRequest::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "uuid", uuid); + field_from_json(Obj, "question", question); + field_from_json(Obj, "created", created); + field_from_json(Obj, "method", method); + return true; + } catch (...) { + std::cout << "Cannot parse: MFAChallengeRequest" << std::endl; + } + return false; }; - void MFAChallengeResponse::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "uuid", uuid); - field_to_json(Obj, "answer", answer); - } + void MFAChallengeResponse::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "uuid", uuid); + field_to_json(Obj, "answer", answer); + } - bool MFAChallengeResponse::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"uuid",uuid); - field_from_json(Obj,"answer",answer); - return true; - } catch (...) { - std::cout << "Cannot parse: MFAChallengeResponse" << std::endl; - } - return false; + bool MFAChallengeResponse::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "uuid", uuid); + field_from_json(Obj, "answer", answer); + return true; + } catch (...) { + std::cout << "Cannot parse: MFAChallengeResponse" << std::endl; + } + return false; + } - } + void UserInfo::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "avatar", avatar); + field_to_json(Obj, "email", email); + field_to_json(Obj, "validated", validated); + field_to_json(Obj, "validationEmail", validationEmail); + field_to_json(Obj, "validationDate", validationDate); + field_to_json(Obj, "creationDate", creationDate); + field_to_json(Obj, "validationURI", validationURI); + field_to_json(Obj, "changePassword", changePassword); + field_to_json(Obj, "lastLogin", lastLogin); + field_to_json(Obj, "currentLoginURI", currentLoginURI); + field_to_json(Obj, "lastPasswordChange", lastPasswordChange); + field_to_json(Obj, "lastEmailCheck", lastEmailCheck); + field_to_json(Obj, "waitingForEmailCheck", waitingForEmailCheck); + field_to_json(Obj, "locale", locale); + field_to_json(Obj, "notes", notes); + field_to_json(Obj, "location", location); + field_to_json(Obj, "owner", owner); + field_to_json(Obj, "suspended", suspended); + field_to_json(Obj, "blackListed", blackListed); + field_to_json(Obj, "userRole", userRole, UserTypeToString); + field_to_json(Obj, "userTypeProprietaryInfo", userTypeProprietaryInfo); + field_to_json(Obj, "securityPolicy", securityPolicy); + field_to_json(Obj, "securityPolicyChange", securityPolicyChange); + field_to_json(Obj, "currentPassword", currentPassword); + field_to_json(Obj, "lastPasswords", lastPasswords); + field_to_json(Obj, "oauthType", oauthType); + field_to_json(Obj, "oauthUserInfo", oauthUserInfo); + field_to_json(Obj, "modified", modified); + field_to_json(Obj, "signingUp", signingUp); + }; - void UserInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id",id); - field_to_json(Obj,"name",name); - field_to_json(Obj,"description", description); - field_to_json(Obj,"avatar", avatar); - field_to_json(Obj,"email", email); - field_to_json(Obj,"validated", validated); - field_to_json(Obj,"validationEmail", validationEmail); - field_to_json(Obj,"validationDate", validationDate); - field_to_json(Obj,"creationDate", creationDate); - field_to_json(Obj,"validationURI", validationURI); - field_to_json(Obj,"changePassword", changePassword); - field_to_json(Obj,"lastLogin", lastLogin); - field_to_json(Obj,"currentLoginURI", currentLoginURI); - field_to_json(Obj,"lastPasswordChange", lastPasswordChange); - field_to_json(Obj,"lastEmailCheck", lastEmailCheck); - field_to_json(Obj,"waitingForEmailCheck", waitingForEmailCheck); - field_to_json(Obj,"locale", locale); - field_to_json(Obj,"notes", notes); - field_to_json(Obj,"location", location); - field_to_json(Obj,"owner", owner); - field_to_json(Obj,"suspended", suspended); - field_to_json(Obj,"blackListed", blackListed); - field_to_json(Obj,"userRole", userRole, UserTypeToString); - field_to_json(Obj,"userTypeProprietaryInfo", userTypeProprietaryInfo); - field_to_json(Obj,"securityPolicy", securityPolicy); - field_to_json(Obj,"securityPolicyChange", securityPolicyChange); - field_to_json(Obj,"currentPassword",currentPassword); - field_to_json(Obj,"lastPasswords",lastPasswords); - field_to_json(Obj,"oauthType",oauthType); - field_to_json(Obj,"oauthUserInfo",oauthUserInfo); - field_to_json(Obj,"modified",modified); - field_to_json(Obj,"signingUp",signingUp); - }; + bool UserInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "avatar", avatar); + field_from_json(Obj, "email", email); + field_from_json(Obj, "validationEmail", validationEmail); + field_from_json(Obj, "validationURI", validationURI); + field_from_json(Obj, "currentLoginURI", currentLoginURI); + field_from_json(Obj, "locale", locale); + field_from_json(Obj, "notes", notes); + field_from_json(Obj, "location", location); + field_from_json(Obj, "owner", owner); + field_from_json(Obj, "userRole", userRole, UserTypeFromString); + field_from_json(Obj, "securityPolicy", securityPolicy); + field_from_json(Obj, "userTypeProprietaryInfo", userTypeProprietaryInfo); + field_from_json(Obj, "validationDate", validationDate); + field_from_json(Obj, "creationDate", creationDate); + field_from_json(Obj, "lastLogin", lastLogin); + field_from_json(Obj, "lastPasswordChange", lastPasswordChange); + field_from_json(Obj, "lastEmailCheck", lastEmailCheck); + field_from_json(Obj, "securityPolicyChange", securityPolicyChange); + field_from_json(Obj, "validated", validated); + field_from_json(Obj, "changePassword", changePassword); + field_from_json(Obj, "waitingForEmailCheck", waitingForEmailCheck); + field_from_json(Obj, "suspended", suspended); + field_from_json(Obj, "blackListed", blackListed); + field_from_json(Obj, "currentPassword", currentPassword); + field_from_json(Obj, "lastPasswords", lastPasswords); + field_from_json(Obj, "oauthType", oauthType); + field_from_json(Obj, "oauthUserInfo", oauthUserInfo); + field_from_json(Obj, "modified", modified); + field_from_json(Obj, "signingUp", signingUp); + return true; + } catch (const Poco::Exception &E) { + std::cout << "Cannot parse: UserInfo" << std::endl; + } + return false; + }; - bool UserInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id",id); - field_from_json(Obj,"name",name); - field_from_json(Obj,"description",description); - field_from_json(Obj,"avatar",avatar); - field_from_json(Obj,"email",email); - field_from_json(Obj,"validationEmail",validationEmail); - field_from_json(Obj,"validationURI",validationURI); - field_from_json(Obj,"currentLoginURI",currentLoginURI); - field_from_json(Obj,"locale",locale); - field_from_json(Obj,"notes",notes); - field_from_json(Obj,"location", location); - field_from_json(Obj,"owner", owner); - field_from_json(Obj,"userRole",userRole, UserTypeFromString); - field_from_json(Obj,"securityPolicy",securityPolicy); - field_from_json(Obj,"userTypeProprietaryInfo",userTypeProprietaryInfo); - field_from_json(Obj,"validationDate",validationDate); - field_from_json(Obj,"creationDate",creationDate); - field_from_json(Obj,"lastLogin",lastLogin); - field_from_json(Obj,"lastPasswordChange",lastPasswordChange); - field_from_json(Obj,"lastEmailCheck",lastEmailCheck); - field_from_json(Obj,"securityPolicyChange",securityPolicyChange); - field_from_json(Obj,"validated",validated); - field_from_json(Obj,"changePassword",changePassword); - field_from_json(Obj,"waitingForEmailCheck",waitingForEmailCheck); - field_from_json(Obj,"suspended",suspended); - field_from_json(Obj,"blackListed",blackListed); - field_from_json(Obj,"currentPassword",currentPassword); - field_from_json(Obj,"lastPasswords",lastPasswords); - field_from_json(Obj,"oauthType",oauthType); - field_from_json(Obj,"oauthUserInfo",oauthUserInfo); - field_from_json(Obj,"modified",modified); - field_from_json(Obj,"signingUp",signingUp); - return true; - } catch (const Poco::Exception &E) { - std::cout << "Cannot parse: UserInfo" << std::endl; - } - return false; - }; + void UserInfoList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "users", users); + } - void UserInfoList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"users",users); - } - - bool UserInfoList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"users",users); - return true; - } catch (...) { - std::cout << "Cannot parse: InternalServiceInfo" << std::endl; - } - return false; - } + bool UserInfoList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "users", users); + return true; + } catch (...) { + std::cout << "Cannot parse: InternalServiceInfo" << std::endl; + } + return false; + } void InternalServiceInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"privateURI",privateURI); - field_to_json(Obj,"publicURI",publicURI); - field_to_json(Obj,"token",token); + field_to_json(Obj, "privateURI", privateURI); + field_to_json(Obj, "publicURI", publicURI); + field_to_json(Obj, "token", token); }; bool InternalServiceInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"privateURI",privateURI); - field_from_json(Obj,"publicURI",publicURI); - field_from_json(Obj,"token",token); + field_from_json(Obj, "privateURI", privateURI); + field_from_json(Obj, "publicURI", publicURI); + field_from_json(Obj, "token", token); return true; } catch (...) { - std::cout << "Cannot parse: InternalServiceInfo" << std::endl; + std::cout << "Cannot parse: InternalServiceInfo" << std::endl; } return false; }; void InternalSystemServices::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"key",key); - field_to_json(Obj,"version",version); - field_to_json(Obj,"services",services); + field_to_json(Obj, "key", key); + field_to_json(Obj, "version", version); + field_to_json(Obj, "services", services); }; - bool InternalSystemServices::from_json(const Poco::JSON::Object::Ptr &Obj) { + bool InternalSystemServices::from_json(const Poco::JSON::Object::Ptr &Obj) { try { field_from_json(Obj, "key", key); field_from_json(Obj, "version", version); field_from_json(Obj, "services", services); return true; - } catch(...) { - std::cout << "Cannot parse: InternalSystemServices" << std::endl; + } catch (...) { + std::cout << "Cannot parse: InternalSystemServices" << std::endl; } return false; }; void SystemEndpoint::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"type",type); - field_to_json(Obj,"id",id); - field_to_json(Obj,"vendor",vendor); - field_to_json(Obj,"uri",uri); - field_to_json(Obj,"authenticationType",authenticationType); + field_to_json(Obj, "type", type); + field_to_json(Obj, "id", id); + field_to_json(Obj, "vendor", vendor); + field_to_json(Obj, "uri", uri); + field_to_json(Obj, "authenticationType", authenticationType); }; bool SystemEndpoint::from_json(const Poco::JSON::Object::Ptr &Obj) { @@ -371,31 +392,31 @@ namespace OpenWifi::SecurityObjects { field_from_json(Obj, "authenticationType", authenticationType); return true; } catch (...) { - std::cout << "Cannot parse: SystemEndpoint" << std::endl; + std::cout << "Cannot parse: SystemEndpoint" << std::endl; } return false; }; void SystemEndpointList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"endpoints",endpoints); + field_to_json(Obj, "endpoints", endpoints); } - bool SystemEndpointList::from_json(const Poco::JSON::Object::Ptr &Obj) { + bool SystemEndpointList::from_json(const Poco::JSON::Object::Ptr &Obj) { try { field_from_json(Obj, "endpoints", endpoints); return true; } catch (...) { - std::cout << "Cannot parse: SystemEndpointList" << std::endl; + std::cout << "Cannot parse: SystemEndpointList" << std::endl; } return false; } void UserInfoAndPolicy::to_json(Poco::JSON::Object &Obj) const { - Poco::JSON::Object UI, TI; + Poco::JSON::Object UI, TI; userinfo.to_json(UI); webtoken.to_json(TI); - Obj.set("tokenInfo",TI); - Obj.set("userInfo",UI); + Obj.set("tokenInfo", TI); + Obj.set("userInfo", UI); } bool UserInfoAndPolicy::from_json(const Poco::JSON::Object::Ptr &Obj) { @@ -403,91 +424,97 @@ namespace OpenWifi::SecurityObjects { field_from_json(Obj, "tokenInfo", webtoken); field_from_json(Obj, "userInfo", userinfo); return true; - } catch(...) { - std::cout << "Cannot parse: UserInfoAndPolicy" << std::endl; + } catch (...) { + std::cout << "Cannot parse: UserInfoAndPolicy" << std::endl; } return false; } void NoteInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"created", created); - field_to_json(Obj,"createdBy", createdBy); - field_to_json(Obj,"note", note); + field_to_json(Obj, "created", created); + field_to_json(Obj, "createdBy", createdBy); + field_to_json(Obj, "note", note); } bool NoteInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"created",created); - field_from_json(Obj,"createdBy",createdBy); - field_from_json(Obj,"note", note); + field_from_json(Obj, "created", created); + field_from_json(Obj, "createdBy", createdBy); + field_from_json(Obj, "note", note); return true; - } catch(...) { - std::cout << "Cannot parse: NoteInfo" << std::endl; + } catch (...) { + std::cout << "Cannot parse: NoteInfo" << std::endl; } return false; } - bool MergeNotes(Poco::JSON::Object::Ptr Obj, const UserInfo &UInfo, NoteInfoVec & Notes) { - try { - if(Obj->has("notes") && Obj->isArray("notes")) { - SecurityObjects::NoteInfoVec NIV; - NIV = RESTAPI_utils::to_object_array(Obj->get("notes").toString()); - for(auto const &i:NIV) { - SecurityObjects::NoteInfo ii{.created=(uint64_t)Utils::Now(), .createdBy=UInfo.email, .note=i.note}; - Notes.push_back(ii); - } - } - return true; - } catch(...) { - std::cout << "Cannot parse: MergeNotes" << std::endl; - } - return false; + bool MergeNotes(Poco::JSON::Object::Ptr Obj, const UserInfo &UInfo, NoteInfoVec &Notes) { + try { + if (Obj->has("notes") && Obj->isArray("notes")) { + SecurityObjects::NoteInfoVec NIV; + NIV = RESTAPI_utils::to_object_array( + Obj->get("notes").toString()); + for (auto const &i : NIV) { + SecurityObjects::NoteInfo ii{.created = (uint64_t)Utils::Now(), + .createdBy = UInfo.email, + .note = i.note}; + Notes.push_back(ii); + } + } + return true; + } catch (...) { + std::cout << "Cannot parse: MergeNotes" << std::endl; + } + return false; } - bool MergeNotes(const NoteInfoVec & NewNotes, const UserInfo &UInfo, NoteInfoVec & ExistingNotes) { - for(auto const &i:NewNotes) { - SecurityObjects::NoteInfo ii{.created=(uint64_t)Utils::Now(), .createdBy=UInfo.email, .note=i.note}; - ExistingNotes.push_back(ii); - } - return true; + bool MergeNotes(const NoteInfoVec &NewNotes, const UserInfo &UInfo, + NoteInfoVec &ExistingNotes) { + for (auto const &i : NewNotes) { + SecurityObjects::NoteInfo ii{ + .created = (uint64_t)Utils::Now(), .createdBy = UInfo.email, .note = i.note}; + ExistingNotes.push_back(ii); + } + return true; } void ProfileAction::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"resource", resource); - field_to_json(Obj,"access", access, ResourceAccessTypeToString); + field_to_json(Obj, "resource", resource); + field_to_json(Obj, "access", access, ResourceAccessTypeToString); } bool ProfileAction::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"resource",resource); - field_from_json(Obj,"access",access,ResourceAccessTypeFromString ); + field_from_json(Obj, "resource", resource); + field_from_json(Obj, "access", access, + ResourceAccessTypeFromString); return true; - } catch(...) { - std::cout << "Cannot parse: ProfileAction" << std::endl; + } catch (...) { + std::cout << "Cannot parse: ProfileAction" << std::endl; } return false; } void SecurityProfile::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id", id); - field_to_json(Obj,"name", name); - field_to_json(Obj,"description", description); - field_to_json(Obj,"policy", policy); - field_to_json(Obj,"role", role); - field_to_json(Obj,"notes", notes); + field_to_json(Obj, "id", id); + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "policy", policy); + field_to_json(Obj, "role", role); + field_to_json(Obj, "notes", notes); } bool SecurityProfile::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"id",id); - field_from_json(Obj,"name",name); - field_from_json(Obj,"description",description); - field_from_json(Obj,"policy",policy); - field_from_json(Obj,"role",role); - field_from_json(Obj,"notes",notes); + field_from_json(Obj, "id", id); + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "policy", policy); + field_from_json(Obj, "role", role); + field_from_json(Obj, "notes", notes); return true; - } catch(...) { - std::cout << "Cannot parse: SecurityProfile" << std::endl; + } catch (...) { + std::cout << "Cannot parse: SecurityProfile" << std::endl; } return false; } @@ -498,201 +525,200 @@ namespace OpenWifi::SecurityObjects { bool SecurityProfileList::from_json(const Poco::JSON::Object::Ptr &Obj) { try { - field_from_json(Obj,"profiles",profiles); + field_from_json(Obj, "profiles", profiles); return true; - } catch(...) { - std::cout << "Cannot parse: SecurityProfileList" << std::endl; + } catch (...) { + std::cout << "Cannot parse: SecurityProfileList" << std::endl; } return false; } - void ActionLink::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id",id); - field_to_json(Obj,"action",action); - field_to_json(Obj,"userId",userId); - field_to_json(Obj,"actionTemplate",actionTemplate); - field_to_json(Obj,"variables",variables); - field_to_json(Obj,"locale",locale); - field_to_json(Obj,"message",message); - field_to_json(Obj,"sent",sent); - field_to_json(Obj,"created",created); - field_to_json(Obj,"expires",expires); - field_to_json(Obj,"completed",completed); - field_to_json(Obj,"canceled",canceled); - field_to_json(Obj,"userAction",userAction); + void ActionLink::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "action", action); + field_to_json(Obj, "userId", userId); + field_to_json(Obj, "actionTemplate", actionTemplate); + field_to_json(Obj, "variables", variables); + field_to_json(Obj, "locale", locale); + field_to_json(Obj, "message", message); + field_to_json(Obj, "sent", sent); + field_to_json(Obj, "created", created); + field_to_json(Obj, "expires", expires); + field_to_json(Obj, "completed", completed); + field_to_json(Obj, "canceled", canceled); + field_to_json(Obj, "userAction", userAction); } - bool ActionLink::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id",id); - field_from_json(Obj,"action",action); - field_from_json(Obj,"userId",userId); - field_from_json(Obj,"actionTemplate",actionTemplate); - field_from_json(Obj,"variables",variables); - field_from_json(Obj,"locale",locale); - field_from_json(Obj,"message",message); - field_from_json(Obj,"sent",sent); - field_from_json(Obj,"created",created); - field_from_json(Obj,"expires",expires); - field_from_json(Obj,"completed",completed); - field_from_json(Obj,"canceled",canceled); - field_from_json(Obj,"userAction",userAction); - return true; - } catch(...) { - std::cout << "Cannot parse: ActionLink" << std::endl; - } - return false; + bool ActionLink::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "action", action); + field_from_json(Obj, "userId", userId); + field_from_json(Obj, "actionTemplate", actionTemplate); + field_from_json(Obj, "variables", variables); + field_from_json(Obj, "locale", locale); + field_from_json(Obj, "message", message); + field_from_json(Obj, "sent", sent); + field_from_json(Obj, "created", created); + field_from_json(Obj, "expires", expires); + field_from_json(Obj, "completed", completed); + field_from_json(Obj, "canceled", canceled); + field_from_json(Obj, "userAction", userAction); + return true; + } catch (...) { + std::cout << "Cannot parse: ActionLink" << std::endl; + } + return false; } - void Preferences::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id",id); - field_to_json(Obj,"modified",modified); - field_to_json(Obj,"data",data); + void Preferences::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "modified", modified); + field_to_json(Obj, "data", data); } - bool Preferences::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id",id); - field_from_json(Obj,"modified",modified); - field_from_json(Obj,"data",data); - return true; - } catch(...) { - std::cout << "Cannot parse: Preferences" << std::endl; - } - return false; + bool Preferences::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "modified", modified); + field_from_json(Obj, "data", data); + return true; + } catch (...) { + std::cout << "Cannot parse: Preferences" << std::endl; + } + return false; } - void SubMfaConfig::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id",id); - field_to_json(Obj,"type",type); - field_to_json(Obj,"sms",sms); - field_to_json(Obj,"email",email); + void SubMfaConfig::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "type", type); + field_to_json(Obj, "sms", sms); + field_to_json(Obj, "email", email); } - bool SubMfaConfig::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id",id); - field_from_json(Obj,"type",type); - field_from_json(Obj,"sms",sms); - field_from_json(Obj,"email",email); - return true; - } catch(...) { - std::cout << "Cannot parse: SubMfaConfig" << std::endl; - } - return false; + bool SubMfaConfig::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "type", type); + field_from_json(Obj, "sms", sms); + field_from_json(Obj, "email", email); + return true; + } catch (...) { + std::cout << "Cannot parse: SubMfaConfig" << std::endl; + } + return false; } - void Token::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"token",token); - field_to_json(Obj,"refreshToken",refreshToken); - field_to_json(Obj,"tokenType",tokenType); - field_to_json(Obj,"userName",userName); - field_to_json(Obj,"created",created); - field_to_json(Obj,"expires",expires); - field_to_json(Obj,"idleTimeout",idleTimeout); - field_to_json(Obj,"revocationDate",revocationDate); - field_to_json(Obj,"lastRefresh", lastRefresh); - } + void Token::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "token", token); + field_to_json(Obj, "refreshToken", refreshToken); + field_to_json(Obj, "tokenType", tokenType); + field_to_json(Obj, "userName", userName); + field_to_json(Obj, "created", created); + field_to_json(Obj, "expires", expires); + field_to_json(Obj, "idleTimeout", idleTimeout); + field_to_json(Obj, "revocationDate", revocationDate); + field_to_json(Obj, "lastRefresh", lastRefresh); + } - bool Token::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"token",token); - field_from_json(Obj,"refreshToken",refreshToken); - field_from_json(Obj,"tokenType",tokenType); - field_from_json(Obj,"userName",userName); - field_from_json(Obj,"created",created); - field_from_json(Obj,"expires",expires); - field_from_json(Obj,"idleTimeout",idleTimeout); - field_from_json(Obj,"revocationDate",revocationDate); - field_from_json(Obj,"lastRefresh", lastRefresh); - return true; - } catch(...) { - std::cout << "Cannot parse: Token" << std::endl; - } - return false; - } + bool Token::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "token", token); + field_from_json(Obj, "refreshToken", refreshToken); + field_from_json(Obj, "tokenType", tokenType); + field_from_json(Obj, "userName", userName); + field_from_json(Obj, "created", created); + field_from_json(Obj, "expires", expires); + field_from_json(Obj, "idleTimeout", idleTimeout); + field_from_json(Obj, "revocationDate", revocationDate); + field_from_json(Obj, "lastRefresh", lastRefresh); + return true; + } catch (...) { + std::cout << "Cannot parse: Token" << std::endl; + } + return false; + } - void LoginRecordInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"sessionId",sessionId); - field_to_json(Obj,"userId",userId); - field_to_json(Obj,"email",email); - field_to_json(Obj,"login",login); - field_to_json(Obj,"logout",logout); - } + void LoginRecordInfo::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "sessionId", sessionId); + field_to_json(Obj, "userId", userId); + field_to_json(Obj, "email", email); + field_to_json(Obj, "login", login); + field_to_json(Obj, "logout", logout); + } - void ApiKeyAccessRight::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "service", service); - field_to_json(Obj, "access", access); - } + void ApiKeyAccessRight::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "service", service); + field_to_json(Obj, "access", access); + } - bool ApiKeyAccessRight::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "service", service); - field_from_json(Obj, "access", access); - return true; - } catch(...) { - std::cout << "Cannot parse: Token" << std::endl; - } - return false; - } + bool ApiKeyAccessRight::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "service", service); + field_from_json(Obj, "access", access); + return true; + } catch (...) { + std::cout << "Cannot parse: Token" << std::endl; + } + return false; + } - void ApiKeyAccessRightList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "acls", acls); - } + void ApiKeyAccessRightList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "acls", acls); + } - bool ApiKeyAccessRightList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "acls", acls); - return true; - } catch(...) { - std::cout << "Cannot parse: Token" << std::endl; - } - return false; - } + bool ApiKeyAccessRightList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "acls", acls); + return true; + } catch (...) { + std::cout << "Cannot parse: Token" << std::endl; + } + return false; + } - void ApiKeyEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "id", id); - field_to_json(Obj, "userUuid", userUuid); - field_to_json(Obj, "name", name); - field_to_json(Obj, "apiKey", apiKey); - field_to_json(Obj, "salt", salt); - field_to_json(Obj, "description", description); - field_to_json(Obj, "expiresOn", expiresOn); - field_to_json(Obj, "rights", rights); - field_to_json(Obj, "lastUse", lastUse); - } + void ApiKeyEntry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "userUuid", userUuid); + field_to_json(Obj, "name", name); + field_to_json(Obj, "apiKey", apiKey); + field_to_json(Obj, "salt", salt); + field_to_json(Obj, "description", description); + field_to_json(Obj, "expiresOn", expiresOn); + field_to_json(Obj, "rights", rights); + field_to_json(Obj, "lastUse", lastUse); + } - bool ApiKeyEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "id", id); - field_from_json(Obj, "userUuid", userUuid); - field_from_json(Obj, "name", name); - field_from_json(Obj, "apiKey", apiKey); - field_from_json(Obj, "salt", salt); - field_from_json(Obj, "description", description); - field_from_json(Obj, "expiresOn", expiresOn); - field_from_json(Obj, "rights", rights); - field_from_json(Obj, "lastUse", lastUse); - return true; - } catch(...) { - std::cout << "Cannot parse: Token" << std::endl; - } - return false; - } + bool ApiKeyEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "userUuid", userUuid); + field_from_json(Obj, "name", name); + field_from_json(Obj, "apiKey", apiKey); + field_from_json(Obj, "salt", salt); + field_from_json(Obj, "description", description); + field_from_json(Obj, "expiresOn", expiresOn); + field_from_json(Obj, "rights", rights); + field_from_json(Obj, "lastUse", lastUse); + return true; + } catch (...) { + std::cout << "Cannot parse: Token" << std::endl; + } + return false; + } - void ApiKeyEntryList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "apiKeys", apiKeys); - } + void ApiKeyEntryList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "apiKeys", apiKeys); + } - bool ApiKeyEntryList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "apiKeys", apiKeys); - return true; - } catch(...) { - std::cout << "Cannot parse: Token" << std::endl; - } - return false; - } - -} + bool ApiKeyEntryList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "apiKeys", apiKeys); + return true; + } catch (...) { + std::cout << "Cannot parse: Token" << std::endl; + } + return false; + } +} // namespace OpenWifi::SecurityObjects diff --git a/src/RESTObjects/RESTAPI_SecurityObjects.h b/src/RESTObjects/RESTAPI_SecurityObjects.h index a804128..649fb49 100644 --- a/src/RESTObjects/RESTAPI_SecurityObjects.h +++ b/src/RESTObjects/RESTAPI_SecurityObjects.h @@ -8,361 +8,364 @@ #pragma once -#include -#include -#include "framework/OpenWifiTypes.h" -#include "Poco/JSON/Object.h" #include "Poco/Data/LOB.h" #include "Poco/Data/LOBStream.h" +#include "Poco/JSON/Object.h" +#include "framework/OpenWifiTypes.h" #include "framework/utils.h" +#include +#include namespace OpenWifi { - uint64_t Now(); - namespace SecurityObjects { - - typedef std::string USER_ID_TYPE; + uint64_t Now(); + namespace SecurityObjects { - struct AclTemplate { - bool Read_ = true; - bool ReadWrite_ = true; - bool ReadWriteCreate_ = true; - bool Delete_ = true; - bool PortalLogin_ = true; + typedef std::string USER_ID_TYPE; - AclTemplate() noexcept = default; + struct AclTemplate { + bool Read_ = true; + bool ReadWrite_ = true; + bool ReadWriteCreate_ = true; + bool Delete_ = true; + bool PortalLogin_ = true; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + AclTemplate() noexcept = default; - static_assert( std::is_nothrow_move_constructible_v ); + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct WebToken { - std::string access_token_; - std::string refresh_token_; - std::string id_token_; - std::string token_type_; - std::string username_; - bool userMustChangePassword=false; - uint64_t errorCode=0; - uint64_t expires_in_=0; - uint64_t idle_timeout_=0; - AclTemplate acl_template_; - uint64_t created_=0; - uint64_t lastRefresh_=0; + static_assert(std::is_nothrow_move_constructible_v); - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct WebToken { + std::string access_token_; + std::string refresh_token_; + std::string id_token_; + std::string token_type_; + std::string username_; + bool userMustChangePassword = false; + uint64_t errorCode = 0; + uint64_t expires_in_ = 0; + uint64_t idle_timeout_ = 0; + AclTemplate acl_template_; + uint64_t created_ = 0; + uint64_t lastRefresh_ = 0; - enum USER_ROLE { - UNKNOWN, ROOT, ADMIN, SUBSCRIBER, CSR, SYSTEM, INSTALLER, NOC, ACCOUNTING, PARTNER - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - USER_ROLE UserTypeFromString(const std::string &U); - std::string UserTypeToString(USER_ROLE U); + enum USER_ROLE { + UNKNOWN, + ROOT, + ADMIN, + SUBSCRIBER, + CSR, + SYSTEM, + INSTALLER, + NOC, + ACCOUNTING, + PARTNER + }; - struct NoteInfo { - uint64_t created=0; // = Utils::Now(); - std::string createdBy; - std::string note; + USER_ROLE UserTypeFromString(const std::string &U); + std::string UserTypeToString(USER_ROLE U); - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector NoteInfoVec; + struct NoteInfo { + uint64_t created = 0; // = Utils::Now(); + std::string createdBy; + std::string note; - struct MobilePhoneNumber { - std::string number; - bool verified = false; - bool primary = false; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector NoteInfoVec; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct MobilePhoneNumber { + std::string number; + bool verified = false; + bool primary = false; - struct MfaAuthInfo { - bool enabled = false; - std::string method; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct MfaAuthInfo { + bool enabled = false; + std::string method; - struct UserLoginLoginExtensions { - std::vector mobiles; - struct MfaAuthInfo mfa; - std::string authenticatorSecret; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct UserLoginLoginExtensions { + std::vector mobiles; + struct MfaAuthInfo mfa; + std::string authenticatorSecret; - struct MFAChallengeRequest { - std::string uuid; - std::string question; - std::string method; - uint64_t created = Utils::Now(); + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct MFAChallengeRequest { + std::string uuid; + std::string question; + std::string method; + uint64_t created = Utils::Now(); - struct MFAChallengeResponse { - std::string uuid; - std::string answer; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct MFAChallengeResponse { + std::string uuid; + std::string answer; - struct UserInfo { - std::string id; - std::string name; - std::string description; - std::string avatar; - std::string email; - bool validated = false; - std::string validationEmail; - uint64_t validationDate = 0; - uint64_t creationDate = 0; - std::string validationURI; - bool changePassword = false; - uint64_t lastLogin = 0; - std::string currentLoginURI; - uint64_t lastPasswordChange = 0; - uint64_t lastEmailCheck = 0; - bool waitingForEmailCheck = false; - std::string locale; - NoteInfoVec notes; - std::string location; - std::string owner; - bool suspended = false; - bool blackListed = false; - USER_ROLE userRole; - UserLoginLoginExtensions userTypeProprietaryInfo; - std::string securityPolicy; - uint64_t securityPolicyChange = 0 ; - std::string currentPassword; - OpenWifi::Types::StringVec lastPasswords; - std::string oauthType; - std::string oauthUserInfo; - uint64_t modified; - std::string signingUp; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector UserInfoVec; + struct UserInfo { + std::string id; + std::string name; + std::string description; + std::string avatar; + std::string email; + bool validated = false; + std::string validationEmail; + uint64_t validationDate = 0; + uint64_t creationDate = 0; + std::string validationURI; + bool changePassword = false; + uint64_t lastLogin = 0; + std::string currentLoginURI; + uint64_t lastPasswordChange = 0; + uint64_t lastEmailCheck = 0; + bool waitingForEmailCheck = false; + std::string locale; + NoteInfoVec notes; + std::string location; + std::string owner; + bool suspended = false; + bool blackListed = false; + USER_ROLE userRole; + UserLoginLoginExtensions userTypeProprietaryInfo; + std::string securityPolicy; + uint64_t securityPolicyChange = 0; + std::string currentPassword; + OpenWifi::Types::StringVec lastPasswords; + std::string oauthType; + std::string oauthUserInfo; + uint64_t modified; + std::string signingUp; - struct UserInfoList { - std::vector users; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector UserInfoVec; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct UserInfoList { + std::vector users; - // bool append_from_json(Poco::JSON::Object::Ptr Obj, const UserInfo &UInfo, NoteInfoVec & Notes); - bool MergeNotes(Poco::JSON::Object::Ptr Obj, const UserInfo &UInfo, NoteInfoVec & Notes); - bool MergeNotes(const NoteInfoVec & NewNotes, const UserInfo &UInfo, NoteInfoVec & ExistingNotes); + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct InternalServiceInfo { - std::string privateURI; - std::string publicURI; - std::string token; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector InternalServiceInfoVec; + // bool append_from_json(Poco::JSON::Object::Ptr Obj, const UserInfo &UInfo, NoteInfoVec & + // Notes); + bool MergeNotes(Poco::JSON::Object::Ptr Obj, const UserInfo &UInfo, NoteInfoVec &Notes); + bool MergeNotes(const NoteInfoVec &NewNotes, const UserInfo &UInfo, + NoteInfoVec &ExistingNotes); - struct InternalSystemServices { - std::string key; - std::string version; - InternalServiceInfoVec services; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct InternalServiceInfo { + std::string privateURI; + std::string publicURI; + std::string token; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector InternalServiceInfoVec; - struct SystemEndpoint { - std::string type; - uint64_t id = 0; - std::string vendor{"OpenWiFi"}; - std::string uri; - std::string authenticationType{"internal_v1"}; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector SystemEndpointVec; + struct InternalSystemServices { + std::string key; + std::string version; + InternalServiceInfoVec services; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct SystemEndpointList { - SystemEndpointVec endpoints; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct SystemEndpoint { + std::string type; + uint64_t id = 0; + std::string vendor{"OpenWiFi"}; + std::string uri; + std::string authenticationType{"internal_v1"}; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector SystemEndpointVec; - struct UserInfoAndPolicy { - WebToken webtoken; - UserInfo userinfo; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::map UserInfoCache; + struct SystemEndpointList { + SystemEndpointVec endpoints; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - enum ResourceAccessType { - NONE, - READ, - MODIFY, - DELETE, - CREATE, - TEST, - MOVE - }; + struct UserInfoAndPolicy { + WebToken webtoken; + UserInfo userinfo; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::map UserInfoCache; - ResourceAccessType ResourceAccessTypeFromString(const std::string &s); - std::string ResourceAccessTypeToString(const ResourceAccessType & T); + enum ResourceAccessType { NONE, READ, MODIFY, DELETE, CREATE, TEST, MOVE }; - struct ProfileAction { - std::string resource; - ResourceAccessType access; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector ProfileActionVec; + ResourceAccessType ResourceAccessTypeFromString(const std::string &s); + std::string ResourceAccessTypeToString(const ResourceAccessType &T); - struct SecurityProfile { - uint64_t id=0; - std::string name; - std::string description; - ProfileActionVec policy; - std::string role; - NoteInfoVec notes; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - typedef std::vector SecurityProfileVec; + struct ProfileAction { + std::string resource; + ResourceAccessType access; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector ProfileActionVec; - struct SecurityProfileList { - SecurityProfileVec profiles; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct SecurityProfile { + uint64_t id = 0; + std::string name; + std::string description; + ProfileActionVec policy; + std::string role; + NoteInfoVec notes; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + typedef std::vector SecurityProfileVec; - enum LinkActions { - FORGOT_PASSWORD=1, - VERIFY_EMAIL, - SUB_FORGOT_PASSWORD, - SUB_VERIFY_EMAIL, - SUB_SIGNUP, - EMAIL_INVITATION - }; + struct SecurityProfileList { + SecurityProfileVec profiles; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct ActionLink { - std::string id; - uint64_t action; - std::string userId; - std::string actionTemplate; - Types::StringPairVec variables; - std::string locale; - std::string message; - uint64_t sent=0; - uint64_t created=Utils::Now(); - uint64_t expires=0; - uint64_t completed=0; - uint64_t canceled=0; - bool userAction=true; + enum LinkActions { + FORGOT_PASSWORD = 1, + VERIFY_EMAIL, + SUB_FORGOT_PASSWORD, + SUB_VERIFY_EMAIL, + SUB_SIGNUP, + EMAIL_INVITATION + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct ActionLink { + std::string id; + uint64_t action; + std::string userId; + std::string actionTemplate; + Types::StringPairVec variables; + std::string locale; + std::string message; + uint64_t sent = 0; + uint64_t created = Utils::Now(); + uint64_t expires = 0; + uint64_t completed = 0; + uint64_t canceled = 0; + bool userAction = true; - struct Preferences { - std::string id; - uint64_t modified; - Types::StringPairVec data; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct SubMfaConfig { - std::string id; - std::string type; - std::string sms; - std::string email; + struct Preferences { + std::string id; + uint64_t modified; + Types::StringPairVec data; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct SubMfaConfig { + std::string id; + std::string type; + std::string sms; + std::string email; - struct Token { - std::string token; - std::string refreshToken; - std::string tokenType; - std::string userName; - uint64_t created=0; - uint64_t expires=0; - uint64_t idleTimeout=0; - uint64_t revocationDate=0; - uint64_t lastRefresh=0; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct Token { + std::string token; + std::string refreshToken; + std::string tokenType; + std::string userName; + uint64_t created = 0; + uint64_t expires = 0; + uint64_t idleTimeout = 0; + uint64_t revocationDate = 0; + uint64_t lastRefresh = 0; - struct Avatar { - std::string id; - std::string type; - uint64_t created=0; - std::string name; - Poco::Data::BLOB avatar; - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct LoginRecordInfo { - std::string sessionId; - std::string userId; - std::string email; - uint64_t login=0; - uint64_t logout=0; + struct Avatar { + std::string id; + std::string type; + uint64_t created = 0; + std::string name; + Poco::Data::BLOB avatar; + }; - void to_json(Poco::JSON::Object &Obj) const; - }; + struct LoginRecordInfo { + std::string sessionId; + std::string userId; + std::string email; + uint64_t login = 0; + uint64_t logout = 0; - struct ApiKeyAccessRight { - std::string service; - std::string access; + void to_json(Poco::JSON::Object &Obj) const; + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct ApiKeyAccessRight { + std::string service; + std::string access; - struct ApiKeyAccessRightList { - std::vector acls; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct ApiKeyAccessRightList { + std::vector acls; - struct ApiKeyEntry { - Types::UUID_t id; - Types::UUID_t userUuid; - std::string name; - std::string description; - std::string apiKey; - std::string salt; - std::uint64_t created; - std::uint64_t expiresOn=0; - ApiKeyAccessRightList rights; - std::uint64_t lastUse=0; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct ApiKeyEntry { + Types::UUID_t id; + Types::UUID_t userUuid; + std::string name; + std::string description; + std::string apiKey; + std::string salt; + std::uint64_t created; + std::uint64_t expiresOn = 0; + ApiKeyAccessRightList rights; + std::uint64_t lastUse = 0; - struct ApiKeyEntryList { - std::vector apiKeys; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + struct ApiKeyEntryList { + std::vector apiKeys; - } -} + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; + + } // namespace SecurityObjects +} // namespace OpenWifi diff --git a/src/RESTObjects/RESTAPI_SubObjects.cpp b/src/RESTObjects/RESTAPI_SubObjects.cpp index a362b15..fabd513 100644 --- a/src/RESTObjects/RESTAPI_SubObjects.cpp +++ b/src/RESTObjects/RESTAPI_SubObjects.cpp @@ -5,598 +5,598 @@ #include "RESTAPI_SubObjects.h" #include "framework/RESTAPI_utils.h" -using OpenWifi::RESTAPI_utils::field_to_json; using OpenWifi::RESTAPI_utils::field_from_json; +using OpenWifi::RESTAPI_utils::field_to_json; namespace OpenWifi::SubObjects { - void HomeDeviceMode::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "enableLEDS", enableLEDS); - field_to_json(Obj, "type", type); - field_to_json(Obj, "subnet", subnet); - field_to_json(Obj, "subnetMask", subnetMask); - field_to_json(Obj, "startIP", startIP); - field_to_json(Obj, "endIP", endIP); - field_to_json(Obj, "created", created); - field_to_json(Obj, "modified", modified); - field_to_json(Obj, "subnetV6", subnetV6); - field_to_json(Obj, "subnetMaskV6", subnetMaskV6); - field_to_json(Obj, "startIPV6", startIPV6); - field_to_json(Obj, "endIPV6", endIPV6); - } + void HomeDeviceMode::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "enableLEDS", enableLEDS); + field_to_json(Obj, "type", type); + field_to_json(Obj, "subnet", subnet); + field_to_json(Obj, "subnetMask", subnetMask); + field_to_json(Obj, "startIP", startIP); + field_to_json(Obj, "endIP", endIP); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + field_to_json(Obj, "subnetV6", subnetV6); + field_to_json(Obj, "subnetMaskV6", subnetMaskV6); + field_to_json(Obj, "startIPV6", startIPV6); + field_to_json(Obj, "endIPV6", endIPV6); + } - bool HomeDeviceMode::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "enableLEDS", enableLEDS); - field_from_json(Obj, "type", type); - field_from_json(Obj, "subnet", subnet); - field_from_json(Obj, "subnetMask", subnetMask); - field_from_json(Obj, "startIP", startIP); - field_from_json(Obj, "endIP", endIP); - field_from_json(Obj, "created", created); - field_from_json(Obj, "modified", modified); - field_from_json(Obj, "subnetV6", subnetV6); - field_from_json(Obj, "subnetMaskV6", subnetMaskV6); - field_from_json(Obj, "startIPV6", startIPV6); - field_from_json(Obj, "endIPV6", endIPV6); - return true; - } catch (...) { - } - return false; - } + bool HomeDeviceMode::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "enableLEDS", enableLEDS); + field_from_json(Obj, "type", type); + field_from_json(Obj, "subnet", subnet); + field_from_json(Obj, "subnetMask", subnetMask); + field_from_json(Obj, "startIP", startIP); + field_from_json(Obj, "endIP", endIP); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + field_from_json(Obj, "subnetV6", subnetV6); + field_from_json(Obj, "subnetMaskV6", subnetMaskV6); + field_from_json(Obj, "startIPV6", startIPV6); + field_from_json(Obj, "endIPV6", endIPV6); + return true; + } catch (...) { + } + return false; + } - void IPReservation::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "nickname", nickname); - field_to_json(Obj, "ipAddress", ipAddress); - field_to_json(Obj, "macAddress", macAddress); - } + void IPReservation::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "nickname", nickname); + field_to_json(Obj, "ipAddress", ipAddress); + field_to_json(Obj, "macAddress", macAddress); + } - bool IPReservation::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "nickname", nickname); - field_from_json(Obj, "ipAddress", ipAddress); - field_from_json(Obj, "macAddress", macAddress); - return true; - } catch (...) { - } - return false; - } + bool IPReservation::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "nickname", nickname); + field_from_json(Obj, "ipAddress", ipAddress); + field_from_json(Obj, "macAddress", macAddress); + return true; + } catch (...) { + } + return false; + } - void IPReservationList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "id", id); - field_to_json(Obj, "reservations", reservations); - field_to_json(Obj, "created", created); - field_to_json(Obj, "modified", modified); - } + void IPReservationList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "reservations", reservations); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + } - bool IPReservationList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "id", id); - field_from_json(Obj, "reservations", reservations); - field_from_json(Obj, "created", created); - field_from_json(Obj, "modified", modified); - return true; - } catch (...) { - } - return false; - } + bool IPReservationList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "reservations", reservations); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + return true; + } catch (...) { + } + return false; + } - void DnsConfiguration::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "ISP", ISP); - field_to_json(Obj, "custom", custom); - field_to_json(Obj, "primary", primary); - field_to_json(Obj, "secondary", secondary); - field_to_json(Obj, "primaryV6", primaryV6); - field_to_json(Obj, "secondaryV6", secondaryV6); - } + void DnsConfiguration::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "ISP", ISP); + field_to_json(Obj, "custom", custom); + field_to_json(Obj, "primary", primary); + field_to_json(Obj, "secondary", secondary); + field_to_json(Obj, "primaryV6", primaryV6); + field_to_json(Obj, "secondaryV6", secondaryV6); + } - bool DnsConfiguration::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "ISP", ISP); - field_from_json(Obj, "custom", custom); - field_from_json(Obj, "primary", primary); - field_from_json(Obj, "secondary", secondary); - field_from_json(Obj, "primaryV6", primaryV6); - field_from_json(Obj, "secondaryV6", secondaryV6); - return true; - } catch (...) { - } - return false; - } + bool DnsConfiguration::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "ISP", ISP); + field_from_json(Obj, "custom", custom); + field_from_json(Obj, "primary", primary); + field_from_json(Obj, "secondary", secondary); + field_from_json(Obj, "primaryV6", primaryV6); + field_from_json(Obj, "secondaryV6", secondaryV6); + return true; + } catch (...) { + } + return false; + } - void InternetConnection::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "type", type); - field_to_json(Obj, "username", username); - field_to_json(Obj, "password", password); - field_to_json(Obj, "ipAddress", ipAddress); - field_to_json(Obj, "subnetMask", subnetMask); - field_to_json(Obj, "defaultGateway", defaultGateway); - field_to_json(Obj, "sendHostname", sendHostname); - field_to_json(Obj, "primaryDns", primaryDns); - field_to_json(Obj, "secondaryDns", secondaryDns); - field_to_json(Obj, "created", created); - field_to_json(Obj, "modified", modified); - field_to_json(Obj, "ipV6Support", ipV6Support); - field_to_json(Obj, "ipAddressV6", ipAddressV6); - field_to_json(Obj, "subnetMaskV6", subnetMaskV6); - field_to_json(Obj, "defaultGatewayV6", defaultGatewayV6); - field_to_json(Obj, "primaryDnsV6", primaryDnsV6); - field_to_json(Obj, "secondaryDnsV6", secondaryDnsV6); - } + void InternetConnection::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "type", type); + field_to_json(Obj, "username", username); + field_to_json(Obj, "password", password); + field_to_json(Obj, "ipAddress", ipAddress); + field_to_json(Obj, "subnetMask", subnetMask); + field_to_json(Obj, "defaultGateway", defaultGateway); + field_to_json(Obj, "sendHostname", sendHostname); + field_to_json(Obj, "primaryDns", primaryDns); + field_to_json(Obj, "secondaryDns", secondaryDns); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + field_to_json(Obj, "ipV6Support", ipV6Support); + field_to_json(Obj, "ipAddressV6", ipAddressV6); + field_to_json(Obj, "subnetMaskV6", subnetMaskV6); + field_to_json(Obj, "defaultGatewayV6", defaultGatewayV6); + field_to_json(Obj, "primaryDnsV6", primaryDnsV6); + field_to_json(Obj, "secondaryDnsV6", secondaryDnsV6); + } - bool InternetConnection::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "type", type); - field_from_json(Obj, "username", username); - field_from_json(Obj, "password", password); - field_from_json(Obj, "ipAddress", ipAddress); - field_from_json(Obj, "subnetMask", subnetMask); - field_from_json(Obj, "defaultGateway", defaultGateway); - field_from_json(Obj, "sendHostname", sendHostname); - field_from_json(Obj, "primaryDns", primaryDns); - field_from_json(Obj, "secondaryDns", secondaryDns); - field_from_json(Obj, "created", created); - field_from_json(Obj, "modified", modified); - field_from_json(Obj, "ipV6Support", ipV6Support); - field_from_json(Obj, "ipAddressV6", ipAddressV6); - field_from_json(Obj, "subnetMaskV6", subnetMaskV6); - field_from_json(Obj, "defaultGatewayV6", defaultGatewayV6); - field_from_json(Obj, "primaryDnsV6", primaryDnsV6); - field_from_json(Obj, "secondaryDnsV6", secondaryDnsV6); - return true; - } catch (...) { - } - return false; - } + bool InternetConnection::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "type", type); + field_from_json(Obj, "username", username); + field_from_json(Obj, "password", password); + field_from_json(Obj, "ipAddress", ipAddress); + field_from_json(Obj, "subnetMask", subnetMask); + field_from_json(Obj, "defaultGateway", defaultGateway); + field_from_json(Obj, "sendHostname", sendHostname); + field_from_json(Obj, "primaryDns", primaryDns); + field_from_json(Obj, "secondaryDns", secondaryDns); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + field_from_json(Obj, "ipV6Support", ipV6Support); + field_from_json(Obj, "ipAddressV6", ipAddressV6); + field_from_json(Obj, "subnetMaskV6", subnetMaskV6); + field_from_json(Obj, "defaultGatewayV6", defaultGatewayV6); + field_from_json(Obj, "primaryDnsV6", primaryDnsV6); + field_from_json(Obj, "secondaryDnsV6", secondaryDnsV6); + return true; + } catch (...) { + } + return false; + } - void WifiNetwork::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "type", type); - field_to_json(Obj, "name", name); - field_to_json(Obj, "password", password); - field_to_json(Obj, "encryption", encryption); - field_to_json(Obj, "bands", bands); - } + void WifiNetwork::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "type", type); + field_to_json(Obj, "name", name); + field_to_json(Obj, "password", password); + field_to_json(Obj, "encryption", encryption); + field_to_json(Obj, "bands", bands); + } - bool WifiNetwork::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "type", type); - field_from_json(Obj, "name", name); - field_from_json(Obj, "password", password); - field_from_json(Obj, "encryption", encryption); - field_from_json(Obj, "bands", bands); - return true; - } catch (...) { - } - return false; - } + bool WifiNetwork::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "type", type); + field_from_json(Obj, "name", name); + field_from_json(Obj, "password", password); + field_from_json(Obj, "encryption", encryption); + field_from_json(Obj, "bands", bands); + return true; + } catch (...) { + } + return false; + } - void WifiNetworkList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "wifiNetworks", wifiNetworks); - field_to_json(Obj, "created", created); - field_to_json(Obj, "modified", modified); - } + void WifiNetworkList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "wifiNetworks", wifiNetworks); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + } - bool WifiNetworkList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "wifiNetworks", wifiNetworks); - field_from_json(Obj, "created", created); - field_from_json(Obj, "modified", modified); - return true; - } catch (...) { - } - return false; - } + bool WifiNetworkList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "wifiNetworks", wifiNetworks); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + return true; + } catch (...) { + } + return false; + } - void AccessTime::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "day", day); - field_to_json(Obj, "rangeList", rangeList); - } + void AccessTime::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "day", day); + field_to_json(Obj, "rangeList", rangeList); + } - bool AccessTime::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "day", day); - field_from_json(Obj, "rangeList", rangeList); - return true; - } catch (...) { - } - return false; - } + bool AccessTime::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "day", day); + field_from_json(Obj, "rangeList", rangeList); + return true; + } catch (...) { + } + return false; + } - void AccessTimes::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "schedule", schedule); - field_to_json(Obj, "created", created); - field_to_json(Obj, "modified", modified); - } + void AccessTimes::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "schedule", schedule); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + } - bool AccessTimes::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "schedule", schedule); - field_from_json(Obj, "created", created); - field_from_json(Obj, "modified", modified); - return true; - } catch (...) { - } - return false; - } + bool AccessTimes::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "schedule", schedule); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + return true; + } catch (...) { + } + return false; + } - void SubscriberDevice::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "name", name); - field_to_json(Obj, "description", description); - field_to_json(Obj, "macAddress", macAddress); - field_to_json(Obj, "manufacturer", manufacturer); - field_to_json(Obj, "firstContact", firstContact); - field_to_json(Obj, "lastContact", lastContact); - field_to_json(Obj, "group", group); - field_to_json(Obj, "icon", icon); - field_to_json(Obj, "suspended", suspended); - field_to_json(Obj, "ip", ip); - field_to_json(Obj, "schedule", schedule); - } + void SubscriberDevice::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "name", name); + field_to_json(Obj, "description", description); + field_to_json(Obj, "macAddress", macAddress); + field_to_json(Obj, "manufacturer", manufacturer); + field_to_json(Obj, "firstContact", firstContact); + field_to_json(Obj, "lastContact", lastContact); + field_to_json(Obj, "group", group); + field_to_json(Obj, "icon", icon); + field_to_json(Obj, "suspended", suspended); + field_to_json(Obj, "ip", ip); + field_to_json(Obj, "schedule", schedule); + } - bool SubscriberDevice::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "name", name); - field_from_json(Obj, "description", description); - field_from_json(Obj, "macAddress", macAddress); - field_from_json(Obj, "manufacturer", manufacturer); - field_from_json(Obj, "firstContact", firstContact); - field_from_json(Obj, "lastContact", lastContact); - field_from_json(Obj, "group", group); - field_from_json(Obj, "icon", icon); - field_from_json(Obj, "suspended", suspended); - field_from_json(Obj, "ip", ip); - field_from_json(Obj, "schedule", schedule); - return true; - } catch (...) { - } - return false; - } + bool SubscriberDevice::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "name", name); + field_from_json(Obj, "description", description); + field_from_json(Obj, "macAddress", macAddress); + field_from_json(Obj, "manufacturer", manufacturer); + field_from_json(Obj, "firstContact", firstContact); + field_from_json(Obj, "lastContact", lastContact); + field_from_json(Obj, "group", group); + field_from_json(Obj, "icon", icon); + field_from_json(Obj, "suspended", suspended); + field_from_json(Obj, "ip", ip); + field_from_json(Obj, "schedule", schedule); + return true; + } catch (...) { + } + return false; + } - void SubscriberDeviceList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "devices", devices); - field_to_json(Obj, "created", created); - field_to_json(Obj, "modified", modified); - } + void SubscriberDeviceList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "devices", devices); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + } - bool SubscriberDeviceList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "devices", devices); - field_from_json(Obj, "created", created); - field_from_json(Obj, "modified", modified); - return true; - } catch (...) { - } - return false; - } + bool SubscriberDeviceList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "devices", devices); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + return true; + } catch (...) { + } + return false; + } - void Association::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "name", name); - field_to_json(Obj, "ssid", ssid); - field_to_json(Obj, "macAddress", macAddress); - field_to_json(Obj, "rssi", rssi); - field_to_json(Obj, "power", power); - field_to_json(Obj, "ipv4", ipv4); - field_to_json(Obj, "ipv6", ipv6); - field_to_json(Obj, "tx", tx); - field_to_json(Obj, "rx", rx); - field_to_json(Obj, "manufacturer", manufacturer); - } + void Association::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "name", name); + field_to_json(Obj, "ssid", ssid); + field_to_json(Obj, "macAddress", macAddress); + field_to_json(Obj, "rssi", rssi); + field_to_json(Obj, "power", power); + field_to_json(Obj, "ipv4", ipv4); + field_to_json(Obj, "ipv6", ipv6); + field_to_json(Obj, "tx", tx); + field_to_json(Obj, "rx", rx); + field_to_json(Obj, "manufacturer", manufacturer); + } - bool Association::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "name", name); - field_from_json(Obj, "ssid", ssid); - field_from_json(Obj, "macAddress", macAddress); - field_from_json(Obj, "rssi", rssi); - field_from_json(Obj, "power", power); - field_from_json(Obj, "ipv4", ipv4); - field_from_json(Obj, "ipv6", ipv6); - field_from_json(Obj, "tx", tx); - field_from_json(Obj, "rx", rx); - field_from_json(Obj, "manufacturer", manufacturer); - return true; - } catch (...) { - } - return false; - } + bool Association::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "name", name); + field_from_json(Obj, "ssid", ssid); + field_from_json(Obj, "macAddress", macAddress); + field_from_json(Obj, "rssi", rssi); + field_from_json(Obj, "power", power); + field_from_json(Obj, "ipv4", ipv4); + field_from_json(Obj, "ipv6", ipv6); + field_from_json(Obj, "tx", tx); + field_from_json(Obj, "rx", rx); + field_from_json(Obj, "manufacturer", manufacturer); + return true; + } catch (...) { + } + return false; + } - void AssociationList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "associations", associations); - field_to_json(Obj, "created", created); - field_to_json(Obj, "modified", modified); - } + void AssociationList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "associations", associations); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + } - bool AssociationList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "associations", associations); - field_from_json(Obj, "created", created); - field_from_json(Obj, "modified", modified); - return true; - } catch (...) { - } - return false; - } + bool AssociationList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "associations", associations); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + return true; + } catch (...) { + } + return false; + } - void Client::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "macAddress", macAddress); - field_to_json(Obj, "speed", speed); - field_to_json(Obj, "mode", mode); - field_to_json(Obj, "ipv4", ipv4); - field_to_json(Obj, "ipv6", ipv6); - field_to_json(Obj, "tx", tx); - field_to_json(Obj, "rx", rx); - field_to_json(Obj, "manufacturer", manufacturer); - } + void Client::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "macAddress", macAddress); + field_to_json(Obj, "speed", speed); + field_to_json(Obj, "mode", mode); + field_to_json(Obj, "ipv4", ipv4); + field_to_json(Obj, "ipv6", ipv6); + field_to_json(Obj, "tx", tx); + field_to_json(Obj, "rx", rx); + field_to_json(Obj, "manufacturer", manufacturer); + } - bool Client::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "macAddress", macAddress); - field_from_json(Obj, "speed", speed); - field_from_json(Obj, "mode", mode); - field_from_json(Obj, "ipv4", ipv4); - field_from_json(Obj, "ipv6", ipv6); - field_from_json(Obj, "tx", tx); - field_from_json(Obj, "rx", rx); - field_from_json(Obj, "manufacturer", manufacturer); - return true; - } catch (...) { - } - return false; - } + bool Client::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "macAddress", macAddress); + field_from_json(Obj, "speed", speed); + field_from_json(Obj, "mode", mode); + field_from_json(Obj, "ipv4", ipv4); + field_from_json(Obj, "ipv6", ipv6); + field_from_json(Obj, "tx", tx); + field_from_json(Obj, "rx", rx); + field_from_json(Obj, "manufacturer", manufacturer); + return true; + } catch (...) { + } + return false; + } - void ClientList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "clients", clients); - field_to_json(Obj, "created", created); - field_to_json(Obj, "modified", modified); - } + void ClientList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "clients", clients); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + } - bool ClientList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "clients", clients); - field_from_json(Obj, "created", created); - field_from_json(Obj, "modified", modified); - return true; - } catch (...) { - } - return false; - } + bool ClientList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "clients", clients); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + return true; + } catch (...) { + } + return false; + } - void Location::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "buildingName", buildingName); - field_to_json(Obj, "addressLines", addressLines); - field_to_json(Obj, "city", city); - field_to_json(Obj, "state", state); - field_to_json(Obj, "postal", postal); - field_to_json(Obj, "country", country); - field_to_json(Obj, "phones", phones); - field_to_json(Obj, "mobiles", mobiles); - } + void Location::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "buildingName", buildingName); + field_to_json(Obj, "addressLines", addressLines); + field_to_json(Obj, "city", city); + field_to_json(Obj, "state", state); + field_to_json(Obj, "postal", postal); + field_to_json(Obj, "country", country); + field_to_json(Obj, "phones", phones); + field_to_json(Obj, "mobiles", mobiles); + } - bool Location::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "buildingName", buildingName); - field_from_json(Obj, "addressLines", addressLines); - field_from_json(Obj, "city", city); - field_from_json(Obj, "state", state); - field_from_json(Obj, "postal", postal); - field_from_json(Obj, "country", country); - field_from_json(Obj, "phones", phones); - field_from_json(Obj, "mobiles", mobiles); - return true; - } catch (...) { - } - return false; - } + bool Location::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "buildingName", buildingName); + field_from_json(Obj, "addressLines", addressLines); + field_from_json(Obj, "city", city); + field_from_json(Obj, "state", state); + field_from_json(Obj, "postal", postal); + field_from_json(Obj, "country", country); + field_from_json(Obj, "phones", phones); + field_from_json(Obj, "mobiles", mobiles); + return true; + } catch (...) { + } + return false; + } - void RadioHE::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "multipleBSSID", multipleBSSID); - field_to_json(Obj, "ema", ema); - field_to_json(Obj, "bssColor", bssColor); - } + void RadioHE::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "multipleBSSID", multipleBSSID); + field_to_json(Obj, "ema", ema); + field_to_json(Obj, "bssColor", bssColor); + } - bool RadioHE::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "multipleBSSID", multipleBSSID); - field_from_json(Obj, "ema", ema); - field_from_json(Obj, "bssColor", bssColor); - return true; - } catch (...) { - } - return false; - } + bool RadioHE::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "multipleBSSID", multipleBSSID); + field_from_json(Obj, "ema", ema); + field_from_json(Obj, "bssColor", bssColor); + return true; + } catch (...) { + } + return false; + } - void RadioRates::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "beacon", beacon); - field_to_json(Obj, "multicast", multicast); - } + void RadioRates::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "beacon", beacon); + field_to_json(Obj, "multicast", multicast); + } - bool RadioRates::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "beacon", beacon); - field_from_json(Obj, "multicast", multicast); - return true; - } catch (...) { - } - return false; - } + bool RadioRates::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "beacon", beacon); + field_from_json(Obj, "multicast", multicast); + return true; + } catch (...) { + } + return false; + } - void RadioInformation::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "band", band); - field_to_json(Obj, "bandwidth", bandwidth); - field_to_json(Obj, "channel", channel); - field_to_json(Obj, "country", country); - field_to_json(Obj, "channelMode", channelMode); - field_to_json(Obj, "channelWidth", channelWidth); - field_to_json(Obj, "requireMode", requireMode); - field_to_json(Obj, "txpower", txpower); - field_to_json(Obj, "legacyRates", legacyRates); - field_to_json(Obj, "beaconInterval", beaconInterval); - field_to_json(Obj, "dtimPeriod", dtimPeriod); - field_to_json(Obj, "maximumClients", maximumClients); - field_to_json(Obj, "rates", rates); - field_to_json(Obj, "he", he); - field_to_json(Obj, "rawInfo", rawInfo); - field_to_json(Obj, "allowDFS", allowDFS); - field_to_json(Obj, "mimo", mimo); - } + void RadioInformation::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "band", band); + field_to_json(Obj, "bandwidth", bandwidth); + field_to_json(Obj, "channel", channel); + field_to_json(Obj, "country", country); + field_to_json(Obj, "channelMode", channelMode); + field_to_json(Obj, "channelWidth", channelWidth); + field_to_json(Obj, "requireMode", requireMode); + field_to_json(Obj, "txpower", txpower); + field_to_json(Obj, "legacyRates", legacyRates); + field_to_json(Obj, "beaconInterval", beaconInterval); + field_to_json(Obj, "dtimPeriod", dtimPeriod); + field_to_json(Obj, "maximumClients", maximumClients); + field_to_json(Obj, "rates", rates); + field_to_json(Obj, "he", he); + field_to_json(Obj, "rawInfo", rawInfo); + field_to_json(Obj, "allowDFS", allowDFS); + field_to_json(Obj, "mimo", mimo); + } - bool RadioInformation::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "band", band); - field_from_json(Obj, "bandwidth", bandwidth); - field_from_json(Obj, "channel", channel); - field_from_json(Obj, "country", country); - field_from_json(Obj, "channelMode", channelMode); - field_from_json(Obj, "channelWidth", channelWidth); - field_from_json(Obj, "requireMode", requireMode); - field_from_json(Obj, "txpower", txpower); - field_from_json(Obj, "legacyRates", legacyRates); - field_from_json(Obj, "beaconInterval", beaconInterval); - field_from_json(Obj, "dtimPeriod", dtimPeriod); - field_from_json(Obj, "maximumClients", maximumClients); - field_from_json(Obj, "rates", rates); - field_from_json(Obj, "he", he); - field_from_json(Obj, "rawInfo", rawInfo); - field_from_json(Obj, "allowDFS", allowDFS); - field_from_json(Obj, "mimo", mimo); - return true; - } catch (...) { - } - return false; - } + bool RadioInformation::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "band", band); + field_from_json(Obj, "bandwidth", bandwidth); + field_from_json(Obj, "channel", channel); + field_from_json(Obj, "country", country); + field_from_json(Obj, "channelMode", channelMode); + field_from_json(Obj, "channelWidth", channelWidth); + field_from_json(Obj, "requireMode", requireMode); + field_from_json(Obj, "txpower", txpower); + field_from_json(Obj, "legacyRates", legacyRates); + field_from_json(Obj, "beaconInterval", beaconInterval); + field_from_json(Obj, "dtimPeriod", dtimPeriod); + field_from_json(Obj, "maximumClients", maximumClients); + field_from_json(Obj, "rates", rates); + field_from_json(Obj, "he", he); + field_from_json(Obj, "rawInfo", rawInfo); + field_from_json(Obj, "allowDFS", allowDFS); + field_from_json(Obj, "mimo", mimo); + return true; + } catch (...) { + } + return false; + } - void AccessPoint::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "id", id); - field_to_json(Obj, "macAddress", macAddress); - field_to_json(Obj, "serialNumber", serialNumber); - field_to_json(Obj, "name", name); - field_to_json(Obj, "deviceType", deviceType); - field_to_json(Obj, "subscriberDevices", subscriberDevices); - field_to_json(Obj, "ipReservations", ipReservations); - field_to_json(Obj, "address", address); - field_to_json(Obj, "wifiNetworks", wifiNetworks); - field_to_json(Obj, "internetConnection", internetConnection); - field_to_json(Obj, "deviceMode", deviceMode); - field_to_json(Obj, "dnsConfiguration", dnsConfiguration); - field_to_json(Obj, "radios", radios); - field_to_json(Obj, "automaticUpgrade", automaticUpgrade); - field_to_json(Obj, "configurationUUID", configurationUUID); - field_to_json(Obj, "currentFirmware", currentFirmware); - field_to_json(Obj, "currentFirmwareDate", currentFirmwareDate); - field_to_json(Obj, "latestFirmware", latestFirmware); - field_to_json(Obj, "latestFirmwareDate", latestFirmwareDate); - field_to_json(Obj, "newFirmwareAvailable", newFirmwareAvailable); - field_to_json(Obj, "latestFirmwareURI", latestFirmwareURI); - } + void AccessPoint::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "macAddress", macAddress); + field_to_json(Obj, "serialNumber", serialNumber); + field_to_json(Obj, "name", name); + field_to_json(Obj, "deviceType", deviceType); + field_to_json(Obj, "subscriberDevices", subscriberDevices); + field_to_json(Obj, "ipReservations", ipReservations); + field_to_json(Obj, "address", address); + field_to_json(Obj, "wifiNetworks", wifiNetworks); + field_to_json(Obj, "internetConnection", internetConnection); + field_to_json(Obj, "deviceMode", deviceMode); + field_to_json(Obj, "dnsConfiguration", dnsConfiguration); + field_to_json(Obj, "radios", radios); + field_to_json(Obj, "automaticUpgrade", automaticUpgrade); + field_to_json(Obj, "configurationUUID", configurationUUID); + field_to_json(Obj, "currentFirmware", currentFirmware); + field_to_json(Obj, "currentFirmwareDate", currentFirmwareDate); + field_to_json(Obj, "latestFirmware", latestFirmware); + field_to_json(Obj, "latestFirmwareDate", latestFirmwareDate); + field_to_json(Obj, "newFirmwareAvailable", newFirmwareAvailable); + field_to_json(Obj, "latestFirmwareURI", latestFirmwareURI); + } - bool AccessPoint::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "id", id); - field_from_json(Obj, "macAddress", macAddress); - field_from_json(Obj, "serialNumber", serialNumber); - field_from_json(Obj, "name", name); - field_from_json(Obj, "deviceType", deviceType); - field_from_json(Obj, "subscriberDevices", subscriberDevices); - field_from_json(Obj, "ipReservations", ipReservations); - field_from_json(Obj, "address", address); - field_from_json(Obj, "wifiNetworks", wifiNetworks); - field_from_json(Obj, "internetConnection", internetConnection); - field_from_json(Obj, "deviceMode", deviceMode); - field_from_json(Obj, "dnsConfiguration", dnsConfiguration); - field_from_json(Obj, "radios", radios); - field_from_json(Obj, "automaticUpgrade", automaticUpgrade); - field_from_json(Obj, "configurationUUID", configurationUUID); - field_from_json(Obj, "currentFirmware", currentFirmware); - field_from_json(Obj, "currentFirmwareDate", currentFirmwareDate); - field_from_json(Obj, "latestFirmware", latestFirmware); - field_from_json(Obj, "latestFirmwareDate", latestFirmwareDate); - field_from_json(Obj, "newFirmwareAvailable", newFirmwareAvailable); - field_from_json(Obj, "latestFirmwareURI", latestFirmwareURI); - return true; - } catch (...) { - } - return false; - } + bool AccessPoint::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "macAddress", macAddress); + field_from_json(Obj, "serialNumber", serialNumber); + field_from_json(Obj, "name", name); + field_from_json(Obj, "deviceType", deviceType); + field_from_json(Obj, "subscriberDevices", subscriberDevices); + field_from_json(Obj, "ipReservations", ipReservations); + field_from_json(Obj, "address", address); + field_from_json(Obj, "wifiNetworks", wifiNetworks); + field_from_json(Obj, "internetConnection", internetConnection); + field_from_json(Obj, "deviceMode", deviceMode); + field_from_json(Obj, "dnsConfiguration", dnsConfiguration); + field_from_json(Obj, "radios", radios); + field_from_json(Obj, "automaticUpgrade", automaticUpgrade); + field_from_json(Obj, "configurationUUID", configurationUUID); + field_from_json(Obj, "currentFirmware", currentFirmware); + field_from_json(Obj, "currentFirmwareDate", currentFirmwareDate); + field_from_json(Obj, "latestFirmware", latestFirmware); + field_from_json(Obj, "latestFirmwareDate", latestFirmwareDate); + field_from_json(Obj, "newFirmwareAvailable", newFirmwareAvailable); + field_from_json(Obj, "latestFirmwareURI", latestFirmwareURI); + return true; + } catch (...) { + } + return false; + } - void AccessPointList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "list", list); - } + void AccessPointList::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "list", list); + } - bool AccessPointList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "list", list); - return true; - } catch (...) { - } - return false; - } + bool AccessPointList::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "list", list); + return true; + } catch (...) { + } + return false; + } - void SubscriberInfo::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "id", id); - field_to_json(Obj, "userId", userId); - field_to_json(Obj, "firstName", firstName); - field_to_json(Obj, "initials", initials); - field_to_json(Obj, "lastName", lastName); - field_to_json(Obj, "phoneNumber", phoneNumber); - field_to_json(Obj, "secondaryEmail", secondaryEmail); - field_to_json(Obj, "accessPoints", accessPoints); - field_to_json(Obj, "serviceAddress", serviceAddress); - field_to_json(Obj, "billingAddress", billingAddress); - field_to_json(Obj, "created", created); - field_to_json(Obj, "modified", modified); - } + void SubscriberInfo::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "id", id); + field_to_json(Obj, "userId", userId); + field_to_json(Obj, "firstName", firstName); + field_to_json(Obj, "initials", initials); + field_to_json(Obj, "lastName", lastName); + field_to_json(Obj, "phoneNumber", phoneNumber); + field_to_json(Obj, "secondaryEmail", secondaryEmail); + field_to_json(Obj, "accessPoints", accessPoints); + field_to_json(Obj, "serviceAddress", serviceAddress); + field_to_json(Obj, "billingAddress", billingAddress); + field_to_json(Obj, "created", created); + field_to_json(Obj, "modified", modified); + } - bool SubscriberInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "id", id); - field_from_json(Obj, "userId", userId); - field_from_json(Obj, "firstName", firstName); - field_from_json(Obj, "initials", initials); - field_from_json(Obj, "lastName", lastName); - field_from_json(Obj, "phoneNumber", phoneNumber); - field_from_json(Obj, "secondaryEmail", secondaryEmail); - field_from_json(Obj, "accessPoints", accessPoints); - field_from_json(Obj, "serviceAddress", serviceAddress); - field_from_json(Obj, "billingAddress", billingAddress); - field_from_json(Obj, "created", created); - field_from_json(Obj, "modified", modified); - return true; - } catch (...) { - } - return false; - } + bool SubscriberInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "id", id); + field_from_json(Obj, "userId", userId); + field_from_json(Obj, "firstName", firstName); + field_from_json(Obj, "initials", initials); + field_from_json(Obj, "lastName", lastName); + field_from_json(Obj, "phoneNumber", phoneNumber); + field_from_json(Obj, "secondaryEmail", secondaryEmail); + field_from_json(Obj, "accessPoints", accessPoints); + field_from_json(Obj, "serviceAddress", serviceAddress); + field_from_json(Obj, "billingAddress", billingAddress); + field_from_json(Obj, "created", created); + field_from_json(Obj, "modified", modified); + return true; + } catch (...) { + } + return false; + } - void StatsEntry::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "timestamp", timestamp); - field_to_json(Obj, "tx", tx); - field_to_json(Obj, "rx", rx); - } + void StatsEntry::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "timestamp", timestamp); + field_to_json(Obj, "tx", tx); + field_to_json(Obj, "rx", rx); + } - bool StatsEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "timestamp", timestamp); - field_from_json(Obj, "tx", tx); - field_from_json(Obj, "rx", rx); - return true; - } catch (...) { - } - return false; - } + bool StatsEntry::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "timestamp", timestamp); + field_from_json(Obj, "tx", tx); + field_from_json(Obj, "rx", rx); + return true; + } catch (...) { + } + return false; + } - void StatsBlock::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj, "modified", modified); - field_to_json(Obj, "external", external); - field_to_json(Obj, "internal", internal); - } + void StatsBlock::to_json(Poco::JSON::Object &Obj) const { + field_to_json(Obj, "modified", modified); + field_to_json(Obj, "external", external); + field_to_json(Obj, "internal", internal); + } - bool StatsBlock::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj, "modified", modified); - field_from_json(Obj, "external", external); - field_from_json(Obj, "internal", internal); - return true; - } catch (...) { - } - return false; - } -} \ No newline at end of file + bool StatsBlock::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + field_from_json(Obj, "modified", modified); + field_from_json(Obj, "external", external); + field_from_json(Obj, "internal", internal); + return true; + } catch (...) { + } + return false; + } +} // namespace OpenWifi::SubObjects \ No newline at end of file diff --git a/src/RESTObjects/RESTAPI_SubObjects.h b/src/RESTObjects/RESTAPI_SubObjects.h index e586af9..64d9941 100644 --- a/src/RESTObjects/RESTAPI_SubObjects.h +++ b/src/RESTObjects/RESTAPI_SubObjects.h @@ -11,312 +11,312 @@ namespace OpenWifi::SubObjects { - struct HomeDeviceMode { - bool enableLEDS = true; - std::string type; // bridge, manual, automatic - std::string subnet; - std::string subnetMask; - std::string startIP; - std::string endIP; - uint64_t created = 0 ; - uint64_t modified = 0 ; - std::string subnetV6; - int subnetMaskV6=0; - std::string startIPV6; - std::string endIPV6; - std::string leaseTime; + struct HomeDeviceMode { + bool enableLEDS = true; + std::string type; // bridge, manual, automatic + std::string subnet; + std::string subnetMask; + std::string startIP; + std::string endIP; + uint64_t created = 0; + uint64_t modified = 0; + std::string subnetV6; + int subnetMaskV6 = 0; + std::string startIPV6; + std::string endIPV6; + std::string leaseTime; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct IPReservation { - std::string nickname; - std::string ipAddress; - std::string macAddress; + struct IPReservation { + std::string nickname; + std::string ipAddress; + std::string macAddress; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct IPReservationList { - std::string id; - std::vector reservations; - uint64_t created = 0 ; - uint64_t modified = 0 ; + struct IPReservationList { + std::string id; + std::vector reservations; + uint64_t created = 0; + uint64_t modified = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct DnsConfiguration { - bool ISP=false; - bool custom=false; - std::string primary; - std::string secondary; - std::string primaryV6; - std::string secondaryV6; + struct DnsConfiguration { + bool ISP = false; + bool custom = false; + std::string primary; + std::string secondary; + std::string primaryV6; + std::string secondaryV6; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct InternetConnection { - std::string type; // automatic, pppoe, manual - std::string username; - std::string password; - std::string ipAddress; - std::string subnetMask; - std::string defaultGateway; - bool sendHostname = true; - std::string primaryDns; - std::string secondaryDns; - uint64_t created=0; - uint64_t modified=0; - bool ipV6Support=false; - std::string ipAddressV6; - int subnetMaskV6=0; - std::string defaultGatewayV6; - std::string primaryDnsV6; - std::string secondaryDnsV6; + struct InternetConnection { + std::string type; // automatic, pppoe, manual + std::string username; + std::string password; + std::string ipAddress; + std::string subnetMask; + std::string defaultGateway; + bool sendHostname = true; + std::string primaryDns; + std::string secondaryDns; + uint64_t created = 0; + uint64_t modified = 0; + bool ipV6Support = false; + std::string ipAddressV6; + int subnetMaskV6 = 0; + std::string defaultGatewayV6; + std::string primaryDnsV6; + std::string secondaryDnsV6; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct WifiNetwork { - std::string type; // main, guest - std::string name; - std::string password; - std::string encryption; - std::vector bands; + struct WifiNetwork { + std::string type; // main, guest + std::string name; + std::string password; + std::string encryption; + std::vector bands; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct WifiNetworkList { - std::vector wifiNetworks; - uint64_t created=0; - uint64_t modified=0; + struct WifiNetworkList { + std::vector wifiNetworks; + uint64_t created = 0; + uint64_t modified = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct AccessTime { - std::string day; - std::vector rangeList; + struct AccessTime { + std::string day; + std::vector rangeList; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct AccessTimes { - std::vector schedule; - uint64_t created=0; - uint64_t modified=0; + struct AccessTimes { + std::vector schedule; + uint64_t created = 0; + uint64_t modified = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct SubscriberDevice { - std::string name; - std::string description; - std::string macAddress; - std::string manufacturer; - uint64_t firstContact=0; - uint64_t lastContact=0; - std::string group; - std::string icon; - bool suspended=false; - std::string ip; - std::vector schedule; + struct SubscriberDevice { + std::string name; + std::string description; + std::string macAddress; + std::string manufacturer; + uint64_t firstContact = 0; + uint64_t lastContact = 0; + std::string group; + std::string icon; + bool suspended = false; + std::string ip; + std::vector schedule; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct SubscriberDeviceList { - std::vector devices; - uint64_t created=0; - uint64_t modified=0; + struct SubscriberDeviceList { + std::vector devices; + uint64_t created = 0; + uint64_t modified = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct Association { - std::string name; - std::string ssid; - std::string macAddress; - int rssi=0; - int power=0; - std::string ipv4; - std::string ipv6; - uint64_t tx=0; - uint64_t rx=0; - std::string manufacturer; + struct Association { + std::string name; + std::string ssid; + std::string macAddress; + int rssi = 0; + int power = 0; + std::string ipv4; + std::string ipv6; + uint64_t tx = 0; + uint64_t rx = 0; + std::string manufacturer; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct AssociationList { - std::vector associations; - uint64_t created=0; - uint64_t modified=0; + struct AssociationList { + std::vector associations; + uint64_t created = 0; + uint64_t modified = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct Client { - std::string macAddress; - std::string speed; - std::string mode; - std::string ipv4; - std::string ipv6; - uint64_t tx=0; - uint64_t rx=0; - std::string manufacturer; + struct Client { + std::string macAddress; + std::string speed; + std::string mode; + std::string ipv4; + std::string ipv6; + uint64_t tx = 0; + uint64_t rx = 0; + std::string manufacturer; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct ClientList { - std::vector clients; - uint64_t created=0; - uint64_t modified=0; + struct ClientList { + std::vector clients; + uint64_t created = 0; + uint64_t modified = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct Location { - std::string buildingName; - std::vector addressLines; - std::string city; - std::string state; - std::string postal; - std::string country; - std::vector phones; - std::vector mobiles; + struct Location { + std::string buildingName; + std::vector addressLines; + std::string city; + std::string state; + std::string postal; + std::string country; + std::vector phones; + std::vector mobiles; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct RadioHE { - bool multipleBSSID = false; - bool ema = false; - uint64_t bssColor = 64; + struct RadioHE { + bool multipleBSSID = false; + bool ema = false; + uint64_t bssColor = 64; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct RadioRates { - uint64_t beacon = 6000; - uint64_t multicast = 24000; + struct RadioRates { + uint64_t beacon = 6000; + uint64_t multicast = 24000; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct RadioInformation { - std::string band; - uint64_t bandwidth; - uint64_t channel = 0 ; - std::string country; - std::string channelMode{"HE"}; - uint64_t channelWidth = 80; - std::string requireMode; - uint64_t txpower=0; - bool legacyRates = false; - uint64_t beaconInterval = 100; - uint64_t dtimPeriod = 2; - uint64_t maximumClients = 64; - RadioRates rates; - RadioHE he; - bool allowDFS=false; - std::string mimo; - std::vector rawInfo; + struct RadioInformation { + std::string band; + uint64_t bandwidth; + uint64_t channel = 0; + std::string country; + std::string channelMode{"HE"}; + uint64_t channelWidth = 80; + std::string requireMode; + uint64_t txpower = 0; + bool legacyRates = false; + uint64_t beaconInterval = 100; + uint64_t dtimPeriod = 2; + uint64_t maximumClients = 64; + RadioRates rates; + RadioHE he; + bool allowDFS = false; + std::string mimo; + std::vector rawInfo; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct AccessPoint { - std::string id; - std::string macAddress; - std::string serialNumber; - std::string name; - std::string deviceType; - SubscriberDeviceList subscriberDevices; - IPReservationList ipReservations; - Location address; - WifiNetworkList wifiNetworks; - InternetConnection internetConnection; - HomeDeviceMode deviceMode; - DnsConfiguration dnsConfiguration; - std::vector radios; - bool automaticUpgrade = true; - std::string configurationUUID; - std::string currentFirmware; - uint64_t currentFirmwareDate; - std::string latestFirmware; - uint64_t latestFirmwareDate; - bool newFirmwareAvailable; - std::string latestFirmwareURI; + struct AccessPoint { + std::string id; + std::string macAddress; + std::string serialNumber; + std::string name; + std::string deviceType; + SubscriberDeviceList subscriberDevices; + IPReservationList ipReservations; + Location address; + WifiNetworkList wifiNetworks; + InternetConnection internetConnection; + HomeDeviceMode deviceMode; + DnsConfiguration dnsConfiguration; + std::vector radios; + bool automaticUpgrade = true; + std::string configurationUUID; + std::string currentFirmware; + uint64_t currentFirmwareDate; + std::string latestFirmware; + uint64_t latestFirmwareDate; + bool newFirmwareAvailable; + std::string latestFirmwareURI; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct AccessPointList { - std::vector list; + struct AccessPointList { + std::vector list; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct SubscriberInfo { - std::string id; - std::string userId; - std::string firstName; - std::string initials; - std::string lastName; - std::string phoneNumber; - std::string secondaryEmail; - AccessPointList accessPoints; - Location serviceAddress; - Location billingAddress; - uint64_t created = 0; - uint64_t modified = 0; + struct SubscriberInfo { + std::string id; + std::string userId; + std::string firstName; + std::string initials; + std::string lastName; + std::string phoneNumber; + std::string secondaryEmail; + AccessPointList accessPoints; + Location serviceAddress; + Location billingAddress; + uint64_t created = 0; + uint64_t modified = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct StatsEntry { - uint64_t timestamp=0; - uint64_t tx=0; - uint64_t rx=0; + struct StatsEntry { + uint64_t timestamp = 0; + uint64_t tx = 0; + uint64_t rx = 0; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - struct StatsBlock { - uint64_t modified=0; - std::vector external, internal; + struct StatsBlock { + uint64_t modified = 0; + std::vector external, internal; - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; -} + void to_json(Poco::JSON::Object &Obj) const; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; +} // namespace OpenWifi::SubObjects -#endif //OWSUB_RESTAPI_SUBOBJECTS_H +#endif // OWSUB_RESTAPI_SUBOBJECTS_H diff --git a/src/StatsSvr.cpp b/src/StatsSvr.cpp index 7475bbb..2e13c67 100644 --- a/src/StatsSvr.cpp +++ b/src/StatsSvr.cpp @@ -3,8 +3,8 @@ // #include "StatsSvr.h" -#include "framework/KafkaTopics.h" #include "framework/KafkaManager.h" +#include "framework/KafkaTopics.h" #include "nlohmann/json.hpp" #include @@ -12,99 +12,120 @@ namespace OpenWifi { - int StatsSvr::Start() { - Running_ = true; - Types::TopicNotifyFunction F = [this](const std::string &Key, const std::string &Payload) { this->StatsReceived(Key,Payload); }; - StatsWatcherId_ = KafkaManager()->RegisterTopicWatcher(KafkaTopics::STATE, F); - Worker_.start(*this); - return 0; - } + int StatsSvr::Start() { + Running_ = true; + Types::TopicNotifyFunction F = [this](const std::string &Key, const std::string &Payload) { + this->StatsReceived(Key, Payload); + }; + StatsWatcherId_ = KafkaManager()->RegisterTopicWatcher(KafkaTopics::STATE, F); + Worker_.start(*this); + return 0; + } - void StatsSvr::Stop() { - Running_ = false; - KafkaManager()->UnregisterTopicWatcher(KafkaTopics::CONNECTION, StatsWatcherId_); - Queue_.wakeUpAll(); - Worker_.join(); - } + void StatsSvr::Stop() { + Running_ = false; + KafkaManager()->UnregisterTopicWatcher(KafkaTopics::CONNECTION, StatsWatcherId_); + Queue_.wakeUpAll(); + Worker_.join(); + } - void StatsSvr::run() { - Utils::SetThreadName("stats-svr"); - Poco::AutoPtr Note(Queue_.waitDequeueNotification()); - while(Note && Running_) { - auto Msg = dynamic_cast(Note.get()); - if(Msg!= nullptr) { - try { - nlohmann::json msg = nlohmann::json::parse(Msg->Payload()); - if (msg.contains(uCentralProtocol::PAYLOAD)) { - auto payload = msg[uCentralProtocol::PAYLOAD]; - if (payload.contains("state") && payload.contains("serial")) { - auto serialNumber = payload["serial"].get(); - auto state = payload["state"]; - if (state.contains("version")) { - if (state.contains("unit")) { - auto unit = state["unit"]; - if (unit.contains("localtime")) { - uint64_t timestamp = unit["localtime"]; - if (state.contains("interfaces")) { - if (state["interfaces"].is_array()) { - auto interfaces = state["interfaces"]; - auto serial_int = Utils::SerialNumberToInt(serialNumber); - uint64_t int_rx=0, int_tx=0, ext_rx=0,ext_tx=0; - for (const auto &cur_int: interfaces) { - bool external_stats=true; - if(cur_int.contains("location")) { - auto location = cur_int["location"].get(); - auto parts = Poco::StringTokenizer(location,"/"); - if(parts.count()==3) { - if(parts[2]=="0") - external_stats = true; - else - external_stats = false; - } - } + void StatsSvr::run() { + Utils::SetThreadName("stats-svr"); + Poco::AutoPtr Note(Queue_.waitDequeueNotification()); + while (Note && Running_) { + auto Msg = dynamic_cast(Note.get()); + if (Msg != nullptr) { + try { + nlohmann::json msg = nlohmann::json::parse(Msg->Payload()); + if (msg.contains(uCentralProtocol::PAYLOAD)) { + auto payload = msg[uCentralProtocol::PAYLOAD]; + if (payload.contains("state") && payload.contains("serial")) { + auto serialNumber = payload["serial"].get(); + auto state = payload["state"]; + if (state.contains("version")) { + if (state.contains("unit")) { + auto unit = state["unit"]; + if (unit.contains("localtime")) { + uint64_t timestamp = unit["localtime"]; + if (state.contains("interfaces")) { + if (state["interfaces"].is_array()) { + auto interfaces = state["interfaces"]; + auto serial_int = + Utils::SerialNumberToInt(serialNumber); + uint64_t int_rx = 0, int_tx = 0, ext_rx = 0, + ext_tx = 0; + for (const auto &cur_int : interfaces) { + bool external_stats = true; + if (cur_int.contains("location")) { + auto location = + cur_int["location"].get(); + auto parts = + Poco::StringTokenizer(location, "/"); + if (parts.count() == 3) { + if (parts[2] == "0") + external_stats = true; + else + external_stats = false; + } + } - if (cur_int.contains("counters") && cur_int["counters"].contains("rx_bytes") && cur_int["counters"].contains("tx_bytes")) { - if(external_stats) { - ext_rx = cur_int["counters"]["rx_bytes"].get(); - ext_tx = cur_int["counters"]["tx_bytes"].get(); - } else { - int_rx = cur_int["counters"]["rx_bytes"].get(); - int_tx = cur_int["counters"]["tx_bytes"].get(); - } - } - } - { - std::lock_guard G(Mutex_); - auto it = DeviceStats_.find(serial_int); - if (it == end(DeviceStats_)) { - DeviceStats D; - D.AddValue(timestamp, ext_tx, ext_rx, int_tx, int_rx); - DeviceStats_[serial_int] = D; - poco_debug(Logger(),fmt::format("Creating statistics cache for device {}",serialNumber)); - } else { - poco_debug(Logger(),fmt::format("Adding statistics cache for device {}",serialNumber)); - it->second.AddValue(timestamp, ext_tx, ext_rx, int_tx, int_rx); - std::cout << "Adding device stats entries for " << serialNumber - << std::endl; - } - } - } - } - } - } - } - } - } - } catch (const Poco::Exception &E) { - Logger().log(E); - } catch (...) { + if (cur_int.contains("counters") && + cur_int["counters"].contains("rx_bytes") && + cur_int["counters"].contains("tx_bytes")) { + if (external_stats) { + ext_rx = cur_int["counters"]["rx_bytes"] + .get(); + ext_tx = cur_int["counters"]["tx_bytes"] + .get(); + } else { + int_rx = cur_int["counters"]["rx_bytes"] + .get(); + int_tx = cur_int["counters"]["tx_bytes"] + .get(); + } + } + } + { + std::lock_guard G(Mutex_); + auto it = DeviceStats_.find(serial_int); + if (it == end(DeviceStats_)) { + DeviceStats D; + D.AddValue(timestamp, ext_tx, ext_rx, + int_tx, int_rx); + DeviceStats_[serial_int] = D; + poco_debug( + Logger(), + fmt::format("Creating statistics cache " + "for device {}", + serialNumber)); + } else { + poco_debug( + Logger(), + fmt::format("Adding statistics cache " + "for device {}", + serialNumber)); + it->second.AddValue(timestamp, ext_tx, + ext_rx, int_tx, int_rx); + std::cout + << "Adding device stats entries for " + << serialNumber << std::endl; + } + } + } + } + } + } + } + } + } + } catch (const Poco::Exception &E) { + Logger().log(E); + } catch (...) { + } + } else { + } + Note = Queue_.waitDequeueNotification(); + } + } - } - } else { - - } - Note = Queue_.waitDequeueNotification(); - } - } - -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/StatsSvr.h b/src/StatsSvr.h index 503aa01..49867a5 100644 --- a/src/StatsSvr.h +++ b/src/StatsSvr.h @@ -4,121 +4,120 @@ #pragma once -#include "RESTObjects/RESTAPI_SubObjects.h" -#include "framework/SubSystemServer.h" #include "Poco/Notification.h" #include "Poco/NotificationQueue.h" +#include "RESTObjects/RESTAPI_SubObjects.h" +#include "framework/SubSystemServer.h" #include "framework/utils.h" namespace OpenWifi { - class Stats_Msg : public Poco::Notification { - public: - explicit Stats_Msg(std::string Key, std::string Payload ) : - Key_(std::move(Key)), - Payload_(std::move(Payload)) {} - const std::string & Key() { return Key_; } - const std::string & Payload() { return Payload_; } - private: - std::string Key_; - std::string Payload_; - }; + class Stats_Msg : public Poco::Notification { + public: + explicit Stats_Msg(std::string Key, std::string Payload) + : Key_(std::move(Key)), Payload_(std::move(Payload)) {} + const std::string &Key() { return Key_; } + const std::string &Payload() { return Payload_; } - struct DeviceStats { - uint64_t LastUpdate_=0; - constexpr static const size_t buffer_size = 20; - std::array timestamps,ext_txs,ext_rxs,int_txs,int_rxs; - uint64_t base_int_tx=0,base_int_rx=0,base_ext_tx=0,base_ext_rx=0; + private: + std::string Key_; + std::string Payload_; + }; - uint32_t index_=0; - bool no_base=true; - void AddValue(uint64_t ts, uint32_t ext_tx, uint32_t ext_rx, uint32_t int_tx, uint32_t int_rx ) { - if(no_base) { - base_int_rx = int_rx; - base_int_tx = int_tx; - base_ext_rx = ext_rx; - base_ext_tx = ext_tx; - no_base = false; - return; - } + struct DeviceStats { + uint64_t LastUpdate_ = 0; + constexpr static const size_t buffer_size = 20; + std::array timestamps, ext_txs, ext_rxs, int_txs, int_rxs; + uint64_t base_int_tx = 0, base_int_rx = 0, base_ext_tx = 0, base_ext_rx = 0; - timestamps[index_] = ts; - int_txs[index_] = int_tx - base_int_tx; - base_int_tx = int_tx; - int_rxs[index_] = int_rx - base_int_rx; - base_int_rx = int_rx; - ext_txs[index_] = ext_tx - base_ext_tx; - base_ext_tx = ext_tx; - ext_rxs[index_] = ext_rx - base_ext_rx; - base_ext_rx = ext_rx; - index_++; + uint32_t index_ = 0; + bool no_base = true; + void AddValue(uint64_t ts, uint32_t ext_tx, uint32_t ext_rx, uint32_t int_tx, + uint32_t int_rx) { + if (no_base) { + base_int_rx = int_rx; + base_int_tx = int_tx; + base_ext_rx = ext_rx; + base_ext_tx = ext_tx; + no_base = false; + return; + } - if(index_==buffer_size) { - // move everything down by one... - std::memmove(×tamps[0],×tamps[1], sizeof(timestamps[1])*buffer_size-1); - std::memmove(&ext_txs[0],&ext_txs[1], sizeof(ext_txs[1])*buffer_size-1); - std::memmove(&ext_rxs[0],&ext_rxs[1], sizeof(ext_rxs[1])*buffer_size-1); - std::memmove(&int_txs[0],&int_txs[1], sizeof(int_txs[1])*buffer_size-1); - std::memmove(&int_rxs[0],&int_rxs[1], sizeof(int_rxs[1])*buffer_size-1); - index_--; - } - LastUpdate_ = Utils::Now(); - } + timestamps[index_] = ts; + int_txs[index_] = int_tx - base_int_tx; + base_int_tx = int_tx; + int_rxs[index_] = int_rx - base_int_rx; + base_int_rx = int_rx; + ext_txs[index_] = ext_tx - base_ext_tx; + base_ext_tx = ext_tx; + ext_rxs[index_] = ext_rx - base_ext_rx; + base_ext_rx = ext_rx; + index_++; - void print() { - for(size_t i=0;isecond.Get(Stats); - } + inline void Get(const std::string &SerialNumber, SubObjects::StatsBlock &Stats) { + std::lock_guard G(Mutex_); - private: - uint64_t StatsWatcherId_=0; - Poco::NotificationQueue Queue_; - Poco::Thread Worker_; - std::atomic_bool Running_=false; - std::map DeviceStats_; + auto it = DeviceStats_.find(Utils::SerialNumberToInt(SerialNumber)); + if (it == end(DeviceStats_)) + return; + it->second.Get(Stats); + } - StatsSvr() noexcept: - SubSystemServer("StateSvr", "STATS-SVR", "statscache") - { - } + private: + uint64_t StatsWatcherId_ = 0; + Poco::NotificationQueue Queue_; + Poco::Thread Worker_; + std::atomic_bool Running_ = false; + std::map DeviceStats_; - }; - inline auto StatsSvr() { return StatsSvr::instance(); } -} \ No newline at end of file + StatsSvr() noexcept : SubSystemServer("StateSvr", "STATS-SVR", "statscache") {} + }; + inline auto StatsSvr() { return StatsSvr::instance(); } +} // namespace OpenWifi \ No newline at end of file diff --git a/src/StorageService.cpp b/src/StorageService.cpp index 40495e7..23af8e3 100644 --- a/src/StorageService.cpp +++ b/src/StorageService.cpp @@ -10,23 +10,23 @@ namespace OpenWifi { - int StorageService::Start() { - std::lock_guard Guard(Mutex_); + int StorageService::Start() { + std::lock_guard Guard(Mutex_); Logger().notice("Starting."); StorageClass::Start(); - SubscriberDB_ = std::make_unique(dbType_,*Pool_, Logger()); + SubscriberDB_ = std::make_unique(dbType_, *Pool_, Logger()); SubscriberDB_->Create(); - return 0; - } + return 0; + } - void StorageService::Stop() { - std::lock_guard Guard(Mutex_); + void StorageService::Stop() { + std::lock_guard Guard(Mutex_); - StorageClass::Stop(); - Logger().notice("Stopping."); - } -} + StorageClass::Stop(); + Logger().notice("Stopping."); + } +} // namespace OpenWifi // namespace \ No newline at end of file diff --git a/src/StorageService.h b/src/StorageService.h index 7c44d1c..889022b 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -10,11 +10,11 @@ #include +#include "Poco/Data/MySQL/Connector.h" +#include "Poco/Data/PostgreSQL/Connector.h" +#include "Poco/Data/SQLite/Connector.h" #include "Poco/Data/Session.h" #include "Poco/Data/SessionPool.h" -#include "Poco/Data/SQLite/Connector.h" -#include "Poco/Data/PostgreSQL/Connector.h" -#include "Poco/Data/MySQL/Connector.h" #include "framework/StorageClass.h" @@ -22,24 +22,24 @@ namespace OpenWifi { - class StorageService : public StorageClass { - public: - static StorageService *instance() { - static auto instance_ = new StorageService; - return instance_; - } + class StorageService : public StorageClass { + public: + static StorageService *instance() { + static auto instance_ = new StorageService; + return instance_; + } - OpenWifi::SubscriberInfoDB & SubInfoDB() { return *SubscriberDB_; }; + OpenWifi::SubscriberInfoDB &SubInfoDB() { return *SubscriberDB_; }; - int Start() override; - void Stop() override; + int Start() override; + void Stop() override; - private: - std::unique_ptr SubscriberDB_; - Poco::Thread Updater_; - std::atomic_bool Running_=false; - }; + private: + std::unique_ptr SubscriberDB_; + Poco::Thread Updater_; + std::atomic_bool Running_ = false; + }; - inline class StorageService * StorageService() { return StorageService::instance(); } + inline class StorageService *StorageService() { return StorageService::instance(); } -} // namespace +} // namespace OpenWifi diff --git a/src/SubscriberCache.cpp b/src/SubscriberCache.cpp index 9cd9cd3..c4cbc34 100644 --- a/src/SubscriberCache.cpp +++ b/src/SubscriberCache.cpp @@ -7,28 +7,27 @@ namespace OpenWifi { - int SubscriberCache::Start() { - return 0; - } + int SubscriberCache::Start() { return 0; } - void SubscriberCache::Stop() { - } + void SubscriberCache::Stop() {} - bool SubscriberCache::GetSubInfo(const std::string &Id, Poco::SharedPtr & SubInfo) { - if(SubsCache_.has(Id)) { - SubInfo = SubsCache_.get(Id); - return true; - } - SubObjects::SubscriberInfo Sub; - if(StorageService()->SubInfoDB().GetRecord("id",Id,Sub)) { - SubsCache_.update(Id,Sub); - SubInfo = SubsCache_.get(Id); - return true; - } - return false; - } + bool SubscriberCache::GetSubInfo(const std::string &Id, + Poco::SharedPtr &SubInfo) { + if (SubsCache_.has(Id)) { + SubInfo = SubsCache_.get(Id); + return true; + } + SubObjects::SubscriberInfo Sub; + if (StorageService()->SubInfoDB().GetRecord("id", Id, Sub)) { + SubsCache_.update(Id, Sub); + SubInfo = SubsCache_.get(Id); + return true; + } + return false; + } - void SubscriberCache::UpdateSubInfo(const std::string &Id, const SubObjects::SubscriberInfo &SubInfo) { - SubsCache_.update(Id,SubInfo); - } -} \ No newline at end of file + void SubscriberCache::UpdateSubInfo(const std::string &Id, + const SubObjects::SubscriberInfo &SubInfo) { + SubsCache_.update(Id, SubInfo); + } +} // namespace OpenWifi \ No newline at end of file diff --git a/src/SubscriberCache.h b/src/SubscriberCache.h index d14f56c..f97b5bf 100644 --- a/src/SubscriberCache.h +++ b/src/SubscriberCache.h @@ -4,34 +4,31 @@ #pragma once -#include "framework/SubSystemServer.h" -#include "RESTObjects/RESTAPI_SubObjects.h" #include "Poco/ExpireLRUCache.h" +#include "RESTObjects/RESTAPI_SubObjects.h" +#include "framework/SubSystemServer.h" namespace OpenWifi { - class SubscriberCache : public SubSystemServer { - public: - static SubscriberCache *instance() { - static auto instance_ = new SubscriberCache; - return instance_; - } + class SubscriberCache : public SubSystemServer { + public: + static SubscriberCache *instance() { + static auto instance_ = new SubscriberCache; + return instance_; + } - int Start() override; - void Stop() override; + int Start() override; + void Stop() override; - bool GetSubInfo(const std::string &Id, Poco::SharedPtr & SubInfo); - void UpdateSubInfo(const std::string &Id, const SubObjects::SubscriberInfo& SubInfo); + bool GetSubInfo(const std::string &Id, + Poco::SharedPtr &SubInfo); + void UpdateSubInfo(const std::string &Id, const SubObjects::SubscriberInfo &SubInfo); - private: - Poco::ExpireLRUCache SubsCache_{2048}; + private: + Poco::ExpireLRUCache SubsCache_{2048}; - SubscriberCache() noexcept: - SubSystemServer("SubscriberCache", "SUb-CACHE", "subcache") - { - } + SubscriberCache() noexcept : SubSystemServer("SubscriberCache", "SUb-CACHE", "subcache") {} + }; - }; + inline class SubscriberCache *SubscriberCache() { return SubscriberCache::instance(); } - inline class SubscriberCache * SubscriberCache() { return SubscriberCache::instance(); } - -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/ALBserver.cpp b/src/framework/ALBserver.cpp index 5e2bf47..1a08503 100644 --- a/src/framework/ALBserver.cpp +++ b/src/framework/ALBserver.cpp @@ -4,17 +4,19 @@ #include "ALBserver.h" -#include "framework/utils.h" -#include "framework/MicroServiceFuncs.h" #include "fmt/format.h" +#include "framework/MicroServiceFuncs.h" +#include "framework/utils.h" namespace OpenWifi { - void ALBRequestHandler::handleRequest([[maybe_unused]] Poco::Net::HTTPServerRequest& Request, Poco::Net::HTTPServerResponse& Response) { + void ALBRequestHandler::handleRequest([[maybe_unused]] Poco::Net::HTTPServerRequest &Request, + Poco::Net::HTTPServerResponse &Response) { Utils::SetThreadName("alb-request"); try { - if((id_ % 100) == 0) { - Logger_.debug(fmt::format("ALB-REQUEST({}): ALB Request {}.", Request.clientAddress().toString(), id_)); + if ((id_ % 100) == 0) { + Logger_.debug(fmt::format("ALB-REQUEST({}): ALB Request {}.", + Request.clientAddress().toString(), id_)); } Response.setChunkedTransferEncoding(true); Response.setContentType("text/html"); @@ -26,31 +28,27 @@ namespace OpenWifi { std::ostream &Answer = Response.send(); Answer << "process Alive and kicking!"; } catch (...) { - } } - ALBRequestHandlerFactory::ALBRequestHandlerFactory(Poco::Logger & L): - Logger_(L) { - } + ALBRequestHandlerFactory::ALBRequestHandlerFactory(Poco::Logger &L) : Logger_(L) {} - ALBRequestHandler* ALBRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest& request) { + ALBRequestHandler * + ALBRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest &request) { if (request.getURI() == "/") return new ALBRequestHandler(Logger_, req_id_++); else return nullptr; } - ALBHealthCheckServer::ALBHealthCheckServer() : - SubSystemServer("ALBHealthCheckServer", "ALB-SVR", "alb") - { - } + ALBHealthCheckServer::ALBHealthCheckServer() + : SubSystemServer("ALBHealthCheckServer", "ALB-SVR", "alb") {} int ALBHealthCheckServer::Start() { - if(MicroServiceConfigGetBool("alb.enable",false)) { - poco_information(Logger(),"Starting..."); - Running_=true; - Port_ = (int)MicroServiceConfigGetInt("alb.port",15015); + if (MicroServiceConfigGetBool("alb.enable", false)) { + poco_information(Logger(), "Starting..."); + Running_ = true; + Port_ = (int)MicroServiceConfigGetInt("alb.port", 15015); Poco::Net::IPAddress Addr(Poco::Net::IPAddress::wildcard( Poco::Net::Socket::supportsIPv6() ? Poco::Net::AddressFamily::IPv6 : Poco::Net::AddressFamily::IPv4)); @@ -60,7 +58,8 @@ namespace OpenWifi { Socket_ = std::make_unique(SockAddr, Port_); auto Params = new Poco::Net::HTTPServerParams; Params->setName("ws:alb"); - Server_ = std::make_unique(new ALBRequestHandlerFactory(Logger()), *Socket_, Params); + Server_ = std::make_unique( + new ALBRequestHandlerFactory(Logger()), *Socket_, Params); Server_->start(); } @@ -68,10 +67,10 @@ namespace OpenWifi { } void ALBHealthCheckServer::Stop() { - poco_information(Logger(),"Stopping..."); - if(Running_) + poco_information(Logger(), "Stopping..."); + if (Running_) Server_->stopAll(true); - poco_information(Logger(),"Stopped..."); + poco_information(Logger(), "Stopped..."); } } // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/ALBserver.h b/src/framework/ALBserver.h index 6316818..6599ac8 100644 --- a/src/framework/ALBserver.h +++ b/src/framework/ALBserver.h @@ -7,35 +7,34 @@ #include "framework/SubSystemServer.h" #include "Poco/Net/HTTPRequestHandler.h" -#include "Poco/Net/HTTPServerRequest.h" -#include "Poco/Net/HTTPServerResponse.h" #include "Poco/Net/HTTPRequestHandlerFactory.h" #include "Poco/Net/HTTPServer.h" +#include "Poco/Net/HTTPServerRequest.h" +#include "Poco/Net/HTTPServerResponse.h" namespace OpenWifi { - class ALBRequestHandler: public Poco::Net::HTTPRequestHandler { + class ALBRequestHandler : public Poco::Net::HTTPRequestHandler { public: - explicit ALBRequestHandler(Poco::Logger & L, uint64_t id) - : Logger_(L), id_(id) { - } + explicit ALBRequestHandler(Poco::Logger &L, uint64_t id) : Logger_(L), id_(id) {} - void handleRequest([[maybe_unused]] Poco::Net::HTTPServerRequest& Request, Poco::Net::HTTPServerResponse& Response) override; + void handleRequest([[maybe_unused]] Poco::Net::HTTPServerRequest &Request, + Poco::Net::HTTPServerResponse &Response) override; private: - Poco::Logger & Logger_; - uint64_t id_; + Poco::Logger &Logger_; + uint64_t id_; }; - class ALBRequestHandlerFactory: public Poco::Net::HTTPRequestHandlerFactory - { + class ALBRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { public: - explicit ALBRequestHandlerFactory(Poco::Logger & L); - ALBRequestHandler* createRequestHandler(const Poco::Net::HTTPServerRequest& request) override; + explicit ALBRequestHandlerFactory(Poco::Logger &L); + ALBRequestHandler * + createRequestHandler(const Poco::Net::HTTPServerRequest &request) override; private: - Poco::Logger &Logger_; - inline static std::atomic_uint64_t req_id_=1; + Poco::Logger &Logger_; + inline static std::atomic_uint64_t req_id_ = 1; }; class ALBHealthCheckServer : public SubSystemServer { @@ -51,13 +50,12 @@ namespace OpenWifi { void Stop() override; private: - std::unique_ptr Server_; - std::unique_ptr Socket_; - int Port_ = 0; - mutable std::atomic_bool Running_=false; + std::unique_ptr Server_; + std::unique_ptr Socket_; + int Port_ = 0; + mutable std::atomic_bool Running_ = false; }; inline auto ALBHealthCheckServer() { return ALBHealthCheckServer::instance(); } } // namespace OpenWifi - diff --git a/src/framework/API_Proxy.h b/src/framework/API_Proxy.h index a6a402a..dcb6f06 100644 --- a/src/framework/API_Proxy.h +++ b/src/framework/API_Proxy.h @@ -4,96 +4,94 @@ #pragma once -#include "Poco/Logger.h" #include "Poco/JSON/Parser.h" +#include "Poco/Logger.h" +#include "Poco/Net/HTTPSClientSession.h" #include "Poco/Net/HTTPServerRequest.h" #include "Poco/Net/HTTPServerResponse.h" -#include "Poco/Net/HTTPSClientSession.h" #include "Poco/URI.h" #include "framework/MicroServiceFuncs.h" namespace OpenWifi { - inline void API_Proxy( Poco::Logger &Logger, - Poco::Net::HTTPServerRequest *Request, - Poco::Net::HTTPServerResponse *Response, - const char * ServiceType, - const char * PathRewrite, - uint64_t msTimeout_ = 10000 ) { - try { - auto Services = MicroServiceGetServices(ServiceType); - for(auto const &Svc:Services) { - Poco::URI SourceURI(Request->getURI()); - Poco::URI DestinationURI(Svc.PrivateEndPoint); - DestinationURI.setPath(PathRewrite); - DestinationURI.setQuery(SourceURI.getQuery()); + inline void API_Proxy(Poco::Logger &Logger, Poco::Net::HTTPServerRequest *Request, + Poco::Net::HTTPServerResponse *Response, const char *ServiceType, + const char *PathRewrite, uint64_t msTimeout_ = 10000) { + try { + auto Services = MicroServiceGetServices(ServiceType); + for (auto const &Svc : Services) { + Poco::URI SourceURI(Request->getURI()); + Poco::URI DestinationURI(Svc.PrivateEndPoint); + DestinationURI.setPath(PathRewrite); + DestinationURI.setQuery(SourceURI.getQuery()); - // std::cout << " Source: " << SourceURI.toString() << std::endl; - // std::cout << "Destination: " << DestinationURI.toString() << std::endl; + // std::cout << " Source: " << SourceURI.toString() << std::endl; + // std::cout << "Destination: " << DestinationURI.toString() << std::endl; - Poco::Net::HTTPSClientSession Session(DestinationURI.getHost(), DestinationURI.getPort()); - Session.setKeepAlive(true); - Session.setTimeout(Poco::Timespan(msTimeout_/1000, msTimeout_ % 1000)); - Poco::Net::HTTPRequest ProxyRequest(Request->getMethod(), - DestinationURI.getPathAndQuery(), - Poco::Net::HTTPMessage::HTTP_1_1); - if(Request->has("Authorization")) { - ProxyRequest.add("Authorization", Request->get("Authorization")); - } else { - ProxyRequest.add("X-API-KEY", Svc.AccessKey); - ProxyRequest.add("X-INTERNAL-NAME", MicroServicePublicEndPoint()); - } + Poco::Net::HTTPSClientSession Session(DestinationURI.getHost(), + DestinationURI.getPort()); + Session.setKeepAlive(true); + Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); + Poco::Net::HTTPRequest ProxyRequest(Request->getMethod(), + DestinationURI.getPathAndQuery(), + Poco::Net::HTTPMessage::HTTP_1_1); + if (Request->has("Authorization")) { + ProxyRequest.add("Authorization", Request->get("Authorization")); + } else { + ProxyRequest.add("X-API-KEY", Svc.AccessKey); + ProxyRequest.add("X-INTERNAL-NAME", MicroServicePublicEndPoint()); + } - if(Request->getMethod() == Poco::Net::HTTPRequest::HTTP_DELETE) { - Session.sendRequest(ProxyRequest); - Poco::Net::HTTPResponse ProxyResponse; - Session.receiveResponse(ProxyResponse); - Response->setStatus(ProxyResponse.getStatus()); - Response->send(); - return; - } else { - Poco::JSON::Parser P; - std::stringstream SS; - try { - auto Body = P.parse(Request->stream()).extract(); - Poco::JSON::Stringifier::condense(Body,SS); - SS << "\r\n\r\n"; - } catch(const Poco::Exception &E) { - Logger.log(E); - } + if (Request->getMethod() == Poco::Net::HTTPRequest::HTTP_DELETE) { + Session.sendRequest(ProxyRequest); + Poco::Net::HTTPResponse ProxyResponse; + Session.receiveResponse(ProxyResponse); + Response->setStatus(ProxyResponse.getStatus()); + Response->send(); + return; + } else { + Poco::JSON::Parser P; + std::stringstream SS; + try { + auto Body = P.parse(Request->stream()).extract(); + Poco::JSON::Stringifier::condense(Body, SS); + SS << "\r\n\r\n"; + } catch (const Poco::Exception &E) { + Logger.log(E); + } - if(SS.str().empty()) { - Session.sendRequest(ProxyRequest); - } else { - ProxyRequest.setContentType("application/json"); - ProxyRequest.setContentLength(SS.str().size()); - std::ostream & os = Session.sendRequest(ProxyRequest); - os << SS.str() ; - } + if (SS.str().empty()) { + Session.sendRequest(ProxyRequest); + } else { + ProxyRequest.setContentType("application/json"); + ProxyRequest.setContentLength(SS.str().size()); + std::ostream &os = Session.sendRequest(ProxyRequest); + os << SS.str(); + } - Poco::Net::HTTPResponse ProxyResponse; - std::stringstream SSR; - try { - std::istream &ProxyResponseStream = Session.receiveResponse(ProxyResponse); - Poco::JSON::Parser P2; - auto ProxyResponseBody = P2.parse(ProxyResponseStream).extract(); - Poco::JSON::Stringifier::condense(ProxyResponseBody,SSR); - Response->setContentType("application/json"); - Response->setContentLength(SSR.str().size()); - Response->setStatus(ProxyResponse.getStatus()); - Response->sendBuffer(SSR.str().c_str(),SSR.str().size()); - return; - } catch( const Poco::Exception & E) { + Poco::Net::HTTPResponse ProxyResponse; + std::stringstream SSR; + try { + std::istream &ProxyResponseStream = Session.receiveResponse(ProxyResponse); + Poco::JSON::Parser P2; + auto ProxyResponseBody = + P2.parse(ProxyResponseStream).extract(); + Poco::JSON::Stringifier::condense(ProxyResponseBody, SSR); + Response->setContentType("application/json"); + Response->setContentLength(SSR.str().size()); + Response->setStatus(ProxyResponse.getStatus()); + Response->sendBuffer(SSR.str().c_str(), SSR.str().size()); + return; + } catch (const Poco::Exception &E) { + } + Response->setStatus(ProxyResponse.getStatus()); + Response->send(); + return; + } + } - } - Response->setStatus(ProxyResponse.getStatus()); - Response->send(); - return; - } - } - - } catch (const Poco::Exception &E) { - Logger.log(E); - } - } -} \ No newline at end of file + } catch (const Poco::Exception &E) { + Logger.log(E); + } + } +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/AppServiceRegistry.h b/src/framework/AppServiceRegistry.h index 0466df7..4855c67 100644 --- a/src/framework/AppServiceRegistry.h +++ b/src/framework/AppServiceRegistry.h @@ -4,13 +4,13 @@ #pragma once -#include #include #include #include +#include -#include "Poco/StreamCopier.h" #include "Poco/File.h" +#include "Poco/StreamCopier.h" #include "framework/MicroServiceFuncs.h" @@ -18,7 +18,6 @@ namespace OpenWifi { - class AppServiceRegistry { public: AppServiceRegistry() { @@ -26,9 +25,9 @@ namespace OpenWifi { Poco::File F(FileName); try { - if(F.exists()) { - std::ostringstream OS; - std::ifstream IF(FileName); + if (F.exists()) { + std::ostringstream OS; + std::ifstream IF(FileName); Poco::StreamCopier::copyStream(IF, OS); Registry_ = nlohmann::json::parse(OS.str()); } @@ -37,55 +36,53 @@ namespace OpenWifi { } } - static AppServiceRegistry & instance() { - static auto instance_= new AppServiceRegistry; + static AppServiceRegistry &instance() { + static auto instance_ = new AppServiceRegistry; return *instance_; } - inline ~AppServiceRegistry() { - Save(); - } + inline ~AppServiceRegistry() { Save(); } inline void Save() { - std::istringstream IS( to_string(Registry_)); - std::ofstream OF; - OF.open(FileName,std::ios::binary | std::ios::trunc); + std::istringstream IS(to_string(Registry_)); + std::ofstream OF; + OF.open(FileName, std::ios::binary | std::ios::trunc); Poco::StreamCopier::copyStream(IS, OF); } - inline void Set(const char *Key, uint64_t Value ) { + inline void Set(const char *Key, uint64_t Value) { Registry_[Key] = Value; Save(); } - inline void Set(const char *Key, const std::string &Value ) { + inline void Set(const char *Key, const std::string &Value) { Registry_[Key] = Value; Save(); } - inline void Set(const char *Key, bool Value ) { + inline void Set(const char *Key, bool Value) { Registry_[Key] = Value; Save(); } - inline bool Get(const char *Key, bool & Value ) { - if(Registry_[Key].is_boolean()) { + inline bool Get(const char *Key, bool &Value) { + if (Registry_[Key].is_boolean()) { Value = Registry_[Key].get(); return true; } return false; } - inline bool Get(const char *Key, uint64_t & Value ) { - if(Registry_[Key].is_number_unsigned()) { + inline bool Get(const char *Key, uint64_t &Value) { + if (Registry_[Key].is_number_unsigned()) { Value = Registry_[Key].get(); return true; } return false; } - inline bool Get(const char *Key, std::string & Value ) { - if(Registry_[Key].is_string()) { + inline bool Get(const char *Key, std::string &Value) { + if (Registry_[Key].is_string()) { Value = Registry_[Key].get(); return true; } @@ -93,10 +90,10 @@ namespace OpenWifi { } private: - std::string FileName; - nlohmann::json Registry_; + std::string FileName; + nlohmann::json Registry_; }; inline auto AppServiceRegistry() { return AppServiceRegistry::instance(); } -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/AuthClient.cpp b/src/framework/AuthClient.cpp index 24c498d..3f2e1c3 100644 --- a/src/framework/AuthClient.cpp +++ b/src/framework/AuthClient.cpp @@ -4,41 +4,40 @@ #include "Poco/Net/HTTPServerResponse.h" +#include "fmt/format.h" #include "framework/AuthClient.h" #include "framework/MicroServiceNames.h" #include "framework/OpenAPIRequests.h" #include "framework/utils.h" -#include "fmt/format.h" namespace OpenWifi { - bool AuthClient::RetrieveTokenInformation(const std::string & SessionToken, - SecurityObjects::UserInfoAndPolicy & UInfo, - std::uint64_t TID, - bool & Expired, bool & Contacted, bool Sub) { + bool AuthClient::RetrieveTokenInformation(const std::string &SessionToken, + SecurityObjects::UserInfoAndPolicy &UInfo, + std::uint64_t TID, bool &Expired, bool &Contacted, + bool Sub) { try { Types::StringPairVec QueryData; - QueryData.push_back(std::make_pair("token",SessionToken)); - std::string AlternateURIForLogging = fmt::format("{}?token={}", Sub ? "/api/v1/validateSubToken" : "/api/v1/validateToken", Utils::SanitizeToken(SessionToken)); - OpenAPIRequestGet Req( uSERVICE_SECURITY, + QueryData.push_back(std::make_pair("token", SessionToken)); + std::string AlternateURIForLogging = fmt::format( + "{}?token={}", Sub ? "/api/v1/validateSubToken" : "/api/v1/validateToken", + Utils::SanitizeToken(SessionToken)); + OpenAPIRequestGet Req(uSERVICE_SECURITY, Sub ? "/api/v1/validateSubToken" : "/api/v1/validateToken", - QueryData, - 10000, - AlternateURIForLogging - ); + QueryData, 10000, AlternateURIForLogging); Poco::JSON::Object::Ptr Response; auto StatusCode = Req.Do(Response); - if(StatusCode==Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT) { + if (StatusCode == Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT) { Contacted = false; return false; } Contacted = true; - if(StatusCode==Poco::Net::HTTPServerResponse::HTTP_OK) { - if(Response->has("tokenInfo") && Response->has("userInfo")) { + if (StatusCode == Poco::Net::HTTPServerResponse::HTTP_OK) { + if (Response->has("tokenInfo") && Response->has("userInfo")) { UInfo.from_json(Response); - if(IsTokenExpired(UInfo.webtoken)) { + if (IsTokenExpired(UInfo.webtoken)) { Expired = true; return false; } @@ -50,18 +49,19 @@ namespace OpenWifi { } } } catch (...) { - poco_error(Logger(),fmt::format("Failed to retrieve token={} for TID={}", Utils::SanitizeToken(SessionToken), TID)); + poco_error(Logger(), fmt::format("Failed to retrieve token={} for TID={}", + Utils::SanitizeToken(SessionToken), TID)); } Expired = false; return false; } - bool AuthClient::IsAuthorized(const std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo, - std::uint64_t TID, - bool & Expired, bool & Contacted, bool Sub) { + bool AuthClient::IsAuthorized(const std::string &SessionToken, + SecurityObjects::UserInfoAndPolicy &UInfo, std::uint64_t TID, + bool &Expired, bool &Contacted, bool Sub) { auto User = Cache_.get(SessionToken); - if(!User.isNull()) { - if(IsTokenExpired(User->webtoken)) { + if (!User.isNull()) { + if (IsTokenExpired(User->webtoken)) { Expired = true; Cache_.remove(SessionToken); return false; @@ -73,57 +73,60 @@ namespace OpenWifi { return RetrieveTokenInformation(SessionToken, UInfo, TID, Expired, Contacted, Sub); } - bool AuthClient::RetrieveApiKeyInformation(const std::string & SessionToken, - SecurityObjects::UserInfoAndPolicy & UInfo, - std::uint64_t TID, - bool & Expired, bool & Contacted, [[maybe_unused]] bool & Suspended) { - try { - Types::StringPairVec QueryData; - QueryData.push_back(std::make_pair("apikey",SessionToken)); - std::string AlternateURIForLogging = fmt::format("/api/v1/validateApiKey?apiKey={}", Utils::SanitizeToken(SessionToken)); - OpenAPIRequestGet Req( uSERVICE_SECURITY, - "/api/v1/validateApiKey" , - QueryData, - 10000, - AlternateURIForLogging); - Poco::JSON::Object::Ptr Response; + bool AuthClient::RetrieveApiKeyInformation(const std::string &SessionToken, + SecurityObjects::UserInfoAndPolicy &UInfo, + std::uint64_t TID, bool &Expired, bool &Contacted, + [[maybe_unused]] bool &Suspended) { + try { + Types::StringPairVec QueryData; + QueryData.push_back(std::make_pair("apikey", SessionToken)); + std::string AlternateURIForLogging = + fmt::format("/api/v1/validateApiKey?apiKey={}", Utils::SanitizeToken(SessionToken)); + OpenAPIRequestGet Req(uSERVICE_SECURITY, "/api/v1/validateApiKey", QueryData, 10000, + AlternateURIForLogging); + Poco::JSON::Object::Ptr Response; - auto StatusCode = Req.Do(Response); - if(StatusCode==Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT) { - Contacted = false; - return false; - } + auto StatusCode = Req.Do(Response); + if (StatusCode == Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT) { + Contacted = false; + return false; + } - Contacted = true; - if(StatusCode==Poco::Net::HTTPServerResponse::HTTP_OK) { - if(Response->has("tokenInfo") && Response->has("userInfo") && Response->has("expiresOn")) { - UInfo.from_json(Response); - Expired = false; - ApiKeyCache_.update(SessionToken, ApiKeyCacheEntry{ .UserInfo = UInfo, .ExpiresOn = Response->get("expiresOn")}); - return true; - } else { - return false; - } - } - } catch (...) { - poco_error(Logger(),fmt::format("Failed to retrieve api key={} for TID={}", Utils::SanitizeToken(SessionToken), TID)); - } - Expired = false; - return false; - } + Contacted = true; + if (StatusCode == Poco::Net::HTTPServerResponse::HTTP_OK) { + if (Response->has("tokenInfo") && Response->has("userInfo") && + Response->has("expiresOn")) { + UInfo.from_json(Response); + Expired = false; + ApiKeyCache_.update(SessionToken, + ApiKeyCacheEntry{.UserInfo = UInfo, + .ExpiresOn = Response->get("expiresOn")}); + return true; + } else { + return false; + } + } + } catch (...) { + poco_error(Logger(), fmt::format("Failed to retrieve api key={} for TID={}", + Utils::SanitizeToken(SessionToken), TID)); + } + Expired = false; + return false; + } - bool AuthClient::IsValidApiKey(const std::string &SessionToken, SecurityObjects::UserInfoAndPolicy &UInfo, - std::uint64_t TID, bool &Expired, bool &Contacted, bool & Suspended) { - auto User = ApiKeyCache_.get(SessionToken); - if (!User.isNull()) { - if(User->ExpiresOn < Utils::Now()) { - Expired = false; - UInfo = User->UserInfo; - return true; - } + bool AuthClient::IsValidApiKey(const std::string &SessionToken, + SecurityObjects::UserInfoAndPolicy &UInfo, std::uint64_t TID, + bool &Expired, bool &Contacted, bool &Suspended) { + auto User = ApiKeyCache_.get(SessionToken); + if (!User.isNull()) { + if (User->ExpiresOn < Utils::Now()) { + Expired = false; + UInfo = User->UserInfo; + return true; + } ApiKeyCache_.remove(SessionToken); - } - return RetrieveApiKeyInformation(SessionToken, UInfo, TID, Expired, Contacted, Suspended); - } + } + return RetrieveApiKeyInformation(SessionToken, UInfo, TID, Expired, Contacted, Suspended); + } } // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/AuthClient.h b/src/framework/AuthClient.h index 0ff4f6c..706dff8 100644 --- a/src/framework/AuthClient.h +++ b/src/framework/AuthClient.h @@ -4,9 +4,9 @@ #pragma once -#include "framework/SubSystemServer.h" -#include "RESTObjects/RESTAPI_SecurityObjects.h" #include "Poco/ExpireLRUCache.h" +#include "RESTObjects/RESTAPI_SecurityObjects.h" +#include "framework/SubSystemServer.h" #include "framework/utils.h" namespace OpenWifi { @@ -14,66 +14,59 @@ namespace OpenWifi { class AuthClient : public SubSystemServer { public: - explicit AuthClient() noexcept: - SubSystemServer("Authentication", "AUTH-CLNT", "authentication") - { - } + explicit AuthClient() noexcept + : SubSystemServer("Authentication", "AUTH-CLNT", "authentication") {} static auto instance() { static auto instance_ = new AuthClient; return instance_; } - struct ApiKeyCacheEntry { - OpenWifi::SecurityObjects::UserInfoAndPolicy UserInfo; - std::uint64_t ExpiresOn; - }; + struct ApiKeyCacheEntry { + OpenWifi::SecurityObjects::UserInfoAndPolicy UserInfo; + std::uint64_t ExpiresOn; + }; - inline int Start() override { - return 0; - } + inline int Start() override { return 0; } inline void Stop() override { - poco_information(Logger(),"Stopping..."); - std::lock_guard G(Mutex_); + poco_information(Logger(), "Stopping..."); + std::lock_guard G(Mutex_); Cache_.clear(); - poco_information(Logger(),"Stopped..."); + poco_information(Logger(), "Stopped..."); } inline void RemovedCachedToken(const std::string &Token) { Cache_.remove(Token); - ApiKeyCache_.remove(Token); + ApiKeyCache_.remove(Token); } inline static bool IsTokenExpired(const SecurityObjects::WebToken &T) { - return ((T.expires_in_+T.created_) < Utils::Now()); + return ((T.expires_in_ + T.created_) < Utils::Now()); } - bool RetrieveTokenInformation(const std::string & SessionToken, - SecurityObjects::UserInfoAndPolicy & UInfo, - std::uint64_t TID, - bool & Expired, bool & Contacted, bool Sub=false); + bool RetrieveTokenInformation(const std::string &SessionToken, + SecurityObjects::UserInfoAndPolicy &UInfo, std::uint64_t TID, + bool &Expired, bool &Contacted, bool Sub = false); - bool RetrieveApiKeyInformation(const std::string & SessionToken, - SecurityObjects::UserInfoAndPolicy & UInfo, - std::uint64_t TID, - bool & Expired, bool & Contacted, bool & Suspended); + bool RetrieveApiKeyInformation(const std::string &SessionToken, + SecurityObjects::UserInfoAndPolicy &UInfo, std::uint64_t TID, + bool &Expired, bool &Contacted, bool &Suspended); - bool IsAuthorized(const std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo, - std::uint64_t TID, - bool & Expired, bool & Contacted, bool Sub = false); + bool IsAuthorized(const std::string &SessionToken, + SecurityObjects::UserInfoAndPolicy &UInfo, std::uint64_t TID, + bool &Expired, bool &Contacted, bool Sub = false); - bool IsValidApiKey(const std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo, - std::uint64_t TID, - bool & Expired, bool & Contacted, bool & Suspended) ; + bool IsValidApiKey(const std::string &SessionToken, + SecurityObjects::UserInfoAndPolicy &UInfo, std::uint64_t TID, + bool &Expired, bool &Contacted, bool &Suspended); private: - - Poco::ExpireLRUCache Cache_{512,1200000 }; - Poco::ExpireLRUCache ApiKeyCache_{512,1200000 }; + Poco::ExpireLRUCache Cache_{ + 512, 1200000}; + Poco::ExpireLRUCache ApiKeyCache_{512, 1200000}; }; inline auto AuthClient() { return AuthClient::instance(); } } // namespace OpenWifi - diff --git a/src/framework/CIDR.h b/src/framework/CIDR.h index 0d40f52..3a7fd92 100644 --- a/src/framework/CIDR.h +++ b/src/framework/CIDR.h @@ -21,13 +21,13 @@ namespace OpenWifi::CIDR { } static bool cidr6_match(const in6_addr &address, const in6_addr &network, uint8_t bits) { - #ifdef __linux__ +#ifdef __linux__ const uint32_t *a = address.s6_addr32; const uint32_t *n = network.s6_addr32; - #else +#else const uint32_t *a = address.__u6_addr.__u6_addr32; const uint32_t *n = network.__u6_addr.__u6_addr32; - #endif +#endif int bits_whole, bits_incomplete; bits_whole = bits >> 5; // number of whole u32 bits_incomplete = bits & 0x1F; // number of bits in incomplete u32 @@ -152,4 +152,4 @@ namespace OpenWifi::CIDR { [[nodiscard]] inline bool ValidateIpRanges(const Types::StringVec &Ranges) { return std::all_of(cbegin(Ranges), cend(Ranges), ValidateRange); } -} \ No newline at end of file +} // namespace OpenWifi::CIDR \ No newline at end of file diff --git a/src/framework/ConfigurationValidator.cpp b/src/framework/ConfigurationValidator.cpp index 0a194cb..85ba6b3 100644 --- a/src/framework/ConfigurationValidator.cpp +++ b/src/framework/ConfigurationValidator.cpp @@ -2,8 +2,8 @@ // Created by stephane bourque on 2021-09-14. // -#include #include +#include #include #include "ConfigurationValidator.h" @@ -17,14 +17,15 @@ #include "fmt/format.h" #include -#include +#include #include #include +#include #include -#include static const std::string GitUCentralJSONSchemaFile{ - "https://raw.githubusercontent.com/Telecominfraproject/wlan-ucentral-schema/main/ucentral.schema.json"}; + "https://raw.githubusercontent.com/Telecominfraproject/wlan-ucentral-schema/main/" + "ucentral.schema.json"}; static std::string DefaultUCentralSchema = R"foo( @@ -3184,7 +3185,6 @@ static std::string DefaultUCentralSchema = R"foo( )foo"; - static inline bool IsIPv4(const std::string &value) { Poco::Net::IPAddress A; return ((Poco::Net::IPAddress::tryParse(value, A) && A.family() == Poco::Net::IPAddress::IPv4)); @@ -3242,57 +3242,68 @@ bool ExternalValijsonFormatChecker(const std::string &format, const std::string if (format == "uc-cidr4") { if (IsCIDRv4(value)) return true; - if(results) results->pushError(context,fmt::format("{} is not a valid CIDR IPv4 block",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid CIDR IPv4 block", value)); } else if (format == "uc-cidr6") { if (IsCIDRv6(value)) return true; - if(results) results->pushError(context,fmt::format("{} is not a valid CIDR IPv6 block",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid CIDR IPv6 block", value)); } else if (format == "uc-cidr") { if (IsCIDR(value)) return true; - if(results) results->pushError(context,fmt::format("{} is not a valid CIDR block",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid CIDR block", value)); } else if (format == "uc-mac") { if (std::regex_match(value, mac_regex)) return true; - if(results) results->pushError(context,fmt::format("{} is not a valid MAC address",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid MAC address", value)); } else if (format == "uc-timeout") { if (std::regex_match(value, uc_timeout_regex)) return true; - if(results) results->pushError(context,fmt::format("{} is not a valid timeout value",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid timeout value", value)); } else if (format == "uc-host") { if (IsIP(value)) return true; if (std::regex_match(value, host_regex)) return true; - if(results) results->pushError(context,fmt::format("{} is not a valid hostname",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid hostname", value)); } else if (format == "fqdn" || format == "uc-fqdn") { if (std::regex_match(value, host_regex)) return true; - if(results) results->pushError(context,fmt::format("{} is not a valid FQDN",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid FQDN", value)); } else if (format == "uc-base64") { std::string s{value}; Poco::trimInPlace(s); if ((s.size() % 4 == 0) && std::regex_match(s, b64_regex)) return true; - if(results) results->pushError(context,fmt::format("{} is not a valid base 64 value",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid base 64 value", value)); } else if (format == "uri") { try { Poco::URI uri(value); return true; } catch (...) { } - if(results) results->pushError(context,fmt::format("{} is not a valid URL",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid URL", value)); } else if (format == "uc-portrange") { try { if (IsPortRangeIsValid(value)) return true; } catch (...) { } - if(results) results->pushError(context,fmt::format("{} is not a valid post range",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid post range", value)); } else if (format == "ip") { if (IsIP(value)) return true; - if(results) results->pushError(context,fmt::format("{} is not a valid IP address",value)); + if (results) + results->pushError(context, fmt::format("{} is not a valid IP address", value)); } return true; } @@ -3304,9 +3315,7 @@ namespace OpenWifi { return 0; } - void ConfigurationValidator::Stop() { - - } + void ConfigurationValidator::Stop() {} bool ConfigurationValidator::SetSchema(const std::string &SchemaStr) { try { @@ -3318,9 +3327,9 @@ namespace OpenWifi { SchemaParser_->populateSchema(*PocoJsonAdapter_, *RootSchema_); Initialized_ = Working_ = true; return true; - } catch(const Poco::Exception &E) { + } catch (const Poco::Exception &E) { Logger().log(E); - } catch(...) { + } catch (...) { Logger().error("Validation schema is invalid, falling back."); } return false; @@ -3331,7 +3340,7 @@ namespace OpenWifi { return; std::string GitSchema; - if(MicroServiceConfigGetBool("ucentral.datamodel.internal",true)) { + if (MicroServiceConfigGetBool("ucentral.datamodel.internal", true)) { SetSchema(DefaultUCentralSchema); poco_information(Logger(), "Using uCentral validation from built-in default."); return; @@ -3339,34 +3348,35 @@ namespace OpenWifi { try { auto GitURI = - MicroServiceConfigGetString("ucentral.datamodel.uri",GitUCentralJSONSchemaFile); - if(Utils::wgets(GitURI, GitSchema) && SetSchema(GitSchema)) { - poco_information(Logger(),"Using uCentral data model validation schema from GIT."); + MicroServiceConfigGetString("ucentral.datamodel.uri", GitUCentralJSONSchemaFile); + if (Utils::wgets(GitURI, GitSchema) && SetSchema(GitSchema)) { + poco_information(Logger(), "Using uCentral data model validation schema from GIT."); + return; + } else { + std::string FileName{MicroServiceDataDirectory() + "/ucentral.schema.json"}; + std::ifstream input(FileName); + std::stringstream schema_file; + schema_file << input.rdbuf(); + input.close(); + if (SetSchema(schema_file.str())) { + poco_information( + Logger(), "Using uCentral data model validation schema from local file."); return; - } else { - std::string FileName{ MicroServiceDataDirectory() + "/ucentral.schema.json" }; - std::ifstream input(FileName); - std::stringstream schema_file; - schema_file << input.rdbuf(); - input.close(); - if(SetSchema(schema_file.str())) { - poco_information(Logger(), - "Using uCentral data model validation schema from local file."); - return; - } } + } } catch (const Poco::Exception &E) { } catch (...) { - } SetSchema(DefaultUCentralSchema); - poco_information(Logger(),"Using uCentral data model validation schema from built-in default."); + poco_information(Logger(), + "Using uCentral data model validation schema from built-in default."); } - bool ConfigurationValidator::Validate(const std::string &C, std::vector &Errors, bool Strict) { - if(Working_) { - try { + bool ConfigurationValidator::Validate(const std::string &C, std::vector &Errors, + bool Strict) { + if (Working_) { + try { Poco::JSON::Parser P; auto Doc = P.parse(C).extract(); valijson::adapters::PocoJsonAdapter Tester(Doc); @@ -3375,27 +3385,28 @@ namespace OpenWifi { if (Validator.validate(*RootSchema_, Tester, &Results)) { return true; } - for(const auto &error:Results) { + for (const auto &error : Results) { Errors.push_back(error.description); } return false; - } catch(const Poco::Exception &E) { + } catch (const Poco::Exception &E) { Logger().log(E); - } catch(const std::exception &E) { - Logger().warning(fmt::format("Error wile validating a configuration (1): {}", E.what())); - } catch(...) { + } catch (const std::exception &E) { + Logger().warning( + fmt::format("Error wile validating a configuration (1): {}", E.what())); + } catch (...) { Logger().warning("Error wile validating a configuration (2)"); - } - } - if(Strict) + } + } + if (Strict) return false; - return true; - } + return true; + } - void ConfigurationValidator::reinitialize([[maybe_unused]] Poco::Util::Application &self) { - poco_information(Logger(),"Reinitializing."); - Working_ = Initialized_ = false; - Init(); - } + void ConfigurationValidator::reinitialize([[maybe_unused]] Poco::Util::Application &self) { + poco_information(Logger(), "Reinitializing."); + Working_ = Initialized_ = false; + Init(); + } -} +} // namespace OpenWifi diff --git a/src/framework/ConfigurationValidator.h b/src/framework/ConfigurationValidator.h index 77e4df9..58ef180 100644 --- a/src/framework/ConfigurationValidator.h +++ b/src/framework/ConfigurationValidator.h @@ -7,43 +7,43 @@ #include "framework/SubSystemServer.h" #include -#include -#include -#include -#include #include #include +#include +#include +#include +#include namespace OpenWifi { - class ConfigurationValidator : public SubSystemServer { - public: + class ConfigurationValidator : public SubSystemServer { + public: + static auto instance() { + static auto instance_ = new ConfigurationValidator; + return instance_; + } - static auto instance() { - static auto instance_ = new ConfigurationValidator; - return instance_; - } + bool Validate(const std::string &C, std::vector &Errors, bool Strict); + int Start() override; + void Stop() override; + void reinitialize(Poco::Util::Application &self) override; - bool Validate(const std::string &C, std::vector &Errors, bool Strict); - int Start() override; - void Stop() override; - void reinitialize(Poco::Util::Application &self) override; + private: + bool Initialized_ = false; + bool Working_ = false; + void Init(); + std::unique_ptr RootSchema_; + std::unique_ptr SchemaParser_; + std::unique_ptr PocoJsonAdapter_; + Poco::JSON::Object::Ptr SchemaDocPtr_; + bool SetSchema(const std::string &SchemaStr); - private: - bool Initialized_=false; - bool Working_=false; - void Init(); - std::unique_ptr RootSchema_; - std::unique_ptr SchemaParser_; - std::unique_ptr PocoJsonAdapter_; - Poco::JSON::Object::Ptr SchemaDocPtr_; - bool SetSchema(const std::string &SchemaStr); - - ConfigurationValidator(): - SubSystemServer("ConfigValidator", "CFG-VALIDATOR", "config.validator") { - } - }; - - inline auto ConfigurationValidator() { return ConfigurationValidator::instance(); } - inline bool ValidateUCentralConfiguration(const std::string &C, std::vector &Error, bool strict) { return ConfigurationValidator::instance()->Validate(C, Error, strict); } -} + ConfigurationValidator() + : SubSystemServer("ConfigValidator", "CFG-VALIDATOR", "config.validator") {} + }; + inline auto ConfigurationValidator() { return ConfigurationValidator::instance(); } + inline bool ValidateUCentralConfiguration(const std::string &C, std::vector &Error, + bool strict) { + return ConfigurationValidator::instance()->Validate(C, Error, strict); + } +} // namespace OpenWifi diff --git a/src/framework/CountryCodes.h b/src/framework/CountryCodes.h index 92a002a..282c1f4 100644 --- a/src/framework/CountryCodes.h +++ b/src/framework/CountryCodes.h @@ -4,268 +4,266 @@ #pragma once -#include #include #include +#include namespace OpenWifi { - - struct CountryInfo { - std::string code; - std::string name; - }; - - inline static const std::vector CountryCodes { - { .code= "US", .name= "United States" }, - { .code= "GB", .name= "United Kingdom" }, - { .code= "CA", .name= "Canada" }, - { .code= "AF", .name= "Afghanistan" }, - { .code= "AX", .name= "Aland Islands" }, - { .code= "AL", .name= "Albania" }, - { .code= "DZ", .name= "Algeria" }, - { .code= "AS", .name= "American Samoa" }, - { .code= "AD", .name= "Andorra" }, - { .code= "AO", .name= "Angola" }, - { .code= "AI", .name= "Anguilla" }, - { .code= "AQ", .name= "Antarctica" }, - { .code= "AG", .name= "Antigua And Barbuda" }, - { .code= "AR", .name= "Argentina" }, - { .code= "AM", .name= "Armenia" }, - { .code= "AN", .name= "Netherlands Antilles" }, - { .code= "AW", .name= "Aruba" }, - { .code= "AU", .name= "Australia" }, - { .code= "AT", .name= "Austria" }, - { .code= "AZ", .name= "Azerbaijan" }, - { .code= "BS", .name= "Bahamas" }, - { .code= "BH", .name= "Bahrain" }, - { .code= "BD", .name= "Bangladesh" }, - { .code= "BB", .name= "Barbados" }, - { .code= "BY", .name= "Belarus" }, - { .code= "BE", .name= "Belgium" }, - { .code= "BZ", .name= "Belize" }, - { .code= "BJ", .name= "Benin" }, - { .code= "BM", .name= "Bermuda" }, - { .code= "BT", .name= "Bhutan" }, - { .code= "BO", .name= "Bolivia" }, - { .code= "BA", .name= "Bosnia And Herzegovina" }, - { .code= "BW", .name= "Botswana" }, - { .code= "BV", .name= "Bouvet Island" }, - { .code= "BR", .name= "Brazil" }, - { .code= "IO", .name= "British Indian Ocean Territory" }, - { .code= "BN", .name= "Brunei Darussalam" }, - { .code= "BG", .name= "Bulgaria" }, - { .code= "BF", .name= "Burkina Faso" }, - { .code= "BI", .name= "Burundi" }, - { .code= "KH", .name= "Cambodia" }, - { .code= "CM", .name= "Cameroon" }, - { .code= "CA", .name= "Canada" }, - { .code= "CV", .name= "Cape Verde" }, - { .code= "KY", .name= "Cayman Islands" }, - { .code= "CF", .name= "Central African Republic" }, - { .code= "TD", .name= "Chad" }, - { .code= "CL", .name= "Chile" }, - { .code= "CN", .name= "China" }, - { .code= "CX", .name= "Christmas Island" }, - { .code= "CC", .name= "Cocos (Keeling) Islands" }, - { .code= "CO", .name= "Colombia" }, - { .code= "KM", .name= "Comoros" }, - { .code= "CG", .name= "Congo" }, - { .code= "CD", .name= "Congo, Democratic Republic" }, - { .code= "CK", .name= "Cook Islands" }, - { .code= "CR", .name= "Costa Rica" }, - { .code= "CI", .name= "Cote D\"Ivoire" }, - { .code= "HR", .name= "Croatia" }, - { .code= "CU", .name= "Cuba" }, - { .code= "CY", .name= "Cyprus" }, - { .code= "CZ", .name= "Czech Republic" }, - { .code= "DK", .name= "Denmark" }, - { .code= "DJ", .name= "Djibouti" }, - { .code= "DM", .name= "Dominica" }, - { .code= "DO", .name= "Dominican Republic" }, - { .code= "EC", .name= "Ecuador" }, - { .code= "EG", .name= "Egypt" }, - { .code= "SV", .name= "El Salvador" }, - { .code= "GQ", .name= "Equatorial Guinea" }, - { .code= "ER", .name= "Eritrea" }, - { .code= "EE", .name= "Estonia" }, - { .code= "ET", .name= "Ethiopia" }, - { .code= "FK", .name= "Falkland Islands (Malvinas)" }, - { .code= "FO", .name= "Faroe Islands" }, - { .code= "FJ", .name= "Fiji" }, - { .code= "FI", .name= "Finland" }, - { .code= "FR", .name= "France" }, - { .code= "GF", .name= "French Guiana" }, - { .code= "PF", .name= "French Polynesia" }, - { .code= "TF", .name= "French Southern Territories" }, - { .code= "GA", .name= "Gabon" }, - { .code= "GM", .name= "Gambia" }, - { .code= "GE", .name= "Georgia" }, - { .code= "DE", .name= "Germany" }, - { .code= "GH", .name= "Ghana" }, - { .code= "GI", .name= "Gibraltar" }, - { .code= "GR", .name= "Greece" }, - { .code= "GL", .name= "Greenland" }, - { .code= "GD", .name= "Grenada" }, - { .code= "GP", .name= "Guadeloupe" }, - { .code= "GU", .name= "Guam" }, - { .code= "GT", .name= "Guatemala" }, - { .code= "GG", .name= "Guernsey" }, - { .code= "GN", .name= "Guinea" }, - { .code= "GW", .name= "Guinea-Bissau" }, - { .code= "GY", .name= "Guyana" }, - { .code= "HT", .name= "Haiti" }, - { .code= "HM", .name= "Heard Island & Mcdonald Islands" }, - { .code= "VA", .name= "Holy See (Vatican City State)" }, - { .code= "HN", .name= "Honduras" }, - { .code= "HK", .name= "Hong Kong" }, - { .code= "HU", .name= "Hungary" }, - { .code= "IS", .name= "Iceland" }, - { .code= "IN", .name= "India" }, - { .code= "ID", .name= "Indonesia" }, - { .code= "IR", .name= "Iran, Islamic Republic Of" }, - { .code= "IQ", .name= "Iraq" }, - { .code= "IE", .name= "Ireland" }, - { .code= "IM", .name= "Isle Of Man" }, - { .code= "IL", .name= "Israel" }, - { .code= "IT", .name= "Italy" }, - { .code= "JM", .name= "Jamaica" }, - { .code= "JP", .name= "Japan" }, - { .code= "JE", .name= "Jersey" }, - { .code= "JO", .name= "Jordan" }, - { .code= "KZ", .name= "Kazakhstan" }, - { .code= "KE", .name= "Kenya" }, - { .code= "KI", .name= "Kiribati" }, - { .code= "KR", .name= "Korea" }, - { .code= "KW", .name= "Kuwait" }, - { .code= "KG", .name= "Kyrgyzstan" }, - { .code= "LA", .name= "Lao People\"s Democratic Republic" }, - { .code= "LV", .name= "Latvia" }, - { .code= "LB", .name= "Lebanon" }, - { .code= "LS", .name= "Lesotho" }, - { .code= "LR", .name= "Liberia" }, - { .code= "LY", .name= "Libyan Arab Jamahiriya" }, - { .code= "LI", .name= "Liechtenstein" }, - { .code= "LT", .name= "Lithuania" }, - { .code= "LU", .name= "Luxembourg" }, - { .code= "MO", .name= "Macao" }, - { .code= "MK", .name= "Macedonia" }, - { .code= "MG", .name= "Madagascar" }, - { .code= "MW", .name= "Malawi" }, - { .code= "MY", .name= "Malaysia" }, - { .code= "MV", .name= "Maldives" }, - { .code= "ML", .name= "Mali" }, - { .code= "MT", .name= "Malta" }, - { .code= "MH", .name= "Marshall Islands" }, - { .code= "MQ", .name= "Martinique" }, - { .code= "MR", .name= "Mauritania" }, - { .code= "MU", .name= "Mauritius" }, - { .code= "YT", .name= "Mayotte" }, - { .code= "MX", .name= "Mexico" }, - { .code= "FM", .name= "Micronesia, Federated States Of" }, - { .code= "MD", .name= "Moldova" }, - { .code= "MC", .name= "Monaco" }, - { .code= "MN", .name= "Mongolia" }, - { .code= "ME", .name= "Montenegro" }, - { .code= "MS", .name= "Montserrat" }, - { .code= "MA", .name= "Morocco" }, - { .code= "MZ", .name= "Mozambique" }, - { .code= "MM", .name= "Myanmar" }, - { .code= "NA", .name= "Namibia" }, - { .code= "NR", .name= "Nauru" }, - { .code= "NP", .name= "Nepal" }, - { .code= "NL", .name= "Netherlands" }, - { .code= "AN", .name= "Netherlands Antilles" }, - { .code= "NC", .name= "New Caledonia" }, - { .code= "NZ", .name= "New Zealand" }, - { .code= "NI", .name= "Nicaragua" }, - { .code= "NE", .name= "Niger" }, - { .code= "NG", .name= "Nigeria" }, - { .code= "NU", .name= "Niue" }, - { .code= "NF", .name= "Norfolk Island" }, - { .code= "MP", .name= "Northern Mariana Islands" }, - { .code= "NO", .name= "Norway" }, - { .code= "OM", .name= "Oman" }, - { .code= "PK", .name= "Pakistan" }, - { .code= "PW", .name= "Palau" }, - { .code= "PS", .name= "Palestinian Territory, Occupied" }, - { .code= "PA", .name= "Panama" }, - { .code= "PG", .name= "Papua New Guinea" }, - { .code= "PY", .name= "Paraguay" }, - { .code= "PE", .name= "Peru" }, - { .code= "PH", .name= "Philippines" }, - { .code= "PN", .name= "Pitcairn" }, - { .code= "PL", .name= "Poland" }, - { .code= "PT", .name= "Portugal" }, - { .code= "PR", .name= "Puerto Rico" }, - { .code= "QA", .name= "Qatar" }, - { .code= "RE", .name= "Reunion" }, - { .code= "RO", .name= "Romania" }, - { .code= "RU", .name= "Russian Federation" }, - { .code= "RW", .name= "Rwanda" }, - { .code= "BL", .name= "Saint Barthelemy" }, - { .code= "SH", .name= "Saint Helena" }, - { .code= "KN", .name= "Saint Kitts And Nevis" }, - { .code= "LC", .name= "Saint Lucia" }, - { .code= "MF", .name= "Saint Martin" }, - { .code= "PM", .name= "Saint Pierre And Miquelon" }, - { .code= "VC", .name= "Saint Vincent And Grenadines" }, - { .code= "WS", .name= "Samoa" }, - { .code= "SM", .name= "San Marino" }, - { .code= "ST", .name= "Sao Tome And Principe" }, - { .code= "SA", .name= "Saudi Arabia" }, - { .code= "SN", .name= "Senegal" }, - { .code= "RS", .name= "Serbia" }, - { .code= "SC", .name= "Seychelles" }, - { .code= "SL", .name= "Sierra Leone" }, - { .code= "SG", .name= "Singapore" }, - { .code= "SK", .name= "Slovakia" }, - { .code= "SI", .name= "Slovenia" }, - { .code= "SB", .name= "Solomon Islands" }, - { .code= "SO", .name= "Somalia" }, - { .code= "ZA", .name= "South Africa" }, - { .code= "GS", .name= "South Georgia And Sandwich Isl." }, - { .code= "ES", .name= "Spain" }, - { .code= "LK", .name= "Sri Lanka" }, - { .code= "SD", .name= "Sudan" }, - { .code= "SR", .name= "Suriname" }, - { .code= "SJ", .name= "Svalbard And Jan Mayen" }, - { .code= "SZ", .name= "Swaziland" }, - { .code= "SE", .name= "Sweden" }, - { .code= "CH", .name= "Switzerland" }, - { .code= "SY", .name= "Syrian Arab Republic" }, - { .code= "TW", .name= "Taiwan" }, - { .code= "TJ", .name= "Tajikistan" }, - { .code= "TZ", .name= "Tanzania" }, - { .code= "TH", .name= "Thailand" }, - { .code= "TL", .name= "Timor-Leste" }, - { .code= "TG", .name= "Togo" }, - { .code= "TK", .name= "Tokelau" }, - { .code= "TO", .name= "Tonga" }, - { .code= "TT", .name= "Trinidad And Tobago" }, - { .code= "TN", .name= "Tunisia" }, - { .code= "TR", .name= "Turkey" }, - { .code= "TM", .name= "Turkmenistan" }, - { .code= "TC", .name= "Turks And Caicos Islands" }, - { .code= "TV", .name= "Tuvalu" }, - { .code= "UG", .name= "Uganda" }, - { .code= "UA", .name= "Ukraine" }, - { .code= "AE", .name= "United Arab Emirates" }, - { .code= "GB", .name= "United Kingdom" }, - { .code= "US", .name= "United States" }, - { .code= "UM", .name= "United States Outlying Islands" }, - { .code= "UY", .name= "Uruguay" }, - { .code= "UZ", .name= "Uzbekistan" }, - { .code= "VU", .name= "Vanuatu" }, - { .code= "VE", .name= "Venezuela" }, - { .code= "VN", .name= "Viet Nam" }, - { .code= "VG", .name= "Virgin Islands, British" }, - { .code= "VI", .name= "Virgin Islands, U.S." }, - { .code= "WF", .name= "Wallis And Futuna" }, - { .code= "EH", .name= "Western Sahara" }, - { .code= "YE", .name= "Yemen" }, - { .code= "ZM", .name= "Zambia" }, - { .code= "ZW", .name= "Zimbabwe" } - }; -} + struct CountryInfo { + std::string code; + std::string name; + }; + inline static const std::vector CountryCodes{ + {.code = "US", .name = "United States"}, + {.code = "GB", .name = "United Kingdom"}, + {.code = "CA", .name = "Canada"}, + {.code = "AF", .name = "Afghanistan"}, + {.code = "AX", .name = "Aland Islands"}, + {.code = "AL", .name = "Albania"}, + {.code = "DZ", .name = "Algeria"}, + {.code = "AS", .name = "American Samoa"}, + {.code = "AD", .name = "Andorra"}, + {.code = "AO", .name = "Angola"}, + {.code = "AI", .name = "Anguilla"}, + {.code = "AQ", .name = "Antarctica"}, + {.code = "AG", .name = "Antigua And Barbuda"}, + {.code = "AR", .name = "Argentina"}, + {.code = "AM", .name = "Armenia"}, + {.code = "AN", .name = "Netherlands Antilles"}, + {.code = "AW", .name = "Aruba"}, + {.code = "AU", .name = "Australia"}, + {.code = "AT", .name = "Austria"}, + {.code = "AZ", .name = "Azerbaijan"}, + {.code = "BS", .name = "Bahamas"}, + {.code = "BH", .name = "Bahrain"}, + {.code = "BD", .name = "Bangladesh"}, + {.code = "BB", .name = "Barbados"}, + {.code = "BY", .name = "Belarus"}, + {.code = "BE", .name = "Belgium"}, + {.code = "BZ", .name = "Belize"}, + {.code = "BJ", .name = "Benin"}, + {.code = "BM", .name = "Bermuda"}, + {.code = "BT", .name = "Bhutan"}, + {.code = "BO", .name = "Bolivia"}, + {.code = "BA", .name = "Bosnia And Herzegovina"}, + {.code = "BW", .name = "Botswana"}, + {.code = "BV", .name = "Bouvet Island"}, + {.code = "BR", .name = "Brazil"}, + {.code = "IO", .name = "British Indian Ocean Territory"}, + {.code = "BN", .name = "Brunei Darussalam"}, + {.code = "BG", .name = "Bulgaria"}, + {.code = "BF", .name = "Burkina Faso"}, + {.code = "BI", .name = "Burundi"}, + {.code = "KH", .name = "Cambodia"}, + {.code = "CM", .name = "Cameroon"}, + {.code = "CA", .name = "Canada"}, + {.code = "CV", .name = "Cape Verde"}, + {.code = "KY", .name = "Cayman Islands"}, + {.code = "CF", .name = "Central African Republic"}, + {.code = "TD", .name = "Chad"}, + {.code = "CL", .name = "Chile"}, + {.code = "CN", .name = "China"}, + {.code = "CX", .name = "Christmas Island"}, + {.code = "CC", .name = "Cocos (Keeling) Islands"}, + {.code = "CO", .name = "Colombia"}, + {.code = "KM", .name = "Comoros"}, + {.code = "CG", .name = "Congo"}, + {.code = "CD", .name = "Congo, Democratic Republic"}, + {.code = "CK", .name = "Cook Islands"}, + {.code = "CR", .name = "Costa Rica"}, + {.code = "CI", .name = "Cote D\"Ivoire"}, + {.code = "HR", .name = "Croatia"}, + {.code = "CU", .name = "Cuba"}, + {.code = "CY", .name = "Cyprus"}, + {.code = "CZ", .name = "Czech Republic"}, + {.code = "DK", .name = "Denmark"}, + {.code = "DJ", .name = "Djibouti"}, + {.code = "DM", .name = "Dominica"}, + {.code = "DO", .name = "Dominican Republic"}, + {.code = "EC", .name = "Ecuador"}, + {.code = "EG", .name = "Egypt"}, + {.code = "SV", .name = "El Salvador"}, + {.code = "GQ", .name = "Equatorial Guinea"}, + {.code = "ER", .name = "Eritrea"}, + {.code = "EE", .name = "Estonia"}, + {.code = "ET", .name = "Ethiopia"}, + {.code = "FK", .name = "Falkland Islands (Malvinas)"}, + {.code = "FO", .name = "Faroe Islands"}, + {.code = "FJ", .name = "Fiji"}, + {.code = "FI", .name = "Finland"}, + {.code = "FR", .name = "France"}, + {.code = "GF", .name = "French Guiana"}, + {.code = "PF", .name = "French Polynesia"}, + {.code = "TF", .name = "French Southern Territories"}, + {.code = "GA", .name = "Gabon"}, + {.code = "GM", .name = "Gambia"}, + {.code = "GE", .name = "Georgia"}, + {.code = "DE", .name = "Germany"}, + {.code = "GH", .name = "Ghana"}, + {.code = "GI", .name = "Gibraltar"}, + {.code = "GR", .name = "Greece"}, + {.code = "GL", .name = "Greenland"}, + {.code = "GD", .name = "Grenada"}, + {.code = "GP", .name = "Guadeloupe"}, + {.code = "GU", .name = "Guam"}, + {.code = "GT", .name = "Guatemala"}, + {.code = "GG", .name = "Guernsey"}, + {.code = "GN", .name = "Guinea"}, + {.code = "GW", .name = "Guinea-Bissau"}, + {.code = "GY", .name = "Guyana"}, + {.code = "HT", .name = "Haiti"}, + {.code = "HM", .name = "Heard Island & Mcdonald Islands"}, + {.code = "VA", .name = "Holy See (Vatican City State)"}, + {.code = "HN", .name = "Honduras"}, + {.code = "HK", .name = "Hong Kong"}, + {.code = "HU", .name = "Hungary"}, + {.code = "IS", .name = "Iceland"}, + {.code = "IN", .name = "India"}, + {.code = "ID", .name = "Indonesia"}, + {.code = "IR", .name = "Iran, Islamic Republic Of"}, + {.code = "IQ", .name = "Iraq"}, + {.code = "IE", .name = "Ireland"}, + {.code = "IM", .name = "Isle Of Man"}, + {.code = "IL", .name = "Israel"}, + {.code = "IT", .name = "Italy"}, + {.code = "JM", .name = "Jamaica"}, + {.code = "JP", .name = "Japan"}, + {.code = "JE", .name = "Jersey"}, + {.code = "JO", .name = "Jordan"}, + {.code = "KZ", .name = "Kazakhstan"}, + {.code = "KE", .name = "Kenya"}, + {.code = "KI", .name = "Kiribati"}, + {.code = "KR", .name = "Korea"}, + {.code = "KW", .name = "Kuwait"}, + {.code = "KG", .name = "Kyrgyzstan"}, + {.code = "LA", .name = "Lao People\"s Democratic Republic"}, + {.code = "LV", .name = "Latvia"}, + {.code = "LB", .name = "Lebanon"}, + {.code = "LS", .name = "Lesotho"}, + {.code = "LR", .name = "Liberia"}, + {.code = "LY", .name = "Libyan Arab Jamahiriya"}, + {.code = "LI", .name = "Liechtenstein"}, + {.code = "LT", .name = "Lithuania"}, + {.code = "LU", .name = "Luxembourg"}, + {.code = "MO", .name = "Macao"}, + {.code = "MK", .name = "Macedonia"}, + {.code = "MG", .name = "Madagascar"}, + {.code = "MW", .name = "Malawi"}, + {.code = "MY", .name = "Malaysia"}, + {.code = "MV", .name = "Maldives"}, + {.code = "ML", .name = "Mali"}, + {.code = "MT", .name = "Malta"}, + {.code = "MH", .name = "Marshall Islands"}, + {.code = "MQ", .name = "Martinique"}, + {.code = "MR", .name = "Mauritania"}, + {.code = "MU", .name = "Mauritius"}, + {.code = "YT", .name = "Mayotte"}, + {.code = "MX", .name = "Mexico"}, + {.code = "FM", .name = "Micronesia, Federated States Of"}, + {.code = "MD", .name = "Moldova"}, + {.code = "MC", .name = "Monaco"}, + {.code = "MN", .name = "Mongolia"}, + {.code = "ME", .name = "Montenegro"}, + {.code = "MS", .name = "Montserrat"}, + {.code = "MA", .name = "Morocco"}, + {.code = "MZ", .name = "Mozambique"}, + {.code = "MM", .name = "Myanmar"}, + {.code = "NA", .name = "Namibia"}, + {.code = "NR", .name = "Nauru"}, + {.code = "NP", .name = "Nepal"}, + {.code = "NL", .name = "Netherlands"}, + {.code = "AN", .name = "Netherlands Antilles"}, + {.code = "NC", .name = "New Caledonia"}, + {.code = "NZ", .name = "New Zealand"}, + {.code = "NI", .name = "Nicaragua"}, + {.code = "NE", .name = "Niger"}, + {.code = "NG", .name = "Nigeria"}, + {.code = "NU", .name = "Niue"}, + {.code = "NF", .name = "Norfolk Island"}, + {.code = "MP", .name = "Northern Mariana Islands"}, + {.code = "NO", .name = "Norway"}, + {.code = "OM", .name = "Oman"}, + {.code = "PK", .name = "Pakistan"}, + {.code = "PW", .name = "Palau"}, + {.code = "PS", .name = "Palestinian Territory, Occupied"}, + {.code = "PA", .name = "Panama"}, + {.code = "PG", .name = "Papua New Guinea"}, + {.code = "PY", .name = "Paraguay"}, + {.code = "PE", .name = "Peru"}, + {.code = "PH", .name = "Philippines"}, + {.code = "PN", .name = "Pitcairn"}, + {.code = "PL", .name = "Poland"}, + {.code = "PT", .name = "Portugal"}, + {.code = "PR", .name = "Puerto Rico"}, + {.code = "QA", .name = "Qatar"}, + {.code = "RE", .name = "Reunion"}, + {.code = "RO", .name = "Romania"}, + {.code = "RU", .name = "Russian Federation"}, + {.code = "RW", .name = "Rwanda"}, + {.code = "BL", .name = "Saint Barthelemy"}, + {.code = "SH", .name = "Saint Helena"}, + {.code = "KN", .name = "Saint Kitts And Nevis"}, + {.code = "LC", .name = "Saint Lucia"}, + {.code = "MF", .name = "Saint Martin"}, + {.code = "PM", .name = "Saint Pierre And Miquelon"}, + {.code = "VC", .name = "Saint Vincent And Grenadines"}, + {.code = "WS", .name = "Samoa"}, + {.code = "SM", .name = "San Marino"}, + {.code = "ST", .name = "Sao Tome And Principe"}, + {.code = "SA", .name = "Saudi Arabia"}, + {.code = "SN", .name = "Senegal"}, + {.code = "RS", .name = "Serbia"}, + {.code = "SC", .name = "Seychelles"}, + {.code = "SL", .name = "Sierra Leone"}, + {.code = "SG", .name = "Singapore"}, + {.code = "SK", .name = "Slovakia"}, + {.code = "SI", .name = "Slovenia"}, + {.code = "SB", .name = "Solomon Islands"}, + {.code = "SO", .name = "Somalia"}, + {.code = "ZA", .name = "South Africa"}, + {.code = "GS", .name = "South Georgia And Sandwich Isl."}, + {.code = "ES", .name = "Spain"}, + {.code = "LK", .name = "Sri Lanka"}, + {.code = "SD", .name = "Sudan"}, + {.code = "SR", .name = "Suriname"}, + {.code = "SJ", .name = "Svalbard And Jan Mayen"}, + {.code = "SZ", .name = "Swaziland"}, + {.code = "SE", .name = "Sweden"}, + {.code = "CH", .name = "Switzerland"}, + {.code = "SY", .name = "Syrian Arab Republic"}, + {.code = "TW", .name = "Taiwan"}, + {.code = "TJ", .name = "Tajikistan"}, + {.code = "TZ", .name = "Tanzania"}, + {.code = "TH", .name = "Thailand"}, + {.code = "TL", .name = "Timor-Leste"}, + {.code = "TG", .name = "Togo"}, + {.code = "TK", .name = "Tokelau"}, + {.code = "TO", .name = "Tonga"}, + {.code = "TT", .name = "Trinidad And Tobago"}, + {.code = "TN", .name = "Tunisia"}, + {.code = "TR", .name = "Turkey"}, + {.code = "TM", .name = "Turkmenistan"}, + {.code = "TC", .name = "Turks And Caicos Islands"}, + {.code = "TV", .name = "Tuvalu"}, + {.code = "UG", .name = "Uganda"}, + {.code = "UA", .name = "Ukraine"}, + {.code = "AE", .name = "United Arab Emirates"}, + {.code = "GB", .name = "United Kingdom"}, + {.code = "US", .name = "United States"}, + {.code = "UM", .name = "United States Outlying Islands"}, + {.code = "UY", .name = "Uruguay"}, + {.code = "UZ", .name = "Uzbekistan"}, + {.code = "VU", .name = "Vanuatu"}, + {.code = "VE", .name = "Venezuela"}, + {.code = "VN", .name = "Viet Nam"}, + {.code = "VG", .name = "Virgin Islands, British"}, + {.code = "VI", .name = "Virgin Islands, U.S."}, + {.code = "WF", .name = "Wallis And Futuna"}, + {.code = "EH", .name = "Western Sahara"}, + {.code = "YE", .name = "Yemen"}, + {.code = "ZM", .name = "Zambia"}, + {.code = "ZW", .name = "Zimbabwe"}}; + +} // namespace OpenWifi diff --git a/src/framework/EventBusManager.cpp b/src/framework/EventBusManager.cpp index 967ca58..ef182bf 100644 --- a/src/framework/EventBusManager.cpp +++ b/src/framework/EventBusManager.cpp @@ -4,45 +4,46 @@ #include "framework/EventBusManager.h" #include "framework/KafkaManager.h" -#include "framework/utils.h" #include "framework/MicroServiceFuncs.h" +#include "framework/utils.h" namespace OpenWifi { - EventBusManager::EventBusManager(Poco::Logger &L) : - Logger_(L) { - } + EventBusManager::EventBusManager(Poco::Logger &L) : Logger_(L) {} void EventBusManager::run() { Running_ = true; Utils::SetThreadName("fmwk:EventMgr"); auto Msg = MicroServiceMakeSystemEventMessage(KafkaTopics::ServiceEvents::EVENT_JOIN); - KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS,MicroServicePrivateEndPoint(),Msg, false); - while(Running_) { + KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS, MicroServicePrivateEndPoint(), Msg, + false); + while (Running_) { Poco::Thread::trySleep((unsigned long)MicroServiceDaemonBusTimer()); - if(!Running_) + if (!Running_) break; Msg = MicroServiceMakeSystemEventMessage(KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE); - KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS,MicroServicePrivateEndPoint(),Msg, false); + KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS, MicroServicePrivateEndPoint(), + Msg, false); } Msg = MicroServiceMakeSystemEventMessage(KafkaTopics::ServiceEvents::EVENT_LEAVE); - KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS,MicroServicePrivateEndPoint(),Msg, false); + KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS, MicroServicePrivateEndPoint(), Msg, + false); }; void EventBusManager::Start() { - poco_information(Logger(),"Starting..."); - if(KafkaManager()->Enabled()) { + poco_information(Logger(), "Starting..."); + if (KafkaManager()->Enabled()) { Thread_.start(*this); } } void EventBusManager::Stop() { - if(KafkaManager()->Enabled()) { - poco_information(Logger(),"Stopping..."); + if (KafkaManager()->Enabled()) { + poco_information(Logger(), "Stopping..."); Running_ = false; Thread_.wakeUp(); Thread_.join(); - poco_information(Logger(),"Stopped..."); + poco_information(Logger(), "Stopped..."); } } diff --git a/src/framework/EventBusManager.h b/src/framework/EventBusManager.h index 4b57d20..9a7316e 100644 --- a/src/framework/EventBusManager.h +++ b/src/framework/EventBusManager.h @@ -4,8 +4,8 @@ #pragma once -#include "Poco/Runnable.h" #include "Poco/Logger.h" +#include "Poco/Runnable.h" #include "Poco/Thread.h" namespace OpenWifi { @@ -16,13 +16,12 @@ namespace OpenWifi { void run() final; void Start(); void Stop(); - inline Poco::Logger & Logger() { return Logger_; } + inline Poco::Logger &Logger() { return Logger_; } private: - mutable std::atomic_bool Running_ = false; - Poco::Thread Thread_; - Poco::Logger &Logger_; + mutable std::atomic_bool Running_ = false; + Poco::Thread Thread_; + Poco::Logger &Logger_; }; } // namespace OpenWifi - diff --git a/src/framework/KafkaManager.cpp b/src/framework/KafkaManager.cpp index 5733c93..af37a8d 100644 --- a/src/framework/KafkaManager.cpp +++ b/src/framework/KafkaManager.cpp @@ -4,251 +4,260 @@ #include "KafkaManager.h" -#include "framework/MicroServiceFuncs.h" #include "fmt/format.h" +#include "framework/MicroServiceFuncs.h" namespace OpenWifi { - void KafkaLoggerFun([[maybe_unused]] cppkafka::KafkaHandleBase & handle, int level, const std::string & facility, const std::string &message) { - switch ((cppkafka::LogLevel) level) { + void KafkaLoggerFun([[maybe_unused]] cppkafka::KafkaHandleBase &handle, int level, + const std::string &facility, const std::string &message) { + switch ((cppkafka::LogLevel)level) { case cppkafka::LogLevel::LogNotice: { - poco_notice(KafkaManager()->Logger(),fmt::format("kafka-log: facility: {} message: {}",facility, message)); - } - break; + poco_notice(KafkaManager()->Logger(), + fmt::format("kafka-log: facility: {} message: {}", facility, message)); + } break; case cppkafka::LogLevel::LogDebug: { - poco_debug(KafkaManager()->Logger(),fmt::format("kafka-log: facility: {} message: {}",facility, message)); - } - break; + poco_debug(KafkaManager()->Logger(), + fmt::format("kafka-log: facility: {} message: {}", facility, message)); + } break; case cppkafka::LogLevel::LogInfo: { - poco_information(KafkaManager()->Logger(),fmt::format("kafka-log: facility: {} message: {}",facility, message)); - } - break; + poco_information(KafkaManager()->Logger(), + fmt::format("kafka-log: facility: {} message: {}", facility, message)); + } break; case cppkafka::LogLevel::LogWarning: { - poco_warning(KafkaManager()->Logger(), fmt::format("kafka-log: facility: {} message: {}",facility, message)); - } - break; + poco_warning(KafkaManager()->Logger(), + fmt::format("kafka-log: facility: {} message: {}", facility, message)); + } break; case cppkafka::LogLevel::LogAlert: case cppkafka::LogLevel::LogCrit: { - poco_critical(KafkaManager()->Logger(),fmt::format("kafka-log: facility: {} message: {}",facility, message)); - } - break; + poco_critical(KafkaManager()->Logger(), + fmt::format("kafka-log: facility: {} message: {}", facility, message)); + } break; case cppkafka::LogLevel::LogErr: case cppkafka::LogLevel::LogEmerg: default: { - poco_error(KafkaManager()->Logger(),fmt::format("kafka-log: facility: {} message: {}",facility, message)); - } - break; + poco_error(KafkaManager()->Logger(), + fmt::format("kafka-log: facility: {} message: {}", facility, message)); + } break; } } - inline void KafkaErrorFun([[maybe_unused]] cppkafka::KafkaHandleBase & handle, int error, const std::string &reason) { - poco_error(KafkaManager()->Logger(),fmt::format("kafka-error: {}, reason: {}", error, reason)); + inline void KafkaErrorFun([[maybe_unused]] cppkafka::KafkaHandleBase &handle, int error, + const std::string &reason) { + poco_error(KafkaManager()->Logger(), + fmt::format("kafka-error: {}, reason: {}", error, reason)); } - inline void AddKafkaSecurity(cppkafka::Configuration & Config) { - auto CA = MicroServiceConfigGetString("openwifi.kafka.ssl.ca.location",""); - auto Certificate = MicroServiceConfigGetString("openwifi.kafka.ssl.certificate.location",""); - auto Key = MicroServiceConfigGetString("openwifi.kafka.ssl.key.location",""); - auto Password = MicroServiceConfigGetString("openwifi.kafka.ssl.key.password",""); + inline void AddKafkaSecurity(cppkafka::Configuration &Config) { + auto CA = MicroServiceConfigGetString("openwifi.kafka.ssl.ca.location", ""); + auto Certificate = + MicroServiceConfigGetString("openwifi.kafka.ssl.certificate.location", ""); + auto Key = MicroServiceConfigGetString("openwifi.kafka.ssl.key.location", ""); + auto Password = MicroServiceConfigGetString("openwifi.kafka.ssl.key.password", ""); - if(CA.empty() || Certificate.empty() || Key.empty()) + if (CA.empty() || Certificate.empty() || Key.empty()) return; Config.set("ssl.ca.location", CA); Config.set("ssl.certificate.location", Certificate); Config.set("ssl.key.location", Key); - if(!Password.empty()) + if (!Password.empty()) Config.set("ssl.key.password", Password); } - - void KafkaManager::initialize(Poco::Util::Application & self) { + void KafkaManager::initialize(Poco::Util::Application &self) { SubSystemServer::initialize(self); - KafkaEnabled_ = MicroServiceConfigGetBool("openwifi.kafka.enable",false); + KafkaEnabled_ = MicroServiceConfigGetBool("openwifi.kafka.enable", false); } inline void KafkaProducer::run() { - Poco::Logger &Logger_ = Poco::Logger::create("KAFKA-PRODUCER", KafkaManager()->Logger().getChannel()); - poco_information(Logger_,"Starting..."); + Poco::Logger &Logger_ = + Poco::Logger::create("KAFKA-PRODUCER", KafkaManager()->Logger().getChannel()); + poco_information(Logger_, "Starting..."); Utils::SetThreadName("Kafka:Prod"); - cppkafka::Configuration Config({ - { "client.id", MicroServiceConfigGetString("openwifi.kafka.client.id", "") }, - { "metadata.broker.list", MicroServiceConfigGetString("openwifi.kafka.brokerlist", "") } - }); + cppkafka::Configuration Config( + {{"client.id", MicroServiceConfigGetString("openwifi.kafka.client.id", "")}, + {"metadata.broker.list", + MicroServiceConfigGetString("openwifi.kafka.brokerlist", "")}}); AddKafkaSecurity(Config); Config.set_log_callback(KafkaLoggerFun); Config.set_error_callback(KafkaErrorFun); - KafkaManager()->SystemInfoWrapper_ = R"lit({ "system" : { "id" : )lit" + - std::to_string(MicroServiceID()) + - R"lit( , "host" : ")lit" + MicroServicePrivateEndPoint() + - R"lit(" } , "payload" : )lit" ; + KafkaManager()->SystemInfoWrapper_ = + R"lit({ "system" : { "id" : )lit" + std::to_string(MicroServiceID()) + + R"lit( , "host" : ")lit" + MicroServicePrivateEndPoint() + + R"lit(" } , "payload" : )lit"; - cppkafka::Producer Producer(Config); + cppkafka::Producer Producer(Config); Running_ = true; - Poco::AutoPtr Note(Queue_.waitDequeueNotification()); - while(Note && Running_) { + Poco::AutoPtr Note(Queue_.waitDequeueNotification()); + while (Note && Running_) { try { auto Msg = dynamic_cast(Note.get()); if (Msg != nullptr) { - Producer.produce( - cppkafka::MessageBuilder(Msg->Topic()).key(Msg->Key()).payload(Msg->Payload())); + Producer.produce(cppkafka::MessageBuilder(Msg->Topic()) + .key(Msg->Key()) + .payload(Msg->Payload())); } } catch (const cppkafka::HandleException &E) { - poco_warning(Logger_,fmt::format("Caught a Kafka exception (producer): {}", E.what())); - } catch( const Poco::Exception &E) { + poco_warning(Logger_, + fmt::format("Caught a Kafka exception (producer): {}", E.what())); + } catch (const Poco::Exception &E) { Logger_.log(E); } catch (...) { - poco_error(Logger_,"std::exception"); + poco_error(Logger_, "std::exception"); } Note = Queue_.waitDequeueNotification(); } - poco_information(Logger_,"Stopped..."); + poco_information(Logger_, "Stopped..."); } inline void KafkaConsumer::run() { Utils::SetThreadName("Kafka:Cons"); - Poco::Logger &Logger_ = Poco::Logger::create("KAFKA-CONSUMER", KafkaManager()->Logger().getChannel()); + Poco::Logger &Logger_ = + Poco::Logger::create("KAFKA-CONSUMER", KafkaManager()->Logger().getChannel()); - poco_information(Logger_,"Starting..."); + poco_information(Logger_, "Starting..."); - cppkafka::Configuration Config({ - { "client.id", MicroServiceConfigGetString("openwifi.kafka.client.id","") }, - { "metadata.broker.list", MicroServiceConfigGetString("openwifi.kafka.brokerlist","") }, - { "group.id", MicroServiceConfigGetString("openwifi.kafka.group.id","") }, - { "enable.auto.commit", MicroServiceConfigGetBool("openwifi.kafka.auto.commit",false) }, - { "auto.offset.reset", "latest" } , - { "enable.partition.eof", false } - }); + cppkafka::Configuration Config( + {{"client.id", MicroServiceConfigGetString("openwifi.kafka.client.id", "")}, + {"metadata.broker.list", MicroServiceConfigGetString("openwifi.kafka.brokerlist", "")}, + {"group.id", MicroServiceConfigGetString("openwifi.kafka.group.id", "")}, + {"enable.auto.commit", MicroServiceConfigGetBool("openwifi.kafka.auto.commit", false)}, + {"auto.offset.reset", "latest"}, + {"enable.partition.eof", false}}); AddKafkaSecurity(Config); Config.set_log_callback(KafkaLoggerFun); Config.set_error_callback(KafkaErrorFun); - cppkafka::TopicConfiguration topic_config = { - { "auto.offset.reset", "smallest" } - }; + cppkafka::TopicConfiguration topic_config = {{"auto.offset.reset", "smallest"}}; // Now configure it to be the default topic config Config.set_default_topic_configuration(topic_config); cppkafka::Consumer Consumer(Config); - Consumer.set_assignment_callback([&](cppkafka::TopicPartitionList& partitions) { - if(!partitions.empty()) { - poco_information(Logger_,fmt::format("Partition assigned: {}...", - partitions.front().get_partition())); + Consumer.set_assignment_callback([&](cppkafka::TopicPartitionList &partitions) { + if (!partitions.empty()) { + poco_information(Logger_, fmt::format("Partition assigned: {}...", + partitions.front().get_partition())); } }); - Consumer.set_revocation_callback([&](const cppkafka::TopicPartitionList& partitions) { - if(!partitions.empty()) { - poco_information(Logger_,fmt::format("Partition revocation: {}...", - partitions.front().get_partition())); + Consumer.set_revocation_callback([&](const cppkafka::TopicPartitionList &partitions) { + if (!partitions.empty()) { + poco_information(Logger_, fmt::format("Partition revocation: {}...", + partitions.front().get_partition())); } }); - bool AutoCommit = MicroServiceConfigGetBool("openwifi.kafka.auto.commit",false); - auto BatchSize = MicroServiceConfigGetInt("openwifi.kafka.consumer.batchsize",20); + bool AutoCommit = MicroServiceConfigGetBool("openwifi.kafka.auto.commit", false); + auto BatchSize = MicroServiceConfigGetInt("openwifi.kafka.consumer.batchsize", 20); - Types::StringVec Topics; + Types::StringVec Topics; KafkaManager()->Topics(Topics); Consumer.subscribe(Topics); Running_ = true; - while(Running_) { + while (Running_) { try { - std::vector MsgVec = Consumer.poll_batch(BatchSize, std::chrono::milliseconds(100)); - for(auto const &Msg:MsgVec) { + std::vector MsgVec = + Consumer.poll_batch(BatchSize, std::chrono::milliseconds(100)); + for (auto const &Msg : MsgVec) { if (!Msg) continue; if (Msg.get_error()) { if (!Msg.is_eof()) { - poco_error(Logger_,fmt::format("Error: {}", Msg.get_error().to_string())); + poco_error(Logger_, + fmt::format("Error: {}", Msg.get_error().to_string())); } - if(!AutoCommit) + if (!AutoCommit) Consumer.async_commit(Msg); continue; } - KafkaManager()->Dispatch(Msg.get_topic(), Msg.get_key(),Msg.get_payload() ); + KafkaManager()->Dispatch(Msg.get_topic(), Msg.get_key(), Msg.get_payload()); if (!AutoCommit) Consumer.async_commit(Msg); } } catch (const cppkafka::HandleException &E) { - poco_warning(Logger_,fmt::format("Caught a Kafka exception (consumer): {}", E.what())); + poco_warning(Logger_, + fmt::format("Caught a Kafka exception (consumer): {}", E.what())); } catch (const Poco::Exception &E) { Logger_.log(E); } catch (...) { - poco_error(Logger_,"std::exception"); + poco_error(Logger_, "std::exception"); } } Consumer.unsubscribe(); - poco_information(Logger_,"Stopped..."); + poco_information(Logger_, "Stopped..."); } void KafkaProducer::Start() { - if(!Running_) { - Running_=true; + if (!Running_) { + Running_ = true; Worker_.start(*this); } } void KafkaProducer::Stop() { - if(Running_) { - Running_=false; + if (Running_) { + Running_ = false; Queue_.wakeUpAll(); Worker_.join(); } } - void KafkaProducer::Produce(const std::string &Topic, const std::string &Key, const std::string &Payload) { - std::lock_guard G(Mutex_); - Queue_.enqueueNotification( new KafkaMessage(Topic,Key,Payload)); + void KafkaProducer::Produce(const std::string &Topic, const std::string &Key, + const std::string &Payload) { + std::lock_guard G(Mutex_); + Queue_.enqueueNotification(new KafkaMessage(Topic, Key, Payload)); } void KafkaConsumer::Start() { - if(!Running_) { - Running_=true; + if (!Running_) { + Running_ = true; Worker_.start(*this); } } void KafkaConsumer::Stop() { - if(Running_) { - Running_=false; + if (Running_) { + Running_ = false; Worker_.wakeUp(); Worker_.join(); } } void KafkaDispatcher::Start() { - if(!Running_) { - Running_=true; + if (!Running_) { + Running_ = true; Worker_.start(*this); } } void KafkaDispatcher::Stop() { - if(Running_) { - Running_=false; + if (Running_) { + Running_ = false; Queue_.wakeUpAll(); Worker_.join(); } } - auto KafkaDispatcher::RegisterTopicWatcher(const std::string &Topic, Types::TopicNotifyFunction &F) { + auto KafkaDispatcher::RegisterTopicWatcher(const std::string &Topic, + Types::TopicNotifyFunction &F) { std::lock_guard G(Mutex_); auto It = Notifiers_.find(Topic); - if(It == Notifiers_.end()) { + if (It == Notifiers_.end()) { Types::TopicNotifyFunctionList L; - L.emplace(L.end(),std::make_pair(F,FunctionId_)); + L.emplace(L.end(), std::make_pair(F, FunctionId_)); Notifiers_[Topic] = std::move(L); } else { - It->second.emplace(It->second.end(),std::make_pair(F,FunctionId_)); + It->second.emplace(It->second.end(), std::make_pair(F, FunctionId_)); } return FunctionId_++; } @@ -256,110 +265,114 @@ namespace OpenWifi { void KafkaDispatcher::UnregisterTopicWatcher(const std::string &Topic, int Id) { std::lock_guard G(Mutex_); auto It = Notifiers_.find(Topic); - if(It != Notifiers_.end()) { - Types::TopicNotifyFunctionList & L = It->second; - for(auto it=L.begin(); it!=L.end(); it++) - if(it->second == Id) { + if (It != Notifiers_.end()) { + Types::TopicNotifyFunctionList &L = It->second; + for (auto it = L.begin(); it != L.end(); it++) + if (it->second == Id) { L.erase(it); break; } } } - void KafkaDispatcher::Dispatch(const std::string &Topic, const std::string &Key, const std::string &Payload) { - std::lock_guard G(Mutex_); + void KafkaDispatcher::Dispatch(const std::string &Topic, const std::string &Key, + const std::string &Payload) { + std::lock_guard G(Mutex_); auto It = Notifiers_.find(Topic); - if(It!=Notifiers_.end()) { + if (It != Notifiers_.end()) { Queue_.enqueueNotification(new KafkaMessage(Topic, Key, Payload)); } } void KafkaDispatcher::run() { - Poco::Logger &Logger_ = Poco::Logger::create("KAFKA-DISPATCHER", KafkaManager()->Logger().getChannel()); - poco_information(Logger_,"Starting..."); - Poco::AutoPtr Note(Queue_.waitDequeueNotification()); + Poco::Logger &Logger_ = + Poco::Logger::create("KAFKA-DISPATCHER", KafkaManager()->Logger().getChannel()); + poco_information(Logger_, "Starting..."); + Poco::AutoPtr Note(Queue_.waitDequeueNotification()); Utils::SetThreadName("kafka:dispatch"); - while(Note && Running_) { - auto Msg = dynamic_cast(Note.get()); - if(Msg!= nullptr) { + while (Note && Running_) { + auto Msg = dynamic_cast(Note.get()); + if (Msg != nullptr) { auto It = Notifiers_.find(Msg->Topic()); if (It != Notifiers_.end()) { - const auto & FL = It->second; - for(const auto &[CallbackFunc,_]:FL) { + const auto &FL = It->second; + for (const auto &[CallbackFunc, _] : FL) { CallbackFunc(Msg->Key(), Msg->Payload()); } } } Note = Queue_.waitDequeueNotification(); } - poco_information(Logger_,"Stopped..."); + poco_information(Logger_, "Stopped..."); } void KafkaDispatcher::Topics(std::vector &T) { T.clear(); - for(const auto &[TopicName,_]:Notifiers_) + for (const auto &[TopicName, _] : Notifiers_) T.push_back(TopicName); } - int KafkaManager::Start() { - if(!KafkaEnabled_) + if (!KafkaEnabled_) return 0; - ConsumerThr_.Start(); + ConsumerThr_.Start(); ProducerThr_.Start(); Dispatcher_.Start(); return 0; } - void KafkaManager::Stop() { - if(KafkaEnabled_) { - poco_information(Logger(),"Stopping..."); + void KafkaManager::Stop() { + if (KafkaEnabled_) { + poco_information(Logger(), "Stopping..."); Dispatcher_.Stop(); ProducerThr_.Stop(); ConsumerThr_.Stop(); - poco_information(Logger(),"Stopped..."); + poco_information(Logger(), "Stopped..."); return; } } - void KafkaManager::PostMessage(const std::string &topic, const std::string & key, const std::string &PayLoad, bool WrapMessage ) { - if(KafkaEnabled_) { - ProducerThr_.Produce(topic,key,WrapMessage ? WrapSystemId(PayLoad) : PayLoad); + void KafkaManager::PostMessage(const std::string &topic, const std::string &key, + const std::string &PayLoad, bool WrapMessage) { + if (KafkaEnabled_) { + ProducerThr_.Produce(topic, key, WrapMessage ? WrapSystemId(PayLoad) : PayLoad); } } - void KafkaManager::Dispatch(const std::string &Topic, const std::string & Key, const std::string &Payload) { + void KafkaManager::Dispatch(const std::string &Topic, const std::string &Key, + const std::string &Payload) { Dispatcher_.Dispatch(Topic, Key, Payload); } - [[nodiscard]] std::string KafkaManager::WrapSystemId(const std::string & PayLoad) { + [[nodiscard]] std::string KafkaManager::WrapSystemId(const std::string &PayLoad) { return SystemInfoWrapper_ + PayLoad + "}"; } - uint64_t KafkaManager::RegisterTopicWatcher(const std::string &Topic, Types::TopicNotifyFunction &F) { - if(KafkaEnabled_) { - return Dispatcher_.RegisterTopicWatcher(Topic,F); + uint64_t KafkaManager::RegisterTopicWatcher(const std::string &Topic, + Types::TopicNotifyFunction &F) { + if (KafkaEnabled_) { + return Dispatcher_.RegisterTopicWatcher(Topic, F); } else { return 0; } } void KafkaManager::UnregisterTopicWatcher(const std::string &Topic, uint64_t Id) { - if(KafkaEnabled_) { + if (KafkaEnabled_) { Dispatcher_.UnregisterTopicWatcher(Topic, Id); } } - void KafkaManager::Topics(std::vector &T) { - Dispatcher_.Topics(T); + void KafkaManager::Topics(std::vector &T) { Dispatcher_.Topics(T); } + + void KafkaManager::PartitionAssignment(const cppkafka::TopicPartitionList &partitions) { + poco_information( + Logger(), fmt::format("Partition assigned: {}...", partitions.front().get_partition())); } - void KafkaManager::PartitionAssignment(const cppkafka::TopicPartitionList& partitions) { - poco_information(Logger(),fmt::format("Partition assigned: {}...", partitions.front().get_partition())); - } - - void KafkaManager::PartitionRevocation(const cppkafka::TopicPartitionList& partitions) { - poco_information(Logger(),fmt::format("Partition revocation: {}...",partitions.front().get_partition())); + void KafkaManager::PartitionRevocation(const cppkafka::TopicPartitionList &partitions) { + poco_information(Logger(), fmt::format("Partition revocation: {}...", + partitions.front().get_partition())); } } // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/KafkaManager.h b/src/framework/KafkaManager.h index e96f25c..b9630df 100644 --- a/src/framework/KafkaManager.h +++ b/src/framework/KafkaManager.h @@ -7,44 +7,43 @@ #include "Poco/Notification.h" #include "Poco/NotificationQueue.h" -#include "framework/SubSystemServer.h" -#include "framework/OpenWifiTypes.h" -#include "framework/utils.h" #include "framework/KafkaTopics.h" +#include "framework/OpenWifiTypes.h" +#include "framework/SubSystemServer.h" +#include "framework/utils.h" #include "cppkafka/cppkafka.h" namespace OpenWifi { - class KafkaMessage: public Poco::Notification { + class KafkaMessage : public Poco::Notification { public: - KafkaMessage( const std::string &Topic, const std::string &Key, const std::string & Payload) : - Topic_(Topic), Key_(Key), Payload_(Payload) { - } + KafkaMessage(const std::string &Topic, const std::string &Key, const std::string &Payload) + : Topic_(Topic), Key_(Key), Payload_(Payload) {} - inline const std::string & Topic() { return Topic_; } - inline const std::string & Key() { return Key_; } - inline const std::string & Payload() { return Payload_; } + inline const std::string &Topic() { return Topic_; } + inline const std::string &Key() { return Key_; } + inline const std::string &Payload() { return Payload_; } private: - std::string Topic_; - std::string Key_; - std::string Payload_; + std::string Topic_; + std::string Key_; + std::string Payload_; }; class KafkaProducer : public Poco::Runnable { - public: - void run () override; - void Start(); - void Stop(); - void Produce(const std::string &Topic, const std::string &Key, const std::string &Payload); + public: + void run() override; + void Start(); + void Stop(); + void Produce(const std::string &Topic, const std::string &Key, const std::string &Payload); private: - std::recursive_mutex Mutex_; - Poco::Thread Worker_; - mutable std::atomic_bool Running_=false; - Poco::NotificationQueue Queue_; - }; + std::recursive_mutex Mutex_; + Poco::Thread Worker_; + mutable std::atomic_bool Running_ = false; + Poco::NotificationQueue Queue_; + }; class KafkaConsumer : public Poco::Runnable { public: @@ -53,9 +52,9 @@ namespace OpenWifi { void Stop(); private: - std::recursive_mutex Mutex_; - Poco::Thread Worker_; - mutable std::atomic_bool Running_=false; + std::recursive_mutex Mutex_; + Poco::Thread Worker_; + mutable std::atomic_bool Running_ = false; }; class KafkaDispatcher : public Poco::Runnable { @@ -69,21 +68,20 @@ namespace OpenWifi { void Topics(std::vector &T); private: - std::recursive_mutex Mutex_; - Types::NotifyTable Notifiers_; - Poco::Thread Worker_; - mutable std::atomic_bool Running_=false; - uint64_t FunctionId_=1; - Poco::NotificationQueue Queue_; + std::recursive_mutex Mutex_; + Types::NotifyTable Notifiers_; + Poco::Thread Worker_; + mutable std::atomic_bool Running_ = false; + uint64_t FunctionId_ = 1; + Poco::NotificationQueue Queue_; }; class KafkaManager : public SubSystemServer { public: - friend class KafkaConsumer; friend class KafkaProducer; - inline void initialize(Poco::Util::Application & self) override; + inline void initialize(Poco::Util::Application &self) override; static auto instance() { static auto instance_ = new KafkaManager; @@ -93,30 +91,28 @@ namespace OpenWifi { int Start() override; void Stop() override; - void PostMessage(const std::string &topic, const std::string & key, const std::string &PayLoad, bool WrapMessage = true ); - void Dispatch(const std::string &Topic, const std::string & Key, const std::string &Payload); - [[nodiscard]] std::string WrapSystemId(const std::string & PayLoad); + void PostMessage(const std::string &topic, const std::string &key, + const std::string &PayLoad, bool WrapMessage = true); + void Dispatch(const std::string &Topic, const std::string &Key, const std::string &Payload); + [[nodiscard]] std::string WrapSystemId(const std::string &PayLoad); [[nodiscard]] inline bool Enabled() const { return KafkaEnabled_; } uint64_t RegisterTopicWatcher(const std::string &Topic, Types::TopicNotifyFunction &F); void UnregisterTopicWatcher(const std::string &Topic, uint64_t Id); void Topics(std::vector &T); private: - bool KafkaEnabled_ = false; - std::string SystemInfoWrapper_; - KafkaProducer ProducerThr_; - KafkaConsumer ConsumerThr_; - KafkaDispatcher Dispatcher_; + bool KafkaEnabled_ = false; + std::string SystemInfoWrapper_; + KafkaProducer ProducerThr_; + KafkaConsumer ConsumerThr_; + KafkaDispatcher Dispatcher_; - void PartitionAssignment(const cppkafka::TopicPartitionList& partitions); - void PartitionRevocation(const cppkafka::TopicPartitionList& partitions); + void PartitionAssignment(const cppkafka::TopicPartitionList &partitions); + void PartitionRevocation(const cppkafka::TopicPartitionList &partitions); - KafkaManager() noexcept: - SubSystemServer("KafkaManager", "KAFKA-SVR", "openwifi.kafka") { - } + KafkaManager() noexcept : SubSystemServer("KafkaManager", "KAFKA-SVR", "openwifi.kafka") {} }; inline auto KafkaManager() { return KafkaManager::instance(); } } // namespace OpenWifi - diff --git a/src/framework/KafkaTopics.h b/src/framework/KafkaTopics.h index c2b7392..4c93b59 100644 --- a/src/framework/KafkaTopics.h +++ b/src/framework/KafkaTopics.h @@ -19,7 +19,7 @@ namespace OpenWifi::KafkaTopics { static const std::string SERVICE_EVENTS{"service_events"}; static const std::string DEVICE_EVENT_QUEUE{"device_event_queue"}; static const std::string DEVICE_TELEMETRY{"device_telemetry"}; - static const std::string PROVISIONING_CHANGE{"provisioning_change"}; + static const std::string PROVISIONING_CHANGE{"provisioning_change"}; namespace ServiceEvents { static const std::string EVENT_JOIN{"join"}; @@ -36,7 +36,6 @@ namespace OpenWifi::KafkaTopics { static const std::string KEY{"key"}; static const std::string VRSN{"version"}; static const std::string TOKEN{"token"}; - } - } -} - + } // namespace Fields + } // namespace ServiceEvents +} // namespace OpenWifi::KafkaTopics diff --git a/src/framework/MicroService.cpp b/src/framework/MicroService.cpp index 5112e58..31d6837 100644 --- a/src/framework/MicroService.cpp +++ b/src/framework/MicroService.cpp @@ -2,40 +2,39 @@ // Created by stephane bourque on 2022-10-26. // -#include "Poco/FileChannel.h" -#include "Poco/ConsoleChannel.h" -#include "Poco/PatternFormatter.h" -#include "Poco/FormattingChannel.h" #include "Poco/AsyncChannel.h" -#include "Poco/NullChannel.h" -#include "Poco/SplitterChannel.h" -#include "Poco/Net/HTTPStreamFactory.h" -#include "Poco/Net/HTTPSStreamFactory.h" +#include "Poco/ConsoleChannel.h" +#include "Poco/FileChannel.h" +#include "Poco/FormattingChannel.h" +#include "Poco/JSON/JSONException.h" #include "Poco/Net/FTPSStreamFactory.h" #include "Poco/Net/FTPStreamFactory.h" +#include "Poco/Net/HTTPSStreamFactory.h" +#include "Poco/Net/HTTPStreamFactory.h" #include "Poco/Net/SSLManager.h" -#include "Poco/JSON/JSONException.h" +#include "Poco/NullChannel.h" +#include "Poco/PatternFormatter.h" +#include "Poco/SplitterChannel.h" +#include "framework/ALBserver.h" +#include "framework/AuthClient.h" +#include "framework/KafkaManager.h" #include "framework/MicroService.h" #include "framework/MicroServiceErrorHandler.h" -#include "framework/UI_WebSocketClientServer.h" #include "framework/MicroServiceNames.h" -#include "framework/AuthClient.h" -#include "framework/ALBserver.h" -#include "framework/KafkaManager.h" -#include "framework/RESTAPI_GenericServerAccounting.h" #include "framework/RESTAPI_ExtServer.h" +#include "framework/RESTAPI_GenericServerAccounting.h" #include "framework/RESTAPI_IntServer.h" -#include "framework/utils.h" +#include "framework/UI_WebSocketClientServer.h" #include "framework/WebSocketLogger.h" +#include "framework/utils.h" namespace OpenWifi { - void MicroService::Exit(int Reason) { - std::exit(Reason); - } + void MicroService::Exit(int Reason) { std::exit(Reason); } - void MicroService::BusMessageReceived([[maybe_unused]] const std::string &Key, const std::string & Payload) { + void MicroService::BusMessageReceived([[maybe_unused]] const std::string &Key, + const std::string &Payload) { std::lock_guard G(InfraMutex_); try { Poco::JSON::Parser P; @@ -43,66 +42,99 @@ namespace OpenWifi { if (Object->has(KafkaTopics::ServiceEvents::Fields::ID) && Object->has(KafkaTopics::ServiceEvents::Fields::EVENT)) { - uint64_t ID = Object->get(KafkaTopics::ServiceEvents::Fields::ID); - auto Event = Object->get(KafkaTopics::ServiceEvents::Fields::EVENT).toString(); + uint64_t ID = Object->get(KafkaTopics::ServiceEvents::Fields::ID); + auto Event = Object->get(KafkaTopics::ServiceEvents::Fields::EVENT).toString(); if (ID != ID_) { - if( Event==KafkaTopics::ServiceEvents::EVENT_JOIN || - Event==KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE || - Event==KafkaTopics::ServiceEvents::EVENT_LEAVE ) { - if( Object->has(KafkaTopics::ServiceEvents::Fields::TYPE) && + if (Event == KafkaTopics::ServiceEvents::EVENT_JOIN || + Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE || + Event == KafkaTopics::ServiceEvents::EVENT_LEAVE) { + if (Object->has(KafkaTopics::ServiceEvents::Fields::TYPE) && Object->has(KafkaTopics::ServiceEvents::Fields::PUBLIC) && Object->has(KafkaTopics::ServiceEvents::Fields::PRIVATE) && Object->has(KafkaTopics::ServiceEvents::Fields::VRSN) && Object->has(KafkaTopics::ServiceEvents::Fields::KEY)) { - auto PrivateEndPoint = Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE).toString(); - if (Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE && Services_.find(PrivateEndPoint) != Services_.end()) { + auto PrivateEndPoint = + Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE).toString(); + if (Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE && + Services_.find(PrivateEndPoint) != Services_.end()) { Services_[PrivateEndPoint].LastUpdate = Utils::Now(); } else if (Event == KafkaTopics::ServiceEvents::EVENT_LEAVE) { Services_.erase(PrivateEndPoint); - poco_debug(logger(),fmt::format("Service {} ID={} leaving system.",Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE).toString(),ID)); - } else if (Event == KafkaTopics::ServiceEvents::EVENT_JOIN || Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE) { - poco_debug(logger(),fmt::format("Service {} ID={} joining system.",Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE).toString(),ID)); + poco_debug( + logger(), + fmt::format( + "Service {} ID={} leaving system.", + Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) + .toString(), + ID)); + } else if (Event == KafkaTopics::ServiceEvents::EVENT_JOIN || + Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE) { + poco_debug( + logger(), + fmt::format( + "Service {} ID={} joining system.", + Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) + .toString(), + ID)); Services_[PrivateEndPoint] = Types::MicroServiceMeta{ .Id = ID, - .Type = Poco::toLower(Object->get(KafkaTopics::ServiceEvents::Fields::TYPE).toString()), - .PrivateEndPoint = Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE).toString(), - .PublicEndPoint = Object->get(KafkaTopics::ServiceEvents::Fields::PUBLIC).toString(), - .AccessKey = Object->get(KafkaTopics::ServiceEvents::Fields::KEY).toString(), - .Version = Object->get(KafkaTopics::ServiceEvents::Fields::VRSN).toString(), - .LastUpdate = Utils::Now() }; + .Type = Poco::toLower( + Object->get(KafkaTopics::ServiceEvents::Fields::TYPE) + .toString()), + .PrivateEndPoint = + Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE) + .toString(), + .PublicEndPoint = + Object->get(KafkaTopics::ServiceEvents::Fields::PUBLIC) + .toString(), + .AccessKey = + Object->get(KafkaTopics::ServiceEvents::Fields::KEY) + .toString(), + .Version = Object->get(KafkaTopics::ServiceEvents::Fields::VRSN) + .toString(), + .LastUpdate = Utils::Now()}; std::string SvcList; - for (const auto &Svc: Services_) { - if(SvcList.empty()) + for (const auto &Svc : Services_) { + if (SvcList.empty()) SvcList = Svc.second.Type; else SvcList += ", " + Svc.second.Type; } - poco_information(logger(),fmt::format("Current list of microservices: {}", SvcList)); + poco_information( + logger(), + fmt::format("Current list of microservices: {}", SvcList)); } } else { - poco_error(logger(),fmt::format("KAFKA-MSG: invalid event '{}', missing a field.",Event)); + poco_error( + logger(), + fmt::format("KAFKA-MSG: invalid event '{}', missing a field.", + Event)); } - } else if (Event==KafkaTopics::ServiceEvents::EVENT_REMOVE_TOKEN) { - if(Object->has(KafkaTopics::ServiceEvents::Fields::TOKEN)) { - #ifndef TIP_SECURITY_SERVICE - AuthClient()->RemovedCachedToken(Object->get(KafkaTopics::ServiceEvents::Fields::TOKEN).toString()); - #endif + } else if (Event == KafkaTopics::ServiceEvents::EVENT_REMOVE_TOKEN) { + if (Object->has(KafkaTopics::ServiceEvents::Fields::TOKEN)) { +#ifndef TIP_SECURITY_SERVICE + AuthClient()->RemovedCachedToken( + Object->get(KafkaTopics::ServiceEvents::Fields::TOKEN).toString()); +#endif } else { - poco_error(logger(),fmt::format("KAFKA-MSG: invalid event '{}', missing token",Event)); + poco_error( + logger(), + fmt::format("KAFKA-MSG: invalid event '{}', missing token", Event)); } } else { - poco_error(logger(),fmt::format("Unknown Event: {} Source: {}", Event, ID)); + poco_error(logger(), + fmt::format("Unknown Event: {} Source: {}", Event, ID)); } } } else { - poco_error(logger(),"Bad bus message."); + poco_error(logger(), "Bad bus message."); } - auto i=Services_.begin(); + auto i = Services_.begin(); auto now = Utils::Now(); - for(;i!=Services_.end();) { - if((now - i->second.LastUpdate)>60) { + for (; i != Services_.end();) { + if ((now - i->second.LastUpdate) > 60) { i = Services_.erase(i); } else ++i; @@ -113,13 +145,13 @@ namespace OpenWifi { } } - Types::MicroServiceMetaVec MicroService::GetServices(const std::string & Type) { + Types::MicroServiceMetaVec MicroService::GetServices(const std::string &Type) { std::lock_guard G(InfraMutex_); auto T = Poco::toLower(Type); - Types::MicroServiceMetaVec Res; - for(const auto &[_,ServiceRec]:Services_) { - if(ServiceRec.Type==T) + Types::MicroServiceMetaVec Res; + for (const auto &[_, ServiceRec] : Services_) { + if (ServiceRec.Type == T) Res.push_back(ServiceRec); } return Res; @@ -128,23 +160,25 @@ namespace OpenWifi { Types::MicroServiceMetaVec MicroService::GetServices() { std::lock_guard G(InfraMutex_); - Types::MicroServiceMetaVec Res; - for(const auto &[_,ServiceRec]:Services_) { + Types::MicroServiceMetaVec Res; + for (const auto &[_, ServiceRec] : Services_) { Res.push_back(ServiceRec); } return Res; } void MicroService::LoadConfigurationFile() { - std::string Location = Poco::Environment::get(DAEMON_CONFIG_ENV_VAR,"."); - ConfigFileName_ = ConfigFileName_.empty() ? Location + "/" + DAEMON_PROPERTIES_FILENAME : ConfigFileName_; + std::string Location = Poco::Environment::get(DAEMON_CONFIG_ENV_VAR, "."); + ConfigFileName_ = + ConfigFileName_.empty() ? Location + "/" + DAEMON_PROPERTIES_FILENAME : ConfigFileName_; Poco::Path ConfigFile(ConfigFileName_); - if(!ConfigFile.isFile()) - { - std::cerr << DAEMON_APP_NAME << ": Configuration " - << ConfigFile.toString() << " does not seem to exist. Please set " + DAEMON_CONFIG_ENV_VAR - + " env variable the path of the " + DAEMON_PROPERTIES_FILENAME + " file." << std::endl; + if (!ConfigFile.isFile()) { + std::cerr << DAEMON_APP_NAME << ": Configuration " << ConfigFile.toString() + << " does not seem to exist. Please set " + DAEMON_CONFIG_ENV_VAR + + " env variable the path of the " + DAEMON_PROPERTIES_FILENAME + + " file." + << std::endl; std::exit(Poco::Util::Application::EXIT_CONFIG); } @@ -159,11 +193,12 @@ namespace OpenWifi { } void MicroService::LoadMyConfig() { - NoAPISecurity_ = ConfigGetBool("openwifi.security.restapi.disable",false); - std::string KeyFile = ConfigPath("openwifi.service.key",""); - if(!KeyFile.empty()) { + NoAPISecurity_ = ConfigGetBool("openwifi.security.restapi.disable", false); + std::string KeyFile = ConfigPath("openwifi.service.key", ""); + if (!KeyFile.empty()) { std::string KeyFilePassword = ConfigPath("openwifi.service.key.password", ""); - AppKey_ = Poco::SharedPtr(new Poco::Crypto::RSAKey("", KeyFile, KeyFilePassword)); + AppKey_ = Poco::SharedPtr( + new Poco::Crypto::RSAKey("", KeyFile, KeyFilePassword)); Cipher_ = CipherFactory_.createCipher(*AppKey_); Signer_.setRSAKey(AppKey_); Signer_.addAllAlgorithms(); @@ -173,8 +208,8 @@ namespace OpenWifi { } ID_ = Utils::GetSystemId(); - if(!DebugMode_) - DebugMode_ = ConfigGetBool("openwifi.system.debug",false); + if (!DebugMode_) + DebugMode_ = ConfigGetBool("openwifi.system.debug", false); MyPrivateEndPoint_ = ConfigGetString("openwifi.system.uri.private"); MyPublicEndPoint_ = ConfigGetString("openwifi.system.uri.public"); UIURI_ = ConfigGetString("openwifi.system.uri.ui"); @@ -184,151 +219,170 @@ namespace OpenWifi { void MicroService::InitializeLoggingSystem() { static auto initialized = false; - if(!initialized) { + if (!initialized) { initialized = true; LoadConfigurationFile(); - auto LoggingDestination = MicroService::instance().ConfigGetString("logging.type", "file"); - auto LoggingFormat = MicroService::instance().ConfigGetString("logging.format", - "%Y-%m-%d %H:%M:%S.%i %s: [%p][thr:%I] %t"); + auto LoggingDestination = + MicroService::instance().ConfigGetString("logging.type", "file"); + auto LoggingFormat = MicroService::instance().ConfigGetString( + "logging.format", "%Y-%m-%d %H:%M:%S.%i %s: [%p][thr:%I] %t"); auto UseAsyncLogs_ = MicroService::instance().ConfigGetBool("logging.asynch", true); - auto DisableWebSocketLogging = MicroService::instance().ConfigGetBool("logging.websocket",false); + auto DisableWebSocketLogging = + MicroService::instance().ConfigGetBool("logging.websocket", false); if (LoggingDestination == "null") { Poco::AutoPtr DevNull(new Poco::NullChannel); Poco::Logger::root().setChannel(DevNull); } else if (LoggingDestination == "console") { - SetConsoleLogs(UseAsyncLogs_, DisableWebSocketLogging, LoggingFormat); + SetConsoleLogs(UseAsyncLogs_, DisableWebSocketLogging, LoggingFormat); } else if (LoggingDestination == "colorconsole") { - SetColorConsoleLogs(UseAsyncLogs_, DisableWebSocketLogging, LoggingFormat); + SetColorConsoleLogs(UseAsyncLogs_, DisableWebSocketLogging, LoggingFormat); } else if (LoggingDestination == "sql") { - SetSQLLogs(UseAsyncLogs_, DisableWebSocketLogging, LoggingFormat); + SetSQLLogs(UseAsyncLogs_, DisableWebSocketLogging, LoggingFormat); } else if (LoggingDestination == "syslog") { - SetSyslogLogs(UseAsyncLogs_, DisableWebSocketLogging, LoggingFormat); + SetSyslogLogs(UseAsyncLogs_, DisableWebSocketLogging, LoggingFormat); } else { - SetFileLogs(UseAsyncLogs_, DisableWebSocketLogging, LoggingFormat, DAEMON_ROOT_ENV_VAR); - } + SetFileLogs(UseAsyncLogs_, DisableWebSocketLogging, LoggingFormat, + DAEMON_ROOT_ENV_VAR); + } - auto Level = Poco::Logger::parseLevel(MicroService::instance().ConfigGetString("logging.level", "debug")); + auto Level = Poco::Logger::parseLevel( + MicroService::instance().ConfigGetString("logging.level", "debug")); Poco::Logger::root().setLevel(Level); - if(!DisableWebSocketLogging) { + if (!DisableWebSocketLogging) { static const UI_WebSocketClientServer::NotificationTypeIdVec Notifications = { {1, "log"}}; UI_WebSocketClientServer()->RegisterNotifications(Notifications); } } - } + } - void MicroService::SetConsoleLogs(bool UseAsync, bool DisableWebSocketLogging, const std::string & FormatterPattern) { + void MicroService::SetConsoleLogs(bool UseAsync, bool DisableWebSocketLogging, + const std::string &FormatterPattern) { - Poco::AutoPtr Console(new Poco::ConsoleChannel); - Poco::AutoPtr Formatter(new Poco::PatternFormatter); - Formatter->setProperty("pattern", FormatterPattern); - Poco::AutoPtr FormattingChannel(new Poco::FormattingChannel(Formatter, Console)); + Poco::AutoPtr Console(new Poco::ConsoleChannel); + Poco::AutoPtr Formatter(new Poco::PatternFormatter); + Formatter->setProperty("pattern", FormatterPattern); + Poco::AutoPtr FormattingChannel( + new Poco::FormattingChannel(Formatter, Console)); - if(DisableWebSocketLogging) { - if(UseAsync) { - Poco::AutoPtr Async(new Poco::AsyncChannel(FormattingChannel)); - Poco::Logger::root().setChannel(Async); - } else { - Poco::Logger::root().setChannel(FormattingChannel); - } - } else { - Poco::AutoPtr WSLogger(new WebSocketLogger); - Poco::AutoPtr Splitter(new Poco::SplitterChannel); - Splitter->addChannel(WSLogger); - Splitter->addChannel(FormattingChannel); - if(UseAsync) { - Poco::AutoPtr Async(new Poco::AsyncChannel(Splitter)); - Poco::Logger::root().setChannel(Async); - } else { - Poco::Logger::root().setChannel(Splitter); - } - } - Poco::Logger::root().information(fmt::format("Enabled console logs: asynch={} websocket={}",UseAsync,DisableWebSocketLogging)); - } + if (DisableWebSocketLogging) { + if (UseAsync) { + Poco::AutoPtr Async(new Poco::AsyncChannel(FormattingChannel)); + Poco::Logger::root().setChannel(Async); + } else { + Poco::Logger::root().setChannel(FormattingChannel); + } + } else { + Poco::AutoPtr WSLogger(new WebSocketLogger); + Poco::AutoPtr Splitter(new Poco::SplitterChannel); + Splitter->addChannel(WSLogger); + Splitter->addChannel(FormattingChannel); + if (UseAsync) { + Poco::AutoPtr Async(new Poco::AsyncChannel(Splitter)); + Poco::Logger::root().setChannel(Async); + } else { + Poco::Logger::root().setChannel(Splitter); + } + } + Poco::Logger::root().information(fmt::format("Enabled console logs: asynch={} websocket={}", + UseAsync, DisableWebSocketLogging)); + } - void MicroService::SetColorConsoleLogs(bool UseAsync, bool DisableWebSocketLogging, const std::string & FormatterPattern) { + void MicroService::SetColorConsoleLogs(bool UseAsync, bool DisableWebSocketLogging, + const std::string &FormatterPattern) { - Poco::AutoPtr Console(new Poco::ColorConsoleChannel); - Poco::AutoPtr Formatter(new Poco::PatternFormatter); - Formatter->setProperty("pattern", FormatterPattern); - Poco::AutoPtr FormattingChannel(new Poco::FormattingChannel(Formatter, Console)); + Poco::AutoPtr Console(new Poco::ColorConsoleChannel); + Poco::AutoPtr Formatter(new Poco::PatternFormatter); + Formatter->setProperty("pattern", FormatterPattern); + Poco::AutoPtr FormattingChannel( + new Poco::FormattingChannel(Formatter, Console)); - if(DisableWebSocketLogging) { - if(UseAsync) { - Poco::AutoPtr Async(new Poco::AsyncChannel(FormattingChannel)); - Poco::Logger::root().setChannel(Async); - } else { - Poco::Logger::root().setChannel(FormattingChannel); - } - } else { - Poco::AutoPtr WSLogger(new WebSocketLogger); - Poco::AutoPtr Splitter(new Poco::SplitterChannel); - Splitter->addChannel(WSLogger); - Splitter->addChannel(FormattingChannel); - if(UseAsync) { - Poco::AutoPtr Async(new Poco::AsyncChannel(Splitter)); - Poco::Logger::root().setChannel(Async); - } else { - Poco::Logger::root().setChannel(Splitter); - } - } - Poco::Logger::root().information(fmt::format("Enabled color console logs: asynch={} websocket={}",UseAsync,DisableWebSocketLogging)); - } + if (DisableWebSocketLogging) { + if (UseAsync) { + Poco::AutoPtr Async(new Poco::AsyncChannel(FormattingChannel)); + Poco::Logger::root().setChannel(Async); + } else { + Poco::Logger::root().setChannel(FormattingChannel); + } + } else { + Poco::AutoPtr WSLogger(new WebSocketLogger); + Poco::AutoPtr Splitter(new Poco::SplitterChannel); + Splitter->addChannel(WSLogger); + Splitter->addChannel(FormattingChannel); + if (UseAsync) { + Poco::AutoPtr Async(new Poco::AsyncChannel(Splitter)); + Poco::Logger::root().setChannel(Async); + } else { + Poco::Logger::root().setChannel(Splitter); + } + } + Poco::Logger::root().information( + fmt::format("Enabled color console logs: asynch={} websocket={}", UseAsync, + DisableWebSocketLogging)); + } - void MicroService::SetSQLLogs([[maybe_unused]] bool UseAsync,[[maybe_unused]] bool DisableWebSocketLogging,[[maybe_unused]] const std::string & FormatterPattern) { - //"CREATE TABLE T_POCO_LOG (Source VARCHAR, Name VARCHAR, ProcessId INTEGER, Thread VARCHAR, ThreadId INTEGER, Priority INTEGER, Text VARCHAR, DateTime DATE)" - } + void MicroService::SetSQLLogs([[maybe_unused]] bool UseAsync, + [[maybe_unused]] bool DisableWebSocketLogging, + [[maybe_unused]] const std::string &FormatterPattern) { + //"CREATE TABLE T_POCO_LOG (Source VARCHAR, Name VARCHAR, ProcessId INTEGER, Thread VARCHAR, + //ThreadId INTEGER, Priority INTEGER, Text VARCHAR, DateTime DATE)" + } - void MicroService::SetSyslogLogs([[maybe_unused]] bool UseAsync,[[maybe_unused]] bool DisableWebSocketLogging,[[maybe_unused]] const std::string & FormatterPattern) { + void MicroService::SetSyslogLogs([[maybe_unused]] bool UseAsync, + [[maybe_unused]] bool DisableWebSocketLogging, + [[maybe_unused]] const std::string &FormatterPattern) {} - } + void MicroService::SetFileLogs(bool UseAsync, bool DisableWebSocketLogging, + const std::string &FormatterPattern, + const std::string &root_env_var) { + std::string DefaultLogPath = fmt::format("${}/logs", root_env_var); + auto LoggingLocationDir = + MicroService::instance().ConfigPath("logging.path", DefaultLogPath); + Poco::File LD(LoggingLocationDir); + try { + if (!LD.exists()) { + LD.createDirectory(); + } + } catch (const Poco::Exception &E) { + std::cout << "Cannot create " << LD.path() << " Error: " << E.message() << std::endl; + } + auto LoggingLocationDirFilePattern = LoggingLocationDir + "/log"; - void MicroService::SetFileLogs(bool UseAsync, bool DisableWebSocketLogging, const std::string & FormatterPattern, const std::string & root_env_var) { - std::string DefaultLogPath = fmt::format("${}/logs",root_env_var); - auto LoggingLocationDir = MicroService::instance().ConfigPath("logging.path", DefaultLogPath); - Poco::File LD(LoggingLocationDir); - try { - if(!LD.exists()) { - LD.createDirectory(); - } - } catch(const Poco::Exception &E) { - std::cout << "Cannot create " << LD.path() << " Error: " << E.message() << std::endl; - } - auto LoggingLocationDirFilePattern = LoggingLocationDir + "/log"; + Poco::AutoPtr FileChannel(new Poco::FileChannel); + FileChannel->setProperty("rotation", "10 M"); + FileChannel->setProperty("archive", "timestamp"); + FileChannel->setProperty("purgeCount", "10"); + FileChannel->setProperty("path", LoggingLocationDirFilePattern); - Poco::AutoPtr FileChannel(new Poco::FileChannel); - FileChannel->setProperty("rotation", "10 M"); - FileChannel->setProperty("archive", "timestamp"); - FileChannel->setProperty("purgeCount", "10"); - FileChannel->setProperty("path", LoggingLocationDirFilePattern); + Poco::AutoPtr Formatter(new Poco::PatternFormatter); + Formatter->setProperty("pattern", FormatterPattern); + Poco::AutoPtr FormattingChannel( + new Poco::FormattingChannel(Formatter, FileChannel)); - Poco::AutoPtr Formatter(new Poco::PatternFormatter); - Formatter->setProperty("pattern", FormatterPattern); - Poco::AutoPtr FormattingChannel(new Poco::FormattingChannel(Formatter, FileChannel)); - - if(DisableWebSocketLogging) { - if(UseAsync) { - Poco::AutoPtr Async(new Poco::AsyncChannel(FormattingChannel)); - Poco::Logger::root().setChannel(Async); - } else { - Poco::Logger::root().setChannel(FormattingChannel); - } - } else { - Poco::AutoPtr WSLogger(new WebSocketLogger); - Poco::AutoPtr Splitter(new Poco::SplitterChannel); - Splitter->addChannel(WSLogger); - Splitter->addChannel(FormattingChannel); - if(UseAsync) { - Poco::AutoPtr Async(new Poco::AsyncChannel(Splitter)); - Poco::Logger::root().setChannel(Async); - } else { - Poco::Logger::root().setChannel(Splitter); - } - } - Poco::Logger::root().information(fmt::format("Enabled file logs: asynch={} websocket={}",UseAsync,DisableWebSocketLogging)); - } + if (DisableWebSocketLogging) { + if (UseAsync) { + Poco::AutoPtr Async(new Poco::AsyncChannel(FormattingChannel)); + Poco::Logger::root().setChannel(Async); + } else { + Poco::Logger::root().setChannel(FormattingChannel); + } + } else { + Poco::AutoPtr WSLogger(new WebSocketLogger); + Poco::AutoPtr Splitter(new Poco::SplitterChannel); + Splitter->addChannel(WSLogger); + Splitter->addChannel(FormattingChannel); + if (UseAsync) { + Poco::AutoPtr Async(new Poco::AsyncChannel(Splitter)); + Poco::Logger::root().setChannel(Async); + } else { + Poco::Logger::root().setChannel(Splitter); + } + } + Poco::Logger::root().information(fmt::format("Enabled file logs: asynch={} websocket={}", + UseAsync, DisableWebSocketLogging)); + } void DaemonPostInitialization(Poco::Util::Application &self); @@ -341,37 +395,39 @@ namespace OpenWifi { SubSystems_.push_back(ALBHealthCheckServer()); SubSystems_.push_back(RESTAPI_ExtServer()); SubSystems_.push_back(RESTAPI_IntServer()); - #ifndef TIP_SECURITY_SERVICE +#ifndef TIP_SECURITY_SERVICE SubSystems_.push_back(AuthClient()); - #endif +#endif Poco::Net::initializeSSL(); Poco::Net::HTTPStreamFactory::registerFactory(); Poco::Net::HTTPSStreamFactory::registerFactory(); Poco::Net::FTPStreamFactory::registerFactory(); Poco::Net::FTPSStreamFactory::registerFactory(); - Poco::File DataDir(ConfigPath("openwifi.system.data")); + Poco::File DataDir(ConfigPath("openwifi.system.data")); DataDir_ = DataDir.path(); - if(!DataDir.exists()) { + if (!DataDir.exists()) { try { DataDir.createDirectory(); } catch (const Poco::Exception &E) { logger().log(E); } } - WWWAssetsDir_ = ConfigPath("openwifi.restapi.wwwassets",""); - if(WWWAssetsDir_.empty()) + WWWAssetsDir_ = ConfigPath("openwifi.restapi.wwwassets", ""); + if (WWWAssetsDir_.empty()) WWWAssetsDir_ = DataDir_; LoadMyConfig(); - AllowExternalMicroServices_ = ConfigGetBool("allowexternalmicroservices",true); + AllowExternalMicroServices_ = ConfigGetBool("allowexternalmicroservices", true); InitializeSubSystemServers(); ServerApplication::initialize(self); DaemonPostInitialization(self); - Types::TopicNotifyFunction F = [this](const std::string &Key,const std::string &Payload) { this->BusMessageReceived(Key, Payload); }; + Types::TopicNotifyFunction F = [this](const std::string &Key, const std::string &Payload) { + this->BusMessageReceived(Key, Payload); + }; KafkaManager()->RegisterTopicWatcher(KafkaTopics::SERVICE_EVENTS, F); } @@ -392,58 +448,64 @@ namespace OpenWifi { Poco::Util::Option("help", "", "display help information on command line arguments") .required(false) .repeatable(false) - .callback(Poco::Util::OptionCallback(this, &MicroService::handleHelp))); + .callback( + Poco::Util::OptionCallback(this, &MicroService::handleHelp))); - options.addOption( - Poco::Util::Option("file", "", "specify the configuration file") - .required(false) - .repeatable(false) - .argument("file") - .callback(Poco::Util::OptionCallback(this, &MicroService::handleConfig))); + options.addOption(Poco::Util::Option("file", "", "specify the configuration file") + .required(false) + .repeatable(false) + .argument("file") + .callback(Poco::Util::OptionCallback( + this, &MicroService::handleConfig))); - options.addOption( - Poco::Util::Option("debug", "", "to run in debug, set to true") - .required(false) - .repeatable(false) - .callback(Poco::Util::OptionCallback(this, &MicroService::handleDebug))); + options.addOption(Poco::Util::Option("debug", "", "to run in debug, set to true") + .required(false) + .repeatable(false) + .callback(Poco::Util::OptionCallback( + this, &MicroService::handleDebug))); options.addOption( Poco::Util::Option("logs", "", "specify the log directory and file (i.e. dir/file.log)") .required(false) .repeatable(false) .argument("dir") - .callback(Poco::Util::OptionCallback(this, &MicroService::handleLogs))); - - options.addOption( - Poco::Util::Option("version", "", "get the version and quit.") - .required(false) - .repeatable(false) - .callback(Poco::Util::OptionCallback(this, &MicroService::handleVersion))); + .callback( + Poco::Util::OptionCallback(this, &MicroService::handleLogs))); + options.addOption(Poco::Util::Option("version", "", "get the version and quit.") + .required(false) + .repeatable(false) + .callback(Poco::Util::OptionCallback( + this, &MicroService::handleVersion))); } - void MicroService::handleHelp([[maybe_unused]] const std::string &name, [[maybe_unused]] const std::string &value) { + void MicroService::handleHelp([[maybe_unused]] const std::string &name, + [[maybe_unused]] const std::string &value) { HelpRequested_ = true; displayHelp(); stopOptionsProcessing(); } - void MicroService::handleVersion([[maybe_unused]] const std::string &name, [[maybe_unused]] const std::string &value) { + void MicroService::handleVersion([[maybe_unused]] const std::string &name, + [[maybe_unused]] const std::string &value) { HelpRequested_ = true; std::cout << Version() << std::endl; stopOptionsProcessing(); } - void MicroService::handleDebug([[maybe_unused]] const std::string &name, const std::string &value) { - if(value == "true") - DebugMode_ = true ; + void MicroService::handleDebug([[maybe_unused]] const std::string &name, + const std::string &value) { + if (value == "true") + DebugMode_ = true; } - void MicroService::handleLogs([[maybe_unused]] const std::string &name, const std::string &value) { + void MicroService::handleLogs([[maybe_unused]] const std::string &name, + const std::string &value) { LogDir_ = value; } - void MicroService::handleConfig([[maybe_unused]] const std::string &name, const std::string &value) { + void MicroService::handleConfig([[maybe_unused]] const std::string &name, + const std::string &value) { ConfigFileName_ = value; } @@ -456,31 +518,32 @@ namespace OpenWifi { } void MicroService::InitializeSubSystemServers() { - for(auto i:SubSystems_) { + for (auto i : SubSystems_) { addSubsystem(i); } } void MicroService::StartSubSystemServers() { AddActivity("Starting"); - for(auto i:SubSystems_) { + for (auto i : SubSystems_) { i->Start(); } - EventBusManager_ = std::make_unique(Poco::Logger::create("EventBusManager",Poco::Logger::root().getChannel(),Poco::Logger::root().getLevel())); + EventBusManager_ = std::make_unique(Poco::Logger::create( + "EventBusManager", Poco::Logger::root().getChannel(), Poco::Logger::root().getLevel())); EventBusManager_->Start(); } void MicroService::StopSubSystemServers() { AddActivity("Stopping"); EventBusManager_->Stop(); - for(auto i=SubSystems_.rbegin(); i!=SubSystems_.rend(); ++i) { + for (auto i = SubSystems_.rbegin(); i != SubSystems_.rend(); ++i) { (*i)->Stop(); } } [[nodiscard]] std::string MicroService::CreateUUID() { - static std::random_device rd; - static std::mt19937_64 gen(rd()); + static std::random_device rd; + static std::mt19937_64 gen(rd()); static std::uniform_int_distribution<> dis(0, 15); static std::uniform_int_distribution<> dis2(8, 11); @@ -510,7 +573,8 @@ namespace OpenWifi { return ss.str(); } - bool MicroService::SetSubsystemLogLevel(const std::string &SubSystem, const std::string &Level) { + bool MicroService::SetSubsystemLogLevel(const std::string &SubSystem, + const std::string &Level) { try { auto P = Poco::Logger::parseLevel(Level); auto Sub = Poco::toLower(SubSystem); @@ -528,7 +592,7 @@ namespace OpenWifi { } } } - } catch (const Poco::Exception & E) { + } catch (const Poco::Exception &E) { std::cerr << "Exception" << std::endl; } return false; @@ -545,7 +609,7 @@ namespace OpenWifi { Types::StringVec MicroService::GetSubSystems() const { Types::StringVec Result; - for(auto i:SubSystems_) + for (auto i : SubSystems_) Result.push_back(Poco::toLower(i->Name())); return Result; } @@ -553,35 +617,32 @@ namespace OpenWifi { Types::StringPairVec MicroService::GetLogLevels() { Types::StringPairVec Result; - for(auto &i:SubSystems_) { - auto P = std::make_pair( i->Name(), Utils::LogLevelToString(i->GetLoggingLevel())); + for (auto &i : SubSystems_) { + auto P = std::make_pair(i->Name(), Utils::LogLevelToString(i->GetLoggingLevel())); Result.push_back(P); } return Result; } - const Types::StringVec & MicroService::GetLogLevelNames() { - static Types::StringVec LevelNames{"none", "fatal", "critical", "error", "warning", "notice", "information", "debug", "trace" }; + const Types::StringVec &MicroService::GetLogLevelNames() { + static Types::StringVec LevelNames{"none", "fatal", "critical", "error", "warning", + "notice", "information", "debug", "trace"}; return LevelNames; } - uint64_t MicroService::ConfigGetInt(const std::string &Key,uint64_t Default) { - return (uint64_t) config().getInt64(Key,Default); + uint64_t MicroService::ConfigGetInt(const std::string &Key, uint64_t Default) { + return (uint64_t)config().getInt64(Key, Default); } - uint64_t MicroService::ConfigGetInt(const std::string &Key) { - return config().getInt(Key); + uint64_t MicroService::ConfigGetInt(const std::string &Key) { return config().getInt(Key); } + + uint64_t MicroService::ConfigGetBool(const std::string &Key, bool Default) { + return config().getBool(Key, Default); } - uint64_t MicroService::ConfigGetBool(const std::string &Key,bool Default) { - return config().getBool(Key,Default); - } + uint64_t MicroService::ConfigGetBool(const std::string &Key) { return config().getBool(Key); } - uint64_t MicroService::ConfigGetBool(const std::string &Key) { - return config().getBool(Key); - } - - std::string MicroService::ConfigGetString(const std::string &Key,const std::string & Default) { + std::string MicroService::ConfigGetString(const std::string &Key, const std::string &Default) { return config().getString(Key, Default); } @@ -589,7 +650,7 @@ namespace OpenWifi { return config().getString(Key); } - std::string MicroService::ConfigPath(const std::string &Key,const std::string & Default) { + std::string MicroService::ConfigPath(const std::string &Key, const std::string &Default) { std::string R = config().getString(Key, Default); return Poco::Path::expand(R); } @@ -600,28 +661,30 @@ namespace OpenWifi { } std::string MicroService::Encrypt(const std::string &S) { - if(NoBuiltInCrypto_) { + if (NoBuiltInCrypto_) { return S; } - return Cipher_->encryptString(S, Poco::Crypto::Cipher::Cipher::ENC_BASE64);; + return Cipher_->encryptString(S, Poco::Crypto::Cipher::Cipher::ENC_BASE64); + ; } std::string MicroService::Decrypt(const std::string &S) { - if(NoBuiltInCrypto_) { + if (NoBuiltInCrypto_) { return S; } - return Cipher_->decryptString(S, Poco::Crypto::Cipher::Cipher::ENC_BASE64);; + return Cipher_->decryptString(S, Poco::Crypto::Cipher::Cipher::ENC_BASE64); + ; } - std::string MicroService::MakeSystemEventMessage( const std::string & Type ) const { - Poco::JSON::Object Obj; - Obj.set(KafkaTopics::ServiceEvents::Fields::EVENT,Type); - Obj.set(KafkaTopics::ServiceEvents::Fields::ID,ID_); - Obj.set(KafkaTopics::ServiceEvents::Fields::TYPE,Poco::toLower(DAEMON_APP_NAME)); - Obj.set(KafkaTopics::ServiceEvents::Fields::PUBLIC,MyPublicEndPoint_); - Obj.set(KafkaTopics::ServiceEvents::Fields::PRIVATE,MyPrivateEndPoint_); - Obj.set(KafkaTopics::ServiceEvents::Fields::KEY,MyHash_); - Obj.set(KafkaTopics::ServiceEvents::Fields::VRSN,Version_); + std::string MicroService::MakeSystemEventMessage(const std::string &Type) const { + Poco::JSON::Object Obj; + Obj.set(KafkaTopics::ServiceEvents::Fields::EVENT, Type); + Obj.set(KafkaTopics::ServiceEvents::Fields::ID, ID_); + Obj.set(KafkaTopics::ServiceEvents::Fields::TYPE, Poco::toLower(DAEMON_APP_NAME)); + Obj.set(KafkaTopics::ServiceEvents::Fields::PUBLIC, MyPublicEndPoint_); + Obj.set(KafkaTopics::ServiceEvents::Fields::PRIVATE, MyPrivateEndPoint_); + Obj.set(KafkaTopics::ServiceEvents::Fields::KEY, MyHash_); + Obj.set(KafkaTopics::ServiceEvents::Fields::VRSN, Version_); std::stringstream ResultText; Poco::JSON::Stringifier::stringify(Obj, ResultText); return ResultText.str(); @@ -640,75 +703,73 @@ namespace OpenWifi { void MicroService::SavePID() { try { std::ofstream O; - O.open(MicroService::instance().DataDir() + "/pidfile",std::ios::binary | std::ios::trunc); + O.open(MicroService::instance().DataDir() + "/pidfile", + std::ios::binary | std::ios::trunc); O << Poco::Process::id(); O.close(); - } catch (...) - { + } catch (...) { std::cout << "Could not save system ID" << std::endl; } } int MicroService::main([[maybe_unused]] const ArgVec &args) { - MicroServiceErrorHandler ErrorHandler(*this); + MicroServiceErrorHandler ErrorHandler(*this); Poco::ErrorHandler::set(&ErrorHandler); if (!HelpRequested_) { SavePID(); Poco::Logger &logger = Poco::Logger::get(DAEMON_APP_NAME); - logger.notice(fmt::format("Starting {} version {}.",DAEMON_APP_NAME, Version())); + logger.notice(fmt::format("Starting {} version {}.", DAEMON_APP_NAME, Version())); - if(Poco::Net::Socket::supportsIPv6()) - poco_information(logger,"System supports IPv6."); + if (Poco::Net::Socket::supportsIPv6()) + poco_information(logger, "System supports IPv6."); else - poco_information(logger,"System does NOT support IPv6."); + poco_information(logger, "System does NOT support IPv6."); if (config().getBool("application.runAsDaemon", false)) { - poco_information(logger,"Starting as a daemon."); + poco_information(logger, "Starting as a daemon."); } - poco_information(logger,fmt::format("System ID set to {}",ID_)); + poco_information(logger, fmt::format("System ID set to {}", ID_)); StartSubSystemServers(); waitForTerminationRequest(); StopSubSystemServers(); - logger.notice(fmt::format("Stopped {}...",DAEMON_APP_NAME)); + logger.notice(fmt::format("Stopped {}...", DAEMON_APP_NAME)); } return Application::EXIT_OK; } void MicroService::AddActivity(const std::string &Activity) { - if(!DataDir_.empty()) { - std::string ActivityFile{ DataDir_ + "/activity.log"}; + if (!DataDir_.empty()) { + std::string ActivityFile{DataDir_ + "/activity.log"}; try { - std::ofstream of(ActivityFile,std::ios_base::app | std::ios_base::out ); + std::ofstream of(ActivityFile, std::ios_base::app | std::ios_base::out); auto t = std::chrono::system_clock::now(); std::time_t now = std::chrono::system_clock::to_time_t(t); - of << Activity << " at " << std::ctime(&now) ; + of << Activity << " at " << std::ctime(&now); } catch (...) { - } } } [[nodiscard]] std::string MicroService::Sign(Poco::JWT::Token &T, const std::string &Algo) { - if(NoBuiltInCrypto_) { + if (NoBuiltInCrypto_) { return T.toString(); } else { - return Signer_.sign(T,Algo); + return Signer_.sign(T, Algo); } } void MicroService::DeleteOverrideConfiguration() { - Poco::File F(DataDir_ + ExtraConfigurationFilename); + Poco::File F(DataDir_ + ExtraConfigurationFilename); try { - if(F.exists()) + if (F.exists()) F.remove(); } catch (...) { - } } -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/MicroService.h b/src/framework/MicroService.h index cda0aa3..00532b4 100644 --- a/src/framework/MicroService.h +++ b/src/framework/MicroService.h @@ -9,12 +9,11 @@ #pragma once #include -#include -#include -#include -#include #include - +#include +#include +#include +#include // This must be defined for poco_debug and poco_trace macros to function. @@ -23,97 +22,94 @@ #endif namespace OpenWifi { - inline uint64_t Now() { return std::time(nullptr); }; -} + inline uint64_t Now() { return std::time(nullptr); }; +} // namespace OpenWifi -#include "Poco/Util/Application.h" -#include "Poco/Util/ServerApplication.h" -#include "Poco/Util/Option.h" -#include "Poco/Util/OptionSet.h" -#include "Poco/Crypto/RSAKey.h" -#include "Poco/Crypto/CipherFactory.h" +#include "Poco/AutoPtr.h" #include "Poco/Crypto/Cipher.h" +#include "Poco/Crypto/CipherFactory.h" +#include "Poco/Crypto/RSAKey.h" +#include "Poco/Environment.h" +#include "Poco/JSON/Object.h" +#include "Poco/JWT/Signer.h" #include "Poco/Net/HTTPServerRequest.h" #include "Poco/Process.h" +#include "Poco/Util/Application.h" #include "Poco/Util/HelpFormatter.h" -#include "Poco/JSON/Object.h" -#include "Poco/AutoPtr.h" +#include "Poco/Util/Option.h" +#include "Poco/Util/OptionSet.h" #include "Poco/Util/PropertyFileConfiguration.h" -#include "Poco/JWT/Signer.h" -#include "Poco/Environment.h" +#include "Poco/Util/ServerApplication.h" #include "framework/OpenWifiTypes.h" +#include "framework/EventBusManager.h" +#include "framework/SubSystemServer.h" #include "framework/ow_constants.h" #include "framework/utils.h" -#include "framework/SubSystemServer.h" -#include "framework/EventBusManager.h" #include "RESTObjects/RESTAPI_SecurityObjects.h" #include "cppkafka/cppkafka.h" +#include "fmt/core.h" #include "nlohmann/json.hpp" #include "ow_version.h" -#include "fmt/core.h" -#define _OWDEBUG_ std::cout<< __FILE__ <<":" << __LINE__ << std::endl; +#define _OWDEBUG_ std::cout << __FILE__ << ":" << __LINE__ << std::endl; // #define _OWDEBUG_ Logger().debug(Poco::format("%s: %lu",__FILE__,__LINE__)); namespace OpenWifi { class MicroService : public Poco::Util::ServerApplication { public: - explicit MicroService( std::string PropFile, - std::string RootEnv, - std::string ConfigVar, - std::string AppName, - uint64_t BusTimer, - SubSystemVec Subsystems) : - DAEMON_PROPERTIES_FILENAME(std::move(PropFile)), - DAEMON_ROOT_ENV_VAR(std::move(RootEnv)), - DAEMON_CONFIG_ENV_VAR(std::move(ConfigVar)), - DAEMON_APP_NAME(std::move(AppName)), - DAEMON_BUS_TIMER(BusTimer), - SubSystems_(std::move(Subsystems)), - Logger_(Poco::Logger::get("FRAMEWORK")) { - instance_ = this; - RandomEngine_.seed(std::chrono::steady_clock::now().time_since_epoch().count()); + explicit MicroService(std::string PropFile, std::string RootEnv, std::string ConfigVar, + std::string AppName, uint64_t BusTimer, SubSystemVec Subsystems) + : DAEMON_PROPERTIES_FILENAME(std::move(PropFile)), + DAEMON_ROOT_ENV_VAR(std::move(RootEnv)), DAEMON_CONFIG_ENV_VAR(std::move(ConfigVar)), + DAEMON_APP_NAME(std::move(AppName)), DAEMON_BUS_TIMER(BusTimer), + SubSystems_(std::move(Subsystems)), Logger_(Poco::Logger::get("FRAMEWORK")) { + instance_ = this; + RandomEngine_.seed(std::chrono::steady_clock::now().time_since_epoch().count()); // Logger_ = Poco::Logger::root().get("BASE-SVC"); } - inline static const char * ExtraConfigurationFilename = "/configuration_override.json"; + inline static const char *ExtraConfigurationFilename = "/configuration_override.json"; inline void SaveConfig() { PropConfigurationFile_->save(ConfigFileName_); } inline auto UpdateConfig() { return PropConfigurationFile_; } inline bool NoAPISecurity() const { return NoAPISecurity_; } - inline Poco::ThreadPool & TimerPool() { return TimerPool_; } + inline Poco::ThreadPool &TimerPool() { return TimerPool_; } [[nodiscard]] std::string Version() { return Version_; } - [[nodiscard]] inline const std::string & DataDir() { return DataDir_; } - [[nodiscard]] inline const std::string & WWWAssetsDir() { return WWWAssetsDir_; } + [[nodiscard]] inline const std::string &DataDir() { return DataDir_; } + [[nodiscard]] inline const std::string &WWWAssetsDir() { return WWWAssetsDir_; } [[nodiscard]] bool Debug() const { return DebugMode_; } [[nodiscard]] uint64_t ID() const { return ID_; } [[nodiscard]] std::string Hash() const { return MyHash_; }; [[nodiscard]] std::string ServiceType() const { return DAEMON_APP_NAME; }; [[nodiscard]] std::string PrivateEndPoint() const { return MyPrivateEndPoint_; }; [[nodiscard]] std::string PublicEndPoint() const { return MyPublicEndPoint_; }; - [[nodiscard]] const SubSystemVec & GetFullSubSystems() { return SubSystems_; } + [[nodiscard]] const SubSystemVec &GetFullSubSystems() { return SubSystems_; } inline uint64_t DaemonBusTimer() const { return DAEMON_BUS_TIMER; }; - [[nodiscard]] const std::string & AppName() { return DAEMON_APP_NAME; } + [[nodiscard]] const std::string &AppName() { return DAEMON_APP_NAME; } static inline uint64_t GetPID() { return Poco::Process::id(); }; - [[nodiscard]] inline const std::string GetPublicAPIEndPoint() { return MyPublicEndPoint_ + "/api/v1"; }; - [[nodiscard]] inline const std::string & GetUIURI() const { return UIURI_;}; - [[nodiscard]] inline uint64_t Random(uint64_t ceiling) { return (RandomEngine_() % ceiling); } - [[nodiscard]] inline uint64_t Random(uint64_t min, uint64_t max) { - return ((RandomEngine_() % (max-min)) + min); + [[nodiscard]] inline const std::string GetPublicAPIEndPoint() { + return MyPublicEndPoint_ + "/api/v1"; + }; + [[nodiscard]] inline const std::string &GetUIURI() const { return UIURI_; }; + [[nodiscard]] inline uint64_t Random(uint64_t ceiling) { + return (RandomEngine_() % ceiling); } - virtual void GetExtraConfiguration(Poco::JSON::Object & Cfg) { - Cfg.set("additionalConfiguration",false); - } - static MicroService & instance() { return *instance_; } + [[nodiscard]] inline uint64_t Random(uint64_t min, uint64_t max) { + return ((RandomEngine_() % (max - min)) + min); + } + virtual void GetExtraConfiguration(Poco::JSON::Object &Cfg) { + Cfg.set("additionalConfiguration", false); + } + static MicroService &instance() { return *instance_; } - inline void Exit(int Reason); - void BusMessageReceived(const std::string &Key, const std::string & Payload); - Types::MicroServiceMetaVec GetServices(const std::string & Type); + inline void Exit(int Reason); + void BusMessageReceived(const std::string &Key, const std::string &Payload); + Types::MicroServiceMetaVec GetServices(const std::string &Type); Types::MicroServiceMetaVec GetServices(); void LoadConfigurationFile(); void Reload(); @@ -136,71 +132,78 @@ namespace OpenWifi { void Reload(const std::string &Sub); Types::StringVec GetSubSystems() const; Types::StringPairVec GetLogLevels(); - const Types::StringVec & GetLogLevelNames(); - uint64_t ConfigGetInt(const std::string &Key,uint64_t Default); + const Types::StringVec &GetLogLevelNames(); + uint64_t ConfigGetInt(const std::string &Key, uint64_t Default); uint64_t ConfigGetInt(const std::string &Key); - uint64_t ConfigGetBool(const std::string &Key,bool Default); + uint64_t ConfigGetBool(const std::string &Key, bool Default); uint64_t ConfigGetBool(const std::string &Key); - std::string ConfigGetString(const std::string &Key,const std::string & Default); + std::string ConfigGetString(const std::string &Key, const std::string &Default); std::string ConfigGetString(const std::string &Key); - std::string ConfigPath(const std::string &Key,const std::string & Default); + std::string ConfigPath(const std::string &Key, const std::string &Default); std::string ConfigPath(const std::string &Key); std::string Encrypt(const std::string &S); std::string Decrypt(const std::string &S); - std::string MakeSystemEventMessage( const std::string & Type ) const; + std::string MakeSystemEventMessage(const std::string &Type) const; [[nodiscard]] bool IsValidAPIKEY(const Poco::Net::HTTPServerRequest &Request); static void SavePID(); int main(const ArgVec &args) override; - void InitializeLoggingSystem(); + void InitializeLoggingSystem(); void DeleteOverrideConfiguration(); [[nodiscard]] std::string Sign(Poco::JWT::Token &T, const std::string &Algo); void AddActivity(const std::string &Activity); - static void SetConsoleLogs(bool UseAsync, bool AllowWebSocket, const std::string & FormatterPattern); - static void SetColorConsoleLogs(bool UseAsync, bool AllowWebSocket, const std::string & FormatterPattern); - static void SetSQLLogs(bool UseAsync, bool AllowWebSocket, const std::string & FormatterPattern); - static void SetSyslogLogs(bool UseAsync, bool AllowWebSocket, const std::string & FormatterPattern); - static void SetFileLogs(bool UseAsync, bool AllowWebSocket, const std::string & FormatterPattern, const std::string & root_env_var); - inline bool AllowExternalMicroServices() const { return AllowExternalMicroServices_; } + static void SetConsoleLogs(bool UseAsync, bool AllowWebSocket, + const std::string &FormatterPattern); + static void SetColorConsoleLogs(bool UseAsync, bool AllowWebSocket, + const std::string &FormatterPattern); + static void SetSQLLogs(bool UseAsync, bool AllowWebSocket, + const std::string &FormatterPattern); + static void SetSyslogLogs(bool UseAsync, bool AllowWebSocket, + const std::string &FormatterPattern); + static void SetFileLogs(bool UseAsync, bool AllowWebSocket, + const std::string &FormatterPattern, + const std::string &root_env_var); + inline bool AllowExternalMicroServices() const { return AllowExternalMicroServices_; } private: - static MicroService * instance_; - bool HelpRequested_ = false; - std::string LogDir_; - std::string ConfigFileName_; - uint64_t ID_ = 1; - Poco::SharedPtr AppKey_; - bool DebugMode_ = false; - std::string DataDir_; - std::string WWWAssetsDir_; - Poco::Crypto::CipherFactory & CipherFactory_ = Poco::Crypto::CipherFactory::defaultFactory(); - Poco::Crypto::Cipher * Cipher_ = nullptr; - Types::MicroServiceMetaMap Services_; - std::string MyHash_; - std::string MyPrivateEndPoint_; - std::string MyPublicEndPoint_; - std::string UIURI_; - std::string Version_{ OW_VERSION::VERSION + "("+ OW_VERSION::BUILD + ")" + " - " + OW_VERSION::HASH }; - std::recursive_mutex InfraMutex_; - std::default_random_engine RandomEngine_; - Poco::Util::PropertyFileConfiguration * PropConfigurationFile_ = nullptr; - std::string DAEMON_PROPERTIES_FILENAME; - std::string DAEMON_ROOT_ENV_VAR; - std::string DAEMON_CONFIG_ENV_VAR; - std::string DAEMON_APP_NAME; - uint64_t DAEMON_BUS_TIMER; - SubSystemVec SubSystems_; - bool NoAPISecurity_=false; - bool NoBuiltInCrypto_=false; - bool AllowExternalMicroServices_=false; - Poco::JWT::Signer Signer_; - Poco::Logger &Logger_; - Poco::ThreadPool TimerPool_{"timer:pool",2,32}; - std::unique_ptr EventBusManager_; - }; + static MicroService *instance_; + bool HelpRequested_ = false; + std::string LogDir_; + std::string ConfigFileName_; + uint64_t ID_ = 1; + Poco::SharedPtr AppKey_; + bool DebugMode_ = false; + std::string DataDir_; + std::string WWWAssetsDir_; + Poco::Crypto::CipherFactory &CipherFactory_ = Poco::Crypto::CipherFactory::defaultFactory(); + Poco::Crypto::Cipher *Cipher_ = nullptr; + Types::MicroServiceMetaMap Services_; + std::string MyHash_; + std::string MyPrivateEndPoint_; + std::string MyPublicEndPoint_; + std::string UIURI_; + std::string Version_{OW_VERSION::VERSION + "(" + OW_VERSION::BUILD + ")" + " - " + + OW_VERSION::HASH}; + std::recursive_mutex InfraMutex_; + std::default_random_engine RandomEngine_; + Poco::Util::PropertyFileConfiguration *PropConfigurationFile_ = nullptr; + std::string DAEMON_PROPERTIES_FILENAME; + std::string DAEMON_ROOT_ENV_VAR; + std::string DAEMON_CONFIG_ENV_VAR; + std::string DAEMON_APP_NAME; + uint64_t DAEMON_BUS_TIMER; + SubSystemVec SubSystems_; + bool NoAPISecurity_ = false; + bool NoBuiltInCrypto_ = false; + bool AllowExternalMicroServices_ = false; + Poco::JWT::Signer Signer_; + Poco::Logger &Logger_; + Poco::ThreadPool TimerPool_{"timer:pool", 2, 32}; + std::unique_ptr EventBusManager_; + }; - inline MicroService * MicroService::instance_ = nullptr; + inline MicroService *MicroService::instance_ = nullptr; -} +} // namespace OpenWifi diff --git a/src/framework/MicroServiceErrorHandler.h b/src/framework/MicroServiceErrorHandler.h index 03de471..51e4925 100644 --- a/src/framework/MicroServiceErrorHandler.h +++ b/src/framework/MicroServiceErrorHandler.h @@ -4,25 +4,24 @@ #pragma once -#include "fmt/format.h" -#include "Poco/Util/Application.h" #include "Poco/ErrorHandler.h" +#include "Poco/JSON/JSONException.h" +#include "Poco/JSON/Template.h" #include "Poco/Net/NetException.h" #include "Poco/Net/SSLException.h" -#include "Poco/JSON/Template.h" -#include "Poco/JSON/JSONException.h" #include "Poco/Thread.h" +#include "Poco/Util/Application.h" +#include "fmt/format.h" namespace OpenWifi { class MicroServiceErrorHandler : public Poco::ErrorHandler { public: - explicit MicroServiceErrorHandler(Poco::Util::Application &App) : App_(App) { - } + explicit MicroServiceErrorHandler(Poco::Util::Application &App) : App_(App) {} - inline void exception(const Poco::Exception & Base) override { + inline void exception(const Poco::Exception &Base) override { try { - if(Poco::Thread::current()!= nullptr) { + if (Poco::Thread::current() != nullptr) { t_name = Poco::Thread::current()->getName(); t_id = Poco::Thread::current()->id(); } else { @@ -34,179 +33,167 @@ namespace OpenWifi { Base.rethrow(); } catch (const Poco::Net::InvalidCertificateException &E) { - poco_error(App_.logger(), fmt::format("Poco::Net::InvalidCertificateException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), + fmt::format("Poco::Net::InvalidCertificateException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), + E.what())); } catch (const Poco::Net::InvalidSocketException &E) { - poco_error(App_.logger(), fmt::format("Poco::Net::InvalidSocketException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), + fmt::format("Poco::Net::InvalidSocketException thr_name={} thr_id={} " + "code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), + E.what())); } catch (const Poco::Net::WebSocketException &E) { - poco_error(App_.logger(), fmt::format("Poco::Net::WebSocketException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::Net::WebSocketException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::Net::ConnectionResetException &E) { - poco_error(App_.logger(), fmt::format("Poco::Net::ConnectionResetException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), + fmt::format("Poco::Net::ConnectionResetException thr_name={} thr_id={} " + "code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), + E.what())); } catch (const Poco::Net::CertificateValidationException &E) { - poco_error(App_.logger(), fmt::format("Poco::Net::CertificateValidationException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), + fmt::format("Poco::Net::CertificateValidationException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), + E.what())); } catch (const Poco::Net::SSLConnectionUnexpectedlyClosedException &E) { - poco_error(App_.logger(), fmt::format("Poco::Net::SSLConnectionUnexpectedlyClosedException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), + fmt::format("Poco::Net::SSLConnectionUnexpectedlyClosedException " + "thr_name={} thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), + E.what())); } catch (const Poco::Net::SSLContextException &E) { - poco_error(App_.logger(), fmt::format("Poco::Net::SSLContextException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::Net::SSLContextException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::Net::SSLException &E) { - poco_error(App_.logger(), fmt::format("Poco::Net::SSLException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::Net::SSLException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::Net::InvalidAddressException &E) { - poco_error(App_.logger(), fmt::format("Poco::Net::InvalidAddressException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), + fmt::format("Poco::Net::InvalidAddressException thr_name={} thr_id={} " + "code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), + E.what())); } catch (const Poco::Net::NetException &E) { - poco_error(App_.logger(), fmt::format("Poco::Net::NetException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::Net::NetException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::IOException &E) { - poco_error(App_.logger(), fmt::format("Poco::IOException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error( + App_.logger(), + fmt::format( + "Poco::IOException thr_name={} thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), E.what())); } catch (const Poco::TimeoutException &E) { - poco_error(App_.logger(), fmt::format("Poco::TimeoutException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::TimeoutException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::NoThreadAvailableException &E) { - poco_error(App_.logger(), fmt::format("Poco::NoThreadAvailableException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), + fmt::format("Poco::NoThreadAvailableException thr_name={} thr_id={} " + "code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), + E.what())); } catch (const Poco::OutOfMemoryException &E) { - poco_error(App_.logger(), fmt::format("Poco::OutOfMemoryException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::OutOfMemoryException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::BadCastException &E) { - poco_error(App_.logger(), fmt::format("Poco::BadCastException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::BadCastException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::DataException &E) { - poco_error(App_.logger(), fmt::format("Poco::DataException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error( + App_.logger(), + fmt::format( + "Poco::DataException thr_name={} thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), E.what())); } catch (const Poco::PoolOverflowException &E) { - poco_error(App_.logger(), fmt::format("Poco::PoolOverflowException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::PoolOverflowException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::SystemException &E) { - poco_error(App_.logger(), fmt::format("Poco::SystemException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::SystemException thr_name={} thr_id={} " + "code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::RuntimeException &E) { - poco_error(App_.logger(), fmt::format("Poco::RuntimeException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::RuntimeException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::JSON::JSONTemplateException &E) { - poco_error(App_.logger(), fmt::format("Poco::JSON::JSONTemplateException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), + fmt::format("Poco::JSON::JSONTemplateException thr_name={} thr_id={} " + "code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), + E.what())); } catch (const Poco::JSON::JSONException &E) { - poco_error(App_.logger(), fmt::format("Poco::JSON::JSONException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::JSON::JSONException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::ApplicationException &E) { - poco_error(App_.logger(), fmt::format("Poco::ApplicationException thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error(App_.logger(), fmt::format("Poco::ApplicationException thr_name={} " + "thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), + E.message(), E.what())); } catch (const Poco::Exception &E) { - poco_error(App_.logger(), fmt::format("Poco::Exception thr_name={} thr_id={} code={} text={} msg={} what={}", - t_name, t_id, E.code(), - E.displayText(), - E.message(), - E.what())); + poco_error( + App_.logger(), + fmt::format( + "Poco::Exception thr_name={} thr_id={} code={} text={} msg={} what={}", + t_name, t_id, E.code(), E.displayText(), E.message(), E.what())); } catch (...) { - poco_error(App_.logger(), fmt::format("Poco:Generic thr_name={}",t_name, t_id)); + poco_error(App_.logger(), fmt::format("Poco:Generic thr_name={}", t_name, t_id)); } } - inline void exception(const std::exception & E) override { - if(Poco::Thread::current()!= nullptr) { + inline void exception(const std::exception &E) override { + if (Poco::Thread::current() != nullptr) { t_name = Poco::Thread::current()->getName(); t_id = Poco::Thread::current()->id(); } else { t_name = "startup_code"; t_id = 0; } - poco_warning(App_.logger(), fmt::format("std::exception in {}: {} thr_id={}", - t_name,E.what(), - t_id)); + poco_warning(App_.logger(), + fmt::format("std::exception in {}: {} thr_id={}", t_name, E.what(), t_id)); } inline void exception() override { - if(Poco::Thread::current()!= nullptr) { + if (Poco::Thread::current() != nullptr) { t_name = Poco::Thread::current()->getName(); t_id = Poco::Thread::current()->id(); } else { t_name = "startup_code"; t_id = 0; } - poco_warning(App_.logger(), fmt::format("generic exception in {} thr_id={}", - t_name, t_id)); + poco_warning(App_.logger(), + fmt::format("generic exception in {} thr_id={}", t_name, t_id)); } + private: - Poco::Util::Application &App_; - std::string t_name; - int t_id=0; + Poco::Util::Application &App_; + std::string t_name; + int t_id = 0; }; -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/MicroServiceExtra.h b/src/framework/MicroServiceExtra.h index 1e06e2a..75ca53d 100644 --- a/src/framework/MicroServiceExtra.h +++ b/src/framework/MicroServiceExtra.h @@ -4,8 +4,8 @@ #pragma once -#include #include +#include #include "Poco/BasicEvent.h" #include "Poco/ExpireLRUCache.h" @@ -13,46 +13,40 @@ namespace OpenWifi { class ConfigurationEntry { public: - template explicit ConfigurationEntry(T def) : - Default_(def), - Current_(def){ + template explicit ConfigurationEntry(T def) : Default_(def), Current_(def) {} + + template + explicit ConfigurationEntry(T def, T cur, const std::string &Hint = "") + : Default_(def), Current_(cur), Hint_(Hint) {} + + inline ConfigurationEntry() = default; + inline ~ConfigurationEntry() = default; + + template explicit operator T() const { return std::get(Current_); } + inline ConfigurationEntry &operator=(const char *v) { + Current_ = std::string(v); + return *this; + } + template ConfigurationEntry &operator=(T v) { + Current_ = (T)v; + return *this; } - template explicit ConfigurationEntry(T def, T cur, const std::string &Hint="") : - Default_(def), - Current_(cur), - Hint_(Hint){ - } - - inline ConfigurationEntry()=default; - inline ~ConfigurationEntry()=default; - - template explicit operator T () const { return std::get(Current_); } - inline ConfigurationEntry & operator=(const char *v) { Current_ = std::string(v); return *this;} - template ConfigurationEntry & operator=(T v) { Current_ = (T) v; return *this;} - - void reset() { - Current_ = Default_; - } + void reset() { Current_ = Default_; } private: - std::variant Default_, Current_; + std::variant Default_, Current_; std::string Hint_; }; - inline std::string to_string(const ConfigurationEntry &v) { return (std::string) v; } + inline std::string to_string(const ConfigurationEntry &v) { return (std::string)v; } - typedef std::map ConfigurationMap_t; + typedef std::map ConfigurationMap_t; template class FIFO { public: - explicit FIFO(uint32_t Size) : - Size_(Size) { - Buffer_ = new T [Size_]; - } + explicit FIFO(uint32_t Size) : Size_(Size) { Buffer_ = new T[Size_]; } - ~FIFO() { - delete [] Buffer_; - } + ~FIFO() { delete[] Buffer_; } mutable Poco::BasicEvent Writable_; mutable Poco::BasicEvent Readable_; @@ -84,7 +78,7 @@ namespace OpenWifi { Write_ = 0; } Used_++; - MaxEverUsed_ = std::max(Used_,MaxEverUsed_); + MaxEverUsed_ = std::max(Used_, MaxEverUsed_); } bool flag = true; Readable_.notify(this, flag); @@ -93,40 +87,32 @@ namespace OpenWifi { inline bool isFull() { std::lock_guard M(Mutex_); - return Used_==Buffer_->capacity(); + return Used_ == Buffer_->capacity(); } inline auto MaxEverUser() const { return MaxEverUsed_; } private: - std::recursive_mutex Mutex_; - uint32_t Size_=0; - uint32_t Read_=0; - uint32_t Write_=0; - uint32_t Used_=0; - uint32_t MaxEverUsed_=0; - T * Buffer_ = nullptr; + std::recursive_mutex Mutex_; + uint32_t Size_ = 0; + uint32_t Read_ = 0; + uint32_t Write_ = 0; + uint32_t Used_ = 0; + uint32_t MaxEverUsed_ = 0; + T *Buffer_ = nullptr; }; - template class RecordCache { + template + class RecordCache { public: - explicit RecordCache( KeyType Record::* Q) : - MemberOffset(Q){ - }; - inline auto update(const Record &R) { - return Cache_.update(R.*MemberOffset, R); - } - inline auto get(const KeyType &K) { - return Cache_.get(K); - } - inline auto remove(const KeyType &K) { - return Cache_.remove(K); - } - inline auto remove(const Record &R) { - return Cache_.remove(R.*MemberOffset); - } + explicit RecordCache(KeyType Record::*Q) : MemberOffset(Q){}; + inline auto update(const Record &R) { return Cache_.update(R.*MemberOffset, R); } + inline auto get(const KeyType &K) { return Cache_.get(K); } + inline auto remove(const KeyType &K) { return Cache_.remove(K); } + inline auto remove(const Record &R) { return Cache_.remove(R.*MemberOffset); } + private: - KeyType Record::* MemberOffset; - Poco::ExpireLRUCache Cache_{Size,Expiry}; + KeyType Record::*MemberOffset; + Poco::ExpireLRUCache Cache_{Size, Expiry}; }; -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/MicroServiceFuncs.cpp b/src/framework/MicroServiceFuncs.cpp index 193d119..22eccbd 100644 --- a/src/framework/MicroServiceFuncs.cpp +++ b/src/framework/MicroServiceFuncs.cpp @@ -2,125 +2,125 @@ // Created by stephane bourque on 2022-10-25. // -#include "framework/MicroService.h" #include "framework/MicroServiceFuncs.h" +#include "framework/MicroService.h" namespace OpenWifi { - const std::string &MicroServiceDataDirectory() { return MicroService::instance().DataDir(); } + const std::string &MicroServiceDataDirectory() { return MicroService::instance().DataDir(); } - Types::MicroServiceMetaVec MicroServiceGetServices(const std::string &Type) { - return MicroService::instance().GetServices(Type); - } + Types::MicroServiceMetaVec MicroServiceGetServices(const std::string &Type) { + return MicroService::instance().GetServices(Type); + } - Types::MicroServiceMetaVec MicroServiceGetServices() { - return MicroService::instance().GetServices(); - } + Types::MicroServiceMetaVec MicroServiceGetServices() { + return MicroService::instance().GetServices(); + } - std::string MicroServicePublicEndPoint() { return MicroService::instance().PublicEndPoint(); } + std::string MicroServicePublicEndPoint() { return MicroService::instance().PublicEndPoint(); } - std::string MicroServiceConfigGetString(const std::string &Key, const std::string &DefaultValue) { - return MicroService::instance().ConfigGetString(Key, DefaultValue); - } + std::string MicroServiceConfigGetString(const std::string &Key, + const std::string &DefaultValue) { + return MicroService::instance().ConfigGetString(Key, DefaultValue); + } - bool MicroServiceConfigGetBool(const std::string &Key, bool DefaultValue) { - return MicroService::instance().ConfigGetBool(Key, DefaultValue); - } + bool MicroServiceConfigGetBool(const std::string &Key, bool DefaultValue) { + return MicroService::instance().ConfigGetBool(Key, DefaultValue); + } - std::uint64_t MicroServiceConfigGetInt(const std::string &Key, std::uint64_t DefaultValue) { - return MicroService::instance().ConfigGetInt(Key, DefaultValue); - } + std::uint64_t MicroServiceConfigGetInt(const std::string &Key, std::uint64_t DefaultValue) { + return MicroService::instance().ConfigGetInt(Key, DefaultValue); + } - std::string MicroServicePrivateEndPoint() { return MicroService::instance().PrivateEndPoint(); } + std::string MicroServicePrivateEndPoint() { return MicroService::instance().PrivateEndPoint(); } - std::uint64_t MicroServiceID() { return MicroService::instance().ID(); } + std::uint64_t MicroServiceID() { return MicroService::instance().ID(); } - bool MicroServiceIsValidAPIKEY(const Poco::Net::HTTPServerRequest &Request) { - return MicroService::instance().IsValidAPIKEY(Request); - } + bool MicroServiceIsValidAPIKEY(const Poco::Net::HTTPServerRequest &Request) { + return MicroService::instance().IsValidAPIKEY(Request); + } - bool MicroServiceNoAPISecurity() { return MicroService::instance().NoAPISecurity(); } + bool MicroServiceNoAPISecurity() { return MicroService::instance().NoAPISecurity(); } - void MicroServiceLoadConfigurationFile() { MicroService::instance().LoadConfigurationFile(); } + void MicroServiceLoadConfigurationFile() { MicroService::instance().LoadConfigurationFile(); } - void MicroServiceReload() { MicroService::instance().Reload(); } + void MicroServiceReload() { MicroService::instance().Reload(); } - void MicroServiceReload(const std::string &Type) { MicroService::instance().Reload(Type); } + void MicroServiceReload(const std::string &Type) { MicroService::instance().Reload(Type); } - const Types::StringVec MicroServiceGetLogLevelNames() { - return MicroService::instance().GetLogLevelNames(); - } + const Types::StringVec MicroServiceGetLogLevelNames() { + return MicroService::instance().GetLogLevelNames(); + } - const Types::StringVec MicroServiceGetSubSystems() { - return MicroService::instance().GetSubSystems(); - } + const Types::StringVec MicroServiceGetSubSystems() { + return MicroService::instance().GetSubSystems(); + } - Types::StringPairVec MicroServiceGetLogLevels() { return MicroService::instance().GetLogLevels(); } + Types::StringPairVec MicroServiceGetLogLevels() { + return MicroService::instance().GetLogLevels(); + } - bool MicroServiceSetSubsystemLogLevel(const std::string &SubSystem, const std::string &Level) { - return MicroService::instance().SetSubsystemLogLevel(SubSystem, Level); - } + bool MicroServiceSetSubsystemLogLevel(const std::string &SubSystem, const std::string &Level) { + return MicroService::instance().SetSubsystemLogLevel(SubSystem, Level); + } - void MicroServiceGetExtraConfiguration(Poco::JSON::Object &Answer) { - MicroService::instance().GetExtraConfiguration(Answer); - } + void MicroServiceGetExtraConfiguration(Poco::JSON::Object &Answer) { + MicroService::instance().GetExtraConfiguration(Answer); + } - std::string MicroServiceVersion() { return MicroService::instance().Version(); } + std::string MicroServiceVersion() { return MicroService::instance().Version(); } - std::uint64_t MicroServiceUptimeTotalSeconds() { - return MicroService::instance().uptime().totalSeconds(); - } + std::uint64_t MicroServiceUptimeTotalSeconds() { + return MicroService::instance().uptime().totalSeconds(); + } - std::uint64_t MicroServiceStartTimeEpochTime() { - return MicroService::instance().startTime().epochTime(); - } + std::uint64_t MicroServiceStartTimeEpochTime() { + return MicroService::instance().startTime().epochTime(); + } - std::string MicroServiceGetUIURI() { return MicroService::instance().GetUIURI(); } + std::string MicroServiceGetUIURI() { return MicroService::instance().GetUIURI(); } - const SubSystemVec MicroServiceGetFullSubSystems() { - return MicroService::instance().GetFullSubSystems(); - } + const SubSystemVec MicroServiceGetFullSubSystems() { + return MicroService::instance().GetFullSubSystems(); + } - std::string MicroServiceCreateUUID() { return MicroService::CreateUUID(); } + std::string MicroServiceCreateUUID() { return MicroService::CreateUUID(); } - std::uint64_t MicroServiceDaemonBusTimer() { return MicroService::instance().DaemonBusTimer(); } + std::uint64_t MicroServiceDaemonBusTimer() { return MicroService::instance().DaemonBusTimer(); } - std::string MicroServiceMakeSystemEventMessage(const std::string &Type) { - return MicroService::instance().MakeSystemEventMessage(Type); - } + std::string MicroServiceMakeSystemEventMessage(const std::string &Type) { + return MicroService::instance().MakeSystemEventMessage(Type); + } - Poco::ThreadPool &MicroServiceTimerPool() { return MicroService::instance().TimerPool(); } + Poco::ThreadPool &MicroServiceTimerPool() { return MicroService::instance().TimerPool(); } - std::string MicroServiceConfigPath(const std::string &Key, - const std::string &DefaultValue) { - return MicroService::instance().ConfigPath(Key, DefaultValue); - } + std::string MicroServiceConfigPath(const std::string &Key, const std::string &DefaultValue) { + return MicroService::instance().ConfigPath(Key, DefaultValue); + } - std::string MicroServiceWWWAssetsDir() { - return MicroService::instance().WWWAssetsDir(); - } + std::string MicroServiceWWWAssetsDir() { return MicroService::instance().WWWAssetsDir(); } - std::uint64_t MicroServiceRandom(std::uint64_t Start,std::uint64_t End) { - return MicroService::instance().Random(Start, End); - } + std::uint64_t MicroServiceRandom(std::uint64_t Start, std::uint64_t End) { + return MicroService::instance().Random(Start, End); + } - std::uint64_t MicroServiceRandom(std::uint64_t Range) { - return MicroService::instance().Random(Range); - } + std::uint64_t MicroServiceRandom(std::uint64_t Range) { + return MicroService::instance().Random(Range); + } - std::string MicroServiceSign(Poco::JWT::Token &T, const std::string &Algo) { - return MicroService::instance().Sign(T, Algo); - } + std::string MicroServiceSign(Poco::JWT::Token &T, const std::string &Algo) { + return MicroService::instance().Sign(T, Algo); + } - std::string MicroServiceGetPublicAPIEndPoint() { - return MicroService::instance().GetPublicAPIEndPoint(); - } + std::string MicroServiceGetPublicAPIEndPoint() { + return MicroService::instance().GetPublicAPIEndPoint(); + } void MicroServiceDeleteOverrideConfiguration() { return MicroService::instance().DeleteOverrideConfiguration(); } - bool AllowExternalMicroServices() { - return MicroService::instance().AllowExternalMicroServices(); - } + bool AllowExternalMicroServices() { + return MicroService::instance().AllowExternalMicroServices(); + } -} +} // namespace OpenWifi diff --git a/src/framework/MicroServiceFuncs.h b/src/framework/MicroServiceFuncs.h index aef4d50..ec06c7a 100644 --- a/src/framework/MicroServiceFuncs.h +++ b/src/framework/MicroServiceFuncs.h @@ -8,20 +8,20 @@ #include "framework/OpenWifiTypes.h" -#include "Poco/Net/HTTPServerRequest.h" #include "Poco/JSON/Object.h" -#include "Poco/ThreadPool.h" #include "Poco/JWT/Token.h" - +#include "Poco/Net/HTTPServerRequest.h" +#include "Poco/ThreadPool.h" namespace OpenWifi { class SubSystemServer; - using SubSystemVec=std::vector; - const std::string & MicroServiceDataDirectory(); - Types::MicroServiceMetaVec MicroServiceGetServices(const std::string & Type); - Types::MicroServiceMetaVec MicroServiceGetServices(); + using SubSystemVec = std::vector; + const std::string &MicroServiceDataDirectory(); + Types::MicroServiceMetaVec MicroServiceGetServices(const std::string &Type); + Types::MicroServiceMetaVec MicroServiceGetServices(); std::string MicroServicePublicEndPoint(); - std::string MicroServiceConfigGetString(const std::string &Key, const std::string &DefaultValue); + std::string MicroServiceConfigGetString(const std::string &Key, + const std::string &DefaultValue); bool MicroServiceConfigGetBool(const std::string &Key, bool DefaultValue); std::uint64_t MicroServiceConfigGetInt(const std::string &Key, std::uint64_t DefaultValue); std::string MicroServicePrivateEndPoint(); @@ -43,15 +43,14 @@ namespace OpenWifi { const SubSystemVec MicroServiceGetFullSubSystems(); std::string MicroServiceCreateUUID(); std::uint64_t MicroServiceDaemonBusTimer(); - std::string MicroServiceMakeSystemEventMessage( const std::string & Type ); - Poco::ThreadPool & MicroServiceTimerPool(); - std::string MicroServiceConfigPath(const std::string &Key, - const std::string &DefaultValue); - std::string MicroServiceWWWAssetsDir(); - std::uint64_t MicroServiceRandom(std::uint64_t Start,std::uint64_t End); - std::uint64_t MicroServiceRandom(std::uint64_t Range); - std::string MicroServiceSign(Poco::JWT::Token &T, const std::string &Algo); - std::string MicroServiceGetPublicAPIEndPoint(); + std::string MicroServiceMakeSystemEventMessage(const std::string &Type); + Poco::ThreadPool &MicroServiceTimerPool(); + std::string MicroServiceConfigPath(const std::string &Key, const std::string &DefaultValue); + std::string MicroServiceWWWAssetsDir(); + std::uint64_t MicroServiceRandom(std::uint64_t Start, std::uint64_t End); + std::uint64_t MicroServiceRandom(std::uint64_t Range); + std::string MicroServiceSign(Poco::JWT::Token &T, const std::string &Algo); + std::string MicroServiceGetPublicAPIEndPoint(); void MicroServiceDeleteOverrideConfiguration(); - bool AllowExternalMicroServices(); -} + bool AllowExternalMicroServices(); +} // namespace OpenWifi diff --git a/src/framework/MicroServiceNames.h b/src/framework/MicroServiceNames.h index 7563e19..430d9d3 100644 --- a/src/framework/MicroServiceNames.h +++ b/src/framework/MicroServiceNames.h @@ -10,13 +10,13 @@ namespace OpenWifi { static const std::string uSERVICE_SECURITY{"owsec"}; static const std::string uSERVICE_GATEWAY{"owgw"}; - static const std::string uSERVICE_FIRMWARE{ "owfms"}; - static const std::string uSERVICE_TOPOLOGY{ "owtopo"}; - static const std::string uSERVICE_PROVISIONING{ "owprov"}; - static const std::string uSERVICE_OWLS{ "owls"}; - static const std::string uSERVICE_SUBCRIBER{ "owsub"}; - static const std::string uSERVICE_INSTALLER{ "owinst"}; - static const std::string uSERVICE_ANALYTICS{ "owanalytics"}; - static const std::string uSERVICE_OWRRM{ "owrrm"}; + static const std::string uSERVICE_FIRMWARE{"owfms"}; + static const std::string uSERVICE_TOPOLOGY{"owtopo"}; + static const std::string uSERVICE_PROVISIONING{"owprov"}; + static const std::string uSERVICE_OWLS{"owls"}; + static const std::string uSERVICE_SUBCRIBER{"owsub"}; + static const std::string uSERVICE_INSTALLER{"owinst"}; + static const std::string uSERVICE_ANALYTICS{"owanalytics"}; + static const std::string uSERVICE_OWRRM{"owrrm"}; -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/OpenAPIRequests.cpp b/src/framework/OpenAPIRequests.cpp index fe157f3..c5e4aad 100644 --- a/src/framework/OpenAPIRequests.cpp +++ b/src/framework/OpenAPIRequests.cpp @@ -4,286 +4,281 @@ #include "OpenAPIRequests.h" +#include "Poco/JSON/Parser.h" #include "Poco/Logger.h" -#include "Poco/URI.h" #include "Poco/Net/HTTPRequest.h" #include "Poco/Net/HTTPSClientSession.h" -#include "Poco/JSON/Parser.h" +#include "Poco/URI.h" #include "fmt/format.h" #include "framework/MicroServiceFuncs.h" namespace OpenWifi { - Poco::Net::HTTPServerResponse::HTTPStatus OpenAPIRequestGet::Do(Poco::JSON::Object::Ptr &ResponseObject, const std::string & BearerToken) { - try { + Poco::Net::HTTPServerResponse::HTTPStatus + OpenAPIRequestGet::Do(Poco::JSON::Object::Ptr &ResponseObject, const std::string &BearerToken) { + try { - auto Services = MicroServiceGetServices(Type_); - for(auto const &Svc:Services) { - Poco::URI URI(Svc.PrivateEndPoint); + auto Services = MicroServiceGetServices(Type_); + for (auto const &Svc : Services) { + Poco::URI URI(Svc.PrivateEndPoint); - auto Secure = (URI.getScheme() == "https"); + auto Secure = (URI.getScheme() == "https"); - URI.setPath(EndPoint_); - for (const auto &qp : QueryData_) - URI.addQueryParameter(qp.first, qp.second); + URI.setPath(EndPoint_); + for (const auto &qp : QueryData_) + URI.addQueryParameter(qp.first, qp.second); - std::string Path(URI.getPathAndQuery()); - Poco::Net::HTTPRequest Request(Poco::Net::HTTPRequest::HTTP_GET, - Path, - Poco::Net::HTTPMessage::HTTP_1_1); + std::string Path(URI.getPathAndQuery()); + Poco::Net::HTTPRequest Request(Poco::Net::HTTPRequest::HTTP_GET, Path, + Poco::Net::HTTPMessage::HTTP_1_1); - poco_debug(Poco::Logger::get("REST-CALLER-GET"), fmt::format(" {}", LoggingStr_.empty() ? URI.toString() : LoggingStr_ ) ); + poco_debug(Poco::Logger::get("REST-CALLER-GET"), + fmt::format(" {}", LoggingStr_.empty() ? URI.toString() : LoggingStr_)); - if(BearerToken.empty()) { - Request.add("X-API-KEY", Svc.AccessKey); - Request.add("X-INTERNAL-NAME", MicroServicePublicEndPoint()); - } else { - // Authorization: Bearer ${token} - Request.add("Authorization", "Bearer " + BearerToken); - } - - if(Secure) { - Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort()); - Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); - - Session.sendRequest(Request); - - Poco::Net::HTTPResponse Response; - std::istream &is = Session.receiveResponse(Response); - if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { - Poco::JSON::Parser P; - ResponseObject = P.parse(is).extract(); - } - return Response.getStatus(); - } else { - Poco::Net::HTTPClientSession Session(URI.getHost(), URI.getPort()); - Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); - - Session.sendRequest(Request); - - Poco::Net::HTTPResponse Response; - std::istream &is = Session.receiveResponse(Response); - if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { - Poco::JSON::Parser P; - ResponseObject = P.parse(is).extract(); - } - return Response.getStatus(); - } - } - } - catch (const Poco::Exception &E) - { - Poco::Logger::get("REST-CALLER-GET").log(E); - } - return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT; -} - - Poco::Net::HTTPServerResponse::HTTPStatus OpenAPIRequestPut::Do(Poco::JSON::Object::Ptr &ResponseObject, const std::string & BearerToken) { - try { - auto Services = MicroServiceGetServices(Type_); - for(auto const &Svc:Services) { - Poco::URI URI(Svc.PrivateEndPoint); - - auto Secure = (URI.getScheme() == "https"); - - URI.setPath(EndPoint_); - for (const auto &qp : QueryData_) - URI.addQueryParameter(qp.first, qp.second); - - poco_debug(Poco::Logger::get("REST-CALLER-PUT"), fmt::format(" {}", LoggingStr_.empty() ? URI.toString() : LoggingStr_ ) ); - - std::string Path(URI.getPathAndQuery()); - - Poco::Net::HTTPRequest Request(Poco::Net::HTTPRequest::HTTP_PUT, - Path, - Poco::Net::HTTPMessage::HTTP_1_1); - std::ostringstream obody; - Poco::JSON::Stringifier::stringify(Body_,obody); - - Request.setContentType("application/json"); - Request.setContentLength(obody.str().size()); - - if(BearerToken.empty()) { - Request.add("X-API-KEY", Svc.AccessKey); - Request.add("X-INTERNAL-NAME", MicroServicePublicEndPoint()); - } else { - // Authorization: Bearer ${token} - Request.add("Authorization", "Bearer " + BearerToken); - } - - if(Secure) { - Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort()); - Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); - - std::ostream &os = Session.sendRequest(Request); - os << obody.str(); - - Poco::Net::HTTPResponse Response; - std::istream &is = Session.receiveResponse(Response); - if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { - Poco::JSON::Parser P; - ResponseObject = P.parse(is).extract(); + if (BearerToken.empty()) { + Request.add("X-API-KEY", Svc.AccessKey); + Request.add("X-INTERNAL-NAME", MicroServicePublicEndPoint()); } else { - Poco::JSON::Parser P; - ResponseObject = P.parse(is).extract(); + // Authorization: Bearer ${token} + Request.add("Authorization", "Bearer " + BearerToken); } - return Response.getStatus(); - } else { - Poco::Net::HTTPClientSession Session(URI.getHost(), URI.getPort()); - Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); - std::ostream &os = Session.sendRequest(Request); - os << obody.str(); + if (Secure) { + Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort()); + Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); - Poco::Net::HTTPResponse Response; - std::istream &is = Session.receiveResponse(Response); - if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { - Poco::JSON::Parser P; - ResponseObject = P.parse(is).extract(); + Session.sendRequest(Request); + + Poco::Net::HTTPResponse Response; + std::istream &is = Session.receiveResponse(Response); + if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { + Poco::JSON::Parser P; + ResponseObject = P.parse(is).extract(); + } + return Response.getStatus(); } else { - Poco::JSON::Parser P; - ResponseObject = P.parse(is).extract(); + Poco::Net::HTTPClientSession Session(URI.getHost(), URI.getPort()); + Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); + + Session.sendRequest(Request); + + Poco::Net::HTTPResponse Response; + std::istream &is = Session.receiveResponse(Response); + if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { + Poco::JSON::Parser P; + ResponseObject = P.parse(is).extract(); + } + return Response.getStatus(); } - return Response.getStatus(); } + } catch (const Poco::Exception &E) { + Poco::Logger::get("REST-CALLER-GET").log(E); } + return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT; } - catch (const Poco::Exception &E) - { - Poco::Logger::get("REST-CALLER-PUT").log(E); - } - return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT; -} - Poco::Net::HTTPServerResponse::HTTPStatus OpenAPIRequestPost::Do(Poco::JSON::Object::Ptr &ResponseObject, const std::string & BearerToken) { - try { - auto Services = MicroServiceGetServices(Type_); + Poco::Net::HTTPServerResponse::HTTPStatus + OpenAPIRequestPut::Do(Poco::JSON::Object::Ptr &ResponseObject, const std::string &BearerToken) { + try { + auto Services = MicroServiceGetServices(Type_); + for (auto const &Svc : Services) { + Poco::URI URI(Svc.PrivateEndPoint); - for(auto const &Svc:Services) { - Poco::URI URI(Svc.PrivateEndPoint); + auto Secure = (URI.getScheme() == "https"); + URI.setPath(EndPoint_); + for (const auto &qp : QueryData_) + URI.addQueryParameter(qp.first, qp.second); - auto Secure = (URI.getScheme() == "https"); + poco_debug(Poco::Logger::get("REST-CALLER-PUT"), + fmt::format(" {}", LoggingStr_.empty() ? URI.toString() : LoggingStr_)); - URI.setPath(EndPoint_); - for (const auto &qp : QueryData_) - URI.addQueryParameter(qp.first, qp.second); + std::string Path(URI.getPathAndQuery()); - poco_debug(Poco::Logger::get("REST-CALLER-POST"),fmt::format(" {}", LoggingStr_.empty() ? URI.toString() : LoggingStr_ ) ); + Poco::Net::HTTPRequest Request(Poco::Net::HTTPRequest::HTTP_PUT, Path, + Poco::Net::HTTPMessage::HTTP_1_1); + std::ostringstream obody; + Poco::JSON::Stringifier::stringify(Body_, obody); - std::string Path(URI.getPathAndQuery()); + Request.setContentType("application/json"); + Request.setContentLength(obody.str().size()); - Poco::Net::HTTPRequest Request(Poco::Net::HTTPRequest::HTTP_POST, - Path, - Poco::Net::HTTPMessage::HTTP_1_1); - std::ostringstream obody; - Poco::JSON::Stringifier::stringify(Body_,obody); - - Request.setContentType("application/json"); - Request.setContentLength(obody.str().size()); - - if(BearerToken.empty()) { - Request.add("X-API-KEY", Svc.AccessKey); - Request.add("X-INTERNAL-NAME", MicroServicePublicEndPoint()); - } else { - // Authorization: Bearer ${token} - Request.add("Authorization", "Bearer " + BearerToken); - } - - if(Secure) { - Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort()); - Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); - std::ostream &os = Session.sendRequest(Request); - os << obody.str(); - - Poco::Net::HTTPResponse Response; - std::istream &is = Session.receiveResponse(Response); - if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { - Poco::JSON::Parser P; - ResponseObject = P.parse(is).extract(); + if (BearerToken.empty()) { + Request.add("X-API-KEY", Svc.AccessKey); + Request.add("X-INTERNAL-NAME", MicroServicePublicEndPoint()); } else { - Poco::JSON::Parser P; - ResponseObject = P.parse(is).extract(); + // Authorization: Bearer ${token} + Request.add("Authorization", "Bearer " + BearerToken); } - return Response.getStatus(); - } else { - Poco::Net::HTTPClientSession Session(URI.getHost(), URI.getPort()); - Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); - std::ostream &os = Session.sendRequest(Request); - os << obody.str(); - Poco::Net::HTTPResponse Response; - std::istream &is = Session.receiveResponse(Response); - if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { - Poco::JSON::Parser P; - ResponseObject = P.parse(is).extract(); + if (Secure) { + Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort()); + Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); + + std::ostream &os = Session.sendRequest(Request); + os << obody.str(); + + Poco::Net::HTTPResponse Response; + std::istream &is = Session.receiveResponse(Response); + if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { + Poco::JSON::Parser P; + ResponseObject = P.parse(is).extract(); + } else { + Poco::JSON::Parser P; + ResponseObject = P.parse(is).extract(); + } + return Response.getStatus(); } else { - Poco::JSON::Parser P; - ResponseObject = P.parse(is).extract(); + Poco::Net::HTTPClientSession Session(URI.getHost(), URI.getPort()); + Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); + + std::ostream &os = Session.sendRequest(Request); + os << obody.str(); + + Poco::Net::HTTPResponse Response; + std::istream &is = Session.receiveResponse(Response); + if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { + Poco::JSON::Parser P; + ResponseObject = P.parse(is).extract(); + } else { + Poco::JSON::Parser P; + ResponseObject = P.parse(is).extract(); + } + return Response.getStatus(); } - return Response.getStatus(); } + } catch (const Poco::Exception &E) { + Poco::Logger::get("REST-CALLER-PUT").log(E); } + return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT; } - catch (const Poco::Exception &E) - { - Poco::Logger::get("REST-CALLER-POST").log(E); - } - return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT; -} - Poco::Net::HTTPServerResponse::HTTPStatus OpenAPIRequestDelete::Do(const std::string & BearerToken) { - try { - auto Services = MicroServiceGetServices(Type_); + Poco::Net::HTTPServerResponse::HTTPStatus + OpenAPIRequestPost::Do(Poco::JSON::Object::Ptr &ResponseObject, + const std::string &BearerToken) { + try { + auto Services = MicroServiceGetServices(Type_); - for(auto const &Svc:Services) { - Poco::URI URI(Svc.PrivateEndPoint); + for (auto const &Svc : Services) { + Poco::URI URI(Svc.PrivateEndPoint); - auto Secure = (URI.getScheme() == "https"); + auto Secure = (URI.getScheme() == "https"); - URI.setPath(EndPoint_); - for (const auto &qp : QueryData_) - URI.addQueryParameter(qp.first, qp.second); + URI.setPath(EndPoint_); + for (const auto &qp : QueryData_) + URI.addQueryParameter(qp.first, qp.second); - poco_debug(Poco::Logger::get("REST-CALLER-DELETE"),fmt::format(" {}", LoggingStr_.empty() ? URI.toString() : LoggingStr_ ) ); + poco_debug(Poco::Logger::get("REST-CALLER-POST"), + fmt::format(" {}", LoggingStr_.empty() ? URI.toString() : LoggingStr_)); - std::string Path(URI.getPathAndQuery()); + std::string Path(URI.getPathAndQuery()); - Poco::Net::HTTPRequest Request(Poco::Net::HTTPRequest::HTTP_DELETE, - Path, - Poco::Net::HTTPMessage::HTTP_1_1); - if(BearerToken.empty()) { - Request.add("X-API-KEY", Svc.AccessKey); - Request.add("X-INTERNAL-NAME", MicroServicePublicEndPoint()); - } else { - // Authorization: Bearer ${token} - Request.add("Authorization", "Bearer " + BearerToken); - } - - if(Secure) { - Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort()); - Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); - Session.sendRequest(Request); - Poco::Net::HTTPResponse Response; - Session.receiveResponse(Response); - return Response.getStatus(); - } else { - Poco::Net::HTTPClientSession Session(URI.getHost(), URI.getPort()); - Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); - Session.sendRequest(Request); - Poco::Net::HTTPResponse Response; - Session.receiveResponse(Response); - return Response.getStatus(); + Poco::Net::HTTPRequest Request(Poco::Net::HTTPRequest::HTTP_POST, Path, + Poco::Net::HTTPMessage::HTTP_1_1); + std::ostringstream obody; + Poco::JSON::Stringifier::stringify(Body_, obody); + + Request.setContentType("application/json"); + Request.setContentLength(obody.str().size()); + + if (BearerToken.empty()) { + Request.add("X-API-KEY", Svc.AccessKey); + Request.add("X-INTERNAL-NAME", MicroServicePublicEndPoint()); + } else { + // Authorization: Bearer ${token} + Request.add("Authorization", "Bearer " + BearerToken); + } + + if (Secure) { + Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort()); + Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); + std::ostream &os = Session.sendRequest(Request); + os << obody.str(); + + Poco::Net::HTTPResponse Response; + std::istream &is = Session.receiveResponse(Response); + if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { + Poco::JSON::Parser P; + ResponseObject = P.parse(is).extract(); + } else { + Poco::JSON::Parser P; + ResponseObject = P.parse(is).extract(); + } + return Response.getStatus(); + } else { + Poco::Net::HTTPClientSession Session(URI.getHost(), URI.getPort()); + Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); + std::ostream &os = Session.sendRequest(Request); + os << obody.str(); + + Poco::Net::HTTPResponse Response; + std::istream &is = Session.receiveResponse(Response); + if (Response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) { + Poco::JSON::Parser P; + ResponseObject = P.parse(is).extract(); + } else { + Poco::JSON::Parser P; + ResponseObject = P.parse(is).extract(); + } + return Response.getStatus(); + } } + } catch (const Poco::Exception &E) { + Poco::Logger::get("REST-CALLER-POST").log(E); } + return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT; } - catch (const Poco::Exception &E) - { - Poco::Logger::get("REST-CALLER-DELETE").log(E); - } - return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT; -} + Poco::Net::HTTPServerResponse::HTTPStatus + OpenAPIRequestDelete::Do(const std::string &BearerToken) { + try { + auto Services = MicroServiceGetServices(Type_); + + for (auto const &Svc : Services) { + Poco::URI URI(Svc.PrivateEndPoint); + + auto Secure = (URI.getScheme() == "https"); + + URI.setPath(EndPoint_); + for (const auto &qp : QueryData_) + URI.addQueryParameter(qp.first, qp.second); + + poco_debug(Poco::Logger::get("REST-CALLER-DELETE"), + fmt::format(" {}", LoggingStr_.empty() ? URI.toString() : LoggingStr_)); + + std::string Path(URI.getPathAndQuery()); + + Poco::Net::HTTPRequest Request(Poco::Net::HTTPRequest::HTTP_DELETE, Path, + Poco::Net::HTTPMessage::HTTP_1_1); + if (BearerToken.empty()) { + Request.add("X-API-KEY", Svc.AccessKey); + Request.add("X-INTERNAL-NAME", MicroServicePublicEndPoint()); + } else { + // Authorization: Bearer ${token} + Request.add("Authorization", "Bearer " + BearerToken); + } + + if (Secure) { + Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort()); + Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); + Session.sendRequest(Request); + Poco::Net::HTTPResponse Response; + Session.receiveResponse(Response); + return Response.getStatus(); + } else { + Poco::Net::HTTPClientSession Session(URI.getHost(), URI.getPort()); + Session.setTimeout(Poco::Timespan(msTimeout_ / 1000, msTimeout_ % 1000)); + Session.sendRequest(Request); + Poco::Net::HTTPResponse Response; + Session.receiveResponse(Response); + return Response.getStatus(); + } + } + } catch (const Poco::Exception &E) { + Poco::Logger::get("REST-CALLER-DELETE").log(E); + } + return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT; + } } // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/OpenAPIRequests.h b/src/framework/OpenAPIRequests.h index ab38c0a..000d3fb 100644 --- a/src/framework/OpenAPIRequests.h +++ b/src/framework/OpenAPIRequests.h @@ -15,96 +15,79 @@ namespace OpenWifi { class OpenAPIRequestGet { public: - explicit OpenAPIRequestGet( const std::string & Type, - const std::string & EndPoint, - const Types::StringPairVec & QueryData, - uint64_t msTimeout, - const std::string &LoggingStr=""): - Type_(Type), - EndPoint_(EndPoint), - QueryData_(QueryData), - msTimeout_(msTimeout), - LoggingStr_(LoggingStr){}; - Poco::Net::HTTPServerResponse::HTTPStatus Do(Poco::JSON::Object::Ptr &ResponseObject, const std::string & BearerToken = ""); + explicit OpenAPIRequestGet(const std::string &Type, const std::string &EndPoint, + const Types::StringPairVec &QueryData, uint64_t msTimeout, + const std::string &LoggingStr = "") + : Type_(Type), EndPoint_(EndPoint), QueryData_(QueryData), msTimeout_(msTimeout), + LoggingStr_(LoggingStr){}; + Poco::Net::HTTPServerResponse::HTTPStatus Do(Poco::JSON::Object::Ptr &ResponseObject, + const std::string &BearerToken = ""); + private: - std::string Type_; - std::string EndPoint_; - Types::StringPairVec QueryData_; - uint64_t msTimeout_; - std::string LoggingStr_; + std::string Type_; + std::string EndPoint_; + Types::StringPairVec QueryData_; + uint64_t msTimeout_; + std::string LoggingStr_; }; class OpenAPIRequestPut { public: - explicit OpenAPIRequestPut( const std::string & Type, - const std::string & EndPoint, - const Types::StringPairVec & QueryData, - const Poco::JSON::Object & Body, - uint64_t msTimeout, - const std::string &LoggingStr=""): - Type_(Type), - EndPoint_(EndPoint), - QueryData_(QueryData), - msTimeout_(msTimeout), - Body_(Body), - LoggingStr_(LoggingStr){}; + explicit OpenAPIRequestPut(const std::string &Type, const std::string &EndPoint, + const Types::StringPairVec &QueryData, + const Poco::JSON::Object &Body, uint64_t msTimeout, + const std::string &LoggingStr = "") + : Type_(Type), EndPoint_(EndPoint), QueryData_(QueryData), msTimeout_(msTimeout), + Body_(Body), LoggingStr_(LoggingStr){}; - Poco::Net::HTTPServerResponse::HTTPStatus Do(Poco::JSON::Object::Ptr &ResponseObject, const std::string & BearerToken = ""); + Poco::Net::HTTPServerResponse::HTTPStatus Do(Poco::JSON::Object::Ptr &ResponseObject, + const std::string &BearerToken = ""); private: - std::string Type_; - std::string EndPoint_; - Types::StringPairVec QueryData_; - uint64_t msTimeout_; - Poco::JSON::Object Body_; - std::string LoggingStr_; + std::string Type_; + std::string EndPoint_; + Types::StringPairVec QueryData_; + uint64_t msTimeout_; + Poco::JSON::Object Body_; + std::string LoggingStr_; }; class OpenAPIRequestPost { public: - explicit OpenAPIRequestPost( const std::string & Type, - const std::string & EndPoint, - const Types::StringPairVec & QueryData, - const Poco::JSON::Object & Body, - uint64_t msTimeout, - const std::string &LoggingStr=""): - Type_(Type), - EndPoint_(EndPoint), - QueryData_(QueryData), - msTimeout_(msTimeout), - Body_(Body), - LoggingStr_(LoggingStr){}; - Poco::Net::HTTPServerResponse::HTTPStatus Do(Poco::JSON::Object::Ptr &ResponseObject, const std::string & BearerToken = ""); + explicit OpenAPIRequestPost(const std::string &Type, const std::string &EndPoint, + const Types::StringPairVec &QueryData, + const Poco::JSON::Object &Body, uint64_t msTimeout, + const std::string &LoggingStr = "") + : Type_(Type), EndPoint_(EndPoint), QueryData_(QueryData), msTimeout_(msTimeout), + Body_(Body), LoggingStr_(LoggingStr){}; + Poco::Net::HTTPServerResponse::HTTPStatus Do(Poco::JSON::Object::Ptr &ResponseObject, + const std::string &BearerToken = ""); + private: - std::string Type_; - std::string EndPoint_; - Types::StringPairVec QueryData_; - uint64_t msTimeout_; - Poco::JSON::Object Body_; - std::string LoggingStr_; + std::string Type_; + std::string EndPoint_; + Types::StringPairVec QueryData_; + uint64_t msTimeout_; + Poco::JSON::Object Body_; + std::string LoggingStr_; }; class OpenAPIRequestDelete { public: - explicit OpenAPIRequestDelete( const std::string & Type, - const std::string & EndPoint, - const Types::StringPairVec & QueryData, - uint64_t msTimeout, - const std::string &LoggingStr=""): - Type_(Type), - EndPoint_(EndPoint), - QueryData_(QueryData), - msTimeout_(msTimeout), - LoggingStr_(LoggingStr){}; - Poco::Net::HTTPServerResponse::HTTPStatus Do(const std::string & BearerToken = ""); + explicit OpenAPIRequestDelete(const std::string &Type, const std::string &EndPoint, + const Types::StringPairVec &QueryData, uint64_t msTimeout, + const std::string &LoggingStr = "") + : Type_(Type), EndPoint_(EndPoint), QueryData_(QueryData), msTimeout_(msTimeout), + LoggingStr_(LoggingStr){}; + Poco::Net::HTTPServerResponse::HTTPStatus Do(const std::string &BearerToken = ""); private: - std::string Type_; - std::string EndPoint_; - Types::StringPairVec QueryData_; - uint64_t msTimeout_; - Poco::JSON::Object Body_; - std::string LoggingStr_; + std::string Type_; + std::string EndPoint_; + Types::StringPairVec QueryData_; + uint64_t msTimeout_; + Poco::JSON::Object Body_; + std::string LoggingStr_; }; } // namespace OpenWifi diff --git a/src/framework/OpenWifiTypes.h b/src/framework/OpenWifiTypes.h index 9671d05..7be530d 100644 --- a/src/framework/OpenWifiTypes.h +++ b/src/framework/OpenWifiTypes.h @@ -4,70 +4,71 @@ #pragma once +#include +#include #include +#include +#include +#include #include #include -#include -#include -#include -#include -#include namespace OpenWifi::Types { - typedef std::pair StringPair; - typedef std::vector StringPairVec; - typedef std::queue StringPairQueue; - typedef std::vector StringVec; - typedef std::set StringSet; - typedef std::map> StringMapStringSet; - typedef std::function TopicNotifyFunction; - typedef std::list> TopicNotifyFunctionList; - typedef std::map NotifyTable; - typedef std::map CountedMap; - typedef std::vector TagList; - typedef std::string UUID_t; - typedef std::vector UUIDvec_t; - typedef std::map> Counted3DMapSII; + typedef std::pair StringPair; + typedef std::vector StringPairVec; + typedef std::queue StringPairQueue; + typedef std::vector StringVec; + typedef std::set StringSet; + typedef std::map> StringMapStringSet; + typedef std::function TopicNotifyFunction; + typedef std::list> TopicNotifyFunctionList; + typedef std::map NotifyTable; + typedef std::map CountedMap; + typedef std::vector TagList; + typedef std::string UUID_t; + typedef std::vector UUIDvec_t; + typedef std::map> Counted3DMapSII; struct MicroServiceMeta { - uint64_t Id=0; - std::string Type; - std::string PrivateEndPoint; - std::string PublicEndPoint; - std::string AccessKey; - std::string Version; - uint64_t LastUpdate=0; + uint64_t Id = 0; + std::string Type; + std::string PrivateEndPoint; + std::string PublicEndPoint; + std::string AccessKey; + std::string Version; + uint64_t LastUpdate = 0; }; - typedef std::map MicroServiceMetaMap; - typedef std::vector MicroServiceMetaVec; -} + typedef std::map MicroServiceMetaMap; + typedef std::vector MicroServiceMetaVec; +} // namespace OpenWifi::Types namespace OpenWifi { - inline void UpdateCountedMap(OpenWifi::Types::CountedMap &M, const std::string &S, uint64_t Increment=1) { - auto it = M.find(S); - if(it==M.end()) - M[S] = Increment; - else - it->second += Increment; - } + inline void UpdateCountedMap(OpenWifi::Types::CountedMap &M, const std::string &S, + uint64_t Increment = 1) { + auto it = M.find(S); + if (it == M.end()) + M[S] = Increment; + else + it->second += Increment; + } - inline void UpdateCountedMap(OpenWifi::Types::Counted3DMapSII &M, const std::string &S, uint32_t Index, uint64_t Increment=1) { - auto it = M.find(S); - if(it==M.end()) { - std::map E; - E[Index] = Increment; - M[S] = E; - } - else { - std::map & IndexMap = it->second; - auto it_index = IndexMap.find(Index); - if(it_index == IndexMap.end()) { - IndexMap[Index] = Increment; - } else { - it_index->second += Increment; - } - } - } -} + inline void UpdateCountedMap(OpenWifi::Types::Counted3DMapSII &M, const std::string &S, + uint32_t Index, uint64_t Increment = 1) { + auto it = M.find(S); + if (it == M.end()) { + std::map E; + E[Index] = Increment; + M[S] = E; + } else { + std::map &IndexMap = it->second; + auto it_index = IndexMap.find(Index); + if (it_index == IndexMap.end()) { + IndexMap[Index] = Increment; + } else { + it_index->second += Increment; + } + } + } +} // namespace OpenWifi diff --git a/src/framework/RESTAPI_ExtServer.cpp b/src/framework/RESTAPI_ExtServer.cpp index 979a850..921272d 100644 --- a/src/framework/RESTAPI_ExtServer.cpp +++ b/src/framework/RESTAPI_ExtServer.cpp @@ -6,22 +6,23 @@ namespace OpenWifi { - Poco::Net::HTTPRequestHandler *ExtRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest &Request) { + Poco::Net::HTTPRequestHandler * + ExtRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest &Request) { try { Poco::URI uri(Request.getURI()); auto TID = NextTransactionId_++; - Utils::SetThreadName(fmt::format("x-rest:{}",TID).c_str()); + Utils::SetThreadName(fmt::format("x-rest:{}", TID).c_str()); return RESTAPI_ExtServer()->CallServer(uri.getPath(), TID); } catch (...) { - } return nullptr; } - Poco::Net::HTTPRequestHandler *RESTAPI_ExtServer::CallServer(const std::string &Path, uint64_t Id) { + Poco::Net::HTTPRequestHandler *RESTAPI_ExtServer::CallServer(const std::string &Path, + uint64_t Id) { RESTAPIHandler::BindingMap Bindings; - Utils::SetThreadName(fmt::format("x-rest:{}",Id).c_str()); + Utils::SetThreadName(fmt::format("x-rest:{}", Id).c_str()); return RESTAPI_ExtRouter(Path, Bindings, Logger(), Server_, Id); } -} +} // namespace OpenWifi diff --git a/src/framework/RESTAPI_ExtServer.h b/src/framework/RESTAPI_ExtServer.h index ab3961a..0358e42 100644 --- a/src/framework/RESTAPI_ExtServer.h +++ b/src/framework/RESTAPI_ExtServer.h @@ -6,20 +6,23 @@ #include "Poco/Net/HTTPServer.h" -#include "framework/SubSystemServer.h" #include "framework/RESTAPI_Handler.h" +#include "framework/SubSystemServer.h" namespace OpenWifi { - Poco::Net::HTTPRequestHandler * RESTAPI_ExtRouter(const std::string &Path, RESTAPIHandler::BindingMap &Bindings, - Poco::Logger & L, RESTAPI_GenericServerAccounting & S, uint64_t Id); + Poco::Net::HTTPRequestHandler * + RESTAPI_ExtRouter(const std::string &Path, RESTAPIHandler::BindingMap &Bindings, + Poco::Logger &L, RESTAPI_GenericServerAccounting &S, uint64_t Id); class ExtRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { public: ExtRequestHandlerFactory() = default; - Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &Request) override; + Poco::Net::HTTPRequestHandler * + createRequestHandler(const Poco::Net::HTTPServerRequest &Request) override; + private: - static inline std::atomic_uint64_t NextTransactionId_ = 1; + static inline std::atomic_uint64_t NextTransactionId_ = 1; }; class RESTAPI_ExtServer : public SubSystemServer { @@ -30,16 +33,21 @@ namespace OpenWifi { } inline int Start() override { - poco_information(Logger(),"Starting."); + poco_information(Logger(), "Starting."); Server_.InitLogging(); - for(const auto & Svr: ConfigServersList_) { + for (const auto &Svr : ConfigServersList_) { - if(MicroServiceNoAPISecurity()) { - poco_information(Logger(),fmt::format("Starting: {}:{}. Security has been disabled for APIs.", Svr.Address(), Svr.Port())); + if (MicroServiceNoAPISecurity()) { + poco_information( + Logger(), + fmt::format("Starting: {}:{}. Security has been disabled for APIs.", + Svr.Address(), Svr.Port())); } else { - poco_information(Logger(),fmt::format("Starting: {}:{} Keyfile:{} CertFile: {}", Svr.Address(), Svr.Port(), - Svr.KeyFile(),Svr.CertFile())); + poco_information(Logger(), + fmt::format("Starting: {}:{} Keyfile:{} CertFile: {}", + Svr.Address(), Svr.Port(), Svr.KeyFile(), + Svr.CertFile())); Svr.LogCert(Logger()); if (!Svr.RootCA().empty()) Svr.LogCas(Logger()); @@ -49,13 +57,15 @@ namespace OpenWifi { Params->setKeepAlive(true); Params->setName("ws:xrest"); - std::unique_ptr NewServer; - if(MicroServiceNoAPISecurity()) { + std::unique_ptr NewServer; + if (MicroServiceNoAPISecurity()) { auto Sock{Svr.CreateSocket(Logger())}; - NewServer = std::make_unique(new ExtRequestHandlerFactory, Pool_, Sock, Params); + NewServer = std::make_unique( + new ExtRequestHandlerFactory, Pool_, Sock, Params); } else { auto Sock{Svr.CreateSecureSocket(Logger())}; - NewServer = std::make_unique(new ExtRequestHandlerFactory, Pool_, Sock, Params); + NewServer = std::make_unique( + new ExtRequestHandlerFactory, Pool_, Sock, Params); }; NewServer->start(); RESTServers_.push_back(std::move(NewServer)); @@ -64,36 +74,34 @@ namespace OpenWifi { } inline void Stop() override { - poco_information(Logger(),"Stopping..."); - for( const auto & svr : RESTServers_ ) + poco_information(Logger(), "Stopping..."); + for (const auto &svr : RESTServers_) svr->stopAll(true); Pool_.stopAll(); Pool_.joinAll(); RESTServers_.clear(); - poco_information(Logger(),"Stopped..."); + poco_information(Logger(), "Stopped..."); } inline void reinitialize([[maybe_unused]] Poco::Util::Application &self) override { MicroServiceLoadConfigurationFile(); - poco_information(Logger(),"Reinitializing."); + poco_information(Logger(), "Reinitializing."); Stop(); Start(); } Poco::Net::HTTPRequestHandler *CallServer(const std::string &Path, uint64_t Id); - const Poco::ThreadPool & Pool() { return Pool_; } + const Poco::ThreadPool &Pool() { return Pool_; } private: - std::vector> RESTServers_; - Poco::ThreadPool Pool_{"x-rest",8,128}; - RESTAPI_GenericServerAccounting Server_; + std::vector> RESTServers_; + Poco::ThreadPool Pool_{"x-rest", 8, 128}; + RESTAPI_GenericServerAccounting Server_; - RESTAPI_ExtServer() noexcept: - SubSystemServer("RESTAPI_ExtServer", "REST-XSRV", "openwifi.restapi") - { - } + RESTAPI_ExtServer() noexcept + : SubSystemServer("RESTAPI_ExtServer", "REST-XSRV", "openwifi.restapi") {} }; inline auto RESTAPI_ExtServer() { return RESTAPI_ExtServer::instance(); }; -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/RESTAPI_GenericServerAccounting.h b/src/framework/RESTAPI_GenericServerAccounting.h index fa91b27..7482613 100644 --- a/src/framework/RESTAPI_GenericServerAccounting.h +++ b/src/framework/RESTAPI_GenericServerAccounting.h @@ -4,72 +4,70 @@ #pragma once -#include #include +#include -#include "Poco/StringTokenizer.h" -#include "Poco/String.h" #include "Poco/Net/HTTPRequest.h" +#include "Poco/String.h" +#include "Poco/StringTokenizer.h" #include "framework/MicroServiceFuncs.h" namespace OpenWifi { class RESTAPI_GenericServerAccounting { public: - - enum { - LOG_GET=0, - LOG_DELETE, - LOG_PUT, - LOG_POST - }; + enum { LOG_GET = 0, LOG_DELETE, LOG_PUT, LOG_POST }; void inline SetFlags(bool External, const std::string &Methods) { - Poco::StringTokenizer Tokens(Methods,","); + Poco::StringTokenizer Tokens(Methods, ","); auto Offset = (External ? 0 : 4); - for(const auto &i:Tokens) { - if(Poco::icompare(i,Poco::Net::HTTPRequest::HTTP_DELETE)==0) - LogFlags_[Offset+LOG_DELETE]=true; - else if(Poco::icompare(i,Poco::Net::HTTPRequest::HTTP_PUT)==0) - LogFlags_[Offset+LOG_PUT]=true; - else if(Poco::icompare(i,Poco::Net::HTTPRequest::HTTP_POST)==0) - LogFlags_[Offset+LOG_POST]=true; - else if(Poco::icompare(i,Poco::Net::HTTPRequest::HTTP_GET)==0) - LogFlags_[Offset+LOG_GET]=true; + for (const auto &i : Tokens) { + if (Poco::icompare(i, Poco::Net::HTTPRequest::HTTP_DELETE) == 0) + LogFlags_[Offset + LOG_DELETE] = true; + else if (Poco::icompare(i, Poco::Net::HTTPRequest::HTTP_PUT) == 0) + LogFlags_[Offset + LOG_PUT] = true; + else if (Poco::icompare(i, Poco::Net::HTTPRequest::HTTP_POST) == 0) + LogFlags_[Offset + LOG_POST] = true; + else if (Poco::icompare(i, Poco::Net::HTTPRequest::HTTP_GET) == 0) + LogFlags_[Offset + LOG_GET] = true; } } inline void InitLogging() { - std::string Public = MicroServiceConfigGetString("apilogging.public.methods","PUT,POST,DELETE"); + std::string Public = + MicroServiceConfigGetString("apilogging.public.methods", "PUT,POST,DELETE"); SetFlags(true, Public); - std::string Private = MicroServiceConfigGetString("apilogging.private.methods","PUT,POST,DELETE"); + std::string Private = + MicroServiceConfigGetString("apilogging.private.methods", "PUT,POST,DELETE"); SetFlags(false, Private); - std::string PublicBadTokens = MicroServiceConfigGetString("apilogging.public.badtokens.methods",""); - LogBadTokens_[0] = (Poco::icompare(PublicBadTokens,"true")==0); - std::string PrivateBadTokens = MicroServiceConfigGetString("apilogging.private.badtokens.methods",""); - LogBadTokens_[1] = (Poco::icompare(PrivateBadTokens,"true")==0); + std::string PublicBadTokens = + MicroServiceConfigGetString("apilogging.public.badtokens.methods", ""); + LogBadTokens_[0] = (Poco::icompare(PublicBadTokens, "true") == 0); + std::string PrivateBadTokens = + MicroServiceConfigGetString("apilogging.private.badtokens.methods", ""); + LogBadTokens_[1] = (Poco::icompare(PrivateBadTokens, "true") == 0); } [[nodiscard]] inline bool LogIt(const std::string &Method, bool External) const { auto Offset = (External ? 0 : 4); - if(Method == Poco::Net::HTTPRequest::HTTP_GET) - return LogFlags_[Offset+LOG_GET]; - if(Method == Poco::Net::HTTPRequest::HTTP_POST) - return LogFlags_[Offset+LOG_POST]; - if(Method == Poco::Net::HTTPRequest::HTTP_PUT) - return LogFlags_[Offset+LOG_PUT]; - if(Method == Poco::Net::HTTPRequest::HTTP_DELETE) - return LogFlags_[Offset+LOG_DELETE]; + if (Method == Poco::Net::HTTPRequest::HTTP_GET) + return LogFlags_[Offset + LOG_GET]; + if (Method == Poco::Net::HTTPRequest::HTTP_POST) + return LogFlags_[Offset + LOG_POST]; + if (Method == Poco::Net::HTTPRequest::HTTP_PUT) + return LogFlags_[Offset + LOG_PUT]; + if (Method == Poco::Net::HTTPRequest::HTTP_DELETE) + return LogFlags_[Offset + LOG_DELETE]; return false; }; [[nodiscard]] inline bool LogBadTokens(bool External) const { - return LogBadTokens_[ (External ? 0 : 1) ]; + return LogBadTokens_[(External ? 0 : 1)]; }; private: - std::array LogFlags_{false}; - std::array LogBadTokens_{false}; + std::array LogFlags_{false}; + std::array LogBadTokens_{false}; }; -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/RESTAPI_Handler.cpp b/src/framework/RESTAPI_Handler.cpp index acaf371..b8ba754 100644 --- a/src/framework/RESTAPI_Handler.cpp +++ b/src/framework/RESTAPI_Handler.cpp @@ -4,5 +4,4 @@ #include "RESTAPI_Handler.h" -namespace OpenWifi { -} // namespace OpenWifi \ No newline at end of file +namespace OpenWifi {} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/RESTAPI_Handler.h b/src/framework/RESTAPI_Handler.h index 4c72497..232958c 100644 --- a/src/framework/RESTAPI_Handler.h +++ b/src/framework/RESTAPI_Handler.h @@ -4,27 +4,27 @@ #pragma once +#include #include #include -#include -#include "Poco/Net/HTTPRequestHandler.h" -#include "Poco/Logger.h" +#include "Poco/DeflatingStream.h" #include "Poco/JSON/Object.h" #include "Poco/JSON/Parser.h" +#include "Poco/Logger.h" +#include "Poco/Net/HTTPRequestHandler.h" #include "Poco/Net/HTTPResponse.h" #include "Poco/Net/HTTPServerResponse.h" -#include "Poco/DeflatingStream.h" -#include "Poco/TemporaryFile.h" #include "Poco/Net/OAuth20Credentials.h" +#include "Poco/TemporaryFile.h" -#include "framework/ow_constants.h" +#include "RESTObjects/RESTAPI_SecurityObjects.h" +#include "framework/AuthClient.h" #include "framework/RESTAPI_GenericServerAccounting.h" #include "framework/RESTAPI_RateLimiter.h" -#include "framework/utils.h" #include "framework/RESTAPI_utils.h" -#include "framework/AuthClient.h" -#include "RESTObjects/RESTAPI_SecurityObjects.h" +#include "framework/ow_constants.h" +#include "framework/utils.h" #if defined(TIP_SECURITY_SERVICE) #include "AuthService.h" @@ -37,42 +37,32 @@ namespace OpenWifi { class RESTAPIHandler : public Poco::Net::HTTPRequestHandler { public: struct QueryBlock { - uint64_t StartDate = 0 , EndDate = 0 , Offset = 0 , Limit = 0, LogType = 0 ; + uint64_t StartDate = 0, EndDate = 0, Offset = 0, Limit = 0, LogType = 0; std::string SerialNumber, Filter; - std::vector Select; - bool Lifetime=false, LastOnly=false, Newest=false, CountOnly=false, AdditionalInfo=false; + std::vector Select; + bool Lifetime = false, LastOnly = false, Newest = false, CountOnly = false, + AdditionalInfo = false; }; typedef std::map BindingMap; struct RateLimit { - int64_t Interval=1000; - int64_t MaxCalls=10; + int64_t Interval = 1000; + int64_t MaxCalls = 10; }; - RESTAPIHandler( BindingMap map, - Poco::Logger &l, - std::vector Methods, - RESTAPI_GenericServerAccounting & Server, - uint64_t TransactionId, - bool Internal, - bool AlwaysAuthorize=true, - bool RateLimited=false, - const RateLimit & Profile = RateLimit{.Interval=1000,.MaxCalls=100}, - bool SubscriberOnly=false) - : Bindings_(std::move(map)), - Logger_(l), - Methods_(std::move(Methods)), - Internal_(Internal), - RateLimited_(RateLimited), - SubOnlyService_(SubscriberOnly), - AlwaysAuthorize_(AlwaysAuthorize), - Server_(Server), - MyRates_(Profile), - TransactionId_(TransactionId) - { - } + RESTAPIHandler(BindingMap map, Poco::Logger &l, std::vector Methods, + RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, + bool Internal, bool AlwaysAuthorize = true, bool RateLimited = false, + const RateLimit &Profile = RateLimit{.Interval = 1000, .MaxCalls = 100}, + bool SubscriberOnly = false) + : Bindings_(std::move(map)), Logger_(l), Methods_(std::move(Methods)), + Internal_(Internal), RateLimited_(RateLimited), SubOnlyService_(SubscriberOnly), + AlwaysAuthorize_(AlwaysAuthorize), Server_(Server), MyRates_(Profile), + TransactionId_(TransactionId) {} - inline bool RoleIsAuthorized([[maybe_unused]] const std::string & Path, [[maybe_unused]] const std::string & Method, [[maybe_unused]] std::string & Reason) { + inline bool RoleIsAuthorized([[maybe_unused]] const std::string &Path, + [[maybe_unused]] const std::string &Method, + [[maybe_unused]] std::string &Reason) { return true; } @@ -85,31 +75,33 @@ namespace OpenWifi { // std::string th_name = "restsvr_" + std::to_string(TransactionId_); // Utils::SetThreadName(th_name.c_str()); - if(Request->getContentLength()>0) { - if(Request->getContentType().find("application/json")!=std::string::npos) { - ParsedBody_ = IncomingParser_.parse(Request->stream()).extract(); + if (Request->getContentLength() > 0) { + if (Request->getContentType().find("application/json") != std::string::npos) { + ParsedBody_ = IncomingParser_.parse(Request->stream()) + .extract(); } } - if(RateLimited_ && RESTAPI_RateLimiter()->IsRateLimited(RequestIn,MyRates_.Interval, MyRates_.MaxCalls)) { + if (RateLimited_ && RESTAPI_RateLimiter()->IsRateLimited( + RequestIn, MyRates_.Interval, MyRates_.MaxCalls)) { return UnAuthorized(RESTAPI::Errors::RATE_LIMIT_EXCEEDED); } if (!ContinueProcessing()) return; - bool Expired=false, Contacted=false; + bool Expired = false, Contacted = false; if (AlwaysAuthorize_ && !IsAuthorized(Expired, Contacted, SubOnlyService_)) { - if(Expired) + if (Expired) return UnAuthorized(RESTAPI::Errors::EXPIRED_TOKEN); - if(Contacted) + if (Contacted) return UnAuthorized(RESTAPI::Errors::INVALID_TOKEN); else return UnAuthorized(RESTAPI::Errors::SECURITY_SERVICE_UNREACHABLE); } std::string Reason; - if(!RoleIsAuthorized(RequestIn.getURI(), Request->getMethod(), Reason)) { + if (!RoleIsAuthorized(RequestIn.getURI(), Request->getMethod(), Reason)) { return UnAuthorized(RESTAPI::Errors::ACCESS_DENIED); } @@ -130,13 +122,17 @@ namespace OpenWifi { } [[nodiscard]] inline bool NeedAdditionalInfo() const { return QB_.AdditionalInfo; } - [[nodiscard]] inline const std::vector & SelectedRecords() const { return QB_.Select; } + [[nodiscard]] inline const std::vector &SelectedRecords() const { + return QB_.Select; + } - inline static bool ParseBindings(const std::string & Request, const std::list & EndPoints, BindingMap &bindings) { + inline static bool ParseBindings(const std::string &Request, + const std::list &EndPoints, + BindingMap &bindings) { bindings.clear(); auto PathItems = Poco::StringTokenizer(Request, "/"); - for(const auto &EndPoint:EndPoints) { + for (const auto &EndPoint : EndPoints) { auto ParamItems = Poco::StringTokenizer(EndPoint, "/"); if (PathItems.count() != ParamItems.count()) continue; @@ -153,7 +149,7 @@ namespace OpenWifi { } } } - if(Matched) + if (Matched) return true; } return false; @@ -180,44 +176,57 @@ namespace OpenWifi { return false; } - [[nodiscard]] inline uint64_t GetParameter(const std::string &Name, const uint64_t Default) { - auto Hint = std::find_if(Parameters_.begin(),Parameters_.end(),[&](const std::pair &S){ return S.first==Name; }); - if(Hint==Parameters_.end() || !is_number(Hint->second)) + [[nodiscard]] inline uint64_t GetParameter(const std::string &Name, + const uint64_t Default) { + auto Hint = std::find_if( + Parameters_.begin(), Parameters_.end(), + [&](const std::pair &S) { return S.first == Name; }); + if (Hint == Parameters_.end() || !is_number(Hint->second)) return Default; return std::stoull(Hint->second); } - [[nodiscard]] inline bool GetBoolParameter(const std::string &Name, bool Default=false) { - auto Hint = std::find_if(begin(Parameters_),end(Parameters_),[&](const std::pair &S){ return S.first==Name; }); - if(Hint==end(Parameters_) || !is_bool(Hint->second)) + [[nodiscard]] inline bool GetBoolParameter(const std::string &Name, bool Default = false) { + auto Hint = std::find_if( + begin(Parameters_), end(Parameters_), + [&](const std::pair &S) { return S.first == Name; }); + if (Hint == end(Parameters_) || !is_bool(Hint->second)) return Default; - return Hint->second=="true"; + return Hint->second == "true"; } - [[nodiscard]] inline std::string GetParameter(const std::string &Name, const std::string &Default="") { - auto Hint = std::find_if(begin(Parameters_),end(Parameters_),[&](const std::pair &S){ return S.first==Name; }); - if(Hint==end(Parameters_)) + [[nodiscard]] inline std::string GetParameter(const std::string &Name, + const std::string &Default = "") { + auto Hint = std::find_if( + begin(Parameters_), end(Parameters_), + [&](const std::pair &S) { return S.first == Name; }); + if (Hint == end(Parameters_)) return Default; return Hint->second; } [[nodiscard]] inline bool HasParameter(const std::string &Name, std::string &Value) { - auto Hint = std::find_if(begin(Parameters_),end(Parameters_),[&](const std::pair &S){ return S.first==Name; }); - if(Hint==end(Parameters_)) + auto Hint = std::find_if( + begin(Parameters_), end(Parameters_), + [&](const std::pair &S) { return S.first == Name; }); + if (Hint == end(Parameters_)) return false; Value = Hint->second; return true; } - [[nodiscard]] inline bool HasParameter(const std::string &Name, uint64_t & Value) { - auto Hint = std::find_if(begin(Parameters_),end(Parameters_),[&](const std::pair &S){ return S.first==Name; }); - if(Hint==end(Parameters_)) + [[nodiscard]] inline bool HasParameter(const std::string &Name, uint64_t &Value) { + auto Hint = std::find_if( + begin(Parameters_), end(Parameters_), + [&](const std::pair &S) { return S.first == Name; }); + if (Hint == end(Parameters_)) return false; Value = std::stoull(Hint->second); return true; } - [[nodiscard]] inline const std::string & GetBinding(const std::string &Name, const std::string &Default="") { + [[nodiscard]] inline const std::string &GetBinding(const std::string &Name, + const std::string &Default = "") { auto E = Bindings_.find(Poco::toLower(Name)); if (E == Bindings_.end()) return Default; @@ -236,68 +245,75 @@ namespace OpenWifi { return Return; } - static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, Types::UUIDvec_t & Value) { - if(O->has(Field) && O->isArray(Field)) { + static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, + const std::string &Field, Types::UUIDvec_t &Value) { + if (O->has(Field) && O->isArray(Field)) { auto Arr = O->getArray(Field); - for(const auto &i:*Arr) + for (const auto &i : *Arr) Value.emplace_back(i.toString()); return true; } return false; } - static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, std::string &Value) { - if(O->has(Field)) { + static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, + const std::string &Field, std::string &Value) { + if (O->has(Field)) { Value = O->get(Field).toString(); return true; } return false; } - static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, uint64_t &Value) { - if(O->has(Field)) { + static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, + const std::string &Field, uint64_t &Value) { + if (O->has(Field)) { Value = O->get(Field); return true; } return false; } - static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, bool &Value) { - if(O->has(Field)) { - Value = O->get(Field).toString()=="true"; + static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, + const std::string &Field, bool &Value) { + if (O->has(Field)) { + Value = O->get(Field).toString() == "true"; return true; } return false; } - static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, double &Value) { - if(O->has(Field)) { - Value = (double) O->get(Field); + static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, + const std::string &Field, double &Value) { + if (O->has(Field)) { + Value = (double)O->get(Field); return true; } return false; } - static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, Poco::Data::BLOB &Value) { - if(O->has(Field)) { + static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, + const std::string &Field, Poco::Data::BLOB &Value) { + if (O->has(Field)) { std::string Content = O->get(Field).toString(); auto DecodedBlob = Utils::base64decode(Content); - Value.assignRaw((const unsigned char *)&DecodedBlob[0],DecodedBlob.size()); + Value.assignRaw((const unsigned char *)&DecodedBlob[0], DecodedBlob.size()); return true; } return false; } - - template bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, const T &value, T & assignee) { - if(O->has(Field)) { + template + bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, + const T &value, T &assignee) { + if (O->has(Field)) { assignee = value; return true; } return false; } - inline void SetCommonHeaders(bool CloseConnection=false) { + inline void SetCommonHeaders(bool CloseConnection = false) { Response->setVersion(Poco::Net::HTTPMessage::HTTP_1_1); Response->setChunkedTransferEncoding(true); Response->setContentType("application/json"); @@ -308,7 +324,7 @@ namespace OpenWifi { Response->set("Access-Control-Allow-Origin", "*"); } Response->set("Vary", "Origin, Accept-Encoding"); - if(CloseConnection) { + if (CloseConnection) { Response->set("Connection", "close"); Response->setKeepAlive(false); } else { @@ -329,7 +345,7 @@ namespace OpenWifi { } Response->set("Access-Control-Allow-Methods", MakeList(Methods_)); auto RequestHeaders = Request->find("Access-Control-Request-Headers"); - if(RequestHeaders!=Request->end()) + if (RequestHeaders != Request->end()) Response->set("Access-Control-Allow-Headers", RequestHeaders->second); Response->set("Vary", "Origin, Accept-Encoding"); Response->set("Access-Control-Allow-Credentials", "true"); @@ -342,21 +358,24 @@ namespace OpenWifi { Response->send(); } - inline void PrepareResponse(Poco::Net::HTTPResponse::HTTPStatus Status = Poco::Net::HTTPResponse::HTTP_OK, - bool CloseConnection = false) { + inline void PrepareResponse( + Poco::Net::HTTPResponse::HTTPStatus Status = Poco::Net::HTTPResponse::HTTP_OK, + bool CloseConnection = false) { Response->setStatus(Status); SetCommonHeaders(CloseConnection); } - inline void BadRequest(const OpenWifi::RESTAPI::Errors::msg &E, const std::string & Extra="") { + inline void BadRequest(const OpenWifi::RESTAPI::Errors::msg &E, + const std::string &Extra = "") { PrepareResponse(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST); - Poco::JSON::Object ErrorObject; - ErrorObject.set("ErrorCode",400); - ErrorObject.set("ErrorDetails",Request->getMethod()); - if(Extra.empty()) - ErrorObject.set("ErrorDescription",fmt::format("{}: {}",E.err_num,E.err_txt)) ; + Poco::JSON::Object ErrorObject; + ErrorObject.set("ErrorCode", 400); + ErrorObject.set("ErrorDetails", Request->getMethod()); + if (Extra.empty()) + ErrorObject.set("ErrorDescription", fmt::format("{}: {}", E.err_num, E.err_txt)); else - ErrorObject.set("ErrorDescription",fmt::format("{}: {} ({})",E.err_num,E.err_txt, Extra)) ; + ErrorObject.set("ErrorDescription", + fmt::format("{}: {} ({})", E.err_num, E.err_txt, Extra)); std::ostream &Answer = Response->send(); Poco::JSON::Stringifier::stringify(ErrorObject, Answer); @@ -364,44 +383,43 @@ namespace OpenWifi { inline void InternalError(const OpenWifi::RESTAPI::Errors::msg &E) { PrepareResponse(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); - Poco::JSON::Object ErrorObject; - ErrorObject.set("ErrorCode",500); - ErrorObject.set("ErrorDetails",Request->getMethod()); - ErrorObject.set("ErrorDescription",fmt::format("{}: {}",E.err_num,E.err_txt)) ; + Poco::JSON::Object ErrorObject; + ErrorObject.set("ErrorCode", 500); + ErrorObject.set("ErrorDetails", Request->getMethod()); + ErrorObject.set("ErrorDescription", fmt::format("{}: {}", E.err_num, E.err_txt)); std::ostream &Answer = Response->send(); Poco::JSON::Stringifier::stringify(ErrorObject, Answer); } inline void UnAuthorized(const OpenWifi::RESTAPI::Errors::msg &E) { PrepareResponse(Poco::Net::HTTPResponse::HTTP_FORBIDDEN); - Poco::JSON::Object ErrorObject; - ErrorObject.set("ErrorCode",E.err_num); - ErrorObject.set("ErrorDetails",Request->getMethod()); - ErrorObject.set("ErrorDescription",fmt::format("{}: {}",E.err_num,E.err_txt)) ; + Poco::JSON::Object ErrorObject; + ErrorObject.set("ErrorCode", E.err_num); + ErrorObject.set("ErrorDetails", Request->getMethod()); + ErrorObject.set("ErrorDescription", fmt::format("{}: {}", E.err_num, E.err_txt)); std::ostream &Answer = Response->send(); Poco::JSON::Stringifier::stringify(ErrorObject, Answer); } inline void NotFound() { PrepareResponse(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); - Poco::JSON::Object ErrorObject; - ErrorObject.set("ErrorCode",404); - ErrorObject.set("ErrorDetails",Request->getMethod()); - const auto & E = OpenWifi::RESTAPI::Errors::Error404; - ErrorObject.set("ErrorDescription",fmt::format("{}: {}",E.err_num,E.err_txt)) ; + Poco::JSON::Object ErrorObject; + ErrorObject.set("ErrorCode", 404); + ErrorObject.set("ErrorDetails", Request->getMethod()); + const auto &E = OpenWifi::RESTAPI::Errors::Error404; + ErrorObject.set("ErrorDescription", fmt::format("{}: {}", E.err_num, E.err_txt)); std::ostream &Answer = Response->send(); Poco::JSON::Stringifier::stringify(ErrorObject, Answer); - poco_debug(Logger_,fmt::format("RES-NOTFOUND: User='{}@{}' Method='{}' Path='{}", - Requester(), - Utils::FormatIPv6(Request->clientAddress().toString()), - Request->getMethod(), - Request->getURI())); + poco_debug(Logger_, + fmt::format("RES-NOTFOUND: User='{}@{}' Method='{}' Path='{}", Requester(), + Utils::FormatIPv6(Request->clientAddress().toString()), + Request->getMethod(), Request->getURI())); } inline void OK() { PrepareResponse(); - if( Request->getMethod()==Poco::Net::HTTPRequest::HTTP_DELETE || - Request->getMethod()==Poco::Net::HTTPRequest::HTTP_OPTIONS) { + if (Request->getMethod() == Poco::Net::HTTPRequest::HTTP_DELETE || + Request->getMethod() == Poco::Net::HTTPRequest::HTTP_OPTIONS) { Response->send(); } else { Poco::JSON::Object ErrorObject; @@ -413,85 +431,86 @@ namespace OpenWifi { } } - inline void SendCompressedTarFile(const std::string & FileName, const std::string & Content) { + inline void SendCompressedTarFile(const std::string &FileName, const std::string &Content) { Response->setStatus(Poco::Net::HTTPResponse::HTTPStatus::HTTP_OK); SetCommonHeaders(); - Response->set("Content-Type","application/gzip"); - Response->set("Content-Disposition", "attachment; filename=" + FileName ); - Response->set("Content-Transfer-Encoding","binary"); + Response->set("Content-Type", "application/gzip"); + Response->set("Content-Disposition", "attachment; filename=" + FileName); + Response->set("Content-Transfer-Encoding", "binary"); Response->set("Accept-Ranges", "bytes"); Response->set("Cache-Control", "no-store"); Response->set("Expires", "Mon, 26 Jul 2027 05:00:00 GMT"); Response->setStatus(Poco::Net::HTTPResponse::HTTP_OK); Response->setContentLength(Content.size()); Response->setChunkedTransferEncoding(true); - std::ostream& OutputStream = Response->send(); + std::ostream &OutputStream = Response->send(); OutputStream << Content; } - inline void SendFile(Poco::File & File, const std::string & UUID) { + inline void SendFile(Poco::File &File, const std::string &UUID) { Response->setStatus(Poco::Net::HTTPResponse::HTTPStatus::HTTP_OK); SetCommonHeaders(); - Response->set("Content-Type","application/octet-stream"); - Response->set("Content-Disposition", "attachment; filename=" + UUID ); - Response->set("Content-Transfer-Encoding","binary"); + Response->set("Content-Type", "application/octet-stream"); + Response->set("Content-Disposition", "attachment; filename=" + UUID); + Response->set("Content-Transfer-Encoding", "binary"); Response->set("Accept-Ranges", "bytes"); Response->set("Cache-Control", "no-store"); Response->set("Expires", "Mon, 26 Jul 2027 05:00:00 GMT"); Response->setContentLength(File.getSize()); - Response->sendFile(File.path(),"application/octet-stream"); + Response->sendFile(File.path(), "application/octet-stream"); } - inline void SendFile(Poco::File & File) { + inline void SendFile(Poco::File &File) { Response->setStatus(Poco::Net::HTTPResponse::HTTPStatus::HTTP_OK); SetCommonHeaders(); - Poco::Path P(File.path()); + Poco::Path P(File.path()); auto MT = Utils::FindMediaType(File); - if(MT.Encoding==Utils::BINARY) { - Response->set("Content-Transfer-Encoding","binary"); + if (MT.Encoding == Utils::BINARY) { + Response->set("Content-Transfer-Encoding", "binary"); Response->set("Accept-Ranges", "bytes"); } Response->set("Cache-Control", "no-store"); Response->set("Expires", "Mon, 26 Jul 2027 05:00:00 GMT"); - Response->sendFile(File.path(),MT.ContentType); + Response->sendFile(File.path(), MT.ContentType); } - inline void SendFile(Poco::TemporaryFile &TempAvatar, [[maybe_unused]] const std::string &Type, const std::string & Name) { + inline void SendFile(Poco::TemporaryFile &TempAvatar, + [[maybe_unused]] const std::string &Type, const std::string &Name) { Response->setStatus(Poco::Net::HTTPResponse::HTTPStatus::HTTP_OK); SetCommonHeaders(); auto MT = Utils::FindMediaType(Name); - if(MT.Encoding==Utils::BINARY) { - Response->set("Content-Transfer-Encoding","binary"); + if (MT.Encoding == Utils::BINARY) { + Response->set("Content-Transfer-Encoding", "binary"); Response->set("Accept-Ranges", "bytes"); } - Response->set("Content-Disposition", "attachment; filename=" + Name ); + Response->set("Content-Disposition", "attachment; filename=" + Name); Response->set("Accept-Ranges", "bytes"); Response->set("Cache-Control", "no-store"); Response->set("Expires", "Mon, 26 Jul 2027 05:00:00 GMT"); Response->setContentLength(TempAvatar.getSize()); - Response->sendFile(TempAvatar.path(),MT.ContentType); + Response->sendFile(TempAvatar.path(), MT.ContentType); } - inline void SendFileContent(const std::string &Content, const std::string &Type, const std::string & Name) { + inline void SendFileContent(const std::string &Content, const std::string &Type, + const std::string &Name) { Response->setStatus(Poco::Net::HTTPResponse::HTTPStatus::HTTP_OK); SetCommonHeaders(); auto MT = Utils::FindMediaType(Name); - if(MT.Encoding==Utils::BINARY) { - Response->set("Content-Transfer-Encoding","binary"); + if (MT.Encoding == Utils::BINARY) { + Response->set("Content-Transfer-Encoding", "binary"); Response->set("Accept-Ranges", "bytes"); } - Response->set("Content-Disposition", "attachment; filename=" + Name ); + Response->set("Content-Disposition", "attachment; filename=" + Name); Response->set("Accept-Ranges", "bytes"); Response->set("Cache-Control", "no-store"); Response->set("Expires", "Mon, 26 Jul 2027 05:00:00 GMT"); Response->setContentLength(Content.size()); - Response->setContentType(Type ); - auto & OutputStream = Response->send(); - OutputStream << Content ; + Response->setContentType(Type); + auto &OutputStream = Response->send(); + OutputStream << Content; } - inline void SendHTMLFileBack(Poco::File & File, - const Types::StringPairVec & FormVars) { + inline void SendHTMLFileBack(Poco::File &File, const Types::StringPairVec &FormVars) { Response->setStatus(Poco::Net::HTTPResponse::HTTPStatus::HTTP_OK); SetCommonHeaders(); Response->set("Pragma", "private"); @@ -501,13 +520,14 @@ namespace OpenWifi { Response->setContentLength(FormContent.size()); Response->setChunkedTransferEncoding(true); Response->setContentType("text/html"); - std::ostream& ostr = Response->send(); + std::ostream &ostr = Response->send(); ostr << FormContent; } - inline void ReturnStatus(Poco::Net::HTTPResponse::HTTPStatus Status, bool CloseConnection=false) { + inline void ReturnStatus(Poco::Net::HTTPResponse::HTTPStatus Status, + bool CloseConnection = false) { PrepareResponse(Status, CloseConnection); - if(Status == Poco::Net::HTTPResponse::HTTP_NO_CONTENT) { + if (Status == Poco::Net::HTTPResponse::HTTP_NO_CONTENT) { Response->setContentLength(0); Response->erase("Content-Type"); Response->setChunkedTransferEncoding(false); @@ -519,7 +539,8 @@ namespace OpenWifi { if (Request->getMethod() == Poco::Net::HTTPRequest::HTTP_OPTIONS) { ProcessOptions(); return false; - } else if (std::find(Methods_.begin(), Methods_.end(), Request->getMethod()) == Methods_.end()) { + } else if (std::find(Methods_.begin(), Methods_.end(), Request->getMethod()) == + Methods_.end()) { BadRequest(RESTAPI::Errors::UnsupportedHTTPMethod); return false; } @@ -527,19 +548,20 @@ namespace OpenWifi { return true; } - inline bool IsAuthorized(bool & Expired, bool & Contacted, bool SubOnly = false ); + inline bool IsAuthorized(bool &Expired, bool &Contacted, bool SubOnly = false); inline void ReturnObject(Poco::JSON::Object &Object) { PrepareResponse(); - if(Request!= nullptr) { + if (Request != nullptr) { // can we compress ??? auto AcceptedEncoding = Request->find("Accept-Encoding"); - if(AcceptedEncoding!=Request->end()) { - if( AcceptedEncoding->second.find("gzip")!=std::string::npos || - AcceptedEncoding->second.find("compress")!=std::string::npos) { + if (AcceptedEncoding != Request->end()) { + if (AcceptedEncoding->second.find("gzip") != std::string::npos || + AcceptedEncoding->second.find("compress") != std::string::npos) { Response->set("Content-Encoding", "gzip"); std::ostream &Answer = Response->send(); - Poco::DeflatingOutputStream deflater(Answer, Poco::DeflatingStreamBuf::STREAM_GZIP); + Poco::DeflatingOutputStream deflater(Answer, + Poco::DeflatingStreamBuf::STREAM_GZIP); Poco::JSON::Stringifier::stringify(Object, deflater); deflater.close(); return; @@ -552,74 +574,79 @@ namespace OpenWifi { inline void ReturnRawJSON(const std::string &json_doc) { PrepareResponse(); - if(Request!= nullptr) { + if (Request != nullptr) { // can we compress ??? auto AcceptedEncoding = Request->find("Accept-Encoding"); - if(AcceptedEncoding!=Request->end()) { - if( AcceptedEncoding->second.find("gzip")!=std::string::npos || - AcceptedEncoding->second.find("compress")!=std::string::npos) { + if (AcceptedEncoding != Request->end()) { + if (AcceptedEncoding->second.find("gzip") != std::string::npos || + AcceptedEncoding->second.find("compress") != std::string::npos) { Response->set("Content-Encoding", "gzip"); std::ostream &Answer = Response->send(); - Poco::DeflatingOutputStream deflater(Answer, Poco::DeflatingStreamBuf::STREAM_GZIP); - deflater << json_doc; + Poco::DeflatingOutputStream deflater(Answer, + Poco::DeflatingStreamBuf::STREAM_GZIP); + deflater << json_doc; deflater.close(); return; } } } std::ostream &Answer = Response->send(); - Answer << json_doc; + Answer << json_doc; } inline void ReturnCountOnly(uint64_t Count) { - Poco::JSON::Object Answer; + Poco::JSON::Object Answer; Answer.set("count", Count); ReturnObject(Answer); } inline bool InitQueryBlock() { - if(QueryBlockInitialized_) + if (QueryBlockInitialized_) return true; - QueryBlockInitialized_=true; + QueryBlockInitialized_ = true; QB_.SerialNumber = GetParameter(RESTAPI::Protocol::SERIALNUMBER, ""); QB_.StartDate = GetParameter(RESTAPI::Protocol::STARTDATE, 0); QB_.EndDate = GetParameter(RESTAPI::Protocol::ENDDATE, 0); QB_.Offset = GetParameter(RESTAPI::Protocol::OFFSET, 0); QB_.Limit = GetParameter(RESTAPI::Protocol::LIMIT, 100); QB_.Filter = GetParameter(RESTAPI::Protocol::FILTER, ""); - QB_.Lifetime = GetBoolParameter(RESTAPI::Protocol::LIFETIME,false); - QB_.LogType = GetParameter(RESTAPI::Protocol::LOGTYPE,0); - QB_.LastOnly = GetBoolParameter(RESTAPI::Protocol::LASTONLY,false); - QB_.Newest = GetBoolParameter(RESTAPI::Protocol::NEWEST,false); - QB_.CountOnly = GetBoolParameter(RESTAPI::Protocol::COUNTONLY,false); - QB_.AdditionalInfo = GetBoolParameter(RESTAPI::Protocol::WITHEXTENDEDINFO,false); + QB_.Lifetime = GetBoolParameter(RESTAPI::Protocol::LIFETIME, false); + QB_.LogType = GetParameter(RESTAPI::Protocol::LOGTYPE, 0); + QB_.LastOnly = GetBoolParameter(RESTAPI::Protocol::LASTONLY, false); + QB_.Newest = GetBoolParameter(RESTAPI::Protocol::NEWEST, false); + QB_.CountOnly = GetBoolParameter(RESTAPI::Protocol::COUNTONLY, false); + QB_.AdditionalInfo = GetBoolParameter(RESTAPI::Protocol::WITHEXTENDEDINFO, false); auto RawSelect = GetParameter(RESTAPI::Protocol::SELECT, ""); - auto Entries = Poco::StringTokenizer(RawSelect,","); - for(const auto &i:Entries) { + auto Entries = Poco::StringTokenizer(RawSelect, ","); + for (const auto &i : Entries) { QB_.Select.emplace_back(i); } - if(QB_.Offset<1) - QB_.Offset=0; + if (QB_.Offset < 1) + QB_.Offset = 0; return true; } - [[nodiscard]] inline uint64_t Get(const char *Parameter,const Poco::JSON::Object::Ptr &Obj, uint64_t Default=0){ - if(Obj->has(Parameter)) + [[nodiscard]] inline uint64_t Get(const char *Parameter, const Poco::JSON::Object::Ptr &Obj, + uint64_t Default = 0) { + if (Obj->has(Parameter)) return Obj->get(Parameter); return Default; } - [[nodiscard]] inline std::string GetS(const char *Parameter,const Poco::JSON::Object::Ptr &Obj, const std::string & Default=""){ - if(Obj->has(Parameter)) + [[nodiscard]] inline std::string GetS(const char *Parameter, + const Poco::JSON::Object::Ptr &Obj, + const std::string &Default = "") { + if (Obj->has(Parameter)) return Obj->get(Parameter).toString(); return Default; } - [[nodiscard]] inline bool GetB(const char *Parameter,const Poco::JSON::Object::Ptr &Obj, bool Default=false){ - if(Obj->has(Parameter)) - return Obj->get(Parameter).toString()=="true"; + [[nodiscard]] inline bool GetB(const char *Parameter, const Poco::JSON::Object::Ptr &Obj, + bool Default = false) { + if (Obj->has(Parameter)) + return Obj->get(Parameter).toString() == "true"; return Default; } @@ -627,113 +654,120 @@ namespace OpenWifi { return RESTAPIHandler::Get(RESTAPI::Protocol::WHEN, Obj); } - template void ReturnObject(const char *Name, const std::vector & Objects) { - Poco::JSON::Object Answer; - RESTAPI_utils::field_to_json(Answer,Name,Objects); + template void ReturnObject(const char *Name, const std::vector &Objects) { + Poco::JSON::Object Answer; + RESTAPI_utils::field_to_json(Answer, Name, Objects); ReturnObject(Answer); } - template void Object(const char *Name, const std::vector & Objects) { - Poco::JSON::Object Answer; - RESTAPI_utils::field_to_json(Answer,Name,Objects); + template void Object(const char *Name, const std::vector &Objects) { + Poco::JSON::Object Answer; + RESTAPI_utils::field_to_json(Answer, Name, Objects); ReturnObject(Answer); } template void Object(const T &O) { - Poco::JSON::Object Answer; + Poco::JSON::Object Answer; O.to_json(Answer); ReturnObject(Answer); } - Poco::Logger & Logger() { return Logger_; } + Poco::Logger &Logger() { return Logger_; } - virtual void DoGet() = 0 ; - virtual void DoDelete() = 0 ; - virtual void DoPost() = 0 ; - virtual void DoPut() = 0 ; + virtual void DoGet() = 0; + virtual void DoDelete() = 0; + virtual void DoPost() = 0; + virtual void DoPut() = 0; + + Poco::Net::HTTPServerRequest *Request = nullptr; + Poco::Net::HTTPServerResponse *Response = nullptr; + SecurityObjects::UserInfoAndPolicy UserInfo_; + QueryBlock QB_; + const std::string &Requester() const { return REST_Requester_; } - Poco::Net::HTTPServerRequest *Request= nullptr; - Poco::Net::HTTPServerResponse *Response= nullptr; - SecurityObjects::UserInfoAndPolicy UserInfo_; - QueryBlock QB_; - const std::string & Requester() const { return REST_Requester_; } protected: - BindingMap Bindings_; - Poco::URI::QueryParameters Parameters_; - Poco::Logger &Logger_; - std::string SessionToken_; - std::vector Methods_; - bool Internal_=false; - bool RateLimited_=false; - bool QueryBlockInitialized_=false; - bool SubOnlyService_=false; - bool AlwaysAuthorize_=true; - Poco::JSON::Parser IncomingParser_; - RESTAPI_GenericServerAccounting & Server_; - RateLimit MyRates_; - uint64_t TransactionId_; - Poco::JSON::Object::Ptr ParsedBody_; - std::string REST_Requester_; + BindingMap Bindings_; + Poco::URI::QueryParameters Parameters_; + Poco::Logger &Logger_; + std::string SessionToken_; + std::vector Methods_; + bool Internal_ = false; + bool RateLimited_ = false; + bool QueryBlockInitialized_ = false; + bool SubOnlyService_ = false; + bool AlwaysAuthorize_ = true; + Poco::JSON::Parser IncomingParser_; + RESTAPI_GenericServerAccounting &Server_; + RateLimit MyRates_; + uint64_t TransactionId_; + Poco::JSON::Object::Ptr ParsedBody_; + std::string REST_Requester_; }; -#ifdef TIP_SECURITY_SERVICE - [[nodiscard]] bool AuthServiceIsAuthorized(Poco::Net::HTTPServerRequest & Request,std::string &SessionToken, - SecurityObjects::UserInfoAndPolicy & UInfo, std::uint64_t TID, bool & Expired , bool Sub ); +#ifdef TIP_SECURITY_SERVICE + [[nodiscard]] bool AuthServiceIsAuthorized(Poco::Net::HTTPServerRequest &Request, + std::string &SessionToken, + SecurityObjects::UserInfoAndPolicy &UInfo, + std::uint64_t TID, bool &Expired, bool Sub); #endif - inline bool RESTAPIHandler::IsAuthorized( bool & Expired , [[maybe_unused]] bool & Contacted , bool Sub ) { - if(Internal_ && Request->has("X-INTERNAL-NAME")) { + inline bool RESTAPIHandler::IsAuthorized(bool &Expired, [[maybe_unused]] bool &Contacted, + bool Sub) { + if (Internal_ && Request->has("X-INTERNAL-NAME")) { auto Allowed = MicroServiceIsValidAPIKEY(*Request); Contacted = true; - if(!Allowed) { - if(Server_.LogBadTokens(false)) { - poco_debug(Logger_,fmt::format("I-REQ-DENIED({}): TID={} Method={} Path={}", - Utils::FormatIPv6(Request->clientAddress().toString()), - TransactionId_, - Request->getMethod(), Request->getURI())); + if (!Allowed) { + if (Server_.LogBadTokens(false)) { + poco_debug(Logger_, + fmt::format("I-REQ-DENIED({}): TID={} Method={} Path={}", + Utils::FormatIPv6(Request->clientAddress().toString()), + TransactionId_, Request->getMethod(), + Request->getURI())); } } else { auto Id = Request->get("X-INTERNAL-NAME", "unknown"); REST_Requester_ = Id; - if(Server_.LogIt(Request->getMethod(),true)) { - poco_debug(Logger_,fmt::format("I-REQ-ALLOWED({}): TID={} User='{}' Method={} Path={}", - Utils::FormatIPv6(Request->clientAddress().toString()), - TransactionId_, - Id, - Request->getMethod(), Request->getURI())); + if (Server_.LogIt(Request->getMethod(), true)) { + poco_debug(Logger_, + fmt::format("I-REQ-ALLOWED({}): TID={} User='{}' Method={} Path={}", + Utils::FormatIPv6(Request->clientAddress().toString()), + TransactionId_, Id, Request->getMethod(), + Request->getURI())); } } return Allowed; - } else if(!Internal_ && Request->has("X-API-KEY")) { - SessionToken_ = Request->get("X-API-KEY", ""); - bool suspended=false; -#ifdef TIP_SECURITY_SERVICE - std::uint64_t expiresOn; - if (AuthService()->IsValidApiKey(SessionToken_, UserInfo_.webtoken, UserInfo_.userinfo, Expired, expiresOn, suspended)) { + } else if (!Internal_ && Request->has("X-API-KEY")) { + SessionToken_ = Request->get("X-API-KEY", ""); + bool suspended = false; +#ifdef TIP_SECURITY_SERVICE + std::uint64_t expiresOn; + if (AuthService()->IsValidApiKey(SessionToken_, UserInfo_.webtoken, UserInfo_.userinfo, + Expired, expiresOn, suspended)) { #else - if (AuthClient()->IsValidApiKey( SessionToken_, UserInfo_, TransactionId_, Expired, Contacted, suspended)) { + if (AuthClient()->IsValidApiKey(SessionToken_, UserInfo_, TransactionId_, Expired, + Contacted, suspended)) { #endif - REST_Requester_ = UserInfo_.userinfo.email; - if(Server_.LogIt(Request->getMethod(),true)) { - poco_debug(Logger_,fmt::format("X-REQ-ALLOWED({}): APIKEY-ACCESS TID={} User='{}@{}' Method={} Path={}", - UserInfo_.userinfo.email, - TransactionId_, - Utils::FormatIPv6(Request->clientAddress().toString()), - Request->clientAddress().toString(), - Request->getMethod(), - Request->getURI())); - } - return true; - } else { - if(Server_.LogBadTokens(true)) { - poco_debug(Logger_,fmt::format("X-REQ-DENIED({}): TID={} Method={} Path={}", - Utils::FormatIPv6(Request->clientAddress().toString()), - TransactionId_, - Request->getMethod(), - Request->getURI())); - } - } - return false; - } else { + REST_Requester_ = UserInfo_.userinfo.email; + if (Server_.LogIt(Request->getMethod(), true)) { + poco_debug(Logger_, + fmt::format("X-REQ-ALLOWED({}): APIKEY-ACCESS TID={} User='{}@{}' " + "Method={} Path={}", + UserInfo_.userinfo.email, TransactionId_, + Utils::FormatIPv6(Request->clientAddress().toString()), + Request->clientAddress().toString(), + Request->getMethod(), Request->getURI())); + } + return true; + } else { + if (Server_.LogBadTokens(true)) { + poco_debug(Logger_, + fmt::format("X-REQ-DENIED({}): TID={} Method={} Path={}", + Utils::FormatIPv6(Request->clientAddress().toString()), + TransactionId_, Request->getMethod(), + Request->getURI())); + } + } + return false; + } else { if (SessionToken_.empty()) { try { Poco::Net::OAuth20Credentials Auth(*Request); @@ -744,29 +778,31 @@ namespace OpenWifi { Logger_.log(E); } } -#ifdef TIP_SECURITY_SERVICE - if (AuthServiceIsAuthorized(*Request, SessionToken_, UserInfo_, TransactionId_, Expired, Sub)) { +#ifdef TIP_SECURITY_SERVICE + if (AuthServiceIsAuthorized(*Request, SessionToken_, UserInfo_, TransactionId_, Expired, + Sub)) { #else - if (AuthClient()->IsAuthorized( SessionToken_, UserInfo_, TransactionId_, Expired, Contacted, Sub)) { + if (AuthClient()->IsAuthorized(SessionToken_, UserInfo_, TransactionId_, Expired, + Contacted, Sub)) { #endif REST_Requester_ = UserInfo_.userinfo.email; - if(Server_.LogIt(Request->getMethod(),true)) { - poco_debug(Logger_,fmt::format("X-REQ-ALLOWED({}): TID={} User='{}@{}' Method={} Path={}", - UserInfo_.userinfo.email, - TransactionId_, - Utils::FormatIPv6(Request->clientAddress().toString()), - Request->clientAddress().toString(), - Request->getMethod(), - Request->getURI())); + if (Server_.LogIt(Request->getMethod(), true)) { + poco_debug( + Logger_, + fmt::format("X-REQ-ALLOWED({}): TID={} User='{}@{}' Method={} Path={}", + UserInfo_.userinfo.email, TransactionId_, + Utils::FormatIPv6(Request->clientAddress().toString()), + Request->clientAddress().toString(), Request->getMethod(), + Request->getURI())); } return true; } else { - if(Server_.LogBadTokens(true)) { - poco_debug(Logger_,fmt::format("X-REQ-DENIED({}): TID={} Method={} Path={}", - Utils::FormatIPv6(Request->clientAddress().toString()), - TransactionId_, - Request->getMethod(), - Request->getURI())); + if (Server_.LogBadTokens(true)) { + poco_debug(Logger_, + fmt::format("X-REQ-DENIED({}): TID={} Method={} Path={}", + Utils::FormatIPv6(Request->clientAddress().toString()), + TransactionId_, Request->getMethod(), + Request->getURI())); } } return false; @@ -775,53 +811,59 @@ namespace OpenWifi { class RESTAPI_UnknownRequestHandler : public RESTAPIHandler { public: - RESTAPI_UnknownRequestHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId, bool Internal) - : RESTAPIHandler(bindings, L, std::vector{}, Server, TransactionId, Internal) {} - inline void DoGet() override {}; - inline void DoPost() override {}; - inline void DoPut() override {}; - inline void DoDelete() override {}; + RESTAPI_UnknownRequestHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, + uint64_t TransactionId, bool Internal) + : RESTAPIHandler(bindings, L, std::vector{}, Server, TransactionId, + Internal) {} + inline void DoGet() override{}; + inline void DoPost() override{}; + inline void DoPut() override{}; + inline void DoDelete() override{}; }; - template - constexpr auto test_has_PathName_method(T*) - -> decltype( T::PathName() , std::true_type{} ) - { - return std::true_type{}; - } - constexpr auto test_has_PathName_method(...) -> std::false_type - { - return std::false_type{}; + template + constexpr auto test_has_PathName_method(T *) -> decltype(T::PathName(), std::true_type{}) { + return std::true_type{}; + } + constexpr auto test_has_PathName_method(...) -> std::false_type { return std::false_type{}; } + + template + RESTAPIHandler *RESTAPI_Router(const std::string &RequestedPath, + RESTAPIHandler::BindingMap &Bindings, Poco::Logger &Logger, + RESTAPI_GenericServerAccounting &Server, + uint64_t TransactionId) { + static_assert(test_has_PathName_method((T *)nullptr), + "Class must have a static PathName() method."); + if (RESTAPIHandler::ParseBindings(RequestedPath, T::PathName(), Bindings)) { + return new T(Bindings, Logger, Server, TransactionId, false); } - template - RESTAPIHandler * RESTAPI_Router(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, - Poco::Logger & Logger, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId) { - static_assert(test_has_PathName_method((T*)nullptr), "Class must have a static PathName() method."); - if(RESTAPIHandler::ParseBindings(RequestedPath,T::PathName(),Bindings)) { - return new T(Bindings, Logger, Server, TransactionId, false); - } + if constexpr (sizeof...(Args) == 0) { + return new RESTAPI_UnknownRequestHandler(Bindings, Logger, Server, TransactionId, + false); + } else { + return RESTAPI_Router(RequestedPath, Bindings, Logger, Server, TransactionId); + } + } - if constexpr (sizeof...(Args) == 0) { - return new RESTAPI_UnknownRequestHandler(Bindings,Logger, Server, TransactionId, false); - } else { - return RESTAPI_Router(RequestedPath, Bindings, Logger, Server, TransactionId); - } + template + RESTAPIHandler *RESTAPI_Router_I(const std::string &RequestedPath, + RESTAPIHandler::BindingMap &Bindings, Poco::Logger &Logger, + RESTAPI_GenericServerAccounting &Server, + uint64_t TransactionId) { + static_assert(test_has_PathName_method((T *)nullptr), + "Class must have a static PathName() method."); + if (RESTAPIHandler::ParseBindings(RequestedPath, T::PathName(), Bindings)) { + return new T(Bindings, Logger, Server, TransactionId, true); } - template - RESTAPIHandler * RESTAPI_Router_I(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, - Poco::Logger & Logger, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId) { - static_assert(test_has_PathName_method((T*)nullptr), "Class must have a static PathName() method."); - if(RESTAPIHandler::ParseBindings(RequestedPath,T::PathName(),Bindings)) { - return new T(Bindings, Logger, Server, TransactionId, true ); - } - - if constexpr (sizeof...(Args) == 0) { - return new RESTAPI_UnknownRequestHandler(Bindings,Logger, Server, TransactionId, true); - } else { - return RESTAPI_Router_I(RequestedPath, Bindings, Logger, Server, TransactionId); - } + if constexpr (sizeof...(Args) == 0) { + return new RESTAPI_UnknownRequestHandler(Bindings, Logger, Server, TransactionId, true); + } else { + return RESTAPI_Router_I(RequestedPath, Bindings, Logger, Server, + TransactionId); } + } } // namespace OpenWifi diff --git a/src/framework/RESTAPI_IntServer.h b/src/framework/RESTAPI_IntServer.h index 4926771..6270b95 100644 --- a/src/framework/RESTAPI_IntServer.h +++ b/src/framework/RESTAPI_IntServer.h @@ -6,20 +6,23 @@ #include "Poco/Net/HTTPServer.h" -#include "framework/SubSystemServer.h" #include "framework/RESTAPI_Handler.h" +#include "framework/SubSystemServer.h" namespace OpenWifi { - Poco::Net::HTTPRequestHandler * RESTAPI_IntRouter(const std::string &Path, RESTAPIHandler::BindingMap &Bindings, - Poco::Logger & L, RESTAPI_GenericServerAccounting & S, uint64_t Id); + Poco::Net::HTTPRequestHandler * + RESTAPI_IntRouter(const std::string &Path, RESTAPIHandler::BindingMap &Bindings, + Poco::Logger &L, RESTAPI_GenericServerAccounting &S, uint64_t Id); class IntRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { public: inline IntRequestHandlerFactory() = default; - Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &Request) override; + Poco::Net::HTTPRequestHandler * + createRequestHandler(const Poco::Net::HTTPServerRequest &Request) override; + private: - static inline std::atomic_uint64_t NextTransactionId_ = 1; + static inline std::atomic_uint64_t NextTransactionId_ = 1; }; class RESTAPI_IntServer : public SubSystemServer { @@ -30,16 +33,21 @@ namespace OpenWifi { } inline int Start() override { - poco_information(Logger(),"Starting."); + poco_information(Logger(), "Starting."); Server_.InitLogging(); - for(const auto & Svr: ConfigServersList_) { + for (const auto &Svr : ConfigServersList_) { - if(MicroServiceNoAPISecurity()) { - poco_information(Logger(),fmt::format("Starting: {}:{}. Security has been disabled for APIs.", Svr.Address(), Svr.Port())); + if (MicroServiceNoAPISecurity()) { + poco_information( + Logger(), + fmt::format("Starting: {}:{}. Security has been disabled for APIs.", + Svr.Address(), Svr.Port())); } else { - poco_information(Logger(),fmt::format("Starting: {}:{}. Keyfile:{} CertFile: {}", Svr.Address(), Svr.Port(), - Svr.KeyFile(),Svr.CertFile())); + poco_information(Logger(), + fmt::format("Starting: {}:{}. Keyfile:{} CertFile: {}", + Svr.Address(), Svr.Port(), Svr.KeyFile(), + Svr.CertFile())); Svr.LogCert(Logger()); if (!Svr.RootCA().empty()) Svr.LogCas(Logger()); @@ -49,13 +57,15 @@ namespace OpenWifi { Params->setKeepAlive(true); Params->setName("ws:irest"); - std::unique_ptr NewServer; - if(MicroServiceNoAPISecurity()) { + std::unique_ptr NewServer; + if (MicroServiceNoAPISecurity()) { auto Sock{Svr.CreateSocket(Logger())}; - NewServer = std::make_unique(new IntRequestHandlerFactory, Pool_, Sock, Params); + NewServer = std::make_unique( + new IntRequestHandlerFactory, Pool_, Sock, Params); } else { auto Sock{Svr.CreateSecureSocket(Logger())}; - NewServer = std::make_unique(new IntRequestHandlerFactory, Pool_, Sock, Params); + NewServer = std::make_unique( + new IntRequestHandlerFactory, Pool_, Sock, Params); }; NewServer->start(); RESTServers_.push_back(std::move(NewServer)); @@ -65,40 +75,37 @@ namespace OpenWifi { } inline void Stop() override { - poco_information(Logger(),"Stopping..."); - for( const auto & svr : RESTServers_ ) + poco_information(Logger(), "Stopping..."); + for (const auto &svr : RESTServers_) svr->stopAll(true); Pool_.stopAll(); Pool_.joinAll(); - poco_information(Logger(),"Stopped..."); + poco_information(Logger(), "Stopped..."); } inline void reinitialize([[maybe_unused]] Poco::Util::Application &self) override { MicroServiceLoadConfigurationFile(); - poco_information(Logger(),"Reinitializing."); + poco_information(Logger(), "Reinitializing."); Stop(); Start(); } inline Poco::Net::HTTPRequestHandler *CallServer(const std::string &Path, uint64_t Id) { RESTAPIHandler::BindingMap Bindings; - Utils::SetThreadName(fmt::format("i-rest:{}",Id).c_str()); + Utils::SetThreadName(fmt::format("i-rest:{}", Id).c_str()); return RESTAPI_IntRouter(Path, Bindings, Logger(), Server_, Id); } - const Poco::ThreadPool & Pool() { return Pool_; } - private: - std::vector> RESTServers_; - Poco::ThreadPool Pool_{"i-rest",4,64}; - RESTAPI_GenericServerAccounting Server_; + const Poco::ThreadPool &Pool() { return Pool_; } - RESTAPI_IntServer() noexcept: - SubSystemServer("RESTAPI_IntServer", "REST-ISRV", "openwifi.internal.restapi") - { - } + private: + std::vector> RESTServers_; + Poco::ThreadPool Pool_{"i-rest", 4, 64}; + RESTAPI_GenericServerAccounting Server_; + + RESTAPI_IntServer() noexcept + : SubSystemServer("RESTAPI_IntServer", "REST-ISRV", "openwifi.internal.restapi") {} }; inline auto RESTAPI_IntServer() { return RESTAPI_IntServer::instance(); }; - } // namespace OpenWifi - diff --git a/src/framework/RESTAPI_PartHandler.h b/src/framework/RESTAPI_PartHandler.h index 4e0f2f0..c466102 100644 --- a/src/framework/RESTAPI_PartHandler.h +++ b/src/framework/RESTAPI_PartHandler.h @@ -4,29 +4,26 @@ #pragma once -#include -#include "Poco/Net/PartHandler.h" -#include "Poco/Net/MessageHeader.h" #include "Poco/CountingStream.h" +#include "Poco/Net/MessageHeader.h" +#include "Poco/Net/PartHandler.h" #include "Poco/NullStream.h" #include "Poco/StreamCopier.h" +#include namespace OpenWifi { - class RESTAPI_PartHandler: public Poco::Net::PartHandler { + class RESTAPI_PartHandler : public Poco::Net::PartHandler { public: - RESTAPI_PartHandler(): - _length(0) - { - } + RESTAPI_PartHandler() : _length(0) {} - inline void handlePart(const Poco::Net::MessageHeader& header, std::istream& stream) override - { + inline void handlePart(const Poco::Net::MessageHeader &header, + std::istream &stream) override { _type = header.get("Content-Type", "(unspecified)"); - if (header.has("Content-Disposition")) - { + if (header.has("Content-Disposition")) { std::string disp; Poco::Net::NameValueCollection params; - Poco::Net::MessageHeader::splitParameters(header["Content-Disposition"], disp, params); + Poco::Net::MessageHeader::splitParameters(header["Content-Disposition"], disp, + params); _name = params.get("name", "(unnamed)"); _fileName = params.get("filename", "(unnamed)"); } @@ -37,25 +34,13 @@ namespace OpenWifi { _length = (int)istr.chars(); } - [[nodiscard]] inline int length() const - { - return _length; - } + [[nodiscard]] inline int length() const { return _length; } - [[nodiscard]] inline const std::string& name() const - { - return _name; - } + [[nodiscard]] inline const std::string &name() const { return _name; } - [[nodiscard]] inline const std::string& fileName() const - { - return _fileName; - } + [[nodiscard]] inline const std::string &fileName() const { return _fileName; } - [[nodiscard]] inline const std::string& contentType() const - { - return _type; - } + [[nodiscard]] inline const std::string &contentType() const { return _type; } private: int _length; @@ -63,4 +48,4 @@ namespace OpenWifi { std::string _name; std::string _fileName; }; -} +} // namespace OpenWifi diff --git a/src/framework/RESTAPI_RateLimiter.h b/src/framework/RESTAPI_RateLimiter.h index 954227e..d55ff07 100644 --- a/src/framework/RESTAPI_RateLimiter.h +++ b/src/framework/RESTAPI_RateLimiter.h @@ -6,9 +6,9 @@ #include "framework/SubSystemServer.h" -#include "Poco/URI.h" -#include "Poco/Net/HTTPServerRequest.h" #include "Poco/ExpireLRUCache.h" +#include "Poco/Net/HTTPServerRequest.h" +#include "Poco/URI.h" #include "fmt/format.h" @@ -16,10 +16,9 @@ namespace OpenWifi { class RESTAPI_RateLimiter : public SubSystemServer { public: - struct ClientCacheEntry { - int64_t Start=0; - int Count=0; + int64_t Start = 0; + int Count = 0; }; static auto instance() { @@ -27,49 +26,47 @@ namespace OpenWifi { return instance_; } - inline int Start() final { return 0;}; - inline void Stop() final { }; + inline int Start() final { return 0; }; + inline void Stop() final{}; - inline bool IsRateLimited(const Poco::Net::HTTPServerRequest &R, int64_t Period, int64_t MaxCalls) { - Poco::URI uri(R.getURI()); + inline bool IsRateLimited(const Poco::Net::HTTPServerRequest &R, int64_t Period, + int64_t MaxCalls) { + Poco::URI uri(R.getURI()); auto H = str_hash(uri.getPath() + R.clientAddress().host().toString()); auto E = Cache_.get(H); - auto Now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); - if(E.isNull()) { - Cache_.add(H,ClientCacheEntry{.Start=Now, .Count=1}); + auto Now = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); + if (E.isNull()) { + Cache_.add(H, ClientCacheEntry{.Start = Now, .Count = 1}); return false; } - if((Now-E->Start)Start) < Period) { E->Count++; - Cache_.update(H,E); - if(E->Count > MaxCalls) { - poco_warning(Logger(),fmt::format("RATE-LIMIT-EXCEEDED: from '{}'", R.clientAddress().toString())); + Cache_.update(H, E); + if (E->Count > MaxCalls) { + poco_warning(Logger(), fmt::format("RATE-LIMIT-EXCEEDED: from '{}'", + R.clientAddress().toString())); return true; } return false; } E->Start = Now; E->Count = 1; - Cache_.update(H,E); + Cache_.update(H, E); return false; } - inline void Clear() { - Cache_.clear(); - } + inline void Clear() { Cache_.clear(); } private: - Poco::ExpireLRUCache Cache_{2048}; - std::hash str_hash; - - RESTAPI_RateLimiter() noexcept: - SubSystemServer("RateLimiter", "RATE-LIMITER", "rate.limiter") - { - } + Poco::ExpireLRUCache Cache_{2048}; + std::hash str_hash; + RESTAPI_RateLimiter() noexcept + : SubSystemServer("RateLimiter", "RATE-LIMITER", "rate.limiter") {} }; inline auto RESTAPI_RateLimiter() { return RESTAPI_RateLimiter::instance(); } - -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/RESTAPI_SystemCommand.h b/src/framework/RESTAPI_SystemCommand.h index 9ce9691..f86f57d 100644 --- a/src/framework/RESTAPI_SystemCommand.h +++ b/src/framework/RESTAPI_SystemCommand.h @@ -14,19 +14,19 @@ namespace OpenWifi { class RESTAPI_system_command : public RESTAPIHandler { public: - RESTAPI_system_command(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting & Server, uint64_t TransactionId, bool Internal) + RESTAPI_system_command(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, + bool Internal) : RESTAPIHandler(bindings, L, std::vector{Poco::Net::HTTPRequest::HTTP_POST, Poco::Net::HTTPRequest::HTTP_GET, Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Server, - TransactionId, - Internal) {} - static auto PathName() { return std::list{"/api/v1/system"};} + Server, TransactionId, Internal) {} + static auto PathName() { return std::list{"/api/v1/system"}; } inline void DoGet() { std::string Arg; - if(HasParameter("command",Arg) && Arg=="info") { + if (HasParameter("command", Arg) && Arg == "info") { Poco::JSON::Object Answer; Answer.set(RESTAPI::Protocol::VERSION, MicroServiceVersion()); Answer.set(RESTAPI::Protocol::UPTIME, MicroServiceUptimeTotalSeconds()); @@ -36,19 +36,19 @@ namespace OpenWifi { Answer.set(RESTAPI::Protocol::HOSTNAME, Poco::Environment::nodeName()); Answer.set(RESTAPI::Protocol::UI, MicroServiceGetUIURI()); - Poco::JSON::Array Certificates; + Poco::JSON::Array Certificates; auto SubSystems = MicroServiceGetFullSubSystems(); - std::set CertNames; + std::set CertNames; - for(const auto &i:SubSystems) { - auto Hosts=i->HostSize(); - for(uint64_t j=0;jHostSize(); + for (uint64_t j = 0; j < Hosts; ++j) { auto CertFileName = i->Host(j).CertFile(); - if(!CertFileName.empty()) { - Poco::File F1(CertFileName); - if(F1.exists()) { + if (!CertFileName.empty()) { + Poco::File F1(CertFileName); + if (F1.exists()) { auto InsertResult = CertNames.insert(CertFileName); - if(InsertResult.second) { + if (InsertResult.second) { Poco::JSON::Object Inner; Poco::Path F(CertFileName); Inner.set("filename", F.getFileName()); @@ -64,8 +64,8 @@ namespace OpenWifi { Answer.set("certificates", Certificates); return ReturnObject(Answer); } - if(GetBoolParameter("extraConfiguration")) { - Poco::JSON::Object Answer; + if (GetBoolParameter("extraConfiguration")) { + Poco::JSON::Object Answer; MicroServiceGetExtraConfiguration(Answer); return ReturnObject(Answer); } @@ -73,7 +73,7 @@ namespace OpenWifi { } inline void DoPost() final { - const auto & Obj = ParsedBody_; + const auto &Obj = ParsedBody_; if (Obj->has(RESTAPI::Protocol::COMMAND)) { auto Command = Poco::toLower(Obj->get(RESTAPI::Protocol::COMMAND).toString()); if (Command == RESTAPI::Protocol::SETLOGLEVEL) { @@ -88,7 +88,8 @@ namespace OpenWifi { auto Name = GetS(RESTAPI::Protocol::TAG, InnerObj); auto Value = GetS(RESTAPI::Protocol::VALUE, InnerObj); MicroServiceSetSubsystemLogLevel(Name, Value); - poco_information(Logger_, + poco_information( + Logger_, fmt::format("Setting log level for {} at {}", Name, Value)); } } @@ -109,7 +110,7 @@ namespace OpenWifi { } else if (Command == RESTAPI::Protocol::GETLOGLEVELNAMES) { Poco::JSON::Object Result; Poco::JSON::Array LevelNamesArray; - const Types::StringVec &LevelNames = MicroServiceGetLogLevelNames(); + const Types::StringVec &LevelNames = MicroServiceGetLogLevelNames(); for (const auto &i : LevelNames) LevelNamesArray.add(i); Result.set(RESTAPI::Protocol::LIST, LevelNamesArray); @@ -117,7 +118,7 @@ namespace OpenWifi { } else if (Command == RESTAPI::Protocol::GETSUBSYSTEMNAMES) { Poco::JSON::Object Result; Poco::JSON::Array LevelNamesArray; - const Types::StringVec &SubSystemNames = MicroServiceGetSubSystems(); + const Types::StringVec &SubSystemNames = MicroServiceGetSubSystems(); for (const auto &i : SubSystemNames) LevelNamesArray.add(i); Result.set(RESTAPI::Protocol::LIST, LevelNamesArray); @@ -131,10 +132,10 @@ namespace OpenWifi { std::vector Names; for (const auto &i : *SubSystems) Names.push_back(i.toString()); - std::thread ReloadThread([Names](){ + std::thread ReloadThread([Names]() { std::this_thread::sleep_for(10000ms); - for(const auto &i:Names) { - if(i=="daemon") + for (const auto &i : Names) { + if (i == "daemon") MicroServiceReload(); else MicroServiceReload(i); @@ -150,8 +151,8 @@ namespace OpenWifi { BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); } - void DoPut() final {}; - void DoDelete() final {}; + void DoPut() final{}; + void DoDelete() final{}; }; -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/RESTAPI_SystemConfiguration.h b/src/framework/RESTAPI_SystemConfiguration.h index c75be97..d148784 100644 --- a/src/framework/RESTAPI_SystemConfiguration.h +++ b/src/framework/RESTAPI_SystemConfiguration.h @@ -4,8 +4,8 @@ #pragma once -#include "framework/RESTAPI_Handler.h" #include "framework/MicroServiceFuncs.h" +#include "framework/RESTAPI_Handler.h" using namespace std::chrono_literals; @@ -14,8 +14,8 @@ namespace OpenWifi { class RESTAPI_system_configuration : public RESTAPIHandler { public: RESTAPI_system_configuration(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, - RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, - bool Internal) + RESTAPI_GenericServerAccounting &Server, + uint64_t TransactionId, bool Internal) : RESTAPIHandler(bindings, L, std::vector{Poco::Net::HTTPRequest::HTTP_PUT, Poco::Net::HTTPRequest::HTTP_GET, @@ -27,21 +27,18 @@ namespace OpenWifi { inline void DoPost() final {} - inline void DoGet() final { + inline void DoGet() final { return OK(); } - return OK(); - } - - inline void DoPut() final{ - if(UserInfo_.userinfo.userRole!=SecurityObjects::ROOT) { + inline void DoPut() final { + if (UserInfo_.userinfo.userRole != SecurityObjects::ROOT) { return UnAuthorized(RESTAPI::Errors::ACCESS_DENIED); } return OK(); }; - inline void DoDelete() final{ - if(UserInfo_.userinfo.userRole!=SecurityObjects::ROOT) { + inline void DoDelete() final { + if (UserInfo_.userinfo.userRole != SecurityObjects::ROOT) { return UnAuthorized(RESTAPI::Errors::ACCESS_DENIED); } MicroServiceDeleteOverrideConfiguration(); @@ -49,4 +46,4 @@ namespace OpenWifi { }; }; -} +} // namespace OpenWifi diff --git a/src/framework/RESTAPI_WebSocketServer.h b/src/framework/RESTAPI_WebSocketServer.h index 294c52f..5a4bc99 100644 --- a/src/framework/RESTAPI_WebSocketServer.h +++ b/src/framework/RESTAPI_WebSocketServer.h @@ -4,42 +4,44 @@ #pragma once -#include "framework/RESTAPI_Handler.h" #include "Poco/Net/WebSocket.h" +#include "framework/RESTAPI_Handler.h" -#include "framework/UI_WebSocketClientServer.h" #include "framework/MicroServiceFuncs.h" +#include "framework/UI_WebSocketClientServer.h" namespace OpenWifi { class RESTAPI_webSocketServer : public RESTAPIHandler { public: - inline RESTAPI_webSocketServer(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServerAccounting &Server, uint64_t TransactionId, bool Internal) + inline RESTAPI_webSocketServer(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, + RESTAPI_GenericServerAccounting &Server, + uint64_t TransactionId, bool Internal) : RESTAPIHandler(bindings, L, - std::vector{ Poco::Net::HTTPRequest::HTTP_GET, - Poco::Net::HTTPRequest::HTTP_OPTIONS}, - Server, TransactionId, Internal,false) {} - static auto PathName() { return std::list{"/api/v1/ws"};} + std::vector{Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_OPTIONS}, + Server, TransactionId, Internal, false) {} + static auto PathName() { return std::list{"/api/v1/ws"}; } void DoGet() final { - try - { - if(Request->find("Upgrade") != Request->end() && Poco::icompare((*Request)["Upgrade"], "websocket") == 0) { - try - { - Poco::Net::WebSocket WS(*Request, *Response); - auto Id = MicroServiceCreateUUID(); - UI_WebSocketClientServer()->NewClient(WS,Id,UserInfo_.userinfo.email, TransactionId_); - } - catch (...) { - std::cout << "Cannot create websocket client..." << std::endl; - } + try { + if (Request->find("Upgrade") != Request->end() && + Poco::icompare((*Request)["Upgrade"], "websocket") == 0) { + try { + Poco::Net::WebSocket WS(*Request, *Response); + auto Id = MicroServiceCreateUUID(); + UI_WebSocketClientServer()->NewClient(WS, Id, UserInfo_.userinfo.email, + TransactionId_); + } catch (...) { + std::cout << "Cannot create websocket client..." << std::endl; } - } catch(...) { - std::cout << "Cannot upgrade connection..." << std::endl; } - }; - void DoDelete() final {}; - void DoPost() final {}; - void DoPut() final {}; + } catch (...) { + std::cout << "Cannot upgrade connection..." << std::endl; + } + }; + void DoDelete() final{}; + void DoPost() final{}; + void DoPut() final{}; + private: }; -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/RESTAPI_utils.h b/src/framework/RESTAPI_utils.h index 566b20f..a3eca9d 100644 --- a/src/framework/RESTAPI_utils.h +++ b/src/framework/RESTAPI_utils.h @@ -6,9 +6,9 @@ #include +#include "Poco/Data/LOB.h" #include "Poco/JSON/Object.h" #include "Poco/JSON/Parser.h" -#include "Poco/Data/LOB.h" #include "Poco/Net/HTTPServerRequest.h" #include "framework/OpenWifiTypes.h" @@ -16,7 +16,8 @@ namespace OpenWifi::RESTAPI_utils { - inline void EmbedDocument(const std::string & ObjName, Poco::JSON::Object & Obj, const std::string &ObjStr) { + inline void EmbedDocument(const std::string &ObjName, Poco::JSON::Object &Obj, + const std::string &ObjStr) { std::string D = ObjStr.empty() ? "{}" : ObjStr; Poco::JSON::Parser P; Poco::Dynamic::Var result = P.parse(D); @@ -25,23 +26,23 @@ namespace OpenWifi::RESTAPI_utils { } inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, bool V) { - Obj.set(Field,V); + Obj.set(Field, V); } inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, double V) { - Obj.set(Field,V); + Obj.set(Field, V); } inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, float V) { - Obj.set(Field,V); + Obj.set(Field, V); } - inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const std::string & S) { - Obj.set(Field,S); + inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const std::string &S) { + Obj.set(Field, S); } - inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const char * S) { - Obj.set(Field,S); + inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const char *S) { + Obj.set(Field, S); } inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, int16_t Value) { @@ -65,87 +66,93 @@ namespace OpenWifi::RESTAPI_utils { } inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, uint64_t Value) { - Obj.set(Field,Value); + Obj.set(Field, Value); } - inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Poco::Data::BLOB &Value) { - auto Result = Utils::base64encode((const unsigned char *)Value.rawContent(),Value.size()); - Obj.set(Field,Result); + inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, + const Poco::Data::BLOB &Value) { + auto Result = Utils::base64encode((const unsigned char *)Value.rawContent(), Value.size()); + Obj.set(Field, Result); } - inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Types::StringPairVec & S) { - Poco::JSON::Array Array; - for(const auto &i:S) { - Poco::JSON::Object O; - O.set("tag",i.first); + inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, + const Types::StringPairVec &S) { + Poco::JSON::Array Array; + for (const auto &i : S) { + Poco::JSON::Object O; + O.set("tag", i.first); O.set("value", i.second); Array.add(O); } - Obj.set(Field,Array); + Obj.set(Field, Array); } - inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Types::StringVec &V) { - Poco::JSON::Array A; - for(const auto &i:V) + inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, + const Types::StringVec &V) { + Poco::JSON::Array A; + for (const auto &i : V) A.add(i); - Obj.set(Field,A); + Obj.set(Field, A); } inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Types::TagList &V) { - Poco::JSON::Array A; - for(const auto &i:V) + Poco::JSON::Array A; + for (const auto &i : V) A.add(i); - Obj.set(Field,A); + Obj.set(Field, A); } - inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Types::CountedMap &M) { - Poco::JSON::Array A; - for(const auto &[Key,Value]:M) { - Poco::JSON::Object O; - O.set("tag",Key); + inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, + const Types::CountedMap &M) { + Poco::JSON::Array A; + for (const auto &[Key, Value] : M) { + Poco::JSON::Object O; + O.set("tag", Key); O.set("value", Value); A.add(O); } - Obj.set(Field,A); + Obj.set(Field, A); } - inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Types::Counted3DMapSII &M) { - Poco::JSON::Array A; - for(const auto &[OrgName,MonthlyNumberMap]:M) { - Poco::JSON::Object OrgObject; - OrgObject.set("tag",OrgName); - Poco::JSON::Array MonthlyArray; - for(const auto &[Month,Counter]:MonthlyNumberMap) { - Poco::JSON::Object Inner; + inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, + const Types::Counted3DMapSII &M) { + Poco::JSON::Array A; + for (const auto &[OrgName, MonthlyNumberMap] : M) { + Poco::JSON::Object OrgObject; + OrgObject.set("tag", OrgName); + Poco::JSON::Array MonthlyArray; + for (const auto &[Month, Counter] : MonthlyNumberMap) { + Poco::JSON::Object Inner; Inner.set("value", Month); Inner.set("counter", Counter); MonthlyArray.add(Inner); } - OrgObject.set("index",MonthlyArray); + OrgObject.set("index", MonthlyArray); A.add(OrgObject); } Obj.set(Field, A); } - template void field_to_json(Poco::JSON::Object &Obj, - const char *Field, - const T &V, + template + void field_to_json(Poco::JSON::Object &Obj, const char *Field, const T &V, std::function F) { Obj.set(Field, F(V)); } - template void field_to_json(Poco::JSON::Object &Obj, const char *Field, const std::vector &Value) { + template + void field_to_json(Poco::JSON::Object &Obj, const char *Field, const std::vector &Value) { Poco::JSON::Array Arr; - for(const auto &i:Value) { - Poco::JSON::Object AO; + for (const auto &i : Value) { + Poco::JSON::Object AO; i.to_json(AO); Arr.add(AO); } Obj.set(Field, Arr); } - template void field_to_json(Poco::JSON::Object &Obj, const char *Field, const T &Value) { - Poco::JSON::Object Answer; + template + void field_to_json(Poco::JSON::Object &Obj, const char *Field, const T &Value) { + Poco::JSON::Object Answer; Value.to_json(Answer); Obj.set(Field, Answer); } @@ -155,188 +162,207 @@ namespace OpenWifi::RESTAPI_utils { /////////////////////////// /////////////////////////// - template bool field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, T & V, + template + bool field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, T &V, std::function F) { - if(Obj->has(Field) && !Obj->isNull(Field)) + if (Obj->has(Field) && !Obj->isNull(Field)) V = F(Obj->get(Field).toString()); return true; } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, std::string &S) { - if(Obj->has(Field) && !Obj->isNull(Field)) + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + std::string &S) { + if (Obj->has(Field) && !Obj->isNull(Field)) S = Obj->get(Field).toString(); } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, double & Value) { - if(Obj->has(Field) && !Obj->isNull(Field)) + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + double &Value) { + if (Obj->has(Field) && !Obj->isNull(Field)) Value = (double)Obj->get(Field); } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, float & Value) { - if(Obj->has(Field) && !Obj->isNull(Field)) + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + float &Value) { + if (Obj->has(Field) && !Obj->isNull(Field)) Value = (float)Obj->get(Field); } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, bool &Value) { - if(Obj->has(Field) && !Obj->isNull(Field)) + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + bool &Value) { + if (Obj->has(Field) && !Obj->isNull(Field)) Value = (Obj->get(Field).toString() == "true"); } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, int16_t &Value) { - if(Obj->has(Field) && !Obj->isNull(Field)) + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + int16_t &Value) { + if (Obj->has(Field) && !Obj->isNull(Field)) Value = (int16_t)Obj->get(Field); } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, int32_t &Value) { - if(Obj->has(Field) && !Obj->isNull(Field)) - Value = (int32_t) Obj->get(Field); + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + int32_t &Value) { + if (Obj->has(Field) && !Obj->isNull(Field)) + Value = (int32_t)Obj->get(Field); } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, int64_t &Value) { - if(Obj->has(Field) && !Obj->isNull(Field)) + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + int64_t &Value) { + if (Obj->has(Field) && !Obj->isNull(Field)) Value = (int64_t)Obj->get(Field); } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, uint16_t &Value) { - if(Obj->has(Field) && !Obj->isNull(Field)) + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + uint16_t &Value) { + if (Obj->has(Field) && !Obj->isNull(Field)) Value = (uint16_t)Obj->get(Field); } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, uint32_t &Value) { - if(Obj->has(Field) && !Obj->isNull(Field)) + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + uint32_t &Value) { + if (Obj->has(Field) && !Obj->isNull(Field)) Value = (uint32_t)Obj->get(Field); } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, uint64_t &Value) { - if(Obj->has(Field) && !Obj->isNull(Field)) + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + uint64_t &Value) { + if (Obj->has(Field) && !Obj->isNull(Field)) Value = (uint64_t)Obj->get(Field); } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, Poco::Data::BLOB &Value) { - if(Obj->has(Field) && !Obj->isNull(Field)) { + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + Poco::Data::BLOB &Value) { + if (Obj->has(Field) && !Obj->isNull(Field)) { auto Result = Utils::base64decode(Obj->get(Field).toString()); - Value.assignRaw((const unsigned char *)&Result[0],Result.size()); + Value.assignRaw((const unsigned char *)&Result[0], Result.size()); } } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, Types::StringPairVec &Vec) { - if(Obj->isArray(Field) && !Obj->isNull(Field)) { + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + Types::StringPairVec &Vec) { + if (Obj->isArray(Field) && !Obj->isNull(Field)) { auto O = Obj->getArray(Field); - for(const auto &i:*O) { - std::string S1,S2; + for (const auto &i : *O) { + std::string S1, S2; auto Inner = i.extract(); - if(Inner->has("tag")) + if (Inner->has("tag")) S1 = Inner->get("tag").toString(); - if(Inner->has("value")) + if (Inner->has("value")) S2 = Inner->get("value").toString(); - auto P = std::make_pair(S1,S2); + auto P = std::make_pair(S1, S2); Vec.push_back(P); } } } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, Types::StringVec &Value) { - if(Obj->isArray(Field) && !Obj->isNull(Field)) { + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + Types::StringVec &Value) { + if (Obj->isArray(Field) && !Obj->isNull(Field)) { Value.clear(); Poco::JSON::Array::Ptr A = Obj->getArray(Field); - for(const auto &i:*A) { + for (const auto &i : *A) { Value.push_back(i.toString()); } } } - inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, Types::TagList &Value) { - if(Obj->isArray(Field) && !Obj->isNull(Field)) { + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + Types::TagList &Value) { + if (Obj->isArray(Field) && !Obj->isNull(Field)) { Value.clear(); Poco::JSON::Array::Ptr A = Obj->getArray(Field); - for(const auto &i:*A) { + for (const auto &i : *A) { Value.push_back(i); } } } - template void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, std::vector &Value) { - if(Obj->isArray(Field) && !Obj->isNull(Field)) { - Poco::JSON::Array::Ptr Arr = Obj->getArray(Field); - for(auto &i:*Arr) { + template + void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, + std::vector &Value) { + if (Obj->isArray(Field) && !Obj->isNull(Field)) { + Poco::JSON::Array::Ptr Arr = Obj->getArray(Field); + for (auto &i : *Arr) { auto InnerObj = i.extract(); - T NewItem; + T NewItem; NewItem.from_json(InnerObj); Value.push_back(NewItem); } } } - template void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, T &Value) { - if(Obj->isObject(Field) && !Obj->isNull(Field)) { - Poco::JSON::Object::Ptr A = Obj->getObject(Field); + template + void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, T &Value) { + if (Obj->isObject(Field) && !Obj->isNull(Field)) { + Poco::JSON::Object::Ptr A = Obj->getObject(Field); Value.from_json(A); } } - inline std::string to_string(const Types::TagList & ObjectArray) { + inline std::string to_string(const Types::TagList &ObjectArray) { Poco::JSON::Array OutputArr; - if(ObjectArray.empty()) + if (ObjectArray.empty()) return "[]"; - for(auto const &i:ObjectArray) { + for (auto const &i : ObjectArray) { OutputArr.add(i); } std::ostringstream OS; - Poco::JSON::Stringifier::stringify(OutputArr,OS, 0,0, Poco::JSON_PRESERVE_KEY_ORDER ); + Poco::JSON::Stringifier::stringify(OutputArr, OS, 0, 0, Poco::JSON_PRESERVE_KEY_ORDER); return OS.str(); } - inline std::string to_string(const Types::StringVec & ObjectArray) { + inline std::string to_string(const Types::StringVec &ObjectArray) { Poco::JSON::Array OutputArr; - if(ObjectArray.empty()) + if (ObjectArray.empty()) return "[]"; - for(auto const &i:ObjectArray) { + for (auto const &i : ObjectArray) { OutputArr.add(i); } std::ostringstream OS; - Poco::JSON::Stringifier::condense(OutputArr,OS); + Poco::JSON::Stringifier::condense(OutputArr, OS); return OS.str(); } - inline std::string to_string(const Types::StringPairVec & ObjectArray) { + inline std::string to_string(const Types::StringPairVec &ObjectArray) { Poco::JSON::Array OutputArr; - if(ObjectArray.empty()) + if (ObjectArray.empty()) return "[]"; - for(auto const &i:ObjectArray) { + for (auto const &i : ObjectArray) { Poco::JSON::Array InnerArray; InnerArray.add(i.first); InnerArray.add(i.second); OutputArr.add(InnerArray); } std::ostringstream OS; - Poco::JSON::Stringifier::condense(OutputArr,OS); + Poco::JSON::Stringifier::condense(OutputArr, OS); return OS.str(); } - template std::string to_string(const std::vector & ObjectArray) { + template std::string to_string(const std::vector &ObjectArray) { Poco::JSON::Array OutputArr; - if(ObjectArray.empty()) + if (ObjectArray.empty()) return "[]"; - for(auto const &i:ObjectArray) { + for (auto const &i : ObjectArray) { Poco::JSON::Object O; i.to_json(O); OutputArr.add(O); } std::ostringstream OS; - Poco::JSON::Stringifier::condense(OutputArr,OS); + Poco::JSON::Stringifier::condense(OutputArr, OS); return OS.str(); } - template std::string to_string(const std::vector> & ObjectArray) { + template std::string to_string(const std::vector> &ObjectArray) { Poco::JSON::Array OutputArr; - if(ObjectArray.empty()) + if (ObjectArray.empty()) return "[]"; - for(auto const &i:ObjectArray) { + for (auto const &i : ObjectArray) { Poco::JSON::Array InnerArr; - for(auto const &j:i) { - if constexpr(std::is_integral::value) { + for (auto const &j : i) { + if constexpr (std::is_integral::value) { InnerArr.add(j); - } if constexpr(std::is_same_v) { + } + if constexpr (std::is_same_v) { InnerArr.add(j); } else { InnerArr.add(j); @@ -348,22 +374,22 @@ namespace OpenWifi::RESTAPI_utils { OutputArr.add(InnerArr); } std::ostringstream OS; - Poco::JSON::Stringifier::condense(OutputArr,OS); + Poco::JSON::Stringifier::condense(OutputArr, OS); return OS.str(); } - template std::string to_string(const T & Object) { + template std::string to_string(const T &Object) { Poco::JSON::Object OutputObj; Object.to_json(OutputObj); std::ostringstream OS; - Poco::JSON::Stringifier::condense(OutputObj,OS); + Poco::JSON::Stringifier::condense(OutputObj, OS); return OS.str(); } - inline Types::StringVec to_object_array(const std::string & ObjectString) { + inline Types::StringVec to_object_array(const std::string &ObjectString) { - Types::StringVec Result; - if(ObjectString.empty()) + Types::StringVec Result; + if (ObjectString.empty()) return Result; try { @@ -373,14 +399,13 @@ namespace OpenWifi::RESTAPI_utils { Result.push_back(i.toString()); } } catch (...) { - } return Result; } - inline OpenWifi::Types::TagList to_taglist(const std::string & ObjectString) { - Types::TagList Result; - if(ObjectString.empty()) + inline OpenWifi::Types::TagList to_taglist(const std::string &ObjectString) { + Types::TagList Result; + if (ObjectString.empty()) return Result; try { @@ -390,36 +415,34 @@ namespace OpenWifi::RESTAPI_utils { Result.push_back(i); } } catch (...) { - } return Result; } inline Types::StringPairVec to_stringpair_array(const std::string &S) { - Types::StringPairVec R; - if(S.empty()) + Types::StringPairVec R; + if (S.empty()) return R; try { Poco::JSON::Parser P; auto Object = P.parse(S).template extract(); for (const auto &i : *Object) { auto InnerObject = i.template extract(); - if(InnerObject->size()==2) { + if (InnerObject->size() == 2) { auto S1 = InnerObject->getElement(0); auto S2 = InnerObject->getElement(1); - R.push_back(std::make_pair(S1,S2)); + R.push_back(std::make_pair(S1, S2)); } } } catch (...) { - } return R; } - template std::vector to_object_array(const std::string & ObjectString) { - std::vector Result; - if(ObjectString.empty()) + template std::vector to_object_array(const std::string &ObjectString) { + std::vector Result; + if (ObjectString.empty()) return Result; try { @@ -432,14 +455,14 @@ namespace OpenWifi::RESTAPI_utils { Result.push_back(Obj); } } catch (...) { - } return Result; } - template std::vector> to_array_of_array_of_object(const std::string & ObjectString) { - std::vector> Result; - if(ObjectString.empty()) + template + std::vector> to_array_of_array_of_object(const std::string &ObjectString) { + std::vector> Result; + if (ObjectString.empty()) return Result; try { Poco::JSON::Parser P1; @@ -447,8 +470,8 @@ namespace OpenWifi::RESTAPI_utils { for (auto const &i : *OutterArray) { Poco::JSON::Parser P2; auto InnerArray = P2.parse(i).template extract(); - std::vector InnerVector; - for(auto const &j: *InnerArray) { + std::vector InnerVector; + for (auto const &j : *InnerArray) { auto Object = j.template extract(); T Obj; Obj.from_json(Object); @@ -457,28 +480,27 @@ namespace OpenWifi::RESTAPI_utils { Result.push_back(InnerVector); } } catch (...) { - } return Result; } - template T to_object(const std::string & ObjectString) { - T Result; + template T to_object(const std::string &ObjectString) { + T Result; - if(ObjectString.empty()) + if (ObjectString.empty()) return Result; - Poco::JSON::Parser P; + Poco::JSON::Parser P; auto Object = P.parse(ObjectString).template extract(); Result.from_json(Object); return Result; } - template bool from_request(T & Obj, Poco::Net::HTTPServerRequest &Request) { + template bool from_request(T &Obj, Poco::Net::HTTPServerRequest &Request) { Poco::JSON::Parser IncomingParser; auto RawObject = IncomingParser.parse(Request.stream()).extract(); Obj.from_json(RawObject); return true; } -} +} // namespace OpenWifi::RESTAPI_utils diff --git a/src/framework/StorageClass.h b/src/framework/StorageClass.h index e9f3feb..2255de5 100644 --- a/src/framework/StorageClass.h +++ b/src/framework/StorageClass.h @@ -4,137 +4,133 @@ #pragma once +#include "Poco/Data/SQLite/Connector.h" #include "Poco/Data/Session.h" #include "Poco/Data/SessionPool.h" -#include "Poco/Data/SQLite/Connector.h" #include "Poco/JSON/Object.h" #ifndef SMALL_BUILD -#include "Poco/Data/PostgreSQL/Connector.h" #include "Poco/Data/MySQL/Connector.h" +#include "Poco/Data/PostgreSQL/Connector.h" #endif -#include "framework/SubSystemServer.h" #include "framework/MicroServiceFuncs.h" +#include "framework/SubSystemServer.h" namespace OpenWifi { - enum DBType { - sqlite, - pgsql, - mysql - }; + enum DBType { sqlite, pgsql, mysql }; - class StorageClass : public SubSystemServer { - public: - StorageClass() noexcept: - SubSystemServer("StorageClass", "STORAGE-SVR", "storage") - { - } + class StorageClass : public SubSystemServer { + public: + StorageClass() noexcept : SubSystemServer("StorageClass", "STORAGE-SVR", "storage") {} - int Start() override { - std::lock_guard Guard(Mutex_); + int Start() override { + std::lock_guard Guard(Mutex_); - Logger().notice("Starting."); - std::string DBType = MicroServiceConfigGetString("storage.type",""); + Logger().notice("Starting."); + std::string DBType = MicroServiceConfigGetString("storage.type", ""); - if (DBType == "sqlite") { - Setup_SQLite(); - } else if (DBType == "postgresql") { - Setup_PostgreSQL(); - } else if (DBType == "mysql") { - Setup_MySQL(); - } - return 0; - } + if (DBType == "sqlite") { + Setup_SQLite(); + } else if (DBType == "postgresql") { + Setup_PostgreSQL(); + } else if (DBType == "mysql") { + Setup_MySQL(); + } + return 0; + } - void Stop() override { - Pool_->shutdown(); - } + void Stop() override { Pool_->shutdown(); } - DBType Type() const { return dbType_; }; - private: - inline int Setup_SQLite(); - inline int Setup_MySQL(); - inline int Setup_PostgreSQL(); + DBType Type() const { return dbType_; }; - protected: - std::unique_ptr Pool_; - Poco::Data::SQLite::Connector SQLiteConn_; - Poco::Data::PostgreSQL::Connector PostgresConn_; - Poco::Data::MySQL::Connector MySQLConn_; - DBType dbType_ = sqlite; - }; + private: + inline int Setup_SQLite(); + inline int Setup_MySQL(); + inline int Setup_PostgreSQL(); -#ifdef SMALL_BUILD - int Service::Setup_MySQL() { Daemon()->exit(Poco::Util::Application::EXIT_CONFIG); return 0; } - int Service::Setup_PostgreSQL() { Daemon()->exit(Poco::Util::Application::EXIT_CONFIG); return 0; } + protected: + std::unique_ptr Pool_; + Poco::Data::SQLite::Connector SQLiteConn_; + Poco::Data::PostgreSQL::Connector PostgresConn_; + Poco::Data::MySQL::Connector MySQLConn_; + DBType dbType_ = sqlite; + }; + +#ifdef SMALL_BUILD + int Service::Setup_MySQL() { + Daemon()->exit(Poco::Util::Application::EXIT_CONFIG); + return 0; + } + int Service::Setup_PostgreSQL() { + Daemon()->exit(Poco::Util::Application::EXIT_CONFIG); + return 0; + } #else - inline int StorageClass::Setup_SQLite() { - Logger().notice("SQLite StorageClass enabled."); - dbType_ = sqlite; - auto DBName = MicroServiceDataDirectory() + "/" + MicroServiceConfigGetString("storage.type.sqlite.db",""); - int NumSessions = (int) MicroServiceConfigGetInt("storage.type.sqlite.maxsessions", 64); - int IdleTime = (int) MicroServiceConfigGetInt("storage.type.sqlite.idletime", 60); + inline int StorageClass::Setup_SQLite() { + Logger().notice("SQLite StorageClass enabled."); + dbType_ = sqlite; + auto DBName = MicroServiceDataDirectory() + "/" + + MicroServiceConfigGetString("storage.type.sqlite.db", ""); + int NumSessions = (int)MicroServiceConfigGetInt("storage.type.sqlite.maxsessions", 64); + int IdleTime = (int)MicroServiceConfigGetInt("storage.type.sqlite.idletime", 60); - Poco::Data::SQLite::Connector::registerConnector(); -// Pool_ = std::make_unique(new Poco::Data::SessionPool(SQLiteConn_.name(), DBName, 8, -// (int)NumSessions, (int)IdleTime)); - Pool_ = std::make_unique(SQLiteConn_.name(), DBName, 8, - (int)NumSessions, (int)IdleTime); - return 0; - } + Poco::Data::SQLite::Connector::registerConnector(); + // Pool_ = std::make_unique(new + // Poco::Data::SessionPool(SQLiteConn_.name(), DBName, 8, + // (int)NumSessions, + // (int)IdleTime)); + Pool_ = std::make_unique(SQLiteConn_.name(), DBName, 8, + (int)NumSessions, (int)IdleTime); + return 0; + } - inline int StorageClass::Setup_MySQL() { - Logger().notice("MySQL StorageClass enabled."); - dbType_ = mysql; - int NumSessions = (int) MicroServiceConfigGetInt("storage.type.mysql.maxsessions", 64); - int IdleTime = (int) MicroServiceConfigGetInt("storage.type.mysql.idletime", 60); - auto Host = MicroServiceConfigGetString("storage.type.mysql.host",""); - auto Username = MicroServiceConfigGetString("storage.type.mysql.username",""); - auto Password = MicroServiceConfigGetString("storage.type.mysql.password",""); - auto Database = MicroServiceConfigGetString("storage.type.mysql.database",""); - auto Port = MicroServiceConfigGetString("storage.type.mysql.port",""); + inline int StorageClass::Setup_MySQL() { + Logger().notice("MySQL StorageClass enabled."); + dbType_ = mysql; + int NumSessions = (int)MicroServiceConfigGetInt("storage.type.mysql.maxsessions", 64); + int IdleTime = (int)MicroServiceConfigGetInt("storage.type.mysql.idletime", 60); + auto Host = MicroServiceConfigGetString("storage.type.mysql.host", ""); + auto Username = MicroServiceConfigGetString("storage.type.mysql.username", ""); + auto Password = MicroServiceConfigGetString("storage.type.mysql.password", ""); + auto Database = MicroServiceConfigGetString("storage.type.mysql.database", ""); + auto Port = MicroServiceConfigGetString("storage.type.mysql.port", ""); - std::string ConnectionStr = - "host=" + Host + - ";user=" + Username + - ";password=" + Password + - ";db=" + Database + - ";port=" + Port + - ";compress=true;auto-reconnect=true"; + std::string ConnectionStr = "host=" + Host + ";user=" + Username + ";password=" + Password + + ";db=" + Database + ";port=" + Port + + ";compress=true;auto-reconnect=true"; - Poco::Data::MySQL::Connector::registerConnector(); - Pool_ = std::make_unique(MySQLConn_.name(), ConnectionStr, 8, NumSessions, IdleTime); + Poco::Data::MySQL::Connector::registerConnector(); + Pool_ = std::make_unique(MySQLConn_.name(), ConnectionStr, 8, + NumSessions, IdleTime); - return 0; - } + return 0; + } - inline int StorageClass::Setup_PostgreSQL() { - Logger().notice("PostgreSQL StorageClass enabled."); - dbType_ = pgsql; - int NumSessions = (int) MicroServiceConfigGetInt("storage.type.postgresql.maxsessions", 64); - int IdleTime = (int) MicroServiceConfigGetInt("storage.type.postgresql.idletime", 60); - auto Host = MicroServiceConfigGetString("storage.type.postgresql.host", ""); - auto Username = MicroServiceConfigGetString("storage.type.postgresql.username", ""); - auto Password = MicroServiceConfigGetString("storage.type.postgresql.password", ""); - auto Database = MicroServiceConfigGetString("storage.type.postgresql.database", ""); - auto Port = MicroServiceConfigGetString("storage.type.postgresql.port", ""); - auto ConnectionTimeout = MicroServiceConfigGetString("storage.type.postgresql.connectiontimeout", ""); + inline int StorageClass::Setup_PostgreSQL() { + Logger().notice("PostgreSQL StorageClass enabled."); + dbType_ = pgsql; + int NumSessions = (int)MicroServiceConfigGetInt("storage.type.postgresql.maxsessions", 64); + int IdleTime = (int)MicroServiceConfigGetInt("storage.type.postgresql.idletime", 60); + auto Host = MicroServiceConfigGetString("storage.type.postgresql.host", ""); + auto Username = MicroServiceConfigGetString("storage.type.postgresql.username", ""); + auto Password = MicroServiceConfigGetString("storage.type.postgresql.password", ""); + auto Database = MicroServiceConfigGetString("storage.type.postgresql.database", ""); + auto Port = MicroServiceConfigGetString("storage.type.postgresql.port", ""); + auto ConnectionTimeout = + MicroServiceConfigGetString("storage.type.postgresql.connectiontimeout", ""); - std::string ConnectionStr = - "host=" + Host + - " user=" + Username + - " password=" + Password + - " dbname=" + Database + - " port=" + Port + - " connect_timeout=" + ConnectionTimeout; + std::string ConnectionStr = "host=" + Host + " user=" + Username + " password=" + Password + + " dbname=" + Database + " port=" + Port + + " connect_timeout=" + ConnectionTimeout; - Poco::Data::PostgreSQL::Connector::registerConnector(); - Pool_ = std::make_unique(PostgresConn_.name(), ConnectionStr, 8, NumSessions, IdleTime); + Poco::Data::PostgreSQL::Connector::registerConnector(); + Pool_ = std::make_unique(PostgresConn_.name(), ConnectionStr, 8, + NumSessions, IdleTime); - return 0; - } + return 0; + } #endif -} +} // namespace OpenWifi diff --git a/src/framework/SubSystemServer.cpp b/src/framework/SubSystemServer.cpp index 0222a83..f6df356 100644 --- a/src/framework/SubSystemServer.cpp +++ b/src/framework/SubSystemServer.cpp @@ -6,36 +6,29 @@ #include "framework/SubSystemServer.h" -#include "Poco/Net/SSLManager.h" -#include "Poco/DateTimeFormatter.h" #include "Poco/DateTimeFormat.h" +#include "Poco/DateTimeFormatter.h" +#include "Poco/Net/SSLManager.h" #include "framework/MicroServiceFuncs.h" namespace OpenWifi { - PropertiesFileServerEntry::PropertiesFileServerEntry(std::string Address, uint32_t port, std::string Key_file, - std::string Cert_file, std::string RootCa, std::string Issuer, - std::string ClientCas, std::string Cas, - std::string Key_file_password, std::string Name, - Poco::Net::Context::VerificationMode M, - int backlog) - : address_(std::move(Address)), - port_(port), - cert_file_(std::move(Cert_file)), - key_file_(std::move(Key_file)), - root_ca_(std::move(RootCa)), - key_file_password_(std::move(Key_file_password)), - issuer_cert_file_(std::move(Issuer)), - client_cas_(std::move(ClientCas)), - cas_(std::move(Cas)), - name_(std::move(Name)), - backlog_(backlog), - level_(M) { + PropertiesFileServerEntry::PropertiesFileServerEntry( + std::string Address, uint32_t port, std::string Key_file, std::string Cert_file, + std::string RootCa, std::string Issuer, std::string ClientCas, std::string Cas, + std::string Key_file_password, std::string Name, Poco::Net::Context::VerificationMode M, + int backlog) + : address_(std::move(Address)), port_(port), cert_file_(std::move(Cert_file)), + key_file_(std::move(Key_file)), root_ca_(std::move(RootCa)), + key_file_password_(std::move(Key_file_password)), issuer_cert_file_(std::move(Issuer)), + client_cas_(std::move(ClientCas)), cas_(std::move(Cas)), name_(std::move(Name)), + backlog_(backlog), level_(M){ - }; + }; - [[nodiscard]] Poco::Net::SecureServerSocket PropertiesFileServerEntry::CreateSecureSocket(Poco::Logger &L) const { + [[nodiscard]] Poco::Net::SecureServerSocket + PropertiesFileServerEntry::CreateSecureSocket(Poco::Logger &L) const { Poco::Net::Context::Params P; P.verificationMode = level_; @@ -45,11 +38,13 @@ namespace OpenWifi { P.dhUse2048Bits = true; P.caLocation = cas_; - auto Context = Poco::AutoPtr(new Poco::Net::Context(Poco::Net::Context::TLS_SERVER_USE, P)); + auto Context = Poco::AutoPtr( + new Poco::Net::Context(Poco::Net::Context::TLS_SERVER_USE, P)); - if(!key_file_password_.empty()) { - auto PassphraseHandler = Poco::SharedPtr( new MyPrivateKeyPassphraseHandler(key_file_password_,L)); - Poco::Net::SSLManager::instance().initializeServer(PassphraseHandler, nullptr,Context); + if (!key_file_password_.empty()) { + auto PassphraseHandler = Poco::SharedPtr( + new MyPrivateKeyPassphraseHandler(key_file_password_, L)); + Poco::Net::SSLManager::instance().initializeServer(PassphraseHandler, nullptr, Context); } if (!cert_file_.empty() && !key_file_.empty()) { @@ -111,7 +106,8 @@ namespace OpenWifi { } } - [[nodiscard]] Poco::Net::ServerSocket PropertiesFileServerEntry::CreateSocket([[maybe_unused]] Poco::Logger &L) const { + [[nodiscard]] Poco::Net::ServerSocket + PropertiesFileServerEntry::CreateSocket([[maybe_unused]] Poco::Logger &L) const { Poco::Net::Context::Params P; if (address_ == "*") { @@ -127,20 +123,25 @@ namespace OpenWifi { } } - void PropertiesFileServerEntry::LogCertInfo(Poco::Logger &L, const Poco::Crypto::X509Certificate &C) const { - L.information("============================================================================================="); + void PropertiesFileServerEntry::LogCertInfo(Poco::Logger &L, + const Poco::Crypto::X509Certificate &C) const { + L.information("============================================================================" + "================="); L.information(fmt::format("> Issuer: {}", C.issuerName())); - L.information("---------------------------------------------------------------------------------------------"); + L.information("----------------------------------------------------------------------------" + "-----------------"); L.information(fmt::format("> Common Name: {}", C.issuerName(Poco::Crypto::X509Certificate::NID_COMMON_NAME))); L.information(fmt::format("> Country: {}", C.issuerName(Poco::Crypto::X509Certificate::NID_COUNTRY))); L.information(fmt::format("> Locality: {}", C.issuerName(Poco::Crypto::X509Certificate::NID_LOCALITY_NAME))); - L.information(fmt::format("> State/Prov: {}", - C.issuerName(Poco::Crypto::X509Certificate::NID_STATE_OR_PROVINCE))); - L.information(fmt::format("> Org name: {}", - C.issuerName(Poco::Crypto::X509Certificate::NID_ORGANIZATION_NAME))); + L.information( + fmt::format("> State/Prov: {}", + C.issuerName(Poco::Crypto::X509Certificate::NID_STATE_OR_PROVINCE))); + L.information( + fmt::format("> Org name: {}", + C.issuerName(Poco::Crypto::X509Certificate::NID_ORGANIZATION_NAME))); L.information( fmt::format("> Org unit: {}", C.issuerName(Poco::Crypto::X509Certificate::NID_ORGANIZATION_UNIT_NAME))); @@ -149,9 +150,11 @@ namespace OpenWifi { C.issuerName(Poco::Crypto::X509Certificate::NID_PKCS9_EMAIL_ADDRESS))); L.information(fmt::format("> Serial#: {}", C.issuerName(Poco::Crypto::X509Certificate::NID_SERIAL_NUMBER))); - L.information("---------------------------------------------------------------------------------------------"); + L.information("----------------------------------------------------------------------------" + "-----------------"); L.information(fmt::format("> Subject: {}", C.subjectName())); - L.information("---------------------------------------------------------------------------------------------"); + L.information("----------------------------------------------------------------------------" + "-----------------"); L.information(fmt::format("> Common Name: {}", C.subjectName(Poco::Crypto::X509Certificate::NID_COMMON_NAME))); L.information(fmt::format("> Country: {}", @@ -172,52 +175,66 @@ namespace OpenWifi { C.subjectName(Poco::Crypto::X509Certificate::NID_PKCS9_EMAIL_ADDRESS))); L.information(fmt::format("> Serial#: {}", C.subjectName(Poco::Crypto::X509Certificate::NID_SERIAL_NUMBER))); - L.information("---------------------------------------------------------------------------------------------"); + L.information("----------------------------------------------------------------------------" + "-----------------"); L.information(fmt::format("> Signature Algo: {}", C.signatureAlgorithm())); - auto From = Poco::DateTimeFormatter::format(C.validFrom(), Poco::DateTimeFormat::HTTP_FORMAT); + auto From = + Poco::DateTimeFormatter::format(C.validFrom(), Poco::DateTimeFormat::HTTP_FORMAT); L.information(fmt::format("> Valid from: {}", From)); auto Expires = Poco::DateTimeFormatter::format(C.expiresOn(), Poco::DateTimeFormat::HTTP_FORMAT); L.information(fmt::format("> Expires on: {}", Expires)); L.information(fmt::format("> Version: {}", (int)C.version())); L.information(fmt::format("> Serial #: {}", C.serialNumber())); - L.information("============================================================================================="); + L.information("============================================================================" + "================="); } void PropertiesFileServerEntry::LogCert(Poco::Logger &L) const { try { Poco::Crypto::X509Certificate C(cert_file_); - L.information("============================================================================================="); - L.information("============================================================================================="); + L.information("========================================================================" + "====================="); + L.information("========================================================================" + "====================="); L.information(fmt::format("Certificate Filename: {}", cert_file_)); LogCertInfo(L, C); - L.information("============================================================================================="); + L.information("========================================================================" + "====================="); if (!issuer_cert_file_.empty()) { Poco::Crypto::X509Certificate C1(issuer_cert_file_); - L.information("============================================================================================="); - L.information("============================================================================================="); + L.information("====================================================================" + "========================="); + L.information("====================================================================" + "========================="); L.information(fmt::format("Issues Certificate Filename: {}", issuer_cert_file_)); LogCertInfo(L, C1); - L.information("============================================================================================="); + L.information("====================================================================" + "========================="); } if (!client_cas_.empty()) { std::vector Certs = Poco::Net::X509Certificate::readPEM(client_cas_); - L.information("============================================================================================="); - L.information("============================================================================================="); + L.information("====================================================================" + "========================="); + L.information("====================================================================" + "========================="); L.information(fmt::format("Client CAs Filename: {}", client_cas_)); - L.information("============================================================================================="); + L.information("====================================================================" + "========================="); auto i = 1; for (const auto &C3 : Certs) { L.information(fmt::format(" Index: {}", i)); - L.information("============================================================================================="); + L.information("================================================================" + "============================="); LogCertInfo(L, C3); i++; } - L.information("============================================================================================="); + L.information("====================================================================" + "========================="); } } catch (const Poco::Exception &E) { @@ -230,28 +247,31 @@ namespace OpenWifi { std::vector Certs = Poco::Net::X509Certificate::readPEM(root_ca_); - L.information("============================================================================================="); - L.information("============================================================================================="); + L.information("========================================================================" + "====================="); + L.information("========================================================================" + "====================="); L.information(fmt::format("CA Filename: {}", root_ca_)); - L.information("============================================================================================="); + L.information("========================================================================" + "====================="); auto i = 1; for (const auto &C : Certs) { L.information(fmt::format(" Index: {}", i)); - L.information("============================================================================================="); + L.information("====================================================================" + "========================="); LogCertInfo(L, C); i++; } - L.information("============================================================================================="); + L.information("========================================================================" + "====================="); } catch (const Poco::Exception &E) { L.log(E); } } SubSystemServer::SubSystemServer(const std::string &Name, const std::string &LoggingPrefix, - const std::string &SubSystemConfigPrefix): - Name_(Name), - LoggerPrefix_(LoggingPrefix), - SubSystemConfigPrefix_(SubSystemConfigPrefix) { + const std::string &SubSystemConfigPrefix) + : Name_(Name), LoggerPrefix_(LoggingPrefix), SubSystemConfigPrefix_(SubSystemConfigPrefix) { } void SubSystemServer::initialize([[maybe_unused]] Poco::Util::Application &self) { @@ -259,10 +279,13 @@ namespace OpenWifi { bool good = true; auto NewLevel = MicroServiceConfigGetString("logging.level." + Name_, ""); - if(NewLevel.empty()) - Logger_ = std::make_unique(Poco::Logger::create(LoggerPrefix_, Poco::Logger::root().getChannel(), Poco::Logger::root().getLevel())); + if (NewLevel.empty()) + Logger_ = std::make_unique(Poco::Logger::create( + LoggerPrefix_, Poco::Logger::root().getChannel(), Poco::Logger::root().getLevel())); else - Logger_ = std::make_unique(Poco::Logger::create(LoggerPrefix_, Poco::Logger::root().getChannel(), Poco::Logger::parseLevel(NewLevel))); + Logger_ = std::make_unique( + Poco::Logger::create(LoggerPrefix_, Poco::Logger::root().getChannel(), + Poco::Logger::parseLevel(NewLevel))); ConfigServersList_.clear(); while (good) { @@ -297,24 +320,18 @@ namespace OpenWifi { } else if (L == "once") M = Poco::Net::Context::VERIFY_ONCE; - PropertiesFileServerEntry entry(MicroServiceConfigGetString(address, ""), - MicroServiceConfigGetInt(port, 0), - MicroServiceConfigPath(key, ""), - MicroServiceConfigPath(cert, ""), - MicroServiceConfigPath(rootca, ""), - MicroServiceConfigPath(issuer, ""), - MicroServiceConfigPath(clientcas, ""), - MicroServiceConfigPath(cas, ""), - MicroServiceConfigGetString(key_password, ""), - MicroServiceConfigGetString(name, ""), M, - (int)MicroServiceConfigGetInt(backlog, 64)); + PropertiesFileServerEntry entry( + MicroServiceConfigGetString(address, ""), MicroServiceConfigGetInt(port, 0), + MicroServiceConfigPath(key, ""), MicroServiceConfigPath(cert, ""), + MicroServiceConfigPath(rootca, ""), MicroServiceConfigPath(issuer, ""), + MicroServiceConfigPath(clientcas, ""), MicroServiceConfigPath(cas, ""), + MicroServiceConfigGetString(key_password, ""), + MicroServiceConfigGetString(name, ""), M, + (int)MicroServiceConfigGetInt(backlog, 64)); ConfigServersList_.push_back(entry); i++; } } } - - - } // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/SubSystemServer.h b/src/framework/SubSystemServer.h index 268f899..303fbf4 100644 --- a/src/framework/SubSystemServer.h +++ b/src/framework/SubSystemServer.h @@ -4,32 +4,30 @@ #pragma once -#include #include +#include -#include "Poco/Util/Application.h" #include "Poco/Net/Context.h" -#include "Poco/Net/SecureServerSocket.h" #include "Poco/Net/PrivateKeyPassphraseHandler.h" +#include "Poco/Net/SecureServerSocket.h" +#include "Poco/Util/Application.h" namespace OpenWifi { class MyPrivateKeyPassphraseHandler : public Poco::Net::PrivateKeyPassphraseHandler { public: - explicit MyPrivateKeyPassphraseHandler(const std::string &Password, Poco::Logger & Logger): - PrivateKeyPassphraseHandler(true), - Password_(Password), - Logger_(Logger) { - } + explicit MyPrivateKeyPassphraseHandler(const std::string &Password, Poco::Logger &Logger) + : PrivateKeyPassphraseHandler(true), Password_(Password), Logger_(Logger) {} - void onPrivateKeyRequested([[maybe_unused]] const void * pSender,std::string & privateKey) { - poco_information(Logger_,"Returning key passphrase."); + void onPrivateKeyRequested([[maybe_unused]] const void *pSender, std::string &privateKey) { + poco_information(Logger_, "Returning key passphrase."); privateKey = Password_; }; - inline Poco::Logger & Logger() { return Logger_; } + inline Poco::Logger &Logger() { return Logger_; } + private: std::string Password_; - Poco::Logger & Logger_; + Poco::Logger &Logger_; }; class PropertiesFileServerEntry { @@ -76,24 +74,24 @@ namespace OpenWifi { class SubSystemServer : public Poco::Util::Application::Subsystem { public: - SubSystemServer(const std::string & Name, const std::string &LoggingPrefix, - const std::string & SubSystemConfigPrefix); + SubSystemServer(const std::string &Name, const std::string &LoggingPrefix, + const std::string &SubSystemConfigPrefix); void initialize(Poco::Util::Application &self) override; - inline void uninitialize() override { - } + inline void uninitialize() override {} inline void reinitialize([[maybe_unused]] Poco::Util::Application &self) override { - poco_information(Logger_->L_,"Reloading of this subsystem is not supported."); + poco_information(Logger_->L_, "Reloading of this subsystem is not supported."); } - inline void defineOptions([[maybe_unused]] Poco::Util::OptionSet &options) override { - } - inline const std::string & Name() const { return Name_; }; - inline const char * name() const override { return Name_.c_str(); } + inline void defineOptions([[maybe_unused]] Poco::Util::OptionSet &options) override {} + inline const std::string &Name() const { return Name_; }; + inline const char *name() const override { return Name_.c_str(); } - inline const PropertiesFileServerEntry & Host(uint64_t index) { return ConfigServersList_[index]; }; + inline const PropertiesFileServerEntry &Host(uint64_t index) { + return ConfigServersList_[index]; + }; inline uint64_t HostSize() const { return ConfigServersList_.size(); } - inline Poco::Logger & Logger() const { return Logger_->L_; } - inline void SetLoggingLevel(const std::string & levelName) { + inline Poco::Logger &Logger() const { return Logger_->L_; } + inline void SetLoggingLevel(const std::string &levelName) { Logger_->L_.setLevel(Poco::Logger::parseLevel(levelName)); } inline int GetLoggingLevel() { return Logger_->L_.getLevel(); } @@ -102,23 +100,21 @@ namespace OpenWifi { virtual void Stop() = 0; struct LoggerWrapper { - Poco::Logger & L_; - LoggerWrapper(Poco::Logger &L) : - L_(L) { - } + Poco::Logger &L_; + LoggerWrapper(Poco::Logger &L) : L_(L) {} }; protected: - std::recursive_mutex Mutex_; + std::recursive_mutex Mutex_; std::vector ConfigServersList_; private: - std::unique_ptr Logger_; - std::string Name_; - std::string LoggerPrefix_; - std::string SubSystemConfigPrefix_; + std::unique_ptr Logger_; + std::string Name_; + std::string LoggerPrefix_; + std::string SubSystemConfigPrefix_; }; - typedef std::vector SubSystemVec; + typedef std::vector SubSystemVec; } // namespace OpenWifi diff --git a/src/framework/UI_WebSocketClientNotifications.cpp b/src/framework/UI_WebSocketClientNotifications.cpp index c8fcfda..7ad187b 100644 --- a/src/framework/UI_WebSocketClientNotifications.cpp +++ b/src/framework/UI_WebSocketClientNotifications.cpp @@ -5,7 +5,4 @@ #include "framework/UI_WebSocketClientNotifications.h" #include "framework/UI_WebSocketClientServer.h" -namespace OpenWifi { - -} - +namespace OpenWifi {} diff --git a/src/framework/UI_WebSocketClientNotifications.h b/src/framework/UI_WebSocketClientNotifications.h index d534a5c..fc10863 100644 --- a/src/framework/UI_WebSocketClientNotifications.h +++ b/src/framework/UI_WebSocketClientNotifications.h @@ -9,37 +9,35 @@ namespace OpenWifi { - template + template - struct WebSocketNotification { - inline static uint64_t xid = 1; - uint64_t notification_id = ++xid; - std::uint64_t type_id=0; - ContentStruct content; + struct WebSocketNotification { + inline static uint64_t xid = 1; + uint64_t notification_id = ++xid; + std::uint64_t type_id = 0; + ContentStruct content; - void to_json(Poco::JSON::Object &Obj) const; + void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; + bool from_json(const Poco::JSON::Object::Ptr &Obj); + }; - template - void WebSocketNotification::to_json(Poco::JSON::Object &Obj) const { - RESTAPI_utils::field_to_json(Obj, "notification_id", notification_id); + template + void WebSocketNotification::to_json(Poco::JSON::Object &Obj) const { + RESTAPI_utils::field_to_json(Obj, "notification_id", notification_id); RESTAPI_utils::field_to_json(Obj, "type_id", type_id); - RESTAPI_utils::field_to_json(Obj, "content", content); - } + RESTAPI_utils::field_to_json(Obj, "content", content); + } - template - bool WebSocketNotification::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - RESTAPI_utils::field_from_json(Obj, "notification_id", notification_id); + template + bool WebSocketNotification::from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + RESTAPI_utils::field_from_json(Obj, "notification_id", notification_id); RESTAPI_utils::field_from_json(Obj, "type_id", type_id); RESTAPI_utils::field_from_json(Obj, "content", content); - return true; - } catch (...) { - - } - return false; - } -} - + return true; + } catch (...) { + } + return false; + } +} // namespace OpenWifi diff --git a/src/framework/UI_WebSocketClientServer.cpp b/src/framework/UI_WebSocketClientServer.cpp index ee4a53d..37c426f 100644 --- a/src/framework/UI_WebSocketClientServer.cpp +++ b/src/framework/UI_WebSocketClientServer.cpp @@ -4,14 +4,14 @@ #include -#include "Poco/NObserver.h" #include "Poco/JSON/JSONException.h" #include "Poco/JSON/Parser.h" #include "Poco/Logger.h" +#include "Poco/NObserver.h" -#include "framework/UI_WebSocketClientServer.h" #include "framework/AuthClient.h" #include "framework/MicroServiceFuncs.h" +#include "framework/UI_WebSocketClientServer.h" #include "fmt/format.h" @@ -19,80 +19,82 @@ #include "AuthService.h" #endif -#define DBG { std::cout << __LINE__ << std::endl; } +#define DBG \ + { std::cout << __LINE__ << std::endl; } namespace OpenWifi { - void UI_WebSocketClientServer::NewClient(Poco::Net::WebSocket & WS, const std::string &Id, const std::string &UserName, std::uint64_t TID ) { + void UI_WebSocketClientServer::NewClient(Poco::Net::WebSocket &WS, const std::string &Id, + const std::string &UserName, std::uint64_t TID) { - std::lock_guard G(LocalMutex_); - auto Client = std::make_unique(WS,Id, UserName); - auto ClientSocket = Client->WS_->impl()->sockfd(); - TID_ = TID; - Client->WS_->setNoDelay(true); - Client->WS_->setKeepAlive(true); - Client->WS_->setBlocking(false); - Reactor_.addEventHandler(*Client->WS_, - Poco::NObserver( - *this, &UI_WebSocketClientServer::OnSocketReadable)); - Reactor_.addEventHandler(*Client->WS_, - Poco::NObserver( - *this, &UI_WebSocketClientServer::OnSocketShutdown)); - Reactor_.addEventHandler(*Client->WS_, - Poco::NObserver( - *this, &UI_WebSocketClientServer::OnSocketError)); - Client->SocketRegistered_ = true; - Clients_[ClientSocket] = std::move(Client); + std::lock_guard G(LocalMutex_); + auto Client = std::make_unique(WS, Id, UserName); + auto ClientSocket = Client->WS_->impl()->sockfd(); + TID_ = TID; + Client->WS_->setNoDelay(true); + Client->WS_->setKeepAlive(true); + Client->WS_->setBlocking(false); + Reactor_.addEventHandler( + *Client->WS_, + Poco::NObserver( + *this, &UI_WebSocketClientServer::OnSocketReadable)); + Reactor_.addEventHandler( + *Client->WS_, + Poco::NObserver( + *this, &UI_WebSocketClientServer::OnSocketShutdown)); + Reactor_.addEventHandler( + *Client->WS_, Poco::NObserver( + *this, &UI_WebSocketClientServer::OnSocketError)); + Client->SocketRegistered_ = true; + Clients_[ClientSocket] = std::move(Client); UsersConnected_ = Clients_.size(); - } - - void UI_WebSocketClientServer::SetProcessor( UI_WebSocketClientProcessor * F) { - Processor_ = F; } - UI_WebSocketClientServer::UI_WebSocketClientServer() noexcept: - SubSystemServer("WebSocketClientServer", "UI-WSCLNT-SVR", "websocketclients") - { - } + void UI_WebSocketClientServer::SetProcessor(UI_WebSocketClientProcessor *F) { Processor_ = F; } + + UI_WebSocketClientServer::UI_WebSocketClientServer() noexcept + : SubSystemServer("WebSocketClientServer", "UI-WSCLNT-SVR", "websocketclients") {} void UI_WebSocketClientServer::run() { Running_ = true; - while(Running_) { + while (Running_) { Poco::Thread::trySleep(2000); - if(!Running_) + if (!Running_) break; - std::lock_guard G(LocalMutex_); - for(const auto i:ToBeRemoved_) { + std::lock_guard G(LocalMutex_); + for (const auto i : ToBeRemoved_) { // std::cout << "Erasing old WS UI connection..." << std::endl; Clients_.erase(i); } - ToBeRemoved_.clear(); + ToBeRemoved_.clear(); UsersConnected_ = Clients_.size(); } - } - void UI_WebSocketClientServer::EndConnection(ClientList::iterator Client) { - if(Client->second->SocketRegistered_) { - Client->second->SocketRegistered_ = false; - Reactor_.removeEventHandler(*Client->second->WS_, - Poco::NObserver(*this,&UI_WebSocketClientServer::OnSocketReadable)); - Reactor_.removeEventHandler(*Client->second->WS_, - Poco::NObserver(*this,&UI_WebSocketClientServer::OnSocketShutdown)); - Reactor_.removeEventHandler(*Client->second->WS_, - Poco::NObserver(*this,&UI_WebSocketClientServer::OnSocketError)); - } + void UI_WebSocketClientServer::EndConnection(ClientList::iterator Client) { + if (Client->second->SocketRegistered_) { + Client->second->SocketRegistered_ = false; + Reactor_.removeEventHandler( + *Client->second->WS_, + Poco::NObserver( + *this, &UI_WebSocketClientServer::OnSocketReadable)); + Reactor_.removeEventHandler( + *Client->second->WS_, + Poco::NObserver( + *this, &UI_WebSocketClientServer::OnSocketShutdown)); + Reactor_.removeEventHandler( + *Client->second->WS_, + Poco::NObserver( + *this, &UI_WebSocketClientServer::OnSocketError)); + } ToBeRemoved_.push_back(Client); - } + } int UI_WebSocketClientServer::Start() { - poco_information(Logger(),"Starting..."); - GoogleApiKey_ = MicroServiceConfigGetString("google.apikey",""); + poco_information(Logger(), "Starting..."); + GoogleApiKey_ = MicroServiceConfigGetString("google.apikey", ""); GeoCodeEnabled_ = !GoogleApiKey_.empty(); ReactorThread_.start(Reactor_); ReactorThread_.setName("ws:ui-reactor"); @@ -102,29 +104,31 @@ namespace OpenWifi { }; void UI_WebSocketClientServer::Stop() { - if(Running_) { - poco_information(Logger(),"Stopping..."); - Clients_.clear(); + if (Running_) { + poco_information(Logger(), "Stopping..."); + Clients_.clear(); Reactor_.stop(); ReactorThread_.join(); Running_ = false; CleanerThread_.wakeUp(); CleanerThread_.join(); - poco_information(Logger(),"Stopped..."); + poco_information(Logger(), "Stopped..."); } }; - bool UI_WebSocketClientServer::IsFiltered(std::uint64_t id, const OpenWifi::UI_WebSocketClientInfo &Client) { - return std::find(Client.Filter_.begin(), Client.Filter_.end(),id)!=end(Client.Filter_); + bool UI_WebSocketClientServer::IsFiltered(std::uint64_t id, + const OpenWifi::UI_WebSocketClientInfo &Client) { + return std::find(Client.Filter_.begin(), Client.Filter_.end(), id) != end(Client.Filter_); } - bool UI_WebSocketClientServer::SendToUser(const std::string &UserName, std::uint64_t id, const std::string &Payload) { + bool UI_WebSocketClientServer::SendToUser(const std::string &UserName, std::uint64_t id, + const std::string &Payload) { std::lock_guard G(LocalMutex_); - for(const auto &Client:Clients_) { - if(Client.second->UserName_ == UserName) { + for (const auto &Client : Clients_) { + if (Client.second->UserName_ == UserName) { try { - if(!IsFiltered(id,*Client.second) && Client.second->Authenticated_) { + if (!IsFiltered(id, *Client.second) && Client.second->Authenticated_) { return Client.second->WS_->sendFrame( Payload.c_str(), (int)Payload.size()) == (int)Payload.size(); } else { @@ -141,31 +145,32 @@ namespace OpenWifi { void UI_WebSocketClientServer::SendToAll(std::uint64_t id, const std::string &Payload) { std::lock_guard G(LocalMutex_); - for(const auto &Client:Clients_) { + for (const auto &Client : Clients_) { try { - if(!IsFiltered(id,*Client.second) && Client.second->Authenticated_) - Client.second->WS_->sendFrame(Payload.c_str(),(int)Payload.size()); + if (!IsFiltered(id, *Client.second) && Client.second->Authenticated_) + Client.second->WS_->sendFrame(Payload.c_str(), (int)Payload.size()); } catch (...) { - } } } - UI_WebSocketClientServer::ClientList::iterator UI_WebSocketClientServer::FindWSClient( [[maybe_unused]] std::lock_guard &G, int ClientSocket) { - return Clients_.find(ClientSocket); - } + UI_WebSocketClientServer::ClientList::iterator UI_WebSocketClientServer::FindWSClient( + [[maybe_unused]] std::lock_guard &G, int ClientSocket) { + return Clients_.find(ClientSocket); + } void UI_WebSocketClientServer::SortNotifications() { struct { - bool operator()(const NotificationEntry &A, const NotificationEntry & B) const { - return A.id < B.id; }; + bool operator()(const NotificationEntry &A, const NotificationEntry &B) const { + return A.id < B.id; + }; } CompareNotifications; std::sort(NotificationTypes_.begin(), NotificationTypes_.end(), CompareNotifications); NotificationTypesJSON_.clear(); - Poco::JSON::Array AllNotifications; - for(const auto ¬ification:NotificationTypes_) { - Poco::JSON::Object Notification; + Poco::JSON::Array AllNotifications; + for (const auto ¬ification : NotificationTypes_) { + Poco::JSON::Object Notification; Notification.set("id", notification.id); Notification.set("helper", notification.helper); AllNotifications.add(Notification); @@ -173,52 +178,60 @@ namespace OpenWifi { NotificationTypesJSON_.set("notificationTypes", AllNotifications); } - void UI_WebSocketClientServer::RegisterNotifications(const OpenWifi::UI_WebSocketClientServer::NotificationTypeIdVec &Notifications) { - std::copy(Notifications.begin(), Notifications.end(), std::back_inserter(NotificationTypes_)); + void UI_WebSocketClientServer::RegisterNotifications( + const OpenWifi::UI_WebSocketClientServer::NotificationTypeIdVec &Notifications) { + std::copy(Notifications.begin(), Notifications.end(), + std::back_inserter(NotificationTypes_)); SortNotifications(); } - void UI_WebSocketClientServer::OnSocketError([[maybe_unused]] const Poco::AutoPtr &pNf) { - std::lock_guard G(LocalMutex_); - auto Client = Clients_.find(pNf->socket().impl()->sockfd()); - if(Client==end(Clients_)) - return; - EndConnection(Client); + void UI_WebSocketClientServer::OnSocketError( + [[maybe_unused]] const Poco::AutoPtr &pNf) { + std::lock_guard G(LocalMutex_); + auto Client = Clients_.find(pNf->socket().impl()->sockfd()); + if (Client == end(Clients_)) + return; + EndConnection(Client); } - void UI_WebSocketClientServer::OnSocketReadable([[maybe_unused]] const Poco::AutoPtr &pNf) { + void UI_WebSocketClientServer::OnSocketReadable( + [[maybe_unused]] const Poco::AutoPtr &pNf) { - UI_WebSocketClientServer::ClientList::iterator Client; - std::lock_guard G(LocalMutex_); + UI_WebSocketClientServer::ClientList::iterator Client; + std::lock_guard G(LocalMutex_); try { - Client = Clients_.find(pNf->socket().impl()->sockfd()); - if( Client == end(Clients_)) - return; + Client = Clients_.find(pNf->socket().impl()->sockfd()); + if (Client == end(Clients_)) + return; Poco::Buffer IncomingFrame(0); - int flags; - int n; + int flags; + int n; n = Client->second->WS_->receiveFrame(IncomingFrame, flags); auto Op = flags & Poco::Net::WebSocket::FRAME_OP_BITMASK; if (n == 0) { - poco_debug(Logger(),fmt::format("CLOSE({}): {} UI Client is closing WS connection.", Client->second->Id_, Client->second->UserName_)); + poco_debug(Logger(), + fmt::format("CLOSE({}): {} UI Client is closing WS connection.", + Client->second->Id_, Client->second->UserName_)); return EndConnection(Client); } switch (Op) { case Poco::Net::WebSocket::FRAME_OP_PING: { - Client->second->WS_->sendFrame("", 0, - (int)Poco::Net::WebSocket::FRAME_OP_PONG | - (int)Poco::Net::WebSocket::FRAME_FLAG_FIN); + Client->second->WS_->sendFrame("", 0, + (int)Poco::Net::WebSocket::FRAME_OP_PONG | + (int)Poco::Net::WebSocket::FRAME_FLAG_FIN); } break; case Poco::Net::WebSocket::FRAME_OP_PONG: { } break; case Poco::Net::WebSocket::FRAME_OP_CLOSE: { - poco_debug(Logger(),fmt::format("CLOSE({}): {} UI Client is closing WS connection.", Client->second->Id_, Client->second->UserName_)); - return EndConnection(Client); + poco_debug(Logger(), + fmt::format("CLOSE({}): {} UI Client is closing WS connection.", + Client->second->Id_, Client->second->UserName_)); + return EndConnection(Client); } break; case Poco::Net::WebSocket::FRAME_OP_TEXT: { constexpr const char *DropMessagesCommand = "drop-notifications"; @@ -228,80 +241,84 @@ namespace OpenWifi { auto Tokens = Utils::Split(Frame, ':'); bool Expired = false; #if not defined(TIP_SECURITY_SERVICE) - bool Contacted = false; + bool Contacted = false; #endif if (Tokens.size() == 2 && #if defined(TIP_SECURITY_SERVICE) - AuthService()->IsAuthorized(Tokens[1], Client->second->UserInfo_, TID_, Expired)) { + AuthService()->IsAuthorized(Tokens[1], Client->second->UserInfo_, TID_, + Expired)) { #else - AuthClient()->IsAuthorized(Tokens[1], Client->second->UserInfo_, TID_, Expired, Contacted)) { + AuthClient()->IsAuthorized(Tokens[1], Client->second->UserInfo_, TID_, + Expired, Contacted)) { #endif - Client->second->Authenticated_ = true; - Client->second->UserName_ = Client->second->UserInfo_.userinfo.email; - poco_debug(Logger(),fmt::format("START({}): {} UI Client is starting WS connection.", Client->second->Id_, Client->second->UserName_)); + Client->second->Authenticated_ = true; + Client->second->UserName_ = Client->second->UserInfo_.userinfo.email; + poco_debug(Logger(), + fmt::format("START({}): {} UI Client is starting WS connection.", + Client->second->Id_, Client->second->UserName_)); auto WelcomeMessage = NotificationTypesJSON_; WelcomeMessage.set("success", "Welcome! Bienvenue! Bienvenidos!"); std::ostringstream OS; WelcomeMessage.stringify(OS); - Client->second->WS_->sendFrame(OS.str().c_str(), (int) OS.str().size()); - Client->second->UserName_ = Client->second->UserInfo_.userinfo.email; + Client->second->WS_->sendFrame(OS.str().c_str(), (int)OS.str().size()); + Client->second->UserName_ = Client->second->UserInfo_.userinfo.email; } else { - Poco::JSON::Object WelcomeMessage; + Poco::JSON::Object WelcomeMessage; WelcomeMessage.set("error", "Invalid token. Closing connection."); std::ostringstream OS; WelcomeMessage.stringify(OS); - Client->second->WS_->sendFrame(OS.str().c_str(), (int) OS.str().size()); - return EndConnection(Client); + Client->second->WS_->sendFrame(OS.str().c_str(), (int)OS.str().size()); + return EndConnection(Client); } } else { - Poco::JSON::Parser P; - auto Obj = - P.parse(IncomingFrame.begin()).extract(); + Poco::JSON::Parser P; + auto Obj = P.parse(IncomingFrame.begin()).extract(); - if(Obj->has(DropMessagesCommand) && Obj->isArray(DropMessagesCommand)) { + if (Obj->has(DropMessagesCommand) && Obj->isArray(DropMessagesCommand)) { auto Filters = Obj->getArray(DropMessagesCommand); Client->second->Filter_.clear(); - for(const auto &Filter:*Filters) { - Client->second->Filter_.emplace_back( (std::uint64_t) Filter); + for (const auto &Filter : *Filters) { + Client->second->Filter_.emplace_back((std::uint64_t)Filter); } - std::sort(begin(Client->second->Filter_),end(Client->second->Filter_)); + std::sort(begin(Client->second->Filter_), end(Client->second->Filter_)); return; } - std::string Answer; - bool CloseConnection=false; - if (Processor_ != nullptr) { - Processor_->Processor(Obj, Answer, CloseConnection,Client->second->UserInfo_.userinfo); - } - if (!Answer.empty()) - Client->second->WS_->sendFrame(Answer.c_str(), (int)Answer.size()); - else { - Client->second->WS_->sendFrame("{}", 2); - } + std::string Answer; + bool CloseConnection = false; + if (Processor_ != nullptr) { + Processor_->Processor(Obj, Answer, CloseConnection, + Client->second->UserInfo_.userinfo); + } + if (!Answer.empty()) + Client->second->WS_->sendFrame(Answer.c_str(), (int)Answer.size()); + else { + Client->second->WS_->sendFrame("{}", 2); + } - if(CloseConnection) { - return EndConnection(Client); - } + if (CloseConnection) { + return EndConnection(Client); + } } } break; default: { } } } catch (...) { - return EndConnection(Client); + return EndConnection(Client); } } - void UI_WebSocketClientServer::OnSocketShutdown([[maybe_unused]] const Poco::AutoPtr &pNf) { - try { - std::lock_guard G(LocalMutex_); + void UI_WebSocketClientServer::OnSocketShutdown( + [[maybe_unused]] const Poco::AutoPtr &pNf) { + try { + std::lock_guard G(LocalMutex_); auto Client = Clients_.find(pNf->socket().impl()->sockfd()); - if (Client == end(Clients_)) - return; - EndConnection(Client); - } catch (...) { - - } + if (Client == end(Clients_)) + return; + EndConnection(Client); + } catch (...) { + } } } // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/UI_WebSocketClientServer.h b/src/framework/UI_WebSocketClientServer.h index e008faf..a727b4c 100644 --- a/src/framework/UI_WebSocketClientServer.h +++ b/src/framework/UI_WebSocketClientServer.h @@ -7,11 +7,11 @@ #include #include -#include "Poco/Runnable.h" -#include "Poco/Net/SocketReactor.h" -#include "Poco/Net/WebSocket.h" #include "Poco/JSON/Object.h" #include "Poco/Net/SocketNotification.h" +#include "Poco/Net/SocketReactor.h" +#include "Poco/Net/WebSocket.h" +#include "Poco/Runnable.h" #include "RESTObjects/RESTAPI_SecurityObjects.h" #include "framework/SubSystemServer.h" @@ -21,25 +21,28 @@ namespace OpenWifi { class UI_WebSocketClientProcessor { public: - virtual void Processor(const Poco::JSON::Object::Ptr &O, std::string &Answer, bool &Done , const SecurityObjects::UserInfo & UserInfo) = 0; + virtual void Processor(const Poco::JSON::Object::Ptr &O, std::string &Answer, bool &Done, + const SecurityObjects::UserInfo &UserInfo) = 0; + private: }; - struct UI_WebSocketClientInfo { - std::unique_ptr WS_ = nullptr; - std::string Id_; - std::string UserName_; - bool Authenticated_ = false; - bool SocketRegistered_=false; - std::vector Filter_; - SecurityObjects::UserInfoAndPolicy UserInfo_; + struct UI_WebSocketClientInfo { + std::unique_ptr WS_ = nullptr; + std::string Id_; + std::string UserName_; + bool Authenticated_ = false; + bool SocketRegistered_ = false; + std::vector Filter_; + SecurityObjects::UserInfoAndPolicy UserInfo_; - UI_WebSocketClientInfo(Poco::Net::WebSocket &WS, const std::string &Id, const std::string &username) { - WS_ = std::make_unique(WS); - Id_ = Id; - UserName_ = username; - } - }; + UI_WebSocketClientInfo(Poco::Net::WebSocket &WS, const std::string &Id, + const std::string &username) { + WS_ = std::make_unique(WS); + Id_ = Id; + UserName_ = username; + } + }; class UI_WebSocketClientServer : public SubSystemServer, Poco::Runnable { @@ -49,26 +52,26 @@ namespace OpenWifi { return instance_; } - bool IsAnyoneConnected() { - return UsersConnected_; - } + bool IsAnyoneConnected() { return UsersConnected_; } int Start() override; void Stop() override; void run() override; - Poco::Net::SocketReactor & Reactor() { return Reactor_; } - void NewClient(Poco::Net::WebSocket &WS, const std::string &Id, const std::string &UserName, std::uint64_t TID); + Poco::Net::SocketReactor &Reactor() { return Reactor_; } + void NewClient(Poco::Net::WebSocket &WS, const std::string &Id, const std::string &UserName, + std::uint64_t TID); void SetProcessor(UI_WebSocketClientProcessor *F); [[nodiscard]] inline bool GeoCodeEnabled() const { return GeoCodeEnabled_; } [[nodiscard]] inline std::string GoogleApiKey() const { return GoogleApiKey_; } - template bool - SendUserNotification(const std::string &userName, const WebSocketNotification &Notification) { + template + bool SendUserNotification(const std::string &userName, + const WebSocketNotification &Notification) { - Poco::JSON::Object Payload; + Poco::JSON::Object Payload; Notification.to_json(Payload); - Poco::JSON::Object Msg; - Msg.set("notification",Payload); + Poco::JSON::Object Msg; + Msg.set("notification", Payload); std::ostringstream OO; Msg.stringify(OO); @@ -76,58 +79,59 @@ namespace OpenWifi { } template void SendNotification(const WebSocketNotification &Notification) { - Poco::JSON::Object Payload; + Poco::JSON::Object Payload; Notification.to_json(Payload); - Poco::JSON::Object Msg; - Msg.set("notification",Payload); + Poco::JSON::Object Msg; + Msg.set("notification", Payload); std::ostringstream OO; Msg.stringify(OO); SendToAll(Notification.type_id, OO.str()); } - [[nodiscard]] bool SendToUser(const std::string &userName, std::uint64_t id, const std::string &Payload); + [[nodiscard]] bool SendToUser(const std::string &userName, std::uint64_t id, + const std::string &Payload); void SendToAll(std::uint64_t id, const std::string &Payload); struct NotificationEntry { - std::uint64_t id=0; - std::string helper; + std::uint64_t id = 0; + std::string helper; }; - using ClientList = std::map>; + using ClientList = std::map>; using NotificationTypeIdVec = std::vector; - void RegisterNotifications(const NotificationTypeIdVec & Notifications); + void RegisterNotifications(const NotificationTypeIdVec &Notifications); bool IsFiltered(std::uint64_t id, const UI_WebSocketClientInfo &Client); - private: - volatile bool Running_ = false; - std::atomic_uint64_t UsersConnected_=0; - Poco::Net::SocketReactor Reactor_; - Poco::Thread ReactorThread_; - Poco::Thread CleanerThread_; - std::recursive_mutex LocalMutex_; - bool GeoCodeEnabled_ = false; - std::string GoogleApiKey_; - ClientList Clients_; - UI_WebSocketClientProcessor *Processor_ = nullptr; - NotificationTypeIdVec NotificationTypes_; - Poco::JSON::Object NotificationTypesJSON_; - std::vector ToBeRemoved_; - std::uint64_t TID_=0; + private: + volatile bool Running_ = false; + std::atomic_uint64_t UsersConnected_ = 0; + Poco::Net::SocketReactor Reactor_; + Poco::Thread ReactorThread_; + Poco::Thread CleanerThread_; + std::recursive_mutex LocalMutex_; + bool GeoCodeEnabled_ = false; + std::string GoogleApiKey_; + ClientList Clients_; + UI_WebSocketClientProcessor *Processor_ = nullptr; + NotificationTypeIdVec NotificationTypes_; + Poco::JSON::Object NotificationTypesJSON_; + std::vector ToBeRemoved_; + std::uint64_t TID_ = 0; UI_WebSocketClientServer() noexcept; - void EndConnection(ClientList::iterator Client); + void EndConnection(ClientList::iterator Client); - void OnSocketReadable(const Poco::AutoPtr &pNf); - void OnSocketShutdown(const Poco::AutoPtr &pNf); - void OnSocketError(const Poco::AutoPtr &pNf); + void OnSocketReadable(const Poco::AutoPtr &pNf); + void OnSocketShutdown(const Poco::AutoPtr &pNf); + void OnSocketError(const Poco::AutoPtr &pNf); - ClientList::iterator FindWSClient( std::lock_guard &G, int ClientSocket); + ClientList::iterator FindWSClient(std::lock_guard &G, + int ClientSocket); void SortNotifications(); }; inline auto UI_WebSocketClientServer() { return UI_WebSocketClientServer::instance(); } -}; - +}; // namespace OpenWifi diff --git a/src/framework/WebSocketLogger.h b/src/framework/WebSocketLogger.h index 7c0553e..7b4174a 100644 --- a/src/framework/WebSocketLogger.h +++ b/src/framework/WebSocketLogger.h @@ -5,61 +5,64 @@ #pragma once #include "framework/SubSystemServer.h" -#include "framework/UI_WebSocketClientServer.h" #include "framework/UI_WebSocketClientNotifications.h" - +#include "framework/UI_WebSocketClientServer.h" namespace OpenWifi { class WebSocketLogger : public Poco::Channel { public: + WebSocketLogger() {} - WebSocketLogger() { - } + ~WebSocketLogger() {} - ~WebSocketLogger() { - } - - std::string getProperty( [[maybe_unused]] const std::string &p ) const { + std::string getProperty([[maybe_unused]] const std::string &p) const { std::cout << "WS getProperty" << std::endl; return ""; } - void close() final { - } + void close() final {} - void open() final { - } + void open() final {} static std::string to_string(Poco::Message::Priority p) { - switch(p) { - case Poco::Message::PRIO_INFORMATION: return "information"; - case Poco::Message::PRIO_CRITICAL: return "critical"; - case Poco::Message::PRIO_DEBUG: return "debug"; - case Poco::Message::PRIO_ERROR: return "error"; - case Poco::Message::PRIO_FATAL: return "fatal"; - case Poco::Message::PRIO_NOTICE: return "notice"; - case Poco::Message::PRIO_TRACE: return "trace"; - case Poco::Message::PRIO_WARNING: return "warning"; - default: return "none"; + switch (p) { + case Poco::Message::PRIO_INFORMATION: + return "information"; + case Poco::Message::PRIO_CRITICAL: + return "critical"; + case Poco::Message::PRIO_DEBUG: + return "debug"; + case Poco::Message::PRIO_ERROR: + return "error"; + case Poco::Message::PRIO_FATAL: + return "fatal"; + case Poco::Message::PRIO_NOTICE: + return "notice"; + case Poco::Message::PRIO_TRACE: + return "trace"; + case Poco::Message::PRIO_WARNING: + return "warning"; + default: + return "none"; } } struct NotificationLogMessage { - std::string msg; - std::string level; - std::uint64_t timestamp; - std::string source; - std::string thread_name; - std::uint64_t thread_id=0; + std::string msg; + std::string level; + std::uint64_t timestamp; + std::string source; + std::string thread_name; + std::uint64_t thread_id = 0; inline void to_json(Poco::JSON::Object &Obj) const { - RESTAPI_utils::field_to_json(Obj,"msg", msg); - RESTAPI_utils::field_to_json(Obj,"level", level); - RESTAPI_utils::field_to_json(Obj,"timestamp", timestamp); - RESTAPI_utils::field_to_json(Obj,"source", source); - RESTAPI_utils::field_to_json(Obj,"thread_name", thread_name); - RESTAPI_utils::field_to_json(Obj,"thread_id", thread_id); + RESTAPI_utils::field_to_json(Obj, "msg", msg); + RESTAPI_utils::field_to_json(Obj, "level", level); + RESTAPI_utils::field_to_json(Obj, "timestamp", timestamp); + RESTAPI_utils::field_to_json(Obj, "source", source); + RESTAPI_utils::field_to_json(Obj, "thread_name", thread_name); + RESTAPI_utils::field_to_json(Obj, "thread_id", thread_id); } inline bool from_json(const Poco::JSON::Object::Ptr &Obj) { @@ -71,18 +74,18 @@ namespace OpenWifi { RESTAPI_utils::field_from_json(Obj, "thread_name", thread_name); RESTAPI_utils::field_from_json(Obj, "thread_id", thread_id); return true; - } catch(...) { - + } catch (...) { } return false; } }; - typedef WebSocketNotification WebSocketClientNotificationLogMessage_t; + typedef WebSocketNotification + WebSocketClientNotificationLogMessage_t; void log(const Poco::Message &m) final { - if(UI_WebSocketClientServer()->IsAnyoneConnected()) { - WebSocketClientNotificationLogMessage_t Msg; + if (UI_WebSocketClientServer()->IsAnyoneConnected()) { + WebSocketClientNotificationLogMessage_t Msg; Msg.content.msg = m.getText(); Msg.content.level = WebSocketLogger::to_string(m.getPriority()); Msg.content.timestamp = m.getTime().epochTime(); @@ -94,14 +97,15 @@ namespace OpenWifi { } } - void setProperty([[maybe_unused]] const std::string &name, [[maybe_unused]] const std::string &value) { + void setProperty([[maybe_unused]] const std::string &name, + [[maybe_unused]] const std::string &value) { std::cout << "WS setProperty" << std::endl; } private: - std::recursive_mutex Mutex_; + std::recursive_mutex Mutex_; }; -// inline auto WebSocketLogger() { return WebSocketLogger::instance(); } + // inline auto WebSocketLogger() { return WebSocketLogger::instance(); } -} \ No newline at end of file +} // namespace OpenWifi \ No newline at end of file diff --git a/src/framework/orm.h b/src/framework/orm.h index 23eb591..9e83540 100644 --- a/src/framework/orm.h +++ b/src/framework/orm.h @@ -8,1015 +8,1030 @@ #pragma once -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include -#include "Poco/Tuple.h" -#include "Poco/Data/SessionPool.h" -#include "Poco/Data/Statement.h" #include "Poco/Data/RecordSet.h" #include "Poco/Data/SQLite/Connector.h" +#include "Poco/Data/SessionPool.h" +#include "Poco/Data/Statement.h" #include "Poco/Logger.h" #include "Poco/StringTokenizer.h" +#include "Poco/Tuple.h" #include "StorageClass.h" #include "fmt/format.h" namespace ORM { - enum FieldType { - FT_INT, - FT_BIGINT, - FT_TEXT, - FT_VARCHAR, - FT_BLOB, - FT_BOOLEAN, - FT_REAL - }; - - enum Indextype { - ASC, - DESC - }; - - struct Field { - std::string Name; - FieldType Type; - int Size=0; - bool Index=false; - - - Field(std::string N, FieldType T, int S=0, bool Index=false) : - Name(std::move(N)), - Type(T), - Size(S), - Index(Index) {} - - explicit Field(std::string N) : - Name(std::move(N)) - { - Type = FT_TEXT; - } - - Field(std::string N, int S) : - Name(std::move(N)), Size(S) - { - if(Size>0 && Size<255) - Type = FT_VARCHAR; - else - Type = FT_TEXT; - } - - Field(std::string N, int S, bool I): - Name(std::move(N)), Size(S), Index(I) - { - if(Size>0 && Size<255) - Type = FT_VARCHAR; - else - Type = FT_TEXT; - } - }; - typedef std::vector FieldVec; - - struct IndexEntry { - std::string FieldName; - Indextype Type; - }; - typedef std::vector IndexEntryVec; - - struct Index { - std::string Name; - IndexEntryVec Entries; - }; - typedef std::vector IndexVec; - - inline std::string FieldTypeToChar(OpenWifi::DBType Type, FieldType T, int Size=0) { - switch(T) { - case FT_INT: return "INT"; - case FT_BIGINT: return "BIGINT"; - case FT_TEXT: return "TEXT"; - case FT_BOOLEAN: return "BOOLEAN"; - case FT_VARCHAR: - if(Size) - return std::string("VARCHAR(") + std::to_string(Size) + std::string(")"); - else - return "TEXT"; - case FT_BLOB: - if(Type==OpenWifi::DBType::mysql) - return "LONGBLOB"; - else if(Type==OpenWifi::DBType::pgsql) - return "BYTEA"; - else - return "BLOB"; - case FT_REAL: - return "REAL"; - default: - assert(false); - - } - assert(false); - return ""; - } - - inline std::string Escape(const std::string &S) { - std::string R; - - for(const auto &i:S) { - if (i == '\'') - R += "''"; - else - R += i; - } - return R; - } - - inline std::string WHERE_AND_(std::string Result) { - return Result; - } - - template std::string WHERE_AND_(std::string Result, const char *fieldName, const T & Value, Args... args) { - if constexpr(std::is_same_v) - { - if(!Value.empty()) { - if(!Result.empty()) - Result += " and "; - Result += fieldName; - Result += '='; - Result += "'"; - Result += Escape(Value); - Result += "'"; - } - return WHERE_AND_(Result,args...); - } else if constexpr(std::is_same_v) { - if(*Value!=0) { - if(!Result.empty()) - Result += " and "; - Result += fieldName; - Result += '='; - Result += "'"; - Result += Escape(Value); - Result += "'"; - } - return WHERE_AND_(Result,args...); - } else if constexpr (std::is_same_v) { - if(!Result.empty()) - Result += " and "; - Result += fieldName; - Result += '='; - Result += Value ? "true" : "false"; - return WHERE_AND_(Result,args...); - } else if constexpr (std::is_arithmetic_v) { - if(!Result.empty()) - Result += " and "; - Result += fieldName ; - Result += '='; - Result += std::to_string(Value); - return WHERE_AND_(Result,args...); - } else { - assert(false); - } - return WHERE_AND_(Result,args...); - } - - template std::string WHERE_AND(Args... args) { - std::string Result; - return WHERE_AND_(Result, args...); - } - - enum SqlComparison { EQ = 0, NEQ, LT, LTE, GT, GTE }; - enum SqlBinaryOp { AND = 0 , OR }; - - static const std::vector BOPS{" and ", " or "}; - static const std::vector SQLCOMPS{"=","!=","<","<=",">",">="}; - - inline std::string to_string(uint64_t V) { - return std::to_string(V); - } - - inline std::string to_string(int V) { - return std::to_string(V); - } - - inline std::string to_string(bool V) { - return std::to_string(V); - } - - inline std::string to_string(const std::string &S) { - return S; - } - - inline std::string to_string(const Poco::Data::BLOB &blob) { - std::string result; - result.assign(blob.begin(),blob.end()); - return result; - } - - inline std::string to_string(const char * S) { - return S; - } - - template class DBCache { - public: - DBCache(unsigned Size, unsigned Timeout) : - Size_(Size), - Timeout_(Timeout) - { - - } - virtual void Create(const RecordType &R)=0; - virtual bool GetFromCache(const std::string &FieldName, const std::string &Value, RecordType &R)=0; - virtual void UpdateCache(const RecordType &R)=0; - virtual void Delete(const std::string &FieldName, const std::string &Value)=0; - private: - size_t Size_=0; - uint64_t Timeout_=0; - }; - - template class DB { - public: - - typedef const char * field_name_t; - - DB( OpenWifi::DBType dbtype, - const char *TableName, - const FieldVec & Fields, - const IndexVec & Indexes, - Poco::Data::SessionPool & Pool, - Poco::Logger &L, - const char *Prefix, - DBCache * Cache=nullptr): - TableName_(TableName), - Type_(dbtype), - Pool_(Pool), - Logger_(L), - Prefix_(Prefix), - Cache_(Cache) - { - assert(RecordTuple::length == Fields.size()); - - bool first = true; - int Place=0; - - for(const auto &i:Fields) { - std::string FieldName = Poco::toLower(i.Name); - FieldNames_[FieldName] = Place; - if(!first) { - CreateFields_ += ", "; - SelectFields_ += ", "; - UpdateFields_ += ", "; - SelectList_ += ", "; - } else { - SelectList_ += "("; - } - - CreateFields_ += FieldName + " " + FieldTypeToChar(Type_, i.Type,i.Size) + (i.Index ? " unique primary key" : ""); - SelectFields_ += FieldName ; - UpdateFields_ += FieldName + "=?"; - SelectList_ += "?"; - first = false; - Place++; - } - SelectList_ += ")"; - - if(!Indexes.empty()) { - if(Type_==OpenWifi::DBType::sqlite || Type_==OpenWifi::DBType::pgsql) { - for(const auto &j:Indexes) { - std::string IndexLine; - - IndexLine = std::string("CREATE INDEX IF NOT EXISTS ") + j.Name + std::string(" ON ") + TableName_+ " ("; - bool first_entry=true; - for(const auto &k:j.Entries) { - auto IndexFieldName = Poco::toLower(k.FieldName); - assert(ValidFieldName(IndexFieldName)); - if(!first_entry) { - IndexLine += " , "; - } - first_entry = false; - IndexLine += IndexFieldName + std::string(" ") + std::string(k.Type == Indextype::ASC ? "ASC" : "DESC") ; - } - IndexLine += " )"; - IndexCreation_.template emplace_back(IndexLine); - } - } else if(Type_==OpenWifi::DBType::mysql) { - bool firstIndex = true; - std::string IndexLine; - for(const auto &j:Indexes) { - if(!firstIndex) - IndexLine += ", "; - firstIndex = false; - IndexLine += " INDEX " + j.Name + " ( " ; - bool first_entry=true; - for(const auto &k:j.Entries) { - auto IndexFieldName = Poco::toLower(k.FieldName); - assert(FieldNames_.find(IndexFieldName) != FieldNames_.end()); - if(!first_entry) { - IndexLine += " ,"; - } - first_entry = false; - IndexLine += IndexFieldName + std::string(k.Type == Indextype::ASC ? " ASC" : " DESC"); - } - IndexLine += " ) "; - } - IndexCreation_.template emplace_back(IndexLine); - } - } - } - - [[nodiscard]] const std::string & CreateFields() const { return CreateFields_; }; - [[nodiscard]] const std::string & SelectFields() const { return SelectFields_; }; - [[nodiscard]] const std::string & SelectList() const { return SelectList_; }; - [[nodiscard]] const std::string & UpdateFields() const { return UpdateFields_; }; - - inline std::string OP(field_name_t F, SqlComparison O , bool V) { - assert(ValidFieldName(F)); - return std::string{"("} + F + SQLCOMPS[O] + (V ? "true" : "false") + ")" ; - } - - inline std::string OP(field_name_t F, SqlComparison O , int V) { - assert(ValidFieldName(F)); - return std::string{"("} + F + SQLCOMPS[O] + std::to_string(V) + ")" ; - } - - inline std::string OP(field_name_t F, SqlComparison O , uint64_t V) { - assert(ValidFieldName(F)); - return std::string{"("} + F + SQLCOMPS[O] + std::to_string(V) + ")" ; - } - - std::string OP(field_name_t F, SqlComparison O , const std::string & V) { - assert(ValidFieldName(F)); - return std::string{"("} + F + SQLCOMPS[O] + "'" + Escape(V) + "')" ; - } - - std::string OP(field_name_t F, SqlComparison O , const char * V) { - assert(ValidFieldName(F)); - return std::string{"("} + F + SQLCOMPS[O] + "'" + Escape(V) + "')" ; - } - - static std::string OP( const std::string &P1, SqlBinaryOp BOP , const std::string &P2) { - return std::string("(")+P1 + BOPS[BOP] + P2 +")"; - } - - std::string OP( [[maybe_unused]] bool Paran, const std::string &P1, SqlBinaryOp BOP , const std::string &P2) { - return P1 + BOPS[BOP] + P2 +")"; - } - - template std::string OP( bool ParanOpen, const std::string &P1, SqlBinaryOp BOP , const std::string &P2, Others... More) { - return P1 + BOPS[BOP] + OP(ParanOpen, P2, More...) + ")"; - } - - template std::string OP( const std::string &P1, SqlBinaryOp BOP , const std::string &P2, Others... More) { - return std::string{"("} + P1 + BOPS[BOP] + OP(true, P2, More...); - } - - bool Upgrade() { - uint32_t To; - return Upgrade(0, To); - } - - inline bool Create() { - switch(Type_) { - case OpenWifi::DBType::mysql: { - try { - Poco::Data::Session Session = Pool_.get(); - std::string Statement = IndexCreation_.empty() ? "create table if not exists " + TableName_ +" ( " + CreateFields_ + " )" : - "create table if not exists " + TableName_ +" ( " + CreateFields_ + " ), " + IndexCreation_[0] + " )"; - Session << Statement , Poco::Data::Keywords::now; - } catch (const Poco::Exception &E) { - Logger_.error("Failure to create MySQL DB resources."); - Logger_.log(E); - } - } - break; - - case OpenWifi::DBType::sqlite: { - try { - Poco::Data::Session Session = Pool_.get(); - std::string Statement = "create table if not exists " + TableName_ + " ( " + CreateFields_ + " )"; - Session << Statement , Poco::Data::Keywords::now; - for(const auto &i:IndexCreation_) { - Session << i , Poco::Data::Keywords::now; - } - } catch (const Poco::Exception &E) { - Logger_.error("Failure to create SQLITE DB resources."); - Logger_.log(E); - } - } - break; - - case OpenWifi::DBType::pgsql: { - try { - Poco::Data::Session Session = Pool_.get(); - std::string Statement = "create table if not exists " + TableName_ + " ( " + CreateFields_ + " )"; - Session << Statement , Poco::Data::Keywords::now; - for(const auto &i:IndexCreation_) { - Session << i , Poco::Data::Keywords::now; - } - } catch (const Poco::Exception &E) { - Logger_.error("Failure to create POSTGRESQL DB resources."); - Logger_.log(E); - } - } - break; - } - return Upgrade(); - } - - [[nodiscard]] std::string ConvertParams(const std::string & S) const { - if(Type_!=OpenWifi::DBType::pgsql) - return S; - - std::string R; - R.reserve(S.size()*2+1); - auto Idx=1; - for(auto const & i:S) - { - if(i=='?') { - R += '$'; - R.append(std::to_string(Idx++)); - } else { - R += i; - } - } - - return R; - } - - void Convert( const RecordTuple &in , RecordType &out); - void Convert( const RecordType &in , RecordTuple &out); - - inline const std::string & Prefix() { return Prefix_; }; - - bool CreateRecord( const RecordType & R) { - try { - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Insert(Session); - - RecordTuple RT; - Convert(R, RT); - std::string St = "insert into " + TableName_ + " ( " + SelectFields_ + " ) values " + SelectList_; - Insert << ConvertParams(St) , - Poco::Data::Keywords::use(RT); - Insert.execute(); - - if(Cache_) - Cache_->Create(R); - return true; - - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - template bool GetRecord(field_name_t FieldName, const T & Value, RecordType & R) { - try { - assert(ValidFieldName(FieldName)); - - if(Cache_) { - if(Cache_->GetFromCache(FieldName, Value, R)) - return true; - } - - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Select(Session); - RecordTuple RT; - - std::string St = "select " + SelectFields_ + " from " + TableName_ + " where " + FieldName + "=?" + " limit 1"; - - auto tValue{Value}; - - Select << ConvertParams(St) , - Poco::Data::Keywords::into(RT), - Poco::Data::Keywords::use(tValue); - Select.execute(); - - if(Select.execute()==1) { - Convert(RT,R); - if(Cache_) - Cache_->UpdateCache(R); - return true; - } - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - bool GetRecord(RecordType & T , const std::string &WhereClause) { - try { - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Select(Session); - RecordTuple RT; - - std::string St = "select " + SelectFields_ + " from " + TableName_ + " where " + WhereClause + " limit 1"; - - Select << ConvertParams(St) , - Poco::Data::Keywords::into(RT); - Select.execute(); - - if(Select.execute()==1) { - Convert(RT,T); - if(Cache_) - Cache_->UpdateCache(T); - return true; - } - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - typedef std::vector StringVec; - - template < typename T, - typename T0, typename T1> bool GR(field_name_t FieldName, T & R,T0 &V0, T1 &V1) { - try { - - assert( ValidFieldName(FieldName) ); - - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Select(Session); - RecordTuple RT; - - std::string St = "select " + SelectFields_ + " from " + TableName_ - + " where " + FieldName[0] + "=? and " + FieldName[1] + "=?" ; - Select << ConvertParams(St) , - Poco::Data::Keywords::into(RT), - Poco::Data::Keywords::use(V0), - Poco::Data::Keywords::use(V1); - - if(Select.execute()==1) { - Convert(RT,R); - return true; - } - return true; - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - template bool Join(const std::string &statement, std::vector &records) { - try { - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Select(Session); - - Select << statement , - Poco::Data::Keywords::into(records); - Select.execute(); - return true; - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - typedef std::vector RecordList; - typedef std::vector RecordVec; - typedef RecordType RecordName; - - bool GetRecords( uint64_t Offset, uint64_t HowMany, RecordVec & Records, const std::string & Where = "", const std::string & OrderBy = "") { - try { - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Select(Session); - RecordList RL; - std::string St = "select " + SelectFields_ + " from " + TableName_ + - (Where.empty() ? "" : " where " + Where) + OrderBy + - ComputeRange(Offset, HowMany) ; - - Select << St , - Poco::Data::Keywords::into(RL); - Select.execute(); - - if(Select.rowsExtracted()>0) { - for(auto &i:RL) { - RecordType R; - Convert(i, R); - Records.template emplace_back(R); - } - return true; - } - return false; - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - template bool UpdateRecord(field_name_t FieldName, const T & Value, const RecordType & R) { - try { - assert( ValidFieldName(FieldName) ); - - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Update(Session); - - RecordTuple RT; - - Convert(R, RT); - - auto tValue(Value); - - std::string St = "update " + TableName_ + " set " + UpdateFields_ + " where " + FieldName + "=?" ; - Update << ConvertParams(St) , - Poco::Data::Keywords::use(RT), - Poco::Data::Keywords::use(tValue); - Update.execute(); - if(Cache_) - Cache_->UpdateCache(R); - return true; - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - bool RunStatement(const std::string &St) { - try { - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Command(Session); - - Command << St ; - Command.execute(); - - return true; - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - template bool ReplaceRecord(field_name_t FieldName, const T & Value, RecordType & R) { - try { - if(Exists(FieldName, Value)) { - return UpdateRecord(FieldName,Value,R); - } - return CreateRecord(R); - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - template bool GetNameAndDescription(field_name_t FieldName, const T & Value, std::string & Name, std::string & Description ) { - try { - assert( ValidFieldName(FieldName) ); - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Select(Session); - RecordTuple RT; - - std::string St = "select " + SelectFields_ + " from " + TableName_ + " where " + FieldName + "=?" ; - RecordType R; - auto tValue{Value}; - Select << ConvertParams(St) , - Poco::Data::Keywords::into(RT), - Poco::Data::Keywords::use(tValue); - - if(Select.execute()==1) { - Convert(RT,R); - Name = R.info.name; - Description = R.info.description; - return true; - } - return false; - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - template bool DeleteRecord(field_name_t FieldName, const T & Value) { - try { - assert( ValidFieldName(FieldName) ); - - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Delete(Session); - - std::string St = "delete from " + TableName_ + " where " + FieldName + "=?" ; - auto tValue{Value}; - - Delete << ConvertParams(St) , - Poco::Data::Keywords::use(tValue); - Delete.execute(); - if(Cache_) - Cache_->Delete(FieldName, Value); - return true; - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - bool DeleteRecords( const std::string & WhereClause ) { - try { - assert( !WhereClause.empty()); - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Delete(Session); - - std::string St = "delete from " + TableName_ + " where " + WhereClause; - Delete << St; - Delete.execute(); - return true; - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - bool Exists(field_name_t FieldName, const std::string & Value) { - try { - assert( ValidFieldName(FieldName) ); - - RecordType R; - if(GetRecord(FieldName,Value,R)) - return true; - return false; - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - bool Iterate( std::function F, const std::string & WhereClause = "" ) { - try { - - uint64_t Offset=0; - uint64_t Batch=50; - bool Done=false; - while(!Done) { - std::vector Records; - if(GetRecords(Offset,Batch,Records, WhereClause)) { - for(const auto &i:Records) { - if(!F(i)) - return true; - } - if(Records.size() bool ManipulateVectorMember( X T, field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID, bool Add) { - try { - assert( ValidFieldName(FieldName) ); - - RecordType R; - if(GetRecord(FieldName, ParentUUID, R)) { - auto it = std::find((R.*T).begin(),(R.*T).end(),ChildUUID); - if(Add) { - if(it!=(R.*T).end() && *it == ChildUUID) - return false; - (R.*T).push_back(ChildUUID); - std::sort((R.*T).begin(),(R.*T).end()); - } else { - if(it!=(R.*T).end() && *it == ChildUUID) - (R.*T).erase(it); - else - return false; - } - UpdateRecord(FieldName, ParentUUID, R); - return true; - } - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - bool RunScript(const std::vector & Statements, bool IgnoreExceptions=true) { - try { - Poco::Data::Session Session = Pool_.get(); - Poco::Data::Statement Command(Session); - - for(const auto &i:Statements) { - try { - Command << i, Poco::Data::Keywords::now; - } catch (const Poco::Exception &E) { - // Logger_.log(E); - // Logger_.error(Poco::format("The following statement '%s' generated an exception during a table upgrade. This may or may not be a problem.", i)); - if(!IgnoreExceptions) { - return false; - } - } - Command.reset(Session); - } - return true; - } catch (const Poco::Exception &E) { - Logger_.log(E); - } - return false; - } - - virtual uint32_t Version() { - return 0; - } - - virtual bool Upgrade(uint32_t from, uint32_t &to) { - to = from; - return true; - } - - inline bool AddChild(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::children, FieldName, ParentUUID, ChildUUID, true); - } - - inline bool DeleteChild(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::children, FieldName, ParentUUID, ChildUUID, false); - } - - inline bool AddLocation(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::locations, FieldName, ParentUUID, ChildUUID, true); - } - - inline bool DeleteLocation(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::locations, FieldName, ParentUUID, ChildUUID, false); - } - - inline bool AddContact(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::contacts, FieldName, ParentUUID, ChildUUID, true); - } - - inline bool DeleteContact(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::contacts, FieldName, ParentUUID, ChildUUID, false); - } - - inline bool AddVenue(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::venues, FieldName, ParentUUID, ChildUUID, true); - } - - inline bool DeleteVenue(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::venues, FieldName, ParentUUID, ChildUUID, false); - } - - inline bool AddDevice(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::devices, FieldName, ParentUUID, ChildUUID, true); - } - - inline bool DeleteDevice(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::devices, FieldName, ParentUUID, ChildUUID, false); - } - - inline bool AddEntity(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::entities, FieldName, ParentUUID, ChildUUID, true); - } - - inline bool DeleteEntity(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::entities, FieldName, ParentUUID, ChildUUID, false); - } - - inline bool AddUser(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::users, FieldName, ParentUUID, ChildUUID, true); - } - - inline bool DelUser(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::users, FieldName, ParentUUID, ChildUUID, false); - } - - inline bool AddConfiguration(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::deviceConfiguration, FieldName, ParentUUID, ChildUUID, true); - } - - inline bool DelConfiguration(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::deviceConfiguration, FieldName, ParentUUID, ChildUUID, false); - } - - inline bool AddVariable(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::variables, FieldName, ParentUUID, ChildUUID, true); - } - - inline bool DelVariable(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { - return ManipulateVectorMember(&RecordType::variables, FieldName, ParentUUID, ChildUUID, false); - } - - inline bool AddInUse(field_name_t FieldName, const std::string & ParentUUID, const std::string & Prefix, const std::string & ChildUUID) { - std::string FakeUUID{ Prefix + ":" + ChildUUID}; - return ManipulateVectorMember(&RecordType::inUse,FieldName, ParentUUID, FakeUUID, true); - } - - inline bool DeleteInUse(field_name_t FieldName, const std::string & ParentUUID, const std::string & Prefix, const std::string & ChildUUID) { - std::string FakeUUID{ Prefix + ":" + ChildUUID}; - return ManipulateVectorMember(&RecordType::inUse,FieldName, ParentUUID, FakeUUID, false); - } - - inline bool GetInUse(field_name_t FieldName, const std::string & UUID, std::vector & UUIDs ) { - RecordType R; - if(GetRecord(FieldName,UUID,R)) { - UUIDs = R.inUse; - return true; - } - return false; - } - - inline bool ValidFieldName(const std::string &FieldName) { - return FieldNames_.find(Poco::toLower(FieldName)) != FieldNames_.end(); - } - - inline bool ValidFieldName(const char *FieldName) { - std::string Field{FieldName}; - return ValidFieldName(Field); - } - - [[nodiscard]] inline std::string ComputeRange(uint64_t From, uint64_t HowMany) { - if(From<1) From=0; - switch(Type_) { - case OpenWifi::DBType::sqlite: - return " LIMIT " + std::to_string(From) + ", " + std::to_string(HowMany) + " "; - case OpenWifi::DBType::pgsql: - return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + " "; - case OpenWifi::DBType::mysql: - return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + " "; - default: - return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + " "; - } - } - - Poco::Logger & Logger() { return Logger_; } - - inline bool DeleteRecordsFromCache(const char *FieldName, const std::string &Value ) { - if(Cache_) - Cache_->Delete(FieldName, Value); - return true; - } - - inline void GetFieldNames( OpenWifi::Types::StringVec & F) { - for(const auto &[field,_]:FieldNames_) - F.push_back(field); - } - - protected: - std::string TableName_; - OpenWifi::DBType Type_; - Poco::Data::SessionPool &Pool_; - Poco::Logger &Logger_; - std::string Prefix_; - DBCache *Cache_= nullptr; - private: - std::string CreateFields_; - std::string SelectFields_; - std::string SelectList_; - std::string UpdateFields_; - std::vector IndexCreation_; - std::map FieldNames_; - }; -} - + enum FieldType { FT_INT, FT_BIGINT, FT_TEXT, FT_VARCHAR, FT_BLOB, FT_BOOLEAN, FT_REAL }; + + enum Indextype { ASC, DESC }; + + struct Field { + std::string Name; + FieldType Type; + int Size = 0; + bool Index = false; + + Field(std::string N, FieldType T, int S = 0, bool Index = false) + : Name(std::move(N)), Type(T), Size(S), Index(Index) {} + + explicit Field(std::string N) : Name(std::move(N)) { Type = FT_TEXT; } + + Field(std::string N, int S) : Name(std::move(N)), Size(S) { + if (Size > 0 && Size < 255) + Type = FT_VARCHAR; + else + Type = FT_TEXT; + } + + Field(std::string N, int S, bool I) : Name(std::move(N)), Size(S), Index(I) { + if (Size > 0 && Size < 255) + Type = FT_VARCHAR; + else + Type = FT_TEXT; + } + }; + typedef std::vector FieldVec; + + struct IndexEntry { + std::string FieldName; + Indextype Type; + }; + typedef std::vector IndexEntryVec; + + struct Index { + std::string Name; + IndexEntryVec Entries; + }; + typedef std::vector IndexVec; + + inline std::string FieldTypeToChar(OpenWifi::DBType Type, FieldType T, int Size = 0) { + switch (T) { + case FT_INT: + return "INT"; + case FT_BIGINT: + return "BIGINT"; + case FT_TEXT: + return "TEXT"; + case FT_BOOLEAN: + return "BOOLEAN"; + case FT_VARCHAR: + if (Size) + return std::string("VARCHAR(") + std::to_string(Size) + std::string(")"); + else + return "TEXT"; + case FT_BLOB: + if (Type == OpenWifi::DBType::mysql) + return "LONGBLOB"; + else if (Type == OpenWifi::DBType::pgsql) + return "BYTEA"; + else + return "BLOB"; + case FT_REAL: + return "REAL"; + default: + assert(false); + } + assert(false); + return ""; + } + + inline std::string Escape(const std::string &S) { + std::string R; + + for (const auto &i : S) { + if (i == '\'') + R += "''"; + else + R += i; + } + return R; + } + + inline std::string WHERE_AND_(std::string Result) { return Result; } + + template + std::string WHERE_AND_(std::string Result, const char *fieldName, const T &Value, + Args... args) { + if constexpr (std::is_same_v) { + if (!Value.empty()) { + if (!Result.empty()) + Result += " and "; + Result += fieldName; + Result += '='; + Result += "'"; + Result += Escape(Value); + Result += "'"; + } + return WHERE_AND_(Result, args...); + } else if constexpr (std::is_same_v) { + if (*Value != 0) { + if (!Result.empty()) + Result += " and "; + Result += fieldName; + Result += '='; + Result += "'"; + Result += Escape(Value); + Result += "'"; + } + return WHERE_AND_(Result, args...); + } else if constexpr (std::is_same_v) { + if (!Result.empty()) + Result += " and "; + Result += fieldName; + Result += '='; + Result += Value ? "true" : "false"; + return WHERE_AND_(Result, args...); + } else if constexpr (std::is_arithmetic_v) { + if (!Result.empty()) + Result += " and "; + Result += fieldName; + Result += '='; + Result += std::to_string(Value); + return WHERE_AND_(Result, args...); + } else { + assert(false); + } + return WHERE_AND_(Result, args...); + } + + template std::string WHERE_AND(Args... args) { + std::string Result; + return WHERE_AND_(Result, args...); + } + + enum SqlComparison { EQ = 0, NEQ, LT, LTE, GT, GTE }; + enum SqlBinaryOp { AND = 0, OR }; + + static const std::vector BOPS{" and ", " or "}; + static const std::vector SQLCOMPS{"=", "!=", "<", "<=", ">", ">="}; + + inline std::string to_string(uint64_t V) { return std::to_string(V); } + + inline std::string to_string(int V) { return std::to_string(V); } + + inline std::string to_string(bool V) { return std::to_string(V); } + + inline std::string to_string(const std::string &S) { return S; } + + inline std::string to_string(const Poco::Data::BLOB &blob) { + std::string result; + result.assign(blob.begin(), blob.end()); + return result; + } + + inline std::string to_string(const char *S) { return S; } + + template class DBCache { + public: + DBCache(unsigned Size, unsigned Timeout) : Size_(Size), Timeout_(Timeout) {} + virtual void Create(const RecordType &R) = 0; + virtual bool GetFromCache(const std::string &FieldName, const std::string &Value, + RecordType &R) = 0; + virtual void UpdateCache(const RecordType &R) = 0; + virtual void Delete(const std::string &FieldName, const std::string &Value) = 0; + + private: + size_t Size_ = 0; + uint64_t Timeout_ = 0; + }; + + template class DB { + public: + typedef const char *field_name_t; + + DB(OpenWifi::DBType dbtype, const char *TableName, const FieldVec &Fields, + const IndexVec &Indexes, Poco::Data::SessionPool &Pool, Poco::Logger &L, + const char *Prefix, DBCache *Cache = nullptr) + : TableName_(TableName), Type_(dbtype), Pool_(Pool), Logger_(L), Prefix_(Prefix), + Cache_(Cache) { + assert(RecordTuple::length == Fields.size()); + + bool first = true; + int Place = 0; + + for (const auto &i : Fields) { + std::string FieldName = Poco::toLower(i.Name); + FieldNames_[FieldName] = Place; + if (!first) { + CreateFields_ += ", "; + SelectFields_ += ", "; + UpdateFields_ += ", "; + SelectList_ += ", "; + } else { + SelectList_ += "("; + } + + CreateFields_ += FieldName + " " + FieldTypeToChar(Type_, i.Type, i.Size) + + (i.Index ? " unique primary key" : ""); + SelectFields_ += FieldName; + UpdateFields_ += FieldName + "=?"; + SelectList_ += "?"; + first = false; + Place++; + } + SelectList_ += ")"; + + if (!Indexes.empty()) { + if (Type_ == OpenWifi::DBType::sqlite || Type_ == OpenWifi::DBType::pgsql) { + for (const auto &j : Indexes) { + std::string IndexLine; + + IndexLine = std::string("CREATE INDEX IF NOT EXISTS ") + j.Name + + std::string(" ON ") + TableName_ + " ("; + bool first_entry = true; + for (const auto &k : j.Entries) { + auto IndexFieldName = Poco::toLower(k.FieldName); + assert(ValidFieldName(IndexFieldName)); + if (!first_entry) { + IndexLine += " , "; + } + first_entry = false; + IndexLine += IndexFieldName + std::string(" ") + + std::string(k.Type == Indextype::ASC ? "ASC" : "DESC"); + } + IndexLine += " )"; + IndexCreation_.template emplace_back(IndexLine); + } + } else if (Type_ == OpenWifi::DBType::mysql) { + bool firstIndex = true; + std::string IndexLine; + for (const auto &j : Indexes) { + if (!firstIndex) + IndexLine += ", "; + firstIndex = false; + IndexLine += " INDEX " + j.Name + " ( "; + bool first_entry = true; + for (const auto &k : j.Entries) { + auto IndexFieldName = Poco::toLower(k.FieldName); + assert(FieldNames_.find(IndexFieldName) != FieldNames_.end()); + if (!first_entry) { + IndexLine += " ,"; + } + first_entry = false; + IndexLine += IndexFieldName + + std::string(k.Type == Indextype::ASC ? " ASC" : " DESC"); + } + IndexLine += " ) "; + } + IndexCreation_.template emplace_back(IndexLine); + } + } + } + + [[nodiscard]] const std::string &CreateFields() const { return CreateFields_; }; + [[nodiscard]] const std::string &SelectFields() const { return SelectFields_; }; + [[nodiscard]] const std::string &SelectList() const { return SelectList_; }; + [[nodiscard]] const std::string &UpdateFields() const { return UpdateFields_; }; + + inline std::string OP(field_name_t F, SqlComparison O, bool V) { + assert(ValidFieldName(F)); + return std::string{"("} + F + SQLCOMPS[O] + (V ? "true" : "false") + ")"; + } + + inline std::string OP(field_name_t F, SqlComparison O, int V) { + assert(ValidFieldName(F)); + return std::string{"("} + F + SQLCOMPS[O] + std::to_string(V) + ")"; + } + + inline std::string OP(field_name_t F, SqlComparison O, uint64_t V) { + assert(ValidFieldName(F)); + return std::string{"("} + F + SQLCOMPS[O] + std::to_string(V) + ")"; + } + + std::string OP(field_name_t F, SqlComparison O, const std::string &V) { + assert(ValidFieldName(F)); + return std::string{"("} + F + SQLCOMPS[O] + "'" + Escape(V) + "')"; + } + + std::string OP(field_name_t F, SqlComparison O, const char *V) { + assert(ValidFieldName(F)); + return std::string{"("} + F + SQLCOMPS[O] + "'" + Escape(V) + "')"; + } + + static std::string OP(const std::string &P1, SqlBinaryOp BOP, const std::string &P2) { + return std::string("(") + P1 + BOPS[BOP] + P2 + ")"; + } + + std::string OP([[maybe_unused]] bool Paran, const std::string &P1, SqlBinaryOp BOP, + const std::string &P2) { + return P1 + BOPS[BOP] + P2 + ")"; + } + + template + std::string OP(bool ParanOpen, const std::string &P1, SqlBinaryOp BOP, + const std::string &P2, Others... More) { + return P1 + BOPS[BOP] + OP(ParanOpen, P2, More...) + ")"; + } + + template + std::string OP(const std::string &P1, SqlBinaryOp BOP, const std::string &P2, + Others... More) { + return std::string{"("} + P1 + BOPS[BOP] + OP(true, P2, More...); + } + + bool Upgrade() { + uint32_t To; + return Upgrade(0, To); + } + + inline bool Create() { + switch (Type_) { + case OpenWifi::DBType::mysql: { + try { + Poco::Data::Session Session = Pool_.get(); + std::string Statement = IndexCreation_.empty() + ? "create table if not exists " + TableName_ + + " ( " + CreateFields_ + " )" + : "create table if not exists " + TableName_ + + " ( " + CreateFields_ + " ), " + + IndexCreation_[0] + " )"; + Session << Statement, Poco::Data::Keywords::now; + } catch (const Poco::Exception &E) { + Logger_.error("Failure to create MySQL DB resources."); + Logger_.log(E); + } + } break; + + case OpenWifi::DBType::sqlite: { + try { + Poco::Data::Session Session = Pool_.get(); + std::string Statement = + "create table if not exists " + TableName_ + " ( " + CreateFields_ + " )"; + Session << Statement, Poco::Data::Keywords::now; + for (const auto &i : IndexCreation_) { + Session << i, Poco::Data::Keywords::now; + } + } catch (const Poco::Exception &E) { + Logger_.error("Failure to create SQLITE DB resources."); + Logger_.log(E); + } + } break; + + case OpenWifi::DBType::pgsql: { + try { + Poco::Data::Session Session = Pool_.get(); + std::string Statement = + "create table if not exists " + TableName_ + " ( " + CreateFields_ + " )"; + Session << Statement, Poco::Data::Keywords::now; + for (const auto &i : IndexCreation_) { + Session << i, Poco::Data::Keywords::now; + } + } catch (const Poco::Exception &E) { + Logger_.error("Failure to create POSTGRESQL DB resources."); + Logger_.log(E); + } + } break; + } + return Upgrade(); + } + + [[nodiscard]] std::string ConvertParams(const std::string &S) const { + if (Type_ != OpenWifi::DBType::pgsql) + return S; + + std::string R; + R.reserve(S.size() * 2 + 1); + auto Idx = 1; + for (auto const &i : S) { + if (i == '?') { + R += '$'; + R.append(std::to_string(Idx++)); + } else { + R += i; + } + } + + return R; + } + + void Convert(const RecordTuple &in, RecordType &out); + void Convert(const RecordType &in, RecordTuple &out); + + inline const std::string &Prefix() { return Prefix_; }; + + bool CreateRecord(const RecordType &R) { + try { + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Insert(Session); + + RecordTuple RT; + Convert(R, RT); + std::string St = "insert into " + TableName_ + " ( " + SelectFields_ + + " ) values " + SelectList_; + Insert << ConvertParams(St), Poco::Data::Keywords::use(RT); + Insert.execute(); + + if (Cache_) + Cache_->Create(R); + return true; + + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + template + bool GetRecord(field_name_t FieldName, const T &Value, RecordType &R) { + try { + assert(ValidFieldName(FieldName)); + + if (Cache_) { + if (Cache_->GetFromCache(FieldName, Value, R)) + return true; + } + + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Select(Session); + RecordTuple RT; + + std::string St = "select " + SelectFields_ + " from " + TableName_ + " where " + + FieldName + "=?" + " limit 1"; + + auto tValue{Value}; + + Select << ConvertParams(St), Poco::Data::Keywords::into(RT), + Poco::Data::Keywords::use(tValue); + Select.execute(); + + if (Select.execute() == 1) { + Convert(RT, R); + if (Cache_) + Cache_->UpdateCache(R); + return true; + } + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + bool GetRecord(RecordType &T, const std::string &WhereClause) { + try { + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Select(Session); + RecordTuple RT; + + std::string St = "select " + SelectFields_ + " from " + TableName_ + " where " + + WhereClause + " limit 1"; + + Select << ConvertParams(St), Poco::Data::Keywords::into(RT); + Select.execute(); + + if (Select.execute() == 1) { + Convert(RT, T); + if (Cache_) + Cache_->UpdateCache(T); + return true; + } + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + typedef std::vector StringVec; + + template + bool GR(field_name_t FieldName, T &R, T0 &V0, T1 &V1) { + try { + + assert(ValidFieldName(FieldName)); + + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Select(Session); + RecordTuple RT; + + std::string St = "select " + SelectFields_ + " from " + TableName_ + " where " + + FieldName[0] + "=? and " + FieldName[1] + "=?"; + Select << ConvertParams(St), Poco::Data::Keywords::into(RT), + Poco::Data::Keywords::use(V0), Poco::Data::Keywords::use(V1); + + if (Select.execute() == 1) { + Convert(RT, R); + return true; + } + return true; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + template bool Join(const std::string &statement, std::vector &records) { + try { + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Select(Session); + + Select << statement, Poco::Data::Keywords::into(records); + Select.execute(); + return true; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + typedef std::vector RecordList; + typedef std::vector RecordVec; + typedef RecordType RecordName; + + bool GetRecords(uint64_t Offset, uint64_t HowMany, RecordVec &Records, + const std::string &Where = "", const std::string &OrderBy = "") { + try { + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Select(Session); + RecordList RL; + std::string St = "select " + SelectFields_ + " from " + TableName_ + + (Where.empty() ? "" : " where " + Where) + OrderBy + + ComputeRange(Offset, HowMany); + + Select << St, Poco::Data::Keywords::into(RL); + Select.execute(); + + if (Select.rowsExtracted() > 0) { + for (auto &i : RL) { + RecordType R; + Convert(i, R); + Records.template emplace_back(R); + } + return true; + } + return false; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + template + bool UpdateRecord(field_name_t FieldName, const T &Value, const RecordType &R) { + try { + assert(ValidFieldName(FieldName)); + + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Update(Session); + + RecordTuple RT; + + Convert(R, RT); + + auto tValue(Value); + + std::string St = + "update " + TableName_ + " set " + UpdateFields_ + " where " + FieldName + "=?"; + Update << ConvertParams(St), Poco::Data::Keywords::use(RT), + Poco::Data::Keywords::use(tValue); + Update.execute(); + if (Cache_) + Cache_->UpdateCache(R); + return true; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + bool RunStatement(const std::string &St) { + try { + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Command(Session); + + Command << St; + Command.execute(); + + return true; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + template + bool ReplaceRecord(field_name_t FieldName, const T &Value, RecordType &R) { + try { + if (Exists(FieldName, Value)) { + return UpdateRecord(FieldName, Value, R); + } + return CreateRecord(R); + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + template + bool GetNameAndDescription(field_name_t FieldName, const T &Value, std::string &Name, + std::string &Description) { + try { + assert(ValidFieldName(FieldName)); + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Select(Session); + RecordTuple RT; + + std::string St = "select " + SelectFields_ + " from " + TableName_ + " where " + + FieldName + "=?"; + RecordType R; + auto tValue{Value}; + Select << ConvertParams(St), Poco::Data::Keywords::into(RT), + Poco::Data::Keywords::use(tValue); + + if (Select.execute() == 1) { + Convert(RT, R); + Name = R.info.name; + Description = R.info.description; + return true; + } + return false; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + template bool DeleteRecord(field_name_t FieldName, const T &Value) { + try { + assert(ValidFieldName(FieldName)); + + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Delete(Session); + + std::string St = "delete from " + TableName_ + " where " + FieldName + "=?"; + auto tValue{Value}; + + Delete << ConvertParams(St), Poco::Data::Keywords::use(tValue); + Delete.execute(); + if (Cache_) + Cache_->Delete(FieldName, Value); + return true; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + bool DeleteRecords(const std::string &WhereClause) { + try { + assert(!WhereClause.empty()); + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Delete(Session); + + std::string St = "delete from " + TableName_ + " where " + WhereClause; + Delete << St; + Delete.execute(); + return true; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + bool Exists(field_name_t FieldName, const std::string &Value) { + try { + assert(ValidFieldName(FieldName)); + + RecordType R; + if (GetRecord(FieldName, Value, R)) + return true; + return false; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + bool Iterate(std::function F, + const std::string &WhereClause = "") { + try { + + uint64_t Offset = 0; + uint64_t Batch = 50; + bool Done = false; + while (!Done) { + std::vector Records; + if (GetRecords(Offset, Batch, Records, WhereClause)) { + for (const auto &i : Records) { + if (!F(i)) + return true; + } + if (Records.size() < Batch) + return true; + Offset += Batch; + } else { + Done = true; + } + } + return true; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + bool PrepareOrderBy(const std::string &OrderByList, std::string &OrderByString) { + auto items = Poco::StringTokenizer(OrderByList, ","); + std::string ItemList; + + for (const auto &i : items) { + auto T = Poco::StringTokenizer(i, ":"); + if (T.count() != 2) { + return false; + } + if (T[1] != "a" && T[1] != "d") { + return false; + } + if (!ItemList.empty()) + ItemList += " , "; + auto hint = FieldNames_.find(Poco::toLower(T[0])); + if (hint == FieldNames_.end()) { + return false; + } + ItemList += T[0] + (T[1] == "a" ? " ASC" : " DESC"); + } + + if (!ItemList.empty()) { + OrderByString = " ORDER BY " + ItemList; + } + return true; + } + + uint64_t Count(const std::string &Where = "") { + try { + uint64_t Cnt = 0; + + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Select(Session); + + std::string st{"SELECT COUNT(*) FROM " + TableName_ + " " + + (Where.empty() ? "" : (" where " + Where))}; + + Select << st, Poco::Data::Keywords::into(Cnt); + Select.execute(); + + return Cnt; + + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return 0; + } + + template + bool ManipulateVectorMember(X T, field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID, bool Add) { + try { + assert(ValidFieldName(FieldName)); + + RecordType R; + if (GetRecord(FieldName, ParentUUID, R)) { + auto it = std::find((R.*T).begin(), (R.*T).end(), ChildUUID); + if (Add) { + if (it != (R.*T).end() && *it == ChildUUID) + return false; + (R.*T).push_back(ChildUUID); + std::sort((R.*T).begin(), (R.*T).end()); + } else { + if (it != (R.*T).end() && *it == ChildUUID) + (R.*T).erase(it); + else + return false; + } + UpdateRecord(FieldName, ParentUUID, R); + return true; + } + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + bool RunScript(const std::vector &Statements, bool IgnoreExceptions = true) { + try { + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Command(Session); + + for (const auto &i : Statements) { + try { + Command << i, Poco::Data::Keywords::now; + } catch (const Poco::Exception &E) { + // Logger_.log(E); + // Logger_.error(Poco::format("The following statement '%s' generated an + // exception during a table upgrade. This may or may not be a problem.", + // i)); + if (!IgnoreExceptions) { + return false; + } + } + Command.reset(Session); + } + return true; + } catch (const Poco::Exception &E) { + Logger_.log(E); + } + return false; + } + + virtual uint32_t Version() { return 0; } + + virtual bool Upgrade(uint32_t from, uint32_t &to) { + to = from; + return true; + } + + inline bool AddChild(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::children, FieldName, ParentUUID, ChildUUID, + true); + } + + inline bool DeleteChild(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::children, FieldName, ParentUUID, ChildUUID, + false); + } + + inline bool AddLocation(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::locations, FieldName, ParentUUID, ChildUUID, + true); + } + + inline bool DeleteLocation(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::locations, FieldName, ParentUUID, ChildUUID, + false); + } + + inline bool AddContact(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::contacts, FieldName, ParentUUID, ChildUUID, + true); + } + + inline bool DeleteContact(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::contacts, FieldName, ParentUUID, ChildUUID, + false); + } + + inline bool AddVenue(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::venues, FieldName, ParentUUID, ChildUUID, + true); + } + + inline bool DeleteVenue(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::venues, FieldName, ParentUUID, ChildUUID, + false); + } + + inline bool AddDevice(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::devices, FieldName, ParentUUID, ChildUUID, + true); + } + + inline bool DeleteDevice(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::devices, FieldName, ParentUUID, ChildUUID, + false); + } + + inline bool AddEntity(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::entities, FieldName, ParentUUID, ChildUUID, + true); + } + + inline bool DeleteEntity(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::entities, FieldName, ParentUUID, ChildUUID, + false); + } + + inline bool AddUser(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::users, FieldName, ParentUUID, ChildUUID, + true); + } + + inline bool DelUser(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::users, FieldName, ParentUUID, ChildUUID, + false); + } + + inline bool AddConfiguration(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::deviceConfiguration, FieldName, ParentUUID, + ChildUUID, true); + } + + inline bool DelConfiguration(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::deviceConfiguration, FieldName, ParentUUID, + ChildUUID, false); + } + + inline bool AddVariable(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::variables, FieldName, ParentUUID, ChildUUID, + true); + } + + inline bool DelVariable(field_name_t FieldName, const std::string &ParentUUID, + const std::string &ChildUUID) { + return ManipulateVectorMember(&RecordType::variables, FieldName, ParentUUID, ChildUUID, + false); + } + + inline bool AddInUse(field_name_t FieldName, const std::string &ParentUUID, + const std::string &Prefix, const std::string &ChildUUID) { + std::string FakeUUID{Prefix + ":" + ChildUUID}; + return ManipulateVectorMember(&RecordType::inUse, FieldName, ParentUUID, FakeUUID, + true); + } + + inline bool DeleteInUse(field_name_t FieldName, const std::string &ParentUUID, + const std::string &Prefix, const std::string &ChildUUID) { + std::string FakeUUID{Prefix + ":" + ChildUUID}; + return ManipulateVectorMember(&RecordType::inUse, FieldName, ParentUUID, FakeUUID, + false); + } + + inline bool GetInUse(field_name_t FieldName, const std::string &UUID, + std::vector &UUIDs) { + RecordType R; + if (GetRecord(FieldName, UUID, R)) { + UUIDs = R.inUse; + return true; + } + return false; + } + + inline bool ValidFieldName(const std::string &FieldName) { + return FieldNames_.find(Poco::toLower(FieldName)) != FieldNames_.end(); + } + + inline bool ValidFieldName(const char *FieldName) { + std::string Field{FieldName}; + return ValidFieldName(Field); + } + + [[nodiscard]] inline std::string ComputeRange(uint64_t From, uint64_t HowMany) { + if (From < 1) + From = 0; + switch (Type_) { + case OpenWifi::DBType::sqlite: + return " LIMIT " + std::to_string(From) + ", " + std::to_string(HowMany) + " "; + case OpenWifi::DBType::pgsql: + return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + + " "; + case OpenWifi::DBType::mysql: + return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + + " "; + default: + return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + + " "; + } + } + + Poco::Logger &Logger() { return Logger_; } + + inline bool DeleteRecordsFromCache(const char *FieldName, const std::string &Value) { + if (Cache_) + Cache_->Delete(FieldName, Value); + return true; + } + + inline void GetFieldNames(OpenWifi::Types::StringVec &F) { + for (const auto &[field, _] : FieldNames_) + F.push_back(field); + } + + protected: + std::string TableName_; + OpenWifi::DBType Type_; + Poco::Data::SessionPool &Pool_; + Poco::Logger &Logger_; + std::string Prefix_; + DBCache *Cache_ = nullptr; + + private: + std::string CreateFields_; + std::string SelectFields_; + std::string SelectList_; + std::string UpdateFields_; + std::vector IndexCreation_; + std::map FieldNames_; + }; +} // namespace ORM diff --git a/src/framework/ow_constants.h b/src/framework/ow_constants.h index 060873d..207d5d8 100644 --- a/src/framework/ow_constants.h +++ b/src/framework/ow_constants.h @@ -4,8 +4,8 @@ #pragma once -#include #include +#include #include "Poco/String.h" @@ -21,7 +21,7 @@ namespace OpenWifi { enum UNAUTHORIZED_REASON { - SUCCESS=0, + SUCCESS = 0, PASSWORD_CHANGE_REQUIRED, INVALID_CREDENTIALS, PASSWORD_ALREADY_USED, @@ -36,449 +36,592 @@ namespace OpenWifi { MFA_FAILURE, SECURITY_SERVICE_UNREACHABLE, CANNOT_REFRESH_TOKEN, - ACCOUNT_SUSPENDED + ACCOUNT_SUSPENDED }; } namespace OpenWifi::RESTAPI::Errors { - struct msg { uint64_t err_num; std::string err_txt; }; - static const struct msg Error404{404,"Resource does not exist."}; + struct msg { + uint64_t err_num; + std::string err_txt; + }; + static const struct msg Error404 { 404, "Resource does not exist." }; - static const struct msg SUCCESS{0,"No error."}; - static const struct msg PASSWORD_CHANGE_REQUIRED{1,"Password change required"}; - static const struct msg INVALID_CREDENTIALS{2,"Invalid credentials."}; - static const struct msg PASSWORD_ALREADY_USED{3,"Password already used."}; - static const struct msg USERNAME_PENDING_VERIFICATION{4,"Username pending verification."}; - static const struct msg PASSWORD_INVALID{5,"Password is invalid"}; - static const struct msg INTERNAL_ERROR{6,"Internal error."}; - static const struct msg ACCESS_DENIED{7,"Access denied."}; - static const struct msg INVALID_TOKEN{8,"Invalid token."}; - static const struct msg EXPIRED_TOKEN{9,"Expired token."}; - static const struct msg RATE_LIMIT_EXCEEDED{10,"Rate limit exceeded."}; - static const struct msg BAD_MFA_TRANSACTION{11,"Bad MFA transaction."}; - static const struct msg MFA_FAILURE{12,"MFA failure."}; - static const struct msg SECURITY_SERVICE_UNREACHABLE{13,"Security service is unreachable, try again later."}; - static const struct msg CANNOT_REFRESH_TOKEN{14,"Cannot refresh token."}; - static const struct msg ACCOUNT_SUSPENDED{15,"Account has been suspended."}; + static const struct msg SUCCESS { 0, "No error." }; + static const struct msg PASSWORD_CHANGE_REQUIRED { 1, "Password change required" }; + static const struct msg INVALID_CREDENTIALS { 2, "Invalid credentials." }; + static const struct msg PASSWORD_ALREADY_USED { 3, "Password already used." }; + static const struct msg USERNAME_PENDING_VERIFICATION { 4, "Username pending verification." }; + static const struct msg PASSWORD_INVALID { 5, "Password is invalid" }; + static const struct msg INTERNAL_ERROR { 6, "Internal error." }; + static const struct msg ACCESS_DENIED { 7, "Access denied." }; + static const struct msg INVALID_TOKEN { 8, "Invalid token." }; + static const struct msg EXPIRED_TOKEN { 9, "Expired token." }; + static const struct msg RATE_LIMIT_EXCEEDED { 10, "Rate limit exceeded." }; + static const struct msg BAD_MFA_TRANSACTION { 11, "Bad MFA transaction." }; + static const struct msg MFA_FAILURE { 12, "MFA failure." }; + static const struct msg SECURITY_SERVICE_UNREACHABLE { + 13, "Security service is unreachable, try again later." + }; + static const struct msg CANNOT_REFRESH_TOKEN { 14, "Cannot refresh token." }; + static const struct msg ACCOUNT_SUSPENDED { 15, "Account has been suspended." }; - static const struct msg MissingUUID{1000,"Missing UUID."}; - static const struct msg MissingSerialNumber{1001,"Missing Serial Number."}; - static const struct msg InternalError{1002,"Internal error. Please try later."}; - static const struct msg InvalidJSONDocument{1003,"Invalid JSON document."}; - static const struct msg UnsupportedHTTPMethod{1004,"Unsupported HTTP Method"}; - static const struct msg StillInUse{1005,"Element still in use."}; - static const struct msg CouldNotBeDeleted{1006,"Element could not be deleted."}; - static const struct msg NameMustBeSet{1007,"The name property must be set."}; - static const struct msg ConfigBlockInvalid{1008,"Configuration block type invalid."}; - static const struct msg UnknownId{1009,"Unknown UUID."}; - static const struct msg InvalidDeviceTypes{1010,"Unknown or invalid device type(s)."}; - static const struct msg RecordNotCreated{1011,"Record could not be created."}; - static const struct msg RecordNotUpdated{1012,"Record could not be updated."}; - static const struct msg UnknownManagementPolicyUUID{1013,"Unknown management policy UUID."}; - static const struct msg CannotDeleteRoot{1014,"Root Entity cannot be removed, only modified."}; - static const struct msg MustCreateRootFirst{1015,"Root entity must be created first."}; - static const struct msg ParentUUIDMustExist{1016,"Parent UUID must exist."}; - static const struct msg ConfigurationMustExist{1017,"Configuration must exist."}; - static const struct msg MissingOrInvalidParameters{1018,"Invalid or missing parameters."}; - static const struct msg UnknownSerialNumber{1019,"Unknown Serial Number."}; - static const struct msg InvalidSerialNumber{1020,"Invalid Serial Number."}; - static const struct msg SerialNumberExists{1021,"Serial Number already exists."}; - static const struct msg ValidNonRootUUID{1022,"Must be a non-root, and valid UUID."}; - static const struct msg VenueMustExist{1023,"Venue does not exist."}; - static const struct msg NotBoth{1024,"You cannot specify both Entity and Venue"}; - static const struct msg EntityMustExist{1025,"Entity must exist."}; - static const struct msg ParentOrEntityMustBeSet{1026,"Parent or Entity must be set."}; - static const struct msg ContactMustExist{1027,"Contact must exist."}; - static const struct msg LocationMustExist{1028,"Location must exist."}; - static const struct msg OnlyWSSupported{1029,"This endpoint only supports WebSocket."}; - static const struct msg SerialNumberMismatch{1030,"Serial Number mismatch."}; - static const struct msg InvalidCommand{1031,"Invalid command."}; - static const struct msg NoRecordsDeleted{1032,"No records deleted."}; - static const struct msg DeviceNotConnected{1033,"Device is not currently connected."}; - static const struct msg CannotCreateWS{1034,"Telemetry system could not create WS endpoint. Please try again."}; - static const struct msg BothDeviceTypeRevision{1035,"Both deviceType and revision must be set."}; - static const struct msg IdOrSerialEmpty{1036,"SerialNumber and Id must not be empty."}; - static const struct msg MissingUserID{1037,"Missing user ID."}; - static const struct msg IdMustBe0{1038,"To create a user, you must set the ID to 0"}; - static const struct msg InvalidUserRole{1039,"Invalid userRole."}; - static const struct msg InvalidEmailAddress{1040,"Invalid email address."}; - static const struct msg PasswordRejected{1041,"Password was rejected. This maybe an old password."}; - static const struct msg InvalidIPRanges{1042,"Invalid IP range specifications."}; - static const struct msg InvalidLOrderBy{1043,"Invalid orderBy specification."}; - static const struct msg NeedMobileNumber{1044,"You must provide at least one validated phone number."}; - static const struct msg BadMFAMethod{1045,"MFA only supports sms or email."}; - static const struct msg InvalidCredentials{1046,"Invalid credentials (username/password)."}; - static const struct msg InvalidPassword{1047,"Password does not conform to basic password rules."}; - static const struct msg UserPendingVerification{1048,"User access denied pending email verification."}; - static const struct msg PasswordMustBeChanged{1049,"Password must be changed."}; - static const struct msg UnrecognizedRequest{1050,"Ill-formed request. Please consult documentation."}; - static const struct msg MissingAuthenticationInformation{1051,"Missing authentication information."}; - static const struct msg InsufficientAccessRights{1052,"Insufficient access rights to complete the operation."}; - static const struct msg ExpiredToken{1053,"Token has expired, user must login."}; - static const struct msg SubscriberMustExist{1054,"Subscriber must exist."}; - static const struct msg AuthenticatorVerificationIncomplete{1055,"Authenticator validation is not complete."}; - static const struct msg SMSCouldNotBeSentRetry{1056,"SMS could not be sent to validate device, try later or change the phone number."}; - static const struct msg SMSCouldNotValidate{1057,"Code and number could not be validated"}; - static const struct msg InvalidDeviceClass{1058,"Invalid device class. Must be: any, venue, entity, or subscriber"}; - static const struct msg SerialNumberAlreadyProvisioned{1059,"This device has already been provisioned to a subscriber."}; - static const struct msg SerialNumberNotTheProperClass{1060,"Device is not available to subscribers. It ahs been assigned to another class of devices."}; - static const struct msg UserAlreadyExists{1061,"Username already exists."}; - static const struct msg NotImplemented{1062,"Function not implemented."}; - static const struct msg VariableMustExist{1063,"Specified variable does not exist."}; - static const struct msg InvalidEntityType{1064,"Invalid entity type."}; - static const struct msg CannotDeleteSubEntity{1065,"Cannot delete the default subscriber entity."}; - static const struct msg OperatorIdMustExist{1066,"Missing or bad Operator ID"}; - static const struct msg CannotDeleteDefaultOperator{1067,"Cannot delete the default operator."}; - static const struct msg CannotCreateDefaultOperator{1068,"Cannot create the default operator."}; - static const struct msg InvalidRRM{1069,"Invalid RRM value."}; - static const struct msg InvalidIPAddresses{1070,"Invalid IP addresses."}; - static const struct msg InvalidBillingCode{1071,"Empty of invalid billing code."}; - static const struct msg InvalidBillingPeriod{1072,"Invalid billing period."}; - static const struct msg InvalidSubscriberId{1073,"Invalid subscriber ID."}; - static const struct msg InvalidContactId{1074,"Invalid contact ID."}; - static const struct msg InvalidLocationId{1075,"Invalid location ID."}; - static const struct msg InvalidContactType{1076,"Invalid contact type."}; - static const struct msg InvalidLocationType{1077,"Invalid location type."}; - static const struct msg InvalidOperatorId{1078,"Invalid operator ID."}; - static const struct msg InvalidServiceClassId{1079,"Invalid service class ID."}; - static const struct msg InvalidSubscriberDeviceId{1080,"Invalid subscriber device ID."}; - static const struct msg InvalidRegistrationOperatorId{1081,"Invalid registration operator ID."}; - static const struct msg InvalidRegistrationOperatorName{1082,"Invalid registration operator name."}; - static const struct msg RegistrationNameDuplicate{1083,"Registration name must be unique."}; - static const struct msg SMSMFANotEnabled{1084,"SMS is not enabled in the security service."}; - static const struct msg EMailMFANotEnabled{1085,"email is not enabled in the security service."}; + static const struct msg MissingUUID { 1000, "Missing UUID." }; + static const struct msg MissingSerialNumber { 1001, "Missing Serial Number." }; + static const struct msg InternalError { 1002, "Internal error. Please try later." }; + static const struct msg InvalidJSONDocument { 1003, "Invalid JSON document." }; + static const struct msg UnsupportedHTTPMethod { 1004, "Unsupported HTTP Method" }; + static const struct msg StillInUse { 1005, "Element still in use." }; + static const struct msg CouldNotBeDeleted { 1006, "Element could not be deleted." }; + static const struct msg NameMustBeSet { 1007, "The name property must be set." }; + static const struct msg ConfigBlockInvalid { 1008, "Configuration block type invalid." }; + static const struct msg UnknownId { 1009, "Unknown UUID." }; + static const struct msg InvalidDeviceTypes { 1010, "Unknown or invalid device type(s)." }; + static const struct msg RecordNotCreated { 1011, "Record could not be created." }; + static const struct msg RecordNotUpdated { 1012, "Record could not be updated." }; + static const struct msg UnknownManagementPolicyUUID { 1013, "Unknown management policy UUID." }; + static const struct msg CannotDeleteRoot { + 1014, "Root Entity cannot be removed, only modified." + }; + static const struct msg MustCreateRootFirst { 1015, "Root entity must be created first." }; + static const struct msg ParentUUIDMustExist { 1016, "Parent UUID must exist." }; + static const struct msg ConfigurationMustExist { 1017, "Configuration must exist." }; + static const struct msg MissingOrInvalidParameters { 1018, "Invalid or missing parameters." }; + static const struct msg UnknownSerialNumber { 1019, "Unknown Serial Number." }; + static const struct msg InvalidSerialNumber { 1020, "Invalid Serial Number." }; + static const struct msg SerialNumberExists { 1021, "Serial Number already exists." }; + static const struct msg ValidNonRootUUID { 1022, "Must be a non-root, and valid UUID." }; + static const struct msg VenueMustExist { 1023, "Venue does not exist." }; + static const struct msg NotBoth { 1024, "You cannot specify both Entity and Venue" }; + static const struct msg EntityMustExist { 1025, "Entity must exist." }; + static const struct msg ParentOrEntityMustBeSet { 1026, "Parent or Entity must be set." }; + static const struct msg ContactMustExist { 1027, "Contact must exist." }; + static const struct msg LocationMustExist { 1028, "Location must exist." }; + static const struct msg OnlyWSSupported { 1029, "This endpoint only supports WebSocket." }; + static const struct msg SerialNumberMismatch { 1030, "Serial Number mismatch." }; + static const struct msg InvalidCommand { 1031, "Invalid command." }; + static const struct msg NoRecordsDeleted { 1032, "No records deleted." }; + static const struct msg DeviceNotConnected { 1033, "Device is not currently connected." }; + static const struct msg CannotCreateWS { + 1034, "Telemetry system could not create WS endpoint. Please try again." + }; + static const struct msg BothDeviceTypeRevision { + 1035, "Both deviceType and revision must be set." + }; + static const struct msg IdOrSerialEmpty { 1036, "SerialNumber and Id must not be empty." }; + static const struct msg MissingUserID { 1037, "Missing user ID." }; + static const struct msg IdMustBe0 { 1038, "To create a user, you must set the ID to 0" }; + static const struct msg InvalidUserRole { 1039, "Invalid userRole." }; + static const struct msg InvalidEmailAddress { 1040, "Invalid email address." }; + static const struct msg PasswordRejected { + 1041, "Password was rejected. This maybe an old password." + }; + static const struct msg InvalidIPRanges { 1042, "Invalid IP range specifications." }; + static const struct msg InvalidLOrderBy { 1043, "Invalid orderBy specification." }; + static const struct msg NeedMobileNumber { + 1044, "You must provide at least one validated phone number." + }; + static const struct msg BadMFAMethod { 1045, "MFA only supports sms or email." }; + static const struct msg InvalidCredentials { 1046, "Invalid credentials (username/password)." }; + static const struct msg InvalidPassword { + 1047, "Password does not conform to basic password rules." + }; + static const struct msg UserPendingVerification { + 1048, "User access denied pending email verification." + }; + static const struct msg PasswordMustBeChanged { 1049, "Password must be changed." }; + static const struct msg UnrecognizedRequest { + 1050, "Ill-formed request. Please consult documentation." + }; + static const struct msg MissingAuthenticationInformation { + 1051, "Missing authentication information." + }; + static const struct msg InsufficientAccessRights { + 1052, "Insufficient access rights to complete the operation." + }; + static const struct msg ExpiredToken { 1053, "Token has expired, user must login." }; + static const struct msg SubscriberMustExist { 1054, "Subscriber must exist." }; + static const struct msg AuthenticatorVerificationIncomplete { + 1055, "Authenticator validation is not complete." + }; + static const struct msg SMSCouldNotBeSentRetry { + 1056, "SMS could not be sent to validate device, try later or change the phone number." + }; + static const struct msg SMSCouldNotValidate { 1057, "Code and number could not be validated" }; + static const struct msg InvalidDeviceClass { + 1058, "Invalid device class. Must be: any, venue, entity, or subscriber" + }; + static const struct msg SerialNumberAlreadyProvisioned { + 1059, "This device has already been provisioned to a subscriber." + }; + static const struct msg SerialNumberNotTheProperClass { + 1060, "Device is not available to subscribers. It ahs been assigned to another class of " + "devices." + }; + static const struct msg UserAlreadyExists { 1061, "Username already exists." }; + static const struct msg NotImplemented { 1062, "Function not implemented." }; + static const struct msg VariableMustExist { 1063, "Specified variable does not exist." }; + static const struct msg InvalidEntityType { 1064, "Invalid entity type." }; + static const struct msg CannotDeleteSubEntity { + 1065, "Cannot delete the default subscriber entity." + }; + static const struct msg OperatorIdMustExist { 1066, "Missing or bad Operator ID" }; + static const struct msg CannotDeleteDefaultOperator { + 1067, "Cannot delete the default operator." + }; + static const struct msg CannotCreateDefaultOperator { + 1068, "Cannot create the default operator." + }; + static const struct msg InvalidRRM { 1069, "Invalid RRM value." }; + static const struct msg InvalidIPAddresses { 1070, "Invalid IP addresses." }; + static const struct msg InvalidBillingCode { 1071, "Empty of invalid billing code." }; + static const struct msg InvalidBillingPeriod { 1072, "Invalid billing period." }; + static const struct msg InvalidSubscriberId { 1073, "Invalid subscriber ID." }; + static const struct msg InvalidContactId { 1074, "Invalid contact ID." }; + static const struct msg InvalidLocationId { 1075, "Invalid location ID." }; + static const struct msg InvalidContactType { 1076, "Invalid contact type." }; + static const struct msg InvalidLocationType { 1077, "Invalid location type." }; + static const struct msg InvalidOperatorId { 1078, "Invalid operator ID." }; + static const struct msg InvalidServiceClassId { 1079, "Invalid service class ID." }; + static const struct msg InvalidSubscriberDeviceId { 1080, "Invalid subscriber device ID." }; + static const struct msg InvalidRegistrationOperatorId { + 1081, "Invalid registration operator ID." + }; + static const struct msg InvalidRegistrationOperatorName { + 1082, "Invalid registration operator name." + }; + static const struct msg RegistrationNameDuplicate { 1083, "Registration name must be unique." }; + static const struct msg SMSMFANotEnabled { + 1084, "SMS is not enabled in the security service." + }; + static const struct msg EMailMFANotEnabled { + 1085, "email is not enabled in the security service." + }; - static const struct msg TOTInvalidCode{1086,"Invalid code."}; - static const struct msg TOTInvalidIndex{1087,"Invalid index."}; - static const struct msg TOTRepeatedCode{1088,"Code is repeated. Must be new code."}; - static const struct msg TOTInvalidProtocol{1089,"Invalid protocol sequence."}; - static const struct msg TOTNoSession{1090,"No validation session present."}; + static const struct msg TOTInvalidCode { 1086, "Invalid code." }; + static const struct msg TOTInvalidIndex { 1087, "Invalid index." }; + static const struct msg TOTRepeatedCode { 1088, "Code is repeated. Must be new code." }; + static const struct msg TOTInvalidProtocol { 1089, "Invalid protocol sequence." }; + static const struct msg TOTNoSession { 1090, "No validation session present." }; - static const struct msg SignupAlreadySigned{1091,"Code is repeated. Must be new code."}; - static const struct msg SignupEmailCheck{1092,"Waiting for email check completion."}; - static const struct msg SignupWaitingForDevice{1093,"Waiting for device."}; + static const struct msg SignupAlreadySigned { 1091, "Code is repeated. Must be new code." }; + static const struct msg SignupEmailCheck { 1092, "Waiting for email check completion." }; + static const struct msg SignupWaitingForDevice { 1093, "Waiting for device." }; - static const struct msg SMSMissingPhoneNumber{1094,"Missing phone number"}; - static const struct msg SMSTryLater{1095,"SMS could not be sent. Verify the number or try again later."}; - static const struct msg SMSMissingChallenge{1096,"Missing 'challengeCode'"}; - static const struct msg MustHaveConfigElement{1097,"Must have 'configuration' element."}; + static const struct msg SMSMissingPhoneNumber { 1094, "Missing phone number" }; + static const struct msg SMSTryLater { + 1095, "SMS could not be sent. Verify the number or try again later." + }; + static const struct msg SMSMissingChallenge { 1096, "Missing 'challengeCode'" }; + static const struct msg MustHaveConfigElement { 1097, "Must have 'configuration' element." }; - static const struct msg ModelIDListCannotBeEmpty{1098,"Model ID list cannot be empty."}; - static const struct msg DefConfigNameExists{1099,"Configuration name already exists."}; + static const struct msg ModelIDListCannotBeEmpty { 1098, "Model ID list cannot be empty." }; + static const struct msg DefConfigNameExists { 1099, "Configuration name already exists." }; - static const struct msg SubNoDeviceActivated{1100,"No devices activated yet."}; - static const struct msg SubConfigNotRefreshed{1101,"Configuration could not be refreshed."}; + static const struct msg SubNoDeviceActivated { 1100, "No devices activated yet." }; + static const struct msg SubConfigNotRefreshed { 1101, "Configuration could not be refreshed." }; - static const struct msg ProvServiceNotAvailable{1102,"Provisioning service not available yet."}; - static const struct msg SSIDInvalidPassword{1103,"Invalid password length. Must be 8 characters or greater, and a maximum of 32 characters."}; - static const struct msg InvalidStartingIPAddress{1104,"Invalid starting/ending IP address."}; - static const struct msg SubnetFormatError{1105,"Subnet must be in format like 192.168.1.1/24."}; - static const struct msg DeviceModeError{1106,"Device mode subnet must be of the form 192.168.1.1/24."}; + static const struct msg ProvServiceNotAvailable { + 1102, "Provisioning service not available yet." + }; + static const struct msg SSIDInvalidPassword { + 1103, "Invalid password length. Must be 8 characters or greater, and a maximum of 32 " + "characters." + }; + static const struct msg InvalidStartingIPAddress { + 1104, "Invalid starting/ending IP address." + }; + static const struct msg SubnetFormatError { + 1105, "Subnet must be in format like 192.168.1.1/24." + }; + static const struct msg DeviceModeError { + 1106, "Device mode subnet must be of the form 192.168.1.1/24." + }; - static const struct msg BadDeviceMode{1107,"Mode must be bridge, nat, or manual."}; - static const struct msg DefaultGatewayFormat{1108,"Default gateway must be in format like 192.168.1.1."}; - static const struct msg PrimaryDNSFormat{1109,"Primary DNS must be an IP address i.e. 192.168.1.1."}; + static const struct msg BadDeviceMode { 1107, "Mode must be bridge, nat, or manual." }; + static const struct msg DefaultGatewayFormat { + 1108, "Default gateway must be in format like 192.168.1.1." + }; + static const struct msg PrimaryDNSFormat { + 1109, "Primary DNS must be an IP address i.e. 192.168.1.1." + }; - static const struct msg SecondaryDNSFormat{1110,"Secondary DNS must be an IP address i.e. 192.168.1.1."}; - static const struct msg BadConnectionType{1111,"Internet Connection must be automatic, bridge, pppoe, or manual."}; - static const struct msg InvalidDeviceID{1112,"Invalid deviceID."}; - static const struct msg InvalidVisibilityAttribute{1113,"Invalid visibility attribute."}; - static const struct msg UnknownConfigurationSection{1114,"Unknown section."}; + static const struct msg SecondaryDNSFormat { + 1110, "Secondary DNS must be an IP address i.e. 192.168.1.1." + }; + static const struct msg BadConnectionType { + 1111, "Internet Connection must be automatic, bridge, pppoe, or manual." + }; + static const struct msg InvalidDeviceID { 1112, "Invalid deviceID." }; + static const struct msg InvalidVisibilityAttribute { 1113, "Invalid visibility attribute." }; + static const struct msg UnknownConfigurationSection { 1114, "Unknown section." }; - static const struct msg CannotValidatePhoneNumber{1115,"Phone number could not be validated."}; - static const struct msg RootUsersNoOwners{1116,"ROOT users may not have owners."}; - static const struct msg PartnerMustHaveEntity{1118,"Partner user must belong to an entity."}; - static const struct msg RootCannotModifyUsers{1119,"ROOT may not modify user roles."}; + static const struct msg CannotValidatePhoneNumber { + 1115, "Phone number could not be validated." + }; + static const struct msg RootUsersNoOwners { 1116, "ROOT users may not have owners." }; + static const struct msg PartnerMustHaveEntity { + 1118, "Partner user must belong to an entity." + }; + static const struct msg RootCannotModifyUsers { 1119, "ROOT may not modify user roles." }; - static const struct msg CertificateNotIssued{1120,"Certificate was not issued."}; - static const struct msg IncompleteCertificate{1121,"Incomplete certificate information. Cannot be downloaded. You must delete and recreate."}; - static const struct msg InvalidCertificateType{1122,"Invalid certificate type."}; - static const struct msg InvalidDeviceName{1123,"Invalid device name."}; + static const struct msg CertificateNotIssued { 1120, "Certificate was not issued." }; + static const struct msg IncompleteCertificate { + 1121, "Incomplete certificate information. Cannot be downloaded. You must delete and " + "recreate." + }; + static const struct msg InvalidCertificateType { 1122, "Invalid certificate type." }; + static const struct msg InvalidDeviceName { 1123, "Invalid device name." }; - static const struct msg InvalidRedirectorName{1124,"Invalid redirector name"}; - static const struct msg CommonNameAlreadyExists{1125,"A device/server of this name already exists"}; - static const struct msg CertificateAlreadyExists{1126,"A certificate for this device already exists."}; - static const struct msg CannotCreateCertTryAgain{1127,"Device certificate could not be created. Please try later."}; - static const struct msg CouldNotRevoke{1128,"Certificate could not be revoked."}; + static const struct msg InvalidRedirectorName { 1124, "Invalid redirector name" }; + static const struct msg CommonNameAlreadyExists { + 1125, "A device/server of this name already exists" + }; + static const struct msg CertificateAlreadyExists { + 1126, "A certificate for this device already exists." + }; + static const struct msg CannotCreateCertTryAgain { + 1127, "Device certificate could not be created. Please try later." + }; + static const struct msg CouldNotRevoke { 1128, "Certificate could not be revoked." }; - static const struct msg CouldNotModifyCert{1129,"Certificate could not me modified. Please verify the information you supplied."}; - static const struct msg BatchCertNoCreated{1130,"Certificates have not been created for this batch."}; - static const struct msg BatchTooBig{1131,"Illegal number of MAC Addresses: must be between 1 and 1000."}; + static const struct msg CouldNotModifyCert { + 1129, "Certificate could not me modified. Please verify the information you supplied." + }; + static const struct msg BatchCertNoCreated { + 1130, "Certificates have not been created for this batch." + }; + static const struct msg BatchTooBig { + 1131, "Illegal number of MAC Addresses: must be between 1 and 1000." + }; - static const struct msg OutstandingJobs{1132,"Batch has running outstanding jobs. Please wait until job is finished."}; - static const struct msg InvalidSMSNotificationList{1133,"Invalid SMS Notification list."}; - static const struct msg InvalidEMailNotificationList{1134,"Invalid email Notification list."}; - static const struct msg CannotChangeCommanNames{1135,"You cannot provide new/modified common names after jobs have been run for a batch."}; - static const struct msg FailedToVerifyDigicert{1136,"Failed to verify the DigiCert information provided."}; - static const struct msg CouldNotPerformCommand{1137,"Could not perform command."}; + static const struct msg OutstandingJobs { + 1132, "Batch has running outstanding jobs. Please wait until job is finished." + }; + static const struct msg InvalidSMSNotificationList { 1133, "Invalid SMS Notification list." }; + static const struct msg InvalidEMailNotificationList { + 1134, "Invalid email Notification list." + }; + static const struct msg CannotChangeCommanNames { + 1135, "You cannot provide new/modified common names after jobs have been run for a batch." + }; + static const struct msg FailedToVerifyDigicert { + 1136, "Failed to verify the DigiCert information provided." + }; + static const struct msg CouldNotPerformCommand { 1137, "Could not perform command." }; - static const struct msg PoolNameInvalid{1138,"Pool name is invalid."}; - static const struct msg InvalidRadiusProxyStrategy{1139,"Strategy name must be: random, round_robin, weighted."}; - static const struct msg InvalidRadiusProxyMonitorMethod{1140,"monitorMethod must be: none, https, radius."}; - static const struct msg MustHaveAtLeastOneRadiusServer{1141,"Must have at least one RADIUS server."}; - static const struct msg InvalidRadiusServerEntry{1142,"RADIUS Server IP address invalid or port missing."}; - static const struct msg InvalidRadiusServerWeigth{1143,"RADIUS Server IP weight cannot be 0."}; + static const struct msg PoolNameInvalid { 1138, "Pool name is invalid." }; + static const struct msg InvalidRadiusProxyStrategy { + 1139, "Strategy name must be: random, round_robin, weighted." + }; + static const struct msg InvalidRadiusProxyMonitorMethod { + 1140, "monitorMethod must be: none, https, radius." + }; + static const struct msg MustHaveAtLeastOneRadiusServer { + 1141, "Must have at least one RADIUS server." + }; + static const struct msg InvalidRadiusServerEntry { + 1142, "RADIUS Server IP address invalid or port missing." + }; + static const struct msg InvalidRadiusServerWeigth { + 1143, "RADIUS Server IP weight cannot be 0." + }; - static const struct msg MaximumRTTYSessionsReached{1144,"Too many RTTY sessions currently active"}; - static const struct msg DeviceIsAlreadyBusy{1145,"Device is already executing a command. Please try later."}; + static const struct msg MaximumRTTYSessionsReached { + 1144, "Too many RTTY sessions currently active" + }; + static const struct msg DeviceIsAlreadyBusy { + 1145, "Device is already executing a command. Please try later." + }; - static const struct msg DeviceRequiresSignature{1146,"Device requires device signature to be provided."}; + static const struct msg DeviceRequiresSignature { + 1146, "Device requires device signature to be provided." + }; - static const struct msg ApiKeyNameAlreadyExists{1147,"API Key name must be unique."}; - static const struct msg TooManyApiKeys{1148,"Too many API Keys have already been created."}; - static const struct msg UserMustExist{1149,"User must exist."}; - static const struct msg ApiKeyNameDoesNotExist{1150,"API Key name does not exist."}; - static const struct msg ApiKeyDoesNotExist{1150,"API Key does not exist."}; + static const struct msg ApiKeyNameAlreadyExists { 1147, "API Key name must be unique." }; + static const struct msg TooManyApiKeys { 1148, "Too many API Keys have already been created." }; + static const struct msg UserMustExist { 1149, "User must exist." }; + static const struct msg ApiKeyNameDoesNotExist { 1150, "API Key name does not exist." }; + static const struct msg ApiKeyDoesNotExist { 1150, "API Key does not exist." }; - static const struct msg DeviceIsRestricted{1151,"Device is protected by regulation. This function is not allowed."}; - static const struct msg InvalidURI{1152,"Invalid URI."}; - static const struct msg InvalidScriptSelection{1153,"Only script or scriptId must be specified. Not both."}; + static const struct msg DeviceIsRestricted { + 1151, "Device is protected by regulation. This function is not allowed." + }; + static const struct msg InvalidURI { 1152, "Invalid URI." }; + static const struct msg InvalidScriptSelection { + 1153, "Only script or scriptId must be specified. Not both." + }; - static const struct msg NoDeviceStatisticsYet{1154,"Device statistics not available yet."}; - static const struct msg AccountSuspended{1155,"You account was suspended. You can only use this site in read-only mode for now."}; - static const struct msg BatchNameAlreadyExists{1156,"Batch name must be unique."}; - static const struct msg RedirectorNameIsInvalid{1157,"Redirector name is invalid."}; - static const struct msg CertificateAlreadyBelongsToYou{1158,"The serial number already belongs to you. Please use the certificate modification API to change the redirector."}; - static const struct msg RelocationDisabledForThisDevice{1159,"Relocation disabled for this device."}; - static const struct msg CannotModifyServerCertificates{1160,"Server certificates cannot be modified."}; - - static const struct msg TransferNotInDispute{1161,"The specified transfer is not being disputed."}; - static const struct msg MissingComment{1162,"Missing comment."}; - static const struct msg EntityNotAllowedToTransfer{1163,"Entity is not allowed to transfer devices."}; - static const struct msg DailyTransferQuotaExceeded{1164,"Entity has exceeded its daily quota."}; - static const struct msg CertificateWasNotRevoked{1165,"Certificate was not revoked, so it may not be re-created."}; - static const struct msg CertificateTransferNoLongerExists{1166,"The device certificate associated with this transfer no longer seem to exist."}; - static const struct msg CertificateTransferEntityNoLongerExists{1167,"The entity tied to this transfer no longer seems to exist."}; - static const struct msg CannotRollBackDueToDigiCert{1168,"The change could not be rolled back at this time. Please try later."}; - static const struct msg CertificateTransferAlreadyRolledBack{1169,"The certificate has already been rolled back."}; - static const struct msg FirmwareBDInProgress{1170,"Firmware DB update already in progress."}; - - } + static const struct msg NoDeviceStatisticsYet { 1154, "Device statistics not available yet." }; + static const struct msg AccountSuspended { + 1155, "You account was suspended. You can only use this site in read-only mode for now." + }; + static const struct msg BatchNameAlreadyExists { 1156, "Batch name must be unique." }; + static const struct msg RedirectorNameIsInvalid { 1157, "Redirector name is invalid." }; + static const struct msg CertificateAlreadyBelongsToYou { + 1158, "The serial number already belongs to you. Please use the certificate modification " + "API to change the redirector." + }; + static const struct msg RelocationDisabledForThisDevice { + 1159, "Relocation disabled for this device." + }; + static const struct msg CannotModifyServerCertificates { + 1160, "Server certificates cannot be modified." + }; + static const struct msg TransferNotInDispute { + 1161, "The specified transfer is not being disputed." + }; + static const struct msg MissingComment { 1162, "Missing comment." }; + static const struct msg EntityNotAllowedToTransfer { + 1163, "Entity is not allowed to transfer devices." + }; + static const struct msg DailyTransferQuotaExceeded { + 1164, "Entity has exceeded its daily quota." + }; + static const struct msg CertificateWasNotRevoked { + 1165, "Certificate was not revoked, so it may not be re-created." + }; + static const struct msg CertificateTransferNoLongerExists { + 1166, "The device certificate associated with this transfer no longer seem to exist." + }; + static const struct msg CertificateTransferEntityNoLongerExists { + 1167, "The entity tied to this transfer no longer seems to exist." + }; + static const struct msg CannotRollBackDueToDigiCert { + 1168, "The change could not be rolled back at this time. Please try later." + }; + static const struct msg CertificateTransferAlreadyRolledBack { + 1169, "The certificate has already been rolled back." + }; + static const struct msg FirmwareBDInProgress { + 1170, "Firmware DB update already in progress." + }; +} // namespace OpenWifi::RESTAPI::Errors namespace OpenWifi::RESTAPI::Protocol { - static const char * CAPABILITIES = "capabilities"; - static const char * LOGS = "logs"; - static const char * HEALTHCHECKS = "healthchecks"; - static const char * STATISTICS = "statistics"; - static const char * STATUS = "status"; - static const char * SERIALNUMBER = "serialNumber"; - static const char * PERFORM = "perform"; - static const char * CONFIGURE = "configure"; - static const char * UPGRADE = "upgrade"; - static const char * REBOOT = "reboot"; - static const char * FACTORY = "factory"; - static const char * LEDS = "leds"; - static const char * TRACE = "trace"; - static const char * REQUEST = "request"; - static const char * WIFISCAN = "wifiscan"; - static const char * EVENTQUEUE = "eventqueue"; - static const char * RTTY = "rtty"; - static const char * COMMAND = "command"; - static const char * STARTDATE = "startDate"; - static const char * ENDDATE = "endDate"; - static const char * OFFSET = "offset"; - static const char * LIMIT = "limit"; - static const char * LIFETIME = "lifetime"; - static const char * UUID = "UUID"; - static const char * DATA = "data"; - static const char * CONFIGURATION = "configuration"; - static const char * WHEN = "when"; - static const char * URI = "uri"; - static const char * LOGTYPE = "logType"; - static const char * VALUES = "values"; - static const char * TYPES = "types"; - static const char * PAYLOAD = "payload"; - static const char * KEEPREDIRECTOR = "keepRedirector"; - static const char * NETWORK = "network"; - static const char * INTERFACE = "interface"; - static const char * BANDS = "bands"; - static const char * CHANNELS = "channels"; - static const char * VERBOSE = "verbose"; - static const char * MESSAGE = "message"; - static const char * STATE = "state"; - static const char * HEALTHCHECK = "healthcheck"; - static const char * PCAP_FILE_TYPE = "pcap"; - static const char * DURATION = "duration"; - static const char * NUMBEROFPACKETS = "numberOfPackets"; - static const char * FILTER = "filter"; - static const char * SELECT = "select"; - static const char * SERIALONLY = "serialOnly"; - static const char * COUNTONLY = "countOnly"; - static const char * DEVICEWITHSTATUS = "deviceWithStatus"; - static const char * DEVICESWITHSTATUS = "devicesWithStatus"; - static const char * DEVICES = "devices"; - static const char * COUNT = "count"; - static const char * SERIALNUMBERS = "serialNumbers"; - static const char * CONFIGURATIONS = "configurations"; - static const char * NAME = "name"; - static const char * COMMANDS = "commands"; - static const char * COMMANDUUID = "commandUUID"; - static const char * FIRMWARES = "firmwares"; - static const char * TOPIC = "topic"; - static const char * HOST = "host"; - static const char * OS = "os"; - static const char * HOSTNAME = "hostname"; - static const char * PROCESSORS = "processors"; - static const char * REASON = "reason"; - static const char * RELOAD = "reload"; - static const char * SUBSYSTEMS = "subsystems"; - static const char * FILEUUID = "uuid"; - static const char * USERID = "userId"; - static const char * PASSWORD = "password"; - static const char * TOKEN = "token"; - static const char * SETLOGLEVEL = "setloglevel"; - static const char * GETLOGLEVELS = "getloglevels"; - static const char * GETSUBSYSTEMNAMES = "getsubsystemnames"; - static const char * GETLOGLEVELNAMES = "getloglevelnames"; - static const char * STATS = "stats"; - static const char * PING = "ping"; - static const char * PARAMETERS = "parameters"; - static const char * VALUE = "value"; - static const char * LASTONLY = "lastOnly"; - static const char * NEWEST = "newest"; - static const char * ACTIVESCAN = "activeScan"; - static const char * OVERRIDEDFS = "override_dfs"; - static const char * LIST = "list"; - static const char * TAG = "tag"; - static const char * TAGLIST = "tagList"; - static const char * DESCRIPTION = "description"; - static const char * NOTES = "notes"; - static const char * DEVICETYPE = "deviceType"; - static const char * REVISION = "revision"; - static const char * AGES = "ages"; - static const char * REVISIONS = "revisions"; - static const char * DEVICETYPES = "deviceTypes"; - static const char * LATESTONLY = "latestOnly"; - static const char * IDONLY = "idOnly"; - static const char * REVISIONSET = "revisionSet"; - static const char * DEVICESET = "deviceSet"; - static const char * HISTORY = "history"; - static const char * ID = "id"; - static const char * VERSION = "version"; - static const char * TIMES = "times"; - static const char * UPTIME = "uptime"; - static const char * START = "start"; - static const char * DEBUG = "debug"; - static const char * SCRIPT = "script"; - static const char * TIMEOUT = "timeout"; + static const char *CAPABILITIES = "capabilities"; + static const char *LOGS = "logs"; + static const char *HEALTHCHECKS = "healthchecks"; + static const char *STATISTICS = "statistics"; + static const char *STATUS = "status"; + static const char *SERIALNUMBER = "serialNumber"; + static const char *PERFORM = "perform"; + static const char *CONFIGURE = "configure"; + static const char *UPGRADE = "upgrade"; + static const char *REBOOT = "reboot"; + static const char *FACTORY = "factory"; + static const char *LEDS = "leds"; + static const char *TRACE = "trace"; + static const char *REQUEST = "request"; + static const char *WIFISCAN = "wifiscan"; + static const char *EVENTQUEUE = "eventqueue"; + static const char *RTTY = "rtty"; + static const char *COMMAND = "command"; + static const char *STARTDATE = "startDate"; + static const char *ENDDATE = "endDate"; + static const char *OFFSET = "offset"; + static const char *LIMIT = "limit"; + static const char *LIFETIME = "lifetime"; + static const char *UUID = "UUID"; + static const char *DATA = "data"; + static const char *CONFIGURATION = "configuration"; + static const char *WHEN = "when"; + static const char *URI = "uri"; + static const char *LOGTYPE = "logType"; + static const char *VALUES = "values"; + static const char *TYPES = "types"; + static const char *PAYLOAD = "payload"; + static const char *KEEPREDIRECTOR = "keepRedirector"; + static const char *NETWORK = "network"; + static const char *INTERFACE = "interface"; + static const char *BANDS = "bands"; + static const char *CHANNELS = "channels"; + static const char *VERBOSE = "verbose"; + static const char *MESSAGE = "message"; + static const char *STATE = "state"; + static const char *HEALTHCHECK = "healthcheck"; + static const char *PCAP_FILE_TYPE = "pcap"; + static const char *DURATION = "duration"; + static const char *NUMBEROFPACKETS = "numberOfPackets"; + static const char *FILTER = "filter"; + static const char *SELECT = "select"; + static const char *SERIALONLY = "serialOnly"; + static const char *COUNTONLY = "countOnly"; + static const char *DEVICEWITHSTATUS = "deviceWithStatus"; + static const char *DEVICESWITHSTATUS = "devicesWithStatus"; + static const char *DEVICES = "devices"; + static const char *COUNT = "count"; + static const char *SERIALNUMBERS = "serialNumbers"; + static const char *CONFIGURATIONS = "configurations"; + static const char *NAME = "name"; + static const char *COMMANDS = "commands"; + static const char *COMMANDUUID = "commandUUID"; + static const char *FIRMWARES = "firmwares"; + static const char *TOPIC = "topic"; + static const char *HOST = "host"; + static const char *OS = "os"; + static const char *HOSTNAME = "hostname"; + static const char *PROCESSORS = "processors"; + static const char *REASON = "reason"; + static const char *RELOAD = "reload"; + static const char *SUBSYSTEMS = "subsystems"; + static const char *FILEUUID = "uuid"; + static const char *USERID = "userId"; + static const char *PASSWORD = "password"; + static const char *TOKEN = "token"; + static const char *SETLOGLEVEL = "setloglevel"; + static const char *GETLOGLEVELS = "getloglevels"; + static const char *GETSUBSYSTEMNAMES = "getsubsystemnames"; + static const char *GETLOGLEVELNAMES = "getloglevelnames"; + static const char *STATS = "stats"; + static const char *PING = "ping"; + static const char *PARAMETERS = "parameters"; + static const char *VALUE = "value"; + static const char *LASTONLY = "lastOnly"; + static const char *NEWEST = "newest"; + static const char *ACTIVESCAN = "activeScan"; + static const char *OVERRIDEDFS = "override_dfs"; + static const char *LIST = "list"; + static const char *TAG = "tag"; + static const char *TAGLIST = "tagList"; + static const char *DESCRIPTION = "description"; + static const char *NOTES = "notes"; + static const char *DEVICETYPE = "deviceType"; + static const char *REVISION = "revision"; + static const char *AGES = "ages"; + static const char *REVISIONS = "revisions"; + static const char *DEVICETYPES = "deviceTypes"; + static const char *LATESTONLY = "latestOnly"; + static const char *IDONLY = "idOnly"; + static const char *REVISIONSET = "revisionSet"; + static const char *DEVICESET = "deviceSet"; + static const char *HISTORY = "history"; + static const char *ID = "id"; + static const char *VERSION = "version"; + static const char *TIMES = "times"; + static const char *UPTIME = "uptime"; + static const char *START = "start"; + static const char *DEBUG = "debug"; + static const char *SCRIPT = "script"; + static const char *TIMEOUT = "timeout"; - static const char * NEWPASSWORD = "newPassword"; - static const char * USERS = "users"; - static const char * WITHEXTENDEDINFO = "withExtendedInfo"; + static const char *NEWPASSWORD = "newPassword"; + static const char *USERS = "users"; + static const char *WITHEXTENDEDINFO = "withExtendedInfo"; - static const char * ERRORTEXT = "errorText"; - static const char * ERRORCODE = "errorCode"; - static const char * AVATARID = "avatarId"; - static const char * UNNAMED = "(unnamed)"; - static const char * UNSPECIFIED = "(unspecified)"; - static const char * CONTENTDISPOSITION = "Content-Disposition"; - static const char * CONTENTTYPE = "Content-Type"; + static const char *ERRORTEXT = "errorText"; + static const char *ERRORCODE = "errorCode"; + static const char *AVATARID = "avatarId"; + static const char *UNNAMED = "(unnamed)"; + static const char *UNSPECIFIED = "(unspecified)"; + static const char *CONTENTDISPOSITION = "Content-Disposition"; + static const char *CONTENTTYPE = "Content-Type"; - static const char * REQUIREMENTS = "requirements"; - static const char * PASSWORDPATTERN = "passwordPattern"; - static const char * ACCESSPOLICY = "accessPolicy"; - static const char * PASSWORDPOLICY = "passwordPolicy"; - static const char * FORGOTPASSWORD = "forgotPassword"; - static const char * RESENDMFACODE = "resendMFACode"; - static const char * COMPLETEMFACHALLENGE = "completeMFAChallenge"; - static const char * ME = "me"; - static const char * TELEMETRY = "telemetry"; - static const char * INTERVAL = "interval"; - static const char * UI = "UI"; - static const char * BANDWIDTH = "bandwidth"; -} + static const char *REQUIREMENTS = "requirements"; + static const char *PASSWORDPATTERN = "passwordPattern"; + static const char *ACCESSPOLICY = "accessPolicy"; + static const char *PASSWORDPOLICY = "passwordPolicy"; + static const char *FORGOTPASSWORD = "forgotPassword"; + static const char *RESENDMFACODE = "resendMFACode"; + static const char *COMPLETEMFACHALLENGE = "completeMFAChallenge"; + static const char *ME = "me"; + static const char *TELEMETRY = "telemetry"; + static const char *INTERVAL = "interval"; + static const char *UI = "UI"; + static const char *BANDWIDTH = "bandwidth"; +} // namespace OpenWifi::RESTAPI::Protocol namespace OpenWifi::uCentralProtocol { - const int SERIAL_NUMBER_LENGTH = 30; + const int SERIAL_NUMBER_LENGTH = 30; - // vocabulary used in the PROTOCOL.md file - static const char *JSONRPC = "jsonrpc"; - static const char *ID = "id"; - static const char *UUID = "uuid"; - static const char *JSONRPC_VERSION = "2.0"; - static const char *METHOD = "method"; - static const char *PARAMS = "params"; - static const char *SERIAL = "serial"; - static const char *FIRMWARE = "firmware"; - static const char *CONNECT = "connect"; - static const char *STATE = "state"; - static const char *STATUS = "status"; - static const char *ERROR = "error"; - static const char *TEXT = "text"; - static const char *HEALTHCHECK = "healthcheck"; - static const char *LOG = "log"; - static const char *CRASHLOG = "crashlog"; - static const char *PING = "ping"; - static const char *CFGPENDING = "cfgpending"; - static const char *RECOVERY = "recovery"; - static const char *COMPRESS_64 = "compress_64"; - static const char *CAPABILITIES = "capabilities"; - static const char *REQUEST_UUID = "request_uuid"; - static const char *SANITY = "sanity"; - static const char *DATA = "data"; - static const char *LOGLINES = "loglines"; - static const char *SEVERITY = "severity"; - static const char *ACTIVE = "active"; - static const char *OVERRIDEDFS = "override_dfs"; - static const char *REBOOT = "reboot"; - static const char *WHEN = "when"; - static const char *CONFIG = "config"; - static const char *EMPTY_JSON_DOC = "{}"; - static const char *RESULT = "result"; + // vocabulary used in the PROTOCOL.md file + static const char *JSONRPC = "jsonrpc"; + static const char *ID = "id"; + static const char *UUID = "uuid"; + static const char *JSONRPC_VERSION = "2.0"; + static const char *METHOD = "method"; + static const char *PARAMS = "params"; + static const char *SERIAL = "serial"; + static const char *FIRMWARE = "firmware"; + static const char *CONNECT = "connect"; + static const char *STATE = "state"; + static const char *STATUS = "status"; + static const char *ERROR = "error"; + static const char *TEXT = "text"; + static const char *HEALTHCHECK = "healthcheck"; + static const char *LOG = "log"; + static const char *CRASHLOG = "crashlog"; + static const char *PING = "ping"; + static const char *CFGPENDING = "cfgpending"; + static const char *RECOVERY = "recovery"; + static const char *COMPRESS_64 = "compress_64"; + static const char *CAPABILITIES = "capabilities"; + static const char *REQUEST_UUID = "request_uuid"; + static const char *SANITY = "sanity"; + static const char *DATA = "data"; + static const char *LOGLINES = "loglines"; + static const char *SEVERITY = "severity"; + static const char *ACTIVE = "active"; + static const char *OVERRIDEDFS = "override_dfs"; + static const char *REBOOT = "reboot"; + static const char *WHEN = "when"; + static const char *CONFIG = "config"; + static const char *EMPTY_JSON_DOC = "{}"; + static const char *RESULT = "result"; static const char *RESULT_64 = "result_64"; static const char *RESULT_SZ = "result_sz"; - static const char *REQUEST = "request"; - static const char *PERFORM = "perform"; - static const char *CONFIGURE = "configure"; - static const char *PENDING = "pending"; - static const char *SUBMITTED_BY_SYSTEM = "*system"; - static const char *URI = "uri"; - static const char *COMMAND = "command"; - static const char *PAYLOAD = "payload"; - static const char *KEEP_REDIRECTOR = "keep_redirector"; - static const char *DURATION = "duration"; - static const char *PATTERN = "pattern"; - static const char *LEDS = "leds"; + static const char *REQUEST = "request"; + static const char *PERFORM = "perform"; + static const char *CONFIGURE = "configure"; + static const char *PENDING = "pending"; + static const char *SUBMITTED_BY_SYSTEM = "*system"; + static const char *URI = "uri"; + static const char *COMMAND = "command"; + static const char *PAYLOAD = "payload"; + static const char *KEEP_REDIRECTOR = "keep_redirector"; + static const char *DURATION = "duration"; + static const char *PATTERN = "pattern"; + static const char *LEDS = "leds"; static const char *DEBUG = "debug"; - static const char *ON = "on"; - static const char *OFF = "off"; - static const char *BLINK = "blink"; - static const char *PACKETS = "packets"; - static const char *NETWORK = "network"; - static const char *INTERFACE = "interface"; - static const char *TRACE = "trace"; - static const char *WIFISCAN = "wifiscan"; - static const char *TYPES = "types"; - static const char *EVENT = "event"; - static const char *MESSAGE = "message"; - static const char *RTTY = "rtty"; - static const char *TOKEN = "token"; - static const char *SERVER = "server"; - static const char *PORT = "port"; - static const char *USER = "user"; - static const char *TIMEOUT = "timeout"; - static const char *UPGRADE = "upgrade"; - static const char *FACTORY = "factory"; - static const char *VERBOSE = "verbose"; - static const char *BANDS = "bands"; - static const char *CHANNELS = "channels"; - static const char *PASSWORD = "password"; - static const char *DEVICEUPDATE = "deviceupdate"; + static const char *ON = "on"; + static const char *OFF = "off"; + static const char *BLINK = "blink"; + static const char *PACKETS = "packets"; + static const char *NETWORK = "network"; + static const char *INTERFACE = "interface"; + static const char *TRACE = "trace"; + static const char *WIFISCAN = "wifiscan"; + static const char *TYPES = "types"; + static const char *EVENT = "event"; + static const char *MESSAGE = "message"; + static const char *RTTY = "rtty"; + static const char *TOKEN = "token"; + static const char *SERVER = "server"; + static const char *PORT = "port"; + static const char *USER = "user"; + static const char *TIMEOUT = "timeout"; + static const char *UPGRADE = "upgrade"; + static const char *FACTORY = "factory"; + static const char *VERBOSE = "verbose"; + static const char *BANDS = "bands"; + static const char *CHANNELS = "channels"; + static const char *PASSWORD = "password"; + static const char *DEVICEUPDATE = "deviceupdate"; static const char *FWSIGNATURE = "FWsignature"; static const char *SIGNATURE = "signature"; - static const char *SERIALNUMBER = "serialNumber"; - static const char *COMPATIBLE = "compatible"; - static const char *DISCONNECTION = "disconnection"; - static const char *TIMESTAMP = "timestamp"; - static const char *SYSTEM = "system"; - static const char *HOST = "host"; - static const char *CONNECTIONIP = "connectionIp"; - static const char *TELEMETRY = "telemetry"; - static const char *BANDWIDTH = "bandwidth"; + static const char *SERIALNUMBER = "serialNumber"; + static const char *COMPATIBLE = "compatible"; + static const char *DISCONNECTION = "disconnection"; + static const char *TIMESTAMP = "timestamp"; + static const char *SYSTEM = "system"; + static const char *HOST = "host"; + static const char *CONNECTIONIP = "connectionIp"; + static const char *TELEMETRY = "telemetry"; + static const char *BANDWIDTH = "bandwidth"; static const char *SCRIPT = "script"; static const char *TYPE = "type"; @@ -490,24 +633,24 @@ namespace OpenWifi::uCentralProtocol { static const char *RADIUSCOA = "coa"; static const char *RADIUSDST = "dst"; static const char *IES = "ies"; - } +} // namespace OpenWifi::uCentralProtocol namespace OpenWifi::uCentralProtocol::Events { - static const char *CONNECT = "connect"; - static const char *STATE = "state"; - static const char *HEALTHCHECK = "healthcheck"; - static const char *LOG = "log"; - static const char *CRASHLOG = "crashlog"; - static const char *PING = "ping"; - static const char *CFGPENDING = "cfgpending"; - static const char *RECOVERY = "recovery"; - static const char *TELEMETRY = "telemetry"; - static const char *DEVICEUPDATE = "deviceupdate"; + static const char *CONNECT = "connect"; + static const char *STATE = "state"; + static const char *HEALTHCHECK = "healthcheck"; + static const char *LOG = "log"; + static const char *CRASHLOG = "crashlog"; + static const char *PING = "ping"; + static const char *CFGPENDING = "cfgpending"; + static const char *RECOVERY = "recovery"; + static const char *TELEMETRY = "telemetry"; + static const char *DEVICEUPDATE = "deviceupdate"; static const char *VENUE_BROADCAST = "venue_broadcast"; static const char *ALARM = "alarm"; - enum EVENT_MSG { + enum EVENT_MSG { ET_UNKNOWN, ET_CONNECT, ET_STATE, @@ -525,41 +668,41 @@ namespace OpenWifi::uCentralProtocol::Events { ET_ALARM }; - inline EVENT_MSG EventFromString(const std::string & Method) { - if(strcmp(STATE,Method.c_str())==0) + inline EVENT_MSG EventFromString(const std::string &Method) { + if (strcmp(STATE, Method.c_str()) == 0) return ET_STATE; - else if(strcmp(HEALTHCHECK,Method.c_str())==0) + else if (strcmp(HEALTHCHECK, Method.c_str()) == 0) return ET_HEALTHCHECK; - else if(strcmp(CONNECT,Method.c_str())==0) + else if (strcmp(CONNECT, Method.c_str()) == 0) return ET_CONNECT; - else if(strcmp(CFGPENDING,Method.c_str())==0) + else if (strcmp(CFGPENDING, Method.c_str()) == 0) return ET_CFGPENDING; - else if(strcmp(CRASHLOG,Method.c_str())==0) + else if (strcmp(CRASHLOG, Method.c_str()) == 0) return ET_CRASHLOG; - else if(strcmp(DEVICEUPDATE,Method.c_str())==0) + else if (strcmp(DEVICEUPDATE, Method.c_str()) == 0) return ET_DEVICEUPDATE; - else if(strcmp(LOG,Method.c_str())==0) + else if (strcmp(LOG, Method.c_str()) == 0) return ET_LOG; - else if(strcmp(PING,Method.c_str())==0) + else if (strcmp(PING, Method.c_str()) == 0) return ET_PING; - else if(strcmp(RECOVERY,Method.c_str())==0) + else if (strcmp(RECOVERY, Method.c_str()) == 0) return ET_RECOVERY; - else if(strcmp(TELEMETRY,Method.c_str())==0) + else if (strcmp(TELEMETRY, Method.c_str()) == 0) return ET_TELEMETRY; - else if(strcmp(VENUE_BROADCAST,Method.c_str())==0) + else if (strcmp(VENUE_BROADCAST, Method.c_str()) == 0) return ET_VENUEBROADCAST; - else if(strcmp(EVENT,Method.c_str())==0) + else if (strcmp(EVENT, Method.c_str()) == 0) return ET_EVENT; - else if(strcmp(WIFISCAN,Method.c_str())==0) + else if (strcmp(WIFISCAN, Method.c_str()) == 0) return ET_WIFISCAN; - else if(strcmp(ALARM,Method.c_str())==0) + else if (strcmp(ALARM, Method.c_str()) == 0) return ET_WIFISCAN; return ET_ALARM; }; -} +} // namespace OpenWifi::uCentralProtocol::Events namespace OpenWifi::APCommands { - enum class Commands:uint8_t { + enum class Commands : uint8_t { capabilities, logs, healthchecks, @@ -581,60 +724,47 @@ namespace OpenWifi::APCommands { unknown }; - inline static const std::vector uCentralAPCommands { - RESTAPI::Protocol::CAPABILITIES, - RESTAPI::Protocol::LOGS, - RESTAPI::Protocol::HEALTHCHECKS, - RESTAPI::Protocol::STATISTICS, - RESTAPI::Protocol::STATUS, - RESTAPI::Protocol::RTTY, - RESTAPI::Protocol::CONFIGURE, - RESTAPI::Protocol::UPGRADE, - RESTAPI::Protocol::REBOOT, - RESTAPI::Protocol::FACTORY, - RESTAPI::Protocol::LEDS, - RESTAPI::Protocol::TRACE, - RESTAPI::Protocol::REQUEST, - RESTAPI::Protocol::WIFISCAN, - RESTAPI::Protocol::EVENTQUEUE, - RESTAPI::Protocol::TELEMETRY, - RESTAPI::Protocol::PING, - RESTAPI::Protocol::SCRIPT}; + inline static const std::vector uCentralAPCommands{ + RESTAPI::Protocol::CAPABILITIES, RESTAPI::Protocol::LOGS, + RESTAPI::Protocol::HEALTHCHECKS, RESTAPI::Protocol::STATISTICS, + RESTAPI::Protocol::STATUS, RESTAPI::Protocol::RTTY, + RESTAPI::Protocol::CONFIGURE, RESTAPI::Protocol::UPGRADE, + RESTAPI::Protocol::REBOOT, RESTAPI::Protocol::FACTORY, + RESTAPI::Protocol::LEDS, RESTAPI::Protocol::TRACE, + RESTAPI::Protocol::REQUEST, RESTAPI::Protocol::WIFISCAN, + RESTAPI::Protocol::EVENTQUEUE, RESTAPI::Protocol::TELEMETRY, + RESTAPI::Protocol::PING, RESTAPI::Protocol::SCRIPT}; - inline const char * to_string(Commands Cmd) { - return uCentralAPCommands[(uint8_t)Cmd]; - } + inline const char *to_string(Commands Cmd) { return uCentralAPCommands[(uint8_t)Cmd]; } inline Commands to_apcommand(const char *cmd) { - for(auto i=(uint8_t)Commands::capabilities;i!=(uint8_t)Commands::unknown;++i) { - if(strcmp(uCentralAPCommands[i],cmd)==0) + for (auto i = (uint8_t)Commands::capabilities; i != (uint8_t)Commands::unknown; ++i) { + if (strcmp(uCentralAPCommands[i], cmd) == 0) return (Commands)i; } return Commands::unknown; } -} +} // namespace OpenWifi::APCommands namespace OpenWifi::Provisioning::DeviceClass { - static const char * ANY = "any"; - static const char * SUBSCRIBER = "subscriber"; - static const char * VENUE = "venue"; - static const char * ENTITY = "entity"; + static const char *ANY = "any"; + static const char *SUBSCRIBER = "subscriber"; + static const char *VENUE = "venue"; + static const char *ENTITY = "entity"; - inline bool Validate(const char *s) { - static std::vector Values{ ANY, ENTITY, SUBSCRIBER, VENUE }; - return std::find(cbegin(Values), cend(Values), s) != cend(Values); - } + inline bool Validate(const char *s) { + static std::vector Values{ANY, ENTITY, SUBSCRIBER, VENUE}; + return std::find(cbegin(Values), cend(Values), s) != cend(Values); + } -} +} // namespace OpenWifi::Provisioning::DeviceClass -#if defined(__GNUC__ ) +#if defined(__GNUC__) #pragma GCC diagnostic pop #endif #if defined(__clang__) #pragma clang diagnostic pop #endif - - diff --git a/src/framework/utils.cpp b/src/framework/utils.cpp index a03f0a5..976bcc6 100644 --- a/src/framework/utils.cpp +++ b/src/framework/utils.cpp @@ -4,572 +4,586 @@ #include "Poco/Path.h" -#include "framework/utils.h" #include "framework/AppServiceRegistry.h" +#include "framework/utils.h" namespace OpenWifi::Utils { -bool NormalizeMac(std::string & Mac) { - Poco::replaceInPlace(Mac,":",""); - Poco::replaceInPlace(Mac,"-",""); - if(Mac.size()!=12) - return false; - for(const auto &i:Mac) { - if(!std::isxdigit(i)) + bool NormalizeMac(std::string &Mac) { + Poco::replaceInPlace(Mac, ":", ""); + Poco::replaceInPlace(Mac, "-", ""); + if (Mac.size() != 12) return false; + for (const auto &i : Mac) { + if (!std::isxdigit(i)) + return false; + } + Poco::toLowerInPlace(Mac); + return true; } - Poco::toLowerInPlace(Mac); - return true; -} -[[nodiscard]] bool ValidSerialNumber(const std::string &Serial) { - return ((Serial.size() < uCentralProtocol::SERIAL_NUMBER_LENGTH) && - std::all_of(Serial.begin(),Serial.end(),[](auto i){return std::isxdigit(i);})); -} + [[nodiscard]] bool ValidSerialNumber(const std::string &Serial) { + return ((Serial.size() < uCentralProtocol::SERIAL_NUMBER_LENGTH) && + std::all_of(Serial.begin(), Serial.end(), [](auto i) { return std::isxdigit(i); })); + } -[[nodiscard]] bool ValidUUID(const std::string &UUID) { - if(UUID.size()>36) - return false; - uint dashes=0; - return (std::all_of(UUID.begin(),UUID.end(),[&](auto i){ if(i=='-') dashes++; return i=='-' || std::isxdigit(i);})) && (dashes>0); -} + [[nodiscard]] bool ValidUUID(const std::string &UUID) { + if (UUID.size() > 36) + return false; + uint dashes = 0; + return (std::all_of(UUID.begin(), UUID.end(), + [&](auto i) { + if (i == '-') + dashes++; + return i == '-' || std::isxdigit(i); + })) && + (dashes > 0); + } -[[nodiscard]] std::vector Split(const std::string &List, char Delimiter ) { - std::vector ReturnList; + [[nodiscard]] std::vector Split(const std::string &List, char Delimiter) { + std::vector ReturnList; - unsigned long P=0; + unsigned long P = 0; - while(P 12) + R = R.substr(0, 12); + + char buf[18]; + + buf[0] = R[0]; + buf[1] = R[1]; + buf[2] = ':'; + buf[3] = R[2]; + buf[4] = R[3]; + buf[5] = ':'; + buf[6] = R[4]; + buf[7] = R[5]; + buf[8] = ':'; + buf[9] = R[6]; + buf[10] = R[7]; + buf[11] = ':'; + buf[12] = R[8]; + buf[13] = R[9]; + buf[14] = ':'; + buf[15] = R[10]; + buf[16] = R[11]; + buf[17] = 0; + + return buf; + } + + uint64_t MACToInt(const std::string &MAC) { + uint64_t Result = 0; + for (const auto &c : MAC) { + if (c == ':') + continue; + Result <<= 4; + if (c >= '0' && c <= '9') { + Result += (c - '0'); + } else if (c >= 'a' && c <= 'f') { + Result += (c - 'a' + 10); + } else if (c >= 'A' && c <= 'F') { + Result += (c - 'A' + 10); + } + } + return Result; + } + + [[nodiscard]] bool ValidHostname(const std::string &Hostname) { + static std::regex HostNameRegex( + "^(?!-)[A-Za-z0-9-]+([\\-\\.]{1}[a-z0-9]+)*\\.[A-Za-z]{2,6}$"); + return std::regex_match(Hostname, HostNameRegex); + } + + [[nodiscard]] std::string ToHex(const std::vector &B) { + std::string R; + R.reserve(B.size() * 2); + + static const char hex[] = "0123456789abcdef"; + + for (const auto &i : B) { + R += (hex[(i & 0xf0) >> 4]); + R += (hex[(i & 0x0f)]); + } + + return R; + } + + inline static const char kEncodeLookup[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + inline static const char kPadCharacter = '='; + + using byte = std::uint8_t; + + [[nodiscard]] std::string base64encode(const byte *input, uint32_t size) { + std::string encoded; + encoded.reserve(((size / 3) + (size % 3 > 0)) * 4); + + std::uint32_t temp, i, ee; + ee = (size / 3); + + for (i = 0; i < 3 * ee; ++i) { + temp = input[i++] << 16; + temp += input[i++] << 8; + temp += input[i]; + encoded.append(1, kEncodeLookup[(temp & 0x00FC0000) >> 18]); + encoded.append(1, kEncodeLookup[(temp & 0x0003F000) >> 12]); + encoded.append(1, kEncodeLookup[(temp & 0x00000FC0) >> 6]); + encoded.append(1, kEncodeLookup[(temp & 0x0000003F)]); + } + + switch (size % 3) { + case 1: + temp = input[i] << 16; + encoded.append(1, kEncodeLookup[(temp & 0x00FC0000) >> 18]); + encoded.append(1, kEncodeLookup[(temp & 0x0003F000) >> 12]); + encoded.append(2, kPadCharacter); + break; + case 2: + temp = input[i++] << 16; + temp += input[i] << 8; + encoded.append(1, kEncodeLookup[(temp & 0x00FC0000) >> 18]); + encoded.append(1, kEncodeLookup[(temp & 0x0003F000) >> 12]); + encoded.append(1, kEncodeLookup[(temp & 0x00000FC0) >> 6]); + encoded.append(1, kPadCharacter); break; } - else - ReturnList.push_back(List.substr(P,P2-P)); - P=P2+1; - } - return ReturnList; -} - -[[nodiscard]] std::string FormatIPv6(const std::string & I ) -{ - if(I.substr(0,8) == "[::ffff:") - { - unsigned long PClosingBracket = I.find_first_of(']'); - - std::string ip = I.substr(8, PClosingBracket-8); - std::string port = I.substr(PClosingBracket+1); - return ip + port; + return encoded; } - return I; -} + [[nodiscard]] std::vector base64decode(const std::string &input) { + if (input.length() % 4) + throw std::runtime_error("Invalid base64 length!"); -void padTo(std::string& str, size_t num, char paddingChar) { - str.append(num - str.length() % num, paddingChar); -} + std::size_t padding = 0; -[[nodiscard]] std::string SerialToMAC(const std::string &Serial) { - std::string R = Serial; + if (input.length()) { + if (input[input.length() - 1] == kPadCharacter) + padding++; + if (input[input.length() - 2] == kPadCharacter) + padding++; + } - if(R.size()<12) - padTo(R,12,'0'); - else if (R.size()>12) - R = R.substr(0,12); + std::vector decoded; + decoded.reserve(((input.length() / 4) * 3) - padding); - char buf[18]; + std::uint32_t temp = 0; + auto it = input.begin(); - buf[0] = R[0]; buf[1] = R[1] ; buf[2] = ':' ; - buf[3] = R[2] ; buf[4] = R[3]; buf[5] = ':' ; - buf[6] = R[4]; buf[7] = R[5] ; buf[8] = ':' ; - buf[9] = R[6] ; buf[10]= R[7]; buf[11] = ':'; - buf[12] = R[8] ; buf[13]= R[9]; buf[14] = ':'; - buf[15] = R[10] ; buf[16]= R[11];buf[17] = 0; + while (it < input.end()) { + for (std::size_t i = 0; i < 4; ++i) { + temp <<= 6; + if (*it >= 0x41 && *it <= 0x5A) + temp |= *it - 0x41; + else if (*it >= 0x61 && *it <= 0x7A) + temp |= *it - 0x47; + else if (*it >= 0x30 && *it <= 0x39) + temp |= *it + 0x04; + else if (*it == 0x2B) + temp |= 0x3E; + else if (*it == 0x2F) + temp |= 0x3F; + else if (*it == kPadCharacter) { + switch (input.end() - it) { + case 1: + decoded.push_back((temp >> 16) & 0x000000FF); + decoded.push_back((temp >> 8) & 0x000000FF); + return decoded; + case 2: + decoded.push_back((temp >> 10) & 0x000000FF); + return decoded; + default: + throw std::runtime_error("Invalid padding in base64!"); + } + } else + throw std::runtime_error("Invalid character in base64!"); - return buf; -} + ++it; + } -uint64_t MACToInt(const std::string &MAC) { - uint64_t Result = 0 ; - for(const auto &c:MAC) { - if(c==':') - continue; - Result <<= 4; - if(c>='0' && c<='9') { - Result += (c - '0'); - } else if (c>='a' && c<='f') { - Result += (c-'a'+10); - } else if (c>='A' && c<='F') { - Result += (c-'A'+10); + decoded.push_back((temp >> 16) & 0x000000FF); + decoded.push_back((temp >> 8) & 0x000000FF); + decoded.push_back((temp)&0x000000FF); + } + + return decoded; + } + + bool ParseTime(const std::string &Time, int &Hours, int &Minutes, int &Seconds) { + Poco::StringTokenizer TimeTokens(Time, ":", Poco::StringTokenizer::TOK_TRIM); + + Hours = Minutes = Seconds = 0; + if (TimeTokens.count() == 1) { + Hours = std::atoi(TimeTokens[0].c_str()); + } else if (TimeTokens.count() == 2) { + Hours = std::atoi(TimeTokens[0].c_str()); + Minutes = std::atoi(TimeTokens[1].c_str()); + } else if (TimeTokens.count() == 3) { + Hours = std::atoi(TimeTokens[0].c_str()); + Minutes = std::atoi(TimeTokens[1].c_str()); + Seconds = std::atoi(TimeTokens[2].c_str()); + } else + return false; + return true; + } + + bool ParseDate(const std::string &Time, int &Year, int &Month, int &Day) { + Poco::StringTokenizer DateTokens(Time, "-", Poco::StringTokenizer::TOK_TRIM); + + Year = Month = Day = 0; + if (DateTokens.count() == 3) { + Year = std::atoi(DateTokens[0].c_str()); + Month = std::atoi(DateTokens[1].c_str()); + Day = std::atoi(DateTokens[2].c_str()); + } else + return false; + return true; + } + + bool CompareTime(int H1, int H2, int M1, int M2, int S1, int S2) { + if (H1 < H2) + return true; + if (H1 > H2) + return false; + if (M1 < M2) + return true; + if (M2 > M1) + return false; + if (S1 <= S2) + return true; + return false; + } + + [[nodiscard]] std::string LogLevelToString(int Level) { + switch (Level) { + case Poco::Message::PRIO_DEBUG: + return "debug"; + case Poco::Message::PRIO_INFORMATION: + return "information"; + case Poco::Message::PRIO_FATAL: + return "fatal"; + case Poco::Message::PRIO_WARNING: + return "warning"; + case Poco::Message::PRIO_NOTICE: + return "notice"; + case Poco::Message::PRIO_CRITICAL: + return "critical"; + case Poco::Message::PRIO_ERROR: + return "error"; + case Poco::Message::PRIO_TRACE: + return "trace"; + default: + return "none"; } } - return Result; -} -[[nodiscard]] bool ValidHostname(const std::string &Hostname) { - static std::regex HostNameRegex("^(?!-)[A-Za-z0-9-]+([\\-\\.]{1}[a-z0-9]+)*\\.[A-Za-z]{2,6}$"); - return std::regex_match(Hostname,HostNameRegex); -} - -[[nodiscard]] std::string ToHex(const std::vector & B) { - std::string R; - R.reserve(B.size()*2); - - static const char hex[] = "0123456789abcdef"; - - for(const auto &i:B) - { - R += (hex[ (i & 0xf0) >> 4]); - R += (hex[ (i & 0x0f) ]); + [[nodiscard]] uint64_t SerialNumberToInt(const std::string &S) { + return std::stoull(S, nullptr, 16); } - return R; -} - -inline static const char kEncodeLookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -inline static const char kPadCharacter = '='; - -using byte = std::uint8_t; - -[[nodiscard]] std::string base64encode(const byte *input, uint32_t size) { - std::string encoded; - encoded.reserve(((size / 3) + (size % 3 > 0)) * 4); - - std::uint32_t temp,i,ee; - ee = (size/3); - - for (i = 0; i < 3*ee; ++i) { - temp = input[i++] << 16; - temp += input[i++] << 8; - temp += input[i]; - encoded.append(1, kEncodeLookup[(temp & 0x00FC0000) >> 18]); - encoded.append(1, kEncodeLookup[(temp & 0x0003F000) >> 12]); - encoded.append(1, kEncodeLookup[(temp & 0x00000FC0) >> 6]); - encoded.append(1, kEncodeLookup[(temp & 0x0000003F)]); + [[nodiscard]] std::string IntToSerialNumber(uint64_t S) { + char b[16]; + for (int i = 0; i < 12; ++i) { + int B = (S & 0x0f); + if (B < 10) + b[11 - i] = B + '0'; + else + b[11 - i] = B - 10 + 'a'; + S >>= 4; + } + b[12] = 0; + return b; } - switch (size % 3) { - case 1: - temp = input[i] << 16; - encoded.append(1, kEncodeLookup[(temp & 0x00FC0000) >> 18]); - encoded.append(1, kEncodeLookup[(temp & 0x0003F000) >> 12]); - encoded.append(2, kPadCharacter); - break; - case 2: - temp = input[i++] << 16; - temp += input[i] << 8; - encoded.append(1, kEncodeLookup[(temp & 0x00FC0000) >> 18]); - encoded.append(1, kEncodeLookup[(temp & 0x0003F000) >> 12]); - encoded.append(1, kEncodeLookup[(temp & 0x00000FC0) >> 6]); - encoded.append(1, kPadCharacter); - break; + [[nodiscard]] bool SerialNumberMatch(const std::string &S1, const std::string &S2, int Bits) { + auto S1_i = SerialNumberToInt(S1); + auto S2_i = SerialNumberToInt(S2); + return ((S1_i >> Bits) == (S2_i >> Bits)); } - return encoded; -} + [[nodiscard]] uint64_t SerialNumberToOUI(const std::string &S) { + uint64_t Result = 0; + int Digits = 0; -[[nodiscard]] std::vector base64decode(const std::string& input) -{ - if(input.length() % 4) - throw std::runtime_error("Invalid base64 length!"); - - std::size_t padding=0; - - if(input.length()) - { - if(input[input.length() - 1] == kPadCharacter) padding++; - if(input[input.length() - 2] == kPadCharacter) padding++; + for (const auto &i : S) { + if (std::isxdigit(i)) { + if (i >= '0' && i <= '9') { + Result <<= 4; + Result += i - '0'; + } else if (i >= 'A' && i <= 'F') { + Result <<= 4; + Result += i - 'A' + 10; + } else if (i >= 'a' && i <= 'f') { + Result <<= 4; + Result += i - 'a' + 10; + } + Digits++; + if (Digits == 6) + break; + } + } + return Result; } - std::vector decoded; - decoded.reserve(((input.length() / 4) * 3) - padding); + [[nodiscard]] uint64_t GetDefaultMacAsInt64() { + uint64_t Result = 0; + auto IFaceList = Poco::Net::NetworkInterface::list(); - std::uint32_t temp=0; - auto it = input.begin(); + for (const auto &iface : IFaceList) { + if (iface.isRunning() && !iface.isLoopback()) { + auto MAC = iface.macAddress(); + for (auto const &i : MAC) { + Result <<= 8; + Result += (uint8_t)i; + } + if (Result != 0) + break; + } + } + return Result; + } - while(it < input.end()) - { - for(std::size_t i = 0; i < 4; ++i) - { - temp <<= 6; - if (*it >= 0x41 && *it <= 0x5A) temp |= *it - 0x41; - else if(*it >= 0x61 && *it <= 0x7A) temp |= *it - 0x47; - else if(*it >= 0x30 && *it <= 0x39) temp |= *it + 0x04; - else if(*it == 0x2B) temp |= 0x3E; - else if(*it == 0x2F) temp |= 0x3F; - else if(*it == kPadCharacter) - { - switch(input.end() - it) - { - case 1: - decoded.push_back((temp >> 16) & 0x000000FF); - decoded.push_back((temp >> 8 ) & 0x000000FF); - return decoded; - case 2: - decoded.push_back((temp >> 10) & 0x000000FF); - return decoded; - default: - throw std::runtime_error("Invalid padding in base64!"); + [[nodiscard]] uint64_t InitializeSystemId() { + std::random_device RDev; + std::srand(RDev()); + std::chrono::high_resolution_clock Clock; + auto Now = Clock.now().time_since_epoch().count(); + auto S = (GetDefaultMacAsInt64() + std::rand() + Now); + OpenWifi::AppServiceRegistry().Set("systemid", S); + return S; + } + + [[nodiscard]] uint64_t GetSystemId() { + uint64_t ID = 0; + if (!AppServiceRegistry().Get("systemid", ID)) { + return InitializeSystemId(); + } + return ID; + } + + [[nodiscard]] bool ValidEMailAddress(const std::string &email) { + // define a regular expression + static const std::regex pattern( + "[_a-z0-9-]+(\\.[_a-z0-9-]+)*(\\+[a-z0-9-]+)?@[a-z0-9-]+(\\.[a-z0-9-]+)*"); + // try to match the string with the regular expression + return std::regex_match(email, pattern); + } + + [[nodiscard]] std::string LoadFile(const Poco::File &F) { + std::string Result; + try { + std::ostringstream OS; + std::ifstream IF(F.path()); + Poco::StreamCopier::copyStream(IF, OS); + Result = OS.str(); + } catch (...) { + } + return Result; + } + + void ReplaceVariables(std::string &Content, const Types::StringPairVec &P) { + for (const auto &[Variable, Value] : P) { + Poco::replaceInPlace(Content, "${" + Variable + "}", Value); + } + } + + [[nodiscard]] MediaTypeEncoding FindMediaType(const Poco::File &F) { + const auto E = Poco::Path(F.path()).getExtension(); + if (E == "png") + return MediaTypeEncoding{.Encoding = BINARY, .ContentType = "image/png"}; + if (E == "gif") + return MediaTypeEncoding{.Encoding = BINARY, .ContentType = "image/gif"}; + if (E == "jpeg" || E == "jpg") + return MediaTypeEncoding{.Encoding = BINARY, .ContentType = "image/jpeg"}; + if (E == "svg" || E == "svgz") + return MediaTypeEncoding{.Encoding = PLAIN, .ContentType = "image/svg+xml"}; + if (E == "html") + return MediaTypeEncoding{.Encoding = PLAIN, .ContentType = "text/html"}; + if (E == "css") + return MediaTypeEncoding{.Encoding = PLAIN, .ContentType = "text/css"}; + if (E == "js") + return MediaTypeEncoding{.Encoding = PLAIN, .ContentType = "application/javascript"}; + return MediaTypeEncoding{.Encoding = BINARY, .ContentType = "application/octet-stream"}; + } + + [[nodiscard]] std::string BinaryFileToHexString(const Poco::File &F) { + static const char hex[] = "0123456789abcdef"; + std::string Result; + try { + std::ifstream IF(F.path()); + + int Count = 0; + while (IF.good()) { + if (Count) + Result += ", "; + if ((Count % 32) == 0) + Result += "\r\n"; + Count++; + unsigned char C = IF.get(); + Result += "0x"; + Result += (char)(hex[(C & 0xf0) >> 4]); + Result += (char)(hex[(C & 0x0f)]); + } + } catch (...) { + } + return Result; + } + + [[nodiscard]] std::string SecondsToNiceText(uint64_t Seconds) { + std::string Result; + int Days = Seconds / (24 * 60 * 60); + Seconds -= Days * (24 * 60 * 60); + int Hours = Seconds / (60 * 60); + Seconds -= Hours * (60 * 60); + int Minutes = Seconds / 60; + Seconds -= Minutes * 60; + Result = std::to_string(Days) + " days, " + std::to_string(Hours) + ":" + + std::to_string(Minutes) + ":" + std::to_string(Seconds); + return Result; + } + + [[nodiscard]] bool wgets(const std::string &URL, std::string &Response) { + try { + Poco::URI uri(URL); + Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort()); + + // prepare path + std::string path(uri.getPathAndQuery()); + if (path.empty()) { + path = "/"; + } + + // send request + Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_GET, path, + Poco::Net::HTTPMessage::HTTP_1_1); + session.sendRequest(req); + + Poco::Net::HTTPResponse res; + std::istream &is = session.receiveResponse(res); + std::ostringstream os; + + Poco::StreamCopier::copyStream(is, os); + Response = os.str(); + + return true; + } catch (...) { + } + return false; + } + + [[nodiscard]] bool wgetfile(const Poco::URI &uri, const std::string &FileName) { + try { + Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort()); + + // send request + Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_GET, uri.getPath(), + Poco::Net::HTTPMessage::HTTP_1_1); + session.sendRequest(req); + + Poco::Net::HTTPResponse res; + std::istream &is = session.receiveResponse(res); + std::fstream os(FileName, + std::ios_base::trunc | std::ios_base::binary | std::ios_base::out); + Poco::StreamCopier::copyStream(is, os); + return true; + } catch (...) { + } + return false; + } + + bool ExtractBase64CompressedData(const std::string &CompressedData, + std::string &UnCompressedData, uint64_t compress_sz) { + std::istringstream ifs(CompressedData); + Poco::Base64Decoder b64in(ifs); + std::ostringstream ofs; + Poco::StreamCopier::copyStream(b64in, ofs); + + int factor = 20; + unsigned long MaxSize = compress_sz ? (unsigned long)(compress_sz + 5000) + : (unsigned long)(ofs.str().size() * factor); + while (true) { + std::vector UncompressedBuffer(MaxSize); + unsigned long FinalSize = MaxSize; + auto status = uncompress((uint8_t *)&UncompressedBuffer[0], &FinalSize, + (uint8_t *)ofs.str().c_str(), ofs.str().size()); + if (status == Z_OK) { + UncompressedBuffer[FinalSize] = 0; + UnCompressedData = (char *)&UncompressedBuffer[0]; + return true; + } + if (status == Z_BUF_ERROR) { + if (factor < 300) { + factor += 10; + MaxSize = ofs.str().size() * factor; + continue; + } else { + return false; } } - else throw std::runtime_error("Invalid character in base64!"); - - ++it; - } - - decoded.push_back((temp >> 16) & 0x000000FF); - decoded.push_back((temp >> 8 ) & 0x000000FF); - decoded.push_back((temp ) & 0x000000FF); - } - - return decoded; -} - -bool ParseTime(const std::string &Time, int & Hours, int & Minutes, int & Seconds) { - Poco::StringTokenizer TimeTokens(Time,":",Poco::StringTokenizer::TOK_TRIM); - - Hours = Minutes = Seconds = 0 ; - if(TimeTokens.count()==1) { - Hours = std::atoi(TimeTokens[0].c_str()); - } else if(TimeTokens.count()==2) { - Hours = std::atoi(TimeTokens[0].c_str()); - Minutes = std::atoi(TimeTokens[1].c_str()); - } else if(TimeTokens.count()==3) { - Hours = std::atoi(TimeTokens[0].c_str()); - Minutes = std::atoi(TimeTokens[1].c_str()); - Seconds = std::atoi(TimeTokens[2].c_str()); - } else - return false; - return true; -} - - -bool ParseDate(const std::string &Time, int & Year, int & Month, int & Day) { - Poco::StringTokenizer DateTokens(Time,"-",Poco::StringTokenizer::TOK_TRIM); - - Year = Month = Day = 0 ; - if(DateTokens.count()==3) { - Year = std::atoi(DateTokens[0].c_str()); - Month = std::atoi(DateTokens[1].c_str()); - Day = std::atoi(DateTokens[2].c_str()); - } else - return false; - return true; -} - -bool CompareTime( int H1, int H2, int M1, int M2, int S1, int S2) { - if(H1H2) - return false; - if(M1M1) - return false; - if(S1<=S2) - return true; - return false; -} - -[[nodiscard]] std::string LogLevelToString(int Level) { - switch(Level) { - case Poco::Message::PRIO_DEBUG: return "debug"; - case Poco::Message::PRIO_INFORMATION: return "information"; - case Poco::Message::PRIO_FATAL: return "fatal"; - case Poco::Message::PRIO_WARNING: return "warning"; - case Poco::Message::PRIO_NOTICE: return "notice"; - case Poco::Message::PRIO_CRITICAL: return "critical"; - case Poco::Message::PRIO_ERROR: return "error"; - case Poco::Message::PRIO_TRACE: return "trace"; - default: return "none"; - } -} - -[[nodiscard]] uint64_t SerialNumberToInt(const std::string & S) { - return std::stoull(S,nullptr,16); -} - -[[nodiscard]] std::string IntToSerialNumber(uint64_t S) { - char b[16]; - for(int i=0;i<12;++i) { - int B = (S & 0x0f); - if(B<10) - b[11-i] = B+'0'; - else - b[11-i] = B - 10 + 'a'; - S >>= 4 ; - } - b[12]=0; - return b; -} - - -[[nodiscard]] bool SerialNumberMatch(const std::string &S1, const std::string &S2, int Bits) { - auto S1_i = SerialNumberToInt(S1); - auto S2_i = SerialNumberToInt(S2); - return ((S1_i>>Bits)==(S2_i>>Bits)); -} - -[[nodiscard]] uint64_t SerialNumberToOUI(const std::string & S) { - uint64_t Result = 0 ; - int Digits=0; - - for(const auto &i:S) { - if(std::isxdigit(i)) { - if(i>='0' && i<='9') { - Result <<=4; - Result += i-'0'; - } else if(i>='A' && i<='F') { - Result <<=4; - Result += i-'A'+10; - } else if(i>='a' && i<='f') { - Result <<=4; - Result += i-'a'+10; - } - Digits++; - if(Digits==6) - break; - } - } - return Result; -} - -[[nodiscard]] uint64_t GetDefaultMacAsInt64() { - uint64_t Result=0; - auto IFaceList = Poco::Net::NetworkInterface::list(); - - for(const auto &iface:IFaceList) { - if(iface.isRunning() && !iface.isLoopback()) { - auto MAC = iface.macAddress(); - for (auto const &i : MAC) { - Result <<= 8; - Result += (uint8_t)i; - } - if (Result != 0) - break; - } - } - return Result; -} - -[[nodiscard]] uint64_t InitializeSystemId() { - std::random_device RDev; - std::srand(RDev()); - std::chrono::high_resolution_clock Clock; - auto Now = Clock.now().time_since_epoch().count(); - auto S = (GetDefaultMacAsInt64() + std::rand() + Now) ; - OpenWifi::AppServiceRegistry().Set("systemid",S); - return S; -} - -[[nodiscard]] uint64_t GetSystemId() { - uint64_t ID=0; - if(!AppServiceRegistry().Get("systemid",ID)) { - return InitializeSystemId(); - } - return ID; -} - -[[nodiscard]] bool ValidEMailAddress(const std::string &email) { - // define a regular expression - static const std::regex pattern - ("[_a-z0-9-]+(\\.[_a-z0-9-]+)*(\\+[a-z0-9-]+)?@[a-z0-9-]+(\\.[a-z0-9-]+)*"); - // try to match the string with the regular expression - return std::regex_match(email, pattern); -} - -[[nodiscard]] std::string LoadFile( const Poco::File & F) { - std::string Result; - try { - std::ostringstream OS; - std::ifstream IF(F.path()); - Poco::StreamCopier::copyStream(IF, OS); - Result = OS.str(); - } catch (...) { - - } - return Result; -} - -void ReplaceVariables( std::string & Content , const Types::StringPairVec & P) { - for(const auto &[Variable,Value]:P) { - Poco::replaceInPlace(Content,"${" + Variable + "}", Value); - } -} - -[[nodiscard]] MediaTypeEncoding FindMediaType(const Poco::File &F) { - const auto E = Poco::Path(F.path()).getExtension(); - if(E=="png") - return MediaTypeEncoding{ .Encoding = BINARY, - .ContentType = "image/png" }; - if(E=="gif") - return MediaTypeEncoding{ .Encoding = BINARY, - .ContentType = "image/gif" }; - if(E=="jpeg" || E=="jpg") - return MediaTypeEncoding{ .Encoding = BINARY, - .ContentType = "image/jpeg" }; - if(E=="svg" || E=="svgz") - return MediaTypeEncoding{ .Encoding = PLAIN, - .ContentType = "image/svg+xml" }; - if(E=="html") - return MediaTypeEncoding{ .Encoding = PLAIN, - .ContentType = "text/html" }; - if(E=="css") - return MediaTypeEncoding{ .Encoding = PLAIN, - .ContentType = "text/css" }; - if(E=="js") - return MediaTypeEncoding{ .Encoding = PLAIN, - .ContentType = "application/javascript" }; - return MediaTypeEncoding{ .Encoding = BINARY, - .ContentType = "application/octet-stream" }; -} - -[[nodiscard]] std::string BinaryFileToHexString(const Poco::File &F) { - static const char hex[] = "0123456789abcdef"; - std::string Result; - try { - std::ifstream IF(F.path()); - - int Count = 0; - while (IF.good()) { - if (Count) - Result += ", "; - if ((Count % 32) == 0) - Result += "\r\n"; - Count++; - unsigned char C = IF.get(); - Result += "0x"; - Result += (char) (hex[(C & 0xf0) >> 4]); - Result += (char) (hex[(C & 0x0f)]); - } - } catch(...) { - - } - return Result; -} - -[[nodiscard]] std::string SecondsToNiceText(uint64_t Seconds) { - std::string Result; - int Days = Seconds / (24*60*60); - Seconds -= Days * (24*60*60); - int Hours= Seconds / (60*60); - Seconds -= Hours * (60*60); - int Minutes = Seconds / 60; - Seconds -= Minutes * 60; - Result = std::to_string(Days) +" days, " + std::to_string(Hours) + ":" + std::to_string(Minutes) + ":" + std::to_string(Seconds); - return Result; -} - -[[nodiscard]] bool wgets(const std::string &URL, std::string &Response) { - try { - Poco::URI uri(URL); - Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort()); - - // prepare path - std::string path(uri.getPathAndQuery()); - if (path.empty()) { - path = "/"; - } - - // send request - Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1); - session.sendRequest(req); - - Poco::Net::HTTPResponse res; - std::istream &is = session.receiveResponse(res); - std::ostringstream os; - - Poco::StreamCopier::copyStream(is,os); - Response = os.str(); - - return true; - } catch (...) { - - } - return false; -} - -[[nodiscard]] bool wgetfile(const Poco::URI &uri, const std::string &FileName) { - try { - Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort()); - - // send request - Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_GET, uri.getPath(), Poco::Net::HTTPMessage::HTTP_1_1); - session.sendRequest(req); - - Poco::Net::HTTPResponse res; - std::istream &is = session.receiveResponse(res); - std::fstream os(FileName,std::ios_base::trunc | std::ios_base::binary | std::ios_base::out); - Poco::StreamCopier::copyStream(is,os); - return true; - } catch (...) { - - } - return false; -} - -bool ExtractBase64CompressedData(const std::string &CompressedData, - std::string &UnCompressedData, uint64_t compress_sz ) { - std::istringstream ifs(CompressedData); - Poco::Base64Decoder b64in(ifs); - std::ostringstream ofs; - Poco::StreamCopier::copyStream(b64in, ofs); - - int factor = 20; - unsigned long MaxSize = compress_sz ? (unsigned long) (compress_sz + 5000) : (unsigned long) (ofs.str().size() * factor); - while(true) { - std::vector UncompressedBuffer(MaxSize); - unsigned long FinalSize = MaxSize; - auto status = uncompress((uint8_t *)&UncompressedBuffer[0], &FinalSize, - (uint8_t *)ofs.str().c_str(), ofs.str().size()); - if(status==Z_OK) { - UncompressedBuffer[FinalSize] = 0; - UnCompressedData = (char *)&UncompressedBuffer[0]; - return true; - } - if(status==Z_BUF_ERROR) { - if(factor<300) { - factor+=10; - MaxSize = ofs.str().size() * factor; - continue; - } else { - return false; - } + return false; } return false; } - return false; -} - bool IsAlphaNumeric(const std::string &s) { - return std::all_of(s.begin(),s.end(),[](char c) -> bool { return isalnum(c); }); - } + bool IsAlphaNumeric(const std::string &s) { + return std::all_of(s.begin(), s.end(), [](char c) -> bool { return isalnum(c); }); + } - std::string SanitizeToken(const std::string &Token) { - if(Token.size()>8) { - return Token.substr(0,4) + "****" + Token.substr(Token.size()-4,4); - } - return "*******"; - } + std::string SanitizeToken(const std::string &Token) { + if (Token.size() > 8) { + return Token.substr(0, 4) + "****" + Token.substr(Token.size() - 4, 4); + } + return "*******"; + } [[nodiscard]] bool ValidateURI(const std::string &uri) { try { - Poco::URI u(uri); + Poco::URI u(uri); return true; } catch (...) { - } return false; } - [[nodiscard]] std::uint64_t ConvertDate(const std::string &Date) { - Poco::DateTime DT; - int TZ; - Poco::DateTimeParser::parse(Poco::DateTimeFormat::ISO8601_FORMAT,Date,DT,TZ); - return DT.timestamp().epochTime(); - } + [[nodiscard]] std::uint64_t ConvertDate(const std::string &Date) { + Poco::DateTime DT; + int TZ; + Poco::DateTimeParser::parse(Poco::DateTimeFormat::ISO8601_FORMAT, Date, DT, TZ); + return DT.timestamp().epochTime(); + } -} +} // namespace OpenWifi::Utils diff --git a/src/framework/utils.h b/src/framework/utils.h index 7a8255a..698912f 100644 --- a/src/framework/utils.h +++ b/src/framework/utils.h @@ -4,145 +4,140 @@ #pragma once -#include #include -#include #include +#include #include #include -#include #include +#include +#include -#include "Poco/Thread.h" -#include "Poco/StringTokenizer.h" -#include "Poco/String.h" -#include "Poco/SHA2Engine.h" -#include "Poco/Message.h" -#include "Poco/StreamCopier.h" -#include "Poco/File.h" -#include "Poco/Net/NetworkInterface.h" -#include "Poco/URI.h" -#include "Poco/Net/HTTPRequest.h" -#include "Poco/Net/HTTPClientSession.h" -#include "Poco/Net/HTTPSClientSession.h" -#include "Poco/Net/HTTPResponse.h" #include "Poco/Base64Decoder.h" #include "Poco/Base64Encoder.h" +#include "Poco/File.h" +#include "Poco/Message.h" +#include "Poco/Net/HTTPClientSession.h" +#include "Poco/Net/HTTPRequest.h" +#include "Poco/Net/HTTPResponse.h" +#include "Poco/Net/HTTPSClientSession.h" +#include "Poco/Net/NetworkInterface.h" +#include "Poco/SHA2Engine.h" +#include "Poco/StreamCopier.h" +#include "Poco/String.h" +#include "Poco/StringTokenizer.h" +#include "Poco/Thread.h" +#include "Poco/URI.h" #include "Poco/zlib.h" -#include "framework/ow_constants.h" #include "framework/OpenWifiTypes.h" +#include "framework/ow_constants.h" namespace OpenWifi::Utils { inline uint64_t Now() { return std::time(nullptr); }; - bool NormalizeMac(std::string & Mac); + bool NormalizeMac(std::string &Mac); inline void SetThreadName(const char *name) { - #ifdef __linux__ +#ifdef __linux__ Poco::Thread::current()->setName(name); pthread_setname_np(pthread_self(), name); - #endif - #ifdef __APPLE__ +#endif +#ifdef __APPLE__ Poco::Thread::current()->setName(name); pthread_setname_np(name); - #endif +#endif } inline void SetThreadName(Poco::Thread &thr, const char *name) { - #ifdef __linux__ +#ifdef __linux__ thr.setName(name); pthread_setname_np(thr.tid(), name); - #endif - #ifdef __APPLE__ +#endif +#ifdef __APPLE__ thr.setName(name); - #endif +#endif } - enum MediaTypeEncodings { - PLAIN, - BINARY, - BASE64 - }; + enum MediaTypeEncodings { PLAIN, BINARY, BASE64 }; struct MediaTypeEncoding { - MediaTypeEncodings Encoding=PLAIN; - std::string ContentType; + MediaTypeEncodings Encoding = PLAIN; + std::string ContentType; }; [[nodiscard]] bool ValidSerialNumber(const std::string &Serial); [[nodiscard]] bool ValidUUID(const std::string &UUID); - [[nodiscard]] bool ValidHostname(const std::string &hostname); + [[nodiscard]] bool ValidHostname(const std::string &hostname); - template std::string ComputeHash(Args&&... args) { - Poco::SHA2Engine E; + template std::string ComputeHash(Args &&...args) { + Poco::SHA2Engine E; auto as_string = [](auto p) { - if constexpr(std::is_arithmetic_v) { + if constexpr (std::is_arithmetic_v) { return std::to_string(p); } else { return p; } }; - (E.update(as_string(args)),...); + (E.update(as_string(args)), ...); return Poco::SHA2Engine::digestToHex(E.digest()); } - [[nodiscard]] std::vector Split(const std::string &List, char Delimiter=',' ); - [[nodiscard]] std::string FormatIPv6(const std::string & I ); - void padTo(std::string& str, size_t num, char paddingChar = '\0'); + [[nodiscard]] std::vector Split(const std::string &List, char Delimiter = ','); + [[nodiscard]] std::string FormatIPv6(const std::string &I); + void padTo(std::string &str, size_t num, char paddingChar = '\0'); [[nodiscard]] std::string SerialToMAC(const std::string &Serial); uint64_t MACToInt(const std::string &MAC); - [[nodiscard]] std::string ToHex(const std::vector & B); + [[nodiscard]] std::string ToHex(const std::vector &B); using byte = std::uint8_t; [[nodiscard]] std::string base64encode(const byte *input, uint32_t size); - [[nodiscard]] std::vector base64decode(const std::string& input);; - bool ParseTime(const std::string &Time, int & Hours, int & Minutes, int & Seconds); - bool ParseDate(const std::string &Time, int & Year, int & Month, int & Day); - bool CompareTime( int H1, int H2, int M1, int M2, int S1, int S2); + [[nodiscard]] std::vector base64decode(const std::string &input); + ; + bool ParseTime(const std::string &Time, int &Hours, int &Minutes, int &Seconds); + bool ParseDate(const std::string &Time, int &Year, int &Month, int &Day); + bool CompareTime(int H1, int H2, int M1, int M2, int S1, int S2); [[nodiscard]] std::string LogLevelToString(int Level); - [[nodiscard]] uint64_t SerialNumberToInt(const std::string & S); + [[nodiscard]] uint64_t SerialNumberToInt(const std::string &S); [[nodiscard]] std::string IntToSerialNumber(uint64_t S); - [[nodiscard]] bool SerialNumberMatch(const std::string &S1, const std::string &S2, int Bits=2); - [[nodiscard]] uint64_t SerialNumberToOUI(const std::string & S); + [[nodiscard]] bool SerialNumberMatch(const std::string &S1, const std::string &S2, + int Bits = 2); + [[nodiscard]] uint64_t SerialNumberToOUI(const std::string &S); [[nodiscard]] uint64_t GetDefaultMacAsInt64(); [[nodiscard]] uint64_t InitializeSystemId(); [[nodiscard]] uint64_t GetSystemId(); [[nodiscard]] bool ValidEMailAddress(const std::string &email); - [[nodiscard]] std::string LoadFile( const Poco::File & F); - void ReplaceVariables( std::string & Content , const Types::StringPairVec & P); + [[nodiscard]] std::string LoadFile(const Poco::File &F); + void ReplaceVariables(std::string &Content, const Types::StringPairVec &P); [[nodiscard]] MediaTypeEncoding FindMediaType(const Poco::File &F); [[nodiscard]] std::string BinaryFileToHexString(const Poco::File &F); [[nodiscard]] std::string SecondsToNiceText(uint64_t Seconds); [[nodiscard]] bool wgets(const std::string &URL, std::string &Response); [[nodiscard]] bool wgetfile(const Poco::URI &uri, const std::string &FileName); [[nodiscard]] bool IsAlphaNumeric(const std::string &s); - [[nodiscard]] std::string SanitizeToken(const std::string &Token); + [[nodiscard]] std::string SanitizeToken(const std::string &Token); [[nodiscard]] bool ValidateURI(const std::string &uri); - [[nodiscard]] std::uint64_t ConvertDate(const std::string &d); + [[nodiscard]] std::uint64_t ConvertDate(const std::string &d); - template< typename T > - std::string int_to_hex( T i ) - { + template std::string int_to_hex(T i) { std::stringstream stream; - stream << std::setfill ('0') << std::setw(12) - << std::hex << i; + stream << std::setfill('0') << std::setw(12) << std::hex << i; return stream.str(); } - inline bool SpinLock_Read(std::shared_mutex &M, volatile bool &Flag, uint64_t wait_ms=100) { - while(!M.try_lock_shared() && Flag) { + inline bool SpinLock_Read(std::shared_mutex &M, volatile bool &Flag, uint64_t wait_ms = 100) { + while (!M.try_lock_shared() && Flag) { Poco::Thread::yield(); Poco::Thread::trySleep((long)wait_ms); } return Flag; } - inline bool SpinLock_Write(std::shared_mutex &M, volatile bool &Flag, uint64_t wait_ms=100) { - while(!M.try_lock() && Flag) { + inline bool SpinLock_Write(std::shared_mutex &M, volatile bool &Flag, uint64_t wait_ms = 100) { + while (!M.try_lock() && Flag) { Poco::Thread::yield(); Poco::Thread::trySleep(wait_ms); } @@ -150,5 +145,5 @@ namespace OpenWifi::Utils { } bool ExtractBase64CompressedData(const std::string &CompressedData, - std::string &UnCompressedData, uint64_t compress_sz ); -} + std::string &UnCompressedData, uint64_t compress_sz); +} // namespace OpenWifi::Utils diff --git a/src/sdks/SDK_fms.cpp b/src/sdks/SDK_fms.cpp index e802be2..8ad605e 100644 --- a/src/sdks/SDK_fms.cpp +++ b/src/sdks/SDK_fms.cpp @@ -3,25 +3,25 @@ // #include "SDK_fms.h" -#include "framework/OpenAPIRequests.h" #include "framework/MicroServiceNames.h" +#include "framework/OpenAPIRequests.h" namespace OpenWifi::SDK::FMS { - namespace Firmware { - bool GetDeviceInformation(RESTAPIHandler *client, const Types::UUID_t & SerialNumber, FMSObjects::DeviceInformation & DI) { - OpenAPIRequestGet Req( uSERVICE_FIRMWARE, - "/api/v1/deviceInformation/" + SerialNumber, - {}, - 10000); + namespace Firmware { + bool GetDeviceInformation(RESTAPIHandler *client, const Types::UUID_t &SerialNumber, + FMSObjects::DeviceInformation &DI) { + OpenAPIRequestGet Req(uSERVICE_FIRMWARE, "/api/v1/deviceInformation/" + SerialNumber, + {}, 10000); - Poco::JSON::Object::Ptr Response; - auto StatusCode = Req.Do(Response,client== nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if( StatusCode == Poco::Net::HTTPResponse::HTTP_OK) { - return DI.from_json(Response); - } - return false; - } - } + Poco::JSON::Object::Ptr Response; + auto StatusCode = + Req.Do(Response, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (StatusCode == Poco::Net::HTTPResponse::HTTP_OK) { + return DI.from_json(Response); + } + return false; + } + } // namespace Firmware -} \ No newline at end of file +} // namespace OpenWifi::SDK::FMS \ No newline at end of file diff --git a/src/sdks/SDK_fms.h b/src/sdks/SDK_fms.h index b0f5080..14d9b72 100644 --- a/src/sdks/SDK_fms.h +++ b/src/sdks/SDK_fms.h @@ -4,14 +4,14 @@ #pragma once -#include "framework/RESTAPI_Handler.h" #include "RESTObjects/RESTAPI_FMSObjects.h" +#include "framework/RESTAPI_Handler.h" namespace OpenWifi::SDK::FMS { - namespace Firmware { - bool GetDeviceInformation(RESTAPIHandler *client, const Types::UUID_t & SerialNumber, FMSObjects::DeviceInformation & DI); - } - -} + namespace Firmware { + bool GetDeviceInformation(RESTAPIHandler *client, const Types::UUID_t &SerialNumber, + FMSObjects::DeviceInformation &DI); + } +} // namespace OpenWifi::SDK::FMS diff --git a/src/sdks/SDK_gw.cpp b/src/sdks/SDK_gw.cpp index 5ba479e..8687c99 100644 --- a/src/sdks/SDK_gw.cpp +++ b/src/sdks/SDK_gw.cpp @@ -3,237 +3,242 @@ // #include "SDK_gw.h" -#include "framework/OpenAPIRequests.h" #include "framework/MicroServiceNames.h" +#include "framework/OpenAPIRequests.h" #include "framework/utils.h" namespace OpenWifi::SDK::GW { - namespace Device { - void Reboot(RESTAPIHandler *client, const std::string & Mac, [[maybe_unused]] uint64_t When) { - std::string EndPoint = "/api/v1/device/" + Mac + "/reboot"; - Poco::JSON::Object ObjRequest; + namespace Device { + void Reboot(RESTAPIHandler *client, const std::string &Mac, + [[maybe_unused]] uint64_t When) { + std::string EndPoint = "/api/v1/device/" + Mac + "/reboot"; + Poco::JSON::Object ObjRequest; - ObjRequest.set("serialNumber", Mac); - ObjRequest.set("when",0); - PerformCommand(client,"reboot",EndPoint, ObjRequest); - } + ObjRequest.set("serialNumber", Mac); + ObjRequest.set("when", 0); + PerformCommand(client, "reboot", EndPoint, ObjRequest); + } - void LEDs(RESTAPIHandler *client, const std::string & Mac, uint64_t When, uint64_t Duration, const std::string & Pattern) { - std::string EndPoint = "/api/v1/device/" + Mac + "/leds"; - Poco::JSON::Object ObjRequest; + void LEDs(RESTAPIHandler *client, const std::string &Mac, uint64_t When, uint64_t Duration, + const std::string &Pattern) { + std::string EndPoint = "/api/v1/device/" + Mac + "/leds"; + Poco::JSON::Object ObjRequest; - ObjRequest.set("serialNumber", Mac); - ObjRequest.set("when",When); - ObjRequest.set("duration",Duration); - ObjRequest.set("pattern", Pattern); - PerformCommand(client,"leds",EndPoint, ObjRequest); - } + ObjRequest.set("serialNumber", Mac); + ObjRequest.set("when", When); + ObjRequest.set("duration", Duration); + ObjRequest.set("pattern", Pattern); + PerformCommand(client, "leds", EndPoint, ObjRequest); + } - void Factory(RESTAPIHandler *client, const std::string & Mac, uint64_t When, bool KeepRedirector) { - std::string EndPoint = "/api/v1/device/" + Mac + "/factory"; - Poco::JSON::Object ObjRequest; + void Factory(RESTAPIHandler *client, const std::string &Mac, uint64_t When, + bool KeepRedirector) { + std::string EndPoint = "/api/v1/device/" + Mac + "/factory"; + Poco::JSON::Object ObjRequest; - ObjRequest.set("serialNumber", Mac); - ObjRequest.set("when",When); - ObjRequest.set("keepRedirector",KeepRedirector); - PerformCommand(client,"factory",EndPoint, ObjRequest); - } + ObjRequest.set("serialNumber", Mac); + ObjRequest.set("when", When); + ObjRequest.set("keepRedirector", KeepRedirector); + PerformCommand(client, "factory", EndPoint, ObjRequest); + } - void Upgrade(RESTAPIHandler *client, const std::string & Mac, uint64_t When, const std::string & ImageName, [[maybe_unused]] bool KeepRedirector) { - std::string EndPoint = "/api/v1/device/" + Mac + "/upgrade"; - Poco::JSON::Object ObjRequest; + void Upgrade(RESTAPIHandler *client, const std::string &Mac, uint64_t When, + const std::string &ImageName, [[maybe_unused]] bool KeepRedirector) { + std::string EndPoint = "/api/v1/device/" + Mac + "/upgrade"; + Poco::JSON::Object ObjRequest; - ObjRequest.set("serialNumber", Mac); - ObjRequest.set("when",When); - ObjRequest.set("uri",ImageName); - PerformCommand(client,"upgrade",EndPoint, ObjRequest); - } + ObjRequest.set("serialNumber", Mac); + ObjRequest.set("when", When); + ObjRequest.set("uri", ImageName); + PerformCommand(client, "upgrade", EndPoint, ObjRequest); + } - void Refresh(RESTAPIHandler *client, const std::string & Mac, uint64_t When) { - std::string EndPoint = "/api/v1/device/" + Mac + "/refresh"; - Poco::JSON::Object ObjRequest; + void Refresh(RESTAPIHandler *client, const std::string &Mac, uint64_t When) { + std::string EndPoint = "/api/v1/device/" + Mac + "/refresh"; + Poco::JSON::Object ObjRequest; - ObjRequest.set("serialNumber", Mac); - ObjRequest.set("when",When); - PerformCommand(client,"refresh",EndPoint, ObjRequest); - } + ObjRequest.set("serialNumber", Mac); + ObjRequest.set("when", When); + PerformCommand(client, "refresh", EndPoint, ObjRequest); + } - void PerformCommand(RESTAPIHandler *client, const std::string &Command, const std::string & EndPoint, Poco::JSON::Object & CommandRequest) { - auto API = OpenAPIRequestPost(uSERVICE_GATEWAY, EndPoint, {}, CommandRequest, 60000); - Poco::JSON::Object::Ptr CallResponse; + void PerformCommand(RESTAPIHandler *client, const std::string &Command, + const std::string &EndPoint, Poco::JSON::Object &CommandRequest) { + auto API = OpenAPIRequestPost(uSERVICE_GATEWAY, EndPoint, {}, CommandRequest, 60000); + Poco::JSON::Object::Ptr CallResponse; - auto ResponseStatus = API.Do(CallResponse, client->UserInfo_.webtoken.access_token_); - if (ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT) { - Poco::JSON::Object ResponseObject; - ResponseObject.set("Code", Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT); - ResponseObject.set("Details", - "Command could not complete, you may want to retry this operation later."); - ResponseObject.set("Operation", Command); - client->Response->setStatus(ResponseStatus); - std::stringstream SS; - Poco::JSON::Stringifier::condense(ResponseObject, SS); - client->Response->setContentLength(SS.str().size()); - client->Response->setContentType("application/json"); - auto &os = client->Response->send(); - os << SS.str(); - } else { - client->Response->setStatus(ResponseStatus); - std::stringstream SS; - Poco::JSON::Stringifier::condense(CallResponse, SS); - Poco::JSON::Parser P; - auto Raw = P.parse(SS.str()).extract(); - if (Raw->has("command") && Raw->has("errorCode") && Raw->has("errorText")) { - Poco::JSON::Object ReturnResponse; - ReturnResponse.set("Operation", Raw->get("command").toString()); - ReturnResponse.set("Details", Raw->get("errorText").toString()); - ReturnResponse.set("Code", Raw->get("errorCode")); + auto ResponseStatus = API.Do(CallResponse, client->UserInfo_.webtoken.access_token_); + if (ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT) { + Poco::JSON::Object ResponseObject; + ResponseObject.set("Code", Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT); + ResponseObject.set( + "Details", + "Command could not complete, you may want to retry this operation later."); + ResponseObject.set("Operation", Command); + client->Response->setStatus(ResponseStatus); + std::stringstream SS; + Poco::JSON::Stringifier::condense(ResponseObject, SS); + client->Response->setContentLength(SS.str().size()); + client->Response->setContentType("application/json"); + auto &os = client->Response->send(); + os << SS.str(); + } else { + client->Response->setStatus(ResponseStatus); + std::stringstream SS; + Poco::JSON::Stringifier::condense(CallResponse, SS); + Poco::JSON::Parser P; + auto Raw = P.parse(SS.str()).extract(); + if (Raw->has("command") && Raw->has("errorCode") && Raw->has("errorText")) { + Poco::JSON::Object ReturnResponse; + ReturnResponse.set("Operation", Raw->get("command").toString()); + ReturnResponse.set("Details", Raw->get("errorText").toString()); + ReturnResponse.set("Code", Raw->get("errorCode")); - std::stringstream Ret; - Poco::JSON::Stringifier::condense(ReturnResponse, Ret); - client->Response->setContentLength(Ret.str().size()); - client->Response->setContentType("application/json"); - auto &os = client->Response->send(); - os << Ret.str(); - } - } - } + std::stringstream Ret; + Poco::JSON::Stringifier::condense(ReturnResponse, Ret); + client->Response->setContentLength(Ret.str().size()); + client->Response->setContentType("application/json"); + auto &os = client->Response->send(); + os << Ret.str(); + } + } + } - bool SetVenue(RESTAPIHandler *client, const std::string & SerialNumber, const std::string &uuid) { - Poco::JSON::Object Body; + bool SetVenue(RESTAPIHandler *client, const std::string &SerialNumber, + const std::string &uuid) { + Poco::JSON::Object Body; - Body.set("serialNumber", SerialNumber); - Body.set("venue", uuid); - OpenWifi::OpenAPIRequestPut R(OpenWifi::uSERVICE_GATEWAY, - "/api/v1/device/" +SerialNumber, - {}, - Body, - 10000); - Poco::JSON::Object::Ptr Response; - auto ResponseStatus = R.Do(Response, client ? client->UserInfo_.webtoken.access_token_ : ""); - if(ResponseStatus == Poco::Net::HTTPResponse::HTTP_OK) { - return true; - } - return false; - } + Body.set("serialNumber", SerialNumber); + Body.set("venue", uuid); + OpenWifi::OpenAPIRequestPut R(OpenWifi::uSERVICE_GATEWAY, + "/api/v1/device/" + SerialNumber, {}, Body, 10000); + Poco::JSON::Object::Ptr Response; + auto ResponseStatus = + R.Do(Response, client ? client->UserInfo_.webtoken.access_token_ : ""); + if (ResponseStatus == Poco::Net::HTTPResponse::HTTP_OK) { + return true; + } + return false; + } - bool GetLastStats(RESTAPIHandler *client, const std::string &Mac, Poco::JSON::Object::Ptr & Response) { - // "https://${OWGW}/api/v1/device/$1/statistics?lastOnly=true" - std::string EndPoint = "/api/v1/device/" + Mac + "/statistics"; - auto API = OpenAPIRequestGet(uSERVICE_GATEWAY, EndPoint, { { "lastOnly" , "true" }}, 60000); - auto ResponseStatus = API.Do(Response, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { - try { - return true; - } catch (...) { - return false; - } - } - return false; - } + bool GetLastStats(RESTAPIHandler *client, const std::string &Mac, + Poco::JSON::Object::Ptr &Response) { + // "https://${OWGW}/api/v1/device/$1/statistics?lastOnly=true" + std::string EndPoint = "/api/v1/device/" + Mac + "/statistics"; + auto API = OpenAPIRequestGet(uSERVICE_GATEWAY, EndPoint, {{"lastOnly", "true"}}, 60000); + auto ResponseStatus = + API.Do(Response, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { + try { + return true; + } catch (...) { + return false; + } + } + return false; + } - bool Configure(RESTAPIHandler *client, const std::string &Mac, Poco::JSON::Object::Ptr & Configuration, Poco::JSON::Object::Ptr & Response) { + bool Configure(RESTAPIHandler *client, const std::string &Mac, + Poco::JSON::Object::Ptr &Configuration, Poco::JSON::Object::Ptr &Response) { - Poco::JSON::Object Body; + Poco::JSON::Object Body; - Poco::JSON::Parser P; - uint64_t Now = Utils::Now(); + Poco::JSON::Parser P; + uint64_t Now = Utils::Now(); - Configuration->set("uuid", Now); - Body.set("serialNumber", Mac); - Body.set("UUID", Now); - Body.set("when",0); - Body.set("configuration", Configuration); + Configuration->set("uuid", Now); + Body.set("serialNumber", Mac); + Body.set("UUID", Now); + Body.set("when", 0); + Body.set("configuration", Configuration); - OpenWifi::OpenAPIRequestPost R(OpenWifi::uSERVICE_GATEWAY, - "/api/v1/device/" + Mac + "/configure", - {}, - Body, - 10000); + OpenWifi::OpenAPIRequestPost R(OpenWifi::uSERVICE_GATEWAY, + "/api/v1/device/" + Mac + "/configure", {}, Body, 10000); - auto ResponseStatus = R.Do(Response, client ? client->UserInfo_.webtoken.access_token_ : ""); - if(ResponseStatus == Poco::Net::HTTPResponse::HTTP_OK) { - std::ostringstream os; - Poco::JSON::Stringifier::stringify(Response,os); - return true; - } + auto ResponseStatus = + R.Do(Response, client ? client->UserInfo_.webtoken.access_token_ : ""); + if (ResponseStatus == Poco::Net::HTTPResponse::HTTP_OK) { + std::ostringstream os; + Poco::JSON::Stringifier::stringify(Response, os); + return true; + } - return false; - } + return false; + } - bool SetSubscriber(RESTAPIHandler *client, const std::string & SerialNumber, const std::string &uuid) { - Poco::JSON::Object Body; + bool SetSubscriber(RESTAPIHandler *client, const std::string &SerialNumber, + const std::string &uuid) { + Poco::JSON::Object Body; - Body.set("serialNumber", SerialNumber); - Body.set("subscriber", uuid); - OpenWifi::OpenAPIRequestPut R(OpenWifi::uSERVICE_GATEWAY, - "/api/v1/device/" +SerialNumber, - {}, - Body, - 10000); - auto CallResponse = Poco::makeShared(); - auto ResponseStatus = R.Do(CallResponse, client ? client->UserInfo_.webtoken.access_token_ : ""); - if(ResponseStatus == Poco::Net::HTTPResponse::HTTP_OK) { - return true; - } - return false; - } + Body.set("serialNumber", SerialNumber); + Body.set("subscriber", uuid); + OpenWifi::OpenAPIRequestPut R(OpenWifi::uSERVICE_GATEWAY, + "/api/v1/device/" + SerialNumber, {}, Body, 10000); + auto CallResponse = Poco::makeShared(); + auto ResponseStatus = + R.Do(CallResponse, client ? client->UserInfo_.webtoken.access_token_ : ""); + if (ResponseStatus == Poco::Net::HTTPResponse::HTTP_OK) { + return true; + } + return false; + } - struct Tag { - std::string tag, value; - bool from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - OpenWifi::RESTAPI_utils::field_from_json(Obj, "tag", tag); - OpenWifi::RESTAPI_utils::field_from_json(Obj, "value", value); - return true; - } catch (...) { - } - return false; - } - }; + struct Tag { + std::string tag, value; + bool from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + OpenWifi::RESTAPI_utils::field_from_json(Obj, "tag", tag); + OpenWifi::RESTAPI_utils::field_from_json(Obj, "value", value); + return true; + } catch (...) { + } + return false; + } + }; - struct TagList { - std::vector tagList; - bool from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - OpenWifi::RESTAPI_utils::field_from_json(Obj, "tagList", tagList); - return true; - } catch (...) { - } - return false; - } - }; + struct TagList { + std::vector tagList; + bool from_json(const Poco::JSON::Object::Ptr &Obj) { + try { + OpenWifi::RESTAPI_utils::field_from_json(Obj, "tagList", tagList); + return true; + } catch (...) { + } + return false; + } + }; - bool GetOUIs(RESTAPIHandler *client, Types::StringPairVec &MacListPair) { - std::string EndPoint = "/api/v1/ouis"; + bool GetOUIs(RESTAPIHandler *client, Types::StringPairVec &MacListPair) { + std::string EndPoint = "/api/v1/ouis"; - std::string MacList; - for(const auto &i:MacListPair) { - if(MacList.empty()) - MacList = i.first; - else - MacList += "," + i.first; - } + std::string MacList; + for (const auto &i : MacListPair) { + if (MacList.empty()) + MacList = i.first; + else + MacList += "," + i.first; + } - auto API = OpenAPIRequestGet(uSERVICE_GATEWAY, EndPoint, { { "macList" , MacList }}, 60000); - Poco::JSON::Object::Ptr Response; - auto ResponseStatus = API.Do(Response, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { - try { - TagList TL; - TL.from_json(Response); - for(const auto &i:TL.tagList) { - for(auto &j:MacListPair) - if(j.first==i.tag) - j.second=i.value; - } - return true; - } catch (...) { - return false; - } - } - return false; + auto API = OpenAPIRequestGet(uSERVICE_GATEWAY, EndPoint, {{"macList", MacList}}, 60000); + Poco::JSON::Object::Ptr Response; + auto ResponseStatus = + API.Do(Response, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { + try { + TagList TL; + TL.from_json(Response); + for (const auto &i : TL.tagList) { + for (auto &j : MacListPair) + if (j.first == i.tag) + j.second = i.value; + } + return true; + } catch (...) { + return false; + } + } + return false; + } - } - - } -} + } // namespace Device +} // namespace OpenWifi::SDK::GW diff --git a/src/sdks/SDK_gw.h b/src/sdks/SDK_gw.h index a696da6..9c10f48 100644 --- a/src/sdks/SDK_gw.h +++ b/src/sdks/SDK_gw.h @@ -4,23 +4,30 @@ #pragma once -#include "framework/RESTAPI_Handler.h" #include "RESTObjects/RESTAPI_GWobjects.h" +#include "framework/RESTAPI_Handler.h" namespace OpenWifi::SDK::GW { - namespace Device { - void Reboot(RESTAPIHandler *client, const std::string & Mac, uint64_t When); - void LEDs(RESTAPIHandler *client, const std::string & Mac, uint64_t When, uint64_t Duration, const std::string & Pattern); - void Factory(RESTAPIHandler *client, const std::string & Mac, uint64_t When, bool KeepRedirector); - void Upgrade(RESTAPIHandler *client, const std::string & Mac, uint64_t When, const std::string & ImageName, bool KeepRedirector); - void Refresh(RESTAPIHandler *client, const std::string & Mac, uint64_t When); - void PerformCommand(RESTAPIHandler *client, const std::string &Command, const std::string & EndPoint, Poco::JSON::Object & CommandRequest); - bool Configure(RESTAPIHandler *client, const std::string &Mac, Poco::JSON::Object::Ptr & Configuration, Poco::JSON::Object::Ptr & Response); - - bool SetVenue(RESTAPIHandler *client, const std::string & SerialNumber, const std::string &uuid); - bool GetLastStats(RESTAPIHandler *client, const std::string &Mac, Poco::JSON::Object::Ptr & Response); - bool SetSubscriber(RESTAPIHandler *client, const std::string & SerialNumber, const std::string &uuid); - bool GetOUIs(RESTAPIHandler *client, Types::StringPairVec & MacList); - } -} + namespace Device { + void Reboot(RESTAPIHandler *client, const std::string &Mac, uint64_t When); + void LEDs(RESTAPIHandler *client, const std::string &Mac, uint64_t When, uint64_t Duration, + const std::string &Pattern); + void Factory(RESTAPIHandler *client, const std::string &Mac, uint64_t When, + bool KeepRedirector); + void Upgrade(RESTAPIHandler *client, const std::string &Mac, uint64_t When, + const std::string &ImageName, bool KeepRedirector); + void Refresh(RESTAPIHandler *client, const std::string &Mac, uint64_t When); + void PerformCommand(RESTAPIHandler *client, const std::string &Command, + const std::string &EndPoint, Poco::JSON::Object &CommandRequest); + bool Configure(RESTAPIHandler *client, const std::string &Mac, + Poco::JSON::Object::Ptr &Configuration, Poco::JSON::Object::Ptr &Response); + bool SetVenue(RESTAPIHandler *client, const std::string &SerialNumber, + const std::string &uuid); + bool GetLastStats(RESTAPIHandler *client, const std::string &Mac, + Poco::JSON::Object::Ptr &Response); + bool SetSubscriber(RESTAPIHandler *client, const std::string &SerialNumber, + const std::string &uuid); + bool GetOUIs(RESTAPIHandler *client, Types::StringPairVec &MacList); + } // namespace Device +} // namespace OpenWifi::SDK::GW diff --git a/src/sdks/SDK_prov.cpp b/src/sdks/SDK_prov.cpp index 7436fa5..882b317 100644 --- a/src/sdks/SDK_prov.cpp +++ b/src/sdks/SDK_prov.cpp @@ -3,188 +3,203 @@ // #include "SDK_prov.h" -#include "framework/OpenAPIRequests.h" #include "framework/MicroServiceNames.h" - +#include "framework/OpenAPIRequests.h" namespace OpenWifi::SDK::Prov { - namespace Device { - bool Get(RESTAPIHandler *client, const std::string &Mac, ProvObjects::InventoryTag & Device) { - std::string EndPoint = "/api/v1/inventory/" + Mac ; + namespace Device { + bool Get(RESTAPIHandler *client, const std::string &Mac, + ProvObjects::InventoryTag &Device) { + std::string EndPoint = "/api/v1/inventory/" + Mac; - auto API = OpenAPIRequestGet(uSERVICE_PROVISIONING, EndPoint, {}, 60000); - auto CallResponse = Poco::makeShared(); + auto API = OpenAPIRequestGet(uSERVICE_PROVISIONING, EndPoint, {}, 60000); + auto CallResponse = Poco::makeShared(); - auto ResponseStatus = API.Do(CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { - try { - return Device.from_json(CallResponse); - } catch (...) { - return false; - } - } - return false; - } + auto ResponseStatus = API.Do( + CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { + try { + return Device.from_json(CallResponse); + } catch (...) { + return false; + } + } + return false; + } - } + } // namespace Device - namespace Configuration { - bool Get( RESTAPIHandler *client, const std::string &ConfigUUID, ProvObjects::DeviceConfiguration & Config) { - std::string EndPoint = "/api/v1/configuration/" + ConfigUUID ; - auto API = OpenAPIRequestGet(uSERVICE_PROVISIONING, EndPoint, {}, 60000); - auto CallResponse = Poco::makeShared(); - auto ResponseStatus = API.Do(CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { - try { - return Config.from_json(CallResponse); - } catch (...) { - return false; - } - } - return false; - } + namespace Configuration { + bool Get(RESTAPIHandler *client, const std::string &ConfigUUID, + ProvObjects::DeviceConfiguration &Config) { + std::string EndPoint = "/api/v1/configuration/" + ConfigUUID; + auto API = OpenAPIRequestGet(uSERVICE_PROVISIONING, EndPoint, {}, 60000); + auto CallResponse = Poco::makeShared(); + auto ResponseStatus = API.Do( + CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { + try { + return Config.from_json(CallResponse); + } catch (...) { + return false; + } + } + return false; + } - bool Delete( RESTAPIHandler *client, const std::string &ConfigUUID) { - std::string EndPoint = "/api/v1/configuration/" + ConfigUUID ; - auto API = OpenAPIRequestDelete(uSERVICE_PROVISIONING, EndPoint, {}, 60000); - auto ResponseStatus = API.Do(client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { - return true; - } - return false; - } + bool Delete(RESTAPIHandler *client, const std::string &ConfigUUID) { + std::string EndPoint = "/api/v1/configuration/" + ConfigUUID; + auto API = OpenAPIRequestDelete(uSERVICE_PROVISIONING, EndPoint, {}, 60000); + auto ResponseStatus = + API.Do(client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { + return true; + } + return false; + } - bool Create( RESTAPIHandler *client, const std::string & SerialNumber, const ProvObjects::DeviceConfiguration & Config , std::string & ConfigUUID) { - std::string EndPoint = "/api/v1/configuration/0" ; - Poco::JSON::Object Body; - Config.to_json(Body); + bool Create(RESTAPIHandler *client, const std::string &SerialNumber, + const ProvObjects::DeviceConfiguration &Config, std::string &ConfigUUID) { + std::string EndPoint = "/api/v1/configuration/0"; + Poco::JSON::Object Body; + Config.to_json(Body); - std::stringstream OOS; - Body.stringify(OOS); + std::stringstream OOS; + Body.stringify(OOS); - auto API = OpenAPIRequestPost(uSERVICE_PROVISIONING, EndPoint, {}, Body, 10000); - auto CallResponse = Poco::makeShared(); - auto ResponseStatus = API.Do(CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { - std::ostringstream OS; - CallResponse->stringify(OS); - // std::cout << "CREATE: " << OS.str() << std::endl; - return false; - } + auto API = OpenAPIRequestPost(uSERVICE_PROVISIONING, EndPoint, {}, Body, 10000); + auto CallResponse = Poco::makeShared(); + auto ResponseStatus = API.Do( + CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { + std::ostringstream OS; + CallResponse->stringify(OS); + // std::cout << "CREATE: " << OS.str() << std::endl; + return false; + } - ProvObjects::DeviceConfiguration NewConfig; - NewConfig.from_json(CallResponse); - ConfigUUID = NewConfig.info.id; + ProvObjects::DeviceConfiguration NewConfig; + NewConfig.from_json(CallResponse); + ConfigUUID = NewConfig.info.id; - Body.clear(); - Body.set("serialNumber", SerialNumber); - Body.set("deviceConfiguration", ConfigUUID); - EndPoint = "/api/v1/inventory/" + SerialNumber; - auto API2 = OpenAPIRequestPut(uSERVICE_PROVISIONING, EndPoint, {}, Body, 10000); - CallResponse->clear(); - ResponseStatus = API2.Do(CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { - std::ostringstream OS; - CallResponse->stringify(OS); - return false; - } - return true; - } + Body.clear(); + Body.set("serialNumber", SerialNumber); + Body.set("deviceConfiguration", ConfigUUID); + EndPoint = "/api/v1/inventory/" + SerialNumber; + auto API2 = OpenAPIRequestPut(uSERVICE_PROVISIONING, EndPoint, {}, Body, 10000); + CallResponse->clear(); + ResponseStatus = API2.Do( + CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { + std::ostringstream OS; + CallResponse->stringify(OS); + return false; + } + return true; + } - bool Update( RESTAPIHandler *client, const std::string &ConfigUUID, ProvObjects::DeviceConfiguration & Config) { - std::string EndPoint = "/api/v1/configuration/"+ConfigUUID ; - Poco::JSON::Object Body; - Config.to_json(Body); - auto API = OpenAPIRequestPut(uSERVICE_PROVISIONING, EndPoint, {}, Body, 10000); - auto CallResponse = Poco::makeShared(); - auto ResponseStatus = API.Do(CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { - return false; - } - return true; - } + bool Update(RESTAPIHandler *client, const std::string &ConfigUUID, + ProvObjects::DeviceConfiguration &Config) { + std::string EndPoint = "/api/v1/configuration/" + ConfigUUID; + Poco::JSON::Object Body; + Config.to_json(Body); + auto API = OpenAPIRequestPut(uSERVICE_PROVISIONING, EndPoint, {}, Body, 10000); + auto CallResponse = Poco::makeShared(); + auto ResponseStatus = API.Do( + CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { + return false; + } + return true; + } - bool Push( RESTAPIHandler *client, const std::string &serialNumber, ProvObjects::InventoryConfigApplyResult &Results ) { - std::string EndPoint = "/api/v1/inventory/"+serialNumber ; - Poco::JSON::Object Body; - auto API = OpenAPIRequestGet(uSERVICE_PROVISIONING, EndPoint, { - { "applyConfiguration", "true" } - }, 30000); + bool Push(RESTAPIHandler *client, const std::string &serialNumber, + ProvObjects::InventoryConfigApplyResult &Results) { + std::string EndPoint = "/api/v1/inventory/" + serialNumber; + Poco::JSON::Object Body; + auto API = OpenAPIRequestGet(uSERVICE_PROVISIONING, EndPoint, + {{"applyConfiguration", "true"}}, 30000); - auto CallResponse = Poco::makeShared(); - auto ResponseStatus = API.Do(CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus == Poco::Net::HTTPResponse::HTTP_OK) { - Results.from_json(CallResponse); - return true; - } + auto CallResponse = Poco::makeShared(); + auto ResponseStatus = API.Do( + CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus == Poco::Net::HTTPResponse::HTTP_OK) { + Results.from_json(CallResponse); + return true; + } - std::ostringstream OO; - CallResponse->stringify(OO); - return false; - } - } + std::ostringstream OO; + CallResponse->stringify(OO); + return false; + } + } // namespace Configuration - namespace Subscriber { - bool GetDevices(RESTAPIHandler *client, const std::string &SubscriberId, const std::string &OperatorId, ProvObjects::SubscriberDeviceList &devList) { + namespace Subscriber { + bool GetDevices(RESTAPIHandler *client, const std::string &SubscriberId, + const std::string &OperatorId, ProvObjects::SubscriberDeviceList &devList) { - std::string EndPoint = "/api/v1/subscriberDevice"; - auto API = OpenAPIRequestGet(uSERVICE_PROVISIONING, EndPoint, { - {"subscriberId", SubscriberId}, - {"operatorId", OperatorId} - }, 60000); - auto CallResponse = Poco::makeShared(); - auto ResponseStatus = API.Do(CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { - try { - return devList.from_json(CallResponse); - } catch (...) { - return false; - } - } - return false; - } + std::string EndPoint = "/api/v1/subscriberDevice"; + auto API = OpenAPIRequestGet( + uSERVICE_PROVISIONING, EndPoint, + {{"subscriberId", SubscriberId}, {"operatorId", OperatorId}}, 60000); + auto CallResponse = Poco::makeShared(); + auto ResponseStatus = API.Do( + CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_OK) { + try { + return devList.from_json(CallResponse); + } catch (...) { + return false; + } + } + return false; + } - bool ReturnDeviceToInventory(RESTAPIHandler *client, const std::string &SubscriberId, const std::string &SerialNumber) { - std::string EndPoint = "/api/v1/inventory/"+SerialNumber ; - Poco::JSON::Object Body; - Body.set("serialNumber",SerialNumber); - auto API = OpenAPIRequestPut(uSERVICE_PROVISIONING, EndPoint, { - { "removeSubscriber", SubscriberId} - }, Body, 20000); - auto CallResponse = Poco::makeShared(); - auto ResponseStatus = API.Do(CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { - // std::cout << "ReturnDeviceToInventory: " << ResponseStatus << std::endl; - return false; - } - return true; - } + bool ReturnDeviceToInventory(RESTAPIHandler *client, const std::string &SubscriberId, + const std::string &SerialNumber) { + std::string EndPoint = "/api/v1/inventory/" + SerialNumber; + Poco::JSON::Object Body; + Body.set("serialNumber", SerialNumber); + auto API = OpenAPIRequestPut(uSERVICE_PROVISIONING, EndPoint, + {{"removeSubscriber", SubscriberId}}, Body, 20000); + auto CallResponse = Poco::makeShared(); + auto ResponseStatus = API.Do( + CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { + // std::cout << "ReturnDeviceToInventory: " << ResponseStatus << std::endl; + return false; + } + return true; + } - bool SetDevice(RESTAPIHandler *client, const ProvObjects::SubscriberDevice &D) { - std::string EndPoint = "/api/v1/subscriberDevice/"+D.info.id ; - Poco::JSON::Object Body; - D.to_json(Body); - auto API = OpenAPIRequestPut(uSERVICE_PROVISIONING, EndPoint, {}, Body, 120000); - auto CallResponse = Poco::makeShared(); - auto ResponseStatus = API.Do(CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { - return false; - } - return true; - } + bool SetDevice(RESTAPIHandler *client, const ProvObjects::SubscriberDevice &D) { + std::string EndPoint = "/api/v1/subscriberDevice/" + D.info.id; + Poco::JSON::Object Body; + D.to_json(Body); + auto API = OpenAPIRequestPut(uSERVICE_PROVISIONING, EndPoint, {}, Body, 120000); + auto CallResponse = Poco::makeShared(); + auto ResponseStatus = API.Do( + CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { + return false; + } + return true; + } - bool GetDevice(RESTAPIHandler *client, const std::string &SerialNumber, ProvObjects::SubscriberDevice &D) { - std::string EndPoint = "/api/v1/subscriberDevice/"+SerialNumber ; - Poco::JSON::Object Body; - auto API = OpenAPIRequestGet(uSERVICE_PROVISIONING, EndPoint, {}, 60000); - auto CallResponse = Poco::makeShared(); - auto ResponseStatus = API.Do(CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if(ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { - return false; - } - return D.from_json(CallResponse); - } - } + bool GetDevice(RESTAPIHandler *client, const std::string &SerialNumber, + ProvObjects::SubscriberDevice &D) { + std::string EndPoint = "/api/v1/subscriberDevice/" + SerialNumber; + Poco::JSON::Object Body; + auto API = OpenAPIRequestGet(uSERVICE_PROVISIONING, EndPoint, {}, 60000); + auto CallResponse = Poco::makeShared(); + auto ResponseStatus = API.Do( + CallResponse, client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (ResponseStatus != Poco::Net::HTTPResponse::HTTP_OK) { + return false; + } + return D.from_json(CallResponse); + } + } // namespace Subscriber -} \ No newline at end of file +} // namespace OpenWifi::SDK::Prov \ No newline at end of file diff --git a/src/sdks/SDK_prov.h b/src/sdks/SDK_prov.h index ba30327..f4d7d67 100644 --- a/src/sdks/SDK_prov.h +++ b/src/sdks/SDK_prov.h @@ -4,29 +4,38 @@ #pragma once -#include "framework/RESTAPI_Handler.h" #include "RESTObjects/RESTAPI_ProvObjects.h" +#include "framework/RESTAPI_Handler.h" namespace OpenWifi::SDK::Prov { - namespace Device { - bool Get(RESTAPIHandler *client, const std::string &Mac, ProvObjects::InventoryTagList & Device); - bool SetConfiguration(RESTAPIHandler *client, const std::string &Mac, const std::string &ConfigUUID); - } + namespace Device { + bool Get(RESTAPIHandler *client, const std::string &Mac, + ProvObjects::InventoryTagList &Device); + bool SetConfiguration(RESTAPIHandler *client, const std::string &Mac, + const std::string &ConfigUUID); + } // namespace Device - namespace Configuration { - bool Get( RESTAPIHandler *client, const std::string &ConfigUUID, ProvObjects::DeviceConfiguration & Config); - bool Delete( RESTAPIHandler *client, const std::string &ConfigUUID); - bool Create( RESTAPIHandler *client, const std::string & SerialNumber, const ProvObjects::DeviceConfiguration & Config , std::string & ConfigUUID); - bool Update( RESTAPIHandler *client, const std::string &ConfigUUID, ProvObjects::DeviceConfiguration & Config); - bool Push( RESTAPIHandler *client, const std::string &serialNumber, ProvObjects::InventoryConfigApplyResult &Results ); - } + namespace Configuration { + bool Get(RESTAPIHandler *client, const std::string &ConfigUUID, + ProvObjects::DeviceConfiguration &Config); + bool Delete(RESTAPIHandler *client, const std::string &ConfigUUID); + bool Create(RESTAPIHandler *client, const std::string &SerialNumber, + const ProvObjects::DeviceConfiguration &Config, std::string &ConfigUUID); + bool Update(RESTAPIHandler *client, const std::string &ConfigUUID, + ProvObjects::DeviceConfiguration &Config); + bool Push(RESTAPIHandler *client, const std::string &serialNumber, + ProvObjects::InventoryConfigApplyResult &Results); + } // namespace Configuration - namespace Subscriber { - bool GetDevices(RESTAPIHandler *client, const std::string &SubscriberId, const std::string &OperatorId, ProvObjects::SubscriberDeviceList &devList); - bool ReturnDeviceToInventory(RESTAPIHandler *client, const std::string &SubscriberId, const std::string &SerialNumber); - bool SetDevice(RESTAPIHandler *client, const ProvObjects::SubscriberDevice &D); - bool GetDevice(RESTAPIHandler *client, const std::string &SerialNumber, ProvObjects::SubscriberDevice &D); - } + namespace Subscriber { + bool GetDevices(RESTAPIHandler *client, const std::string &SubscriberId, + const std::string &OperatorId, ProvObjects::SubscriberDeviceList &devList); + bool ReturnDeviceToInventory(RESTAPIHandler *client, const std::string &SubscriberId, + const std::string &SerialNumber); + bool SetDevice(RESTAPIHandler *client, const ProvObjects::SubscriberDevice &D); + bool GetDevice(RESTAPIHandler *client, const std::string &SerialNumber, + ProvObjects::SubscriberDevice &D); + } // namespace Subscriber -} \ No newline at end of file +} // namespace OpenWifi::SDK::Prov \ No newline at end of file diff --git a/src/sdks/SDK_sec.cpp b/src/sdks/SDK_sec.cpp index 9ef10e3..54e3b28 100644 --- a/src/sdks/SDK_sec.cpp +++ b/src/sdks/SDK_sec.cpp @@ -3,83 +3,71 @@ // #include "SDK_sec.h" -#include "framework/OpenAPIRequests.h" #include "framework/MicroServiceNames.h" +#include "framework/OpenAPIRequests.h" namespace OpenWifi::SDK::Sec { - namespace User { - bool Exists([[maybe_unused]] RESTAPIHandler *client, const Types::UUID_t & Id) { - OpenAPIRequestGet Req( uSERVICE_SECURITY, - "/api/v1/user/" + Id, - {}, - 5000); + namespace User { + bool Exists([[maybe_unused]] RESTAPIHandler *client, const Types::UUID_t &Id) { + OpenAPIRequestGet Req(uSERVICE_SECURITY, "/api/v1/user/" + Id, {}, 5000); - Poco::JSON::Object::Ptr Response; - auto StatusCode = Req.Do(Response); - if( StatusCode == Poco::Net::HTTPResponse::HTTP_OK) { - return true; - } - return false; - } + Poco::JSON::Object::Ptr Response; + auto StatusCode = Req.Do(Response); + if (StatusCode == Poco::Net::HTTPResponse::HTTP_OK) { + return true; + } + return false; + } - bool Get([[maybe_unused]] RESTAPIHandler *client, const Types::UUID_t & Id, SecurityObjects::UserInfo & UserInfo) { - OpenAPIRequestGet Req( uSERVICE_SECURITY, - "/api/v1/user/" + Id, - {}, - 5000); + bool Get([[maybe_unused]] RESTAPIHandler *client, const Types::UUID_t &Id, + SecurityObjects::UserInfo &UserInfo) { + OpenAPIRequestGet Req(uSERVICE_SECURITY, "/api/v1/user/" + Id, {}, 5000); - Poco::JSON::Object::Ptr Response; - auto StatusCode = Req.Do(Response); - if( StatusCode == Poco::Net::HTTPResponse::HTTP_OK) { - return UserInfo.from_json(Response); - } - return false; - } - } + Poco::JSON::Object::Ptr Response; + auto StatusCode = Req.Do(Response); + if (StatusCode == Poco::Net::HTTPResponse::HTTP_OK) { + return UserInfo.from_json(Response); + } + return false; + } + } // namespace User - namespace Subscriber { - bool Exists([[maybe_unused]] RESTAPIHandler *client, const Types::UUID_t & Id) { - OpenAPIRequestGet Req( uSERVICE_SECURITY, - "/api/v1/subuser/" + Id, - {}, - 5000); + namespace Subscriber { + bool Exists([[maybe_unused]] RESTAPIHandler *client, const Types::UUID_t &Id) { + OpenAPIRequestGet Req(uSERVICE_SECURITY, "/api/v1/subuser/" + Id, {}, 5000); - Poco::JSON::Object::Ptr Response; - auto StatusCode = Req.Do(Response); - if( StatusCode == Poco::Net::HTTPResponse::HTTP_OK) { - return true; - } - return false; - } + Poco::JSON::Object::Ptr Response; + auto StatusCode = Req.Do(Response); + if (StatusCode == Poco::Net::HTTPResponse::HTTP_OK) { + return true; + } + return false; + } - bool Get([[maybe_unused]] RESTAPIHandler *client, const Types::UUID_t & Id, SecurityObjects::UserInfo & UserInfo) { - OpenAPIRequestGet Req( uSERVICE_SECURITY, - "/api/v1/subuser/" + Id, - {}, - 5000); + bool Get([[maybe_unused]] RESTAPIHandler *client, const Types::UUID_t &Id, + SecurityObjects::UserInfo &UserInfo) { + OpenAPIRequestGet Req(uSERVICE_SECURITY, "/api/v1/subuser/" + Id, {}, 5000); - Poco::JSON::Object::Ptr Response; - auto StatusCode = Req.Do(Response); - if( StatusCode == Poco::Net::HTTPResponse::HTTP_OK) { - return UserInfo.from_json(Response); - } - return false; - } + Poco::JSON::Object::Ptr Response; + auto StatusCode = Req.Do(Response); + if (StatusCode == Poco::Net::HTTPResponse::HTTP_OK) { + return UserInfo.from_json(Response); + } + return false; + } - bool Delete(RESTAPIHandler *client, const Types::UUID_t & Id) { - OpenAPIRequestDelete Req( uSERVICE_SECURITY, - "/api/v1/subuser/" + Id, - {}, - 15000); + bool Delete(RESTAPIHandler *client, const Types::UUID_t &Id) { + OpenAPIRequestDelete Req(uSERVICE_SECURITY, "/api/v1/subuser/" + Id, {}, 15000); - auto StatusCode = Req.Do(client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); - if( StatusCode >=200 && StatusCode <= 204) { - return true; - } - return false; - } + auto StatusCode = + Req.Do(client == nullptr ? "" : client->UserInfo_.webtoken.access_token_); + if (StatusCode >= 200 && StatusCode <= 204) { + return true; + } + return false; + } - } + } // namespace Subscriber -} +} // namespace OpenWifi::SDK::Sec diff --git a/src/sdks/SDK_sec.h b/src/sdks/SDK_sec.h index 917f143..119879b 100644 --- a/src/sdks/SDK_sec.h +++ b/src/sdks/SDK_sec.h @@ -4,21 +4,22 @@ #pragma once -#include "framework/RESTAPI_Handler.h" #include "RESTObjects/RESTAPI_SecurityObjects.h" +#include "framework/RESTAPI_Handler.h" namespace OpenWifi::SDK::Sec { - namespace User { - bool Exists(RESTAPIHandler *client, const Types::UUID_t & User); - bool Get(RESTAPIHandler *client, const Types::UUID_t & User, SecurityObjects::UserInfo & UserInfo); - } + namespace User { + bool Exists(RESTAPIHandler *client, const Types::UUID_t &User); + bool Get(RESTAPIHandler *client, const Types::UUID_t &User, + SecurityObjects::UserInfo &UserInfo); + } // namespace User - namespace Subscriber { - bool Exists(RESTAPIHandler *client, const Types::UUID_t & User); - bool Get(RESTAPIHandler *client, const Types::UUID_t & User, SecurityObjects::UserInfo & UserInfo); - bool Delete(RESTAPIHandler *client, const Types::UUID_t & Id); - } - -} + namespace Subscriber { + bool Exists(RESTAPIHandler *client, const Types::UUID_t &User); + bool Get(RESTAPIHandler *client, const Types::UUID_t &User, + SecurityObjects::UserInfo &UserInfo); + bool Delete(RESTAPIHandler *client, const Types::UUID_t &Id); + } // namespace Subscriber +} // namespace OpenWifi::SDK::Sec diff --git a/src/storage/storage_subscriber_info.cpp b/src/storage/storage_subscriber_info.cpp index f8ae0ba..d15abf8 100644 --- a/src/storage/storage_subscriber_info.cpp +++ b/src/storage/storage_subscriber_info.cpp @@ -8,168 +8,172 @@ namespace OpenWifi { - static ORM::FieldVec SubInfoDBDB_Fields{ - // object info - ORM::Field{"id",64, true}, - ORM::Field{"userId",ORM::FieldType::FT_TEXT}, - ORM::Field{"firstName",ORM::FieldType::FT_TEXT}, - ORM::Field{"initials",ORM::FieldType::FT_TEXT}, - ORM::Field{"lastName",ORM::FieldType::FT_TEXT}, - ORM::Field{"phoneNumber",ORM::FieldType::FT_TEXT}, - ORM::Field{"secondaryEmail",ORM::FieldType::FT_TEXT}, - ORM::Field{"accessPoints",ORM::FieldType::FT_TEXT}, - ORM::Field{"serviceAddress",ORM::FieldType::FT_TEXT}, - ORM::Field{"billingAddress",ORM::FieldType::FT_TEXT}, - ORM::Field{"created",ORM::FieldType::FT_BIGINT}, - ORM::Field{"modified",ORM::FieldType::FT_BIGINT} - }; + static ORM::FieldVec SubInfoDBDB_Fields{// object info + ORM::Field{"id", 64, true}, + ORM::Field{"userId", ORM::FieldType::FT_TEXT}, + ORM::Field{"firstName", ORM::FieldType::FT_TEXT}, + ORM::Field{"initials", ORM::FieldType::FT_TEXT}, + ORM::Field{"lastName", ORM::FieldType::FT_TEXT}, + ORM::Field{"phoneNumber", ORM::FieldType::FT_TEXT}, + ORM::Field{"secondaryEmail", ORM::FieldType::FT_TEXT}, + ORM::Field{"accessPoints", ORM::FieldType::FT_TEXT}, + ORM::Field{"serviceAddress", ORM::FieldType::FT_TEXT}, + ORM::Field{"billingAddress", ORM::FieldType::FT_TEXT}, + ORM::Field{"created", ORM::FieldType::FT_BIGINT}, + ORM::Field{"modified", ORM::FieldType::FT_BIGINT}}; - static ORM::IndexVec SubInfoDBDB_Fields_Indexes{ - { std::string("subinfo_userid_index"), - ORM::IndexEntryVec{ - {std::string("userId"), - ORM::Indextype::ASC} } }, - { std::string("subinfo_phone_index"), - ORM::IndexEntryVec{ - {std::string("phoneNumber"), - ORM::Indextype::ASC} } } - }; + static ORM::IndexVec SubInfoDBDB_Fields_Indexes{ + {std::string("subinfo_userid_index"), + ORM::IndexEntryVec{{std::string("userId"), ORM::Indextype::ASC}}}, + {std::string("subinfo_phone_index"), + ORM::IndexEntryVec{{std::string("phoneNumber"), ORM::Indextype::ASC}}}}; - SubscriberInfoDB::SubscriberInfoDB( OpenWifi::DBType T, Poco::Data::SessionPool & P, Poco::Logger &L) : - DB(T, "subscriberinfo", SubInfoDBDB_Fields, SubInfoDBDB_Fields_Indexes, P, L, "sui") {} + SubscriberInfoDB::SubscriberInfoDB(OpenWifi::DBType T, Poco::Data::SessionPool &P, + Poco::Logger &L) + : DB(T, "subscriberinfo", SubInfoDBDB_Fields, SubInfoDBDB_Fields_Indexes, P, L, "sui") {} - void SubscriberInfoDB::CreateDefaultSubscriberInfo(const SecurityObjects::UserInfoAndPolicy & UI, SubObjects::SubscriberInfo &SI, const ProvObjects::SubscriberDeviceList & Devices) { - auto Now = Utils::Now(); + void SubscriberInfoDB::CreateDefaultSubscriberInfo( + const SecurityObjects::UserInfoAndPolicy &UI, SubObjects::SubscriberInfo &SI, + const ProvObjects::SubscriberDeviceList &Devices) { + auto Now = Utils::Now(); - // ok, we need to generate a default record and store it... - SI.id = UI.userinfo.id; - SI.created = SI.modified = Now; - SI.userId = UI.userinfo.email; - const auto NameParts = Poco::StringTokenizer(UI.userinfo.name.empty() ? SI.userId : UI.userinfo.name," @._-"); - if(NameParts.count()>0) { - for(size_t i=0; i 0) { + for (size_t i = 0; i < NameParts.count(); ++i) { + if (i == 0) + SI.firstName = NameParts[0]; + else + SI.lastName = NameParts[i] + " "; + } + Poco::trimInPlace(SI.firstName); + Poco::trimInPlace(SI.lastName); + } else { + SI.firstName = UI.userinfo.name; + } - if(!UI.userinfo.userTypeProprietaryInfo.mobiles.empty()) - SI.phoneNumber = UI.userinfo.userTypeProprietaryInfo.mobiles[0].number; + if (!UI.userinfo.userTypeProprietaryInfo.mobiles.empty()) + SI.phoneNumber = UI.userinfo.userTypeProprietaryInfo.mobiles[0].number; - int ap_num=1; - for(const auto &i:Devices.subscriberDevices) { - SubObjects::AccessPoint AP; - AP.macAddress = i.realMacAddress; - AP.serialNumber = i.serialNumber; - AP.deviceType = i.deviceType; - AP.id = i.info.id; - AP.name = "Access Point #" + std::to_string(ap_num++); - AP.deviceMode.created = AP.deviceMode.modified = Now; - AP.deviceMode.type = "nat"; - AP.deviceMode.enableLEDS = true; - AP.internetConnection.modified = AP.internetConnection.created = Now; - AP.internetConnection.type = "automatic"; - AP.deviceMode.subnet = "192.168.1./24"; - AP.deviceMode.subnetMask = "24"; - AP.deviceMode.startIP = "192.168.1.10"; - AP.deviceMode.endIP = "192.168.1.100"; - AP.deviceMode.leaseTime = "24h"; + int ap_num = 1; + for (const auto &i : Devices.subscriberDevices) { + SubObjects::AccessPoint AP; + AP.macAddress = i.realMacAddress; + AP.serialNumber = i.serialNumber; + AP.deviceType = i.deviceType; + AP.id = i.info.id; + AP.name = "Access Point #" + std::to_string(ap_num++); + AP.deviceMode.created = AP.deviceMode.modified = Now; + AP.deviceMode.type = "nat"; + AP.deviceMode.enableLEDS = true; + AP.internetConnection.modified = AP.internetConnection.created = Now; + AP.internetConnection.type = "automatic"; + AP.deviceMode.subnet = "192.168.1./24"; + AP.deviceMode.subnetMask = "24"; + AP.deviceMode.startIP = "192.168.1.10"; + AP.deviceMode.endIP = "192.168.1.100"; + AP.deviceMode.leaseTime = "24h"; - SubObjects::WifiNetwork WN; - WN.type = "main"; - WN.name = "OpenWifi-" + AP.macAddress.substr(6); - WN.password = Poco::toUpper(AP.macAddress); - WN.encryption = "wpa2-personal"; - if(AP.deviceType=="linksys_ea8300") { - WN.bands.emplace_back("2G"); - WN.bands.emplace_back("5GL"); - WN.bands.emplace_back("5GU"); - } else { - WN.bands.emplace_back("2G"); - WN.bands.emplace_back("5G"); - } + SubObjects::WifiNetwork WN; + WN.type = "main"; + WN.name = "OpenWifi-" + AP.macAddress.substr(6); + WN.password = Poco::toUpper(AP.macAddress); + WN.encryption = "wpa2-personal"; + if (AP.deviceType == "linksys_ea8300") { + WN.bands.emplace_back("2G"); + WN.bands.emplace_back("5GL"); + WN.bands.emplace_back("5GU"); + } else { + WN.bands.emplace_back("2G"); + WN.bands.emplace_back("5G"); + } - AP.wifiNetworks.created = AP.wifiNetworks.modified = Now; - AP.wifiNetworks.wifiNetworks.push_back(WN); + AP.wifiNetworks.created = AP.wifiNetworks.modified = Now; + AP.wifiNetworks.wifiNetworks.push_back(WN); - for(const auto &b:WN.bands) { - SubObjects::RadioInformation RI; + for (const auto &b : WN.bands) { + SubObjects::RadioInformation RI; - RI.band = b; - RI.rates.beacon = 6000; - RI.rates.multicast = 24000; - RI.channel = 0; - RI.country = i.locale; - RI.maximumClients = 64; - RI.legacyRates = false; - RI.he.bssColor = 1; - RI.he.ema = false; - RI.he.multipleBSSID = false; - RI.beaconInterval = 100; - RI.dtimPeriod = 2; - RI.allowDFS = false; - RI.mimo = ""; - RI.txpower = 24 ; - if(b=="2G") { - RI.bandwidth = 20; - RI.channelWidth = 20 ; - RI.channelMode = "VHT"; - } else if(b=="5G") { - RI.bandwidth = 20; - RI.channelWidth = 40 ; - RI.channelMode = "VHT"; - } else if(b=="5GU") { - RI.bandwidth = 20; - RI.channelWidth = 40 ; - RI.channelMode = "VHT"; - } else if(b=="5GL") { - RI.bandwidth = 20; - RI.channelWidth = 40 ; - RI.channelMode = "VHT"; - } else if(b=="6G") { - RI.bandwidth = 20; - RI.channelWidth = 40 ; - RI.channelMode = "HE"; - } - AP.radios.emplace_back(RI); - } - SI.accessPoints.list.push_back(AP); - } - } + RI.band = b; + RI.rates.beacon = 6000; + RI.rates.multicast = 24000; + RI.channel = 0; + RI.country = i.locale; + RI.maximumClients = 64; + RI.legacyRates = false; + RI.he.bssColor = 1; + RI.he.ema = false; + RI.he.multipleBSSID = false; + RI.beaconInterval = 100; + RI.dtimPeriod = 2; + RI.allowDFS = false; + RI.mimo = ""; + RI.txpower = 24; + if (b == "2G") { + RI.bandwidth = 20; + RI.channelWidth = 20; + RI.channelMode = "VHT"; + } else if (b == "5G") { + RI.bandwidth = 20; + RI.channelWidth = 40; + RI.channelMode = "VHT"; + } else if (b == "5GU") { + RI.bandwidth = 20; + RI.channelWidth = 40; + RI.channelMode = "VHT"; + } else if (b == "5GL") { + RI.bandwidth = 20; + RI.channelWidth = 40; + RI.channelMode = "VHT"; + } else if (b == "6G") { + RI.bandwidth = 20; + RI.channelWidth = 40; + RI.channelMode = "HE"; + } + AP.radios.emplace_back(RI); + } + SI.accessPoints.list.push_back(AP); + } + } +} // namespace OpenWifi + +template <> +void ORM::DB::Convert( + const OpenWifi::SubInfoDBRecordType &In, OpenWifi::SubObjects::SubscriberInfo &Out) { + Out.id = In.get<0>(); + Out.userId = In.get<1>(); + Out.firstName = In.get<2>(); + Out.initials = In.get<3>(); + Out.lastName = In.get<4>(); + Out.phoneNumber = In.get<5>(); + Out.secondaryEmail = In.get<6>(); + Out.accessPoints = + OpenWifi::RESTAPI_utils::to_object(In.get<7>()); + Out.serviceAddress = + OpenWifi::RESTAPI_utils::to_object(In.get<8>()); + Out.billingAddress = + OpenWifi::RESTAPI_utils::to_object(In.get<9>()); + Out.created = In.get<10>(); + Out.modified = In.get<11>(); } -template<> void ORM::DB::Convert(const OpenWifi::SubInfoDBRecordType &In, OpenWifi::SubObjects::SubscriberInfo &Out) { - Out.id = In.get<0>(); - Out.userId = In.get<1>(); - Out.firstName = In.get<2>(); - Out.initials = In.get<3>(); - Out.lastName = In.get<4>(); - Out.phoneNumber = In.get<5>(); - Out.secondaryEmail = In.get<6>(); - Out.accessPoints = OpenWifi::RESTAPI_utils::to_object(In.get<7>()); - Out.serviceAddress = OpenWifi::RESTAPI_utils::to_object(In.get<8>()); - Out.billingAddress = OpenWifi::RESTAPI_utils::to_object(In.get<9>()); - Out.created = In.get<10>(); - Out.modified = In.get<11>(); -} - -template<> void ORM::DB::Convert(const OpenWifi::SubObjects::SubscriberInfo &In, OpenWifi::SubInfoDBRecordType &Out) { - Out.set<0>(In.id); - Out.set<1>(In.userId); - Out.set<2>(In.firstName); - Out.set<3>(In.initials); - Out.set<4>(In.lastName); - Out.set<5>(In.phoneNumber); - Out.set<6>(In.secondaryEmail); - Out.set<7>(OpenWifi::RESTAPI_utils::to_string(In.accessPoints)); - Out.set<8>(OpenWifi::RESTAPI_utils::to_string(In.serviceAddress)); - Out.set<9>(OpenWifi::RESTAPI_utils::to_string(In.billingAddress)); - Out.set<10>(In.created); - Out.set<11>(In.modified); +template <> +void ORM::DB::Convert( + const OpenWifi::SubObjects::SubscriberInfo &In, OpenWifi::SubInfoDBRecordType &Out) { + Out.set<0>(In.id); + Out.set<1>(In.userId); + Out.set<2>(In.firstName); + Out.set<3>(In.initials); + Out.set<4>(In.lastName); + Out.set<5>(In.phoneNumber); + Out.set<6>(In.secondaryEmail); + Out.set<7>(OpenWifi::RESTAPI_utils::to_string(In.accessPoints)); + Out.set<8>(OpenWifi::RESTAPI_utils::to_string(In.serviceAddress)); + Out.set<9>(OpenWifi::RESTAPI_utils::to_string(In.billingAddress)); + Out.set<10>(In.created); + Out.set<11>(In.modified); } diff --git a/src/storage/storage_subscriber_info.h b/src/storage/storage_subscriber_info.h index c66b767..08fbda4 100644 --- a/src/storage/storage_subscriber_info.h +++ b/src/storage/storage_subscriber_info.h @@ -4,33 +4,26 @@ #pragma once -#include "framework/orm.h" -#include "RESTObjects/RESTAPI_SubObjects.h" #include "RESTObjects/RESTAPI_ProvObjects.h" +#include "RESTObjects/RESTAPI_SubObjects.h" +#include "framework/orm.h" namespace OpenWifi { - typedef Poco::Tuple< - std::string, - std::string, - std::string, - std::string, - std::string, - std::string, - std::string, - std::string, - std::string, - std::string, - uint64_t, - uint64_t - > SubInfoDBRecordType; + typedef Poco::Tuple + SubInfoDBRecordType; - typedef std::vector SubInfoDBRecordList; + typedef std::vector SubInfoDBRecordList; - class SubscriberInfoDB : public ORM::DB { - public: - SubscriberInfoDB( OpenWifi::DBType T, Poco::Data::SessionPool & P, Poco::Logger &L); - virtual ~SubscriberInfoDB() {}; - void CreateDefaultSubscriberInfo(const SecurityObjects::UserInfoAndPolicy & UI, SubObjects::SubscriberInfo &SI,const ProvObjects::SubscriberDeviceList &Devices); - private: - }; -} \ No newline at end of file + class SubscriberInfoDB : public ORM::DB { + public: + SubscriberInfoDB(OpenWifi::DBType T, Poco::Data::SessionPool &P, Poco::Logger &L); + virtual ~SubscriberInfoDB(){}; + void CreateDefaultSubscriberInfo(const SecurityObjects::UserInfoAndPolicy &UI, + SubObjects::SubscriberInfo &SI, + const ProvObjects::SubscriberDeviceList &Devices); + + private: + }; +} // namespace OpenWifi \ No newline at end of file