2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
18 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
20 * @brief This file contains implementation of Logic class - main libcynara-admin class
25 #include <cynara-admin-error.h>
27 #include <exceptions/ServerConnectionErrorException.h>
29 #include <protocol/Protocol.h>
30 #include <protocol/ProtocolAdmin.h>
31 #include <request/InsertOrUpdateBucketRequest.h>
32 #include <request/pointers.h>
33 #include <request/RemoveBucketRequest.h>
34 #include <request/SetPoliciesRequest.h>
35 #include <response/CodeResponse.h>
36 #include <response/pointers.h>
37 #include <sockets/SocketClient.h>
38 #include <types/ProtocolFields.h>
44 const std::string adminSocketPath("/run/cynara/cynara-admin.socket");
47 m_socketClient = std::make_shared<SocketClient>(adminSocketPath,
48 std::make_shared<ProtocolAdmin>());
51 ProtocolFrameSequenceNumber generateSequenceNumber(void) {
52 static ProtocolFrameSequenceNumber sequenceNumber = 0;
53 return ++sequenceNumber;
56 template<typename T, typename... Args>
57 int Logic::askCynaraAndInterpreteCodeResponse(Args... args) {
58 ProtocolFrameSequenceNumber sequenceNumber = generateSequenceNumber();
61 CodeResponsePtr codeResponse;
63 RequestPtr request = std::make_shared<T>(args..., sequenceNumber);
64 ResponsePtr response = m_socketClient->askCynaraServer(request);
66 LOGW("Disconnected by cynara server.");
67 return CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE;
69 codeResponse = std::dynamic_pointer_cast<CodeResponse>(response);
71 LOGC("Critical error. Casting Response to CodeResponse failed.");
72 return CYNARA_ADMIN_API_UNEXPECTED_CLIENT_ERROR;
75 LOGD("codeResponse: code = %d", static_cast<int>(codeResponse->m_code));
76 switch (codeResponse->m_code) {
77 case CodeResponse::Code::OK:
78 LOGI("Policies set successfully.");
79 return CYNARA_ADMIN_API_SUCCESS;
80 case CodeResponse::Code::NOT_ALLOWED:
81 LOGE("Cynara service answered: Operation not allowed.");
82 return CYNARA_ADMIN_API_OPERATION_NOT_ALLOWED;
83 case CodeResponse::Code::NO_BUCKET:
84 LOGE("Trying to use unexisting bucket.");
85 return CYNARA_ADMIN_API_BUCKET_NOT_FOUND;
87 LOGE("Unexpected response code from server: %d",
88 static_cast<int>(codeResponse->m_code));
89 return CYNARA_ADMIN_API_UNEXPECTED_CLIENT_ERROR;
91 } catch (const ServerConnectionErrorException &ex) {
92 LOGE("Cynara service not available.");
93 return CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE;
94 } catch (const std::bad_alloc &ex) {
95 LOGE("Cynara admin client out of memory.");
96 return CYNARA_ADMIN_API_OUT_OF_MEMORY;
97 } catch (const std::exception &ex) {
98 LOGE("Unexpected client error: %s", ex.what());
99 return CYNARA_ADMIN_API_UNEXPECTED_CLIENT_ERROR;
103 int Logic::setPolicies(const std::map<PolicyBucketId, std::vector<Policy>> &insertOrUpdate,
104 const std::map<PolicyBucketId, std::vector<PolicyKey>> &remove) noexcept {
105 return askCynaraAndInterpreteCodeResponse<SetPoliciesRequest>(insertOrUpdate, remove);
108 int Logic::insertOrUpdateBucket(const PolicyBucketId &bucket,
109 const PolicyResult &policyResult) noexcept {
110 return askCynaraAndInterpreteCodeResponse<InsertOrUpdateBucketRequest>(bucket, policyResult);
113 int Logic::removeBucket(const PolicyBucketId &bucket) noexcept {
114 return askCynaraAndInterpreteCodeResponse<RemoveBucketRequest>(bucket);
117 } // namespace Cynara