Skip to content

Commit

Permalink
Adding default subscriber entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephb9959 committed Apr 5, 2022
1 parent 65d7b28 commit f36a9f5
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 4 deletions.
6 changes: 6 additions & 0 deletions SUBSCRIBERS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Subscribers Architecture

## Overview
The goal is to provide multiple WISPs with access to a set of subscribers. All subscribers will fall in the default WISP and can be moved to any other WISP later. You can use the source IP to detect which WISP to select.
Entities can be generic entities when created OR WISP entities.

19 changes: 19 additions & 0 deletions openapi/owprov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ components:
- inherit
sourceIP:
$ref: '#/components/schemas/StringList'
defaultEntity:
type: boolean
default: false
type:
type: string
enum:
- normal
- subscriber
default: normal

EntityList:
type: object
Expand Down Expand Up @@ -1165,6 +1174,16 @@ paths:
schema:
type: boolean
required: false
- in: query
decription: entity type
name: type
schema:
type: string
enum:
- subscriber
- normal
default: normal
required: false

responses:
200:
Expand Down
4 changes: 4 additions & 0 deletions src/RESTAPI/RESTAPI_db_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,8 @@ namespace OpenWifi {

return Result;
}

inline bool ValidEntityType(const std::string &v) {
return (v=="normal" || v=="subscriber");
}
}
12 changes: 12 additions & 0 deletions src/RESTAPI/RESTAPI_entity_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ namespace OpenWifi{
return BadRequest(RESTAPI::Errors::CannotDeleteRoot);
}

if(Existing.type=="subscriber" && Existing.defaultEntity) {
return BadRequest(RESTAPI::Errors::CannotDeleteSubEntity);
}

