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
17 * @file src/admin/logic/Logic.cpp
18 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19 * @author Aleksander Zdyb <a.zdyb@samsung.com>
21 * @brief This file contains implementation of Logic class - main libcynara-admin class
25 #include <config/PathConfig.h>
29 #include "OfflineLogic.h"
30 #include "OnlineLogic.h"
34 Logic::Logic() : m_onlineLogic(new OnlineLogic()), m_offlineLogic(new OfflineLogic()),
35 m_lockable(PathConfig::StoragePath::lockFile) {}
39 delete m_offlineLogic;
42 int Logic::setPolicies(const ApiInterface::PoliciesByBucket &insertOrUpdate,
43 const ApiInterface::KeysByBucket &remove) {
44 using std::placeholders::_1;
45 return callApiFunction(std::bind(&ApiInterface::setPolicies, _1,
46 std::cref(insertOrUpdate), std::cref(remove)));
49 int Logic::insertOrUpdateBucket(const PolicyBucketId &bucket,
50 const PolicyResult &policyResult) {
51 using std::placeholders::_1;
52 auto f = std::bind(&ApiInterface::insertOrUpdateBucket, _1,
53 std::cref(bucket), std::cref(policyResult));
54 return callApiFunction(f);
57 int Logic::removeBucket(const PolicyBucketId &bucket) {
58 using std::placeholders::_1;
59 return callApiFunction(std::bind(&ApiInterface::removeBucket, _1, std::cref(bucket)));
62 int Logic::adminCheck(const PolicyBucketId &startBucket, bool recursive, const PolicyKey &key,
63 PolicyResult &result) {
64 using std::placeholders::_1;
65 return callApiFunction(std::bind(&ApiInterface::adminCheck, _1, std::cref(startBucket),
66 recursive, std::cref(key), std::ref(result)));
69 int Logic::listPolicies(const PolicyBucketId &bucket, const PolicyKey &filter,
70 std::vector<Policy> &policies) {
71 using std::placeholders::_1;
72 return callApiFunction(std::bind(&ApiInterface::listPolicies, _1, std::cref(bucket),
73 std::cref(filter), std::ref(policies)));
76 int Logic::erasePolicies(const PolicyBucketId &startBucket, bool recursive,
77 const PolicyKey &filter) {
78 using std::placeholders::_1;
79 return callApiFunction(std::bind(&ApiInterface::erasePolicies, _1, std::cref(startBucket),
80 recursive, std::cref(filter)));
83 int Logic::callApiFunction(std::function<int(ApiInterface *api)> apiCall) {
84 FileLock lock(m_lockable);
85 if (lock.tryLock() == true) {
86 m_offlineLogic->acquireDatabase();
87 LOGI("Admin uses offline API");
88 return apiCall(m_offlineLogic);
90 LOGI("Admin uses online API");
91 return apiCall(m_onlineLogic);