blob: 0f87ab10d3f08c0303e7590a266b398fc616d309 [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 Shi2f741322016-08-29 00:53:18 +0000117statusMain(const std::vector<std::string>& args)
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
126 Face face;
127 KeyChain keyChain;
128 unique_ptr<Validator> validator = make_unique<ndn::ValidatorNull>();
129 CommandOptions ctrlOptions;
130
131 StatusReport report;
132
133 if (options.wantForwarderGeneral) {
134 auto nfdIdCollector = make_unique<NfdIdCollector>(std::move(validator));
135 auto forwarderGeneralModule = make_unique<ForwarderGeneralModule>();
136 forwarderGeneralModule->setNfdIdCollector(*nfdIdCollector);
137 report.sections.push_back(std::move(forwarderGeneralModule));
138 validator = std::move(nfdIdCollector);
139 }
140
141 if (options.wantChannels) {
142 report.sections.push_back(make_unique<ChannelModule>());
143 }
144
145 if (options.wantFaces) {
146 report.sections.push_back(make_unique<FaceModule>());
147 }
148
149 if (options.wantFib) {
150 report.sections.push_back(make_unique<FibModule>());
151 }
152
153 if (options.wantRib) {
154 report.sections.push_back(make_unique<RibModule>());
155 }
156
157 if (options.wantStrategyChoice) {
158 report.sections.push_back(make_unique<StrategyChoiceModule>());
159 }
160
161 uint32_t code = report.collect(face, keyChain, *validator, ctrlOptions);
162 if (code != 0) {
163 // Give a simple error code for end user.
164 // Technical support personnel:
165 // 1. get the exact command from end user
166 // 2. code div 1000000 is zero-based section index
167 // 3. code mod 1000000 is a Controller.fetch error code
168 std::cerr << "Error while collecting status report (" << code << ").\n";
169 return 1;
170 }
171
172 switch (options.output) {
Junxiao Shi8c39bf02016-08-28 23:54:13 +0000173 case ReportFormat::XML:
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000174 report.formatXml(std::cout);
175 break;
Junxiao Shi8c39bf02016-08-28 23:54:13 +0000176 case ReportFormat::TEXT:
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000177 report.formatText(std::cout);
178 break;
179 }
180 return 0;
181}
182
Junxiao Shi331ade72016-08-19 14:07:19 +0000183} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000184} // namespace tools
185} // namespace nfd