Use Storage to return value for check() function in Logic layer
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 20 Jun 2014 22:39:46 +0000 (00:39 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:09 +0000 (14:19 +0200)
Change-Id: I30bdb8eafffa6a5cc06c9ddac3a724412c10733b

src/common/exceptions/PluginNotFoundException.h [new file with mode: 0644]
src/service/logic/Logic.cpp

diff --git a/src/common/exceptions/PluginNotFoundException.h b/src/common/exceptions/PluginNotFoundException.h
new file mode 100644 (file)
index 0000000..5cbb155
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * 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        PluginNotFoundException.h
+ * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version     1.0
+ * @brief       Implementation of PluginNotFoundException
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_PLUGINNOTFOUNDEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_PLUGINNOTFOUNDEXCEPTION_H_
+
+#include <exception>
+#include <sstream>
+
+#include <types/PolicyResult.h>
+#include "Exception.h"
+
+namespace Cynara {
+
+class PluginNotFoundException : public Exception {
+private:
+    std::string m_whatMessage;
+
+public:
+    PluginNotFoundException() = delete;
+    PluginNotFoundException(const PolicyResult &result) {
+        std::ostringstream stream;
+        stream << "No proper plugin found to interprete PolicyResult {type = ["
+                  << result.policyType() << "], metadata = <"
+                  << result.metadata().substr(0, 20) << ">}";
+        m_whatMessage = stream.str();
+    }
+
+    virtual ~PluginNotFoundException() = default;
+
+    virtual const char *what(void) const noexcept {
+        return m_whatMessage.c_str();
+    }
+};
+
+} // namespace Cynara
+
+#endif /* SRC_COMMON_EXCEPTIONS_PLUGINNOTFOUNDEXCEPTION_H_ */
index df52e9f..c230ab8 100644 (file)
  * @brief       This file implements main class of logic layer in cynara service
  */
 
+#include <log/log.h>
 #include <common.h>
+#include <exceptions/PluginNotFoundException.h>
+
+#include <main/Cynara.h>
+#include <storage/Storage.h>
 
 #include "Logic.h"
 
@@ -31,9 +36,24 @@ Logic::Logic() {
 Logic::~Logic() {
 }
 
-PolicyResult Logic::check(const RequestContext &context UNUSED, const PolicyKey &key UNUSED) {
-//todo this is only a stub
-    return PolicyResult(PredefinedPolicyType::ALLOW);
+PolicyResult Logic::check(const RequestContext &context UNUSED, const PolicyKey &key) {
+    PolicyResult result = Cynara::getStorage()->checkPolicy(key);
+
+    switch (result.policyType()) {
+        case PredefinedPolicyType::ALLOW :
+            LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
+            return result;
+        case PredefinedPolicyType::DENY :
+            LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
+            return result;
+    }
+    //todo pass question to proper plugin that:
+    //  1) might throw NoResponseGeneratedException when answer has to be waited for (UI)
+    //  2) might return PolicyResult
+    // In case 1) context should be saved in plugin in order to return answer when ready.
+
+    //in case no proper plugin is found
+    throw PluginNotFoundException(result);
 }
 
 } // namespace Cynara