From fc138e872c18428818e9fb68e37df26315601a65 Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Tue, 26 Nov 2019 14:06:35 +0900 Subject: [PATCH] Enable policy-provider to set compare method Signed-off-by: Sangwan Kwon --- src/vist/policy/policy-manager.cpp | 49 +++++++++++++++++++++++--------------- src/vist/policy/policy-manager.hpp | 3 +++ src/vist/policy/policy-storage.cpp | 38 +++++++++-------------------- src/vist/policy/policy-storage.hpp | 5 ++-- src/vist/policy/tests/core.cpp | 30 ++++++++++++++++++----- src/vist/policy/tests/storage.cpp | 35 --------------------------- src/vist/sdk/policy-model.hpp | 14 +++++++++++ 7 files changed, 84 insertions(+), 90 deletions(-) diff --git a/src/vist/policy/policy-manager.cpp b/src/vist/policy/policy-manager.cpp index 8743388..a7431b6 100644 --- a/src/vist/policy/policy-manager.cpp +++ b/src/vist/policy/policy-manager.cpp @@ -22,6 +22,8 @@ #include +#include + namespace vist { namespace policy { @@ -106,34 +108,26 @@ void PolicyManager::set(const std::string& policy, const PolicyValue& value, const std::string& admin) { - if (this->policies.find(policy) == this->policies.end()) - THROW(ErrCode::RuntimeError) << "Not exist policy: " << policy; - this->storage.update(admin, policy, value); - - for (auto& p : providers) { - if (p->getName() != this->policies[policy]) - continue; - - /// dispatch callback written by provider - if (p->policies.find(policy) != p->policies.end()) { - p->policies[policy]->set(value); - return; - } - } + this->getPolicy(policy)->set(value); } PolicyValue PolicyManager::get(const std::string& policy) { - if (this->policies.find(policy) == this->policies.end()) - THROW(ErrCode::RuntimeError) << "Not exist policy: " << policy; - - return storage.strictest(policy); + return storage.strictest(this->getPolicy(policy)); } std::unordered_map PolicyManager::getAll() { - return storage.strictest(); + std::unordered_map policies; + for (const auto& pair : this->policies) { + std::string policyName = pair.first; + auto value = this->get(policyName); + + policies.emplace(std::move(policyName), std::move(value)); + } + + return policies; } std::vector PolicyManager::getAdmins() @@ -141,5 +135,22 @@ std::vector PolicyManager::getAdmins() return storage.getAdmins(); } +const std::shared_ptr& PolicyManager::getPolicy(const std::string& name) +{ + if (this->policies.find(name) == this->policies.end()) + THROW(ErrCode::RuntimeError) << "Not exist policy: " << name; + + auto provider = this->policies[name]; + auto iter = std::find_if(this->providers.begin(), this->providers.end(), + [&provider](const std::unique_ptr& p) { + return p->getName() == provider; + }); + if (iter == this->providers.end()) + THROW(ErrCode::RuntimeError) << "Not exist provider[" << provider + << "] about policy: " << name; + + return (*iter)->policies[name]; +} + } // namespace policy } // namespace vist diff --git a/src/vist/policy/policy-manager.hpp b/src/vist/policy/policy-manager.hpp index 103d616..b0bacc2 100644 --- a/src/vist/policy/policy-manager.hpp +++ b/src/vist/policy/policy-manager.hpp @@ -66,10 +66,13 @@ private: PolicyStorage storage; std::vector> providers; + const std::shared_ptr& getPolicy(const std::string& name); + /// Policy-Provider std::unordered_map policies; FRIEND_TEST(PolicyCoreTests, policy_loader); + FRIEND_TEST(PolicyCoreTests, policy_get_policy); }; } // namespace policy diff --git a/src/vist/policy/policy-storage.cpp b/src/vist/policy/policy-storage.cpp index ad32e5a..f7d777f 100644 --- a/src/vist/policy/policy-storage.cpp +++ b/src/vist/policy/policy-storage.cpp @@ -227,18 +227,18 @@ void PolicyStorage::update(const std::string& admin, /// TODO(sangwan.kwon) Re-design strictest logic /// PolicyValue PolicyStorage::strictest(const PolicyValue& policy) -PolicyValue PolicyStorage::strictest(const std::string& policy) +PolicyValue PolicyStorage::strictest(const std::shared_ptr& policy) { - if (this->definitions.find(policy) == this->definitions.end()) - THROW(ErrCode::LogicError) << "Not exist policy: " << policy; + if (this->definitions.find(policy->getName()) == this->definitions.end()) + THROW(ErrCode::LogicError) << "Not exist policy: " << policy->getName(); - /// There is no enrolled admins. - /// Make PolicyValue by dumped string. - if (this->activatedPolicies.size() == 0) - return PolicyValue(definitions[policy].ivalue, true); + if (this->activatedPolicies.size() == 0) { + INFO(VIST) << "There is no enrolled admin. Return policy initial value."; + return policy->getInitial(); + } std::shared_ptr strictestPtr = nullptr; - auto range = activatedPolicies.equal_range(policy); + auto range = activatedPolicies.equal_range(policy->getName()); for (auto iter = range.first; iter != range.second; iter++) { DEBUG(VIST) << "Admin: " << iter->second.admin << ", " << "Policy: " << iter->second.policy << ", " @@ -247,9 +247,7 @@ PolicyValue PolicyStorage::strictest(const std::string& policy) if (strictestPtr == nullptr) { strictestPtr = std::make_shared(iter->second.value, true); } else { - /// TODO: Support String type - int strictestValue = *strictestPtr; - if (strictestValue < PolicyValue(iter->second.value, true)) + if (policy->compare(*strictestPtr, PolicyValue(iter->second.value, true)) > 0) strictestPtr.reset(new PolicyValue(iter->second.value, true)); } } @@ -257,26 +255,12 @@ PolicyValue PolicyStorage::strictest(const std::string& policy) if (strictestPtr == nullptr) THROW(ErrCode::RuntimeError) << "Not exist managed policy: " << policy; - int strictestValue = *strictestPtr; - DEBUG(VIST) << "The strictest value of [" << policy - << "] is " << strictestValue; + DEBUG(VIST) << "The strictest value of [" << policy->getName() + << "] is " << strictestPtr->dump(); return std::move(*strictestPtr); } -std::unordered_map PolicyStorage::strictest() -{ - std::unordered_map policies; - for (const auto& pair : definitions) { - std::string name = pair.first; - auto value = this->strictest(name); - - policies.emplace(std::move(name), std::move(value)); - } - - return policies; -} - const std::vector& PolicyStorage::getAdmins() const noexcept { return admins; diff --git a/src/vist/policy/policy-storage.hpp b/src/vist/policy/policy-storage.hpp index 9def0b1..27bc27e 100644 --- a/src/vist/policy/policy-storage.hpp +++ b/src/vist/policy/policy-storage.hpp @@ -16,6 +16,7 @@ #pragma once +#include #include #include "db-schema.hpp" @@ -60,9 +61,7 @@ public: const std::string& policy, const PolicyValue& value); - PolicyValue strictest(const std::string& policy); - /// Return all strictest policy values - std::unordered_map strictest(); + PolicyValue strictest(const std::shared_ptr& policy); const std::vector& getAdmins() const noexcept; diff --git a/src/vist/policy/tests/core.cpp b/src/vist/policy/tests/core.cpp index 0c0a416..8c1ca83 100644 --- a/src/vist/policy/tests/core.cpp +++ b/src/vist/policy/tests/core.cpp @@ -21,33 +21,51 @@ namespace vist { namespace policy { -class PolicyCoreTests : public testing::Test {}; - -TEST_F(PolicyCoreTests, policy_loader) { +TEST(PolicyCoreTests, policy_loader) { auto& manager = PolicyManager::Instance(); EXPECT_TRUE(manager.providers.size() > 0); EXPECT_TRUE(manager.policies.size() > 0); } -TEST_F(PolicyCoreTests, policy_set_get) { +TEST(PolicyCoreTests, policy_set_get) { auto& manager = PolicyManager::Instance(); manager.enroll("testAdmin"); manager.set("bluetooth", PolicyValue(5), "testAdmin"); auto policy = manager.get("bluetooth"); - EXPECT_EQ((int)policy, 5); + EXPECT_EQ(static_cast(policy), 5); manager.enroll("testAdmin1"); manager.set("bluetooth", PolicyValue(10), "testAdmin1"); /// Manager should return the strongest policy. policy = manager.get("bluetooth"); - EXPECT_EQ((int)policy, 10); + EXPECT_EQ(static_cast(policy), 10); manager.disenroll("testAdmin"); manager.disenroll("testAdmin1"); } +TEST(PolicyCoreTests, policy_get_all) { + auto& manager = PolicyManager::Instance(); + auto policies = manager.getAll(); + EXPECT_TRUE(policies.size() > 0); +} + +TEST(PolicyCoreTests, policy_get_policy) { + auto& manager = PolicyManager::Instance(); + const auto& policy = manager.getPolicy("bluetooth"); + EXPECT_EQ(policy->getName(), "bluetooth"); + + bool raised = false; + try { + manager.getPolicy("fakePolicy"); + } catch (const vist::Exception&) { + raised = true; + } + EXPECT_TRUE(raised); +} + } // namespace policy } // namespace vist diff --git a/src/vist/policy/tests/storage.cpp b/src/vist/policy/tests/storage.cpp index 58d9a1f..71e57aa 100644 --- a/src/vist/policy/tests/storage.cpp +++ b/src/vist/policy/tests/storage.cpp @@ -100,41 +100,6 @@ TEST_F(PolicyStorageTests, update) storage->disenroll("testAdmin"); } -TEST_F(PolicyStorageTests, strictest) -{ - auto storage = getStorage(); - storage->enroll("testAdmin0"); - storage->enroll("testAdmin1"); - - storage->update("testAdmin0", "bluetooth", PolicyValue(3)); - storage->update("testAdmin1", "bluetooth", PolicyValue(6)); - - bool isRaised = false; - try { - auto value = storage->strictest("FakePolicy"); - } catch (const std::exception&) { - isRaised = true; - } - EXPECT_TRUE(isRaised); - - auto policy = storage->strictest("bluetooth"); - EXPECT_EQ((int)policy, 6); - - storage->disenroll("testAdmin0"); - storage->disenroll("testAdmin1"); -} - -TEST_F(PolicyStorageTests, strictest_all) -{ - auto storage = getStorage(); - storage->enroll("testAdmin"); - - auto policies = storage->strictest(); - EXPECT_TRUE(policies.size() > 0); - - storage->disenroll("testAdmin"); -} - TEST_F(PolicyStorageTests, admin_list) { auto storage = getStorage(); diff --git a/src/vist/sdk/policy-model.hpp b/src/vist/sdk/policy-model.hpp index 4cfbb53..20a5229 100644 --- a/src/vist/sdk/policy-model.hpp +++ b/src/vist/sdk/policy-model.hpp @@ -56,6 +56,20 @@ public: return current; } + /// Default compare function for int type + virtual int compare(const PolicyValue& lhs, const PolicyValue& rhs) const + { + int lvalue = lhs; + int rvalue = rhs; + + if (lvalue < rvalue) + return 1; + else if (lvalue == rvalue) + return 0; + else + return -1; + } + virtual void onChanged(const PolicyValue& value) = 0; const std::string& getName() const noexcept { return name; } -- 2.7.4