Finishing framework refactor.

This commit is contained in:
stephb9959
2021-10-24 10:44:27 -07:00
parent f28ef3f65b
commit 75cb2057fe
118 changed files with 4949 additions and 4773 deletions

View File

@@ -52,33 +52,33 @@ include_directories(/usr/local/include /usr/local/opt/openssl/include src inclu
add_executable(owprov
build
src/framework/CountryCodes.h
src/framework/KafkaTopics.h
src/framework/MicroService.h
src/framework/OpenWifiTypes.h
src/framework/orm.h
src/framework/RESTAPI_errors.h
src/framework/RESTAPI_protocol.h
src/framework/StorageClass.h
src/framework/uCentral_Protocol.h
src/RESTObjects/RESTAPI_SecurityObjects.h src/RESTObjects/RESTAPI_SecurityObjects.cpp
src/RESTObjects/RESTAPI_ProvObjects.cpp src/RESTObjects/RESTAPI_ProvObjects.h
src/RESTObjects/RESTAPI_GWobjects.h src/RESTObjects/RESTAPI_GWobjects.cpp
src/RESTObjects/RESTAPI_FMSObjects.h src/RESTObjects/RESTAPI_FMSObjects.cpp
src/APIServers.cpp
src/Daemon.cpp src/Daemon.h
src/Dashboard.h src/Dashboard.cpp
src/framework/MicroService.cpp src/framework/MicroService.h
src/framework/Utils.h src/framework/Utils.cpp
src/framework/RESTAPI_utils.cpp src/framework/RESTAPI_utils.h
src/RESTAPI/RESTAPI_SecurityObjects.h src/RESTAPI/RESTAPI_SecurityObjects.h
src/framework/ALBHealthCheckServer.h
src/framework/AuthClient.cpp src/framework/AuthClient.h
src/framework/KafkaManager.cpp src/framework/KafkaManager.h
src/framework/Kafka_topics.h
src/framework/OpenAPIRequest.cpp src/framework/OpenAPIRequest.h
src/RESTAPI/RESTAPI_SecurityObjects.h src/RESTAPI/RESTAPI_SecurityObjects.cpp
src/framework/RESTAPI_handler.cpp src/framework/RESTAPI_handler.h
src/RESTAPI/RESTAPI_server.h src/RESTAPI/RESTAPI_server.cpp
src/framework/RESTAPI_system_command.h src/framework/RESTAPI_system_command.cpp
src/RESTAPI/RESTAPI_InternalServer.h src/RESTAPI/RESTAPI_InternalServer.cpp
src/StorageService.cpp src/StorageService.h
src/framework/SubSystemServer.cpp src/framework/SubSystemServer.h
src/RESTAPI/RESTAPI_ProvObjects.cpp src/RESTAPI/RESTAPI_ProvObjects.h
src/storage/storage_entity.cpp src/storage/storage_entity.h
src/storage/storage_policies.cpp src/storage/storage_policies.h
src/framework/OpenWifiTypes.h
src/storage/storage_venue.cpp src/storage/storage_venue.h
src/storage/storage_location.cpp src/storage/storage_location.h
src/storage/storage_contact.cpp src/storage/storage_contact.h
src/RESTAPI/RESTAPI_entity_handler.cpp src/RESTAPI/RESTAPI_entity_handler.h
src/storage/storage_inventory.cpp src/storage/storage_inventory.h
src/storage/storage_management_roles.cpp src/storage/storage_management_roles.h
src/storage/storage_configurations.cpp src/storage/storage_configurations.h
src/storage/storage_tags.cpp src/storage/storage_tags.h
src/RESTAPI/RESTAPI_entity_handler.cpp src/RESTAPI/RESTAPI_entity_handler.h
src/RESTAPI/RESTAPI_contact_handler.cpp src/RESTAPI/RESTAPI_contact_handler.h
src/RESTAPI/RESTAPI_location_handler.cpp src/RESTAPI/RESTAPI_location_handler.h
src/RESTAPI/RESTAPI_venue_handler.cpp src/RESTAPI/RESTAPI_venue_handler.h
@@ -86,13 +86,24 @@ add_executable(owprov
src/RESTAPI/RESTAPI_managementPolicy_handler.cpp src/RESTAPI/RESTAPI_managementPolicy_handler.h
src/RESTAPI/RESTAPI_inventory_list_handler.cpp src/RESTAPI/RESTAPI_inventory_list_handler.h
src/RESTAPI/RESTAPI_entity_list_handler.cpp src/RESTAPI/RESTAPI_entity_list_handler.h
src/storage/storage_management_roles.cpp src/storage/storage_management_roles.h
src/framework/storage_setup.cpp
src/SerialNumberCache.h src/SerialNumberCache.cpp
src/storage/storage_configurations.cpp src/storage/storage_configurations.h
src/RESTAPI/RESTAPI_configurations_handler.cpp src/RESTAPI/RESTAPI_configurations_handler.h
src/RESTAPI/RESTAPI_webSocketServer.h src/RESTAPI/RESTAPI_webSocketServer.cpp src/RESTAPI/RESTAPI_contact_list_handler.cpp src/RESTAPI/RESTAPI_contact_list_handler.h src/RESTAPI/RESTAPI_location_list_handler.cpp src/RESTAPI/RESTAPI_location_list_handler.h src/RESTAPI/RESTAPI_venue_list_handler.cpp src/RESTAPI/RESTAPI_venue_list_handler.h src/RESTAPI/RESTAPI_managementPolicy_list_handler.cpp src/RESTAPI/RESTAPI_managementPolicy_list_handler.h src/RESTAPI/RESTAPI_managementRole_handler.cpp src/RESTAPI/RESTAPI_managementRole_handler.h src/RESTAPI/RESTAPI_managementRole_list_handler.cpp src/RESTAPI/RESTAPI_managementRole_list_handler.h src/RESTAPI/RESTAPI_configurations_list_handler.cpp src/RESTAPI/RESTAPI_configurations_list_handler.h src/SecurityDBProxy.cpp src/SecurityDBProxy.h src/APConfig.cpp src/APConfig.h src/framework/RESTAPI_errors.h src/ConfigurationValidator.cpp src/ConfigurationValidator.h src/framework/RESTAPI_GenericServer.cpp src/framework/RESTAPI_GenericServer.h src/AutoDiscovery.cpp src/AutoDiscovery.h src/framework/CIDRUtils.cpp src/framework/CIDRUtils.h src/SDK_stubs.cpp src/SDK_stubs.h src/ConfigSanityChecker.cpp src/ConfigSanityChecker.h src/storage/storage_tags.cpp src/storage/storage_tags.h src/TagServer.cpp src/TagServer.h src/framework/Storage.h
src/framework/CountryCodes.h src/RESTAPI/RESTAPI_db_helpers.cpp src/RESTAPI/RESTAPI_db_helpers.h)
src/RESTAPI/RESTAPI_webSocketServer.h src/RESTAPI/RESTAPI_webSocketServer.cpp
src/RESTAPI/RESTAPI_contact_list_handler.cpp src/RESTAPI/RESTAPI_contact_list_handler.h
src/RESTAPI/RESTAPI_location_list_handler.cpp src/RESTAPI/RESTAPI_location_list_handler.h
src/RESTAPI/RESTAPI_venue_list_handler.cpp src/RESTAPI/RESTAPI_venue_list_handler.h
src/RESTAPI/RESTAPI_managementPolicy_list_handler.cpp src/RESTAPI/RESTAPI_managementPolicy_list_handler.h
src/RESTAPI/RESTAPI_managementRole_handler.cpp src/RESTAPI/RESTAPI_managementRole_handler.h
src/RESTAPI/RESTAPI_managementRole_list_handler.cpp src/RESTAPI/RESTAPI_managementRole_list_handler.h
src/RESTAPI/RESTAPI_configurations_list_handler.cpp src/RESTAPI/RESTAPI_configurations_list_handler.h
src/SerialNumberCache.h src/SerialNumberCache.cpp
src/SecurityDBProxy.cpp src/SecurityDBProxy.h
src/APConfig.cpp src/APConfig.h
src/ConfigurationValidator.cpp src/ConfigurationValidator.h
src/AutoDiscovery.cpp src/AutoDiscovery.h
src/SDK_stubs.cpp src/SDK_stubs.h
src/ConfigSanityChecker.cpp src/ConfigSanityChecker.h
src/TagServer.cpp src/TagServer.h
src/RESTAPI/RESTAPI_db_helpers.cpp src/RESTAPI/RESTAPI_db_helpers.h)
target_link_libraries(owprov PUBLIC
${Poco_LIBRARIES} ${MySQL_LIBRARIES}

2
build
View File

@@ -1 +1 @@
354
432

View File

@@ -130,7 +130,7 @@ namespace OpenWifi {
Explanation_.clear();
try {
ProvObjects::InventoryTag D;
if(Storage()->InventoryDB().GetRecord("serialNumber", SerialNumber_, D)) {
if(StorageService()->InventoryDB().GetRecord("serialNumber", SerialNumber_, D)) {
if(!D.deviceConfiguration.empty()) {
AddConfiguration(D.deviceConfiguration);
}
@@ -202,7 +202,7 @@ namespace OpenWifi {
if(UUID.empty())
return;
if(Storage()->ConfigurationDB().GetRecord("id", UUID,Config)) {
if(StorageService()->ConfigurationDB().GetRecord("id", UUID,Config)) {
// find where to insert into this list using the weight.
if(!Config.configuration.empty()) {
if(DeviceTypeMatch(DeviceType_,Config.deviceTypes)) {
@@ -232,7 +232,7 @@ namespace OpenWifi {
void APConfig::AddEntityConfig(const std::string &UUID) {
ProvObjects::Entity E;
if(Storage()->EntityDB().GetRecord("id",UUID,E)) {
if(StorageService()->EntityDB().GetRecord("id",UUID,E)) {
AddConfiguration(E.deviceConfiguration);
if(!E.parent.empty())
AddEntityConfig(E.parent);
@@ -241,7 +241,7 @@ namespace OpenWifi {
void APConfig::AddVenueConfig(const std::string &UUID) {
ProvObjects::Venue V;
if(Storage()->VenueDB().GetRecord("id",UUID,V)) {
if(StorageService()->VenueDB().GetRecord("id",UUID,V)) {
AddConfiguration(V.deviceConfiguration);
if(!V.entity.empty()) {
AddEntityConfig(V.entity);

View File

@@ -7,7 +7,7 @@
#include <string>
#include "Poco/Logger.h"
#include "RESTAPI/RESTAPI_ProvObjects.h"
#include "RESTObjects//RESTAPI_ProvObjects.h"
namespace OpenWifi {

58
src/APIServers.cpp Normal file
View File

@@ -0,0 +1,58 @@
//
// Created by stephane bourque on 2021-10-23.
//
#include "framework/MicroService.h"
#include "RESTAPI/RESTAPI_entity_handler.h"
#include "RESTAPI/RESTAPI_contact_handler.h"
#include "RESTAPI/RESTAPI_location_handler.h"
#include "RESTAPI/RESTAPI_venue_handler.h"
#include "RESTAPI/RESTAPI_inventory_handler.h"
#include "RESTAPI/RESTAPI_managementPolicy_handler.h"
#include "RESTAPI/RESTAPI_managementPolicy_list_handler.h"
#include "RESTAPI/RESTAPI_inventory_list_handler.h"
#include "RESTAPI/RESTAPI_entity_list_handler.h"
#include "RESTAPI/RESTAPI_configurations_handler.h"
#include "RESTAPI/RESTAPI_configurations_list_handler.h"
#include "RESTAPI/RESTAPI_webSocketServer.h"
#include "RESTAPI/RESTAPI_contact_list_handler.h"
#include "RESTAPI/RESTAPI_location_list_handler.h"
#include "RESTAPI/RESTAPI_venue_list_handler.h"
#include "RESTAPI/RESTAPI_managementRole_list_handler.h"
namespace OpenWifi {
Poco::Net::HTTPRequestHandler * RESTAPI_external_server(const char *Path, RESTAPIHandler::BindingMap &Bindings,
Poco::Logger & L, RESTAPI_GenericServer & S) {
return RESTAPI_Router<
RESTAPI_system_command,
RESTAPI_entity_handler,
RESTAPI_entity_list_handler,
RESTAPI_contact_handler,
RESTAPI_contact_list_handler,
RESTAPI_location_handler,
RESTAPI_location_list_handler,
RESTAPI_venue_handler,
RESTAPI_venue_list_handler,
RESTAPI_inventory_handler,
RESTAPI_inventory_list_handler,
RESTAPI_managementPolicy_handler,
RESTAPI_managementPolicy_list_handler,
RESTAPI_managementRole_list_handler,
RESTAPI_configurations_handler,
RESTAPI_configurations_list_handler,
RESTAPI_webSocketServer
>(Path,Bindings,L, S);
}
Poco::Net::HTTPRequestHandler * RESTAPI_internal_server(const char *Path, RESTAPIHandler::BindingMap &Bindings,
Poco::Logger & L, RESTAPI_GenericServer & S) {
return RESTAPI_Router_I<
RESTAPI_system_command ,
RESTAPI_inventory_handler,
RESTAPI_configurations_handler,
RESTAPI_configurations_list_handler
>(Path, Bindings, L, S);
}
}

View File

@@ -3,12 +3,11 @@
//
#include "AutoDiscovery.h"
#include "framework/uCentralProtocol.h"
#include "framework/KafkaManager.h"
#include "framework/Kafka_topics.h"
#include "framework/uCentral_Protocol.h"
#include "framework/KafkaTopics.h"
#include "storage/storage_inventory.h"
#include "StorageService.h"
#include "Daemon.h"
#include "framework/MicroService.h"
namespace OpenWifi {
@@ -60,7 +59,7 @@ namespace OpenWifi {
}
if(!SerialNumber.empty()) {
// std::cout << "SerialNUmber: " << SerialNumber << " CID: " << ConnectedIP << " DeviceType: " << DeviceType << std::endl;
Storage()->InventoryDB().CreateFromConnection(SerialNumber,ConnectedIP,DeviceType);
StorageService()->InventoryDB().CreateFromConnection(SerialNumber,ConnectedIP,DeviceType);
}
}
} catch (const Poco::Exception &E) {
@@ -71,8 +70,8 @@ namespace OpenWifi {
};
int AutoDiscovery::Start() {
firmwareUpgrade_ = Daemon()->ConfigGetString("firmware.updater.upgrade","no");
firmwareRCOnly_ = Daemon()->ConfigGetBool("firmware.updater.rconly",false);
firmwareUpgrade_ = MicroService::instance().ConfigGetString("firmware.updater.upgrade","no");
firmwareRCOnly_ = MicroService::instance().ConfigGetBool("firmware.updater.rconly",false);
Types::TopicNotifyFunction F = [this](std::string s1,std::string s2) { this->ConnectionReceived(s1,s2); };
ConnectionWatcherId_ = KafkaManager()->RegisterTopicWatcher(KafkaTopics::CONNECTION, F);
Worker_.start(*this);

View File

@@ -5,7 +5,7 @@
#ifndef OWPROV_AUTODISCOVERY_H
#define OWPROV_AUTODISCOVERY_H
#include "framework/SubSystemServer.h"
#include "framework/MicroService.h"
#include "framework/OpenWifiTypes.h"
namespace OpenWifi {

View File

@@ -6,11 +6,8 @@
#include <fstream>
#include <regex>
#include "framework/MicroService.h"
#include "ConfigurationValidator.h"
#include "framework/Utils.h"
#include "Daemon.h"
#include "Poco/Logger.h"
#include "Poco/URI.h"
#include "framework/CountryCodes.h"
#include "Poco/StringTokenizer.h"
@@ -2172,7 +2169,7 @@ namespace OpenWifi {
Validator_->set_root_schema(schema);
Logger_.information("Using uCentral validation schema from GIT.");
} else {
std::string FileName{ Daemon()->DataDir() + "/ucentral.schema.json" };
std::string FileName{ MicroService::instance().DataDir() + "/ucentral.schema.json" };
try {
std::ifstream input(FileName);
std::stringstream schema_file;

View File

@@ -6,8 +6,7 @@
#define OWPROV_CONFIGURATIONVALIDATOR_H
#include <nlohmann/json-schema.hpp>
#include "Poco/Logger.h"
#include "framework/SubSystemServer.h"
#include "framework/MicroService.h"
using nlohmann::json;
using nlohmann::json_schema::json_validator;

View File

@@ -12,12 +12,7 @@
#include "Poco/Environment.h"
#include "Daemon.h"
#include "StorageService.h"
#include "framework/Utils.h"
#include "framework/AuthClient.h"
#include "RESTAPI/RESTAPI_server.h"
#include "RESTAPI/RESTAPI_InternalServer.h"
#include "SecurityDBProxy.h"
#include "AutoDiscovery.h"
#include "ConfigurationValidator.h"
@@ -33,13 +28,10 @@ namespace OpenWifi {
vDAEMON_CONFIG_ENV_VAR,
vDAEMON_APP_NAME,
vDAEMON_BUS_TIMER,
Types::SubSystemVec{
OpenWifi::Storage(),
SubSystemVec{
OpenWifi::StorageService(),
ConfigurationValidator(),
AuthClient(),
SerialNumberCache(),
RESTAPI_server(),
RESTAPI_InternalServer(),
SecurityDBProxy(),
AutoDiscovery()
});
@@ -47,8 +39,11 @@ namespace OpenWifi {
return instance_;
}
void Daemon::initialize(Poco::Util::Application &self) {
MicroService::initialize(*this);
void Daemon::initialize() {
}
void MicroServicePostInitialization() {
Daemon()->initialize();
}
}

View File

@@ -15,6 +15,7 @@
#include <vector>
#include <set>
/*
#include "Poco/Util/Application.h"
#include "Poco/Util/ServerApplication.h"
#include "Poco/Util/Option.h"
@@ -24,7 +25,7 @@
#include "Poco/Crypto/RSAKey.h"
#include "Poco/Crypto/CipherFactory.h"
#include "Poco/Crypto/Cipher.h"
*/
#include "Dashboard.h"
#include "framework/MicroService.h"
#include "framework/OpenWifiTypes.h"
@@ -44,10 +45,10 @@ namespace OpenWifi {
const std::string & ConfigEnv,
const std::string & AppName,
uint64_t BusTimer,
const Types::SubSystemVec & SubSystems) :
const SubSystemVec & SubSystems) :
MicroService( PropFile, RootEnv, ConfigEnv, AppName, BusTimer, SubSystems) {};
void initialize(Poco::Util::Application &self);
void initialize();
static Daemon *instance();
inline OpenWifi::TopoDashboard & GetDashboard() { return DB_; }
Poco::Logger & Log() { return Poco::Logger::get(AppName()); }

View File

@@ -9,7 +9,7 @@
#define UCENTRALGW_DASHBOARD_H
#include "framework/OpenWifiTypes.h"
#include "RESTAPI/RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
namespace OpenWifi {
class TopoDashboard {

View File

@@ -1,75 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include "RESTAPI_InternalServer.h"
#include "Poco/URI.h"
#include "framework/RESTAPI_system_command.h"
#include "RESTAPI_inventory_handler.h"
#include "RESTAPI/RESTAPI_configurations_list_handler.h"
#include "RESTAPI/RESTAPI_configurations_handler.h"
#include "framework/Utils.h"
namespace OpenWifi {
class RESTAPI_InternalServer *RESTAPI_InternalServer::instance_ = nullptr;
int RESTAPI_InternalServer::Start() {
Logger_.information("Starting.");
LogServer_.InitLogging();
for(const auto & Svr: ConfigServersList_) {
Logger_.information(Poco::format("Starting: %s:%s Keyfile:%s CertFile: %s", Svr.Address(), std::to_string(Svr.Port()),
Svr.KeyFile(),Svr.CertFile()));
auto Sock{Svr.CreateSecureSocket(Logger_)};
Svr.LogCert(Logger_);
if(!Svr.RootCA().empty())
Svr.LogCas(Logger_);
auto Params = new Poco::Net::HTTPServerParams;
Params->setMaxThreads(50);
Params->setMaxQueued(200);
Params->setKeepAlive(true);
auto NewServer = std::make_unique<Poco::Net::HTTPServer>(new InternalRequestHandlerFactory(LogServer_), Pool_, Sock, Params);
NewServer->start();
RESTServers_.push_back(std::move(NewServer));
}
return 0;
}
void RESTAPI_InternalServer::Stop() {
Logger_.information("Stopping ");
for( const auto & svr : RESTServers_ )
svr->stop();
RESTServers_.clear();
}
void RESTAPI_InternalServer::reinitialize(Poco::Util::Application &self) {
Logger_.information("Reinitializing.");
Daemon()->LoadConfigurationFile();
Stop();
Start();
}
Poco::Net::HTTPRequestHandler *InternalRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest & Request) {
Poco::URI uri(Request.getURI());
const auto &Path = uri.getPath();
RESTAPIHandler::BindingMap Bindings;
return RESTAPI_Router_I<
RESTAPI_system_command ,
RESTAPI_inventory_handler,
RESTAPI_configurations_handler,
RESTAPI_configurations_list_handler
>(Path, Bindings, Logger_, Server_);
}
}

View File

@@ -1,67 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRALSEC_RESTAPI_INTERNALSERVER_H
#define UCENTRALSEC_RESTAPI_INTERNALSERVER_H
#include "framework/SubSystemServer.h"
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/NetException.h"
#include "framework/RESTAPI_GenericServer.h"
namespace OpenWifi {
class RESTAPI_InternalServer : public SubSystemServer {
public:
static RESTAPI_InternalServer *instance() {
if (instance_ == nullptr) {
instance_ = new RESTAPI_InternalServer;
}
return instance_;
}
int Start() override;
void Stop() override;
void reinitialize(Poco::Util::Application &self) override;
private:
static RESTAPI_InternalServer *instance_;
std::vector<std::unique_ptr<Poco::Net::HTTPServer>> RESTServers_;
Poco::ThreadPool Pool_;
RESTAPI_GenericServer LogServer_;
RESTAPI_InternalServer() noexcept
:SubSystemServer("RESTAPIInternalServer", "REST-ISRV", "openwifi.internal.restapi")
{
}
};
inline RESTAPI_InternalServer * RESTAPI_InternalServer() { return RESTAPI_InternalServer::instance(); };
class InternalRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory {
public:
explicit InternalRequestHandlerFactory(RESTAPI_GenericServer & Server) :
Logger_(RESTAPI_InternalServer()->Logger()),
Server_(Server)
{}
Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &request) override;
private:
Poco::Logger &Logger_;
RESTAPI_GenericServer &Server_;
};
} // namespace
#endif //UCENTRALSEC_RESTAPI_INTERNALSERVER_H

View File

@@ -6,12 +6,11 @@
// Arilia Wireless Inc.
//
#include "framework/MicroService.h"
#include "RESTAPI_configurations_handler.h"
#include "RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "Daemon.h"
#include "framework/RESTAPI_errors.h"
#include "ConfigurationValidator.h"
namespace OpenWifi{
@@ -29,7 +28,7 @@ namespace OpenWifi{
Storage::ExpandedListMap M;
std::vector<std::string> Errors;
Poco::JSON::Object Inner;
if(Storage()->ExpandInUse(Existing.inUse,M,Errors)) {
if(StorageService()->ExpandInUse(Existing.inUse,M,Errors)) {
for(const auto &[type,list]:M) {
Poco::JSON::Array ObjList;
for(const auto &i:list.entries) {
@@ -47,7 +46,7 @@ namespace OpenWifi{
if(!Existing.managementPolicy.empty()) {
Poco::JSON::Object PolObj;
ProvObjects::ManagementPolicy Policy;
if(Storage()->PolicyDB().GetRecord("id",Existing.managementPolicy,Policy)) {
if(StorageService()->PolicyDB().GetRecord("id",Existing.managementPolicy,Policy)) {
PolObj.set( "name", Policy.info.name);
PolObj.set( "description", Policy.info.description);
}
@@ -142,17 +141,17 @@ namespace OpenWifi{
return BadRequest(RESTAPI::Errors::NameMustBeSet);
}
if(!C.managementPolicy.empty() && !Storage()->PolicyDB().Exists("id",C.managementPolicy)) {
if(!C.managementPolicy.empty() && !StorageService()->PolicyDB().Exists("id",C.managementPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownId);
}
C.info.modified = C.info.created = std::time(nullptr);
C.info.id = Daemon()->CreateUUID();
C.info.id = MicroService::instance().CreateUUID();
for(auto &i:C.info.notes)
i.createdBy = UserInfo_.userinfo.email;
C.inUse.clear();
if(C.deviceTypes.empty() || !Storage()->AreAcceptableDeviceTypes(C.deviceTypes, true)) {
if(C.deviceTypes.empty() || !StorageService()->AreAcceptableDeviceTypes(C.deviceTypes, true)) {
return BadRequest(RESTAPI::Errors::InvalidDeviceTypes);
}
@@ -164,7 +163,7 @@ namespace OpenWifi{
if(DB_.CreateRecord(C)) {
DB_.GetRecord("id", C.info.id, C);
if(!C.managementPolicy.empty())
Storage()->PolicyDB().AddInUse("id",C.managementPolicy,DB_.Prefix(), C.info.id);
StorageService()->PolicyDB().AddInUse("id",C.managementPolicy,DB_.Prefix(), C.info.id);
Poco::JSON::Object Answer;
C.to_json(Answer);
@@ -186,7 +185,7 @@ namespace OpenWifi{
return BadRequest(RESTAPI::Errors::InvalidJSONDocument);
}
if(!NewConfig.deviceTypes.empty() && !Storage()->AreAcceptableDeviceTypes(NewConfig.deviceTypes, true)) {
if(!NewConfig.deviceTypes.empty() && !StorageService()->AreAcceptableDeviceTypes(NewConfig.deviceTypes, true)) {
return BadRequest(RESTAPI::Errors::InvalidDeviceTypes);
}
@@ -207,7 +206,7 @@ namespace OpenWifi{
std::string MovePolicy;
bool MovingPolicy=false;
if(AssignIfPresent(ParsedObj,"managementPolicy",MovePolicy)) {
if(!MovePolicy.empty() && !Storage()->PolicyDB().Exists("id",NewConfig.managementPolicy)) {
if(!MovePolicy.empty() && !StorageService()->PolicyDB().Exists("id",NewConfig.managementPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
MovingPolicy = NewConfig.managementPolicy != Existing.managementPolicy;
@@ -229,9 +228,9 @@ namespace OpenWifi{
if(DB_.UpdateRecord("id",UUID,Existing)) {
if(MovingPolicy) {
if(!Existing.managementPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,DB_.Prefix(),Existing.info.id);
StorageService()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,DB_.Prefix(),Existing.info.id);
if(!MovePolicy.empty())
Storage()->PolicyDB().AddInUse("id",MovePolicy,DB_.Prefix(),Existing.info.id);
StorageService()->PolicyDB().AddInUse("id",MovePolicy,DB_.Prefix(),Existing.info.id);
Existing.managementPolicy = MovePolicy;
}
DB_.UpdateRecord("id", UUID, Existing);

View File

@@ -8,10 +8,8 @@
#ifndef OWPROV_RESTAPI_CONFIGURATIONS_HANDLER_H
#define OWPROV_RESTAPI_CONFIGURATIONS_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "RESTAPI_ProvObjects.h"
#include "framework/MicroService.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
namespace OpenWifi {
@@ -25,7 +23,7 @@ namespace OpenWifi {
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal),
DB_(Storage()->ConfigurationDB()){}
DB_(StorageService()->ConfigurationDB()){}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/configurations/{uuid}"}; };

View File

@@ -4,8 +4,7 @@
#include "RESTAPI_configurations_list_handler.h"
#include "framework/Utils.h"
#include "RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "framework/RESTAPI_errors.h"
@@ -16,7 +15,7 @@ namespace OpenWifi{
ProvObjects::DeviceConfigurationVec Configs;
for(const auto &i:DevUUIDS) {
ProvObjects::DeviceConfiguration E;
if(Storage()->ConfigurationDB().GetRecord("id",i,E)) {
if(StorageService()->ConfigurationDB().GetRecord("id",i,E)) {
Configs.push_back(E);
} else {
return BadRequest(RESTAPI::Errors::UnknownId + " (" + i + ")");
@@ -25,11 +24,11 @@ namespace OpenWifi{
return ReturnObject("configurations", Configs);
} else if(QB_.CountOnly) {
Poco::JSON::Object Answer;
auto C = Storage()->ConfigurationDB().Count();
auto C = StorageService()->ConfigurationDB().Count();
return ReturnCountOnly(C);
} else {
ProvObjects::DeviceConfigurationVec Configs;
Storage()->ConfigurationDB().GetRecords(QB_.Offset,QB_.Limit,Configs);
StorageService()->ConfigurationDB().GetRecords(QB_.Offset,QB_.Limit,Configs);
if(QB_.AdditionalInfo) {
Poco::JSON::Array ObjArray;
@@ -41,7 +40,7 @@ namespace OpenWifi{
if(!i.managementPolicy.empty()) {
Poco::JSON::Object PolObj;
ProvObjects::ManagementPolicy Policy;
if(Storage()->PolicyDB().GetRecord("id",i.managementPolicy,Policy)) {
if(StorageService()->PolicyDB().GetRecord("id",i.managementPolicy,Policy)) {
PolObj.set( "name", Policy.info.name);
PolObj.set( "description", Policy.info.description);
}

View File

@@ -5,10 +5,7 @@
#ifndef OWPROV_RESTAPI_CONFIGURATIONS_LIST_HANDLER_H
#define OWPROV_RESTAPI_CONFIGURATIONS_LIST_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "RESTAPI_ProvObjects.h"
#include "framework/MicroService.h"
namespace OpenWifi {

View File

@@ -7,12 +7,10 @@
//
#include "RESTAPI_contact_handler.h"
#include "framework/RESTAPI_protocol.h"
#include "RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "Poco/JSON/Parser.h"
#include "Daemon.h"
#include "framework/RESTAPI_errors.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "RESTAPI_db_helpers.h"
namespace OpenWifi{
@@ -31,7 +29,7 @@ namespace OpenWifi{
Storage::ExpandedListMap M;
std::vector<std::string> Errors;
Poco::JSON::Object Inner;
if(Storage()->ExpandInUse(Existing.inUse,M,Errors)) {
if(StorageService()->ExpandInUse(Existing.inUse,M,Errors)) {
for(const auto &[type,list]:M) {
Poco::JSON::Array ObjList;
for(const auto &i:list.entries) {
@@ -71,7 +69,7 @@ namespace OpenWifi{
if(DB_.DeleteRecord("id",UUID)) {
if(!Existing.entity.empty())
Storage()->EntityDB().DeleteLocation("id",Existing.entity,UUID);
StorageService()->EntityDB().DeleteLocation("id",Existing.entity,UUID);
return OK();
}
InternalError(RESTAPI::Errors::CouldNotBeDeleted);
@@ -90,26 +88,26 @@ namespace OpenWifi{
return BadRequest(RESTAPI::Errors::InvalidJSONDocument);
}
if(NewObject.entity.empty() || !Storage()->EntityDB().Exists("id",NewObject.entity)) {
if(NewObject.entity.empty() || !StorageService()->EntityDB().Exists("id",NewObject.entity)) {
return BadRequest(RESTAPI::Errors::EntityMustExist);
}
if(!NewObject.managementPolicy.empty() && !Storage()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
if(!NewObject.managementPolicy.empty() && !StorageService()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
NewObject.info.id = Daemon()->CreateUUID();
NewObject.info.id = MicroService::instance().CreateUUID();
NewObject.info.created = NewObject.info.modified = std::time(nullptr);
NewObject.inUse.clear();
if(DB_.CreateRecord(NewObject)) {
Storage()->EntityDB().AddContact("id",NewObject.entity,NewObject.info.id);
StorageService()->EntityDB().AddContact("id",NewObject.entity,NewObject.info.id);
if(!NewObject.managementPolicy.empty())
Storage()->PolicyDB().AddInUse("id",NewObject.managementPolicy,DB_.Prefix(),NewObject.info.id);
StorageService()->PolicyDB().AddInUse("id",NewObject.managementPolicy,DB_.Prefix(),NewObject.info.id);
ProvObjects::Contact NewContact;
Storage()->ContactDB().GetRecord("id", NewObject.info.id, NewContact);
StorageService()->ContactDB().GetRecord("id", NewObject.info.id, NewContact);
Poco::JSON::Object Answer;
NewContact.to_json(Answer);
@@ -140,7 +138,7 @@ namespace OpenWifi{
std::string MoveToPolicy, MoveFromPolicy;
bool MovingPolicy=false;
if(AssignIfPresent(RawObject,"managementPolicy",MoveToPolicy)) {
if(!MoveToPolicy.empty() && !Storage()->PolicyDB().Exists("id",MoveToPolicy)) {
if(!MoveToPolicy.empty() && !StorageService()->PolicyDB().Exists("id",MoveToPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
MoveFromPolicy = Existing.managementPolicy;
@@ -150,7 +148,7 @@ namespace OpenWifi{
std::string MoveToEntity,MoveFromEntity;
bool MovingEntity=false;
if(AssignIfPresent(RawObject,"entity",MoveToEntity)) {
if(!MoveToEntity.empty() && !Storage()->EntityDB().Exists("id",MoveToEntity)) {
if(!MoveToEntity.empty() && !StorageService()->EntityDB().Exists("id",MoveToEntity)) {
return BadRequest(RESTAPI::Errors::EntityMustExist);
}
MoveFromEntity = Existing.entity;
@@ -183,17 +181,17 @@ namespace OpenWifi{
if(MovingPolicy) {
if(!MoveFromPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",MoveFromPolicy,DB_.Prefix(),Existing.info.id);
StorageService()->PolicyDB().DeleteInUse("id",MoveFromPolicy,DB_.Prefix(),Existing.info.id);
if(!MoveToPolicy.empty())
Storage()->PolicyDB().AddInUse("id", MoveToPolicy, DB_.Prefix(), Existing.info.id);
StorageService()->PolicyDB().AddInUse("id", MoveToPolicy, DB_.Prefix(), Existing.info.id);
}
if(MovingEntity) {
if(!MoveFromEntity.empty()) {
Storage()->EntityDB().DeleteContact("id", MoveFromEntity, Existing.info.id);
StorageService()->EntityDB().DeleteContact("id", MoveFromEntity, Existing.info.id);
}
if(!MoveToEntity.empty()) {
Storage()->EntityDB().AddContact("id", MoveToEntity, Existing.info.id);
StorageService()->EntityDB().AddContact("id", MoveToEntity, Existing.info.id);
}
}

View File

@@ -9,11 +9,8 @@
#ifndef OWPROV_RESTAPI_CONTACT_HANDLER_H
#define OWPROV_RESTAPI_CONTACT_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "framework/MicroService.h"
#include "StorageService.h"
#include "framework/RESTAPI_GenericServer.h"
namespace OpenWifi {
class RESTAPI_contact_handler : public RESTAPIHandler {
@@ -26,7 +23,7 @@ namespace OpenWifi {
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal),
DB_(Storage()->ContactDB()){}
DB_(StorageService()->ContactDB()){}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/contact/{uuid}"}; };
private:

View File

@@ -3,8 +3,9 @@
//
#include "RESTAPI_contact_list_handler.h"
#include "framework/Utils.h"
#include "RESTAPI_ProvObjects.h"
#include "framework/MicroService.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "framework/RESTAPI_errors.h"
#include "RESTAPI_db_helpers.h"
@@ -17,7 +18,7 @@ namespace OpenWifi{
for(const auto &i:DevUUIDS) {
ProvObjects::Contact E;
Poco::JSON::Object Obj;
if(Storage()->ContactDB().GetRecord("id",i,E)) {
if(StorageService()->ContactDB().GetRecord("id",i,E)) {
E.to_json(Obj);
if(QB_.AdditionalInfo)
AddContactExtendedInfo(E, Obj);
@@ -31,12 +32,12 @@ namespace OpenWifi{
return ReturnObject(Answer);
} else if(QB_.CountOnly) {
Poco::JSON::Object Answer;
auto C = Storage()->ContactDB().Count();
auto C = StorageService()->ContactDB().Count();
ReturnCountOnly(C);
return;
} else {
ProvObjects::ContactVec Contacts;
Storage()->ContactDB().GetRecords(QB_.Offset,QB_.Limit,Contacts);
StorageService()->ContactDB().GetRecords(QB_.Offset,QB_.Limit,Contacts);
Poco::JSON::Array ObjArray;
for(const auto &i:Contacts) {
Poco::JSON::Object Obj;

View File

@@ -5,10 +5,8 @@
#ifndef OWPROV_RESTAPI_CONTACT_LIST_HANDLER_H
#define OWPROV_RESTAPI_CONTACT_LIST_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "RESTAPI_ProvObjects.h"
#include "framework/MicroService.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
namespace OpenWifi {

View File

@@ -12,7 +12,7 @@ namespace OpenWifi {
if(!T.entity.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Entity Entity;
if(Storage()->EntityDB().GetRecord("id",T.entity,Entity)) {
if(StorageService()->EntityDB().GetRecord("id",T.entity,Entity)) {
EntObj.set( "name", Entity.info.name);
EntObj.set( "description", Entity.info.description);
}
@@ -21,7 +21,7 @@ namespace OpenWifi {
if(!T.managementPolicy.empty()) {
Poco::JSON::Object PolObj;
ProvObjects::ManagementPolicy Policy;
if(Storage()->PolicyDB().GetRecord("id",T.managementPolicy,Policy)) {
if(StorageService()->PolicyDB().GetRecord("id",T.managementPolicy,Policy)) {
PolObj.set( "name", Policy.info.name);
PolObj.set( "description", Policy.info.description);
}
@@ -30,7 +30,7 @@ namespace OpenWifi {
if(!T.venue.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Venue Venue;
if(Storage()->VenueDB().GetRecord("id",T.venue,Venue)) {
if(StorageService()->VenueDB().GetRecord("id",T.venue,Venue)) {
EntObj.set( "name", Venue.info.name);
EntObj.set( "description", Venue.info.description);
}
@@ -39,7 +39,7 @@ namespace OpenWifi {
if(!T.contact.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Contact Contact;
if(Storage()->ContactDB().GetRecord("id",T.contact,Contact)) {
if(StorageService()->ContactDB().GetRecord("id",T.contact,Contact)) {
EntObj.set( "name", Contact.info.name);
EntObj.set( "description", Contact.info.description);
}
@@ -48,7 +48,7 @@ namespace OpenWifi {
if(!T.location.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Location Location;
if(Storage()->LocationDB().GetRecord("id",T.location,Location)) {
if(StorageService()->LocationDB().GetRecord("id",T.location,Location)) {
EntObj.set( "name", Location.info.name);
EntObj.set( "description", Location.info.description);
}
@@ -57,7 +57,7 @@ namespace OpenWifi {
if(!T.deviceConfiguration.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::DeviceConfiguration DevConf;
if(Storage()->ConfigurationDB().GetRecord("id",T.deviceConfiguration,DevConf)) {
if(StorageService()->ConfigurationDB().GetRecord("id",T.deviceConfiguration,DevConf)) {
EntObj.set( "name", DevConf.info.name);
EntObj.set( "description", DevConf.info.description);
}
@@ -72,7 +72,7 @@ namespace OpenWifi {
if(!T.entity.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Entity Entity;
if(Storage()->EntityDB().GetRecord("id",T.entity,Entity)) {
if(StorageService()->EntityDB().GetRecord("id",T.entity,Entity)) {
EntObj.set( "name", Entity.info.name);
EntObj.set( "description", Entity.info.description);
}
@@ -82,7 +82,7 @@ namespace OpenWifi {
if(!T.managementPolicy.empty()) {
Poco::JSON::Object PolObj;
ProvObjects::ManagementPolicy Policy;
if(Storage()->PolicyDB().GetRecord("id",T.managementPolicy,Policy)) {
if(StorageService()->PolicyDB().GetRecord("id",T.managementPolicy,Policy)) {
PolObj.set( "name", Policy.info.name);
PolObj.set( "description", Policy.info.description);
}
@@ -97,7 +97,7 @@ namespace OpenWifi {
if(!T.entity.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Entity Entity;
if(Storage()->EntityDB().GetRecord("id",T.entity,Entity)) {
if(StorageService()->EntityDB().GetRecord("id",T.entity,Entity)) {
EntObj.set( "name", Entity.info.name);
EntObj.set( "description", Entity.info.description);
}
@@ -107,7 +107,7 @@ namespace OpenWifi {
if(!T.managementPolicy.empty()) {
Poco::JSON::Object PolObj;
ProvObjects::ManagementPolicy Policy;
if(Storage()->PolicyDB().GetRecord("id",T.managementPolicy,Policy)) {
if(StorageService()->PolicyDB().GetRecord("id",T.managementPolicy,Policy)) {
PolObj.set( "name", Policy.info.name);
PolObj.set( "description", Policy.info.description);
}
@@ -122,7 +122,7 @@ namespace OpenWifi {
if(!T.managementPolicy.empty()) {
Poco::JSON::Object PolObj;
ProvObjects::ManagementPolicy Policy;
if(Storage()->PolicyDB().GetRecord("id",T.managementPolicy,Policy)) {
if(StorageService()->PolicyDB().GetRecord("id",T.managementPolicy,Policy)) {
PolObj.set( "name", Policy.info.name);
PolObj.set( "description", Policy.info.description);
}
@@ -131,7 +131,7 @@ namespace OpenWifi {
if(!T.deviceConfiguration.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::DeviceConfiguration DevConf;
if(Storage()->ConfigurationDB().GetRecord("id",T.deviceConfiguration,DevConf)) {
if(StorageService()->ConfigurationDB().GetRecord("id",T.deviceConfiguration,DevConf)) {
EntObj.set( "name", DevConf.info.name);
EntObj.set( "description", DevConf.info.description);
}
@@ -146,7 +146,7 @@ namespace OpenWifi {
if(!T.entity.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Entity Entity;
if(Storage()->EntityDB().GetRecord("id",T.entity,Entity)) {
if(StorageService()->EntityDB().GetRecord("id",T.entity,Entity)) {
EntObj.set( "name", Entity.info.name);
EntObj.set( "description", Entity.info.description);
}
@@ -155,7 +155,7 @@ namespace OpenWifi {
if(!T.managementPolicy.empty()) {
Poco::JSON::Object PolObj;
ProvObjects::ManagementPolicy Policy;
if(Storage()->PolicyDB().GetRecord("id",T.managementPolicy,Policy)) {
if(StorageService()->PolicyDB().GetRecord("id",T.managementPolicy,Policy)) {
PolObj.set( "name", Policy.info.name);
PolObj.set( "description", Policy.info.description);
}
@@ -164,7 +164,7 @@ namespace OpenWifi {
if(!T.contact.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Contact Contact;
if(Storage()->ContactDB().GetRecord("id",T.contact,Contact)) {
if(StorageService()->ContactDB().GetRecord("id",T.contact,Contact)) {
EntObj.set( "name", Contact.info.name);
EntObj.set( "description", Contact.info.description);
}
@@ -173,7 +173,7 @@ namespace OpenWifi {
if(!T.location.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Location Location;
if(Storage()->LocationDB().GetRecord("id",T.location,Location)) {
if(StorageService()->LocationDB().GetRecord("id",T.location,Location)) {
EntObj.set( "name", Location.info.name);
EntObj.set( "description", Location.info.description);
}
@@ -182,7 +182,7 @@ namespace OpenWifi {
if(!T.deviceConfiguration.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::DeviceConfiguration DevConf;
if(Storage()->ConfigurationDB().GetRecord("id",T.deviceConfiguration,DevConf)) {
if(StorageService()->ConfigurationDB().GetRecord("id",T.deviceConfiguration,DevConf)) {
EntObj.set( "name", DevConf.info.name);
EntObj.set( "description", DevConf.info.description);
}
@@ -197,7 +197,7 @@ namespace OpenWifi {
if(!T.entity.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Entity Entity;
if(Storage()->EntityDB().GetRecord("id",T.entity,Entity)) {
if(StorageService()->EntityDB().GetRecord("id",T.entity,Entity)) {
EntObj.set( "name", Entity.info.name);
EntObj.set( "description", Entity.info.description);
}
@@ -212,7 +212,7 @@ namespace OpenWifi {
if(!T.entity.empty()) {
Poco::JSON::Object EntObj;
ProvObjects::Entity Entity;
if(Storage()->EntityDB().GetRecord("id",T.entity,Entity)) {
if(StorageService()->EntityDB().GetRecord("id",T.entity,Entity)) {
EntObj.set( "name", Entity.info.name);
EntObj.set( "description", Entity.info.description);
}
@@ -221,7 +221,7 @@ namespace OpenWifi {
if(!T.managementPolicy.empty()) {
Poco::JSON::Object PolObj;
ProvObjects::ManagementPolicy P;
if(Storage()->PolicyDB().GetRecord("id",T.managementPolicy,P)) {
if(StorageService()->PolicyDB().GetRecord("id",T.managementPolicy,P)) {
PolObj.set( "name", P.info.name);
PolObj.set( "description", P.info.description);
}

View File

@@ -5,7 +5,7 @@
#ifndef OWPROV_RESTAPI_DB_HELPERS_H
#define OWPROV_RESTAPI_DB_HELPERS_H
#include "RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
namespace OpenWifi {

View File

@@ -8,15 +8,10 @@
#include "RESTAPI_entity_handler.h"
#include "RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "Poco/JSON/Parser.h"
#include "Daemon.h"
#include "RESTAPI_SecurityObjects.h"
#include "framework/RESTAPI_utils.h"
#include "framework/RESTAPI_errors.h"
#include "framework/CIDRUtils.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_SecurityObjects.h"
#include "StorageService.h"
#include "RESTAPI_db_helpers.h"
namespace OpenWifi{
@@ -51,14 +46,14 @@ namespace OpenWifi{
}
if(!Existing.deviceConfiguration.empty())
Storage()->ConfigurationDB().DeleteInUse("id", Existing.deviceConfiguration, DB_.Prefix(), Existing.info.id);
StorageService()->ConfigurationDB().DeleteInUse("id", Existing.deviceConfiguration, DB_.Prefix(), Existing.info.id);
for(auto &i:Existing.locations) {
Storage()->LocationDB().DeleteInUse("id", i, DB_.Prefix(), Existing.info.id);
StorageService()->LocationDB().DeleteInUse("id", i, DB_.Prefix(), Existing.info.id);
}
for(auto &i:Existing.contacts) {
Storage()->ContactDB().DeleteInUse("id", i, DB_.Prefix(), Existing.info.id);
StorageService()->ContactDB().DeleteInUse("id", i, DB_.Prefix(), Existing.info.id);
}
if(DB_.DeleteRecord("id",UUID)) {
@@ -90,18 +85,18 @@ namespace OpenWifi{
// When creating an entity, it cannot have any relations other that parent, notes, name, description. Everything else
// must be conveyed through PUT.
NewEntity.info.id = (UUID==EntityDB::RootUUID()) ? UUID : Daemon()->CreateUUID() ;
NewEntity.info.id = (UUID==EntityDB::RootUUID()) ? UUID : MicroService::instance().CreateUUID() ;
if(UUID==EntityDB::RootUUID()) {
NewEntity.parent="";
} else if(NewEntity.parent.empty() || !DB_.Exists("id",NewEntity.parent)) {
return BadRequest(RESTAPI::Errors::ParentUUIDMustExist);
}
if(!NewEntity.deviceConfiguration.empty() && !Storage()->ConfigurationDB().Exists("id",NewEntity.deviceConfiguration)) {
if(!NewEntity.deviceConfiguration.empty() && !StorageService()->ConfigurationDB().Exists("id",NewEntity.deviceConfiguration)) {
return BadRequest(RESTAPI::Errors::ConfigurationMustExist);
}
if(!NewEntity.managementPolicy.empty() && !Storage()->PolicyDB().Exists("id", NewEntity.managementPolicy)){
if(!NewEntity.managementPolicy.empty() && !StorageService()->PolicyDB().Exists("id", NewEntity.managementPolicy)){
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
@@ -154,13 +149,13 @@ namespace OpenWifi{
bool MovingConfiguration=false,
MovingManagementPolicy=false;
if(AssignIfPresent(RawObject,"deviceConfiguration",NewConfiguration)) {
if(!NewConfiguration.empty() && !Storage()->ConfigurationDB().Exists("id",NewConfiguration)) {
if(!NewConfiguration.empty() && !StorageService()->ConfigurationDB().Exists("id",NewConfiguration)) {
return BadRequest(RESTAPI::Errors::ConfigurationMustExist);
}
MovingConfiguration = Existing.deviceConfiguration != NewConfiguration;
}
if(AssignIfPresent(RawObject,"managementPolicy",NewManagementPolicy)) {
if(!NewManagementPolicy.empty() && !Storage()->PolicyDB().Exists("id",NewManagementPolicy)) {
if(!NewManagementPolicy.empty() && !StorageService()->PolicyDB().Exists("id",NewManagementPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
MovingManagementPolicy = Existing.managementPolicy != NewManagementPolicy;
@@ -179,7 +174,7 @@ namespace OpenWifi{
}
std::string Error;
if(!Storage()->Validate(Parameters_,Error)) {
if(!StorageService()->Validate(Parameters_,Error)) {
return BadRequest(Error);
}
@@ -194,32 +189,32 @@ namespace OpenWifi{
auto UUID_parts = Utils::Split(Child,':');
if(i.first=="add" && UUID_parts[0] == "con") {
DB_.AddContact("id", UUID, UUID_parts[1]);
Storage()->ContactDB().AddInUse("id",UUID_parts[1],DB_.Prefix(), UUID);
StorageService()->ContactDB().AddInUse("id",UUID_parts[1],DB_.Prefix(), UUID);
} else if (i.first == "del" && UUID_parts[0] == "con") {
DB_.DeleteContact("id", UUID, UUID_parts[1]);
Storage()->ContactDB().DeleteInUse("id",UUID_parts[1],DB_.Prefix(),UUID);
StorageService()->ContactDB().DeleteInUse("id",UUID_parts[1],DB_.Prefix(),UUID);
} else if (i.first == "add" && UUID_parts[0] == "loc") {
DB_.AddLocation("id", UUID, UUID_parts[1]);
Storage()->LocationDB().AddInUse("id",UUID_parts[1],DB_.Prefix(),UUID);
StorageService()->LocationDB().AddInUse("id",UUID_parts[1],DB_.Prefix(),UUID);
} else if (i.first == "del" && UUID_parts[0] == "loc") {
DB_.DeleteLocation("id", UUID, UUID_parts[1]);
Storage()->LocationDB().DeleteInUse("id",UUID_parts[1],DB_.Prefix(),UUID);
StorageService()->LocationDB().DeleteInUse("id",UUID_parts[1],DB_.Prefix(),UUID);
}
}
if(MovingConfiguration) {
if(!Existing.deviceConfiguration.empty())
Storage()->ConfigurationDB().DeleteInUse("id",Existing.deviceConfiguration,DB_.Prefix(),Existing.info.id);
StorageService()->ConfigurationDB().DeleteInUse("id",Existing.deviceConfiguration,DB_.Prefix(),Existing.info.id);
if(!NewConfiguration.empty())
Storage()->ConfigurationDB().AddInUse("id",NewConfiguration,DB_.Prefix(),Existing.info.id);
StorageService()->ConfigurationDB().AddInUse("id",NewConfiguration,DB_.Prefix(),Existing.info.id);
Existing.deviceConfiguration = NewConfiguration;
}
if(MovingManagementPolicy) {
if(!Existing.managementPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",Existing.managementPolicy, DB_.Prefix(), Existing.info.id);
StorageService()->PolicyDB().DeleteInUse("id",Existing.managementPolicy, DB_.Prefix(), Existing.info.id);
if(!NewManagementPolicy.empty())
Storage()->PolicyDB().AddInUse("id", NewManagementPolicy, DB_.Prefix(), Existing.info.id);
StorageService()->PolicyDB().AddInUse("id", NewManagementPolicy, DB_.Prefix(), Existing.info.id);
Existing.managementPolicy = NewManagementPolicy;
}
@@ -227,7 +222,7 @@ namespace OpenWifi{
Poco::JSON::Object Answer;
ProvObjects::Entity NewRecord;
Storage()->EntityDB().GetRecord("id",UUID, NewRecord);
StorageService()->EntityDB().GetRecord("id",UUID, NewRecord);
NewRecord.to_json(Answer);
return ReturnObject(Answer);
}

View File

@@ -10,10 +10,7 @@
#ifndef OWPROV_RESTAPI_ENTITY_HANDLER_H
#define OWPROV_RESTAPI_ENTITY_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "framework/MicroService.h"
#include "StorageService.h"
namespace OpenWifi {
@@ -22,12 +19,14 @@ namespace OpenWifi {
RESTAPI_entity_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal)
: RESTAPIHandler(bindings, L,
std::vector<std::string>{
Poco::Net::HTTPRequest::HTTP_GET, Poco::Net::HTTPRequest::HTTP_POST,
Poco::Net::HTTPRequest::HTTP_PUT, Poco::Net::HTTPRequest::HTTP_DELETE,
Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_POST,
Poco::Net::HTTPRequest::HTTP_PUT,
Poco::Net::HTTPRequest::HTTP_DELETE,
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal),
DB_(Storage()->EntityDB()){}
DB_(StorageService()->EntityDB()){}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/entity/{uuid}"}; };
private:

View File

@@ -6,12 +6,9 @@
// Arilia Wireless Inc.
//
#include "framework/MicroService.h"
#include "RESTAPI_entity_list_handler.h"
#include "framework/Utils.h"
#include "StorageService.h"
#include "framework/RESTAPI_utils.h"
#include "framework/RESTAPI_errors.h"
#include "RESTAPI_db_helpers.h"
namespace OpenWifi{
@@ -23,7 +20,7 @@ namespace OpenWifi{
ProvObjects::EntityVec Entities;
for(const auto &i:EntityUIDs) {
ProvObjects::Entity E;
if(Storage()->EntityDB().GetRecord("id",i,E)) {
if(StorageService()->EntityDB().GetRecord("id",i,E)) {
Entities.push_back(E);
} else {
return BadRequest(RESTAPI::Errors::UnknownId + " (" + i + ")");
@@ -31,15 +28,15 @@ namespace OpenWifi{
}
return ReturnObject("entities", Entities);
} else if(QB_.CountOnly) {
auto C = Storage()->EntityDB().Count();
auto C = StorageService()->EntityDB().Count();
return ReturnCountOnly(C);
} if (HasParameter("getTree",Arg) && Arg=="true") {
Poco::JSON::Object FullTree;
Storage()->EntityDB().BuildTree(FullTree);
StorageService()->EntityDB().BuildTree(FullTree);
return ReturnObject(FullTree);
} else {
ProvObjects::EntityVec Entities;
Storage()->EntityDB().GetRecords(QB_.Offset, QB_.Limit,Entities);
StorageService()->EntityDB().GetRecords(QB_.Offset, QB_.Limit,Entities);
Poco::JSON::Array ObjArray;
for(const auto &i:Entities) {
Poco::JSON::Object O;
@@ -58,7 +55,7 @@ namespace OpenWifi{
std::string Arg;
if (HasParameter("setTree",Arg) && Arg=="true") {
auto FullTree = ParseStream();
Storage()->EntityDB().ImportTree(FullTree);
StorageService()->EntityDB().ImportTree(FullTree);
return OK();
}
BadRequest(RESTAPI::Errors::MissingOrInvalidParameters);

View File

@@ -10,9 +10,7 @@
#ifndef OWPROV_RESTAPI_ENTITY_LIST_HANDLER_H
#define OWPROV_RESTAPI_ENTITY_LIST_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "framework/MicroService.h"
namespace OpenWifi {
class RESTAPI_entity_list_handler : public RESTAPIHandler {

View File

@@ -8,11 +8,9 @@
#include "RESTAPI_inventory_handler.h"
#include "framework/RESTAPI_protocol.h"
#include "StorageService.h"
#include "Poco/JSON/Parser.h"
#include "Daemon.h"
#include "framework/RESTAPI_utils.h"
#include "APConfig.h"
#include "framework/RESTAPI_errors.h"
#include "AutoDiscovery.h"
@@ -61,7 +59,7 @@ namespace OpenWifi{
std::string firmwareUpgrade = AutoDiscovery()->firmwareUpgrade();
bool firmwareRCOnly = AutoDiscovery()->firmwareRCOnly();
Storage()->InventoryDB().FindFirmwareOptions(SerialNumber,firmwareUpgrade, firmwareRCOnly);
StorageService()->InventoryDB().FindFirmwareOptions(SerialNumber,firmwareUpgrade, firmwareRCOnly);
Answer.set("firmwareUpgrade",firmwareUpgrade);
Answer.set("firmwareRCOnly", firmwareRCOnly);
@@ -112,19 +110,19 @@ namespace OpenWifi{
}
if(!Existing.venue.empty())
Storage()->VenueDB().DeleteDevice("id",Existing.venue,Existing.info.id);
StorageService()->VenueDB().DeleteDevice("id",Existing.venue,Existing.info.id);
if(!Existing.entity.empty())
Storage()->EntityDB().DeleteDevice("id",Existing.entity,Existing.info.id);
StorageService()->EntityDB().DeleteDevice("id",Existing.entity,Existing.info.id);
if(!Existing.location.empty())
Storage()->LocationDB().DeleteInUse("id",Existing.location,DB_.Prefix(),Existing.info.id);
StorageService()->LocationDB().DeleteInUse("id",Existing.location,DB_.Prefix(),Existing.info.id);
if(!Existing.contact.empty())
Storage()->ContactDB().DeleteInUse("id",Existing.contact,DB_.Prefix(),Existing.info.id);
StorageService()->ContactDB().DeleteInUse("id",Existing.contact,DB_.Prefix(),Existing.info.id);
if(!Existing.deviceConfiguration.empty())
Storage()->ConfigurationDB().DeleteInUse("id", Existing.deviceConfiguration, DB_.Prefix(), Existing.info.id);
StorageService()->ConfigurationDB().DeleteInUse("id", Existing.deviceConfiguration, DB_.Prefix(), Existing.info.id);
if(DB_.DeleteRecord("id", Existing.info.id)) {
DB_.DeleteRecord(RESTAPI::Protocol::ID, Existing.info.id);
@@ -159,15 +157,15 @@ namespace OpenWifi{
return BadRequest( RESTAPI::Errors::NameMustBeSet);
}
if(NewObject.deviceType.empty() || !Storage()->IsAcceptableDeviceType(NewObject.deviceType)) {
if(NewObject.deviceType.empty() || !StorageService()->IsAcceptableDeviceType(NewObject.deviceType)) {
return BadRequest(RESTAPI::Errors::InvalidDeviceTypes);
}
if(OpenWifi::EntityDB::IsRoot(NewObject.entity) || (!NewObject.entity.empty() && !Storage()->EntityDB().Exists("id",NewObject.entity))) {
if(OpenWifi::EntityDB::IsRoot(NewObject.entity) || (!NewObject.entity.empty() && !StorageService()->EntityDB().Exists("id",NewObject.entity))) {
return BadRequest(RESTAPI::Errors::ValidNonRootUUID);
}
if(!NewObject.venue.empty() && !Storage()->VenueDB().Exists("id",NewObject.venue)) {
if(!NewObject.venue.empty() && !StorageService()->VenueDB().Exists("id",NewObject.venue)) {
return BadRequest(RESTAPI::Errors::VenueMustExist);
}
@@ -175,39 +173,39 @@ namespace OpenWifi{
return BadRequest(RESTAPI::Errors::NotBoth);
}
if(!NewObject.location.empty() && !Storage()->LocationDB().Exists("id",NewObject.location)) {
if(!NewObject.location.empty() && !StorageService()->LocationDB().Exists("id",NewObject.location)) {
return BadRequest(RESTAPI::Errors::LocationMustExist);
}
if(!NewObject.contact.empty() && !Storage()->ContactDB().Exists("id",NewObject.contact)) {
if(!NewObject.contact.empty() && !StorageService()->ContactDB().Exists("id",NewObject.contact)) {
return BadRequest(RESTAPI::Errors::ContactMustExist);
}
if(!NewObject.deviceConfiguration.empty() && !Storage()->ConfigurationDB().Exists("id",NewObject.deviceConfiguration)) {
if(!NewObject.deviceConfiguration.empty() && !StorageService()->ConfigurationDB().Exists("id",NewObject.deviceConfiguration)) {
return BadRequest(RESTAPI::Errors::ConfigurationMustExist);
}
if(!NewObject.managementPolicy.empty() && !Storage()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
if(!NewObject.managementPolicy.empty() && !StorageService()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
NewObject.info.modified = NewObject.info.created = std::time(nullptr);
NewObject.info.id = Daemon()->CreateUUID();
NewObject.info.id = MicroService::instance().CreateUUID();
if(DB_.CreateRecord(NewObject)) {
SerialNumberCache()->AddSerialNumber(SerialNumber);
if (!NewObject.venue.empty())
Storage()->VenueDB().AddDevice("id",NewObject.venue,NewObject.info.id);
StorageService()->VenueDB().AddDevice("id",NewObject.venue,NewObject.info.id);
if (!NewObject.entity.empty())
Storage()->EntityDB().AddDevice("id",NewObject.entity,NewObject.info.id);
StorageService()->EntityDB().AddDevice("id",NewObject.entity,NewObject.info.id);
if (!NewObject.location.empty())
Storage()->LocationDB().AddInUse("id",NewObject.location,DB_.Prefix(),NewObject.info.id);
StorageService()->LocationDB().AddInUse("id",NewObject.location,DB_.Prefix(),NewObject.info.id);
if (!NewObject.contact.empty())
Storage()->ContactDB().AddInUse("id",NewObject.contact,DB_.Prefix(),NewObject.info.id);
StorageService()->ContactDB().AddInUse("id",NewObject.contact,DB_.Prefix(),NewObject.info.id);
if (!NewObject.deviceConfiguration.empty())
Storage()->ConfigurationDB().AddInUse("id",NewObject.deviceConfiguration,DB_.Prefix(),NewObject.info.id);
StorageService()->ConfigurationDB().AddInUse("id",NewObject.deviceConfiguration,DB_.Prefix(),NewObject.info.id);
if (!NewObject.managementPolicy.empty())
Storage()->PolicyDB().AddInUse("id",NewObject.managementPolicy,DB_.Prefix(),NewObject.info.id);
StorageService()->PolicyDB().AddInUse("id",NewObject.managementPolicy,DB_.Prefix(),NewObject.info.id);
ProvObjects::InventoryTag NewTag;
DB_.GetRecord("id",NewObject.info.id,NewTag);
@@ -232,7 +230,7 @@ namespace OpenWifi{
}
if(!NewObject.deviceType.empty()) {
if(!Storage()->IsAcceptableDeviceType(NewObject.deviceType)) {
if(!StorageService()->IsAcceptableDeviceType(NewObject.deviceType)) {
return BadRequest(RESTAPI::Errors::InvalidDeviceTypes);
}
}
@@ -253,14 +251,14 @@ namespace OpenWifi{
AssignIfPresent(RawObject, "rrm",Existing.rrm);
if(AssignIfPresent(RawObject, "venue",NewVenue)) {
if(!NewVenue.empty() && !Storage()->VenueDB().Exists("id",NewVenue)) {
if(!NewVenue.empty() && !StorageService()->VenueDB().Exists("id",NewVenue)) {
return BadRequest(RESTAPI::Errors::VenueMustExist);
}
MovingVenue = Existing.venue != NewVenue;
}
if(AssignIfPresent(RawObject, "entity",NewEntity)) {
if(!NewEntity.empty() && !Storage()->EntityDB().Exists("id",NewEntity)) {
if(!NewEntity.empty() && !StorageService()->EntityDB().Exists("id",NewEntity)) {
return BadRequest(RESTAPI::Errors::EntityMustExist);
}
MovingEntity = Existing.entity != NewEntity;
@@ -271,28 +269,28 @@ namespace OpenWifi{
}
if(AssignIfPresent(RawObject, "location",NewLocation)) {
if(!NewLocation.empty() && !Storage()->LocationDB().Exists("id",NewLocation)) {
if(!NewLocation.empty() && !StorageService()->LocationDB().Exists("id",NewLocation)) {
return BadRequest(RESTAPI::Errors::LocationMustExist);
}
MovingLocation = Existing.location != NewLocation;
}
if(AssignIfPresent(RawObject, "contact",NewContact)) {
if(!NewContact.empty() && !Storage()->ContactDB().Exists("id",NewContact)) {
if(!NewContact.empty() && !StorageService()->ContactDB().Exists("id",NewContact)) {
return BadRequest(RESTAPI::Errors::ContactMustExist);
}
MovingContact = Existing.contact != NewContact;
}
if(AssignIfPresent(RawObject, "deviceConfiguration",NewConfiguration)) {
if(!NewConfiguration.empty() && !Storage()->ConfigurationDB().Exists("id",NewConfiguration)) {
if(!NewConfiguration.empty() && !StorageService()->ConfigurationDB().Exists("id",NewConfiguration)) {
return BadRequest(RESTAPI::Errors::ConfigurationMustExist);
}
MovingConfiguration = Existing.deviceConfiguration != NewConfiguration;
}
if(AssignIfPresent(RawObject, "managementPolicy",NewPolicy)) {
if(!NewPolicy.empty() && !Storage()->PolicyDB().Exists("id",NewPolicy)) {
if(!NewPolicy.empty() && !StorageService()->PolicyDB().Exists("id",NewPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
MovingPolicy = Existing.managementPolicy != NewPolicy;
@@ -303,18 +301,18 @@ namespace OpenWifi{
if(HasParameter("unassign", Arg) && Arg=="true") {
UnAssign=true;
if(!Existing.venue.empty()) {
Storage()->VenueDB().DeleteDevice("id",Existing.venue,Existing.info.id);
StorageService()->VenueDB().DeleteDevice("id",Existing.venue,Existing.info.id);
} else if(!Existing.entity.empty()) {
Storage()->EntityDB().DeleteDevice("id",Existing.entity,Existing.info.id);
StorageService()->EntityDB().DeleteDevice("id",Existing.entity,Existing.info.id);
}
if(!Existing.location.empty())
Storage()->LocationDB().DeleteInUse("id",Existing.location,DB_.Prefix(),Existing.info.id);
StorageService()->LocationDB().DeleteInUse("id",Existing.location,DB_.Prefix(),Existing.info.id);
if(!Existing.contact.empty())
Storage()->ContactDB().DeleteInUse("id",Existing.contact,DB_.Prefix(),Existing.info.id);
StorageService()->ContactDB().DeleteInUse("id",Existing.contact,DB_.Prefix(),Existing.info.id);
if(!Existing.deviceConfiguration.empty())
Storage()->ConfigurationDB().DeleteInUse("id",Existing.deviceConfiguration,DB_.Prefix(),Existing.info.id);
StorageService()->ConfigurationDB().DeleteInUse("id",Existing.deviceConfiguration,DB_.Prefix(),Existing.info.id);
if(!Existing.managementPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,DB_.Prefix(),Existing.info.id);
StorageService()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,DB_.Prefix(),Existing.info.id);
Existing.venue.clear();
Existing.entity.clear();
Existing.deviceConfiguration.clear();
@@ -327,48 +325,48 @@ namespace OpenWifi{
AssignIfPresent(RawObject, "description", Existing.info.description);
Existing.info.modified = std::time(nullptr);
if(Storage()->InventoryDB().UpdateRecord("id", Existing.info.id, Existing)) {
if(StorageService()->InventoryDB().UpdateRecord("id", Existing.info.id, Existing)) {
if(!UnAssign) {
if(MovingEntity) {
if(!Existing.entity.empty())
Storage()->EntityDB().DeleteDevice("id",Existing.entity,Existing.info.id);
StorageService()->EntityDB().DeleteDevice("id",Existing.entity,Existing.info.id);
if(!NewEntity.empty())
Storage()->EntityDB().AddDevice("id", NewEntity, Existing.info.id);
StorageService()->EntityDB().AddDevice("id", NewEntity, Existing.info.id);
Existing.entity = NewEntity;
}
if(MovingVenue) {
if(!Existing.venue.empty())
Storage()->VenueDB().DeleteDevice("id",Existing.venue,Existing.info.id);
StorageService()->VenueDB().DeleteDevice("id",Existing.venue,Existing.info.id);
if(!NewVenue.empty())
Storage()->VenueDB().AddDevice("id", NewVenue, Existing.info.id);
StorageService()->VenueDB().AddDevice("id", NewVenue, Existing.info.id);
Existing.venue = NewVenue;
}
if(MovingConfiguration) {
if(!Existing.deviceConfiguration.empty())
Storage()->ConfigurationDB().DeleteInUse("id",Existing.deviceConfiguration,DB_.Prefix(),Existing.info.id);
StorageService()->ConfigurationDB().DeleteInUse("id",Existing.deviceConfiguration,DB_.Prefix(),Existing.info.id);
if(!NewConfiguration.empty())
Storage()->ConfigurationDB().AddInUse("id",NewConfiguration,DB_.Prefix(),Existing.info.id);
StorageService()->ConfigurationDB().AddInUse("id",NewConfiguration,DB_.Prefix(),Existing.info.id);
Existing.deviceConfiguration = NewConfiguration;
}
if(MovingContact) {
if(!Existing.contact.empty())
Storage()->ContactDB().DeleteInUse("id",Existing.contact,DB_.Prefix(),Existing.info.id);
StorageService()->ContactDB().DeleteInUse("id",Existing.contact,DB_.Prefix(),Existing.info.id);
if(!NewContact.empty())
Storage()->ContactDB().AddInUse("id",NewContact,DB_.Prefix(),Existing.info.id);
StorageService()->ContactDB().AddInUse("id",NewContact,DB_.Prefix(),Existing.info.id);
Existing.contact = NewContact;
}
if(MovingLocation) {
if(!Existing.location.empty())
Storage()->LocationDB().DeleteInUse("id",Existing.location,DB_.Prefix(),Existing.info.id);
StorageService()->LocationDB().DeleteInUse("id",Existing.location,DB_.Prefix(),Existing.info.id);
if(!NewLocation.empty())
Storage()->LocationDB().AddInUse("id",NewLocation,DB_.Prefix(),Existing.info.id);
StorageService()->LocationDB().AddInUse("id",NewLocation,DB_.Prefix(),Existing.info.id);
Existing.location = NewLocation;
}
if(MovingPolicy) {
if(!Existing.managementPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,DB_.Prefix(),Existing.info.id);
StorageService()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,DB_.Prefix(),Existing.info.id);
if(!NewPolicy.empty())
Storage()->PolicyDB().AddInUse("id",NewPolicy,DB_.Prefix(),Existing.info.id);
StorageService()->PolicyDB().AddInUse("id",NewPolicy,DB_.Prefix(),Existing.info.id);
Existing.managementPolicy = NewPolicy;
}
}

View File

@@ -10,9 +10,7 @@
#ifndef OWPROV_RESTAPI_INVENTORY_HANDLER_H
#define OWPROV_RESTAPI_INVENTORY_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "framework/MicroService.h"
#include "StorageService.h"
namespace OpenWifi {
@@ -26,7 +24,7 @@ namespace OpenWifi {
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal),
DB_(Storage()->InventoryDB()){}
DB_(StorageService()->InventoryDB()){}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/inventory/{serialNumber}"}; };
private:

View File

@@ -9,7 +9,6 @@
#include "RESTAPI_inventory_list_handler.h"
#include "StorageService.h"
#include "framework/Utils.h"
#include "framework/RESTAPI_errors.h"
#include "RESTAPI/RESTAPI_db_helpers.h"
@@ -45,7 +44,7 @@ namespace OpenWifi{
std::string OrderBy{" ORDER BY serialNumber ASC "};
if(HasParameter("orderBy",Arg)) {
if(!Storage()->InventoryDB().PrepareOrderBy(Arg,OrderBy)) {
if(!StorageService()->InventoryDB().PrepareOrderBy(Arg,OrderBy)) {
return BadRequest(RESTAPI::Errors::InvalidLOrderBy);
}
}
@@ -56,7 +55,7 @@ namespace OpenWifi{
Poco::JSON::Array ObjArr;
for(const auto &i:DevUUIDS) {
ProvObjects::InventoryTag E;
if(Storage()->InventoryDB().GetRecord("id",i,E)) {
if(StorageService()->InventoryDB().GetRecord("id",i,E)) {
Poco::JSON::Object O;
E.to_json(O);
if(QB_.AdditionalInfo)
@@ -71,38 +70,38 @@ namespace OpenWifi{
return ReturnObject( Answer);
} else if(HasParameter("entity",UUID)) {
if(QB_.CountOnly) {
auto C = Storage()->InventoryDB().Count( Storage()->InventoryDB().OP("entity",ORM::EQ,UUID));
auto C = StorageService()->InventoryDB().Count( StorageService()->InventoryDB().OP("entity",ORM::EQ,UUID));
return ReturnCountOnly( C);
}
ProvObjects::InventoryTagVec Tags;
Storage()->InventoryDB().GetRecords(QB_.Offset, QB_.Limit, Tags, Storage()->InventoryDB().OP("entity",ORM::EQ,UUID), OrderBy);
StorageService()->InventoryDB().GetRecords(QB_.Offset, QB_.Limit, Tags, StorageService()->InventoryDB().OP("entity",ORM::EQ,UUID), OrderBy);
return SendList(Tags, SerialOnly);
} else if(HasParameter("venue",UUID)) {
if(QB_.CountOnly) {
auto C = Storage()->InventoryDB().Count(Storage()->InventoryDB().OP("venue",ORM::EQ,UUID));
auto C = StorageService()->InventoryDB().Count(StorageService()->InventoryDB().OP("venue",ORM::EQ,UUID));
return ReturnCountOnly( C);
}
ProvObjects::InventoryTagVec Tags;
Storage()->InventoryDB().GetRecords(QB_.Offset, QB_.Limit, Tags, Storage()->InventoryDB().OP("venue",ORM::EQ,UUID), OrderBy);
StorageService()->InventoryDB().GetRecords(QB_.Offset, QB_.Limit, Tags, StorageService()->InventoryDB().OP("venue",ORM::EQ,UUID), OrderBy);
return SendList( Tags, SerialOnly);
} else if(HasParameter("unassigned",Arg) && Arg=="true") {
if(QB_.CountOnly) {
std::string Empty;
auto C = Storage()->InventoryDB().Count( InventoryDB::OP( Storage()->InventoryDB().OP("venue",ORM::EQ,Empty),
ORM::AND, Storage()->InventoryDB().OP("entity",ORM::EQ,Empty) ));
auto C = StorageService()->InventoryDB().Count( InventoryDB::OP( StorageService()->InventoryDB().OP("venue",ORM::EQ,Empty),
ORM::AND, StorageService()->InventoryDB().OP("entity",ORM::EQ,Empty) ));
return ReturnCountOnly(C);
}
ProvObjects::InventoryTagVec Tags;
std::string Empty;
Storage()->InventoryDB().GetRecords(QB_.Offset, QB_.Limit, Tags, InventoryDB::OP( Storage()->InventoryDB().OP("venue",ORM::EQ,Empty),
ORM::AND, Storage()->InventoryDB().OP("entity",ORM::EQ,Empty) ) , OrderBy );
StorageService()->InventoryDB().GetRecords(QB_.Offset, QB_.Limit, Tags, InventoryDB::OP( StorageService()->InventoryDB().OP("venue",ORM::EQ,Empty),
ORM::AND, StorageService()->InventoryDB().OP("entity",ORM::EQ,Empty) ) , OrderBy );
return SendList(Tags, SerialOnly);
} else if(QB_.CountOnly) {
auto C = Storage()->InventoryDB().Count();
auto C = StorageService()->InventoryDB().Count();
return ReturnCountOnly(C);
} else {
ProvObjects::InventoryTagVec Tags;
Storage()->InventoryDB().GetRecords(QB_.Offset,QB_.Limit,Tags,"",OrderBy);
StorageService()->InventoryDB().GetRecords(QB_.Offset,QB_.Limit,Tags,"",OrderBy);
Poco::JSON::Array Arr;
for(const auto &i:Tags) {

View File

@@ -10,11 +10,8 @@
#ifndef OWPROV_RESTAPI_INVENTORY_LIST_HANDLER_H
#define OWPROV_RESTAPI_INVENTORY_LIST_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "RESTAPI_ProvObjects.h"
#include "framework/MicroService.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
namespace OpenWifi {

View File

@@ -10,7 +10,7 @@
#include "RESTAPI_location_handler.h"
#include "framework/RESTAPI_errors.h"
#include "RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "framework/RESTAPI_protocol.h"
#include "Daemon.h"
@@ -31,7 +31,7 @@ namespace OpenWifi{
Storage::ExpandedListMap M;
std::vector<std::string> Errors;
Poco::JSON::Object Inner;
if(Storage()->ExpandInUse(Existing.inUse,M,Errors)) {
if(StorageService()->ExpandInUse(Existing.inUse,M,Errors)) {
for(const auto &[type,list]:M) {
Poco::JSON::Array ObjList;
for(const auto &i:list.entries) {
@@ -69,7 +69,7 @@ namespace OpenWifi{
if(DB_.DeleteRecord("id",UUID)) {
if(!Existing.entity.empty())
Storage()->EntityDB().DeleteLocation("id",Existing.entity,UUID);
StorageService()->EntityDB().DeleteLocation("id",Existing.entity,UUID);
return OK();
}
@@ -88,23 +88,23 @@ namespace OpenWifi{
return BadRequest(RESTAPI::Errors::InvalidJSONDocument);
}
if(NewObject.entity.empty() || !Storage()->EntityDB().Exists("id",NewObject.entity)) {
if(NewObject.entity.empty() || !StorageService()->EntityDB().Exists("id",NewObject.entity)) {
return BadRequest(RESTAPI::Errors::EntityMustExist);
}
if(!NewObject.managementPolicy.empty() && !Storage()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
if(!NewObject.managementPolicy.empty() && !StorageService()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
NewObject.info.id = Daemon()->CreateUUID();
NewObject.info.id = MicroService::instance().CreateUUID();
NewObject.info.created = NewObject.info.modified = std::time(nullptr);
NewObject.inUse.clear();
if(DB_.CreateRecord(NewObject)) {
if(!NewObject.managementPolicy.empty())
Storage()->PolicyDB().AddInUse("id",NewObject.managementPolicy,DB_.Prefix(),NewObject.info.id);
StorageService()->PolicyDB().AddInUse("id",NewObject.managementPolicy,DB_.Prefix(),NewObject.info.id);
Storage()->EntityDB().AddLocation("id",NewObject.entity,NewObject.info.id);
StorageService()->EntityDB().AddLocation("id",NewObject.entity,NewObject.info.id);
Poco::JSON::Object Answer;
NewObject.to_json(Answer);
@@ -129,7 +129,7 @@ namespace OpenWifi{
std::string MoveFromPolicy,MoveToPolicy;
bool MovingPolicy=false;
if(AssignIfPresent(RawObject,"managementPolicy",MoveToPolicy)) {
if(!MoveToPolicy.empty() && !Storage()->PolicyDB().Exists("id",MoveToPolicy)) {
if(!MoveToPolicy.empty() && !StorageService()->PolicyDB().Exists("id",MoveToPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
MoveFromPolicy = Existing.managementPolicy;
@@ -140,7 +140,7 @@ namespace OpenWifi{
std::string MoveFromEntity,MoveToEntity;
bool MovingEntity=false;
if(AssignIfPresent(RawObject,"entity",MoveToEntity)) {
if(!MoveToEntity.empty() && !Storage()->EntityDB().Exists("id",MoveToEntity)) {
if(!MoveToEntity.empty() && !StorageService()->EntityDB().Exists("id",MoveToEntity)) {
return BadRequest(RESTAPI::Errors::EntityMustExist);
}
MoveFromEntity = Existing.entity;
@@ -174,15 +174,15 @@ namespace OpenWifi{
if(DB_.UpdateRecord("id", UUID, Existing)) {
if(MovingPolicy) {
if(!MoveFromPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",MoveFromPolicy,DB_.Prefix(),Existing.info.id);
StorageService()->PolicyDB().DeleteInUse("id",MoveFromPolicy,DB_.Prefix(),Existing.info.id);
if(!MoveToPolicy.empty())
Storage()->PolicyDB().AddInUse("id", MoveToPolicy, DB_.Prefix(), Existing.info.id);
StorageService()->PolicyDB().AddInUse("id", MoveToPolicy, DB_.Prefix(), Existing.info.id);
}
if(MovingEntity) {
if(!MoveFromEntity.empty())
Storage()->EntityDB().DeleteLocation("id",MoveFromEntity,Existing.info.id);
StorageService()->EntityDB().DeleteLocation("id",MoveFromEntity,Existing.info.id);
if(!MoveToEntity.empty())
Storage()->EntityDB().AddLocation("id", MoveToEntity, Existing.info.id);
StorageService()->EntityDB().AddLocation("id", MoveToEntity, Existing.info.id);
}
ProvObjects::Location NewObjectAdded;

View File

@@ -10,9 +10,7 @@
#ifndef OWPROV_RESTAPI_LOCATION_HANDLER_H
#define OWPROV_RESTAPI_LOCATION_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "framework/MicroService.h"
#include "StorageService.h"
namespace OpenWifi {
@@ -26,7 +24,7 @@ namespace OpenWifi {
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal),
DB_(Storage()->LocationDB()){}
DB_(StorageService()->LocationDB()){}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/location/{uuid}"}; };
void DoGet() final ;

View File

@@ -3,8 +3,8 @@
//
#include "RESTAPI_location_list_handler.h"
#include "framework/Utils.h"
#include "RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "framework/RESTAPI_errors.h"
#include "RESTAPI/RESTAPI_db_helpers.h"
@@ -17,7 +17,7 @@ namespace OpenWifi{
Poco::JSON::Array ObjArr;
for(const auto &i:DevUUIDS) {
ProvObjects::Location E;
if(Storage()->LocationDB().GetRecord("id",i,E)) {
if(StorageService()->LocationDB().GetRecord("id",i,E)) {
Poco::JSON::Object Obj;
E.to_json(Obj);
if(QB_.AdditionalInfo)
@@ -32,11 +32,11 @@ namespace OpenWifi{
return ReturnObject(Answer);
} else if(QB_.CountOnly) {
Poco::JSON::Object Answer;
auto C = Storage()->LocationDB().Count();
auto C = StorageService()->LocationDB().Count();
return ReturnCountOnly(C);
} else {
ProvObjects::LocationVec Locations;
Storage()->LocationDB().GetRecords(QB_.Offset,QB_.Limit,Locations);
StorageService()->LocationDB().GetRecords(QB_.Offset,QB_.Limit,Locations);
Poco::JSON::Array ObjArray;
for(const auto &i:Locations) {

View File

@@ -5,10 +5,7 @@
#ifndef OWPROV_RESTAPI_LOCATION_LIST_HANDLER_H
#define OWPROV_RESTAPI_LOCATION_LIST_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "RESTAPI_ProvObjects.h"
#include "framework/MicroService.h"
namespace OpenWifi {

View File

@@ -8,7 +8,7 @@
#include "RESTAPI_managementPolicy_handler.h"
#include "RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "Poco/JSON/Parser.h"
#include "Daemon.h"
@@ -29,7 +29,7 @@ namespace OpenWifi{
Storage::ExpandedListMap M;
std::vector<std::string> Errors;
Poco::JSON::Object Inner;
if(Storage()->ExpandInUse(Existing.inUse,M,Errors)) {
if(StorageService()->ExpandInUse(Existing.inUse,M,Errors)) {
for(const auto &[type,list]:M) {
Poco::JSON::Array ObjList;
for(const auto &i:list.entries) {
@@ -64,7 +64,7 @@ namespace OpenWifi{
return BadRequest(RESTAPI::Errors::StillInUse);
}
if(Storage()->PolicyDB().DeleteRecord("id", UUID)) {
if(StorageService()->PolicyDB().DeleteRecord("id", UUID)) {
return OK();
}
@@ -88,7 +88,7 @@ namespace OpenWifi{
}
NewPolicy.inUse.clear();
NewPolicy.info.id = Daemon()->CreateUUID();
NewPolicy.info.id = MicroService::instance().CreateUUID();
NewPolicy.info.created = NewPolicy.info.modified = std::time(nullptr);
if(DB_.CreateRecord(NewPolicy)) {
ProvObjects::ManagementPolicy Policy;

View File

@@ -10,9 +10,7 @@
#ifndef OWPROV_RESTAPI_MANAGEMENTPOLICY_HANDLER_H
#define OWPROV_RESTAPI_MANAGEMENTPOLICY_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "framework/MicroService.h"
#include "StorageService.h"
namespace OpenWifi {
@@ -26,7 +24,7 @@ namespace OpenWifi {
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal),
DB_(Storage()->PolicyDB()){}
DB_(StorageService()->PolicyDB()){}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/managementPolicy/{uuid}"}; };
private:

View File

@@ -4,8 +4,7 @@
#include "RESTAPI_managementPolicy_list_handler.h"
#include "framework/Utils.h"
#include "RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "framework/RESTAPI_errors.h"
#include "RESTAPI/RESTAPI_db_helpers.h"
@@ -17,7 +16,7 @@ namespace OpenWifi{
Poco::JSON::Array ObjArr;
for(const auto &i:DevUUIDS) {
ProvObjects::ManagementPolicy E;
if(Storage()->PolicyDB().GetRecord("id",i,E)) {
if(StorageService()->PolicyDB().GetRecord("id",i,E)) {
Poco::JSON::Object Obj;
E.to_json(Obj);
if(QB_.AdditionalInfo)
@@ -32,11 +31,11 @@ namespace OpenWifi{
return ReturnObject(Answer);
} else if(QB_.CountOnly) {
Poco::JSON::Object Answer;
auto C = Storage()->ContactDB().Count();
auto C = StorageService()->ContactDB().Count();
return ReturnCountOnly(C);
} else {
ProvObjects::ManagementPolicyVec Policies;
Storage()->PolicyDB().GetRecords(QB_.Offset,QB_.Limit,Policies);
StorageService()->PolicyDB().GetRecords(QB_.Offset,QB_.Limit,Policies);
return ReturnObject("managementPolicies", Policies);
}
}

View File

@@ -5,10 +5,7 @@
#ifndef OWPROV_RESTAPI_MANAGEMENTPOLICY_LIST_HANDLER_H
#define OWPROV_RESTAPI_MANAGEMENTPOLICY_LIST_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "RESTAPI_ProvObjects.h"
#include "framework/MicroService.h"
namespace OpenWifi {

View File

@@ -5,7 +5,7 @@
#include "RESTAPI_managementRole_handler.h"
#include "framework/RESTAPI_protocol.h"
#include "RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "Poco/JSON/Parser.h"
#include "Daemon.h"
@@ -28,7 +28,7 @@ namespace OpenWifi{
Storage::ExpandedListMap M;
std::vector<std::string> Errors;
Poco::JSON::Object Inner;
if(Storage()->ExpandInUse(Existing.inUse,M,Errors)) {
if(StorageService()->ExpandInUse(Existing.inUse,M,Errors)) {
for(const auto &[type,list]:M) {
Poco::JSON::Array ObjList;
for(const auto &i:list.entries) {
@@ -66,9 +66,9 @@ namespace OpenWifi{
}
if(!Existing.managementPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,DB_.Prefix(),Existing.info.id);
StorageService()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,DB_.Prefix(),Existing.info.id);
if(Storage()->RolesDB().DeleteRecord("id", Existing.info.id)) {
if(StorageService()->RolesDB().DeleteRecord("id", Existing.info.id)) {
return OK();
}
InternalError(RESTAPI::Errors::CouldNotBeDeleted);
@@ -86,17 +86,17 @@ namespace OpenWifi{
return BadRequest(RESTAPI::Errors::InvalidJSONDocument);
}
if(!NewObject.managementPolicy.empty() && !Storage()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
if(!NewObject.managementPolicy.empty() && !StorageService()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
NewObject.info.id = Daemon()->CreateUUID();
NewObject.info.id = MicroService::instance().CreateUUID();
NewObject.info.created = NewObject.info.modified = std::time(nullptr);
std::string f{RESTAPI::Protocol::ID};
if(DB_.CreateRecord(NewObject)) {
if(!NewObject.managementPolicy.empty())
Storage()->PolicyDB().AddInUse("id", NewObject.managementPolicy, DB_.Prefix(), NewObject.info.id);
StorageService()->PolicyDB().AddInUse("id", NewObject.managementPolicy, DB_.Prefix(), NewObject.info.id);
Poco::JSON::Object Answer;
ProvObjects::ManagementRole Role;
DB_.GetRecord("id", NewObject.info.id,Role);
@@ -129,7 +129,7 @@ namespace OpenWifi{
std::string NewPolicy,OldPolicy = Existing.managementPolicy;
AssignIfPresent(RawObject, "managementPolicy", NewPolicy);
if(!NewPolicy.empty() && !Storage()->PolicyDB().Exists("id",NewPolicy)) {
if(!NewPolicy.empty() && !StorageService()->PolicyDB().Exists("id",NewPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
@@ -138,9 +138,9 @@ namespace OpenWifi{
if(DB_.UpdateRecord("id",UUID,Existing)) {
if(!OldPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",OldPolicy,DB_.Prefix(),UUID);
StorageService()->PolicyDB().DeleteInUse("id",OldPolicy,DB_.Prefix(),UUID);
if(!NewPolicy.empty())
Storage()->PolicyDB().AddInUse("id",NewPolicy,DB_.Prefix(),UUID);
StorageService()->PolicyDB().AddInUse("id",NewPolicy,DB_.Prefix(),UUID);
ProvObjects::ManagementRole NewRecord;

View File

@@ -5,9 +5,7 @@
#ifndef OWPROV_RESTAPI_MANAGEMENTROLE_HANDLER_H
#define OWPROV_RESTAPI_MANAGEMENTROLE_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "framework/MicroService.h"
#include "StorageService.h"
namespace OpenWifi {
@@ -21,7 +19,7 @@ namespace OpenWifi {
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal),
DB_(Storage()->RolesDB()){}
DB_(StorageService()->RolesDB()){}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/managementRole/{uuid}"}; };
private:
ManagementRoleDB &DB_;

View File

@@ -2,10 +2,10 @@
// Created by stephane bourque on 2021-08-26.
//
#include "RESTAPI_managementRole_list_handler.h"
#include "framework/MicroService.h"
#include "framework/Utils.h"
#include "RESTAPI_ProvObjects.h"
#include "RESTAPI_managementRole_list_handler.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "framework/RESTAPI_errors.h"
#include "RESTAPI/RESTAPI_db_helpers.h"
@@ -17,7 +17,7 @@ namespace OpenWifi{
Poco::JSON::Array ObjArr;
for(const auto &i:DevUUIDS) {
ProvObjects::ManagementRole E;
if(Storage()->RolesDB().GetRecord("id",i,E)) {
if(StorageService()->RolesDB().GetRecord("id",i,E)) {
Poco::JSON::Object Obj;
E.to_json(Obj);
if(QB_.AdditionalInfo)
@@ -32,11 +32,11 @@ namespace OpenWifi{
return ReturnObject(Answer);
} else if(QB_.CountOnly) {
Poco::JSON::Object Answer;
auto C = Storage()->RolesDB().Count();
auto C = StorageService()->RolesDB().Count();
return ReturnCountOnly(C);
} else {
ProvObjects::ManagementRoleVec Roles;
Storage()->RolesDB().GetRecords(QB_.Offset,QB_.Limit,Roles);
StorageService()->RolesDB().GetRecords(QB_.Offset,QB_.Limit,Roles);
Poco::JSON::Array ObjArr;
for(const auto &i:Roles) {
Poco::JSON::Object Obj;

View File

@@ -5,10 +5,7 @@
#ifndef OWPROV_RESTAPI_MANAGEMENTROLE_LIST_HANDLER_H
#define OWPROV_RESTAPI_MANAGEMENTROLE_LIST_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "RESTAPI_ProvObjects.h"
#include "framework/MicroService.h"
namespace OpenWifi {

View File

@@ -1,106 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include "Poco/URI.h"
#include "framework/Utils.h"
#include "framework/RESTAPI_handler.h"
#include "framework/RESTAPI_system_command.h"
#include "RESTAPI/RESTAPI_server.h"
#include "RESTAPI/RESTAPI_entity_handler.h"
#include "RESTAPI/RESTAPI_contact_handler.h"
#include "RESTAPI/RESTAPI_location_handler.h"
#include "RESTAPI/RESTAPI_venue_handler.h"
#include "RESTAPI/RESTAPI_inventory_handler.h"
#include "RESTAPI/RESTAPI_managementPolicy_handler.h"
#include "RESTAPI/RESTAPI_managementPolicy_list_handler.h"
#include "RESTAPI/RESTAPI_inventory_list_handler.h"
#include "RESTAPI/RESTAPI_entity_list_handler.h"
#include "RESTAPI/RESTAPI_configurations_handler.h"
#include "RESTAPI/RESTAPI_configurations_list_handler.h"
#include "RESTAPI/RESTAPI_webSocketServer.h"
#include "RESTAPI/RESTAPI_contact_list_handler.h"
#include "RESTAPI/RESTAPI_location_list_handler.h"
#include "RESTAPI/RESTAPI_venue_list_handler.h"
#include "RESTAPI/RESTAPI_managementRole_list_handler.h"
namespace OpenWifi {
class RESTAPI_server *RESTAPI_server::instance_ = nullptr;
int RESTAPI_server::Start() {
Logger_.information("Starting.");
LogServer_.InitLogging();
for(const auto & Svr: ConfigServersList_) {
Logger_.information(Poco::format("Starting: %s:%s Keyfile:%s CertFile: %s", Svr.Address(), std::to_string(Svr.Port()),
Svr.KeyFile(),Svr.CertFile()));
auto Sock{Svr.CreateSecureSocket(Logger_)};
Svr.LogCert(Logger_);
if(!Svr.RootCA().empty())
Svr.LogCas(Logger_);
auto Params = new Poco::Net::HTTPServerParams;
Params->setMaxThreads(50);
Params->setMaxQueued(200);
Params->setKeepAlive(true);
auto NewServer = std::make_unique<Poco::Net::HTTPServer>(new RequestHandlerFactory(LogServer_), Pool_, Sock, Params);
NewServer->start();
RESTServers_.push_back(std::move(NewServer));
}
return 0;
}
Poco::Net::HTTPRequestHandler *RequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest & Request) {
Poco::URI uri(Request.getURI());
auto *Path = uri.getPath().c_str();
RESTAPIHandler::BindingMap Bindings;
return RESTAPI_Router<
RESTAPI_system_command,
RESTAPI_entity_handler,
RESTAPI_entity_list_handler,
RESTAPI_contact_handler,
RESTAPI_contact_list_handler,
RESTAPI_location_handler,
RESTAPI_location_list_handler,
RESTAPI_venue_handler,
RESTAPI_venue_list_handler,
RESTAPI_inventory_handler,
RESTAPI_inventory_list_handler,
RESTAPI_managementPolicy_handler,
RESTAPI_managementPolicy_list_handler,
RESTAPI_managementRole_list_handler,
RESTAPI_configurations_handler,
RESTAPI_configurations_list_handler,
RESTAPI_webSocketServer
>(Path,Bindings,Logger_, Server_);
}
void RESTAPI_server::Stop() {
Logger_.information("Stopping ");
for( const auto & svr : RESTServers_ )
svr->stop();
RESTServers_.clear();
}
void RESTAPI_server::reinitialize(Poco::Util::Application &self) {
Logger_.information("Reinitializing.");
Daemon()->LoadConfigurationFile();
Stop();
Start();
}
} // namespace

View File

@@ -1,64 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRALFWS_RESTAPI_SERVER_H
#define UCENTRALFWS_RESTAPI_SERVER_H
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/NetException.h"
#include "framework/SubSystemServer.h"
#include "framework/RESTAPI_GenericServer.h"
namespace OpenWifi {
class RESTAPI_server : public SubSystemServer {
public:
static RESTAPI_server *instance() {
if (instance_ == nullptr) {
instance_ = new RESTAPI_server;
}
return instance_;
}
int Start() override;
void Stop() override;
void reinitialize(Poco::Util::Application &self) override;
private:
static RESTAPI_server *instance_;
std::vector<std::unique_ptr<Poco::Net::HTTPServer>> RESTServers_;
Poco::ThreadPool Pool_;
RESTAPI_GenericServer LogServer_;
RESTAPI_server() noexcept:
SubSystemServer("RESTAPIServer", "RESTAPIServer", "openwifi.restapi")
{
}
};
inline RESTAPI_server * RESTAPI_server() { return RESTAPI_server::instance(); };
class RequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory {
public:
explicit RequestHandlerFactory( RESTAPI_GenericServer & Server ) :
Logger_(RESTAPI_server::instance()->Logger()),
Server_(Server){}
Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &request) override;
private:
Poco::Logger & Logger_;
RESTAPI_GenericServer & Server_;
};
}
#endif //UCENTRALFWS_RESTAPI_SERVER_H

View File

@@ -8,12 +8,11 @@
#include "RESTAPI_venue_handler.h"
#include "RESTAPI/RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
#include "StorageService.h"
#include "Poco/JSON/Parser.h"
#include "Daemon.h"
#include "framework/RESTAPI_errors.h"
#include "framework/CIDRUtils.h"
#include "RESTAPI/RESTAPI_db_helpers.h"
namespace OpenWifi{
@@ -45,17 +44,17 @@ namespace OpenWifi{
}
if(!Existing.contact.empty())
Storage()->ContactDB().DeleteInUse("id",Existing.contact,Storage()->VenueDB().Prefix(),UUID);
StorageService()->ContactDB().DeleteInUse("id",Existing.contact,StorageService()->VenueDB().Prefix(),UUID);
if(!Existing.location.empty())
Storage()->LocationDB().DeleteInUse("id",Existing.location,Storage()->VenueDB().Prefix(),UUID);
StorageService()->LocationDB().DeleteInUse("id",Existing.location,StorageService()->VenueDB().Prefix(),UUID);
if(!Existing.managementPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,Storage()->VenueDB().Prefix(),UUID);
StorageService()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,StorageService()->VenueDB().Prefix(),UUID);
if(!Existing.deviceConfiguration.empty())
Storage()->ConfigurationDB().DeleteInUse("id",Existing.deviceConfiguration,Storage()->VenueDB().Prefix(),UUID);
StorageService()->ConfigurationDB().DeleteInUse("id",Existing.deviceConfiguration,StorageService()->VenueDB().Prefix(),UUID);
if(!Existing.parent.empty())
Storage()->VenueDB().DeleteChild("id",Existing.parent,UUID);
StorageService()->VenueDB().DeleteChild("id",Existing.parent,UUID);
if(!Existing.entity.empty())
Storage()->EntityDB().DeleteVenue("id",Existing.entity,UUID);
StorageService()->EntityDB().DeleteVenue("id",Existing.entity,UUID);
DB_.DeleteRecord("id",UUID);
return OK();
}
@@ -88,23 +87,23 @@ namespace OpenWifi{
return BadRequest(RESTAPI::Errors::ValidNonRootUUID);
}
if(!NewObject.entity.empty() && !Storage()->EntityDB().Exists("id",NewObject.entity)) {
if(!NewObject.entity.empty() && !StorageService()->EntityDB().Exists("id",NewObject.entity)) {
return BadRequest(RESTAPI::Errors::EntityMustExist);
}
if(!NewObject.contact.empty() && !Storage()->ContactDB().Exists("id",NewObject.contact)) {
if(!NewObject.contact.empty() && !StorageService()->ContactDB().Exists("id",NewObject.contact)) {
return BadRequest(RESTAPI::Errors::ContactMustExist);
}
if(!NewObject.location.empty() && !Storage()->LocationDB().Exists("id",NewObject.location)) {
if(!NewObject.location.empty() && !StorageService()->LocationDB().Exists("id",NewObject.location)) {
return BadRequest(RESTAPI::Errors::LocationMustExist);
}
if(!NewObject.managementPolicy.empty() && !Storage()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
if(!NewObject.managementPolicy.empty() && !StorageService()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
if(!NewObject.deviceConfiguration.empty() && !Storage()->ConfigurationDB().Exists("id",NewObject.deviceConfiguration)) {
if(!NewObject.deviceConfiguration.empty() && !StorageService()->ConfigurationDB().Exists("id",NewObject.deviceConfiguration)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
@@ -114,7 +113,7 @@ namespace OpenWifi{
NewObject.children.clear();
NewObject.info.modified = NewObject.info.created = std::time(nullptr);
NewObject.info.id = Daemon()->CreateUUID() ;
NewObject.info.id = MicroService::instance().CreateUUID() ;
for(auto &i:NewObject.info.notes)
i.createdBy = UserInfo_.userinfo.email;
@@ -160,7 +159,7 @@ namespace OpenWifi{
std::string MoveEntity;
bool MovingEntity=false;
if(AssignIfPresent(RawObject, "entity", MoveEntity)) {
if(!MoveEntity.empty() && !Storage()->EntityDB().Exists("id",MoveEntity)) {
if(!MoveEntity.empty() && !StorageService()->EntityDB().Exists("id",MoveEntity)) {
return BadRequest(RESTAPI::Errors::EntityMustExist);
}
MovingEntity = MoveEntity != Existing.entity;
@@ -169,7 +168,7 @@ namespace OpenWifi{
std::string MoveVenue;
bool MovingVenue=false;
if(AssignIfPresent(RawObject, "venue", MoveVenue)) {
if(!MoveVenue.empty() && !Storage()->VenueDB().Exists("id",MoveVenue)) {
if(!MoveVenue.empty() && !StorageService()->VenueDB().Exists("id",MoveVenue)) {
return BadRequest(RESTAPI::Errors::VenueMustExist);
}
MovingVenue = MoveVenue != Existing.parent;
@@ -178,7 +177,7 @@ namespace OpenWifi{
std::string MoveLocation;
bool MovingLocation=false;
if(AssignIfPresent(RawObject,"location",MoveLocation)) {
if(!MoveLocation.empty() && !Storage()->LocationDB().Exists("id",MoveLocation)) {
if(!MoveLocation.empty() && !StorageService()->LocationDB().Exists("id",MoveLocation)) {
return BadRequest(RESTAPI::Errors::LocationMustExist);
}
MovingLocation = MoveLocation!=Existing.location;
@@ -187,7 +186,7 @@ namespace OpenWifi{
std::string MoveContact;
bool MovingContact=false;
if(AssignIfPresent(RawObject,"contact",MoveContact)) {
if(!MoveContact.empty() && !Storage()->ContactDB().Exists("id",MoveContact)) {
if(!MoveContact.empty() && !StorageService()->ContactDB().Exists("id",MoveContact)) {
return BadRequest(RESTAPI::Errors::ContactMustExist);
}
MovingContact = MoveContact!=Existing.contact;
@@ -196,7 +195,7 @@ namespace OpenWifi{
std::string MovePolicy;
bool MovingPolicy=false;
if(AssignIfPresent(RawObject,"managementPolicy",MovePolicy)) {
if(!MovePolicy.empty() && !Storage()->PolicyDB().Exists("id",MovePolicy)) {
if(!MovePolicy.empty() && !StorageService()->PolicyDB().Exists("id",MovePolicy)) {
return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
}
MovingPolicy = MovePolicy != Existing.managementPolicy;
@@ -205,26 +204,26 @@ namespace OpenWifi{
std::string MoveConfiguration;
bool MovingConfiguration=false;
if(AssignIfPresent(RawObject,"deviceConfiguration",MoveConfiguration)) {
if(!MoveConfiguration.empty() && !Storage()->ConfigurationDB().Exists("id",MoveConfiguration)) {
if(!MoveConfiguration.empty() && !StorageService()->ConfigurationDB().Exists("id",MoveConfiguration)) {
return BadRequest(RESTAPI::Errors::ConfigurationMustExist);
}
MovingConfiguration = MoveConfiguration != Existing.deviceConfiguration;
}
Existing.info.modified = std::time(nullptr);
if(Storage()->VenueDB().UpdateRecord("id", UUID, Existing)) {
if(StorageService()->VenueDB().UpdateRecord("id", UUID, Existing)) {
if(MovingContact) {
if(!Existing.contact.empty())
Storage()->ContactDB().DeleteInUse("id",Existing.contact,DB_.Prefix(),Existing.info.id);
StorageService()->ContactDB().DeleteInUse("id",Existing.contact,DB_.Prefix(),Existing.info.id);
if(!MoveContact.empty())
Storage()->ContactDB().AddInUse("id", MoveContact, DB_.Prefix(), Existing.info.id);
StorageService()->ContactDB().AddInUse("id", MoveContact, DB_.Prefix(), Existing.info.id);
Existing.contact = MoveContact;
}
if(MovingEntity) {
if(!Existing.entity.empty())
Storage()->EntityDB().DeleteVenue("id", Existing.entity, Existing.info.id);
StorageService()->EntityDB().DeleteVenue("id", Existing.entity, Existing.info.id);
if(!MoveEntity.empty())
Storage()->EntityDB().AddVenue("id",MoveEntity,Existing.info.id);
StorageService()->EntityDB().AddVenue("id",MoveEntity,Existing.info.id);
Existing.entity = MoveEntity;
}
if(MovingVenue) {
@@ -236,23 +235,23 @@ namespace OpenWifi{
}
if(MovingLocation) {
if(!Existing.location.empty())
Storage()->LocationDB().DeleteInUse("id", Existing.location, DB_.Prefix(), Existing.info.id);
StorageService()->LocationDB().DeleteInUse("id", Existing.location, DB_.Prefix(), Existing.info.id);
if(!MoveLocation.empty())
Storage()->LocationDB().AddInUse("id", MoveLocation, DB_.Prefix(), Existing.info.id);
StorageService()->LocationDB().AddInUse("id", MoveLocation, DB_.Prefix(), Existing.info.id);
Existing.location = MoveLocation;
}
if(MovingPolicy) {
if(!Existing.managementPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id", Existing.managementPolicy, DB_.Prefix(), Existing.info.id);
StorageService()->PolicyDB().DeleteInUse("id", Existing.managementPolicy, DB_.Prefix(), Existing.info.id);
if(!MovePolicy.empty())
Storage()->PolicyDB().AddInUse("id", MovePolicy, DB_.Prefix(), Existing.info.id);
StorageService()->PolicyDB().AddInUse("id", MovePolicy, DB_.Prefix(), Existing.info.id);
Existing.managementPolicy = MovePolicy;
}
if(MovingConfiguration) {
if(!Existing.deviceConfiguration.empty())
Storage()->ConfigurationDB().DeleteInUse("id", Existing.deviceConfiguration, DB_.Prefix(), Existing.info.id);
StorageService()->ConfigurationDB().DeleteInUse("id", Existing.deviceConfiguration, DB_.Prefix(), Existing.info.id);
if(!MoveConfiguration.empty())
Storage()->ConfigurationDB().AddInUse("id", MoveConfiguration, DB_.Prefix(), Existing.info.id);
StorageService()->ConfigurationDB().AddInUse("id", MoveConfiguration, DB_.Prefix(), Existing.info.id);
Existing.deviceConfiguration = MoveConfiguration;
}

View File

@@ -10,9 +10,7 @@
#ifndef OWPROV_RESTAPI_VENUE_HANDLER_H
#define OWPROV_RESTAPI_VENUE_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "framework/MicroService.h"
#include "StorageService.h"
namespace OpenWifi {
@@ -25,7 +23,7 @@ namespace OpenWifi {
Poco::Net::HTTPRequest::HTTP_PUT, Poco::Net::HTTPRequest::HTTP_DELETE,
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal), DB_(Storage()->VenueDB()) {}
Internal), DB_(StorageService()->VenueDB()) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/venue/{uuid}"}; };
private:
VenueDB &DB_;

View File

@@ -3,9 +3,7 @@
//
#include "RESTAPI_venue_list_handler.h"
#include "framework/Utils.h"
#include "StorageService.h"
#include "framework/RESTAPI_utils.h"
#include "framework/RESTAPI_errors.h"
#include "RESTAPI/RESTAPI_db_helpers.h"
@@ -18,7 +16,7 @@ namespace OpenWifi{
Poco::JSON::Array ObjArr;
for(const auto &i:UUIDs) {
ProvObjects::Venue E;
if(Storage()->VenueDB().GetRecord("id",i,E)) {
if(StorageService()->VenueDB().GetRecord("id",i,E)) {
Poco::JSON::Object Obj;
E.to_json(Obj);
if(QB_.AdditionalInfo)
@@ -33,7 +31,7 @@ namespace OpenWifi{
return ReturnObject(Answer);
} else if(HasParameter("entity",Arg)) {
ProvObjects::VenueVec Venues;
Storage()->VenueDB().GetRecords(QB_.Offset,QB_.Limit,Venues, Storage()->VenueDB().OP("entity",ORM::EQ,Arg));
StorageService()->VenueDB().GetRecords(QB_.Offset,QB_.Limit,Venues, StorageService()->VenueDB().OP("entity",ORM::EQ,Arg));
if(QB_.CountOnly) {
return ReturnCountOnly(Venues.size());
} else {
@@ -41,7 +39,7 @@ namespace OpenWifi{
}
} else if(HasParameter("venue",Arg)) {
ProvObjects::VenueVec Venues;
Storage()->VenueDB().GetRecords(QB_.Offset,QB_.Limit,Venues,Storage()->VenueDB().OP("venue",ORM::EQ,Arg));
StorageService()->VenueDB().GetRecords(QB_.Offset,QB_.Limit,Venues,StorageService()->VenueDB().OP("venue",ORM::EQ,Arg));
if(QB_.CountOnly) {
return ReturnCountOnly(Venues.size());
} else {
@@ -49,11 +47,11 @@ namespace OpenWifi{
}
} else if(QB_.CountOnly) {
Poco::JSON::Object Answer;
auto C = Storage()->VenueDB().Count();
auto C = StorageService()->VenueDB().Count();
return ReturnCountOnly(C);
} else {
ProvObjects::VenueVec Venues;
Storage()->VenueDB().GetRecords(QB_.Offset, QB_.Limit,Venues);
StorageService()->VenueDB().GetRecords(QB_.Offset, QB_.Limit,Venues);
Poco::JSON::Array ObjArr;
for(const auto &i:Venues) {

View File

@@ -6,10 +6,7 @@
#define OWPROV_RESTAPI_VENUE_LIST_HANDLER_H
#include "framework/RESTAPI_handler.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "RESTAPI/RESTAPI_ProvObjects.h"
#include "framework/MicroService.h"
namespace OpenWifi {

View File

@@ -3,18 +3,19 @@
//
#include "RESTAPI_webSocketServer.h"
#include "framework/MicroService.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/NetException.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Stringifier.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "SerialNumberCache.h"
#include "framework/AuthClient.h"
#include "framework/Utils.h"
#include "Poco/Net/HTTPSClientSession.h"
namespace OpenWifi {
void RESTAPI_webSocketServer::DoGet() {
@@ -29,7 +30,7 @@ namespace OpenWifi {
int n;
bool Authenticated=false;
bool Done=false;
GoogleApiKey_ = Daemon()->ConfigGetString("google.apikey","");
GoogleApiKey_ = MicroService::instance().ConfigGetString("google.apikey","");
GeoCodeEnabled_ = !GoogleApiKey_.empty();
do

View File

@@ -5,7 +5,7 @@
#ifndef UCENTRALGW_RESTAPI_WEBSOCKETSERVER_H
#define UCENTRALGW_RESTAPI_WEBSOCKETSERVER_H
#include "framework/RESTAPI_handler.h"
#include "framework/MicroService.h"
namespace OpenWifi {
class RESTAPI_webSocketServer : public RESTAPIHandler {

View File

@@ -0,0 +1,248 @@
//
// Created by stephane bourque on 2021-07-12.
//
#include "RESTAPI_FMSObjects.h"
#include "framework/MicroService.h"
using OpenWifi::RESTAPI_utils::field_to_json;
using OpenWifi::RESTAPI_utils::field_from_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);
};
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;
}
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;
}
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);
}
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 DeviceTypeList::to_json(Poco::JSON::Object &Obj) const {
field_to_json(Obj,"deviceTypes", deviceTypes);
}
bool DeviceTypeList::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
field_from_json(Obj,"deviceTypes", deviceTypes);
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);
}
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;
}
void RevisionHistoryEntryList::to_json(Poco::JSON::Object &Obj) const {
field_to_json(Obj,"deviceTypes", history);
}
bool RevisionHistoryEntryList::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
field_from_json(Obj,"deviceTypes", history);
return true;
} catch(...) {
}
return false;
}
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 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;
}
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 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;
}
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 DeviceReport::reset() {
OUI_.clear();
Revisions_.clear();
DeviceTypes_.clear();
Status_.clear();
EndPoints_.clear();
UsingLatest_.clear();
UnknownFirmwares_.clear();
totalSecondsOld_.clear();
numberOfDevices = 0 ;
snapshot = std::time(nullptr);
}
bool DeviceReport::from_json(const Poco::JSON::Object::Ptr &Obj) {
try {
return true;
} catch (...) {
}
return false;
}
}

View File

@@ -0,0 +1,133 @@
//
// Created by stephane bourque on 2021-07-12.
//
#include <string>
#ifndef UCENTRALFMS_RESTAPI_FMSOBJECTS_H
#define UCENTRALFMS_RESTAPI_FMSOBJECTS_H
#include "RESTAPI_SecurityObjects.h"
#include "framework/OpenWifiTypes.h"
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;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
};
typedef std::vector<Firmware> FirmwareVec;
struct FirmwareList {
FirmwareVec firmwares;
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;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
};
typedef std::vector<DeviceType> DeviceTypeVec;
struct DeviceTypeList {
DeviceTypeVec deviceTypes;
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;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
};
typedef std::vector<RevisionHistoryEntry> RevisionHistoryEntryVec;
struct RevisionHistoryEntryList {
RevisionHistoryEntryVec history;
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;
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);
};
}
#endif //UCENTRALFMS_RESTAPI_FMSOBJECTS_H

View File

@@ -0,0 +1,263 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Stringifier.h"
#include "Daemon.h"
#ifdef TIP_GATEWAY_SERVICE
#include "DeviceRegistry.h"
#endif
#include "RESTAPI_GWobjects.h"
#include "framework/MicroService.h"
using OpenWifi::RESTAPI_utils::field_to_json;
using OpenWifi::RESTAPI_utils::field_from_json;
using OpenWifi::RESTAPI_utils::EmbedDocument;
namespace OpenWifi::GWObjects {
void Device::to_json(Poco::JSON::Object &Obj) const {
field_to_json(Obj,"serialNumber", SerialNumber);
#ifdef TIP_GATEWAY_SERVICE
field_to_json(Obj,"deviceType", Daemon::instance()->IdentifyDevice(Compatible));
#endif
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);
}
void Device::to_json_with_status(Poco::JSON::Object &Obj) const {
to_json(Obj);
#ifdef TIP_GATEWAY_SERVICE
ConnectionState ConState;
if (DeviceRegistry()->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);
}
#endif
}
bool Device::from_json(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);
return true;
} catch (const Poco::Exception &E) {
}
return false;
}
void Device::Print() const {
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);
}
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);
}
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);
}
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);
}
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);
}
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);
}
bool DefaultConfiguration::from_json(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);
return true;
} catch (const Poco::Exception &E) {
}
return false;
}
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);
}
bool BlackListedDevice::from_json(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);
return true;
} catch (const Poco::Exception &E) {
}
return false;
}
void ConnectionState::to_json(Poco::JSON::Object &Obj) const {
field_to_json(Obj,"serialNumber", SerialNumber);
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);
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 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);
}
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);
}
void Dashboard::reset() {
commands.clear();
upTimes.clear();
memoryUsed.clear();
load1.clear();
load5.clear();
load15.clear();
vendors.clear();
status.clear();
deviceType.clear();
healths.clear();
certificates.clear();
lastContact.clear();
associations.clear();
numberOfDevices = 0 ;
snapshot = std::time(nullptr);
}
void CapabilitiesModel::to_json(Poco::JSON::Object &Obj) const{
field_to_json(Obj,"deviceType", deviceType);
field_to_json(Obj,"capabilities", capabilities);
};
}

View File

@@ -0,0 +1,195 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRAL_RESTAPI_OBJECTS_H
#define UCENTRAL_RESTAPI_OBJECTS_H
#include "Poco/JSON/Object.h"
#include "RESTAPI_SecurityObjects.h"
namespace OpenWifi::GWObjects {
enum CertificateValidation {
NO_CERTIFICATE,
VALID_CERTIFICATE,
MISMATCH_SERIAL,
VERIFIED
};
struct ConnectionState {
uint64_t MessageCount = 0 ;
std::string SerialNumber;
std::string Address;
uint64_t UUID = 0 ;
uint64_t PendingUUID = 0 ;
uint64_t TX = 0, RX = 0;
uint64_t Associations_2G=0;
uint64_t Associations_5G=0;
bool Connected = false;
uint64_t LastContact=0;
std::string Firmware;
CertificateValidation VerifiedCertificate = NO_CERTIFICATE;
std::string Compatible;
void to_json(Poco::JSON::Object &Obj) const;
};
struct Device {
std::string SerialNumber;
std::string DeviceType;
std::string MACAddress;
std::string Manufacturer;
std::string Configuration;
SecurityObjects::NoteInfoVec Notes;
std::string Owner;
std::string Location;
std::string Firmware;
std::string Compatible;
std::string FWUpdatePolicy;
uint64_t UUID;
uint64_t CreationTimestamp;
uint64_t LastConfigurationChange;
uint64_t LastConfigurationDownload;
uint64_t LastFWUpdate;
std::string Venue;
std::string DevicePassword;
void to_json(Poco::JSON::Object &Obj) const;
void to_json_with_status(Poco::JSON::Object &Obj) const;
bool from_json(Poco::JSON::Object::Ptr Obj);
void Print() const;
};
struct Statistics {
std::string SerialNumber;
uint64_t UUID;
std::string Data;
uint64_t Recorded;
void to_json(Poco::JSON::Object &Obj) const;
};
struct HealthCheck {
std::string SerialNumber;
uint64_t UUID;
std::string Data;
uint64_t Recorded;
uint64_t Sanity;
void to_json(Poco::JSON::Object &Obj) const;
};
struct Capabilities {
std::string Capabilities;
uint64_t FirstUpdate;
uint64_t LastUpdate;
void to_json(Poco::JSON::Object &Obj) const;
};
struct DeviceLog {
enum Level {
LOG_EMERG = 0, /* system is unusable */
LOG_ALERT = 1, /* action must be taken immediately */
LOG_CRIT = 2, /* critical conditions */
LOG_ERR = 3, /* error conditions */
LOG_WARNING = 4, /* warning conditions */
LOG_NOTICE = 5, /* normal but significant condition */
LOG_INFO = 6, /* informational */
LOG_DEBUG = 7 /* debug-level messages */
};
std::string SerialNumber;
std::string Log;
std::string Data;
uint64_t Severity;
uint64_t Recorded;
uint64_t LogType;
uint64_t UUID;
void to_json(Poco::JSON::Object &Obj) const;
};
struct DefaultConfiguration {
std::string Name;
std::string Configuration;
std::string Models;
std::string Description;
uint64_t Created;
uint64_t LastModified;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(Poco::JSON::Object::Ptr Obj);
};
struct CommandDetails {
std::string UUID;
std::string SerialNumber;
std::string Command;
std::string Status;
std::string SubmittedBy;
std::string Results;
std::string Details;
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 ;
std::string AttachType;
void to_json(Poco::JSON::Object &Obj) const;
};
struct BlackListedDevice {
std::string serialNumber;
std::string reason;
std::string author;
uint64_t created;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(Poco::JSON::Object::Ptr Obj);
};
struct RttySessionDetails {
std::string SerialNumber;
std::string Server;
uint64_t Port;
std::string Token;
uint64_t TimeOut;
std::string ConnectionId;
uint64_t Started;
std::string CommandUUID;
uint64_t ViewPort;
std::string DevicePassword;
void to_json(Poco::JSON::Object &Obj) const;
};
struct Dashboard {
uint64_t snapshot;
uint64_t numberOfDevices;
Types::CountedMap commands;
Types::CountedMap upTimes;
Types::CountedMap memoryUsed;
Types::CountedMap load1;
Types::CountedMap load5;
Types::CountedMap load15;
Types::CountedMap vendors;
Types::CountedMap status;
Types::CountedMap deviceType;
Types::CountedMap healths;
Types::CountedMap certificates;
Types::CountedMap lastContact;
Types::CountedMap associations;
void to_json(Poco::JSON::Object &Obj) const;
void reset();
};
struct CapabilitiesModel {
std::string deviceType;
std::string capabilities;
void to_json(Poco::JSON::Object &Obj) const;
};
}
#endif //UCENTRAL_RESTAPI_OBJECTS_H

View File

@@ -8,7 +8,7 @@
#include "RESTAPI_ProvObjects.h"
#include "framework/RESTAPI_utils.h"
#include "framework/MicroService.h"
namespace OpenWifi::ProvObjects {

View File

@@ -9,8 +9,8 @@
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Stringifier.h"
#include "framework/MicroService.h"
#include "RESTAPI_SecurityObjects.h"
#include "framework/RESTAPI_utils.h"
using OpenWifi::RESTAPI_utils::field_to_json;
using OpenWifi::RESTAPI_utils::field_from_json;

View File

@@ -5,7 +5,7 @@
#ifndef OWPROV_SDK_STUBS_H
#define OWPROV_SDK_STUBS_H
#include "framework/OpenAPIRequest.h"
#include "framework/MicroService.h"
namespace OpenWifi::SDK {

View File

@@ -3,10 +3,8 @@
//
#include "SecurityDBProxy.h"
#include "framework/OpenAPIRequest.h"
#include "framework/MicroService.h"
#include "Poco/Net/HTTPResponse.h"
#include "RESTAPI/RESTAPI_SecurityObjects.h"
#include "RESTObjects/RESTAPI_SecurityObjects.h"
namespace OpenWifi {
class SecurityDBProxy *SecurityDBProxy::instance_ = nullptr;

View File

@@ -6,8 +6,8 @@
#define OWPROV_SECURITYDBPROXY_H
#include "framework/OpenWifiTypes.h"
#include "RESTAPI/RESTAPI_SecurityObjects.h"
#include "framework/SubSystemServer.h"
#include "RESTObjects/RESTAPI_SecurityObjects.h"
#include "framework/MicroService.h"
namespace OpenWifi {
class SecurityDBProxy : public SubSystemServer {

View File

@@ -6,7 +6,7 @@
#include <mutex>
#include "StorageService.h"
#include "framework/Utils.h"
#include "framework/MicroService.h"
namespace OpenWifi {

View File

@@ -5,7 +5,7 @@
#ifndef UCENTRALGW_SERIALNUMBERCACHE_H
#define UCENTRALGW_SERIALNUMBERCACHE_H
#include "framework/SubSystemServer.h"
#include "framework/MicroService.h"
namespace OpenWifi {
class SerialNumberCache : public SubSystemServer {

View File

@@ -6,15 +6,8 @@
// Arilia Wireless Inc.
//
#include "Poco/Util/Application.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/JSON/Parser.h"
#include "StorageService.h"
#include "Daemon.h"
#include "framework/Utils.h"
#include "framework/OpenAPIRequest.h"
#include "RESTAPI/RESTAPI_ProvObjects.h"
#include "RESTObjects/RESTAPI_ProvObjects.h"
namespace OpenWifi {
@@ -23,17 +16,7 @@ namespace OpenWifi {
int Storage::Start() {
std::lock_guard Guard(Mutex_);
Logger_.setLevel(Poco::Message::PRIO_NOTICE);
Logger_.notice("Starting.");
std::string DBType = Daemon()->ConfigGetString("storage.type");
if (DBType == "sqlite") {
Setup_SQLite();
} else if (DBType == "postgresql") {
Setup_PostgreSQL();
} else if (DBType == "mysql") {
Setup_MySQL();
}
StorageClass::Start();
EntityDB_ = std::make_unique<OpenWifi::EntityDB>(dbType_,*Pool_, Logger_);
PolicyDB_ = std::make_unique<OpenWifi::PolicyDB>(dbType_, *Pool_, Logger_);

View File

@@ -9,18 +9,9 @@
#ifndef UCENTRAL_USTORAGESERVICE_H
#define UCENTRAL_USTORAGESERVICE_H
#include <map>
#include "framework/MicroService.h"
#include "framework/StorageClass.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 "Poco/URI.h"
#include "framework/SubSystemServer.h"
#include "framework/Storage.h"
#include "storage/storage_entity.h"
#include "storage/storage_policies.h"
#include "storage/storage_venue.h"
@@ -35,7 +26,7 @@
namespace OpenWifi {
class Storage : public SubSystemServer, Poco::Runnable {
class Storage : public StorageClass, Poco::Runnable {
public:
static Storage *instance() {
if (instance_ == nullptr) {
@@ -85,11 +76,6 @@ namespace OpenWifi {
private:
static Storage *instance_;
std::unique_ptr<Poco::Data::SessionPool> Pool_;
std::unique_ptr<Poco::Data::SQLite::Connector> SQLiteConn_;
std::unique_ptr<Poco::Data::PostgreSQL::Connector> PostgresConn_;
std::unique_ptr<Poco::Data::MySQL::Connector> MySQLConn_;
DBType dbType_ = sqlite;
std::unique_ptr<OpenWifi::EntityDB> EntityDB_;
std::unique_ptr<OpenWifi::PolicyDB> PolicyDB_;
std::unique_ptr<OpenWifi::VenueDB> VenueDB_;
@@ -111,17 +97,10 @@ namespace OpenWifi {
std::atomic_bool Running_=false;
bool UpdateDeviceTypes();
Storage() noexcept:
SubSystemServer("Storage", "STORAGE-SVR", "storage")
{
}
int Setup_SQLite();
int Setup_MySQL();
int Setup_PostgreSQL();
};
inline Storage * Storage() { return Storage::instance(); }
inline Storage * StorageService() { return Storage::instance(); }
} // namespace

View File

@@ -12,7 +12,7 @@ namespace OpenWifi {
// we need to get the entire dictionary in memory...
// std::function<bool(const TagsDictionary &)> Function = [](const TagsDictionary &D) -> bool { return true; };
Storage()->TagsDictionaryDB().Iterate([](const TagsDictionary &D) -> bool { return true; });
StorageService()->TagsDictionaryDB().Iterate([](const TagsDictionary &D) -> bool { return true; });
return 0;

View File

@@ -5,7 +5,7 @@
#ifndef OWPROV_TAGSERVER_H
#define OWPROV_TAGSERVER_H
#include "framework/SubSystemServer.h"
#include "framework/MicroService.h"
namespace OpenWifi {

View File

@@ -1,118 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRALGW_ALBHEALTHCHECKSERVER_H
#define UCENTRALGW_ALBHEALTHCHECKSERVER_H
#include <memory>
#include <iostream>
#include <fstream>
#include <sstream>
#include "Poco/Thread.h"
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Logger.h"
#include "Daemon.h"
#include "SubSystemServer.h"
namespace OpenWifi {
class ALBRequestHandler: public Poco::Net::HTTPRequestHandler
/// Return a HTML document with the current date and time.
{
public:
explicit ALBRequestHandler(Poco::Logger & L)
: Logger_(L)
{
}
void handleRequest(Poco::Net::HTTPServerRequest& Request, Poco::Net::HTTPServerResponse& Response) override
{
Logger_.information(Poco::format("ALB-REQUEST(%s): New ALB request.",Request.clientAddress().toString()));
Response.setChunkedTransferEncoding(true);
Response.setContentType("text/html");
Response.setDate(Poco::Timestamp());
Response.setStatus(Poco::Net::HTTPResponse::HTTP_OK);
Response.setKeepAlive(true);
Response.set("Connection","keep-alive");
Response.setVersion(Poco::Net::HTTPMessage::HTTP_1_1);
std::ostream &Answer = Response.send();
Answer << "uCentralGW Alive and kicking!" ;
}
private:
Poco::Logger & Logger_;
};
class ALBRequestHandlerFactory: public Poco::Net::HTTPRequestHandlerFactory
{
public:
explicit ALBRequestHandlerFactory(Poco::Logger & L):
Logger_(L)
{
}
ALBRequestHandler* createRequestHandler(const Poco::Net::HTTPServerRequest& request) override
{
if (request.getURI() == "/")
return new ALBRequestHandler(Logger_);
else
return nullptr;
}
private:
Poco::Logger &Logger_;
};
class ALBHealthCheckServer : public SubSystemServer {
public:
ALBHealthCheckServer() noexcept:
SubSystemServer("ALBHealthCheckServer", "ALB-SVR", "alb")
{
}
static ALBHealthCheckServer *instance() {
if (instance_ == nullptr) {
instance_ = new ALBHealthCheckServer;
}
return instance_;
}
int Start() override {
if(Daemon()->ConfigGetBool("alb.enable",false)) {
Port_ = (int)Daemon()->ConfigGetInt("alb.port",15015);
Socket_ = std::make_unique<Poco::Net::ServerSocket>(Port_);
auto Params = new Poco::Net::HTTPServerParams;
Server_ = std::make_unique<Poco::Net::HTTPServer>(new ALBRequestHandlerFactory(Logger_), *Socket_, Params);
Server_->start();
}
return 0;
}
void Stop() override {
if(Server_)
Server_->stop();
}
private:
static ALBHealthCheckServer *instance_;
std::unique_ptr<Poco::Net::HTTPServer> Server_;
std::unique_ptr<Poco::Net::ServerSocket> Socket_;
int Port_ = 0;
};
inline ALBHealthCheckServer * ALBHealthCheckServer() { return ALBHealthCheckServer::instance(); }
inline class ALBHealthCheckServer * ALBHealthCheckServer::instance_ = nullptr;
}
#endif // UCENTRALGW_ALBHEALTHCHECKSERVER_H

View File

@@ -1,93 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include <utility>
#include "AuthClient.h"
#include "RESTAPI/RESTAPI_SecurityObjects.h"
#include "Daemon.h"
#include "OpenAPIRequest.h"
namespace OpenWifi {
class AuthClient * AuthClient::instance_ = nullptr;
int AuthClient::Start() {
return 0;
}
void AuthClient::Stop() {
}
void AuthClient::RemovedCachedToken(const std::string &Token) {
std::lock_guard G(Mutex_);
UserCache_.erase(Token);
}
bool IsTokenExpired(const SecurityObjects::WebToken &T) {
return ((T.expires_in_+T.created_)<std::time(nullptr));
}
bool AuthClient::IsAuthorized(Poco::Net::HTTPServerRequest & Request, std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo ) {
std::lock_guard G(Mutex_);
auto User = UserCache_.find(SessionToken);
if(User != UserCache_.end() && !IsTokenExpired(User->second.webtoken)) {
UInfo = User->second;
return true;
} else {
Types::StringPairVec QueryData;
QueryData.push_back(std::make_pair("token",SessionToken));
OpenAPIRequestGet Req( uSERVICE_SECURITY,
"/api/v1/validateToken",
QueryData,
5000);
Poco::JSON::Object::Ptr Response;
if(Req.Do(Response)==Poco::Net::HTTPResponse::HTTP_OK) {
if(Response->has("tokenInfo") && Response->has("userInfo")) {
SecurityObjects::UserInfoAndPolicy P;
P.from_json(Response);
UserCache_[SessionToken] = P;
UInfo = P;
}
return true;
}
}
return false;
}
bool AuthClient::IsTokenAuthorized(const std::string &SessionToken, SecurityObjects::UserInfoAndPolicy & UInfo) {
std::lock_guard G(Mutex_);
auto User = UserCache_.find(SessionToken);
if(User != UserCache_.end() && !IsTokenExpired(User->second.webtoken)) {
UInfo = User->second;
return true;
} else {
Types::StringPairVec QueryData;
QueryData.push_back(std::make_pair("token",SessionToken));
OpenAPIRequestGet Req(uSERVICE_SECURITY,
"/api/v1/validateToken",
QueryData,
5000);
Poco::JSON::Object::Ptr Response;
if(Req.Do(Response)==Poco::Net::HTTPResponse::HTTP_OK) {
if(Response->has("tokenInfo") && Response->has("userInfo")) {
SecurityObjects::UserInfoAndPolicy P;
P.from_json(Response);
UserCache_[SessionToken] = P;
UInfo = P;
}
return true;
}
}
return false;
}
}

View File

@@ -1,45 +0,0 @@
//
// Created by stephane bourque on 2021-06-30.
//
#ifndef UCENTRALGW_AUTHCLIENT_H
#define UCENTRALGW_AUTHCLIENT_H
#include "Poco/JSON/Object.h"
#include "Poco/JWT/Signer.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/SHA2Engine.h"
#include "RESTAPI/RESTAPI_SecurityObjects.h"
#include "SubSystemServer.h"
namespace OpenWifi {
class AuthClient : public SubSystemServer {
public:
explicit AuthClient() noexcept:
SubSystemServer("Authentication", "AUTH-CLNT", "authentication")
{
}
static AuthClient *instance() {
if (instance_ == nullptr) {
instance_ = new AuthClient;
}
return instance_;
}
int Start() override;
void Stop() override;
bool IsAuthorized(Poco::Net::HTTPServerRequest & Request, std::string &SessionToken, OpenWifi::SecurityObjects::UserInfoAndPolicy & UInfo );
void RemovedCachedToken(const std::string &Token);
bool IsTokenAuthorized(const std::string &Token, SecurityObjects::UserInfoAndPolicy & UInfo);
private:
static AuthClient *instance_;
OpenWifi::SecurityObjects::UserInfoCache UserCache_;
};
inline AuthClient * AuthClient() { return AuthClient::instance(); }
}
#endif // UCENTRALGW_AUTHCLIENT_H

View File

@@ -1,147 +0,0 @@
//
// Created by stephane bourque on 2021-09-29.
//
#include "CIDRUtils.h"
#include "framework/OpenWifiTypes.h"
namespace OpenWifi::CIDR {
static bool cidr_match(const in_addr &addr, const in_addr &net, uint8_t bits) {
if (bits == 0) {
return true;
}
return !((addr.s_addr ^ net.s_addr) & htonl(0xFFFFFFFFu << (32 - bits)));
}
static bool cidr6_match(const in6_addr &address, const in6_addr &network, uint8_t bits) {
#ifdef __linux__
const uint32_t *a = address.s6_addr32;
const uint32_t *n = network.s6_addr32;
#else
const uint32_t *a = address.__u6_addr.__u6_addr32;
const uint32_t *n = network.__u6_addr.__u6_addr32;
#endif
int bits_whole, bits_incomplete;
bits_whole = bits >> 5; // number of whole u32
bits_incomplete = bits & 0x1F; // number of bits in incomplete u32
if (bits_whole) {
if (memcmp(a, n, bits_whole << 2)!=0) {
return false;
}
}
if (bits_incomplete) {
uint32_t mask = htonl((0xFFFFFFFFu) << (32 - bits_incomplete));
if ((a[bits_whole] ^ n[bits_whole]) & mask) {
return false;
}
}
return true;
}
static bool ConvertStringToLong(const char *S, unsigned long &L) {
char *end;
L = std::strtol(S,&end,10);
return end != S;
}
static bool CidrIPinRange(const Poco::Net::IPAddress &IP, const std::string &Range) {
Poco::StringTokenizer TimeTokens(Range,"/",Poco::StringTokenizer::TOK_TRIM);
Poco::Net::IPAddress RangeIP;
if(Poco::Net::IPAddress::tryParse(TimeTokens[0],RangeIP)) {
if(TimeTokens.count()==2) {
if (RangeIP.family() == Poco::Net::IPAddress::IPv4) {
unsigned long MaskLength;
if (ConvertStringToLong(TimeTokens[1].c_str(), MaskLength)) {
return cidr_match(*static_cast<const in_addr *>(RangeIP.addr()),
*static_cast<const in_addr *>(IP.addr()), MaskLength);
}
} else if (RangeIP.family() == Poco::Net::IPAddress::IPv6) {
unsigned long MaskLength;
if (ConvertStringToLong(TimeTokens[1].c_str(), MaskLength)) {
return cidr6_match(*static_cast<const in6_addr *>(RangeIP.addr()),
*static_cast<const in6_addr *>(IP.addr()), MaskLength);
}
}
}
return false;
}
return false;
}
//
// Ranges can be a single IP, of IP1-IP2, of A set of IPs: IP1,IP2,IP3, or a cidr IP/24
// These can work for IPv6 too...
//
static bool ValidateRange(const std::string &R) {
auto Tokens = Poco::StringTokenizer(R,"-");
if(Tokens.count()==2) {
Poco::Net::IPAddress a,b;
if(!Poco::Net::IPAddress::tryParse(Tokens[0],a) && Poco::Net::IPAddress::tryParse(Tokens[1],b))
return false;
return a.family() == b.family();
}
Tokens = Poco::StringTokenizer(R,",");
if(Tokens.count()>1) {
return std::all_of(Tokens.begin(), Tokens.end(), [](const std::string &A) {
Poco::Net::IPAddress a;
return Poco::Net::IPAddress::tryParse(A,a);
} );
}
Tokens = Poco::StringTokenizer(R,"/");
if(Tokens.count()==2) {
Poco::Net::IPAddress a;
if(!Poco::Net::IPAddress::tryParse(Tokens[0],a))
return false;
if(std::atoi(Tokens[1].c_str())==0)
return false;
return true;
}
Poco::Net::IPAddress a;
return Poco::Net::IPAddress::tryParse(R,a);
}
static bool IpInRange(const Poco::Net::IPAddress & target, const std::string & R) {
auto Tokens = Poco::StringTokenizer(R,"-");
if(Tokens.count()==2) {
auto a = Poco::Net::IPAddress::parse(Tokens[0]);
auto b = Poco::Net::IPAddress::parse(Tokens[1]);
if(target.family() != a.family())
return false;
return (a<=target && b>=target);
}
Tokens = Poco::StringTokenizer(R,",");
if(Tokens.count()>1) {
return std::any_of(Tokens.begin(), Tokens.end(), [target](const std::string &Element) {
return Poco::Net::IPAddress::parse(Element) == target ; });
}
Tokens = Poco::StringTokenizer(R,"/");
if(Tokens.count()==2) {
return CidrIPinRange(target,R);
}
return Poco::Net::IPAddress::parse(R)==target;
}
bool IpInRanges(const std::string &IP, const Types::StringVec &R) {
Poco::Net::IPAddress Target;
if(!Poco::Net::IPAddress::tryParse(IP,Target))
return false;
return std::any_of(cbegin(R),cend(R),[Target](const std::string &i) { return IpInRange(Target,i); });
}
bool ValidateIpRanges(const Types::StringVec & Ranges) {
return std::all_of(cbegin(Ranges), cend(Ranges), ValidateRange);
}
}

View File

@@ -1,16 +0,0 @@
//
// Created by stephane bourque on 2021-09-29.
//
#ifndef OWPROV_CIDRUTILS_H
#define OWPROV_CIDRUTILS_H
#include "framework/OpenWifiTypes.h"
namespace OpenWifi::CIDR {
[[nodiscard]] bool IpInRanges(const std::string &IP, const Types::StringVec &R);
[[nodiscard]] bool ValidateIpRanges(const Types::StringVec & Ranges);
}
#endif //OWPROV_CIDRUTILS_H

View File

@@ -16,7 +16,7 @@ namespace OpenWifi {
std::string name;
};
static const std::vector<CountryInfo> CountryCodes {
inline static const std::vector<CountryInfo> CountryCodes {
{ .code= "US", .name= "United States" },
{ .code= "GB", .name= "United Kingdom" },
{ .code= "CA", .name= "Canada" },

View File

@@ -1,221 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include <thread>
#include "KafkaManager.h"
#include "Daemon.h"
#include "Utils.h"
namespace OpenWifi {
class KafkaManager *KafkaManager::instance_ = nullptr;
KafkaManager::KafkaManager() noexcept:
SubSystemServer("KafkaManager", "KAFKA-SVR", "openwifi.kafka")
{
}
void KafkaManager::initialize(Poco::Util::Application & self) {
SubSystemServer::initialize(self);
KafkaEnabled_ = Daemon()->ConfigGetBool("openwifi.kafka.enable",false);
}
#ifdef SMALL_BUILD
int KafkaManager::Start() {
return 0;
}
void KafkaManager::Stop() {
}
#else
int KafkaManager::Start() {
if(!KafkaEnabled_)
return 0;
ProducerThr_ = std::make_unique<std::thread>([this]() { this->ProducerThr(); });
ConsumerThr_ = std::make_unique<std::thread>([this]() { this->ConsumerThr(); });
return 0;
}
void KafkaManager::Stop() {
if(KafkaEnabled_) {
ProducerRunning_ = ConsumerRunning_ = false;
ProducerThr_->join();
ConsumerThr_->join();
return;
}
}
void KafkaManager::ProducerThr() {
cppkafka::Configuration Config({
{ "client.id", Daemon()->ConfigGetString("openwifi.kafka.client.id") },
{ "metadata.broker.list", Daemon()->ConfigGetString("openwifi.kafka.brokerlist") }
});
SystemInfoWrapper_ = R"lit({ "system" : { "id" : )lit" +
std::to_string(Daemon()->ID()) +
R"lit( , "host" : ")lit" + Daemon()->PrivateEndPoint() +
R"lit(" } , "payload" : )lit" ;
cppkafka::Producer Producer(Config);
ProducerRunning_ = true;
while(ProducerRunning_) {
std::this_thread::sleep_for(std::chrono::milliseconds(200));
try
{
std::lock_guard G(ProducerMutex_);
auto Num=0;
while (!Queue_.empty()) {
const auto M = Queue_.front();
Producer.produce(
cppkafka::MessageBuilder(M.Topic).key(M.Key).payload(M.PayLoad));
Queue_.pop();
Num++;
}
if(Num)
Producer.flush();
} catch (const cppkafka::HandleException &E ) {
Logger_.warning(Poco::format("Caught a Kafka exception (producer): %s",std::string{E.what()}));
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
}
}
void KafkaManager::PartitionAssignment(const cppkafka::TopicPartitionList& partitions) {
Logger_.information(Poco::format("Partition assigned: %Lu...",(uint64_t )partitions.front().get_partition()));
}
void KafkaManager::PartitionRevocation(const cppkafka::TopicPartitionList& partitions) {
Logger_.information(Poco::format("Partition revocation: %Lu...",(uint64_t )partitions.front().get_partition()));
}
void KafkaManager::ConsumerThr() {
cppkafka::Configuration Config({
{ "client.id", Daemon()->ConfigGetString("openwifi.kafka.client.id") },
{ "metadata.broker.list", Daemon()->ConfigGetString("openwifi.kafka.brokerlist") },
{ "group.id", Daemon()->ConfigGetString("openwifi.kafka.group.id") },
{ "enable.auto.commit", Daemon()->ConfigGetBool("openwifi.kafka.auto.commit",false) },
{ "auto.offset.reset", "latest" } ,
{ "enable.partition.eof", false }
});
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([this](cppkafka::TopicPartitionList& partitions) {
if(!partitions.empty()) {
Logger_.information(Poco::format("Partition assigned: %Lu...",
(uint64_t)partitions.front().get_partition()));
}
});
Consumer.set_revocation_callback([this](const cppkafka::TopicPartitionList& partitions) {
if(!partitions.empty()) {
Logger_.information(Poco::format("Partition revocation: %Lu...",
(uint64_t)partitions.front().get_partition()));
}
});
bool AutoCommit = Daemon()->ConfigGetBool("openwifi.kafka.auto.commit",false);
auto BatchSize = Daemon()->ConfigGetInt("openwifi.kafka.consumer.batchsize",20);
Types::StringVec Topics;
for(const auto &i:Notifiers_)
Topics.push_back(i.first);
Consumer.subscribe(Topics);
ConsumerRunning_ = true;
while(ConsumerRunning_) {
try {
std::vector<cppkafka::Message> MsgVec = Consumer.poll_batch(BatchSize, std::chrono::milliseconds(200));
for(auto const &Msg:MsgVec) {
if (!Msg)
continue;
if (Msg.get_error()) {
if (!Msg.is_eof()) {
Logger_.error(Poco::format("Error: %s", Msg.get_error().to_string()));
}if(!AutoCommit)
Consumer.async_commit(Msg);
continue;
}
std::lock_guard G(ConsumerMutex_);
auto It = Notifiers_.find(Msg.get_topic());
if (It != Notifiers_.end()) {
Types::TopicNotifyFunctionList &FL = It->second;
std::string Key{Msg.get_key()};
std::string Payload{Msg.get_payload()};
for (auto &F : FL) {
std::thread T(F.first, Key, Payload);
T.detach();
}
}
if (!AutoCommit)
Consumer.async_commit(Msg);
}
} catch (const cppkafka::HandleException &E) {
Logger_.warning(Poco::format("Caught a Kafka exception (consumer): %s",std::string{E.what()}));
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
}
}
std::string KafkaManager::WrapSystemId(const std::string & PayLoad) {
return std::move( SystemInfoWrapper_ + PayLoad + "}");
}
void KafkaManager::PostMessage(const std::string &topic, const std::string & key, const std::string &PayLoad, bool WrapMessage ) {
if(KafkaEnabled_) {
std::lock_guard G(Mutex_);
KMessage M{
.Topic = topic,
.Key = key,
.PayLoad = WrapMessage ? WrapSystemId(PayLoad) : PayLoad };
Queue_.push(M);
}
}
int KafkaManager::RegisterTopicWatcher(const std::string &Topic, Types::TopicNotifyFunction &F) {
if(KafkaEnabled_) {
std::lock_guard G(Mutex_);
auto It = Notifiers_.find(Topic);
if(It == Notifiers_.end()) {
Types::TopicNotifyFunctionList L;
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_));
}
return FunctionId_++;
} else {
return 0;
}
}
void KafkaManager::UnregisterTopicWatcher(const std::string &Topic, int Id) {
if(KafkaEnabled_) {
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) {
L.erase(it);
break;
}
}
}
}
#endif
} // namespace

View File

@@ -1,74 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRALGW_KAFKAMANAGER_H
#define UCENTRALGW_KAFKAMANAGER_H
#include <queue>
#include <thread>
#include "SubSystemServer.h"
#include "OpenWifiTypes.h"
#include "cppkafka/cppkafka.h"
namespace OpenWifi {
class KafkaManager : public SubSystemServer {
public:
struct KMessage {
std::string Topic,
Key,
PayLoad;
};
void initialize(Poco::Util::Application & self) override;
static KafkaManager *instance() {
if(instance_== nullptr)
instance_ = new KafkaManager;
return instance_;
}
void ProducerThr();
void ConsumerThr();
int Start() override;
void Stop() override;
void PostMessage(const std::string &topic, const std::string & key, const std::string &payload, bool WrapMessage = true);
[[nodiscard]] std::string WrapSystemId(const std::string & PayLoad);
[[nodiscard]] bool Enabled() { return KafkaEnabled_; }
int RegisterTopicWatcher(const std::string &Topic, Types::TopicNotifyFunction & F);
void UnregisterTopicWatcher(const std::string &Topic, int FunctionId);
void WakeUp();
void PartitionAssignment(const cppkafka::TopicPartitionList& partitions);
void PartitionRevocation(const cppkafka::TopicPartitionList& partitions);
private:
static KafkaManager *instance_;
std::mutex ProducerMutex_;
std::mutex ConsumerMutex_;
bool KafkaEnabled_ = false;
std::atomic_bool ProducerRunning_ = false;
std::atomic_bool ConsumerRunning_ = false;
std::queue<KMessage> Queue_;
std::string SystemInfoWrapper_;
std::unique_ptr<std::thread> ConsumerThr_;
std::unique_ptr<std::thread> ProducerThr_;
int FunctionId_=1;
Types::NotifyTable Notifiers_;
std::unique_ptr<cppkafka::Configuration> Config_;
KafkaManager() noexcept;
};
inline KafkaManager * KafkaManager() { return KafkaManager::instance(); }
} // NameSpace
#endif // UCENTRALGW_KAFKAMANAGER_H

View File

@@ -1,532 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include <cstdlib>
#include <boost/algorithm/string.hpp>
#include "Poco/Util/Application.h"
#include "Poco/Util/ServerApplication.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Environment.h"
#include "Poco/Net/HTTPSStreamFactory.h"
#include "Poco/Net/HTTPStreamFactory.h"
#include "Poco/Net/FTPSStreamFactory.h"
#include "Poco/Net/FTPStreamFactory.h"
#include "Poco/Path.h"
#include "Poco/File.h"
#include "Poco/String.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Stringifier.h"
#include "framework/ALBHealthCheckServer.h"
#ifndef SMALL_BUILD
#include "KafkaManager.h"
#endif
#include "Kafka_topics.h"
#include "MicroService.h"
#include "Utils.h"
#ifndef TIP_SECURITY_SERVICE
#include "framework/AuthClient.h"
#endif
namespace OpenWifi {
void MyErrorHandler::exception(const Poco::Exception & E) {
Poco::Thread * CurrentThread = Poco::Thread::current();
App_.logger().log(E);
App_.logger().error(Poco::format("Exception occurred in %s",CurrentThread->getName()));
}
void MyErrorHandler::exception(const std::exception & E) {
Poco::Thread * CurrentThread = Poco::Thread::current();
App_.logger().warning(Poco::format("std::exception on %s",CurrentThread->getName()));
}
void MyErrorHandler::exception() {
Poco::Thread * CurrentThread = Poco::Thread::current();
App_.logger().warning(Poco::format("exception on %s",CurrentThread->getName()));
}
void MicroService::Exit(int Reason) {
std::exit(Reason);
}
void MicroService::BusMessageReceived(const std::string &Key, const std::string & Message) {
std::lock_guard G(InfraMutex_);
try {
Poco::JSON::Parser P;
auto Object = P.parse(Message).extract<Poco::JSON::Object::Ptr>();
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();
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) &&
Object->has(KafkaTopics::ServiceEvents::Fields::PUBLIC) &&
Object->has(KafkaTopics::ServiceEvents::Fields::PRIVATE) &&
Object->has(KafkaTopics::ServiceEvents::Fields::VRSN) &&
Object->has(KafkaTopics::ServiceEvents::Fields::KEY)) {
if (Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE && Services_.find(ID) != Services_.end()) {
Services_[ID].LastUpdate = std::time(nullptr);
} else if (Event == KafkaTopics::ServiceEvents::EVENT_LEAVE) {
Services_.erase(ID);
logger().information(Poco::format("Service %s ID=%Lu leaving system.",Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE).toString(),ID));
} else if (Event == KafkaTopics::ServiceEvents::EVENT_JOIN || Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE) {
logger().information(Poco::format("Service %s ID=%Lu joining system.",Object->get(KafkaTopics::ServiceEvents::Fields::PRIVATE).toString(),ID));
Services_[ID] = 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 = (uint64_t)std::time(nullptr)};
for (const auto &[Id, Svc] : Services_) {
logger().information(Poco::format("ID: %Lu Type: %s EndPoint: %s",Id,Svc.Type,Svc.PrivateEndPoint));
}
}
} else {
logger().error(Poco::format("KAFKA-MSG: invalid event '%s', 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 {
logger().error(Poco::format("KAFKA-MSG: invalid event '%s', missing token",Event));
}
} else {
logger().error(Poco::format("Unknown Event: %s Source: %Lu", Event, ID));
}
}
} else {
logger().error("Bad bus message.");
}
auto i=Services_.begin();
auto Now = (uint64_t )std::time(nullptr);
for(;i!=Services_.end();) {
if((Now - i->second.LastUpdate)>60) {
i = Services_.erase(i);
} else
++i;
}
} catch (const Poco::Exception &E) {
logger().log(E);
}
}
MicroServiceMetaVec MicroService::GetServices(const std::string & Type) {
std::lock_guard G(InfraMutex_);
auto T = Poco::toLower(Type);
MicroServiceMetaVec Res;
for(const auto &[Id,ServiceRec]:Services_) {
if(ServiceRec.Type==T)
Res.push_back(ServiceRec);
}
return Res;
}
MicroServiceMetaVec MicroService::GetServices() {
std::lock_guard G(InfraMutex_);
MicroServiceMetaVec Res;
for(const auto &[Id,ServiceRec]:Services_) {
Res.push_back(ServiceRec);
}
return Res;
}
void MicroService::LoadConfigurationFile() {
std::string Location = Poco::Environment::get(DAEMON_CONFIG_ENV_VAR,".");
Poco::Path ConfigFile;
ConfigFile = ConfigFileName_.empty() ? Location + "/" + DAEMON_PROPERTIES_FILENAME : 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;
std::exit(Poco::Util::Application::EXIT_CONFIG);
}
loadConfiguration(ConfigFile.toString());
}
void MicroService::Reload() {
LoadConfigurationFile();
LoadMyConfig();
}
void MicroService::LoadMyConfig() {
std::string KeyFile = ConfigPath("openwifi.service.key");
std::string KeyFilePassword = ConfigPath("openwifi.service.key.password" , "" );
AppKey_ = Poco::SharedPtr<Poco::Crypto::RSAKey>(new Poco::Crypto::RSAKey("", KeyFile, KeyFilePassword));
Cipher_ = CipherFactory_.createCipher(*AppKey_);
ID_ = Utils::GetSystemId();
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");
MyHash_ = CreateHash(MyPublicEndPoint_);
}
void MicroService::initialize(Poco::Util::Application &self) {
// add the default services
SubSystems_.push_back(KafkaManager());
SubSystems_.push_back(ALBHealthCheckServer());
Poco::Net::initializeSSL();
Poco::Net::HTTPStreamFactory::registerFactory();
Poco::Net::HTTPSStreamFactory::registerFactory();
Poco::Net::FTPStreamFactory::registerFactory();
Poco::Net::FTPSStreamFactory::registerFactory();
LoadConfigurationFile();
static const char * LogFilePathKey = "logging.channels.c2.path";
if(LogDir_.empty()) {
std::string OriginalLogFileValue = ConfigPath(LogFilePathKey);
config().setString(LogFilePathKey, OriginalLogFileValue);
} else {
config().setString(LogFilePathKey, LogDir_);
}
Poco::File DataDir(ConfigPath("openwifi.system.data"));
DataDir_ = DataDir.path();
if(!DataDir.exists()) {
try {
DataDir.createDirectory();
} catch (const Poco::Exception &E) {
logger().log(E);
}
}
LoadMyConfig();
InitializeSubSystemServers();
ServerApplication::initialize(self);
Types::TopicNotifyFunction F = [this](std::string s1,std::string s2) { this->BusMessageReceived(s1,s2); };
KafkaManager()->RegisterTopicWatcher(KafkaTopics::SERVICE_EVENTS, F);
}
void MicroService::uninitialize() {
// add your own uninitialization code here
ServerApplication::uninitialize();
}
void MicroService::reinitialize(Poco::Util::Application &self) {
ServerApplication::reinitialize(self);
// add your own reinitialization code here
}
void MicroService::defineOptions(Poco::Util::OptionSet &options) {
ServerApplication::defineOptions(options);
options.addOption(
Poco::Util::Option("help", "", "display help information on command line arguments")
.required(false)
.repeatable(false)
.callback(Poco::Util::OptionCallback<MicroService>(this, &MicroService::handleHelp)));
options.addOption(
Poco::Util::Option("file", "", "specify the configuration file")
.required(false)
.repeatable(false)
.argument("file")
.callback(Poco::Util::OptionCallback<MicroService>(this, &MicroService::handleConfig)));
options.addOption(
Poco::Util::Option("debug", "", "to run in debug, set to true")
.required(false)
.repeatable(false)
.callback(Poco::Util::OptionCallback<MicroService>(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<MicroService>(this, &MicroService::handleLogs)));
options.addOption(
Poco::Util::Option("version", "", "get the version and quit.")
.required(false)
.repeatable(false)
.callback(Poco::Util::OptionCallback<MicroService>(this, &MicroService::handleVersion)));
}
void MicroService::handleHelp(const std::string &name, const std::string &value) {
HelpRequested_ = true;
displayHelp();
stopOptionsProcessing();
}
void MicroService::handleVersion(const std::string &name, const std::string &value) {
HelpRequested_ = true;
std::cout << Version() << std::endl;
stopOptionsProcessing();
}
void MicroService::handleDebug(const std::string &name, const std::string &value) {
if(value == "true")
DebugMode_ = true ;
}
void MicroService::handleLogs(const std::string &name, const std::string &value) {
LogDir_ = value;
}
void MicroService::handleConfig(const std::string &name, const std::string &value) {
ConfigFileName_ = value;
}
void MicroService::displayHelp() {
Poco::Util::HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader("A " + DAEMON_APP_NAME + " implementation for TIP.");
helpFormatter.format(std::cout);
}
void MicroService::InitializeSubSystemServers() {
for(auto i:SubSystems_)
addSubsystem(i);
}
void MicroService::StartSubSystemServers() {
for(auto i:SubSystems_) {
i->Start();
}
BusEventManager_.Start();
}
void MicroService::StopSubSystemServers() {
BusEventManager_.Stop();
for(auto i=SubSystems_.rbegin(); i!=SubSystems_.rend(); ++i)
(*i)->Stop();
}
std::string MicroService::CreateUUID() {
return UUIDGenerator_.create().toString();
}
bool MicroService::SetSubsystemLogLevel(const std::string &SubSystem, const std::string &Level) {
try {
auto P = Poco::Logger::parseLevel(Level);
auto Sub = Poco::toLower(SubSystem);
if (Sub == "all") {
for (auto i : SubSystems_) {
i->Logger().setLevel(P);
}
return true;
} else {
// std::cout << "Sub:" << SubSystem << " Level:" << Level << std::endl;
for (auto i : SubSystems_) {
if (Sub == Poco::toLower(i->Name())) {
i->Logger().setLevel(P);
return true;
}
}
}
} catch (const Poco::Exception & E) {
std::cout << "Exception" << std::endl;
}
return false;
}
void MicroService::Reload(const std::string &Sub) {
for (auto i : SubSystems_) {
if (Poco::toLower(Sub) == Poco::toLower(i->Name())) {
i->reinitialize(Poco::Util::Application::instance());
return;
}
}
}
Types::StringVec MicroService::GetSubSystems() const {
Types::StringVec Result;
for(auto i:SubSystems_)
Result.push_back(Poco::toLower(i->Name()));
return Result;
}
Types::StringPairVec MicroService::GetLogLevels() {
Types::StringPairVec Result;
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" };
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) {
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) {
return config().getBool(Key);
}
std::string MicroService::ConfigGetString(const std::string &Key,const std::string & Default) {
return config().getString(Key, Default);
}
std::string MicroService::ConfigGetString(const std::string &Key) {
return config().getString(Key);
}
std::string MicroService::ConfigPath(const std::string &Key,const std::string & Default) {
std::string R = config().getString(Key, Default);
return Poco::Path::expand(R);
}
std::string MicroService::ConfigPath(const std::string &Key) {
std::string R = config().getString(Key);
return Poco::Path::expand(R);
}
std::string MicroService::Encrypt(const std::string &S) {
return Cipher_->encryptString(S, Poco::Crypto::Cipher::Cipher::ENC_BASE64);;
}
std::string MicroService::Decrypt(const std::string &S) {
return Cipher_->decryptString(S, Poco::Crypto::Cipher::Cipher::ENC_BASE64);;
}
std::string MicroService::CreateHash(const std::string &S) {
SHA2_.update(S);
return Utils::ToHex(SHA2_.digest());
}
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();
}
void BusEventManager::run() {
Running_ = true;
auto Msg = Daemon()->MakeSystemEventMessage(KafkaTopics::ServiceEvents::EVENT_JOIN);
KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS,Daemon()->PrivateEndPoint(),Msg, false);
while(Running_) {
Poco::Thread::trySleep((unsigned long)Daemon()->DaemonBusTimer());
if(!Running_)
break;
Msg = Daemon()->MakeSystemEventMessage(KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE);
KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS,Daemon()->PrivateEndPoint(),Msg, false);
}
Msg = Daemon()->MakeSystemEventMessage(KafkaTopics::ServiceEvents::EVENT_LEAVE);
KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS,Daemon()->PrivateEndPoint(),Msg, false);
};
void BusEventManager::Start() {
if(KafkaManager()->Enabled()) {
Thread_.start(*this);
}
}
void BusEventManager::Stop() {
if(KafkaManager()->Enabled()) {
Running_ = false;
Thread_.wakeUp();
Thread_.join();
}
}
[[nodiscard]] bool MicroService::IsValidAPIKEY(const Poco::Net::HTTPServerRequest &Request) {
try {
auto APIKEY = Request.get("X-API-KEY");
return APIKEY == MyHash_;
} catch (const Poco::Exception &E) {
logger().log(E);
}
return false;
}
void MicroService::SavePID() {
try {
std::ofstream O;
O.open(Daemon()->DataDir() + "/pidfile",std::ios::binary | std::ios::trunc);
O << Poco::Process::id();
O.close();
} catch (...)
{
std::cout << "Could not save system ID" << std::endl;
}
}
int MicroService::main(const ArgVec &args) {
MyErrorHandler ErrorHandler(*this);
Poco::ErrorHandler::set(&ErrorHandler);
if (!HelpRequested_) {
SavePID();
Poco::Logger &logger = Poco::Logger::get(DAEMON_APP_NAME);
logger.notice(Poco::format("Starting %s version %s.",DAEMON_APP_NAME, Version()));
if(Poco::Net::Socket::supportsIPv6())
logger.information("System supports IPv6.");
else
logger.information("System does NOT support IPv6.");
if (config().getBool("application.runAsDaemon", false)) {
logger.information("Starting as a daemon.");
}
logger.information(Poco::format("System ID set to %Lu",ID_));
StartSubSystemServers();
waitForTerminationRequest();
StopSubSystemServers();
logger.notice(Poco::format("Stopped %s...",DAEMON_APP_NAME));
}
return Application::EXIT_OK;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,162 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
//
#include <iostream>
#include "OpenAPIRequest.h"
#include "Poco/Net/HTTPSClientSession.h"
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/JSON/Parser.h>
#include <Poco/URI.h>
#include <Poco/Exception.h>
#include "Utils.h"
#include "Daemon.h"
namespace OpenWifi {
int OpenAPIRequestGet::Do(Poco::JSON::Object::Ptr &ResponseObject) {
try {
auto Services = Daemon()->GetServices(Type_);
for(auto const &Svc:Services) {
Poco::URI URI(Svc.PrivateEndPoint);
Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort());
URI.setPath(EndPoint_);
for (const auto &qp : QueryData_)
URI.addQueryParameter(qp.first, qp.second);
std::string Path(URI.getPathAndQuery());
Session.setTimeout(Poco::Timespan(msTimeout_/1000, msTimeout_ % 1000));
Poco::Net::HTTPRequest Request(Poco::Net::HTTPRequest::HTTP_GET,
Path,
Poco::Net::HTTPMessage::HTTP_1_1);
Request.add("X-API-KEY", Svc.AccessKey);
Request.add("X-INTERNAL-NAME", Daemon()->PublicEndPoint());
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<Poco::JSON::Object::Ptr>();
}
return Response.getStatus();
}
}
catch (const Poco::Exception &E)
{
std::cerr << E.displayText() << std::endl;
}
return -1;
}
int OpenAPIRequestPut::Do(Poco::JSON::Object::Ptr &ResponseObject) {
try {
auto Services = Daemon()->GetServices(Type_);
for(auto const &Svc:Services) {
Poco::URI URI(Svc.PrivateEndPoint);
Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort());
URI.setPath(EndPoint_);
for (const auto &qp : QueryData_)
URI.addQueryParameter(qp.first, qp.second);
std::string Path(URI.getPathAndQuery());
Session.setTimeout(Poco::Timespan(msTimeout_/1000, msTimeout_ % 1000));
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());
Request.add("X-API-KEY", Svc.AccessKey);
Request.add("X-INTERNAL-NAME", Daemon()->PublicEndPoint());
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<Poco::JSON::Object::Ptr>();
// std::cout << "Response OK" << std::endl;
} else {
Poco::JSON::Parser P;
ResponseObject = P.parse(is).extract<Poco::JSON::Object::Ptr>();
// std::cout << "Response: " << Response.getStatus() << std::endl;
}
return Response.getStatus();
}
}
catch (const Poco::Exception &E)
{
std::cerr << E.displayText() << std::endl;
}
return -1;
}
int OpenAPIRequestPost::Do(Poco::JSON::Object::Ptr &ResponseObject) {
try {
auto Services = Daemon()->GetServices(Type_);
for(auto const &Svc:Services) {
Poco::URI URI(Svc.PrivateEndPoint);
Poco::Net::HTTPSClientSession Session(URI.getHost(), URI.getPort());
URI.setPath(EndPoint_);
for (const auto &qp : QueryData_)
URI.addQueryParameter(qp.first, qp.second);
std::string Path(URI.getPathAndQuery());
Session.setTimeout(Poco::Timespan(msTimeout_/1000, msTimeout_ % 1000));
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());
Request.add("X-API-KEY", Svc.AccessKey);
Request.add("X-INTERNAL-NAME", Daemon()->PublicEndPoint());
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<Poco::JSON::Object::Ptr>();
// std::cout << "Response OK" << std::endl;
} else {
Poco::JSON::Parser P;
ResponseObject = P.parse(is).extract<Poco::JSON::Object::Ptr>();
// std::cout << "Response: " << Response.getStatus() << std::endl;
}
return Response.getStatus();
}
}
catch (const Poco::Exception &E)
{
std::cerr << E.displayText() << std::endl;
}
return -1;
}
}

View File

@@ -1,79 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRALGW_OPENAPIREQUEST_H
#define UCENTRALGW_OPENAPIREQUEST_H
#include "Poco/JSON/Object.h"
#include "OpenWifiTypes.h"
namespace OpenWifi {
class OpenAPIRequestGet {
public:
explicit OpenAPIRequestGet( std::string Type,
std::string EndPoint,
Types::StringPairVec & QueryData,
uint64_t msTimeout):
Type_(std::move(Type)),
EndPoint_(std::move(EndPoint)),
QueryData_(QueryData),
msTimeout_(msTimeout) {};
int Do(Poco::JSON::Object::Ptr &ResponseObject);
private:
std::string Type_;
std::string EndPoint_;
Types::StringPairVec QueryData_;
uint64_t msTimeout_;
};
class OpenAPIRequestPut {
public:
explicit OpenAPIRequestPut( std::string Type,
std::string EndPoint,
Types::StringPairVec & QueryData,
Poco::JSON::Object Body,
uint64_t msTimeout):
Type_(std::move(Type)),
EndPoint_(std::move(EndPoint)),
QueryData_(QueryData),
msTimeout_(msTimeout),
Body_(std::move(Body)){};
int Do(Poco::JSON::Object::Ptr &ResponseObject);
private:
std::string Type_;
std::string EndPoint_;
Types::StringPairVec QueryData_;
uint64_t msTimeout_;
Poco::JSON::Object Body_;
};
class OpenAPIRequestPost {
public:
explicit OpenAPIRequestPost( std::string Type,
std::string EndPoint,
Types::StringPairVec & QueryData,
Poco::JSON::Object Body,
uint64_t msTimeout):
Type_(std::move(Type)),
EndPoint_(std::move(EndPoint)),
QueryData_(QueryData),
msTimeout_(msTimeout),
Body_(std::move(Body)){};
int Do(Poco::JSON::Object::Ptr &ResponseObject);
private:
std::string Type_;
std::string EndPoint_;
Types::StringPairVec QueryData_;
uint64_t msTimeout_;
Poco::JSON::Object Body_;
};
}
#endif // UCENTRALGW_OPENAPIREQUEST_H

View File

@@ -9,8 +9,6 @@
#ifndef UCENTRALGW_UCENTRALTYPES_H
#define UCENTRALGW_UCENTRALTYPES_H
#include "SubSystemServer.h"
#include <vector>
#include <string>
#include <map>
@@ -29,7 +27,6 @@ namespace OpenWifi::Types {
typedef std::queue<StringPair> StringPairQueue;
typedef std::vector<std::string> StringVec;
typedef std::set<std::string> StringSet;
typedef std::vector<SubSystemServer*> SubSystemVec;
typedef std::map<std::string,std::set<std::string>> StringMapStringSet;
typedef std::function<void(std::string, std::string)> TopicNotifyFunction;
typedef std::list<std::pair<TopicNotifyFunction,int>> TopicNotifyFunctionList;

View File

@@ -1,5 +0,0 @@
//
// Created by stephane bourque on 2021-09-15.
//
#include "RESTAPI_GenericServer.h"

View File

@@ -1,78 +0,0 @@
//
// Created by stephane bourque on 2021-09-15.
//
#ifndef OWPROV_RESTAPI_GENERICSERVER_H
#define OWPROV_RESTAPI_GENERICSERVER_H
#include <vector>
#include <string>
#include "Daemon.h"
#include "Poco/StringTokenizer.h"
#include "Poco/Net/HTTPRequest.h"
namespace OpenWifi {
class RESTAPI_GenericServer {
public:
enum {
LOG_GET=0,
LOG_DELETE,
LOG_PUT,
LOG_POST
};
void inline SetFlags(bool External, const std::string &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;
}
}
inline void InitLogging() {
std::string Public = Daemon()->ConfigGetString("apilogging.public.methods","PUT,POST,DELETE");
SetFlags(true, Public);
std::string Private = Daemon()->ConfigGetString("apilogging.private.methods","PUT,POST,DELETE");
SetFlags(false, Private);
std::string PublicBadTokens = Daemon()->ConfigGetString("apilogging.public.badtokens.methods","");
LogBadTokens_[0] = (Poco::icompare(PublicBadTokens,"true")==0);
std::string PrivateBadTokens = Daemon()->ConfigGetString("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];
return false;
};
[[nodiscard]] inline bool LogBadTokens(bool External) const {
return LogBadTokens_[ (External ? 0 : 1) ];
};
private:
std::array<bool,8> LogFlags_{false};
std::array<bool,2> LogBadTokens_{false};
};
}
#endif //OWPROV_RESTAPI_GENERICSERVER_H

View File

@@ -15,7 +15,7 @@ namespace OpenWifi::RESTAPI::Errors {
static const std::string CouldNotBeDeleted{"Element could not be deleted."};
static const std::string NameMustBeSet{"The name property must be set."};
static const std::string ConfigBlockInvalid{"Configuration block type invalid."};
static const std::string UnknownId{"Unknown UUID."};
static const std::string UnknownId{"Unknown management policy."};
static const std::string InvalidDeviceTypes{"Unknown or invalid device type(s)."};
static const std::string RecordNotCreated{"Record could not be created."};
static const std::string RecordNotUpdated{"Record could not be updated."};

View File

@@ -1,489 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include <cctype>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <future>
#include <chrono>
#include "Poco/URI.h"
#include "Poco/Net/OAuth20Credentials.h"
#include "RESTAPI_errors.h"
#ifdef TIP_SECURITY_SERVICE
#include "AuthService.h"
#else
#include "framework/AuthClient.h"
#endif
#include "RESTAPI_handler.h"
#include "RESTAPI_protocol.h"
#include "Utils.h"
#include "Daemon.h"
namespace OpenWifi {
void RESTAPIHandler::handleRequest(Poco::Net::HTTPServerRequest &RequestIn,
Poco::Net::HTTPServerResponse &ResponseIn) {
try {
Request = &RequestIn;
Response = &ResponseIn;
if (!ContinueProcessing())
return;
if (AlwaysAuthorize_ && !IsAuthorized())
return;
ParseParameters();
if (Request->getMethod() == Poco::Net::HTTPRequest::HTTP_GET)
DoGet();
else if (Request->getMethod() == Poco::Net::HTTPRequest::HTTP_POST)
DoPost();
else if (Request->getMethod() == Poco::Net::HTTPRequest::HTTP_DELETE)
DoDelete();
else if (Request->getMethod() == Poco::Net::HTTPRequest::HTTP_PUT)
DoPut();
else
BadRequest(RESTAPI::Errors::UnsupportedHTTPMethod);
return;
} catch (const Poco::Exception &E) {
Logger_.log(E);
BadRequest(RESTAPI::Errors::InternalError);
}
}
const Poco::JSON::Object::Ptr &RESTAPIHandler::ParseStream() {
return IncomingParser_.parse(Request->stream()).extract<Poco::JSON::Object::Ptr>();
}
bool RESTAPIHandler::ParseBindings(const std::string & Request, const std::list<const char *> & EndPoints, BindingMap &bindings) {
bindings.clear();
std::vector<std::string> PathItems = Utils::Split(Request, '/');
for(const auto &EndPoint:EndPoints) {
std::vector<std::string> ParamItems = Utils::Split(EndPoint, '/');
if (PathItems.size() != ParamItems.size())
continue;
bool Matched = true;
for (auto i = 0; i != PathItems.size() && Matched; i++) {
if (PathItems[i] != ParamItems[i]) {
if (ParamItems[i][0] == '{') {
auto ParamName = ParamItems[i].substr(1, ParamItems[i].size() - 2);
bindings[Poco::toLower(ParamName)] = PathItems[i];
} else {
Matched = false;
}
}
}
if(Matched)
return true;
}
return false;
}
void RESTAPIHandler::PrintBindings() {
for (const auto &[key, value] : Bindings_)
std::cout << "Key = " << key << " Value= " << value << std::endl;
}
void RESTAPIHandler::ParseParameters() {
Poco::URI uri(Request->getURI());
Parameters_ = uri.getQueryParameters();
InitQueryBlock();
}
static bool is_number(const std::string &s) {
return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
}
static bool is_bool(const std::string &s) {
if (s == "true" || s == "false")
return true;
return false;
}
uint64_t RESTAPIHandler::GetParameter(const std::string &Name, const uint64_t Default) {
auto Hint = std::find_if(Parameters_.begin(),Parameters_.end(),[Name](const std::pair<std::string,std::string> &S){ return S.first==Name; });
if(Hint==Parameters_.end() || !is_number(Hint->second))
return Default;
return std::stoull(Hint->second);
}
bool RESTAPIHandler::GetBoolParameter(const std::string &Name, bool Default) {
auto Hint = std::find_if(begin(Parameters_),end(Parameters_),[Name](const std::pair<std::string,std::string> &S){ return S.first==Name; });
if(Hint==end(Parameters_) || !is_bool(Hint->second))
return Default;
return Hint->second=="true";
}
std::string RESTAPIHandler::GetParameter(const std::string &Name, const std::string &Default) {
auto Hint = std::find_if(begin(Parameters_),end(Parameters_),[Name](const std::pair<std::string,std::string> &S){ return S.first==Name; });
if(Hint==end(Parameters_))
return Default;
return Hint->second;
}
bool RESTAPIHandler::HasParameter(const std::string &Name, std::string &Value) {
auto Hint = std::find_if(begin(Parameters_),end(Parameters_),[Name](const std::pair<std::string,std::string> &S){ return S.first==Name; });
if(Hint==end(Parameters_))
return false;
Value = Hint->second;
return true;
}
bool RESTAPIHandler::HasParameter(const std::string &Name, uint64_t & Value) {
auto Hint = std::find_if(begin(Parameters_),end(Parameters_),[Name](const std::pair<std::string,std::string> &S){ return S.first==Name; });
if(Hint==end(Parameters_))
return false;
Value = std::stoull(Hint->second);
return true;
}
const std::string &RESTAPIHandler::GetBinding(const std::string &Name, const std::string &Default) {
auto E = Bindings_.find(Poco::toLower(Name));
if (E == Bindings_.end())
return Default;
return E->second;
}
static std::string MakeList(const std::vector<std::string> &L) {
std::string Return;
for (const auto &i : L)
if (Return.empty())
Return = i;
else
Return += ", " + i;
return Return;
}
bool RESTAPIHandler::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;
}
bool RESTAPIHandler::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;
}
bool RESTAPIHandler::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;
}
void RESTAPIHandler::AddCORS() {
auto Origin = Request->find("Origin");
if (Origin != Request->end()) {
Response->set("Access-Control-Allow-Origin", Origin->second);
Response->set("Vary", "Origin");
} else {
Response->set("Access-Control-Allow-Origin", "*");
}
Response->set("Access-Control-Allow-Headers", "*");
Response->set("Access-Control-Allow-Methods", MakeList(Methods_));
Response->set("Access-Control-Max-Age", "86400");
}
void RESTAPIHandler::SetCommonHeaders(bool CloseConnection) {
Response->setVersion(Poco::Net::HTTPMessage::HTTP_1_1);
Response->setChunkedTransferEncoding(true);
Response->setContentType("application/json");
if(CloseConnection) {
Response->set("Connection", "close");
Response->setKeepAlive(false);
} else {
Response->setKeepAlive(true);
Response->set("Connection", "Keep-Alive");
Response->set("Keep-Alive", "timeout=5, max=1000");
}
}
void RESTAPIHandler::ProcessOptions() {
AddCORS();
SetCommonHeaders();
Response->setContentLength(0);
Response->set("Access-Control-Allow-Credentials", "true");
Response->setStatus(Poco::Net::HTTPResponse::HTTP_OK);
Response->set("Vary", "Origin, Access-Control-Request-Headers, Access-Control-Request-Method");
Response->send();
}
void RESTAPIHandler::PrepareResponse( Poco::Net::HTTPResponse::HTTPStatus Status,
bool CloseConnection) {
Response->setStatus(Status);
AddCORS();
SetCommonHeaders(CloseConnection);
}
void RESTAPIHandler::BadRequest(const std::string & Reason) {
PrepareResponse(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST);
Poco::JSON::Object ErrorObject;
ErrorObject.set("ErrorCode",400);
ErrorObject.set("ErrorDetails",Request->getMethod());
ErrorObject.set("ErrorDescription",Reason.empty() ? "Command is missing parameters or wrong values." : Reason) ;
std::ostream &Answer = Response->send();
Poco::JSON::Stringifier::stringify(ErrorObject, Answer);
}
void RESTAPIHandler::InternalError(const std::string & Reason) {
PrepareResponse(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
Poco::JSON::Object ErrorObject;
ErrorObject.set("ErrorCode",500);
ErrorObject.set("ErrorDetails",Request->getMethod());
ErrorObject.set("ErrorDescription",Reason.empty() ? "Please try later or review the data submitted." : Reason) ;
std::ostream &Answer = Response->send();
Poco::JSON::Stringifier::stringify(ErrorObject, Answer);
}
void RESTAPIHandler::UnAuthorized(const std::string & Reason) {
PrepareResponse(Poco::Net::HTTPResponse::HTTP_FORBIDDEN);
Poco::JSON::Object ErrorObject;
ErrorObject.set("ErrorCode",403);
ErrorObject.set("ErrorDetails",Request->getMethod());
ErrorObject.set("ErrorDescription",Reason.empty() ? "No access allowed." : Reason) ;
std::ostream &Answer = Response->send();
Poco::JSON::Stringifier::stringify(ErrorObject, Answer);
}
void RESTAPIHandler::NotFound() {
PrepareResponse(Poco::Net::HTTPResponse::HTTP_NOT_FOUND);
Poco::JSON::Object ErrorObject;
ErrorObject.set("ErrorCode",404);
ErrorObject.set("ErrorDetails",Request->getMethod());
ErrorObject.set("ErrorDescription","This resource does not exist.");
std::ostream &Answer = Response->send();
Poco::JSON::Stringifier::stringify(ErrorObject, Answer);
Logger_.debug(Poco::format("RES-NOTFOUND: User='%s@%s' Method='%s' Path='%s",
UserInfo_.userinfo.email,
Utils::FormatIPv6(Request->clientAddress().toString()),
Request->getMethod(),
Request->getURI()));
}
void RESTAPIHandler::OK() {
PrepareResponse();
if( Request->getMethod()==Poco::Net::HTTPRequest::HTTP_DELETE ||
Request->getMethod()==Poco::Net::HTTPRequest::HTTP_OPTIONS) {
Response->send();
} else {
Poco::JSON::Object ErrorObject;
ErrorObject.set("Code", 0);
ErrorObject.set("Operation", Request->getMethod());
ErrorObject.set("Details", "Command completed.");
std::ostream &Answer = Response->send();
Poco::JSON::Stringifier::stringify(ErrorObject, Answer);
}
}
void RESTAPIHandler::SendFile(Poco::File & File, const std::string & UUID) {
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", "private");
Response->set("Pragma", "private");
Response->set("Expires", "Mon, 26 Jul 2027 05:00:00 GMT");
Response->set("Content-Length", std::to_string(File.getSize()));
AddCORS();
Response->sendFile(File.path(),"application/octet-stream");
}
void RESTAPIHandler::SendFile(Poco::File & File) {
Poco::Path P(File.path());
auto MT = Utils::FindMediaType(File);
if(MT.Encoding==Utils::BINARY) {
Response->set("Content-Transfer-Encoding","binary");
Response->set("Accept-Ranges", "bytes");
}
Response->set("Cache-Control", "private");
Response->set("Pragma", "private");
Response->set("Expires", "Mon, 26 Jul 2027 05:00:00 GMT");
AddCORS();
Response->sendFile(File.path(),MT.ContentType);
}
void RESTAPIHandler::SendFile(Poco::TemporaryFile &TempAvatar, const std::string &Type, const std::string & Name) {
auto MT = Utils::FindMediaType(Name);
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("Accept-Ranges", "bytes");
Response->set("Cache-Control", "private");
Response->set("Pragma", "private");
Response->set("Expires", "Mon, 26 Jul 2027 05:00:00 GMT");
AddCORS();
Response->sendFile(TempAvatar.path(),MT.ContentType);
}
void RESTAPIHandler::SendHTMLFileBack(Poco::File & File,
const Types::StringPairVec & FormVars) {
Response->set("Pragma", "private");
Response->set("Expires", "Mon, 26 Jul 2027 05:00:00 GMT");
Response->set("Content-Length", std::to_string(File.getSize()));
AddCORS();
auto FormContent = Utils::LoadFile(File.path());
Utils::ReplaceVariables(FormContent, FormVars);
Response->setChunkedTransferEncoding(true);
Response->setContentType("text/html");
std::ostream& ostr = Response->send();
ostr << FormContent;
}
void RESTAPIHandler::ReturnStatus(Poco::Net::HTTPResponse::HTTPStatus Status, bool CloseConnection) {
PrepareResponse(Status, CloseConnection);
if(Status == Poco::Net::HTTPResponse::HTTP_NO_CONTENT) {
Response->setContentLength(0);
Response->erase("Content-Type");
Response->setChunkedTransferEncoding(false);
}
Response->send();
}
bool RESTAPIHandler::ContinueProcessing() {
if (Request->getMethod() == Poco::Net::HTTPRequest::HTTP_OPTIONS) {
ProcessOptions();
return false;
} else if (std::find(Methods_.begin(), Methods_.end(), Request->getMethod()) == Methods_.end()) {
BadRequest(RESTAPI::Errors::UnsupportedHTTPMethod);
return false;
}
return true;
}
bool RESTAPIHandler::IsAuthorized() {
if(Internal_) {
auto Allowed = Daemon()->IsValidAPIKEY(*Request);
if(!Allowed) {
if(Server_.LogBadTokens(false)) {
Logger_.debug(Poco::format("I-REQ-DENIED(%s): Method='%s' Path='%s",
Utils::FormatIPv6(Request->clientAddress().toString()),
Request->getMethod(), Request->getURI()));
}
} else {
auto Id = Request->get("X-INTERNAL-NAME", "unknown");
if(Server_.LogIt(Request->getMethod(),true)) {
Logger_.debug(Poco::format("I-REQ-ALLOWED(%s): User='%s' Method='%s' Path='%s",
Utils::FormatIPv6(Request->clientAddress().toString()), Id,
Request->getMethod(), Request->getURI()));
}
}
return Allowed;
} else {
if (SessionToken_.empty()) {
try {
Poco::Net::OAuth20Credentials Auth(*Request);
if (Auth.getScheme() == "Bearer") {
SessionToken_ = Auth.getBearerToken();
}
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
}
#ifdef TIP_SECURITY_SERVICE
if (AuthService()->IsAuthorized(*Request, SessionToken_, UserInfo_)) {
#else
if (AuthClient()->IsAuthorized(*Request, SessionToken_, UserInfo_)) {
#endif
if(Server_.LogIt(Request->getMethod(),true)) {
Logger_.debug(Poco::format("X-REQ-ALLOWED(%s): User='%s@%s' Method='%s' Path='%s",
UserInfo_.userinfo.email,
Utils::FormatIPv6(Request->clientAddress().toString()),
Request->clientAddress().toString(),
Request->getMethod(),
Request->getURI()));
}
return true;
} else {
if(Server_.LogBadTokens(true)) {
Logger_.debug(Poco::format("X-REQ-DENIED(%s): Method='%s' Path='%s",
Utils::FormatIPv6(Request->clientAddress().toString()),
Request->getMethod(), Request->getURI()));
}
UnAuthorized();
}
return false;
}
}
void RESTAPIHandler::ReturnObject(Poco::JSON::Object &Object) {
PrepareResponse();
std::ostream &Answer = Response->send();
Poco::JSON::Stringifier::stringify(Object, Answer);
}
void RESTAPIHandler::ReturnCountOnly(uint64_t Count) {
Poco::JSON::Object Answer;
Answer.set("count", Count);
ReturnObject(Answer);
}
bool RESTAPIHandler::InitQueryBlock() {
if(QueryBlockInitialized_)
return 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, 1);
QB_.Limit = GetParameter(RESTAPI::Protocol::LIMIT, 100);
QB_.Filter = GetParameter(RESTAPI::Protocol::FILTER, "");
QB_.Select = GetParameter(RESTAPI::Protocol::SELECT, "");
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);
if(QB_.Offset<1)
QB_.Offset=1;
return true;
}
[[nodiscard]] uint64_t RESTAPIHandler::Get(const char *Parameter,const Poco::JSON::Object::Ptr &Obj, uint64_t Default){
if(Obj->has(Parameter))
return Obj->get(Parameter);
return Default;
}
[[nodiscard]] std::string RESTAPIHandler::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]] bool RESTAPIHandler::GetB(const char *Parameter,const Poco::JSON::Object::Ptr &Obj, bool Default){
if(Obj->has(Parameter))
return Obj->get(Parameter).toString()=="true";
return Default;
}
[[nodiscard]] uint64_t RESTAPIHandler::GetWhen(const Poco::JSON::Object::Ptr &Obj) {
return RESTAPIHandler::Get(RESTAPI::Protocol::WHEN, Obj);
}
}

View File

@@ -1,234 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRAL_RESTAPI_HANDLER_H
#define UCENTRAL_RESTAPI_HANDLER_H
#include "Poco/URI.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/NetException.h"
#include "Poco/Net/PartHandler.h"
#include "Poco/Logger.h"
#include "Poco/File.h"
#include "Poco/TemporaryFile.h"
#include "Poco/JSON/Object.h"
#include "Poco/CountingStream.h"
#include "Poco/NullStream.h"
#include "RESTAPI/RESTAPI_SecurityObjects.h"
#include "RESTAPI_utils.h"
#include "RESTAPI_GenericServer.h"
namespace OpenWifi {
class RESTAPI_PartHandler: public Poco::Net::PartHandler
{
public:
RESTAPI_PartHandler():
_length(0)
{
}
void handlePart(const Poco::Net::MessageHeader& header, std::istream& stream) override
{
_type = header.get("Content-Type", "(unspecified)");
if (header.has("Content-Disposition"))
{
std::string disp;
Poco::Net::NameValueCollection params;
Poco::Net::MessageHeader::splitParameters(header["Content-Disposition"], disp, params);
_name = params.get("name", "(unnamed)");
_fileName = params.get("filename", "(unnamed)");
}
Poco::CountingInputStream istr(stream);
Poco::NullOutputStream ostr;
Poco::StreamCopier::copyStream(istr, ostr);
_length = (int)istr.chars();
}
[[nodiscard]] int length() const
{
return _length;
}
[[nodiscard]] const std::string& name() const
{
return _name;
}
[[nodiscard]] const std::string& fileName() const
{
return _fileName;
}
[[nodiscard]] const std::string& contentType() const
{
return _type;
}
private:
int _length;
std::string _type;
std::string _name;
std::string _fileName;
};
class RESTAPIHandler : public Poco::Net::HTTPRequestHandler {
public:
struct QueryBlock {
uint64_t StartDate = 0 , EndDate = 0 , Offset = 0 , Limit = 0, LogType = 0 ;
std::string SerialNumber, Filter, Select;
bool Lifetime=false, LastOnly=false, Newest=false, CountOnly=false, AdditionalInfo=false;
};
typedef std::map<std::string, std::string> BindingMap;
RESTAPIHandler(BindingMap map, Poco::Logger &l, std::vector<std::string> Methods, RESTAPI_GenericServer & Server, bool Internal=false, bool AlwaysAuthorize=true)
: Bindings_(std::move(map)), Logger_(l), Methods_(std::move(Methods)), Server_(Server), Internal_(Internal), AlwaysAuthorize_(AlwaysAuthorize) {}
static bool ParseBindings(const std::string & Request, const std::list<const char *> & EndPoints, BindingMap &Keys);
void PrintBindings();
void ParseParameters();
void AddCORS();
void SetCommonHeaders(bool CloseConnection=false);
void ProcessOptions();
void
PrepareResponse(Poco::Net::HTTPResponse::HTTPStatus Status = Poco::Net::HTTPResponse::HTTP_OK,
bool CloseConnection = false);
bool ContinueProcessing();
bool IsAuthorized();
uint64_t GetParameter(const std::string &Name, uint64_t Default);
std::string GetParameter(const std::string &Name, const std::string &Default);
bool GetBoolParameter(const std::string &Name, bool Default);
void BadRequest(const std::string &Reason );
void InternalError(const std::string &Reason = "");
void UnAuthorized(const std::string &Reason = "");
void ReturnObject(Poco::JSON::Object &Object);
void NotFound();
void OK();
void ReturnStatus(Poco::Net::HTTPResponse::HTTPStatus Status,
bool CloseConnection=false);
void SendFile(Poco::File & File, const std::string & UUID);
void SendHTMLFileBack(Poco::File & File,
const Types::StringPairVec & FormVars);
void SendFile(Poco::TemporaryFile &TempAvatar, const std::string &Type, const std::string & Name);
void SendFile(Poco::File & File);
const std::string &GetBinding(const std::string &Name, const std::string &Default);
bool InitQueryBlock();
void ReturnCountOnly(uint64_t Count);
[[nodiscard]] static uint64_t Get(const char *Parameter,const Poco::JSON::Object::Ptr &Obj, uint64_t Default=0);
[[nodiscard]] static std::string GetS(const char *Parameter,const Poco::JSON::Object::Ptr &Obj, const std::string & Default="");
[[nodiscard]] static bool GetB(const char *Parameter,const Poco::JSON::Object::Ptr &Obj, bool Default=false);
[[nodiscard]] static uint64_t GetWhen(const Poco::JSON::Object::Ptr &Obj);
bool HasParameter(const std::string &QueryParameter, std::string &Value);
bool HasParameter(const std::string &QueryParameter, uint64_t & Value);
static bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, std::string &Value);
static bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, uint64_t &Value);
static bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, bool &Value);
template<typename T> void ReturnObject(const char *Name, const std::vector<T> & Objects) {
Poco::JSON::Object Answer;
RESTAPI_utils::field_to_json(Answer,Name,Objects);
ReturnObject(Answer);
}
Poco::Logger & Logger() { return Logger_; }
void handleRequest(Poco::Net::HTTPServerRequest &request,
Poco::Net::HTTPServerResponse &response) final;
virtual void DoGet() = 0 ;
virtual void DoDelete() = 0 ;
virtual void DoPost() = 0 ;
virtual void DoPut() = 0 ;
const Poco::JSON::Object::Ptr & ParseStream();
protected:
BindingMap Bindings_;
Poco::URI::QueryParameters Parameters_;
Poco::Logger &Logger_;
std::string SessionToken_;
SecurityObjects::UserInfoAndPolicy UserInfo_;
std::vector<std::string> Methods_;
QueryBlock QB_;
bool Internal_=false;
bool QueryBlockInitialized_=false;
Poco::Net::HTTPServerRequest *Request= nullptr;
Poco::Net::HTTPServerResponse *Response= nullptr;
bool AlwaysAuthorize_=true;
Poco::JSON::Parser IncomingParser_;
RESTAPI_GenericServer & Server_;
};
class RESTAPI_UnknownRequestHandler : public RESTAPIHandler {
public:
RESTAPI_UnknownRequestHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server)
: RESTAPIHandler(bindings, L, std::vector<std::string>{}, Server) {}
inline void DoGet() override {};
inline void DoPost() override {};
inline void DoPut() override {};
inline void DoDelete() override {};
};
template<class T>
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<typename T, typename... Args>
RESTAPIHandler * RESTAPI_Router(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & Logger, RESTAPI_GenericServer & Server) {
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, false);
}
if constexpr (sizeof...(Args) == 0) {
return new RESTAPI_UnknownRequestHandler(Bindings,Logger, Server);
} else {
return RESTAPI_Router<Args...>(RequestedPath, Bindings, Logger, Server);
}
}
template<typename T, typename... Args>
RESTAPIHandler * RESTAPI_Router_I(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & Logger, RESTAPI_GenericServer & Server) {
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, true);
}
if constexpr (sizeof...(Args) == 0) {
return new RESTAPI_UnknownRequestHandler(Bindings,Logger, Server);
} else {
return RESTAPI_Router_I<Args...>(RequestedPath, Bindings, Logger, Server);
}
}
}
#endif //UCENTRAL_RESTAPI_HANDLER_H

View File

@@ -1,140 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include "RESTAPI_system_command.h"
#include "Poco/Exception.h"
#include "Poco/JSON/Parser.h"
#include "Poco/DateTime.h"
#include "Poco/DateTimeFormat.h"
#include "Daemon.h"
#include "framework/RESTAPI_protocol.h"
#include "framework/RESTAPI_errors.h"
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
namespace OpenWifi {
void RESTAPI_system_command::DoPost() {
auto Obj = ParseStream();
if (Obj->has(RESTAPI::Protocol::COMMAND)) {
auto Command = Poco::toLower(Obj->get(RESTAPI::Protocol::COMMAND).toString());
if (Command == RESTAPI::Protocol::SETLOGLEVEL) {
if (Obj->has(RESTAPI::Protocol::SUBSYSTEMS) &&
Obj->isArray(RESTAPI::Protocol::SUBSYSTEMS)) {
auto ParametersBlock = Obj->getArray(RESTAPI::Protocol::SUBSYSTEMS);
for (const auto &i : *ParametersBlock) {
Poco::JSON::Parser pp;
auto InnerObj = pp.parse(i).extract<Poco::JSON::Object::Ptr>();
if (InnerObj->has(RESTAPI::Protocol::TAG) &&
InnerObj->has(RESTAPI::Protocol::VALUE)) {
auto Name = GetS(RESTAPI::Protocol::TAG, InnerObj);
auto Value = GetS(RESTAPI::Protocol::VALUE, InnerObj);
Daemon()->SetSubsystemLogLevel(Name, Value);
Logger_.information(
Poco::format("Setting log level for %s at %s", Name, Value));
}
}
return OK();
}
} else if (Command == RESTAPI::Protocol::GETLOGLEVELS) {
auto CurrentLogLevels = Daemon()->GetLogLevels();
Poco::JSON::Object Result;
Poco::JSON::Array Array;
for (auto &[Name, Level] : CurrentLogLevels) {
Poco::JSON::Object Pair;
Pair.set(RESTAPI::Protocol::TAG, Name);
Pair.set(RESTAPI::Protocol::VALUE, Level);
Array.add(Pair);
}
Result.set(RESTAPI::Protocol::TAGLIST, Array);
return ReturnObject(Result);
} else if (Command == RESTAPI::Protocol::GETLOGLEVELNAMES) {
Poco::JSON::Object Result;
Poco::JSON::Array LevelNamesArray;
const Types::StringVec &LevelNames = Daemon()->GetLogLevelNames();
for (const auto &i : LevelNames)
LevelNamesArray.add(i);
Result.set(RESTAPI::Protocol::LIST, LevelNamesArray);
return ReturnObject(Result);
} else if (Command == RESTAPI::Protocol::GETSUBSYSTEMNAMES) {
Poco::JSON::Object Result;
Poco::JSON::Array LevelNamesArray;
const Types::StringVec &SubSystemNames = Daemon()->GetSubSystems();
for (const auto &i : SubSystemNames)
LevelNamesArray.add(i);
Result.set(RESTAPI::Protocol::LIST, LevelNamesArray);
return ReturnObject(Result);
} else if (Command == RESTAPI::Protocol::STATS) {
} else if (Command == RESTAPI::Protocol::RELOAD) {
if (Obj->has(RESTAPI::Protocol::SUBSYSTEMS) &&
Obj->isArray(RESTAPI::Protocol::SUBSYSTEMS)) {
auto SubSystems = Obj->getArray(RESTAPI::Protocol::SUBSYSTEMS);
std::vector<std::string> Names;
for (const auto &i : *SubSystems)
Names.push_back(i.toString());
std::thread ReloadThread([Names](){
std::this_thread::sleep_for(10000ms);
for(const auto &i:Names) {
if(i=="daemon")
Daemon()->Reload();
else
Daemon()->Reload(i);
}
});
ReloadThread.detach();
}
return OK();
}
} else {
return BadRequest(RESTAPI::Errors::InvalidCommand);
}
BadRequest(RESTAPI::Errors::MissingOrInvalidParameters);
}
void RESTAPI_system_command::DoGet() {
std::string Arg;
if(HasParameter("command",Arg) && Arg=="info") {
Poco::JSON::Object Answer;
Answer.set(RESTAPI::Protocol::VERSION, Daemon()->Version());
Answer.set(RESTAPI::Protocol::UPTIME, Daemon()->uptime().totalSeconds());
Answer.set(RESTAPI::Protocol::START, Daemon()->startTime().epochTime());
Answer.set(RESTAPI::Protocol::OS, Poco::Environment::osName());
Answer.set(RESTAPI::Protocol::PROCESSORS, Poco::Environment::processorCount());
Answer.set(RESTAPI::Protocol::HOSTNAME, Poco::Environment::nodeName());
Answer.set(RESTAPI::Protocol::UI, Daemon()->GetUIURI());
Poco::JSON::Array Certificates;
auto SubSystems = Daemon()->GetFullSubSystems();
std::set<std::string> CertNames;
for(const auto &i:SubSystems) {
auto Hosts=i->HostSize();
for(uint64_t j=0;j<Hosts;++j) {
auto CertFileName = i->Host(j).CertFile();
if(!CertFileName.empty()) {
auto InsertResult = CertNames.insert(CertFileName);
if(InsertResult.second) {
Poco::JSON::Object Inner;
Inner.set("filename", CertFileName);
Poco::Crypto::X509Certificate C(CertFileName);
auto ExpiresOn = C.expiresOn();
Inner.set("expiresOn",ExpiresOn.timestamp().epochTime());
Certificates.add(Inner);
}
}
}
}
Answer.set("certificates", Certificates);
return ReturnObject(Answer);
}
BadRequest(RESTAPI::Errors::InvalidCommand);
}
}

View File

@@ -1,32 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRALGW_RESTAPI_SYSTEM_COMMAND_H
#define UCENTRALGW_RESTAPI_SYSTEM_COMMAND_H
#include "framework/RESTAPI_handler.h"
namespace OpenWifi {
class RESTAPI_system_command : public RESTAPIHandler {
public:
RESTAPI_system_command(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal)
: RESTAPIHandler(bindings, L,
std::vector<std::string>{Poco::Net::HTTPRequest::HTTP_POST,
Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/system"};}
void DoGet() final;
void DoPost() final;
void DoPut() final {};
void DoDelete() final {};
};
}
#endif // UCENTRALGW_RESTAPI_SYSTEM_COMMAND_H

View File

@@ -1,17 +0,0 @@
//
// Created by stephane bourque on 2021-07-05.
//
#include "RESTAPI_utils.h"
namespace OpenWifi::RESTAPI_utils {
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);
const auto &DetailsObj = result.extract<Poco::JSON::Object::Ptr>();
Obj.set(ObjName, DetailsObj);
}
}

View File

@@ -1,294 +0,0 @@
//
// Created by stephane bourque on 2021-07-05.
//
#ifndef UCENTRALGW_RESTAPI_UTILS_H
#define UCENTRALGW_RESTAPI_UTILS_H
#include <functional>
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Parser.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "framework/OpenWifiTypes.h"
#include "framework/Utils.h"
namespace OpenWifi::RESTAPI_utils {
void EmbedDocument(const std::string & ObjName, Poco::JSON::Object & Obj, const std::string &ObjStr);
inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, bool 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::vector<Types::StringPair> & S) {
Poco::JSON::Array Array;
for(const auto &i:S) {
Poco::JSON::Object O;
O.set("tag",i.first);
O.set("value", i.second);
}
Obj.set(Field,Array);
}
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, uint64_t V) {
Obj.set(Field,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);
}
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)
A.add(i);
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);
O.set("value", Value);
A.add(O);
}
Obj.set(Field,A);
}
template<typename T> void field_to_json(Poco::JSON::Object &Obj,
const char *Field,
const T &V,
std::function<std::string(const T &)> F) {
Obj.set(Field, F(V));
}
template<typename T> bool field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, T & V,
std::function<T(const std::string &)> F) {
if(Obj->has(Field))
V = F(Obj->get(Field).toString());
return true;
}
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, std::string &S) {
if(Obj->has(Field))
S = Obj->get(Field).toString();
}
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, uint64_t &V) {
if(Obj->has(Field))
V = Obj->get(Field);
}
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, bool &V) {
if(Obj->has(Field))
V = (Obj->get(Field).toString() == "true");
}
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, Types::StringPairVec &Vec) {
if(Obj->isArray(Field)) {
auto O = Obj->getArray(Field);
for(const auto &i:*O) {
std::string S1,S2;
auto Inner = i.extract<Poco::JSON::Object::Ptr>();
if(Inner->has("tag"))
S1 = Inner->get("tag").toString();
if(Inner->has("value"))
S2 = Inner->get("value").toString();
auto P = std::make_pair(S1,S2);
Vec.push_back(P);
}
}
}
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, Types::StringVec &V) {
if(Obj->isArray(Field)) {
V.clear();
Poco::JSON::Array::Ptr A = Obj->getArray(Field);
for(const auto &i:*A) {
V.push_back(i.toString());
}
}
}
inline void field_from_json(Poco::JSON::Object::Ptr Obj, const char *Field, Types::TagList &V) {
if(Obj->isArray(Field)) {
V.clear();
Poco::JSON::Array::Ptr A = Obj->getArray(Field);
for(const auto &i:*A) {
V.push_back(i);
}
}
}
template<class T> void field_to_json(Poco::JSON::Object &Obj, const char *Field, const std::vector<T> &Value) {
Poco::JSON::Array Arr;
for(const auto &i:Value) {
Poco::JSON::Object AO;
i.to_json(AO);
Arr.add(AO);
}
Obj.set(Field, Arr);
}
template<class T> 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);
}
template<class T> void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, std::vector<T> &Value) {
if(Obj->isArray(Field)) {
Poco::JSON::Array::Ptr Arr = Obj->getArray(Field);
for(auto &i:*Arr) {
auto InnerObj = i.extract<Poco::JSON::Object::Ptr>();
T NewItem;
NewItem.from_json(InnerObj);
Value.push_back(NewItem);
}
}
}
template<class T> void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, T &Value) {
if(Obj->isObject(Field)) {
Poco::JSON::Object::Ptr A = Obj->getObject(Field);
Value.from_json(A);
}
}
inline std::string to_string(const Types::TagList & ObjectArray) {
Poco::JSON::Array OutputArr;
if(ObjectArray.empty())
return "[]";
for(auto const &i:ObjectArray) {
OutputArr.add(i);
}
std::ostringstream OS;
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) {
Poco::JSON::Array OutputArr;
if(ObjectArray.empty())
return "[]";
for(auto const &i:ObjectArray) {
OutputArr.add(i);
}
std::ostringstream OS;
Poco::JSON::Stringifier::condense(OutputArr,OS);
return OS.str();
}
template<class T> std::string to_string(const std::vector<T> & ObjectArray) {
Poco::JSON::Array OutputArr;
if(ObjectArray.empty())
return "[]";
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);
return OS.str();
}
template<class T> std::string to_string(const T & Object) {
Poco::JSON::Object OutputObj;
Object.to_json(OutputObj);
std::ostringstream OS;
Poco::JSON::Stringifier::condense(OutputObj,OS);
return OS.str();
}
inline Types::StringVec to_object_array(const std::string & ObjectString) {
Types::StringVec Result;
if(ObjectString.empty())
return Result;
try {
Poco::JSON::Parser P;
auto Object = P.parse(ObjectString).template extract<Poco::JSON::Array::Ptr>();
for (auto const &i : *Object) {
Result.push_back(i.toString());
}
} catch (...) {
}
return Result;
}
inline OpenWifi::Types::TagList to_taglist(const std::string & ObjectString) {
Types::TagList Result;
if(ObjectString.empty())
return Result;
try {
Poco::JSON::Parser P;
auto Object = P.parse(ObjectString).template extract<Poco::JSON::Array::Ptr>();
for (auto const &i : *Object) {
Result.push_back(i);
}
} catch (...) {
}
return Result;
}
template<class T> std::vector<T> to_object_array(const std::string & ObjectString) {
std::vector<T> Result;
if(ObjectString.empty())
return Result;
try {
Poco::JSON::Parser P;
auto Object = P.parse(ObjectString).template extract<Poco::JSON::Array::Ptr>();
for (auto const i : *Object) {
auto InnerObject = i.template extract<Poco::JSON::Object::Ptr>();
T Obj;
Obj.from_json(InnerObject);
Result.push_back(Obj);
}
} catch (...) {
}
return Result;
}
template<class T> T to_object(const std::string & ObjectString) {
T Result;
if(ObjectString.empty())
return Result;
Poco::JSON::Parser P;
auto Object = P.parse(ObjectString).template extract<Poco::JSON::Object::Ptr>();
Result.from_json(Object);
return Result;
}
template<class T> bool from_request(T & Obj, Poco::Net::HTTPServerRequest &Request) {
Poco::JSON::Parser IncomingParser;
auto RawObject = IncomingParser.parse(Request.stream()).extract<Poco::JSON::Object::Ptr>();
Obj.from_json(RawObject);
return true;
}
}
#endif // UCENTRALGW_RESTAPI_UTILS_H

View File

@@ -1,18 +0,0 @@
//
// Created by stephane bourque on 2021-10-06.
//
#ifndef OWPROV_STORAGE_H
#define OWPROV_STORAGE_H
namespace OpenWifi {
enum DBType {
sqlite,
pgsql,
mysql
};
}
#endif //OWPROV_STORAGE_H

View File

@@ -0,0 +1,181 @@
//
// Created by stephane bourque on 2021-10-06.
//
#ifndef OPENWIFI_STORAGE_H
#define OPENWIFI_STORAGE_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"
#endif
#include "framework/MicroService.h"
namespace OpenWifi {
enum DBType {
sqlite,
pgsql,
mysql
};
class StorageClass : public SubSystemServer {
public:
/* static StorageClass *instance() {
if (instance_ == nullptr) {
instance_ = new StorageClass;
}
return instance_;
}
*/
StorageClass() noexcept:
SubSystemServer("StorageClass", "STORAGE-SVR", "storage")
{
}
int Start() override {
std::lock_guard Guard(Mutex_);
Logger_.setLevel(Poco::Message::PRIO_NOTICE);
Logger_.notice("Starting.");
std::string DBType = MicroService::instance().ConfigGetString("storage.type");
if (DBType == "sqlite") {
Setup_SQLite();
} else if (DBType == "postgresql") {
Setup_PostgreSQL();
} else if (DBType == "mysql") {
Setup_MySQL();
}
return 0;
}
void Stop() override {
}
[[nodiscard]] inline std::string ComputeRange(uint64_t From, uint64_t HowMany) {
if(dbType_==sqlite) {
return " LIMIT " + std::to_string(From-1) + ", " + std::to_string(HowMany) + " ";
} else if(dbType_==pgsql) {
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
} else if(dbType_==mysql) {
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
}
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
}
inline std::string ConvertParams(const std::string & S) const {
std::string R;
R.reserve(S.size()*2+1);
if(dbType_==pgsql) {
auto Idx=1;
for(auto const & i:S)
{
if(i=='?') {
R += '$';
R.append(std::to_string(Idx++));
} else {
R += i;
}
}
} else {
R = S;
}
return R;
}
private:
inline int Setup_SQLite();
inline int Setup_MySQL();
inline int Setup_PostgreSQL();
protected:
std::unique_ptr<Poco::Data::SessionPool> Pool_;
std::unique_ptr<Poco::Data::SQLite::Connector> SQLiteConn_;
std::unique_ptr<Poco::Data::PostgreSQL::Connector> PostgresConn_;
std::unique_ptr<Poco::Data::MySQL::Connector> MySQLConn_;
DBType dbType_ = sqlite;
};
// inline StorageClass * Storage() { return StorageClass::instance(); }
#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 = MicroService::instance().DataDir() + "/" + MicroService::instance().ConfigGetString("storage.type.sqlite.db");
auto NumSessions = MicroService::instance().ConfigGetInt("storage.type.sqlite.maxsessions", 64);
auto IdleTime = MicroService::instance().ConfigGetInt("storage.type.sqlite.idletime", 60);
SQLiteConn_ = std::make_unique<Poco::Data::SQLite::Connector>();
SQLiteConn_->registerConnector();
Pool_ = std::make_unique<Poco::Data::SessionPool>(SQLiteConn_->name(), DBName, 4, NumSessions, IdleTime);
return 0;
}
inline int StorageClass::Setup_MySQL() {
Logger_.notice("MySQL StorageClass enabled.");
dbType_ = mysql;
auto NumSessions = MicroService::instance().ConfigGetInt("storage.type.mysql.maxsessions", 64);
auto IdleTime = MicroService::instance().ConfigGetInt("storage.type.mysql.idletime", 60);
auto Host = MicroService::instance().ConfigGetString("storage.type.mysql.host");
auto Username = MicroService::instance().ConfigGetString("storage.type.mysql.username");
auto Password = MicroService::instance().ConfigGetString("storage.type.mysql.password");
auto Database = MicroService::instance().ConfigGetString("storage.type.mysql.database");
auto Port = MicroService::instance().ConfigGetString("storage.type.mysql.port");
std::string ConnectionStr =
"host=" + Host +
";user=" + Username +
";password=" + Password +
";db=" + Database +
";port=" + Port +
";compress=true;auto-reconnect=true";
MySQLConn_ = std::make_unique<Poco::Data::MySQL::Connector>();
MySQLConn_->registerConnector();
Pool_ = std::make_unique<Poco::Data::SessionPool>(MySQLConn_->name(), ConnectionStr, 4, NumSessions, IdleTime);
return 0;
}
inline int StorageClass::Setup_PostgreSQL() {
Logger_.notice("PostgreSQL StorageClass enabled.");
dbType_ = pgsql;
auto NumSessions = MicroService::instance().ConfigGetInt("storage.type.postgresql.maxsessions", 64);
auto IdleTime = MicroService::instance().ConfigGetInt("storage.type.postgresql.idletime", 60);
auto Host = MicroService::instance().ConfigGetString("storage.type.postgresql.host");
auto Username = MicroService::instance().ConfigGetString("storage.type.postgresql.username");
auto Password = MicroService::instance().ConfigGetString("storage.type.postgresql.password");
auto Database = MicroService::instance().ConfigGetString("storage.type.postgresql.database");
auto Port = MicroService::instance().ConfigGetString("storage.type.postgresql.port");
auto ConnectionTimeout = MicroService::instance().ConfigGetString("storage.type.postgresql.connectiontimeout");
std::string ConnectionStr =
"host=" + Host +
" user=" + Username +
" password=" + Password +
" dbname=" + Database +
" port=" + Port +
" connect_timeout=" + ConnectionTimeout;
PostgresConn_ = std::make_unique<Poco::Data::PostgreSQL::Connector>();
PostgresConn_->registerConnector();
Pool_ = std::make_unique<Poco::Data::SessionPool>(PostgresConn_->name(), ConnectionStr, 4, NumSessions, IdleTime);
return 0;
}
#endif
}
#endif //OPENWIFI_STORAGE_H

View File

@@ -1,306 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include "SubSystemServer.h"
#include "Daemon.h"
#include "Poco/Net/X509Certificate.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h"
#include "Poco/Net/PrivateKeyPassphraseHandler.h"
#include "Poco/Net/SSLManager.h"
#include "openssl/ssl.h"
#include "Daemon.h"
namespace OpenWifi {
SubSystemServer::SubSystemServer(std::string Name, const std::string &LoggingPrefix,
std::string SubSystemConfigPrefix)
: Name_(std::move(Name)), Logger_(Poco::Logger::get(LoggingPrefix)),
SubSystemConfigPrefix_(std::move(SubSystemConfigPrefix)) {
Logger_.setLevel(Poco::Message::PRIO_NOTICE);
}
void SubSystemServer::initialize(Poco::Util::Application &self) {
Logger_.notice("Initializing...");
auto i = 0;
bool good = true;
ConfigServersList_.clear();
while (good) {
std::string root{SubSystemConfigPrefix_ + ".host." + std::to_string(i) + "."};
std::string address{root + "address"};
if (Daemon()->ConfigGetString(address, "").empty()) {
good = false;
} else {
std::string port{root + "port"};
std::string key{root + "key"};
std::string key_password{root + "key.password"};
std::string cert{root + "cert"};
std::string name{root + "name"};
std::string backlog{root + "backlog"};
std::string rootca{root + "rootca"};
std::string issuer{root + "issuer"};
std::string clientcas(root + "clientcas");
std::string cas{root + "cas"};
std::string level{root + "security"};
Poco::Net::Context::VerificationMode M = Poco::Net::Context::VERIFY_RELAXED;
auto L = Daemon()->ConfigGetString(level, "");
if (L == "strict") {
M = Poco::Net::Context::VERIFY_STRICT;
} else if (L == "none") {
M = Poco::Net::Context::VERIFY_NONE;
} else if (L == "relaxed") {
M = Poco::Net::Context::VERIFY_RELAXED;
} else if (L == "once")
M = Poco::Net::Context::VERIFY_ONCE;
PropertiesFileServerEntry entry(Daemon()->ConfigGetString(address, ""),
Daemon()->ConfigGetInt(port, 0),
Daemon()->ConfigPath(key, ""),
Daemon()->ConfigPath(cert, ""),
Daemon()->ConfigPath(rootca, ""),
Daemon()->ConfigPath(issuer, ""),
Daemon()->ConfigPath(clientcas, ""),
Daemon()->ConfigPath(cas, ""),
Daemon()->ConfigGetString(key_password, ""),
Daemon()->ConfigGetString(name, ""), M,
(int)Daemon()->ConfigGetInt(backlog, 64));
ConfigServersList_.push_back(entry);
i++;
}
}
}
void SubSystemServer::uninitialize() {
}
void SubSystemServer::reinitialize(Poco::Util::Application &self) {
Logger_.information("Reloading of this subsystem is not supported.");
}
void SubSystemServer::defineOptions(Poco::Util::OptionSet &options) {}
class MyPrivateKeyPassphraseHandler : public Poco::Net::PrivateKeyPassphraseHandler {
public:
explicit MyPrivateKeyPassphraseHandler(const std::string &Password, Poco::Logger & Logger):
PrivateKeyPassphraseHandler(true),
Logger_(Logger),
Password_(Password) {}
void onPrivateKeyRequested(const void * pSender,std::string & privateKey) {
Logger_.information("Returning key passphrase.");
privateKey = Password_;
};
private:
std::string Password_;
Poco::Logger & Logger_;
};
Poco::Net::SecureServerSocket PropertiesFileServerEntry::CreateSecureSocket(Poco::Logger &L) const {
Poco::Net::Context::Params P;
P.verificationMode = level_;
P.verificationDepth = 9;
P.loadDefaultCAs = root_ca_.empty();
P.cipherList = "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH";
P.dhUse2048Bits = true;
P.caLocation = cas_;
auto Context = Poco::AutoPtr<Poco::Net::Context>(new Poco::Net::Context(Poco::Net::Context::TLS_SERVER_USE, P));
if(!key_file_password_.empty()) {
auto PassphraseHandler = Poco::SharedPtr<MyPrivateKeyPassphraseHandler>( new MyPrivateKeyPassphraseHandler(key_file_password_,L));
Poco::Net::SSLManager::instance().initializeServer(PassphraseHandler, nullptr,Context);
}
if (!cert_file_.empty() && !key_file_.empty()) {
Poco::Crypto::X509Certificate Cert(cert_file_);
Poco::Crypto::X509Certificate Root(root_ca_);
Context->useCertificate(Cert);
Context->addChainCertificate(Root);
Context->addCertificateAuthority(Root);
if (level_ == Poco::Net::Context::VERIFY_STRICT) {
if (issuer_cert_file_.empty()) {
L.fatal("In strict mode, you must supply ans issuer certificate");
}
if (client_cas_.empty()) {
L.fatal("In strict mode, client cas must be supplied");
}
Poco::Crypto::X509Certificate Issuing(issuer_cert_file_);
Context->addChainCertificate(Issuing);
Context->addCertificateAuthority(Issuing);
}
Poco::Crypto::RSAKey Key("", key_file_, key_file_password_);
Context->usePrivateKey(Key);
SSL_CTX *SSLCtx = Context->sslContext();
if (!SSL_CTX_check_private_key(SSLCtx)) {
L.fatal(Poco::format("Wrong Certificate(%s) for Key(%s)", cert_file_, key_file_));
}
SSL_CTX_set_verify(SSLCtx, SSL_VERIFY_PEER, nullptr);
if (level_ == Poco::Net::Context::VERIFY_STRICT) {
SSL_CTX_set_client_CA_list(SSLCtx, SSL_load_client_CA_file(client_cas_.c_str()));
}
SSL_CTX_enable_ct(SSLCtx, SSL_CT_VALIDATION_STRICT);
SSL_CTX_dane_enable(SSLCtx);
Context->enableSessionCache();
Context->setSessionCacheSize(0);
Context->setSessionTimeout(10);
Context->enableExtendedCertificateVerification(true);
Context->disableStatelessSessionResumption();
}
if (address_ == "*") {
Poco::Net::IPAddress Addr(Poco::Net::IPAddress::wildcard(
Poco::Net::Socket::supportsIPv6() ? Poco::Net::AddressFamily::IPv6
: Poco::Net::AddressFamily::IPv4));
Poco::Net::SocketAddress SockAddr(Addr, port_);
return Poco::Net::SecureServerSocket(SockAddr, backlog_, Context);
} else {
Poco::Net::IPAddress Addr(address_);
Poco::Net::SocketAddress SockAddr(Addr, port_);
return Poco::Net::SecureServerSocket(SockAddr, backlog_, Context);
}
}
void PropertiesFileServerEntry::LogCertInfo(Poco::Logger &L,
const Poco::Crypto::X509Certificate &C) {
L.information("=============================================================================================");
L.information(Poco::format("> Issuer: %s", C.issuerName()));
L.information("---------------------------------------------------------------------------------------------");
L.information(Poco::format("> Common Name: %s",
C.issuerName(Poco::Crypto::X509Certificate::NID_COMMON_NAME)));
L.information(Poco::format("> Country: %s",
C.issuerName(Poco::Crypto::X509Certificate::NID_COUNTRY)));
L.information(Poco::format("> Locality: %s",
C.issuerName(Poco::Crypto::X509Certificate::NID_LOCALITY_NAME)));
L.information(Poco::format("> State/Prov: %s",
C.issuerName(Poco::Crypto::X509Certificate::NID_STATE_OR_PROVINCE)));
L.information(Poco::format("> Org name: %s",
C.issuerName(Poco::Crypto::X509Certificate::NID_ORGANIZATION_NAME)));
L.information(
Poco::format("> Org unit: %s",
C.issuerName(Poco::Crypto::X509Certificate::NID_ORGANIZATION_UNIT_NAME)));
L.information(
Poco::format("> Email: %s",
C.issuerName(Poco::Crypto::X509Certificate::NID_PKCS9_EMAIL_ADDRESS)));
L.information(Poco::format("> Serial#: %s",
C.issuerName(Poco::Crypto::X509Certificate::NID_SERIAL_NUMBER)));
L.information("---------------------------------------------------------------------------------------------");
L.information(Poco::format("> Subject: %s", C.subjectName()));
L.information("---------------------------------------------------------------------------------------------");
L.information(Poco::format("> Common Name: %s",
C.subjectName(Poco::Crypto::X509Certificate::NID_COMMON_NAME)));
L.information(Poco::format("> Country: %s",
C.subjectName(Poco::Crypto::X509Certificate::NID_COUNTRY)));
L.information(Poco::format("> Locality: %s",
C.subjectName(Poco::Crypto::X509Certificate::NID_LOCALITY_NAME)));
L.information(
Poco::format("> State/Prov: %s",
C.subjectName(Poco::Crypto::X509Certificate::NID_STATE_OR_PROVINCE)));
L.information(
Poco::format("> Org name: %s",
C.subjectName(Poco::Crypto::X509Certificate::NID_ORGANIZATION_NAME)));
L.information(
Poco::format("> Org unit: %s",
C.subjectName(Poco::Crypto::X509Certificate::NID_ORGANIZATION_UNIT_NAME)));
L.information(
Poco::format("> Email: %s",
C.subjectName(Poco::Crypto::X509Certificate::NID_PKCS9_EMAIL_ADDRESS)));
L.information(Poco::format("> Serial#: %s",
C.subjectName(Poco::Crypto::X509Certificate::NID_SERIAL_NUMBER)));
L.information("---------------------------------------------------------------------------------------------");
L.information(Poco::format("> Signature Algo: %s", C.signatureAlgorithm()));
auto From = Poco::DateTimeFormatter::format(C.validFrom(), Poco::DateTimeFormat::HTTP_FORMAT);
L.information(Poco::format("> Valid from: %s", From));
auto Expires =
Poco::DateTimeFormatter::format(C.expiresOn(), Poco::DateTimeFormat::HTTP_FORMAT);
L.information(Poco::format("> Expires on: %s", Expires));
L.information(Poco::format("> Version: %d", (int)C.version()));
L.information(Poco::format("> Serial #: %s", C.serialNumber()));
L.information("=============================================================================================");
}
void PropertiesFileServerEntry::LogCert(Poco::Logger &L) const {
try {
Poco::Crypto::X509Certificate C(cert_file_);
L.information("=============================================================================================");
L.information("=============================================================================================");
L.information(Poco::format("Certificate Filename: %s", cert_file_));
LogCertInfo(L, C);
L.information("=============================================================================================");
if (!issuer_cert_file_.empty()) {
Poco::Crypto::X509Certificate C1(issuer_cert_file_);
L.information("=============================================================================================");
L.information("=============================================================================================");
L.information(Poco::format("Issues Certificate Filename: %s", issuer_cert_file_));
LogCertInfo(L, C1);
L.information("=============================================================================================");
}
if (!client_cas_.empty()) {
std::vector<Poco::Crypto::X509Certificate> Certs =
Poco::Net::X509Certificate::readPEM(client_cas_);
L.information("=============================================================================================");
L.information("=============================================================================================");
L.information(Poco::format("Client CAs Filename: %s", client_cas_));
L.information("=============================================================================================");
auto i = 1;
for (const auto &C3 : Certs) {
L.information(Poco::format(" Index: %d", i));
L.information("=============================================================================================");
LogCertInfo(L, C3);
i++;
}
L.information("=============================================================================================");
}
} catch (const Poco::Exception &E) {
L.log(E);
}
}
void PropertiesFileServerEntry::LogCas(Poco::Logger &L) const {
try {
std::vector<Poco::Crypto::X509Certificate> Certs =
Poco::Net::X509Certificate::readPEM(root_ca_);
L.information("=============================================================================================");
L.information("=============================================================================================");
L.information(Poco::format("CA Filename: %s", root_ca_));
L.information("=============================================================================================");
auto i = 1;
for (const auto &C : Certs) {
L.information(Poco::format(" Index: %d", i));
L.information("=============================================================================================");
LogCertInfo(L, C);
i++;
}
L.information("=============================================================================================");
} catch (const Poco::Exception &E) {
L.log(E);
}
}
}

View File

@@ -1,95 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRAL_SUBSYSTEMSERVER_H
#define UCENTRAL_SUBSYSTEMSERVER_H
#include <mutex>
#include "Poco/Util/Application.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Logger.h"
#include "Poco/Net/SecureServerSocket.h"
#include "Poco/Net/X509Certificate.h"
namespace OpenWifi {
class PropertiesFileServerEntry {
public:
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 =
Poco::Net::Context::VerificationMode::VERIFY_RELAXED,
int backlog = 64)
: address_(std::move(Address)), port_(port), key_file_(std::move(Key_file)),
cert_file_(std::move(Cert_file)), root_ca_(std::move(RootCa)),
issuer_cert_file_(std::move(Issuer)), client_cas_(std::move(ClientCas)),
cas_(std::move(Cas)), key_file_password_(std::move(Key_file_password)),
name_(std::move(Name)), level_(M), backlog_(backlog){};
[[nodiscard]] const std::string &Address() const { return address_; };
[[nodiscard]] uint32_t Port() const { return port_; };
[[nodiscard]] const std::string &KeyFile() const { return key_file_; };
[[nodiscard]] const std::string &CertFile() const { return cert_file_; };
[[nodiscard]] const std::string &RootCA() const { return root_ca_; };
[[nodiscard]] const std::string &KeyFilePassword() const { return key_file_password_; };
[[nodiscard]] const std::string &IssuerCertFile() const { return issuer_cert_file_; };
[[nodiscard]] const std::string &Name() const { return name_; };
[[nodiscard]] Poco::Net::SecureServerSocket CreateSecureSocket(Poco::Logger &L) const;
[[nodiscard]] int Backlog() const { return backlog_; }
void LogCert(Poco::Logger &L) const;
void LogCas(Poco::Logger &L) const;
static void LogCertInfo(Poco::Logger &L, const Poco::Crypto::X509Certificate &C);
private:
std::string address_;
std::string cert_file_;
std::string key_file_;
std::string root_ca_;
std::string key_file_password_;
std::string issuer_cert_file_;
std::string client_cas_;
std::string cas_;
uint32_t port_;
std::string name_;
int backlog_;
Poco::Net::Context::VerificationMode level_;
};
class SubSystemServer : public Poco::Util::Application::Subsystem {
public:
SubSystemServer(std::string Name, const std::string &LoggingName, std::string SubSystemPrefix);
void initialize(Poco::Util::Application &self) override;
void uninitialize() override;
void reinitialize(Poco::Util::Application &self) override;
void defineOptions(Poco::Util::OptionSet &options) override;
inline const std::string & Name() const { return Name_; };
const char * name() const override { return Name_.c_str(); }
const PropertiesFileServerEntry & Host(uint64_t index) { return ConfigServersList_[index]; };
uint64_t HostSize() const { return ConfigServersList_.size(); }
Poco::Logger &Logger() { return Logger_; };
void SetLoggingLevel(Poco::Message::Priority NewPriority) { Logger_.setLevel(NewPriority); }
int GetLoggingLevel() { return Logger_.getLevel(); }
virtual int Start() = 0;
virtual void Stop() = 0;
protected:
std::recursive_mutex Mutex_;
Poco::Logger &Logger_;
std::string Name_;
std::vector<PropertiesFileServerEntry> ConfigServersList_;
std::string SubSystemConfigPrefix_;
};
}
#endif //UCENTRAL_SUBSYSTEMSERVER_H

View File

@@ -1,523 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include <stdexcept>
#include <fstream>
#include <cstdlib>
#include <regex>
#include <random>
#include <chrono>
#include "Utils.h"
#include "Poco/Exception.h"
#include "Poco/DateTimeFormat.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTime.h"
#include "Poco/DateTimeParser.h"
#include "Poco/StringTokenizer.h"
#include "Poco/Message.h"
#include "Poco/File.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPResponse.h"
#include "uCentralProtocol.h"
#include "Daemon.h"
namespace OpenWifi::Utils {
[[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]] std::vector<std::string> Split(const std::string &List, char Delimiter ) {
std::vector<std::string> ReturnList;
unsigned long P=0;
while(P<List.size())
{
unsigned long P2 = List.find_first_of(Delimiter, P);
if(P2==std::string::npos) {
ReturnList.push_back(List.substr(P));
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 I;
}
[[nodiscard]] std::string SerialToMAC(const std::string &Serial) {
std::string R = Serial;
if(R.size()<12)
padTo(R,12,'0');
else if (R.size()>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;
}
[[nodiscard]] std::string ToHex(const std::vector<unsigned char> & 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 = '=';
std::string base64encode(const byte *input, unsigned long size) {
std::string encoded;
encoded.reserve(((size / 3) + (size % 3 > 0)) * 4);
std::uint32_t temp;
std::size_t i;
int ee = (int)(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;
}
return encoded;
}
std::vector<byte> 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++;
}
std::vector<byte> decoded;
decoded.reserve(((input.length() / 4) * 3) - padding);
std::uint32_t temp=0;
auto it = input.begin();
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!");
++it;
}
decoded.push_back((temp >> 16) & 0x000000FF);
decoded.push_back((temp >> 8 ) & 0x000000FF);
decoded.push_back((temp ) & 0x000000FF);
}
return decoded;
}
std::string to_RFC3339(uint64_t t)
{
if(t==0)
return "";
return Poco::DateTimeFormatter::format(Poco::DateTime(Poco::Timestamp::fromEpochTime(t)), Poco::DateTimeFormat::ISO8601_FORMAT);
}
uint64_t from_RFC3339(const std::string &TimeString)
{
if(TimeString.empty() || TimeString=="0")
return 0;
try {
int TZ;
Poco::DateTime DT = Poco::DateTimeParser::parse(Poco::DateTimeFormat::ISO8601_FORMAT,TimeString,TZ);
return DT.timestamp().epochTime();
}
catch( const Poco::Exception & E )
{
}
return 0;
}
bool ParseTime(const std::string &Time, int & Hours, int & Minutes, int & Seconds) {
Poco::StringTokenizer TimeTokens(Time,":",Poco::StringTokenizer::TOK_TRIM);
Hours = Minutes = Hours = 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;
}
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";
}
}
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));
}
uint64_t SerialNumberToInt(const std::string & S) {
uint64_t R=0;
for(const auto &i:S)
if(i>='0' && i<='9') {
R <<= 4;
R += (i-'0');
} else if(i>='a' && i<='f') {
R <<= 4;
R += (i-'a') + 10 ;
} else if(i>='A' && i<='F') {
R <<= 4;
R += (i-'A') + 10 ;
}
return R;
}
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;
}
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;
}
void SaveSystemId(uint64_t Id) {
try {
std::ofstream O;
O.open(Daemon()->DataDir() + "/system.id",std::ios::binary | std::ios::trunc);
O << Id;
O.close();
} catch (...)
{
std::cout << "Could not save system ID" << std::endl;
}
}
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) ;
SaveSystemId(S);
std::cout << "ID: " << S << std::endl;
return S;
}
uint64_t GetSystemId() {
uint64_t ID=0;
// if the system ID file exists, open and read it.
Poco::File SID( Daemon()->DataDir() + "/system.id");
try {
if (SID.exists()) {
std::ifstream I;
I.open(SID.path());
I >> ID;
I.close();
if (ID == 0)
return InitializeSystemId();
return ID;
} else {
return InitializeSystemId();
}
} catch (...) {
return InitializeSystemId();
}
}
bool ValidEMailAddress(const std::string &email) {
// define a regular expression
const std::regex pattern
("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
// try to match the string with the regular expression
return std::regex_match(email, pattern);
}
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);
}
}
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" };
}
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;
}
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;
}
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;
}
}

View File

@@ -1,89 +0,0 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#ifndef UCENTRALGW_UTILS_H
#define UCENTRALGW_UTILS_H
#include <vector>
#include <string>
#include <iomanip>
#include <sstream>
#include "Poco/Net/NetworkInterface.h"
#include "Poco/Net/IPAddress.h"
#include "Poco/String.h"
#include "Poco/File.h"
#include "framework/OpenWifiTypes.h"
#define DBGLINE { std::cout << __FILE__ << ":" << __func__ << ":" << __LINE__ << std::endl; };
namespace OpenWifi::Utils {
enum MediaTypeEncodings {
PLAIN,
BINARY,
BASE64
};
struct MediaTypeEncoding {
MediaTypeEncodings Encoding=PLAIN;
std::string ContentType;
};
[[nodiscard]] std::vector<std::string> Split(const std::string &List, char Delimiter=',');
[[nodiscard]] std::string FormatIPv6(const std::string & I );
inline void padTo(std::string& str, size_t num, char paddingChar = '\0') {
str.append(num - str.length() % num, paddingChar);
}
[[nodiscard]] std::string SerialToMAC(const std::string &Serial);
[[nodiscard]] std::string ToHex(const std::vector<unsigned char> & B);
using byte = std::uint8_t;
[[nodiscard]] std::string base64encode(const byte *input, unsigned long size);
std::vector<byte> base64decode(const std::string& input);
// [[nodiscard]] std::string to_RFC3339(uint64_t t);
// [[nodiscard]] uint64_t from_RFC3339(const std::string &t);
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]] bool ValidSerialNumber(const std::string &Serial);
[[nodiscard]] std::string LogLevelToString(int Level);
[[nodiscard]] bool SerialNumberMatch(const std::string &S1, const std::string &S2, int extrabits=2);
[[nodiscard]] uint64_t SerialNumberToInt(const std::string & S);
[[nodiscard]] uint64_t SerialNumberToOUI(const std::string & S);
[[nodiscard]] uint64_t GetDefaultMacAsInt64();
[[nodiscard]] uint64_t GetSystemId();
[[nodiscard]] bool ValidEMailAddress(const std::string &E);
[[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);
template< typename T >
std::string int_to_hex( T i )
{
std::stringstream stream;
stream << std::setfill ('0') << std::setw(12)
<< std::hex << i;
return stream.str();
}
}
#endif // UCENTRALGW_UTILS_H

View File

@@ -24,7 +24,7 @@
#include "Poco/Data/SQLite/Connector.h"
#include "Poco/Logger.h"
#include "Poco/StringTokenizer.h"
#include "Storage.h"
#include "StorageClass.h"
namespace ORM {

View File

@@ -1,83 +0,0 @@
//
// Created by stephane bourque on 2021-08-22.
//
#include "StorageService.h"
#include "Daemon.h"
namespace OpenWifi {
#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
int Storage::Setup_SQLite() {
Logger_.notice("SQLite Storage enabled.");
dbType_ = sqlite;
auto DBName = Daemon()->DataDir() + "/" + Daemon()->ConfigGetString("storage.type.sqlite.db");
auto NumSessions = Daemon()->ConfigGetInt("storage.type.sqlite.maxsessions", 64);
auto IdleTime = Daemon()->ConfigGetInt("storage.type.sqlite.idletime", 60);
SQLiteConn_ = std::make_unique<Poco::Data::SQLite::Connector>();
SQLiteConn_->registerConnector();
Pool_ = std::make_unique<Poco::Data::SessionPool>(SQLiteConn_->name(), DBName, 4, NumSessions, IdleTime);
return 0;
}
int Storage::Setup_MySQL() {
Logger_.notice("MySQL Storage enabled.");
dbType_ = mysql;
auto NumSessions = Daemon()->ConfigGetInt("storage.type.mysql.maxsessions", 64);
auto IdleTime = Daemon()->ConfigGetInt("storage.type.mysql.idletime", 60);
auto Host = Daemon()->ConfigGetString("storage.type.mysql.host");
auto Username = Daemon()->ConfigGetString("storage.type.mysql.username");
auto Password = Daemon()->ConfigGetString("storage.type.mysql.password");
auto Database = Daemon()->ConfigGetString("storage.type.mysql.database");
auto Port = Daemon()->ConfigGetString("storage.type.mysql.port");
std::string ConnectionStr =
"host=" + Host +
";user=" + Username +
";password=" + Password +
";db=" + Database +
";port=" + Port +
";compress=true;auto-reconnect=true";
MySQLConn_ = std::make_unique<Poco::Data::MySQL::Connector>();
MySQLConn_->registerConnector();
Pool_ = std::make_unique<Poco::Data::SessionPool>(MySQLConn_->name(), ConnectionStr, 4, NumSessions, IdleTime);
return 0;
}
int Storage::Setup_PostgreSQL() {
Logger_.notice("PostgreSQL Storage enabled.");
dbType_ = pgsql;
auto NumSessions = Daemon()->ConfigGetInt("storage.type.postgresql.maxsessions", 64);
auto IdleTime = Daemon()->ConfigGetInt("storage.type.postgresql.idletime", 60);
auto Host = Daemon()->ConfigGetString("storage.type.postgresql.host");
auto Username = Daemon()->ConfigGetString("storage.type.postgresql.username");
auto Password = Daemon()->ConfigGetString("storage.type.postgresql.password");
auto Database = Daemon()->ConfigGetString("storage.type.postgresql.database");
auto Port = Daemon()->ConfigGetString("storage.type.postgresql.port");
auto ConnectionTimeout = Daemon()->ConfigGetString("storage.type.postgresql.connectiontimeout");
std::string ConnectionStr =
"host=" + Host +
" user=" + Username +
" password=" + Password +
" dbname=" + Database +
" port=" + Port +
" connect_timeout=" + ConnectionTimeout;
PostgresConn_ = std::make_unique<Poco::Data::PostgreSQL::Connector>();
PostgresConn_->registerConnector();
Pool_ = std::make_unique<Poco::Data::SessionPool>(PostgresConn_->name(), ConnectionStr, 4, NumSessions, IdleTime);
return 0;
}
#endif
}

Some files were not shown because too many files have changed in this diff Show More