Implement detection of online/offline mode in admin
[platform/core/security/cynara.git] / src / admin / logic / Logic.cpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /**
17  * @file        src/admin/logic/Logic.cpp
18  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
20  * @version     1.0
21  * @brief       This file contains implementation of Logic class - main libcynara-admin class
22  */
23
24 #include <common.h>
25 #include <config/PathConfig.h>
26 #include <log/log.h>
27
28 #include "Logic.h"
29 #include "OfflineLogic.h"
30 #include "OnlineLogic.h"
31
32 namespace Cynara {
33
34 Logic::Logic() : m_onlineLogic(new OnlineLogic()), m_offlineLogic(new OfflineLogic()),
35                  m_lockable(PathConfig::StoragePath::lockFile) {}
36
37 Logic::~Logic() {
38     delete m_onlineLogic;
39     delete m_offlineLogic;
40 }
41
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)));
47 }
48
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);
55 }
56
57 int Logic::removeBucket(const PolicyBucketId &bucket) {
58     using std::placeholders::_1;
59     return callApiFunction(std::bind(&ApiInterface::removeBucket, _1, std::cref(bucket)));
60 }
61
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)));
67 }
68
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)));
74 }
75
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)));
81 }
82
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);
89     } else {
90         LOGI("Admin uses online API");
91         return apiCall(m_onlineLogic);
92     }
93 }
94
95 } // namespace Cynara