2 * Copyright (c) 2014-2015 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/storage/StorageSerializer.h
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Headers for Cynara::StorageSerializer
23 #ifndef SRC_STORAGE_STORAGESERIALIZER_H_
24 #define SRC_STORAGE_STORAGESERIALIZER_H_
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>
42 #include <storage/Buckets.h>
46 template<typename StreamType>
47 class StorageSerializer {
50 typedef std::function<std::shared_ptr<StorageSerializer>(const PolicyBucketId &)>
53 StorageSerializer(std::shared_ptr<StreamType> os);
54 virtual ~StorageSerializer() {};
56 virtual void dump(const Buckets &buckets,
57 BucketStreamOpener streamOpener);
58 virtual void dump(const PolicyBucket &bucket);
61 template<typename Arg1, typename... Args>
62 inline void dumpFields(const Arg1 &arg1, const Args&... args) {
64 if (sizeof...(Args) > 0) {
65 *m_outStream << PathConfig::StoragePath::fieldSeparator;
70 inline void dumpFields(void) {
71 *m_outStream << PathConfig::StoragePath::recordSeparator;
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);
80 std::shared_ptr<StreamType> m_outStream;
83 template<typename StreamType>
84 StorageSerializer<StreamType>::StorageSerializer(std::shared_ptr<StreamType> os) : m_outStream(os) {
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;
92 dumpFields(bucket.id(), bucket.defaultPolicy().policyType(),
93 bucket.defaultPolicy().metadata());
96 for (const auto bucketIter : buckets) {
97 const auto &bucketId = bucketIter.first;
98 const auto &bucket = bucketIter.second;
99 auto bucketSerializer = streamOpener(bucketId);
101 if (bucketSerializer != nullptr) {
102 bucketSerializer->dump(bucket);
104 throw BucketSerializationException(bucketId);
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;
117 template<typename StreamType>
118 void StorageSerializer<StreamType>::dump(const PolicyKeyFeature &keyFeature) {
119 *m_outStream << keyFeature.toString();
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);
129 template<typename StreamType>
130 void StorageSerializer<StreamType>::dump(const PolicyResult::PolicyMetadata &metadata) {
131 *m_outStream << metadata;
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();
139 dumpFields(key.client(), key.user(), key.privilege(), result.policyType(), result.metadata());
142 } /* namespace Cynara */
144 #endif /* SRC_STORAGE_STORAGESERIALIZER_H_ */