blob: c4e25f593c5e358b8ab5e8b7203720e21f2b804e [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 Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050029#include "core/common.hpp"
Junxiao Shi05dd4442017-02-06 22:50:07 +000030#include "execute-command.hpp"
31
Davide Pesavento40641272023-03-16 13:31:12 -040032#include <ndn-cxx/mgmt/nfd/face-query-filter.hpp>
33#include <ndn-cxx/mgmt/nfd/face-status.hpp>
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050034#include <ndn-cxx/net/face-uri.hpp>
35
36#include <set>
Davide Pesavento40641272023-03-16 13:31:12 -040037
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038namespace nfd::tools::nfdc {
Junxiao Shi05dd4442017-02-06 22:50:07 +000039
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050040using ndn::FaceUri;
Junxiao Shi05dd4442017-02-06 22:50:07 +000041using ndn::nfd::FaceQueryFilter;
42using ndn::nfd::FaceStatus;
43
Davide Pesavento990620a2022-06-05 00:25:53 -040044/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040045 * \brief Procedure to find a face.
Junxiao Shi05dd4442017-02-06 22:50:07 +000046 */
47class FindFace : noncopyable
48{
49public:
50 enum class Code {
51 OK = 0, ///< found exactly one face, or found multiple faces when allowMulti is true
52 ERROR = 1, ///< unspecified error
53 NOT_FOUND = 3, ///< found zero face
54 CANONIZE_ERROR = 4, ///< error during FaceUri canonization
55 AMBIGUOUS = 5, ///< found multiple faces and allowMulti is false
56 NOT_STARTED = -1, ///< for internal use
57 IN_PROGRESS = -2, ///< for internal use
58 };
59
60 enum class DisambiguationStyle
61 {
62 LOCAL_URI = 1 ///< print FaceId and LocalUri
63 };
64
65 explicit
66 FindFace(ExecuteContext& ctx);
67
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040068 /** \brief Find face by FaceUri.
Junxiao Shi05dd4442017-02-06 22:50:07 +000069 * \pre execute has not been invoked
70 */
71 Code
72 execute(const FaceUri& faceUri, bool allowMulti = false);
73
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040074 /** \brief Find face by FaceId.
Junxiao Shi05dd4442017-02-06 22:50:07 +000075 * \pre execute has not been invoked
76 */
77 Code
78 execute(uint64_t faceId);
79
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040080 /** \brief Find face by FaceId or FaceUri.
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040081 * \param faceIdOrUri either a FaceId (uint64_t) or a FaceUri
Junxiao Shi084b7952017-02-26 22:00:53 +000082 * \param allowMulti effective only if \p faceIdOrUri contains a FaceUri
Davide Pesaventod6ea0b12023-03-13 21:35:03 -040083 * \throw std::bad_any_cast faceIdOrUri is neither uint64_t nor FaceUri
Junxiao Shi918e5d42017-02-25 03:58:21 +000084 */
85 Code
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040086 execute(const std::any& faceIdOrUri, bool allowMulti = false);
Junxiao Shi918e5d42017-02-25 03:58:21 +000087
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040088 /** \brief Find face by FaceQueryFilter.
Junxiao Shi05dd4442017-02-06 22:50:07 +000089 * \pre execute has not been invoked
90 */
91 Code
92 execute(const FaceQueryFilter& filter, bool allowMulti = false);
93
94 /** \return face status for all results
95 */
96 const std::vector<FaceStatus>&
97 getResults() const
98 {
99 return m_results;
100 }
101
Junxiao Shi1d62e622017-03-08 22:39:28 +0000102 /** \return FaceId for all results
103 */
104 std::set<uint64_t>
105 getFaceIds() const;
106
Junxiao Shi05dd4442017-02-06 22:50:07 +0000107 /** \return a single face status
108 * \pre getResults().size() == 1
109 */
110 const FaceStatus&
111 getFaceStatus() const;
112
113 uint64_t
114 getFaceId() const
115 {
116 return this->getFaceStatus().getFaceId();
117 }
118
119 const std::string&
120 getErrorReason() const
121 {
122 return m_errorReason;
123 }
124
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400125 /** \brief Print results for disambiguation.
Junxiao Shi05dd4442017-02-06 22:50:07 +0000126 */
127 void
128 printDisambiguation(std::ostream& os, DisambiguationStyle style) const;
129
130private:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400131 std::optional<FaceUri>
Eric Newberryd656aff2020-04-03 00:30:38 -0700132 canonize(const std::string& fieldName, const FaceUri& uri);
Junxiao Shi05dd4442017-02-06 22:50:07 +0000133
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400134 /** \brief Retrieve FaceStatus from filter.
Junxiao Shi05dd4442017-02-06 22:50:07 +0000135 * \post m_res == Code::OK and m_results is populated if retrieval succeeds
136 * \post m_res == Code::ERROR and m_errorReason is set if retrieval fails
137 */
138 void
139 query();
140
141private:
142 ExecuteContext& m_ctx;
143 FaceQueryFilter m_filter;
144 Code m_res = Code::NOT_STARTED;
145 std::vector<FaceStatus> m_results;
146 std::string m_errorReason;
147};
148
Davide Pesavento990620a2022-06-05 00:25:53 -0400149/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400150 * \brief Canonize a FaceUri.
Davide Pesavento990620a2022-06-05 00:25:53 -0400151 * \return canonical FaceUri (nullopt on failure) and error string
152 */
153std::pair<std::optional<FaceUri>, std::string>
154canonize(ExecuteContext& ctx, const FaceUri& uri);
155
156/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400157 * \brief Helper to generate exit code and error message for face canonization failures.
Davide Pesavento990620a2022-06-05 00:25:53 -0400158 * \param uri The FaceUri
159 * \param error The error string returned by the canonization process
160 * \param field An optional field identifier to include with the message
161 * \return exit code and error message
162 */
163std::pair<FindFace::Code, std::string>
164canonizeErrorHelper(const FaceUri& uri,
165 const std::string& error,
166 const std::string& field = "");
167
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400168} // namespace nfd::tools::nfdc
Junxiao Shi05dd4442017-02-06 22:50:07 +0000169
Davide Pesavento990620a2022-06-05 00:25:53 -0400170#endif // NFD_TOOLS_NFDC_FACE_HELPERS_HPP