Cleanup deprecated policy-loader 06/238906/1
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 20 Jul 2020 06:45:12 +0000 (15:45 +0900)
committerSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 20 Jul 2020 06:46:30 +0000 (15:46 +0900)
Policy-loader is replaced by dynamic-loader (dynamic-table)

Change-Id: Ib58e15880b20a20432a3f70f7664c7bd03b702cc
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/client/tests/virtual-table.cpp
src/vist/policy/CMakeLists.txt
src/vist/policy/policy-loader.cpp [deleted file]
src/vist/policy/policy-loader.hpp [deleted file]
src/vist/policy/policy-manager.cpp
src/vist/policy/policy-manager.hpp
src/vist/policy/tests/core.cpp
src/vist/policy/tests/storage.cpp

index 25178e2..530d11c 100644 (file)
@@ -44,10 +44,10 @@ TEST(VirtualTableTests, policy_int_filter)
        VirtualTable<Policy<int>> table;
        EXPECT_TRUE(table.size() > 0);
 
-       auto row = table.filter(&Policy<int>::name, "sample-int-policy");
+       auto row = table.filter(&Policy<int>::name, "sample_int_policy");
        auto value = row[&Policy<int>::value];
        EXPECT_TRUE(value > 0);
-       EXPECT_EQ(row[&Policy<int>::name], "sample-int-policy");
+       EXPECT_EQ(row[&Policy<int>::name], "sample_int_policy");
 }
 
 TEST(VirtualTableTests, policy_str_table)
@@ -72,8 +72,8 @@ TEST(VirtualTableTests, policy_str_filter)
        VirtualTable<Policy<std::string>> table;
        EXPECT_TRUE(table.size() > 0);
 
-       auto row = table.filter(&Policy<std::string>::name, "sample-str-policy");
+       auto row = table.filter(&Policy<std::string>::name, "sample_str_policy");
        auto value = row[&Policy<std::string>::value];
        EXPECT_TRUE(!value.empty());
-       EXPECT_EQ(row[&Policy<std::string>::name], "sample-str-policy");
+       EXPECT_EQ(row[&Policy<std::string>::name], "sample_str_policy");
 }
index 56912fc..0b9883e 100644 (file)
@@ -17,7 +17,6 @@ SET(${TARGET_VIST_POLICY_LIB}_SRCS "")
 
 ADD_VIST_POLICY_LIBRARY(vist_policy_core api.cpp
                                                                                 policy-manager.cpp
-                                                                                policy-loader.cpp
                                                                                 policy-storage.cpp)
 
 FILE(GLOB POLICY_CORE_TESTS "tests/*.cpp")
diff --git a/src/vist/policy/policy-loader.cpp b/src/vist/policy/policy-loader.cpp
deleted file mode 100644 (file)
index d6c621b..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-
-#include "policy-loader.hpp"
-
-namespace vist {
-namespace policy {
-
-PolicyProvider* PolicyLoader::load(const std::string& path)
-{
-       PluginLoader loader(path);
-       PolicyProvider::FactoryType factory = nullptr;
-       loader.load(PolicyProvider::getFactoryName(), factory);
-       if (factory == nullptr)
-               THROW(ErrCode::RuntimeError) << "Failed to load factory: "
-                                                                        << PolicyProvider::getFactoryName();
-
-       auto provider = (*factory)();
-       if (provider == nullptr)
-               THROW(ErrCode::RuntimeError) << "Failed to make provider: "
-                                                                        << PolicyProvider::getFactoryName();
-
-       return provider;
-}
-
-PluginLoader::PluginLoader(const std::string& path, int flag)
-       : handle(::dlopen(path.c_str(), flag), [](void*)->int{return 0;})
-         // Cleaning object after dlclose() makes SEGFAULT.
-         // TODO: Sync dynamic loading's life-cycle with program.(PluginManager)
-         //  : handle(::dlopen(path.c_str(), flag), ::dlclose)
-{
-       if (handle == nullptr)
-               THROW(ErrCode::LogicError) << "Failed to open: " << path;
-}
-
-} // namespace policy
-} // namespace vist
diff --git a/src/vist/policy/policy-loader.hpp b/src/vist/policy/policy-loader.hpp
deleted file mode 100644 (file)
index fe83036..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-
-#pragma once
-
-#include <vist/sdk/policy-provider.hpp>
-
-#include <vist/exception.hpp>
-
-#include <memory>
-#include <string>
-
-#include <dlfcn.h>
-
-namespace vist {
-namespace policy {
-
-struct PolicyLoader final {
-       static PolicyProvider* load(const std::string& path);
-};
-
-class PluginLoader final {
-public:
-       explicit PluginLoader(const std::string& path, int flag = RTLD_LAZY);
-
-       template<typename T>
-       void load(const std::string& name, T& symbol);
-
-private:
-       using Handle = std::unique_ptr<void, int(*)(void*)>;
-       Handle handle;
-};
-
-template<typename T>
-void PluginLoader::load(const std::string& name, T& symbol)
-{
-       symbol = reinterpret_cast<T>(::dlsym(handle.get(), name.c_str()));
-       if (symbol == nullptr)
-               THROW(ErrCode::RuntimeError) << "Failed to load: " << name;
-}
-
-} // namespace policy
-} // namespace vist
index 8a67a35..52c49eb 100644 (file)
@@ -15,7 +15,6 @@
  */
 
 #include "policy-manager.hpp"
