Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 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 | |
| 26 | #include "status.hpp" |
| 27 | #include "forwarder-general-module.hpp" |
| 28 | #include "channel-module.hpp" |
| 29 | #include "face-module.hpp" |
| 30 | #include "fib-module.hpp" |
| 31 | #include "rib-module.hpp" |
| 32 | #include "strategy-choice-module.hpp" |
| 33 | |
| 34 | #include <ndn-cxx/security/validator-null.hpp> |
| 35 | |
| 36 | namespace nfd { |
| 37 | namespace tools { |
| 38 | namespace nfdc { |
| 39 | |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame^] | 40 | void |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 41 | reportStatus(ExecuteContext& ctx, const StatusReportOptions& options) |
| 42 | { |
| 43 | unique_ptr<Validator> validator = make_unique<ndn::ValidatorNull>(); |
| 44 | CommandOptions ctrlOptions; |
| 45 | |
| 46 | StatusReport report; |
| 47 | |
| 48 | if (options.wantForwarderGeneral) { |
| 49 | auto nfdIdCollector = make_unique<NfdIdCollector>(std::move(validator)); |
| 50 | auto forwarderGeneralModule = make_unique<ForwarderGeneralModule>(); |
| 51 | forwarderGeneralModule->setNfdIdCollector(*nfdIdCollector); |
| 52 | report.sections.push_back(std::move(forwarderGeneralModule)); |
| 53 | validator = std::move(nfdIdCollector); |
| 54 | } |
| 55 | |
| 56 | if (options.wantChannels) { |
| 57 | report.sections.push_back(make_unique<ChannelModule>()); |
| 58 | } |
| 59 | |
| 60 | if (options.wantFaces) { |
| 61 | report.sections.push_back(make_unique<FaceModule>()); |
| 62 | } |
| 63 | |
| 64 | if (options.wantFib) { |
| 65 | report.sections.push_back(make_unique<FibModule>()); |
| 66 | } |
| 67 | |
| 68 | if (options.wantRib) { |
| 69 | report.sections.push_back(make_unique<RibModule>()); |
| 70 | } |
| 71 | |
| 72 | if (options.wantStrategyChoice) { |
| 73 | report.sections.push_back(make_unique<StrategyChoiceModule>()); |
| 74 | } |
| 75 | |
| 76 | uint32_t code = report.collect(ctx.face, ctx.keyChain, *validator, ctrlOptions); |
| 77 | if (code != 0) { |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame^] | 78 | ctx.exitCode = 1; |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 79 | // Give a simple error code for end user. |
| 80 | // Technical support personnel: |
| 81 | // 1. get the exact command from end user |
| 82 | // 2. code div 1000000 is zero-based section index |
| 83 | // 3. code mod 1000000 is a Controller.fetch error code |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame^] | 84 | ctx.err << "Error while collecting status report (" << code << ").\n"; |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | switch (options.output) { |
| 88 | case ReportFormat::XML: |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame^] | 89 | report.formatXml(ctx.out); |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 90 | break; |
| 91 | case ReportFormat::TEXT: |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame^] | 92 | report.formatText(ctx.out); |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 93 | break; |
| 94 | } |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** \brief single-section status command |
| 98 | */ |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame^] | 99 | static void |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 100 | reportStatusSingleSection(ExecuteContext& ctx, bool StatusReportOptions::*wantSection) |
| 101 | { |
| 102 | StatusReportOptions options; |
| 103 | options.*wantSection = true; |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame^] | 104 | reportStatus(ctx, options); |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** \brief the 'status report' command |
| 108 | */ |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame^] | 109 | static void |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 110 | reportStatusComprehensive(ExecuteContext& ctx) |
| 111 | { |
| 112 | StatusReportOptions options; |
| 113 | options.output = ctx.args.get<ReportFormat>("format", ReportFormat::TEXT); |
| 114 | options.wantForwarderGeneral = options.wantChannels = options.wantFaces = |
| 115 | options.wantFib = options.wantRib = options.wantStrategyChoice = true; |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame^] | 116 | reportStatus(ctx, options); |
Junxiao Shi | ae88938 | 2016-11-23 04:52:51 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void |
| 120 | registerStatusCommands(CommandParser& parser) |
| 121 | { |
| 122 | CommandDefinition defStatusReport("status", "report"); |
| 123 | defStatusReport |
| 124 | .setTitle("print NFD status report") |
| 125 | .addArg("format", ArgValueType::REPORT_FORMAT, Required::NO, Positional::YES); |
| 126 | parser.addCommand(defStatusReport, &reportStatusComprehensive); |
| 127 | |
| 128 | CommandDefinition defStatusShow("status", "show"); |
| 129 | defStatusShow |
| 130 | .setTitle("print general status"); |
| 131 | parser.addCommand(defStatusShow, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantForwarderGeneral)); |
| 132 | parser.addAlias("status", "show", "list"); |
| 133 | |
| 134 | CommandDefinition defFaceList("face", "list"); |
| 135 | defFaceList |
| 136 | .setTitle("print face list"); |
| 137 | parser.addCommand(defFaceList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantFaces)); |
| 138 | |
| 139 | CommandDefinition defChannelList("channel", "list"); |
| 140 | defChannelList |
| 141 | .setTitle("print channel list"); |
| 142 | parser.addCommand(defChannelList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantChannels)); |
| 143 | |
| 144 | CommandDefinition defStrategyList("strategy", "list"); |
| 145 | defStrategyList |
| 146 | .setTitle("print strategy choices"); |
| 147 | parser.addCommand(defStrategyList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantStrategyChoice)); |
| 148 | |
| 149 | CommandDefinition defFibList("fib", "list"); |
| 150 | defFibList |
| 151 | .setTitle("print FIB entries"); |
| 152 | parser.addCommand(defFibList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantFib)); |
| 153 | |
| 154 | CommandDefinition defRouteList("route", "list"); |
| 155 | defRouteList |
| 156 | .setTitle("print RIB entries"); |
| 157 | parser.addCommand(defRouteList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantRib)); |
| 158 | } |
| 159 | |
| 160 | } // namespace nfdc |
| 161 | } // namespace tools |
| 162 | } // namespace nfd |