Add listing types of policies 14/32214/5
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 16 Dec 2014 15:48:58 +0000 (16:48 +0100)
committerZofia Abramowska <z.abramowska@samsung.com>
Tue, 23 Dec 2014 16:20:35 +0000 (17:20 +0100)
Change-Id: Iab51f7ec232fb711ac6945be1ce71effa7e59ef1

13 files changed:
src/client-async/logic/Logic.cpp
src/client-common/CMakeLists.txt
src/client-common/cache/CacheInterface.h
src/client-common/plugins/NaiveInterpreter.cpp [deleted file]
src/client-common/plugins/NaiveInterpreter.h
src/client/logic/Logic.cpp
src/common/CMakeLists.txt
src/common/plugin/ExternalPluginInterface.h
src/common/plugin/PluginManager.cpp
src/common/plugin/PluginManager.h
src/common/types/PolicyDescription.cpp [new file with mode: 0644]
src/common/types/PolicyDescription.h [new file with mode: 0644]
src/include/cynara-client-plugin.h

index 48b8bad..7504c7e 100644 (file)
@@ -50,8 +50,8 @@ Logic::Logic(cynara_status_callback callback, void *userStatusData)
 
     m_cache = std::make_shared<CapacityCache>();
     auto naiveInterpreter = std::make_shared<NaiveInterpreter>();
-    for (auto &type : naiveInterpreter->getSupportedPolicyTypes()) {
-        m_cache->registerPlugin(type, naiveInterpreter);
+    for (auto &descr : naiveInterpreter->getSupportedPolicyDescr()) {
+        m_cache->registerPlugin(descr, naiveInterpreter);
     }
 }
 
index bb02eca..2c60ca9 100644 (file)
@@ -28,7 +28,6 @@ INCLUDE_DIRECTORIES(
 
 SET(LIB_CYNARA_COMMON_SOURCES
     ${LIB_CYNARA_COMMON_PATH}/cache/CapacityCache.cpp
-    ${LIB_CYNARA_COMMON_PATH}/plugins/NaiveInterpreter.cpp
     )
 
 ADD_DEFINITIONS("-fvisibility=default")
index 03b7624..bbd18c8 100644 (file)
@@ -31,9 +31,9 @@
 #include <cynara-client.h>
 #include <cynara-client-plugin.h>
 #include <types/ClientSession.h>
+#include <types/PolicyDescription.h>
 #include <types/PolicyKey.h>
 #include <types/PolicyResult.h>
-#include <types/PolicyType.h>
 
 namespace Cynara {
 
@@ -48,8 +48,8 @@ public:
                        const PolicyKey &key,
                        const PolicyResult &result) = 0;
 
-    void registerPlugin(const PolicyType policyType, ClientPluginInterfacePtr plugin) {
-        m_plugins[policyType] = plugin;
+    void registerPlugin(const PolicyDescription &policyDescr, ClientPluginInterfacePtr plugin) {
+        m_plugins[policyDescr] = plugin;
     }
 
     virtual void clear(void) {
@@ -59,7 +59,7 @@ public:
     virtual ~PluginCache() {};
 
 protected:
-    std::map<PolicyType, ClientPluginInterfacePtr> m_plugins;
+    std::map<PolicyDescription, ClientPluginInterfacePtr> m_plugins;
 };
 
 } // namespace Cynara
diff --git a/src/client-common/plugins/NaiveInterpreter.cpp b/src/client-common/plugins/NaiveInterpreter.cpp
deleted file mode 100644 (file)
index 7141cb7..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- *  Copyright (c) 2014 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
- */
-/**
- * @file        src/client-common/plugins/NaiveInterpreter.cpp
- * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @version     1.0
- * @brief       This file contains NaiveInterpreter supported types definition.
- */
-
-#include <plugins/NaiveInterpreter.h>
-
-namespace Cynara {
-
-    const std::vector<PolicyType> NaiveInterpreter::s_supportedTypes =
-    {PredefinedPolicyType::ALLOW, PredefinedPolicyType::DENY};
-
-}
index 059554d..a0e2d67 100644 (file)
@@ -25,6 +25,7 @@
 #include <attributes/attributes.h>
 #include <cache/CacheInterface.h>
 #include <cynara-error.h>
+#include <cynara-client-plugin.h>
 
 namespace Cynara {
 
@@ -45,13 +46,11 @@ public:
             return CYNARA_API_ACCESS_DENIED;
     }
 
-    const std::vector<PolicyType> &getSupportedPolicyTypes(void) {
-        return s_supportedTypes;
+    const std::vector<PolicyDescription> &getSupportedPolicyDescr(void) {
+        return predefinedPolicyDescr;
     }
 
     void invalidate(void) {}
-private:
-    static const std::vector<PolicyType> s_supportedTypes;
 };
 
 } // namespace Cynara
