Add InMemoryStorageBackend::hasBucket()
[platform/core/security/cynara.git] / src / service / storage / InMemoryStorageBackend.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        InMemoryStorageBackend.cpp
18  * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
19  * @version     1.0
20  * @brief       Implementation of InMemoryStorageBackend
21  */
22
23 #include "InMemoryStorageBackend.h"
24
25 namespace Cynara {
26
27 InMemoryStorageBackend::InMemoryStorageBackend() {
28     // Make sure, there's always default bucket
29     this->buckets().insert({ defaultPolicyBucketId, PolicyBucket() });
30 }
31
32 InMemoryStorageBackend::~InMemoryStorageBackend() {}
33
34 PolicyBucket InMemoryStorageBackend::searchDefaultBucket(const PolicyKey &key) {
35     return searchBucket(defaultPolicyBucketId, key);
36 }
37
38 PolicyBucket InMemoryStorageBackend::searchBucket(const PolicyBucketId &bucketId,
39                                                   const PolicyKey &key) {
40     const auto &bucket = this->buckets().at(bucketId);
41     return bucket.filtered(key);
42 }
43
44 void InMemoryStorageBackend::insertPolicy(const PolicyBucketId &bucketId, PolicyPtr policy) {
45     try {
46         auto &bucket = buckets().at(bucketId);
47         auto &policies = bucket.policyCollection();
48         policies.push_back(policy);
49     } catch (const std::out_of_range &) {
50         throw BucketNotExistsException(bucketId);
51     }
52 }
53
54 void InMemoryStorageBackend::createBucket(const PolicyBucketId &bucketId,
55                                           const PolicyResult &defaultPolicy) {
56     PolicyBucket newBucket;
57     newBucket.setDefaultPolicy(defaultPolicy);
58     buckets().insert({ bucketId, newBucket });
59 }
60
61 void InMemoryStorageBackend::deleteBucket(const PolicyBucketId &bucketId) {
62     auto bucketErased = buckets().erase(bucketId);
63     if (bucketErased == 0) {
64         throw BucketNotExistsException(bucketId);
65     }
66 }
67
68 bool InMemoryStorageBackend::hasBucket(const PolicyBucketId &bucketId) {
69     return buckets().find(bucketId) != buckets().end();
70 }
71
72 void InMemoryStorageBackend::deletePolicy(const PolicyBucketId &bucketId, const PolicyKey &key) {
73     try {
74         // TODO: Move the erase code to PolicyCollection maybe?
75         auto &bucket = buckets().at(bucketId);
76         auto &policies = bucket.policyCollection();
77         policies.erase(remove_if(policies.begin(), policies.end(),
78                 [key](PolicyPtr policy) -> bool {
79                     return policy->key() == key;
80             }), policies.end());
81     } catch(const std::out_of_range &) {
82         throw BucketNotExistsException(bucketId);
83     }
84 }
85
86 void InMemoryStorageBackend::deleteLinking(const PolicyBucketId &bucketId) {
87     auto bucketIdMatches = [&bucketId] (PolicyPtr policy) -> bool {
88         auto policyResult = policy->result();
89
90         // Check bucket id only if policy is a bucket policy
91         // TODO: Maybe move the test to PolicyResult
92         if (policyResult.policyType() == PredefinedPolicyType::BUCKET) {
93             return policyResult.metadata() == bucketId;
94         }
95         return false;
96     };
97
98     for(auto &bucketIter : buckets()) {
99         // TODO: Move the erase code to PolicyCollection maybe?
100         auto &bucket = bucketIter.second;
101         auto &policies = bucket.policyCollection();
102         policies.erase(remove_if(policies.begin(), policies.end(), bucketIdMatches),
103                 policies.end());
104     }
105 }
106
107 } /* namespace Cynara */