Prepare for testbed deployment: name assignment and redirection policy

Change-Id: I7f4da10b763f3891d33820e9c6f4c7cb0eea60ce
diff --git a/src/redirection/redirection-email.cpp b/src/redirection/redirection-email.cpp
new file mode 100644
index 0000000..863eb91
--- /dev/null
+++ b/src/redirection/redirection-email.cpp
@@ -0,0 +1,46 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2017-2022, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "redirection-email.hpp"
+#include <boost/algorithm/string.hpp>
+
+namespace ndncert {
+
+NDNCERT_REGISTER_POLICY_FACTORY(RedirectionEmail, "email");
+
+RedirectionEmail::RedirectionEmail(const std::string& format)
+  : RedirectionPolicy(format)
+{
+  m_domain = format;
+}
+
+bool
+RedirectionEmail::isRedirecting(const std::multimap<std::string, std::string>& params)
+{
+  for (auto it = params.find("email"); it != params.end() && it->first == "email"; it++) {
+    auto i = it->second.rfind('@');
+    if (i != std::string::npos && it->second.substr(i + 1) == m_domain) {
+      return true;
+    }
+  }
+  return false;
+}
+
+} // namespace ndncert
diff --git a/src/redirection/redirection-email.hpp b/src/redirection/redirection-email.hpp
new file mode 100644
index 0000000..9a338ef
--- /dev/null
+++ b/src/redirection/redirection-email.hpp
@@ -0,0 +1,45 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2017-2022, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_REDIRECTION_EMAIL_HPP
+#define NDNCERT_REDIRECTION_EMAIL_HPP
+
+#include "redirection-policy.hpp"
+
+namespace ndncert {
+
+/**
+ * assign names base on client probe parameter
+ */
+class RedirectionEmail : public RedirectionPolicy
+{
+public:
+  explicit RedirectionEmail(const std::string& format = "");
+
+  bool
+  isRedirecting(const std::multimap<std::string, std::string>& params) override;
+
+private:
+  std::string m_domain;
+};
+
+} // namespace ndncert
+
+#endif // NDNCERT_REDIRECTION_EMAIL_HPP
diff --git a/src/redirection/redirection-param.cpp b/src/redirection/redirection-param.cpp
new file mode 100644
index 0000000..d74d105
--- /dev/null
+++ b/src/redirection/redirection-param.cpp
@@ -0,0 +1,63 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2017-2022, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "redirection-param.hpp"
+#include <boost/algorithm/string.hpp>
+
+namespace ndncert {
+
+NDNCERT_REGISTER_POLICY_FACTORY(RedirectionParam, "param");
+
+RedirectionParam::RedirectionParam(const std::string& format)
+  : RedirectionPolicy(format)
+{
+  if (format.empty()) {
+    return;
+  }
+  std::vector<std::string> strs;
+  boost::split(strs,format,boost::is_any_of("&"));
+  for (const auto& s : strs) {
+    auto i = s.find('=');
+    if (i == std::string::npos) {
+      NDN_THROW(std::runtime_error("Redirection param format: no '=' in format piece"));
+    }
+    m_format.emplace(s.substr(0, i), s.substr(i + 1));
+  }
+}
+
+bool
+RedirectionParam::isRedirecting(const std::multimap<std::string, std::string>& params)
+{
+  for (const auto& p : m_format) {
+    bool found = false;
+    for (auto it = params.find(p.first); it != params.end() && it->first == p.first; it ++) {
+      if (it->second == p.second) {
+        found = true;
+        break;
+      }
+    }
+    if (!found) {
+      return false;
+    }
+  }
+  return true;
+}
+
+} // namespace ndncert
diff --git a/src/redirection/redirection-param.hpp b/src/redirection/redirection-param.hpp
new file mode 100644
index 0000000..d067a1f
--- /dev/null
+++ b/src/redirection/redirection-param.hpp
@@ -0,0 +1,45 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2017-2022, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_REDIRECTION_PARAM_HPP
+#define NDNCERT_REDIRECTION_PARAM_HPP
+
+#include "redirection-policy.hpp"
+
+namespace ndncert {
+
+/**
+ * assign names base on client probe parameter
+ */
+class RedirectionParam : public RedirectionPolicy
+{
+public:
+  explicit RedirectionParam(const std::string& format = "");
+
+  bool
+  isRedirecting(const std::multimap<std::string, std::string>& params) override;
+
+private:
+  std::map<std::string, std::string> m_format;
+};
+
+} // namespace ndncert
+
+#endif // NDNCERT_REDIRECTION_PARAM_HPP
diff --git a/src/redirection/redirection-policy.cpp b/src/redirection/redirection-policy.cpp
new file mode 100644
index 0000000..88aef3e
--- /dev/null
+++ b/src/redirection/redirection-policy.cpp
@@ -0,0 +1,40 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2017-2022, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "redirection-policy.hpp"
+
+namespace ndncert {
+
+std::unique_ptr<RedirectionPolicy>
+RedirectionPolicy::createPolicyFunc(const std::string& policyType, const std::string& format)
+{
+  PolicyFactory& factory = getFactory();
+  auto i = factory.find(policyType);
+  return i == factory.end() ? nullptr : i->second(format);
+}
+
+RedirectionPolicy::PolicyFactory&
+RedirectionPolicy::getFactory()
+{
+  static PolicyFactory factory;
+  return factory;
+}
+
+} // namespace ndncert
diff --git a/src/redirection/redirection-policy.hpp b/src/redirection/redirection-policy.hpp
new file mode 100644
index 0000000..cba0cd8
--- /dev/null
+++ b/src/redirection/redirection-policy.hpp
@@ -0,0 +1,81 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2017-2022, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_REDIRECTION_POLICY_HPP
+#define NDNCERT_REDIRECTION_POLICY_HPP
+
+#include "detail/ca-request-state.hpp"
+
+#include <map>
+
+namespace ndncert {
+
+class RedirectionPolicy : boost::noncopyable
+{
+protected:
+  explicit RedirectionPolicy(const std::string& format = "") {}
+
+public:
+  virtual ~RedirectionPolicy() = default;
+
+  /**
+   * @brief The Redirection Policy provided by the CA operator to decide if redirection is suitable.
+   *
+   *
+   * @param vector A list of parameter key-value pair from probe.
+   * @return a boolean that is true if the provided params conform to the configured redirection policy.
+   */
+  virtual bool
+  isRedirecting(const std::multimap<std::string, std::string>& params) = 0;
+
+public:
+  template <class PolicyType>
+  static void
+  registerRedirectionPolicy(const std::string& typeName)
+  {
+    PolicyFactory& factory = getFactory();
+    BOOST_ASSERT(factory.count(typeName) == 0);
+    factory[typeName] = [](const std::string& format) { return std::make_unique<PolicyType>(format); };
+  }
+
+  static std::unique_ptr<RedirectionPolicy>
+  createPolicyFunc(const std::string& policyType, const std::string& format = "");
+
+private:
+  typedef std::function<std::unique_ptr<RedirectionPolicy>(const std::string&)> FactoryCreateFunc;
+  typedef std::map<std::string, FactoryCreateFunc> PolicyFactory;
+
+  static PolicyFactory&
+  getFactory();
+};
+
+#define NDNCERT_REGISTER_POLICY_FACTORY(C, T)                                  \
+  static class NdnCert##C##PolicyFactoryRegistrationClass                      \
+  {                                                                               \
+  public:                                                                         \
+    NdnCert##C##PolicyFactoryRegistrationClass()                               \
+    {                                                                             \
+      ::ndncert::RedirectionPolicy::registerRedirectionPolicy<C>(T);        \
+    }                                                                             \
+  } g_NdnCert##C##RedirectionPolicyRegistrationVariable
+
+} // namespace ndncert
+
+#endif // NDNCERT_REDIRECTION_POLICY_HPP