index 498e53c..f8fc543 100644 (file)
@@ -52,8 +52,8 @@ Logic::Logic() {
                                               std::make_shared<ProtocolClient>());
     m_cache = std::make_shared<CapacityCache>();
     auto naiveInterpreter = std::make_shared<NaiveInterpreter>();
-    for (auto &type : naiveInterpreter->getSupportedPolicyTypes()) {
-        m_cache->registerPlugin(type, naiveInterpreter);
+    for (auto &descr : naiveInterpreter->getSupportedPolicyDescr()) {
+        m_cache->registerPlugin(descr, naiveInterpreter);
     }
 }
 
index 37073ad..fe2f727 100644 (file)
@@ -62,9 +62,11 @@ SET(COMMON_SOURCES
     ${COMMON_PATH}/sockets/Socket.cpp
     ${COMMON_PATH}/sockets/SocketClient.cpp
     ${COMMON_PATH}/types/PolicyBucket.cpp
+    ${COMMON_PATH}/types/PolicyDescription.cpp
     ${COMMON_PATH}/types/PolicyKey.cpp
     ${COMMON_PATH}/types/PolicyKeyHelpers.cpp
     ${COMMON_PATH}/types/PolicyResult.cpp
+    ${COMMON_PATH}/types/PolicyType.cpp
     )
 
 IF (CMAKE_BUILD_TYPE MATCHES "DEBUG")
index ddd8363..f4e8a63 100644 (file)
@@ -25,7 +25,7 @@
 
 #include <vector>
 
