blob: 0fa7238a355dbf18d96142ea1b77603c94351587 [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
3 * Copyright (c) 2017-2021, Regents of the University of California.
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
Zhiyi Zhanga62bf982020-10-26 13:10:02 -070021#ifndef NDNCERT_DETAIL_CA_STORAGE_HPP
22#define NDNCERT_DETAIL_CA_STORAGE_HPP
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080023
Zhiyi Zhang34f03f02020-10-29 18:34:42 -070024#include "detail/ca-request-state.hpp"
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080025
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080026namespace ndncert {
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070027namespace ca {
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080028
Davide Pesavento0dc02012021-11-23 22:55:03 -050029class CaStorage : boost::noncopyable
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080030{
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070031public: // request related
Zhiyi Zhang21d52b52020-10-05 18:29:15 -070032 /**
33 * @throw if request cannot be fetched from underlying data storage
34 */
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070035 virtual RequestState
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070036 getRequest(const RequestId& requestId) = 0;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080037
Zhiyi Zhang21d52b52020-10-05 18:29:15 -070038 /**
39 * @throw if there is an existing request with the same request ID
40 */
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080041 virtual void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070042 addRequest(const RequestState& request) = 0;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080043
44 virtual void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070045 updateRequest(const RequestState& request) = 0;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080046
47 virtual void
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070048 deleteRequest(const RequestId& requestId) = 0;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080049
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070050 virtual std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070051 listAllRequests() = 0;
52
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070053 virtual std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070054 listAllRequests(const Name& caName) = 0;
55
Davide Pesavento08994782018-01-22 12:13:41 -050056public: // factory
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080057 template<class CaStorageType>
58 static void
59 registerCaStorage(const std::string& caStorageType = CaStorageType::STORAGE_TYPE)
60 {
61 CaStorageFactory& factory = getFactory();
62 BOOST_ASSERT(factory.count(caStorageType) == 0);
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070063 factory[caStorageType] = [] (const Name& caName, const std::string& path) {
Zhiyi Zhang32437282020-10-10 16:15:37 -070064 return std::make_unique<CaStorageType>(caName, path);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080065 };
66 }
67
Davide Pesavento0dc02012021-11-23 22:55:03 -050068 static std::unique_ptr<CaStorage>
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070069 createCaStorage(const std::string& caStorageType, const Name& caName, const std::string& path);
70
71 virtual
72 ~CaStorage() = default;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080073
74private:
Davide Pesavento0dc02012021-11-23 22:55:03 -050075 using CaStorageCreateFunc = std::function<std::unique_ptr<CaStorage> (const Name&, const std::string&)>;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080076 using CaStorageFactory = std::map<std::string, CaStorageCreateFunc>;
77
78 static CaStorageFactory&
79 getFactory();
80};
81
82#define NDNCERT_REGISTER_CA_STORAGE(C) \
83static class NdnCert ## C ## CaStorageRegistrationClass \
84{ \
85public: \
86 NdnCert ## C ## CaStorageRegistrationClass() \
87 { \
Davide Pesavento0dc02012021-11-23 22:55:03 -050088 ::ndncert::ca::CaStorage::registerCaStorage<C>(); \
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080089 } \
90} g_NdnCert ## C ## CaStorageRegistrationVariable
91
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070092} // namespace ca
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080093} // namespace ndncert
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080094
Zhiyi Zhanga62bf982020-10-26 13:10:02 -070095#endif // NDNCERT_DETAIL_CA_STORAGE_HPP