blob: b8c2dd11dbfb877643252b9b3ac9820d2e989510 [file] [log] [blame]
tylerliu01d63ca2020-10-06 16:29:23 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017-2019, Regents of the University of California.
4 *
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
21#ifndef NDNCERT_ASSIGNMENT_FUNCS_HPP
22#define NDNCERT_ASSIGNMENT_FUNCS_HPP
23
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070024#include "../ca-state.hpp"
tylerliu01d63ca2020-10-06 16:29:23 -070025
26namespace ndn {
27namespace ndncert {
28
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070029/**
30 * @brief The name assignment function provided by the CA operator to generate available
31 * namecomponents.
32 * The function does not guarantee that all the returned names are available. Therefore the
33 * CA should further check the availability of each returned name and remove unavailable results.
34 *
35 * @p vector, input, a list of parameter key-value pair used for name assignment.
36 * @return a vector containing the possible namespaces derived from the parameters.
37 */
38using NameAssignmentFunc = function<std::vector<PartialName>(const std::vector<std::tuple<std::string, std::string>>&)>;
39
tylerliu01d63ca2020-10-06 16:29:23 -070040class NameAssignmentFuncFactory : noncopyable {
41public:
42 explicit
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070043 NameAssignmentFuncFactory(const std::string& factoryType, const std::string& format = "");
tylerliu01d63ca2020-10-06 16:29:23 -070044
45 virtual ~NameAssignmentFuncFactory() = default;
46
47 template <class ChallengeType>
48 static void
tylerliuafb27a02020-10-06 17:20:06 -070049 registerNameAssignmentFuncFactories(const std::string& typeName)
tylerliu01d63ca2020-10-06 16:29:23 -070050 {
51 FuncFactoryFactory& factory = getFactory();
52 BOOST_ASSERT(factory.count(typeName) == 0);
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070053 factory[typeName] = [](const std::string& format) { return make_unique<ChallengeType>(format); };
tylerliu01d63ca2020-10-06 16:29:23 -070054 }
55
tylerliu01d63ca2020-10-06 16:29:23 -070056 static unique_ptr<NameAssignmentFuncFactory>
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070057 createNameAssignmentFuncFactory(const std::string& challengeType, const std::string& format = "");
tylerliu01d63ca2020-10-06 16:29:23 -070058
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070059 virtual std::vector<PartialName>
60 assignName(const std::vector<std::tuple<std::string, std::string>>& params) = 0;
tylerliu01d63ca2020-10-06 16:29:23 -070061
62public:
63 const std::string FACTORY_TYPE;
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070064 std::vector<std::string> m_nameFormat;
tylerliu01d63ca2020-10-06 16:29:23 -070065
66private:
Zhiyi Zhang8683ec92020-10-07 18:18:35 -070067 typedef function<unique_ptr<NameAssignmentFuncFactory>(const std::string&)> FactoryCreateFunc;
tylerliu01d63ca2020-10-06 16:29:23 -070068 typedef std::map<std::string, FactoryCreateFunc> FuncFactoryFactory;
69
70 static FuncFactoryFactory&
71 getFactory();
72};
73
tylerliuafb27a02020-10-06 17:20:06 -070074#define NDNCERT_REGISTER_FUNCFACTORY(C, T) \
75 static class NdnCert##C##FuncFactoryRegistrationClass { \
76 public: \
77 NdnCert##C##FuncFactoryRegistrationClass() \
78 { \
79 ::ndn::ndncert::NameAssignmentFuncFactory::registerNameAssignmentFuncFactories<C>(T); \
80 } \
tylerliu01d63ca2020-10-06 16:29:23 -070081 } g_NdnCert##C##ChallengeRegistrationVariable
82
83} // namespace ndncert
84} // namespace ndn
85
86#endif // NDNCERT_ASSIGNMENT_FUNCS_HPP