blob: fbd5faa134a88772607257a991be10ace26e1a98 [file] [log] [blame]
Junxiao Shi05dd4442017-02-06 22:50:07 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento87fc0f82018-04-11 23:43:51 -04002/*
Davide Pesaventod6ea0b12023-03-13 21:35:03 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Junxiao Shi05dd4442017-02-06 22:50:07 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Davide Pesavento990620a2022-06-05 00:25:53 -040026#ifndef NFD_TOOLS_NFDC_FACE_HELPERS_HPP
27#define NFD_TOOLS_NFDC_FACE_HELPERS_HPP
Junxiao Shi05dd4442017-02-06 22:50:07 +000028
29#include "execute-command.hpp"
30
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tools::nfdc {
Junxiao Shi05dd4442017-02-06 22:50:07 +000032
33using ndn::nfd::FaceQueryFilter;
34using ndn::nfd::FaceStatus;
35
Davide Pesavento990620a2022-06-05 00:25:53 -040036/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040037 * \brief Procedure to find a face.
Junxiao Shi05dd4442017-02-06 22:50:07 +000038 */
39class FindFace : noncopyable
40{
41public:
42 enum class Code {
43 OK = 0, ///< found exactly one face, or found multiple faces when allowMulti is true
44 ERROR = 1, ///< unspecified error
45 NOT_FOUND = 3, ///< found zero face
46 CANONIZE_ERROR = 4, ///< error during FaceUri canonization
47 AMBIGUOUS = 5, ///< found multiple faces and allowMulti is false
48 NOT_STARTED = -1, ///< for internal use
49 IN_PROGRESS = -2, ///< for internal use
50 };
51
52 enum class DisambiguationStyle
53 {
54 LOCAL_URI = 1 ///< print FaceId and LocalUri
55 };
56
57 explicit
58 FindFace(ExecuteContext& ctx);
59
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040060 /** \brief Find face by FaceUri.
Junxiao Shi05dd4442017-02-06 22:50:07 +000061 * \pre execute has not been invoked
62 */
63 Code
64 execute(const FaceUri& faceUri, bool allowMulti = false);
65
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040066 /** \brief Find face by FaceId.
Junxiao Shi05dd4442017-02-06 22:50:07 +000067 * \pre execute has not been invoked
68 */
69 Code
70 execute(uint64_t faceId);
71
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040072 /** \brief Find face by FaceId or FaceUri.
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040073 * \param faceIdOrUri either a FaceId (uint64_t) or a FaceUri
Junxiao Shi084b7952017-02-26 22:00:53 +000074 * \param allowMulti effective only if \p faceIdOrUri contains a FaceUri
Davide Pesaventod6ea0b12023-03-13 21:35:03 -040075 * \throw std::bad_any_cast faceIdOrUri is neither uint64_t nor FaceUri
Junxiao Shi918e5d42017-02-25 03:58:21 +000076 */
77 Code
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040078 execute(const std::any& faceIdOrUri, bool allowMulti = false);
Junxiao Shi918e5d42017-02-25 03:58:21 +000079
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040080 /** \brief Find face by FaceQueryFilter.
Junxiao Shi05dd4442017-02-06 22:50:07 +000081 * \pre execute has not been invoked
82 */
83 Code
84 execute(const FaceQueryFilter& filter, bool allowMulti = false);
85
86 /** \return face status for all results
87 */
88 const std::vector<FaceStatus>&
89 getResults() const
90 {
91 return m_results;
92 }
93
Junxiao Shi1d62e622017-03-08 22:39:28 +000094 /** \return FaceId for all results
95 */
96 std::set<uint64_t>
97 getFaceIds() const;
98
Junxiao Shi05dd4442017-02-06 22:50:07 +000099 /** \return a single face status
100 * \pre getResults().size() == 1
101 */
102 const FaceStatus&
103 getFaceStatus() const;
104
105 uint64_t
106 getFaceId() const
107 {
108 return this->getFaceStatus().getFaceId();
109 }
110
111 const std::string&
112 getErrorReason() const
113 {
114 return m_errorReason;
115 }
116
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400117 /** \brief Print results for disambiguation.
Junxiao Shi05dd4442017-02-06 22:50:07 +0000118 */
119 void
120 printDisambiguation(std::ostream& os, DisambiguationStyle style) const;
121
122private:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400123 std::optional<FaceUri>
Eric Newberryd656aff2020-04-03 00:30:38 -0700124 canonize(const std::string& fieldName, const FaceUri& uri);
Junxiao Shi05dd4442017-02-06 22:50:07 +0000125
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400126 /** \brief Retrieve FaceStatus from filter.
Junxiao Shi05dd4442017-02-06 22:50:07 +0000127 * \post m_res == Code::OK and m_results is populated if retrieval succeeds
128 * \post m_res == Code::ERROR and m_errorReason is set if retrieval fails
129 */
130 void
131 query();
132
133private:
134 ExecuteContext& m_ctx;
135 FaceQueryFilter m_filter;
136 Code m_res = Code::NOT_STARTED;
137 std::vector<FaceStatus> m_results;
138 std::string m_errorReason;
139};
140
Davide Pesavento990620a2022-06-05 00:25:53 -0400141/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400142 * \brief Canonize a FaceUri.
Davide Pesavento990620a2022-06-05 00:25:53 -0400143 * \return canonical FaceUri (nullopt on failure) and error string
144 */
145std::pair<std::optional<FaceUri>, std::string>
146canonize(ExecuteContext& ctx, const FaceUri& uri);
147
148/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400149 * \brief Helper to generate exit code and error message for face canonization failures.
Davide Pesavento990620a2022-06-05 00:25:53 -0400150 * \param uri The FaceUri
151 * \param error The error string returned by the canonization process
152 * \param field An optional field identifier to include with the message
153 * \return exit code and error message
154 */
155std::pair<FindFace::Code, std::string>
156canonizeErrorHelper(const FaceUri& uri,
157 const std::string& error,
158 const std::string& field = "");
159
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400160} // namespace nfd::tools::nfdc
Junxiao Shi05dd4442017-02-06 22:50:07 +0000161
Davide Pesavento990620a2022-06-05 00:25:53 -0400162#endif // NFD_TOOLS_NFDC_FACE_HELPERS_HPP