blob: 1f54e0a15dd217061243d3bc6801f729507cedcb [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 Pesavento40641272023-03-16 13:31:12 -040031#include <ndn-cxx/mgmt/nfd/face-query-filter.hpp>
32#include <ndn-cxx/mgmt/nfd/face-status.hpp>
33
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::tools::nfdc {
Junxiao Shi05dd4442017-02-06 22:50:07 +000035
36using ndn::nfd::FaceQueryFilter;
37using ndn::nfd::FaceStatus;
38
Davide Pesavento990620a2022-06-05 00:25:53 -040039/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040040 * \brief Procedure to find a face.
Junxiao Shi05dd4442017-02-06 22:50:07 +000041 */
42class FindFace : noncopyable
43{
44public:
45 enum class Code {
46 OK = 0, ///< found exactly one face, or found multiple faces when allowMulti is true
47 ERROR = 1, ///< unspecified error
48 NOT_FOUND = 3, ///< found zero face
49 CANONIZE_ERROR = 4, ///< error during FaceUri canonization
50 AMBIGUOUS = 5, ///< found multiple faces and allowMulti is false
51 NOT_STARTED = -1, ///< for internal use
52 IN_PROGRESS = -2, ///< for internal use
53 };
54
55 enum class DisambiguationStyle
56 {
57 LOCAL_URI = 1 ///< print FaceId and LocalUri
58 };
59
60 explicit
61 FindFace(ExecuteContext& ctx);
62
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040063 /** \brief Find face by FaceUri.
Junxiao Shi05dd4442017-02-06 22:50:07 +000064 * \pre execute has not been invoked
65 */
66 Code
67 execute(const FaceUri& faceUri, bool allowMulti = false);
68
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040069 /** \brief Find face by FaceId.
Junxiao Shi05dd4442017-02-06 22:50:07 +000070 * \pre execute has not been invoked
71 */
72 Code
73 execute(uint64_t faceId);
74
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040075 /** \brief Find face by FaceId or FaceUri.
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040076 * \param faceIdOrUri either a FaceId (uint64_t) or a FaceUri
Junxiao Shi084b7952017-02-26 22:00:53 +000077 * \param allowMulti effective only if \p faceIdOrUri contains a FaceUri
Davide Pesaventod6ea0b12023-03-13 21:35:03 -040078 * \throw std::bad_any_cast faceIdOrUri is neither uint64_t nor FaceUri
Junxiao Shi918e5d42017-02-25 03:58:21 +000079 */
80 Code
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040081 execute(const std::any& faceIdOrUri, bool allowMulti = false);
Junxiao Shi918e5d42017-02-25 03:58:21 +000082
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040083 /** \brief Find face by FaceQueryFilter.
Junxiao Shi05dd4442017-02-06 22:50:07 +000084 * \pre execute has not been invoked
85 */
86 Code
87 execute(const FaceQueryFilter& filter, bool allowMulti = false);
88
89 /** \return face status for all results
90 */
91 const std::vector<FaceStatus>&
92 getResults() const
93 {
94 return m_results;
95 }
96
Junxiao Shi1d62e622017-03-08 22:39:28 +000097 /** \return FaceId for all results
98 */
99 std::set<uint64_t>
100 getFaceIds() const;
101
Junxiao Shi05dd4442017-02-06 22:50:07 +0000102 /** \return a single face status
103 * \pre getResults().size() == 1
104 */
105 const FaceStatus&
106 getFaceStatus() const;
107
108 uint64_t
109 getFaceId() const
110 {
111 return this->getFaceStatus().getFaceId();
112 }
113
114 const std::string&
115 getErrorReason() const
116 {
117 return m_errorReason;
118 }
119
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400120 /** \brief Print results for disambiguation.
Junxiao Shi05dd4442017-02-06 22:50:07 +0000121 */
122 void
123 printDisambiguation(std::ostream& os, DisambiguationStyle style) const;
124
125private:
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400126 std::optional<FaceUri>
Eric Newberryd656aff2020-04-03 00:30:38 -0700127 canonize(const std::string& fieldName, const FaceUri& uri);
Junxiao Shi05dd4442017-02-06 22:50:07 +0000128
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400129 /** \brief Retrieve FaceStatus from filter.
Junxiao Shi05dd4442017-02-06 22:50:07 +0000130 * \post m_res == Code::OK and m_results is populated if retrieval succeeds
131 * \post m_res == Code::ERROR and m_errorReason is set if retrieval fails
132 */
133 void
134 query();
135
136private:
137 ExecuteContext& m_ctx;
138 FaceQueryFilter m_filter;
139 Code m_res = Code::NOT_STARTED;
140 std::vector<FaceStatus> m_results;
141 std::string m_errorReason;
142};
143
Davide Pesavento990620a2022-06-05 00:25:53 -0400144/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400145 * \brief Canonize a FaceUri.
Davide Pesavento990620a2022-06-05 00:25:53 -0400146 * \return canonical FaceUri (nullopt on failure) and error string
147 */
148std::pair<std::optional<FaceUri>, std::string>
149canonize(ExecuteContext& ctx, const FaceUri& uri);
150
151/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400152 * \brief Helper to generate exit code and error message for face canonization failures.
Davide Pesavento990620a2022-06-05 00:25:53 -0400153 * \param uri The FaceUri
154 * \param error The error string returned by the canonization process
155 * \param field An optional field identifier to include with the message
156 * \return exit code and error message
157 */
158std::pair<FindFace::Code, std::string>
159canonizeErrorHelper(const FaceUri& uri,
160 const std::string& error,
161 const std::string& field = "");
162
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400163} // namespace nfd::tools::nfdc
Junxiao Shi05dd4442017-02-06 22:50:07 +0000164
Davide Pesavento990620a2022-06-05 00:25:53 -0400165#endif // NFD_TOOLS_NFDC_FACE_HELPERS_HPP