-#include <types/PolicyType.h>
+#include <types/PolicyDescription.h>
 
 namespace Cynara {
 
@@ -47,11 +47,10 @@ typedef void (*destroy_t)(ExternalPluginInterface *);
 
 class ExternalPluginInterface {
 public:
-
     /**
-     * Policy type supported by plugin.
+     * Policy types supported by plugin.
      */
-    virtual const std::vector<PolicyType> &getSupportedPolicyTypes(void) = 0;
+    virtual const std::vector<PolicyDescription> &getSupportedPolicyDescr(void) = 0;
 
     /**
      * Informs, that every data stored based on previously given input
index c8a38c5..300dc7f 100644 (file)
@@ -65,6 +65,15 @@ ExternalPluginPtr PluginManager::getPlugin(PolicyType pType) {
     return m_plugins[pType];
 }
 
+std::vector<PolicyDescription> PluginManager::getPolicyDescriptions(void) const {
+    std::vector<PolicyDescription> descriptions;
+    descriptions.reserve(m_plugins.size());
+    for (auto &plugin : m_plugins) {
+        descriptions.push_back(plugin.first);
+    }
+    return descriptions;
+}
+
 void PluginManager::invalidateAll(void) {
     for (auto &plugin : m_plugins) {
         plugin.second->invalidate();
@@ -125,14 +134,15 @@ void PluginManager::openPlugin(const std::string &path) {
         return;
     }
 
-    auto policies = pluginPtr->getSupportedPolicyTypes();
+    auto policies = pluginPtr->getSupportedPolicyDescr();
     if (policies.empty()) {
         LOGE("Plugin <%s> does not support any type!", path.c_str());
         return;
     }
-    for (auto type : policies) {
-        if (!m_plugins.insert(std::make_pair(type, pluginPtr)).second) {
-            LOGW("policyType [%" PRIu16 "] was already supported.", type);
+    for (auto &desc : policies) {
+        if (!m_plugins.insert(std::make_pair(desc, pluginPtr)).second) {
+            LOGW("policy type: [%" PRIu16 "] name: <%s> was already supported.",
+                 desc.type, desc.name.c_str());
         }
     }
 
index 8de33ae..87b0597 100644 (file)
 #include <map>
 #include <memory>
 #include <string>
+#include <vector>
 
 #include <plugin/ExternalPluginInterface.h>
+#include <types/PolicyDescription.h>
 
 namespace Cynara {
 typedef std::shared_ptr<ExternalPluginInterface> ExternalPluginPtr;
@@ -38,6 +40,7 @@ class PluginManager {
 public:
     PluginManager(const std::string &pluginDir);
     ExternalPluginPtr getPlugin(PolicyType pType);
+    std::vector<PolicyDescription> getPolicyDescriptions(void) const;
     void invalidateAll(void);
     ~PluginManager();
 
@@ -46,7 +49,7 @@ private:
     typedef std::list<PluginLibPtr> PluginLibs;
 
     std::string m_dir;
-    std::map<PolicyType, ExternalPluginPtr> m_plugins;
+    std::map<PolicyDescription, ExternalPluginPtr> m_plugins;
     PluginLibs m_pluginLibs;
 
     void loadPlugins(void);
diff --git a/src/common/types/PolicyDescription.cpp b/src/common/types/PolicyDescription.cpp
new file mode 100644 (file)
index 0000000..1c06137
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2014 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.
+ */
+/**
+ * @file        src/common/types/PolicyDescription.cpp
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @version     1.0
+ * @brief       Implementation of functions for Cynara::PolicyDescription
+ */
+
+#include "PolicyDescription.h"
+
+namespace Cynara {
+
+const std::vector<PolicyDescription> predefinedPolicyDescr = {
+        PolicyDescription(PredefinedPolicyType::ALLOW, "Allow"),
+        PolicyDescription(PredefinedPolicyType::DENY, "Deny")
+};
+
+bool operator<(const PolicyDescription &left, const PolicyDescription &right) {
+    return left.type < right.type;
+}
+
+}  // namespace Cynara
diff --git a/src/common/types/PolicyDescription.h b/src/common/types/PolicyDescription.h
new file mode 100644 (file)
index 0000000..b4d27d5
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2014 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.
+ */
+/**
+ * @file        src/common/types/PolicyDescription.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @version     1.0
+ * @brief       This file defines PolicyDescription for storing policy type
+ *              description.
+ */
+
+#ifndef SRC_COMMON_TYPES_POLICYDESCRIPTION_H_
+#define SRC_COMMON_TYPES_POLICYDESCRIPTION_H_
+
+#include <types/PolicyType.h>
+
+#include <vector>
+#include <string>
+
+namespace Cynara {
+
+struct PolicyDescription {
+    PolicyDescription(const PolicyType &pType, const std::string &pName) : type(pType), name(pName)
+    {};
+    PolicyDescription(const PolicyType &pType) : type(pType) {};
+    PolicyType type;
+    std::string name;
+};
+
+extern const std::vector<PolicyDescription> predefinedPolicyDescr;
+
+bool operator<(const PolicyDescription &left, const PolicyDescription &right);
+
+}  // namespace Cynara
+
+#endif /* SRC_COMMON_TYPES_POLICYDESCRIPTION_H_ */
index 3d39ad7..29e32ce 100644 (file)
@@ -51,13 +51,12 @@ typedef std::shared_ptr<ClientPluginInterface> ClientPluginInterfacePtr;
  */
 class ClientPluginInterface : public ExternalPluginInterface {
 public:
-
     /**
      * Return entry cacheability
      */
     virtual bool isCacheable(const ClientSession &session, const PolicyResult &result) = 0;
     /**
-     * Return entry cacheability
+     * Return entry usability
      */
     virtual bool isUsable(const ClientSession &session, const ClientSession &prevSession,
                           bool &updateSession, PolicyResult &result) = 0;