if( !Existing.children.empty() || !Existing.devices.empty() || !Existing.venues.empty() || !Existing.locations.empty()
|| !Existing.contacts.empty() || !Existing.configurations.empty()) {
return BadRequest(RESTAPI::Errors::StillInUse);
Expand Down Expand Up @@ -69,6 +73,14 @@ namespace OpenWifi{
return BadRequest(RESTAPI::Errors::InvalidJSONDocument);
}

if(NewEntity.type.empty()) {
NewEntity.type = "normal";
}

if(!ValidEntityType(NewEntity.type)) {
return BadRequest(RESTAPI::Errors::InvalidEntityType);
}

if(!ProvObjects::CreateObjectInfo(Obj,UserInfo_.userinfo,NewEntity.info)) {
return BadRequest(RESTAPI::Errors::NameMustBeSet);
}
Expand Down
10 changes: 8 additions & 2 deletions src/RESTAPI/RESTAPI_entity_list_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@
namespace OpenWifi{

void RESTAPI_entity_list_handler::DoGet() {
auto type = GetParameter("type","normal");
if(!ValidEntityType(type)) {
return BadRequest(RESTAPI::Errors::InvalidEntityType);
}

if(!QB_.Select.empty()) {
return ReturnRecordList<decltype(DB_),
ProvObjects::Entity>("entities",DB_,*this );
} else if(QB_.CountOnly) {
auto C = DB_.Count();
auto C = DB_.Count(fmt::format(" where type='{}'", type));
return ReturnCountOnly(C);
} else if (GetBoolParameter("getTree",false)) {
Poco::JSON::Object FullTree;
DB_.BuildTree(FullTree);
return ReturnObject(FullTree);
} else {
EntityDB::RecordVec Entities;
DB_.GetRecords(QB_.Offset, QB_.Limit,Entities);

DB_.GetRecords(QB_.Offset, QB_.Limit,Entities,fmt::format(" where type='{}'", type));
return MakeJSONObjectArray("entities", Entities, *this);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/RESTObjects/RESTAPI_ProvObjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ namespace OpenWifi::ProvObjects {
field_to_json( Obj,"managementRoles", managementRoles);
field_to_json( Obj,"maps", maps);
field_to_json( Obj,"configurations", configurations);
field_to_json( Obj,"type", type);
field_to_json( Obj,"defaultEntity", defaultEntity);
}

bool Entity::from_json(const Poco::JSON::Object::Ptr &Obj) {
Expand All @@ -118,6 +120,8 @@ namespace OpenWifi::ProvObjects {
field_from_json( Obj,"managementRoles", managementRoles);
field_from_json( Obj,"maps", maps);
field_from_json( Obj,"configurations", configurations);
field_from_json( Obj,"type", type);
field_from_json( Obj,"defaultEntity", defaultEntity);
return true;
} catch(...) {

Expand Down
2 changes: 2 additions & 0 deletions src/RESTObjects/RESTAPI_ProvObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ namespace OpenWifi::ProvObjects {
Types::UUIDvec_t managementRoles;
Types::UUIDvec_t maps;
Types::UUIDvec_t configurations;
std::string type;
bool defaultEntity=false;

void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);
Expand Down
28 changes: 28 additions & 0 deletions src/StorageService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ namespace OpenWifi {

ConsistencyCheck();

CreateDefaultSubscriberEntity();

TimerCallback_ = std::make_unique<Poco::TimerCallback<Storage>>(*this,&Storage::onTimer);
Timer_.setStartInterval( 20 * 1000); // first run in 20 seconds
Timer_.setPeriodicInterval(1 * 60 * 60 * 1000); // 1 hours
Expand Down Expand Up @@ -222,6 +224,32 @@ namespace OpenWifi {

}

void Storage::CreateDefaultSubscriberEntity() {

std::vector<ProvObjects::Entity> Entities;
if(EntityDB().GetRecords(0,1,Entities, " where type='subscriber' and defaultEntity=true ")) {
DefaultSubscriberEntity_ = Entities[0].info.id;
} else {
ProvObjects::Entity DefEntity;

DefEntity.info.name = "Default Subscriber Entity";
DefaultSubscriberEntity_ = DefEntity.info.id = MicroService::CreateUUID();
DefEntity.type = "subscriber";
DefEntity.defaultEntity = true;
DefEntity.info.created = DefEntity.info.modified = OpenWifi::Now();
DefEntity.rrm = "inherit";
EntityDB().CreateRecord(DefEntity);
}

// To be backwards compatible, we need to assign all the subscribers that do not have an entity to
// the default entity.
// We also need to make sure that all entities have some type set.
std::vector<std::string> Script{
"update entities set type='normal' where type='' ",
fmt::format("update inventory set entity='{}' where entity='' and subscriber!='' ", DefaultSubscriberEntity_)
};
EntityDB().RunScript(Script);
}
}

// namespace
4 changes: 4 additions & 0 deletions src/StorageService.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace OpenWifi {

void onTimer(Poco::Timer & timer);

inline const std::string & DefaultSubscriberEntity() { return DefaultSubscriberEntity_; }

private:
std::unique_ptr<OpenWifi::EntityDB> EntityDB_;
std::unique_ptr<OpenWifi::PolicyDB> PolicyDB_;
Expand All @@ -76,6 +78,7 @@ namespace OpenWifi {
std::unique_ptr<OpenWifi::MapDB> MapDB_;
std::unique_ptr<OpenWifi::SignupDB> SignupDB_;
std::unique_ptr<OpenWifi::VariablesDB> VariablesDB_;
std::string DefaultSubscriberEntity_;


typedef std::function<bool(const char *FieldName, std::string &Value)> exist_func;
Expand All @@ -86,6 +89,7 @@ namespace OpenWifi {
std::unique_ptr<Poco::TimerCallback<Storage>> TimerCallback_;

void ConsistencyCheck();
void CreateDefaultSubscriberEntity();

};

Expand Down
15 changes: 15 additions & 0 deletions src/framework/orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,21 @@ namespace ORM {
return false;
}

bool RunStatement(const std::string &St) {
try {
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Command(Session);

Command << St ;
Command.execute();

return true;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}

template <typename T> bool ReplaceRecord(field_name_t FieldName, const T & Value, RecordType & R) {
try {
if(Exists(FieldName, Value)) {
Expand Down
2 changes: 2 additions & 0 deletions src/framework/ow_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ namespace OpenWifi::RESTAPI::Errors {
static const std::string UserAlreadyExists{"Username already exists."};
static const std::string NotImplemented{"Function not implemented."};
static const std::string VariableMustExist{"Specified variable does not exist."};
static const std::string InvalidEntityType{"Invalid entity type."};
static const std::string CannotDeleteSubEntity{"Cannot delete the default subscriber entity."};
}

namespace OpenWifi::RESTAPI::Protocol {
Expand Down
10 changes: 9 additions & 1 deletion src/storage/storage_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ namespace OpenWifi {
ORM::Field{"managementPolicies",ORM::FieldType::FT_TEXT},
ORM::Field{"managementRoles",ORM::FieldType::FT_TEXT},
ORM::Field{"maps",ORM::FieldType::FT_TEXT},
ORM::Field{"configurations",ORM::FieldType::FT_TEXT}
ORM::Field{"configurations",ORM::FieldType::FT_TEXT},
ORM::Field{"type",ORM::FieldType::FT_TEXT},
ORM::Field{"defaultEntity",ORM::FieldType::FT_TEXT}
};

static ORM::IndexVec EntityDB_Indexes{
Expand All @@ -62,6 +64,8 @@ namespace OpenWifi {
"alter table " + TableName_ + " add column managementPolicies text",
"alter table " + TableName_ + " add column maps text",
"alter table " + TableName_ + " add column configurations text",
"alter table " + TableName_ + " add column type text",
"alter table " + TableName_ + " add column defaultEntity boolean",
"alter table " + TableName_ + " add column managementRoles text"
};

Expand Down Expand Up @@ -230,6 +234,8 @@ template<> void ORM::DB< OpenWifi::EntityDBRecordType, OpenWifi::ProvObjects:
Out.managementRoles = OpenWifi::RESTAPI_utils::to_object_array(In.get<19>());
Out.maps = OpenWifi::RESTAPI_utils::to_object_array(In.get<20>());
Out.configurations = OpenWifi::RESTAPI_utils::to_object_array(In.get<21>());
Out.type = In.get<22>();
Out.defaultEntity = In.get<23>();
}

template<> void ORM::DB< OpenWifi::EntityDBRecordType, OpenWifi::ProvObjects::Entity>::Convert(const OpenWifi::ProvObjects::Entity &In, OpenWifi::EntityDBRecordType &Out) {
Expand All @@ -255,4 +261,6 @@ template<> void ORM::DB< OpenWifi::EntityDBRecordType, OpenWifi::ProvObjects:
Out.set<19>(OpenWifi::RESTAPI_utils::to_string(In.managementRoles));
Out.set<20>(OpenWifi::RESTAPI_utils::to_string(In.maps));
Out.set<21>(OpenWifi::RESTAPI_utils::to_string(In.configurations));
Out.set<22>(In.type);
Out.set<23>(In.defaultEntity);
}
4 changes: 3 additions & 1 deletion src/storage/storage_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ namespace OpenWifi {
std::string,
std::string,
std::string,
std::string
std::string,
std::string,
bool
> EntityDBRecordType;

class EntityDB : public ORM::DB<EntityDBRecordType, ProvObjects::Entity> {
Expand Down

0 comments on commit f36a9f5

Please sign in to comment.