blob: f08e50c18d20af16a9a27a210322d617d079fd4b [file] [log] [blame]
tylerliu01d63ca2020-10-06 16:29:23 -07001/* -*- 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.
tylerliu01d63ca2020-10-06 16:29:23 -07004 *
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
tylerliu6563f932020-10-30 11:13:38 -070021#ifndef NDNCERT_ASSIGNMENT_FUNC_HPP
22#define NDNCERT_ASSIGNMENT_FUNC_HPP
tylerliu01d63ca2020-10-06 16:29:23 -070023
Zhiyi Zhang34f03f02020-10-29 18:34:42 -070024#include "detail/ca-request-state.hpp"
Davide Pesavento0dc02012021-11-23 22:55:03 -050025
tylerliu40226332020-11-11 15:37:16 -080026#include <map>
tylerliu01d63ca2020-10-06 16:29:23 -070027
tylerliu01d63ca2020-10-06 16:29:23 -070028namespace ndncert {
29
Davide Pesavento0dc02012021-11-23 22:55:03 -050030class NameAssignmentFunc : boost::noncopyable
Zhiyi Zhang684c67c2020-10-12 14:28:17 -070031{
tylerliu1f480be2020-11-10 13:02:53 -080032protected:
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040033 explicit
34 NameAssignmentFunc(const std::string& format = "");
tylerliu01d63ca2020-10-06 16:29:23 -070035
tylerliu1f480be2020-11-10 13:02:53 -080036public:
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040037 virtual
38 ~NameAssignmentFunc() = default;
tylerliu01d63ca2020-10-06 16:29:23 -070039
Zhiyi Zhang74c61142020-10-07 21:00:49 -070040 /**
41 * @brief The name assignment function provided by the CA operator to generate available
42 * namecomponents.
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070043 *
Zhiyi Zhang74c61142020-10-07 21:00:49 -070044 * The function does not guarantee that all the returned names are available. Therefore the
45 * CA should further check the availability of each returned name and remove unavailable results.
46 *
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070047 * @param vector A list of parameter key-value pair used for name assignment.
Zhiyi Zhang74c61142020-10-07 21:00:49 -070048 * @return a vector containing the possible namespaces derived from the parameters.
49 */
Davide Pesavento0dc02012021-11-23 22:55:03 -050050 virtual std::vector<ndn::PartialName>
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080051 assignName(const std::multimap<std::string, std::string>& params) = 0;
Zhiyi Zhang74c61142020-10-07 21:00:49 -070052
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040053public: // factory
54 template<class AssignmentType>
tylerliu01d63ca2020-10-06 16:29:23 -070055 static void
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040056 registerNameAssignmentFunc(const std::string& type)
tylerliu01d63ca2020-10-06 16:29:23 -070057 {
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040058 auto& factory = getFactory();
59 BOOST_ASSERT(factory.count(type) == 0);
60 factory[type] = [] (const std::string& format) { return std::make_unique<AssignmentType>(format); };
tylerliu01d63ca2020-10-06 16:29:23 -070061 }
62
Davide Pesavento0dc02012021-11-23 22:55:03 -050063 static std::unique_ptr<NameAssignmentFunc>
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040064 createNameAssignmentFunc(const std::string& type, const std::string& format = "");
tylerliu01d63ca2020-10-06 16:29:23 -070065
tylerliu40226332020-11-11 15:37:16 -080066NDNCERT_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Zhiyi Zhang84e11842020-11-19 20:03:23 -080067 std::vector<std::string> m_nameFormat;
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080068
tylerliu01d63ca2020-10-06 16:29:23 -070069private:
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040070 using CreateFunc = std::function<std::unique_ptr<NameAssignmentFunc>(const std::string &)>;
71 using FuncFactory = std::map<std::string, CreateFunc>;
tylerliu01d63ca2020-10-06 16:29:23 -070072
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040073 static FuncFactory&
tylerliu01d63ca2020-10-06 16:29:23 -070074 getFactory();
75};
76
Zhiyi Zhange4891b72020-10-10 15:11:57 -070077} // namespace ndncert
tylerliu01d63ca2020-10-06 16:29:23 -070078
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040079#define NDNCERT_REGISTER_NAME_ASSIGNMENT_FUNC(C, T) \
80static class NdnCert##C##NameAssignmentRegistrationClass \
81{ \
82public: \
83 NdnCert##C##NameAssignmentRegistrationClass() \
84 { \
85 ::ndncert::NameAssignmentFunc::registerNameAssignmentFunc<C>(T); \
86 } \
87} g_NdnCert##C##NameAssignmentRegistrationVariable
88
tylerliu6563f932020-10-30 11:13:38 -070089#endif // NDNCERT_ASSIGNMENT_FUNC_HPP