From da85f8f39a1ed20861e0d579d47c40292a578456 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Thu, 17 Jul 2014 21:38:55 +0200 Subject: [PATCH] Implement sending administration requests to Cynara service Implemented requests are: * InsertOrUpdateBucketRequest; * RemoveBucketRequest; * SetPoliciesRequest. All three requests have same response type (CodeResponse) so they are handled in common function. Change-Id: I84f7ad9375279d90db977d8a5d96ae38e29c3e5b --- src/admin/logic/Logic.cpp | 75 ++++++++++++++++++++++++++++++++++++++++------- src/admin/logic/Logic.h | 3 ++ 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/src/admin/logic/Logic.cpp b/src/admin/logic/Logic.cpp index d01353f..641929d 100644 --- a/src/admin/logic/Logic.cpp +++ b/src/admin/logic/Logic.cpp @@ -23,9 +23,18 @@ #include #include +#include +#include #include #include +#include +#include +#include +#include +#include +#include #include +#include #include "Logic.h" @@ -43,21 +52,65 @@ ProtocolFrameSequenceNumber generateSequenceNumber(void) { return ++sequenceNumber; } -int Logic::setPolicies(const std::map> &insertOrUpdate UNUSED, - const std::map> &remove UNUSED) noexcept { -//todo this is only a stub - return CYNARA_ADMIN_API_SUCCESS; +template +int Logic::askCynaraAndInterpreteCodeResponse(Args... args) { + ProtocolFrameSequenceNumber sequenceNumber = generateSequenceNumber(); + + //Ask cynara service + CodeResponsePtr codeResponse; + try { + RequestPtr request = std::make_shared(args..., sequenceNumber); + ResponsePtr response = m_socketClient->askCynaraServer(request); + if (!response) { + LOGW("Disconnected by cynara server."); + return CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE; + } + codeResponse = std::dynamic_pointer_cast(response); + if (!codeResponse) { + LOGC("Critical error. Casting Response to CodeResponse failed."); + return CYNARA_ADMIN_API_UNEXPECTED_CLIENT_ERROR; + } + + LOGD("codeResponse: code = %d", static_cast(codeResponse->m_code)); + switch (codeResponse->m_code) { + case CodeResponse::Code::OK: + LOGI("Policies set successfully."); + return CYNARA_ADMIN_API_SUCCESS; + case CodeResponse::Code::NOT_ALLOWED: + LOGE("Cynara service answered: Operation not allowed."); + return CYNARA_ADMIN_API_OPERATION_NOT_ALLOWED; + case CodeResponse::Code::NO_BUCKET: + LOGE("Trying to use unexisting bucket."); + return CYNARA_ADMIN_API_BUCKET_NOT_FOUND; + default: + LOGE("Unexpected response code from server: %d", + static_cast(codeResponse->m_code)); + return CYNARA_ADMIN_API_UNEXPECTED_CLIENT_ERROR; + } + } catch (const ServerConnectionErrorException &ex) { + LOGE("Cynara service not available."); + return CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE; + } catch (const std::bad_alloc &ex) { + LOGE("Cynara admin client out of memory."); + return CYNARA_ADMIN_API_OUT_OF_MEMORY; + } catch (const std::exception &ex) { + LOGE("Unexpected client error: %s", ex.what()); + return CYNARA_ADMIN_API_UNEXPECTED_CLIENT_ERROR; + } +} + +int Logic::setPolicies(const std::map> &insertOrUpdate, + const std::map> &remove) noexcept { + return askCynaraAndInterpreteCodeResponse(insertOrUpdate, remove); } -int Logic::insertOrUpdateBucket(const PolicyBucketId &bucket UNUSED, - const PolicyResult &policyResult UNUSED) noexcept { -//todo this is only a stub - return CYNARA_ADMIN_API_SUCCESS; +int Logic::insertOrUpdateBucket(const PolicyBucketId &bucket, + const PolicyResult &policyResult) noexcept { + return askCynaraAndInterpreteCodeResponse(bucket, policyResult); } -int Logic::removeBucket(const PolicyBucketId &bucket UNUSED) noexcept { -//todo this is only a stub - return CYNARA_ADMIN_API_SUCCESS; +int Logic::removeBucket(const PolicyBucketId &bucket) noexcept { + return askCynaraAndInterpreteCodeResponse(bucket); } } // namespace Cynara diff --git a/src/admin/logic/Logic.h b/src/admin/logic/Logic.h index 2cc22fc..d61c7e8 100644 --- a/src/admin/logic/Logic.h +++ b/src/admin/logic/Logic.h @@ -35,6 +35,9 @@ class Logic : public ApiInterface { private: SocketClientPtr m_socketClient; + template + int askCynaraAndInterpreteCodeResponse(Args... args); + public: Logic(); virtual ~Logic() = default; -- 2.7.4