blob: c39f694108ac5f82ce0c33a2a4b8608cc65798db [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, Regents of the University of California,
4 * 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
Junxiao Shi8c39bf02016-08-28 23:54:13 +000026#include "status-main.hpp"
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000027#include "core/version.hpp"
Junxiao Shi38f4ce92016-08-04 10:01:52 +000028#include "status-report.hpp"
29#include "forwarder-general-module.hpp"
30#include "channel-module.hpp"
31#include "face-module.hpp"
32#include "fib-module.hpp"
33#include "rib-module.hpp"
34#include "strategy-choice-module.hpp"
35
Junxiao Shi8c39bf02016-08-28 23:54:13 +000036#include <ndn-cxx/security/validator-null.hpp>
37#include <boost/program_options.hpp>
38
Junxiao Shi38f4ce92016-08-04 10:01:52 +000039namespace nfd {
40namespace tools {
Junxiao Shi331ade72016-08-19 14:07:19 +000041namespace nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000042
Junxiao Shi38f4ce92016-08-04 10:01:52 +000043struct Options
44{
Junxiao Shi8c39bf02016-08-28 23:54:13 +000045 ReportFormat output = ReportFormat::TEXT;
Junxiao Shi38f4ce92016-08-04 10:01:52 +000046 bool wantForwarderGeneral = false;
47 bool wantChannels = false;
48 bool wantFaces = false;
49 bool wantFib = false;
50 bool wantRib = false;
51 bool wantStrategyChoice = false;
52};
53
54static void
55showUsage(std::ostream& os, const boost::program_options::options_description& cmdOptions)
56{
57 os << "Usage: nfd-status [options]\n\n"
58 << "Show NFD version and status information.\n\n"
59 << cmdOptions;
60}
61
62/** \brief parse command line, and show usage if necessary
63 * \return if first item is -1, caller should retrieve and display StatusReport;
64 * otherwise, caller should immediately exit with the specified exit code
65 */
66static std::tuple<int, Options>
Junxiao Shi2f741322016-08-29 00:53:18 +000067parseCommandLine(const std::vector<std::string>& args)
Junxiao Shi38f4ce92016-08-04 10:01:52 +000068{
69 Options options;
70
71 namespace po = boost::program_options;
72 po::options_description cmdOptions("Options");
73 cmdOptions.add_options()
74 ("help,h", "print this help message")
75 ("version,V", "show program version")
76 ("general,v", po::bool_switch(&options.wantForwarderGeneral), "show general status")
77 ("channels,c", po::bool_switch(&options.wantChannels), "show channels")
78 ("faces,f", po::bool_switch(&options.wantFaces), "show faces")
79 ("fib,b", po::bool_switch(&options.wantFib), "show FIB entries")
80 ("rib,r", po::bool_switch(&options.wantRib), "show RIB routes")
81 ("sc,s", po::bool_switch(&options.wantStrategyChoice), "show strategy choice entries")
82 ("xml,x", "output as XML instead of text (implies -vcfbrs)");
83 po::variables_map vm;
84 try {
Junxiao Shi2f741322016-08-29 00:53:18 +000085 po::store(po::command_line_parser(args).options(cmdOptions).run(), vm);
86 po::notify(vm);
Junxiao Shi38f4ce92016-08-04 10:01:52 +000087 }
88 catch (const po::error& e) {
89 std::cerr << e.what() << "\n";
90 showUsage(std::cerr, cmdOptions);
91 return std::make_tuple(2, options);
92 }
Junxiao Shi38f4ce92016-08-04 10:01:52 +000093
94 if (vm.count("help") > 0) {
95 showUsage(std::cout, cmdOptions);
96 return std::make_tuple(0, options);
97 }
98 if (vm.count("version") > 0) {
99 std::cout << "nfd-status " << NFD_VERSION_BUILD_STRING << "\n";
100 return std::make_tuple(0, options);
101 }
102
103 if (vm.count("xml") > 0) {
Junxiao Shi8c39bf02016-08-28 23:54:13 +0000104 options.output = ReportFormat::XML;
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000105 }
Junxiao Shi8c39bf02016-08-28 23:54:13 +0000106 if (options.output == ReportFormat::XML ||
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000107 (!options.wantForwarderGeneral && !options.wantChannels && !options.wantFaces &&
108 !options.wantFib && !options.wantRib && !options.wantStrategyChoice)) {
109 options.wantForwarderGeneral = options.wantChannels = options.wantFaces =
110 options.wantFib = options.wantRib = options.wantStrategyChoice = true;
111 }
112
113 return std::make_tuple(-1, options);
114}
115
Junxiao Shi331ade72016-08-19 14:07:19 +0000116int
Junxiao Shi737c43c2016-09-14 02:51:44 +0000117statusMain(const std::vector<std::string>& args, Face& face, KeyChain& keyChain)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000118{
119 int exitCode = -1;
120 Options options;
Junxiao Shi2f741322016-08-29 00:53:18 +0000121 std::tie(exitCode, options) = parseCommandLine(args);
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000122 if (exitCode >= 0) {
123 return exitCode;
124 }
125
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000126 unique_ptr<Validator> validator = make_unique<ndn::ValidatorNull>();
127 CommandOptions ctrlOptions;
128
129 StatusReport report;
130
131 if (options.wantForwarderGeneral) {
132 auto nfdIdCollector = make_unique<NfdIdCollector>(std::move(validator));
133 auto forwarderGeneralModule = make_unique<ForwarderGeneralModule>();
134 forwarderGeneralModule->setNfdIdCollector(*nfdIdCollector);
135 report.sections.push_back(std::move(forwarderGeneralModule));
136 validator = std::move(nfdIdCollector);
137 }
138
139 if (options.wantChannels) {
140 report.sections.push_back(make_unique<ChannelModule>());
141 }
142
143 if (options.wantFaces) {
144 report.sections.push_back(make_unique<FaceModule>());
145 }
146
147 if (options.wantFib) {
148 report.sections.push_back(make_unique<FibModule>());
149 }
150
151 if (options.wantRib) {
152 report.sections.push_back(make_unique<RibModule>());
153 }
154
155 if (options.wantStrategyChoice) {
156 report.sections.push_back(make_unique<StrategyChoiceModule>());
157 }
158
159 uint32_t code = report.collect(face, keyChain, *validator, ctrlOptions);
160 if (code != 0) {
161 // Give a simple error code for end user.
162 // Technical support personnel:
163 // 1. get the exact command from end user
164 // 2. code div 1000000 is zero-based section index
165 // 3. code mod 1000000 is a Controller.fetch error code
166 std::cerr << "Error while collecting status report (" << code << ").\n";
167 return 1;
168 }
169
170 switch (options.output) {
Junxiao Shi8c39bf02016-08-28 23:54:13 +0000171 case ReportFormat::XML:
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000172 report.formatXml(std::cout);
173 break;
Junxiao Shi8c39bf02016-08-28 23:54:13 +0000174 case ReportFormat::TEXT:
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000175 report.formatText(std::cout);
176 break;
177 }
178 return 0;
179}
180
Junxiao Shi331ade72016-08-19 14:07:19 +0000181} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000182} // namespace tools
183} // namespace nfd