-#include "policy-loader.hpp"
 
 #include <vist/exception.hpp>
 #include <vist/logger.hpp>
@@ -29,48 +28,6 @@ namespace policy {
 
 PolicyManager::PolicyManager() : storage(DB_PATH)
 {
-       loadProviders(PLUGIN_INSTALL_DIR);
-       int cnt = loadPolicies();
-       INFO(VIST) << std::to_string(cnt) << "-policies loaded";
-}
-
-std::pair<int, int> PolicyManager::loadProviders(const std::string& path)
-{
-       INFO(VIST) << "Load policies from :" << path;
-       using namespace boost::filesystem;
-       if (!is_directory(path))
-               THROW(ErrCode::LogicError) << "Plugin directory is wrong.: " << path;
-
-       int passed = 0, failed = 0;
-       for (directory_entry& entry : directory_iterator(path)) {
-               if (!is_regular_file(entry.path().string()))
-                       continue;
-
-               try {
-                       auto provider = PolicyLoader::load(entry.path().string());
-                       DEBUG(VIST) << "Loaded provider: " << provider->getName();
-
-                       bool exist = false;
-                       for (const auto& p : this->providers) {
-                               if (p->getName() == provider->getName()) {
-                                       exist = true;
-                                       break;
-                               }
-                       }
-
-                       if (!exist)
-                               this->providers.emplace_back(std::move(provider));
-               } catch (const std::exception& e) {
-                       ++failed;
-                       ERROR(VIST) << "Failed to load: " << entry.path().string() << e.what();
-                       continue;
-               }
-
-               ++passed;
-       }
-
-       INFO(VIST) << "Loaded result >> passed: " << passed << ", failed: " << failed;
-       return std::make_pair(passed, failed);
 }
 
 void PolicyManager::addProvider(std::shared_ptr<PolicyProvider>&& provider)
@@ -94,24 +51,6 @@ void PolicyManager::addProvider(std::shared_ptr<PolicyProvider>&& provider)
        this->providers.emplace_back(std::move(provider));
 }
 
-int PolicyManager::loadPolicies()
-{
-       /// Make policy-provider map for performance
-       for (const auto& provider : providers) {
-               for (const auto& pair : provider->policies) {
-                       std::string policy = pair.first;
-                       this->policies[policy] = provider->getName();
-
-                       /// Check the policy is defined on policy-storage
-                       if (!storage.exists(pair.first)) {
-                               storage.define(pair.first, pair.second->getInitial());
-                       }
-               }
-       }
-
-       return this->policies.size();
-}
-
 void PolicyManager::enroll(const std::string& admin)
 {
        this->storage.enroll(admin);
index ea46d94..11cbe37 100644 (file)
@@ -65,9 +65,6 @@ private:
        explicit PolicyManager();
        ~PolicyManager() = default;
 
-       std::pair<int, int> loadProviders(const std::string& path);
-       int loadPolicies();
-
        PolicyStorage storage;
        std::vector<std::shared_ptr<PolicyProvider>> providers;
 
@@ -76,7 +73,6 @@ private:
        /// Policy-Provider
        std::unordered_map<std::string, std::string> policies;
 
-       FRIEND_TEST(PolicyCoreTests, policy_loader);
        FRIEND_TEST(PolicyCoreTests, policy_get_policy);
 };
 
