Initial commit

This commit is contained in:
stephb9959
2022-03-15 13:45:50 -07:00
parent c098f641b7
commit 7db31c53de
8 changed files with 325 additions and 37 deletions

View File

@@ -36,7 +36,7 @@ namespace OpenWifi {
if(!StorageService()->BoardsDB().DeleteRecord("id",id)) {
return NotFound();
}
VenueCoordinator()->StopVenue(id);
VenueCoordinator()->StopBoard(id);
return OK();
}
@@ -55,7 +55,7 @@ namespace OpenWifi {
ProvObjects::CreateObjectInfo(RawObject,UserInfo_.userinfo,NewObject.info);
if(StorageService()->BoardsDB().CreateRecord(NewObject)) {
VenueCoordinator()->AddVenue(NewObject.info.id);
VenueCoordinator()->AddBoard(NewObject.info.id);
AnalyticsObjects::BoardInfo NewBoard;
StorageService()->BoardsDB().GetRecord("id",NewObject.info.id,NewBoard);
Poco::JSON::Object Answer;
@@ -87,7 +87,7 @@ namespace OpenWifi {
}
if(StorageService()->BoardsDB().CreateRecord(NewObject)) {
VenueCoordinator()->ModifyVenue(NewObject.info.id);
VenueCoordinator()->ModifyBoard(NewObject.info.id);
AnalyticsObjects::BoardInfo NewBoard;
StorageService()->BoardsDB().GetRecord("id",NewObject.info.id,NewBoard);
Poco::JSON::Object Answer;

View File

@@ -36,6 +36,14 @@ namespace OpenWifi {
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
inline bool operator<(const BoardInfo &bb) const {
return info.id < bb.info.id;
}
inline bool operator==(const BoardInfo &bb) const {
return info.id == bb.info.id;
}
};
struct DeviceInfo {

View File

@@ -4,30 +4,19 @@
#include "VenueCoordinator.h"
#include "VenueWatcher.h"
#include "StorageService.h"
#include "sdks/SDK_prov.h"
namespace OpenWifi {
int VenueCoordinator::Start() {
std::vector<std::string> S{
"903cb3bb1ef4",
"04f8f8fc3772",
"04f8f8fc3b02",
"24f5a207a130",
"68215f0427da"
auto F = [&](const AnalyticsObjects::BoardInfo &B) ->bool {
BoardsToWatch_.insert(B);
Logger().information(Poco::format("Starting watch for %s.", B.info.name));
return true;
};
std::vector<uint64_t> Numbers;
for(const auto &i:S)
Numbers.push_back(Utils::SerialNumberToInt(i));
std::sort(Numbers.begin(),Numbers.end());
Watchers_["id"] = std::make_shared<VenueWatcher>(std::string{"id-1"},Logger(),Numbers);
for(const auto &[_,watcher]:Watchers_)
watcher->Start();
StorageService()->BoardsDB().Iterate(F);
Worker_.start(*this);
return 0;
}
@@ -41,20 +30,69 @@ namespace OpenWifi {
void VenueCoordinator::run() {
Running_=true;
while(Running_) {
Poco::Thread::trySleep(2000);
Poco::Thread::trySleep(20000);
std::lock_guard G(Mutex_);
if(!BoardsToWatch_.empty()) {
for(auto board_to_start=BoardsToWatch_.begin(); board_to_start!=BoardsToWatch_.end(); ) {
if(StartBoard(*board_to_start)) {
board_to_start = BoardsToWatch_.erase(board_to_start);
} else {
++board_to_start;
}
}
}
}
}
void VenueCoordinator::StopVenue(const std::string &id) {
bool VenueCoordinator::StartBoard(const AnalyticsObjects::BoardInfo &B) {
if(B.venueList.empty())
return true;
ProvObjects::VenueDeviceList VDL;
if(SDK::Prov::Venue::GetDevices(nullptr,B.venueList[0].id,B.venueList[0].monitorSubVenues, VDL)) {
std::vector<uint64_t> Devices;
for(const auto &device:VDL.devices) {
Devices.push_back(Utils::SerialNumberToInt(device));
}
std::sort(Devices.begin(),Devices.end());
auto LastDevice = std::unique(Devices.begin(),Devices.end());
Devices.erase(LastDevice,Devices.end());
Watchers_[B.venueList[0].id] = std::make_shared<VenueWatcher>(B.info.id,Logger(),Devices);
Watchers_[B.venueList[0].id]->Start();
Logger().information(Poco::format("Started board %s",B.info.name));
return true;
}
Logger().information(Poco::format("Could not start board %s",B.info.name));
return false;
}
void VenueCoordinator::StopBoard(const std::string &id) {
std::lock_guard G(Mutex_);
auto it = Watchers_.find(id);
if(it!=Watchers_.end()) {
it->second->Stop();
Watchers_.erase(it);
}
}
void VenueCoordinator::ModifyBoard(const std::string &id) {
}
void VenueCoordinator::ModifyVenue(const std::string &id) {
}
void VenueCoordinator::AddVenue(const std::string &id) {
void VenueCoordinator::AddBoard(const std::string &id) {
std::lock_guard G(Mutex_);
AnalyticsObjects::BoardInfo B;
if(StorageService()->BoardsDB().GetRecord("id",id,B))
BoardsToWatch_.insert(B);
else
Logger().information(Poco::format("Board %d does not seem to exist",id));
}
void VenueCoordinator::GetDevices(std::string &id, AnalyticsObjects::DeviceInfoList &DIL) {

View File

@@ -20,21 +20,25 @@ namespace OpenWifi {
void Stop() override;
void run() override;
void StopVenue(const std::string &id);
void ModifyVenue(const std::string &id);
void AddVenue(const std::string &id);
void StopBoard(const std::string &id);
void ModifyBoard(const std::string &id);
void AddBoard(const std::string &id);
void GetDevices(std::string &id, AnalyticsObjects::DeviceInfoList & DIL);
private:
Poco::Thread Worker_;
std::atomic_bool Running_=false;
std::map<std::string,std::shared_ptr<VenueWatcher>> Watchers_;
Poco::Thread Worker_;
std::atomic_bool Running_=false;
std::set<AnalyticsObjects::BoardInfo> BoardsToWatch_;
std::map<std::string,std::shared_ptr<VenueWatcher>> Watchers_;
VenueCoordinator() noexcept:
SubSystemServer("VenueCoordinator", "VENUE-COORD", "venue.coordinator")
{
}
bool StartBoard(const AnalyticsObjects::BoardInfo &B);
};
inline auto VenueCoordinator() { return VenueCoordinator::instance(); }

View File

@@ -19,11 +19,11 @@ namespace OpenWifi {
}
void VenueWatcher::Stop() {
DeviceStatusReceiver()->DeRegister(this);
HealthReceiver()->DeRegister(this);
Running_ = false;
Queue_.wakeUpAll();
Worker_.join();
DeviceStatusReceiver()->DeRegister(this);
HealthReceiver()->DeRegister(this);
}
void VenueWatcher::run() {

View File

@@ -10,7 +10,7 @@
namespace OpenWifi::SDK::Prov {
namespace Venue {
bool GetDevices(RESTAPIHandler *client, const std::string &VenueId, bool WithChildren, ProvObjects::VenueDeviceList & Device);
bool GetDevices(RESTAPIHandler *client, const std::string &VenueId, bool WithChildren, ProvObjects::VenueDeviceList & Devices);
}
namespace Device {

237
test_scripts/curl/cli Executable file
View File

@@ -0,0 +1,237 @@
#!/bin/bash
#
# 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.
#
if [[ "$(which jq)" == "" ]]
then
echo "You need the package jq installed to use this script."
exit 1
fi
if [[ "$(which curl)" == "" ]]
then
echo "You need the package curl installed to use this script."
exit 1
fi
if [[ "${OWSEC}" == "" ]]
then
echo "You must set the variable OWSEC in order to use this script. Something like"
echo "OWSEC=security.isp.com:16001"
exit 1
fi
if [[ "${FLAGS}" == "" ]]
then
FLAGS="-s"
fi
token=""
result_file=result.json
browser_list=(firefox sensible-browser xdg-open w3m links links2 lynx youtube-dl)
browser=""
if [ -z ${OWSEC_USERNAME+x} ]; then
username="script.runner@arilia.com"
else
username=${OWSEC_USERNAME}
fi
if [ -z ${OWSEC_PASSWORD+x} ]; then
password="Snoopy99!!!"
else
password=${OWSEC_PASSWORD}
fi
login() {
payload="{ \"userId\" : \"$username\" , \"password\" : \"$password\" }"
token=$(curl ${FLAGS} -X POST -H "Content-Type: application/json" -d "$payload" "https://${OWSEC}/api/v1/oauth2" | jq -r '.access_token')
if [[ "${token}" == "null" ]]
then
echo "Could not login. Please verify the host and username/password."
exit 13
fi
echo "${token}" > token.json
setprov
setanalytics
}
findbrowser() {
if [[ "${browser}" != "" ]]
then
echo
elif [[ "$(uname)" == "Darwin" ]]
then
browser=open
else
BROWSER_LIST=(firefox sensible-browser xdg-open w3m links links2 lynx youtube-dl)
for br_name in "${browser_list[@]}"
do
if [[ $(which ${br_name}) != "" ]]
then
browser=${br_name}
break
fi
done
fi
}
setprov() {
if [ -z ${OWPROV_OVERRIDE+x} ]; then
curl ${FLAGS} -X GET "https://${OWSEC}/api/v1/systemEndpoints" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
rawurl="$(cat ${result_file} | jq -r '.endpoints[] | select( .type == "owprov" ) | .uri')"
if [[ ! -z "${rawurl}" ]]; then
proto="$(echo $rawurl | grep :// | sed -e's,^\(.*://\).*,\1,g')"
url="$(echo ${rawurl/$proto/})"
user="$(echo $url | grep @ | cut -d@ -f1)"
hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
host="$(echo $hostport | sed -e 's,:.*,,g')"
port="$(echo $hostport | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
path="$(echo $url | grep / | cut -d/ -f2-)"
export OWPROV=${url}
echo "Using ${OWPROV}..."
else
echo "OWPROV endpoint is not found:"
jq < ${result_file}
exit 1
fi
else
export OWPROV=${OWPROV_OVERRIDE}
fi
}
setanalytics() {
if [ -z ${OWPROV_OVERRIDE+x} ]; then
curl ${FLAGS} -X GET "https://${OWSEC}/api/v1/systemEndpoints" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
rawurl="$(cat ${result_file} | jq -r '.endpoints[] | select( .type == "owanalytics" ) | .uri')"
if [[ ! -z "${rawurl}" ]]; then
proto="$(echo $rawurl | grep :// | sed -e's,^\(.*://\).*,\1,g')"
url="$(echo ${rawurl/$proto/})"
user="$(echo $url | grep @ | cut -d@ -f1)"
hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
host="$(echo $hostport | sed -e 's,:.*,,g')"
port="$(echo $hostport | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
path="$(echo $url | grep / | cut -d/ -f2-)"
export OWANALYTICS=${url}
echo "Using ${OWANALYTICS}..."
else
echo "OWANALYTICS endpoint is not found:"
jq < ${result_file}
exit 1
fi
else
export OWANALYTICS=${OWANALYTICS_OVERRIDE}
fi
}
logout() {
curl ${FLAGS} -X DELETE -H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
"https://${OWSEC}/api/v1/oauth2/${token}"
rm -rf token.json
}
venuecount() {
curl ${FLAGS} "https://${OWPROV}/api/v1/venue?countOnly=true" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
-H "accept: application/json" > ${result_file}
jq < ${result_file}
}
contactcount() {
curl ${FLAGS} "https://${OWPROV}/api/v1/contact?countOnly=true" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
-H "accept: application/json" > ${result_file}
jq < ${result_file}
}
setloglevel() {
payload="{ \"command\" : \"setloglevel\" , \"subsystems\" : [ { \"tag\" : \"$1\" , \"value\" : \"$2\" } ] }"
curl ${FLAGS} -X POST "https://${OWPROV}/api/v1/system" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload"
}
getloglevels() {
payload="{ \"command\" : \"getloglevels\" }"
curl ${FLAGS} -X POST "https://${OWPROV}/api/v1/system" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload"
}
getloglevelnames() {
payload="{ \"command\" : \"getloglevelnames\" }"
curl ${FLAGS} -X POST "https://${OWPROV}/api/v1/system" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload"
}
getsubsystemnames() {
payload="{ \"command\" : \"getsubsystemnames\" }"
curl ${FLAGS} -X POST "https://${OWPROV}/api/v1/system" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload"
}
systeminfo() {
curl ${FLAGS} -X GET "https://${OWPROV}/api/v1/system?command=info" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
reloadsubsystem() {
payload="{ \"command\" : \"reload\", \"subsystems\" : [ \"$1\" ] }"
curl ${FLAGS} -X POST "https://${OWPROV}/api/v1/system" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload"
}
tree() {
curl ${FLAGS} "https://${OWPROV}/api/v1/entity?getTree=true" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
-H "accept: application/json" > ${result_file}
jq < ${result_file}
}
createboard() {
payload="{ \"name\" : \"MegaLab\", \"venueList\" : [ { \"id\" : \"$1\" , \"name\" : \"MegaLab Venue\" , \"retention\" : 86000 , \"interval\" : 60, \"monitorSubVenues\" : true } ] }"
curl ${FLAGS} -X POST "https://${OWANALYTICS}/api/v1/board/0" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
shopt -s nocasematch
case "$1" in
"login") login; echo "You are logged in..." ; logout ;;
"tree") login; tree ; logout;;
"setloglevel") login; setloglevel "$2" "$3" ; logout ;;
"getloglevels") login; getloglevels; logout ;;
"getloglevelnames") login; getloglevelnames; logout ;;
"getsubsystemnames") login; getsubsystemnames; logout ;;
"reloadsubsystem") login; reloadsubsystem "$2"; logout ;;
"systeminfo") login; systeminfo ; logout;;
*) help ;;
esac

View File

@@ -0,0 +1 @@
{"children":[{"children":[{"children":[{"children":[],"name":"Ottawa Operations","type":"entity","uuid":"323054d0-3ff0-11ec-82ff-061b87871e04","venues":[{"children":[],"name":"Living Lab","type":"venue","uuid":"4159ea84-3ff0-11ec-9edb-061b87871e04"}]}],"name":"Ontario","type":"entity","uuid":"21dfa464-3ff0-11ec-9a86-061b87871e04","venues":[]},{"children":[{"children":[],"name":"Quebec","type":"entity","uuid":"27b74e1b-52d7-4414-a1b4-47574c799beb","venues":[]},{"children":[],"name":"Ontario","type":"entity","uuid":"3cb15a80-3550-11ec-9a22-061b87871e04","venues":[{"children":[],"name":"CN Tower","type":"venue","uuid":"3cb28f0e-3550-11ec-acfa-061b87871e04"},{"children":[],"name":"Rogers Center","type":"venue","uuid":"3cb3c798-3550-11ec-8c1a-061b87871e04"}]},{"children":[],"name":"BC","type":"entity","uuid":"3cb503c4-3550-11ec-b436-061b87871e04","venues":[{"children":[],"name":"BC Place","type":"venue","uuid":"3cb76452-3550-11ec-a5ab-061b87871e04"},{"children":[],"name":"Stanley Park","type":"venue","uuid":"3cb8b230-3550-11ec-bbe6-061b87871e04"},{"children":[],"name":"MegaLab","type":"venue","uuid":"a73a23c4-6f3d-4031-bc92-d28c205f2d85"},{"children":[],"name":"Bowen Development","type":"venue","uuid":"d656500b-9f00-4c09-9096-e209085c2a3e"}]}],"name":"Provinces","type":"entity","uuid":"3cac92ca-3550-11ec-b360-061b87871e04","venues":[]}],"name":"Canada","type":"entity","uuid":"3cab2c32-3550-11ec-8a03-061b87871e04","venues":[]},{"children":[],"name":"USA","type":"entity","uuid":"6392c6b3-ef48-4b81-8785-1b5b4888bbd2","venues":[]},{"children":[{"children":[],"name":"Hamburg","type":"entity","uuid":"5939f995-e059-439d-ab0c-29a6f7c0a495","venues":[]}],"name":"Germany","type":"entity","uuid":"6fb5662d-dc48-4fbd-b8f9-69d0308bb902","venues":[]},{"children":[],"name":"Test Land","type":"entity","uuid":"70161a6f-98af-45d7-b0ed-e6b15ba5d84e","venues":[{"children":[],"name":"Test","type":"venue","uuid":"1d06b289-449e-4669-abc9-1f2dcdd1fbbc"}]}],"name":"World","type":"entity","uuid":"0000-0000-0000","venues":[]}