Make StorageSerializer a template class
[platform/core/security/cynara.git] / src / storage / StorageSerializer.h
1 /*
2  * Copyright (c) 2014-2015 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/storage/StorageSerializer.h
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Headers for Cynara::StorageSerializer
21  */
22
23 #ifndef SRC_STORAGE_STORAGESERIALIZER_H_
24 #define SRC_STORAGE_STORAGESERIALIZER_H_
25
26 #include <functional>
27 #include <fstream>
28 #include <ios>
29 #include <memory>
30 #include <sstream>
31
32 #include <config/PathConfig.h>
33 #include <exceptions/BucketSerializationException.h>
34 #include <types/Policy.h>
35 #include <types/PolicyBucket.h>
36 #include <types/PolicyBucketId.h>
37 #include <types/PolicyCollection.h>
38 #include <types/PolicyKey.h>
39 #include <types/PolicyResult.h>
40 #include <types/PolicyType.h>
41
42 #include <storage/Buckets.h>
43
44 namespace Cynara {
45
46 template<typename StreamType>
47 class StorageSerializer {
48
49 public:
50     typedef std::function<std::shared_ptr<StorageSerializer>(const PolicyBucketId &)>
51             BucketStreamOpener;
52
53     StorageSerializer(std::shared_ptr<StreamType> os);
54     virtual ~StorageSerializer() {};
55
56     virtual void dump(const Buckets &buckets,
57                       BucketStreamOpener streamOpener);
58     virtual void dump(const PolicyBucket &bucket);
59
60 protected:
61     template<typename Arg1, typename... Args>
62     inline void dumpFields(const Arg1 &arg1, const Args&... args) {
63         dump(arg1);
64         if (sizeof...(Args) > 0) {
65             *m_outStream << PathConfig::StoragePath::fieldSeparator;
66         }
67         dumpFields(args...);
68     }
69
70     inline void dumpFields(void) {
71         *m_outStream << PathConfig::StoragePath::recordSeparator;
72     }
73
74     void dump(const PolicyKeyFeature &keyFeature);
75     void dump(const PolicyType &policyType);
76     void dump(const PolicyResult::PolicyMetadata &metadata);
77     void dump(const PolicyCollection::value_type &policy);
78
79 private:
80     std::shared_ptr<StreamType> m_outStream;
81 };
82
83 template<typename StreamType>
84 StorageSerializer<StreamType>::StorageSerializer(std::shared_ptr<StreamType> os) : m_outStream(os) {
85 }
86
87 template<typename StreamType>
88 void StorageSerializer<StreamType>::dump(const Buckets &buckets, BucketStreamOpener streamOpener) {
89     for (const auto bucketIter : buckets) {
90         const auto &bucket = bucketIter.second;
91
92         dumpFields(bucket.id(), bucket.defaultPolicy().policyType(),
93                    bucket.defaultPolicy().metadata());
94     }
95
96     for (const auto bucketIter : buckets) {
97         const auto &bucketId = bucketIter.first;
98         const auto &bucket = bucketIter.second;
99         auto bucketSerializer = streamOpener(bucketId);
100
101         if (bucketSerializer != nullptr) {
102             bucketSerializer->dump(bucket);
103         } else {
104             throw BucketSerializationException(bucketId);
105         }
106     }
107 }
108
109 template<typename StreamType>
110 void StorageSerializer<StreamType>::dump(const PolicyBucket &bucket) {
111     for (auto it = std::begin(bucket); it != std::end(bucket); ++it) {
112         const auto &policy = *it;
113         dump(policy);
114     }
115 }
116
117 template<typename StreamType>
118 void StorageSerializer<StreamType>::dump(const PolicyKeyFeature &keyFeature) {
119     *m_outStream << keyFeature.toString();
120 }
121
122 template<typename StreamType>
123 void StorageSerializer<StreamType>::dump(const PolicyType &policyType) {
124     auto oldFormat = m_outStream->flags();
125     *m_outStream << "0x" << std::uppercase <<  std::hex << policyType;
126     m_outStream->flags(oldFormat);
127 }
128
129 template<typename StreamType>
130 void StorageSerializer<StreamType>::dump(const PolicyResult::PolicyMetadata &metadata) {
131     *m_outStream << metadata;
132 }
133
134 template<typename StreamType>
135 void StorageSerializer<StreamType>::dump(const PolicyCollection::value_type &policy) {
136     const auto &key = policy->key();
137     const auto &result = policy->result();
138
139     dumpFields(key.client(), key.user(), key.privilege(), result.policyType(), result.metadata());
140 }
141
142 } /* namespace Cynara */
143
144 #endif /* SRC_STORAGE_STORAGESERIALIZER_H_ */