blob: 6b8f03ce7b8dba6e3a7ae2973e5e6204df6028eb [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/*
Davide Pesavento0d1d11c2022-04-11 22:11:34 -04003 * Copyright (c) 2017-2022, 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
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040026#include <map>
27
28namespace ndncert::ca {
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080029
Davide Pesavento0dc02012021-11-23 22:55:03 -050030class CaStorage : boost::noncopyable
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080031{
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040032public:
33 virtual
34 ~CaStorage() = default;
35
Zhiyi Zhang21d52b52020-10-05 18:29:15 -070036 /**
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040037 * @throw std::runtime_error The request cannot be fetched from underlying data storage
Zhiyi Zhang21d52b52020-10-05 18:29:15 -070038 */
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070039 virtual RequestState
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070040 getRequest(const RequestId& requestId) = 0;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080041
Zhiyi Zhang21d52b52020-10-05 18:29:15 -070042 /**
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040043 * @throw std::runtime_error There is an existing request with the same request ID
Zhiyi Zhang21d52b52020-10-05 18:29:15 -070044 */
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080045 virtual void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070046 addRequest(const RequestState& request) = 0;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080047
48 virtual void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070049 updateRequest(const RequestState& request) = 0;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080050
51 virtual void
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070052 deleteRequest(const RequestId& requestId) = 0;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080053
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070054 virtual std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070055 listAllRequests() = 0;
56
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070057 virtual std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070058 listAllRequests(const Name& caName) = 0;
59
Davide Pesavento08994782018-01-22 12:13:41 -050060public: // factory
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080061 template<class CaStorageType>
62 static void
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040063 registerCaStorage(const std::string& type = CaStorageType::STORAGE_TYPE)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080064 {
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040065 auto& factory = getFactory();
66 BOOST_ASSERT(factory.count(type) == 0);
67 factory[type] = [] (const Name& caName, const std::string& path) {
Zhiyi Zhang32437282020-10-10 16:15:37 -070068 return std::make_unique<CaStorageType>(caName, path);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080069 };
70 }
71
Davide Pesavento0dc02012021-11-23 22:55:03 -050072 static std::unique_ptr<CaStorage>
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070073 createCaStorage(const std::string& caStorageType, const Name& caName, const std::string& path);
74
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080075private:
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040076 using CreateFunc = std::function<std::unique_ptr<CaStorage>(const Name&, const std::string&)>;
77 using CaStorageFactory = std::map<std::string, CreateFunc>;
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080078
79 static CaStorageFactory&
80 getFactory();
81};
82
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040083} // namespace ndncert::ca
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080084
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040085#define NDNCERT_REGISTER_CA_STORAGE(C) \
86static class NdnCert##C##CaStorageRegistrationClass \
87{ \
88public: \
89 NdnCert##C##CaStorageRegistrationClass() \
90 { \
91 ::ndncert::ca::CaStorage::registerCaStorage<C>(); \
92 } \
93} g_NdnCert##C##CaStorageRegistrationVariable
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080094
Zhiyi Zhanga62bf982020-10-26 13:10:02 -070095#endif // NDNCERT_DETAIL_CA_STORAGE_HPP