index 98b5d5b..9fa3856 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 #include <gtest/gtest.h>
 
 #include <vist/policy/policy-manager.hpp>
+#include <vist/service/vistd.hpp>
 
 namespace vist {
 namespace policy {
 
-TEST(PolicyCoreTests, policy_loader)
-{
-       auto& manager = PolicyManager::Instance();
+namespace test{
 
-       EXPECT_TRUE(manager.providers.size() > 0);
-       EXPECT_TRUE(manager.policies.size() > 0);
-}
+       void init()
+       {
+               // Policies are loaded via dynamic table when actual daemon starts.
+               static_cast<void>(Vistd::Instance());
+       }
+
+} // anonymous namespace
 
 TEST(PolicyCoreTests, policy_set_get_int)
 {
+       test::init();
+
        auto& manager = PolicyManager::Instance();
        manager.enroll("testAdmin");
        manager.set("sample_int_policy", PolicyValue(5), "testAdmin");
@@ -52,18 +57,20 @@ TEST(PolicyCoreTests, policy_set_get_int)
 
 TEST(PolicyCoreTests, policy_set_get_str)
 {
+       test::init();
+
        auto& manager = PolicyManager::Instance();
        manager.enroll("testAdmin");
-       manager.set("sample-str-policy", PolicyValue("AAA"), "testAdmin");
+       manager.set("sample_str_policy", PolicyValue("AAA"), "testAdmin");
 
-       auto policy = manager.get("sample-str-policy");
+       auto policy = manager.get("sample_str_policy");
        EXPECT_EQ(static_cast<std::string>(policy), "AAA");
 
        manager.enroll("testAdmin1");
-       manager.set("sample-str-policy", PolicyValue("BBB"), "testAdmin1");
+       manager.set("sample_str_policy", PolicyValue("BBB"), "testAdmin1");
 
        /// Manager should return the strongest policy.
-       policy = manager.get("sample-str-policy");
+       policy = manager.get("sample_str_policy");
        EXPECT_EQ(static_cast<std::string>(policy), "AAA");
 
        manager.disenroll("testAdmin");
@@ -72,6 +79,8 @@ TEST(PolicyCoreTests, policy_set_get_str)
 
 TEST(PolicyCoreTests, policy_get_all)
 {
+       test::init();
+
        auto& manager = PolicyManager::Instance();
        auto policies = manager.getAll();
        EXPECT_TRUE(policies.size() > 0);
@@ -79,6 +88,8 @@ TEST(PolicyCoreTests, policy_get_all)
 
 TEST(PolicyCoreTests, policy_get_policy)
 {
+       test::init();
+
        auto& manager = PolicyManager::Instance();
        const auto& policy = manager.getPolicy("sample_int_policy");
        EXPECT_EQ(policy->getName(), "sample_int_policy");
index 87c51d0..2ee762e 100644 (file)
@@ -59,14 +59,14 @@ TEST_F(PolicyStorageTests, update)
 
        bool isRaised = false;
        try {
-               storage->update("fakeAdmin", "sample-int-policy", PolicyValue(0));
+               storage->update("fakeAdmin", "sample_int_policy", PolicyValue(0));
        } catch (const std::exception&) {
                isRaised = true;
        }
 
        isRaised = false;
        try {
-               storage->update("testAdmin", "sample-int-policy", PolicyValue(0));
+               storage->update("testAdmin", "sample_int_policy", PolicyValue(0));
        } catch (const std::exception&) {
                isRaised = true;
        }