tools: nfdc status commands

refs #3780

Change-Id: I5222aacc0a7134978e9496c68a3b915dac4aee18
diff --git a/tools/nfdc/available-commands.cpp b/tools/nfdc/available-commands.cpp
index 3cd540a..23d55e3 100644
--- a/tools/nfdc/available-commands.cpp
+++ b/tools/nfdc/available-commands.cpp
@@ -25,78 +25,20 @@
 
 #include "available-commands.hpp"
 #include "help.hpp"
-#include "status-report.hpp"
-#include "status-main.hpp"
+#include "status.hpp"
+#include "legacy-status.hpp"
 #include "legacy-nfdc.hpp"
 
 namespace nfd {
 namespace tools {
 namespace nfdc {
 
-static int
-statusReport(ExecuteContext& ctx)
-{
-  ReportFormat fmt = ctx.args.get<ReportFormat>("format", ReportFormat::TEXT);
-  switch (fmt) {
-    case ReportFormat::XML:
-      return statusMain(std::vector<std::string>{"-x"}, ctx.face, ctx.keyChain);
-    case ReportFormat::TEXT:
-      return statusMain(std::vector<std::string>{}, ctx.face, ctx.keyChain);
-  }
-  BOOST_ASSERT(false);
-  return 1;
-}
-
-static int
-statusList(ExecuteContext& ctx, const std::string& option)
-{
-  return statusMain(std::vector<std::string>{option}, ctx.face, ctx.keyChain);
-}
-
-static int
-legacyNfdStatus(ExecuteContext& ctx)
-{
-  auto args = ctx.args.get<std::vector<std::string>>("args");
-  return statusMain(args, ctx.face, ctx.keyChain);
-}
-
 void
 registerCommands(CommandParser& parser)
 {
   registerHelpCommand(parser);
-
-  CommandDefinition defStatusReport("status", "report");
-  defStatusReport
-    .setTitle("print NFD status report")
-    .addArg("format", ArgValueType::REPORT_FORMAT, Required::NO, Positional::YES);
-  parser.addCommand(defStatusReport, &statusReport);
-
-  struct StatusCommandDefinition
-  {
-    std::string noun;
-    std::string verb;
-    std::string legacyOption;
-    std::string title;
-  };
-  const std::vector<StatusCommandDefinition> statusCommands{
-    {"status", "show", "-v", "print general status"},
-    {"face", "list", "-f", "print face list"},
-    {"channel", "list", "-c", "print channel list"},
-    {"strategy", "list", "-s", "print strategy choices"},
-    {"fib", "list", "-b", "print FIB entries"},
-    {"route", "list", "-r", "print RIB routes"}
-  };
-  for (const StatusCommandDefinition& scd : statusCommands) {
-    CommandDefinition def(scd.noun, scd.verb);
-    def.setTitle(scd.title);
-    parser.addCommand(def, bind(&statusList, _1, scd.legacyOption));
-  }
-  parser.addAlias("status", "show", "list");
-
-  CommandDefinition defLegacyNfdStatus("legacy-nfd-status", "");
-  defLegacyNfdStatus
-    .addArg("args", ArgValueType::ANY, Required::NO, Positional::YES);
-  parser.addCommand(defLegacyNfdStatus, &legacyNfdStatus, AVAILABLE_IN_ALL & ~AVAILABLE_IN_HELP);
+  registerStatusCommands(parser);
+  registerLegacyStatusCommand(parser);
 
   struct LegacyNfdcCommandDefinition
   {
diff --git a/tools/nfdc/status-main.cpp b/tools/nfdc/legacy-status.cpp
similarity index 61%
rename from tools/nfdc/status-main.cpp
rename to tools/nfdc/legacy-status.cpp
index c39f694..5eaa581 100644
--- a/tools/nfdc/status-main.cpp
+++ b/tools/nfdc/legacy-status.cpp
@@ -23,34 +23,15 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "status-main.hpp"
+#include "legacy-status.hpp"
 #include "core/version.hpp"
-#include "status-report.hpp"
-#include "forwarder-general-module.hpp"
-#include "channel-module.hpp"
-#include "face-module.hpp"
-#include "fib-module.hpp"
-#include "rib-module.hpp"
-#include "strategy-choice-module.hpp"
 
-#include <ndn-cxx/security/validator-null.hpp>
 #include <boost/program_options.hpp>
 
 namespace nfd {
 namespace tools {
 namespace nfdc {
 
-struct Options
-{
-  ReportFormat output = ReportFormat::TEXT;
-  bool wantForwarderGeneral = false;
-  bool wantChannels = false;
-  bool wantFaces = false;
-  bool wantFib = false;
-  bool wantRib = false;
-  bool wantStrategyChoice = false;
-};
-
 static void
 showUsage(std::ostream& os, const boost::program_options::options_description& cmdOptions)
 {
@@ -59,17 +40,17 @@
      << cmdOptions;
 }
 
-/** \brief parse command line, and show usage if necessary
+/** \brief parse legacy nfd-status command line, and show usage if necessary
  *  \return if first item is -1, caller should retrieve and display StatusReport;
  *          otherwise, caller should immediately exit with the specified exit code
  */
-static std::tuple<int, Options>
+static std::tuple<int, StatusReportOptions>
 parseCommandLine(const std::vector<std::string>& args)
 {
-  Options options;
+  StatusReportOptions options;
 
   namespace po = boost::program_options;
-  po::options_description cmdOptions("Options");
+  po::options_description cmdOptions("StatusReportOptions");
   cmdOptions.add_options()
     ("help,h", "print this help message")
     ("version,V", "show program version")
@@ -113,69 +94,30 @@
   return std::make_tuple(-1, options);
 }
 
-int
-statusMain(const std::vector<std::string>& args, Face& face, KeyChain& keyChain)
+/** \brief the 'legacy-nfd-status' command
+ */
+static int
+legacyNfdStatus(ExecuteContext& ctx)
 {
+  auto args = ctx.args.get<std::vector<std::string>>("args");
+
   int exitCode = -1;
-  Options options;
+  StatusReportOptions options;
   std::tie(exitCode, options) = parseCommandLine(args);
   if (exitCode >= 0) {
     return exitCode;
   }
 
-  unique_ptr<Validator> validator = make_unique<ndn::ValidatorNull>();
-  CommandOptions ctrlOptions;
+  return reportStatus(ctx, options);
+}
 
-  StatusReport report;
-
-  if (options.wantForwarderGeneral) {
-    auto nfdIdCollector = make_unique<NfdIdCollector>(std::move(validator));
-    auto forwarderGeneralModule = make_unique<ForwarderGeneralModule>();
-    forwarderGeneralModule->setNfdIdCollector(*nfdIdCollector);
-    report.sections.push_back(std::move(forwarderGeneralModule));
-    validator = std::move(nfdIdCollector);
-  }
-
-  if (options.wantChannels) {
-    report.sections.push_back(make_unique<ChannelModule>());
-  }
-
-  if (options.wantFaces) {
-    report.sections.push_back(make_unique<FaceModule>());
-  }
-
-  if (options.wantFib) {
-    report.sections.push_back(make_unique<FibModule>());
-  }
-
-  if (options.wantRib) {
-    report.sections.push_back(make_unique<RibModule>());
-  }
-
-  if (options.wantStrategyChoice) {
-    report.sections.push_back(make_unique<StrategyChoiceModule>());
-  }
-
-  uint32_t code = report.collect(face, keyChain, *validator, ctrlOptions);
-  if (code != 0) {
-    // Give a simple error code for end user.
-    // Technical support personnel:
-    // 1. get the exact command from end user
-    // 2. code div 1000000 is zero-based section index
-    // 3. code mod 1000000 is a Controller.fetch error code
-    std::cerr << "Error while collecting status report (" << code << ").\n";
-    return 1;
-  }
-
-  switch (options.output) {
-    case ReportFormat::XML:
-      report.formatXml(std::cout);
-      break;
-    case ReportFormat::TEXT:
-      report.formatText(std::cout);
-      break;
-  }
-  return 0;
+void
+registerLegacyStatusCommand(CommandParser& parser)
+{
+  CommandDefinition defLegacyNfdStatus("legacy-nfd-status", "");
+  defLegacyNfdStatus
+    .addArg("args", ArgValueType::ANY, Required::NO, Positional::YES);
+  parser.addCommand(defLegacyNfdStatus, &legacyNfdStatus, AVAILABLE_IN_ALL & ~AVAILABLE_IN_HELP);
 }
 
 } // namespace nfdc
diff --git a/tools/nfdc/status-main.hpp b/tools/nfdc/legacy-status.hpp
similarity index 84%
rename from tools/nfdc/status-main.hpp
rename to tools/nfdc/legacy-status.hpp
index 8294a2c..e200292 100644
--- a/tools/nfdc/status-main.hpp
+++ b/tools/nfdc/legacy-status.hpp
@@ -23,20 +23,22 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef NFD_TOOLS_NFDC_STATUS_MAIN_HPP
-#define NFD_TOOLS_NFDC_STATUS_MAIN_HPP
+#ifndef NFD_TOOLS_NFDC_LEGACY_STATUS_HPP
+#define NFD_TOOLS_NFDC_LEGACY_STATUS_HPP
 
-#include "execute-command.hpp"
+#include "status.hpp"
 
 namespace nfd {
 namespace tools {
 namespace nfdc {
 
-int
-statusMain(const std::vector<std::string>& args, Face& face, KeyChain& keyChain);
+/** \brief registers 'legacy-nfd-status' command
+ */
+void
+registerLegacyStatusCommand(CommandParser& parser);
 
 } // namespace nfdc
 } // namespace tools
 } // namespace nfd
 
-#endif // NFD_TOOLS_NFDC_STATUS_MAIN_HPP
+#endif // NFD_TOOLS_NFDC_LEGACY_STATUS_HPP
diff --git a/tools/nfdc/status.cpp b/tools/nfdc/status.cpp
new file mode 100644
index 0000000..c14faa3
--- /dev/null
+++ b/tools/nfdc/status.cpp
@@ -0,0 +1,163 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "status.hpp"
+#include "forwarder-general-module.hpp"
+#include "channel-module.hpp"
+#include "face-module.hpp"
+#include "fib-module.hpp"
+#include "rib-module.hpp"
+#include "strategy-choice-module.hpp"
+
+#include <ndn-cxx/security/validator-null.hpp>
+
+namespace nfd {
+namespace tools {
+namespace nfdc {
+
+int
+reportStatus(ExecuteContext& ctx, const StatusReportOptions& options)
+{
+  unique_ptr<Validator> validator = make_unique<ndn::ValidatorNull>();
+  CommandOptions ctrlOptions;
+
+  StatusReport report;
+
+  if (options.wantForwarderGeneral) {
+    auto nfdIdCollector = make_unique<NfdIdCollector>(std::move(validator));
+    auto forwarderGeneralModule = make_unique<ForwarderGeneralModule>();
+    forwarderGeneralModule->setNfdIdCollector(*nfdIdCollector);
+    report.sections.push_back(std::move(forwarderGeneralModule));
+    validator = std::move(nfdIdCollector);
+  }
+
+  if (options.wantChannels) {
+    report.sections.push_back(make_unique<ChannelModule>());
+  }
+
+  if (options.wantFaces) {
+    report.sections.push_back(make_unique<FaceModule>());
+  }
+
+  if (options.wantFib) {
+    report.sections.push_back(make_unique<FibModule>());
+  }
+
+  if (options.wantRib) {
+    report.sections.push_back(make_unique<RibModule>());
+  }
+
+  if (options.wantStrategyChoice) {
+    report.sections.push_back(make_unique<StrategyChoiceModule>());
+  }
+
+  uint32_t code = report.collect(ctx.face, ctx.keyChain, *validator, ctrlOptions);
+  if (code != 0) {
+    // Give a simple error code for end user.
+    // Technical support personnel:
+    // 1. get the exact command from end user
+    // 2. code div 1000000 is zero-based section index
+    // 3. code mod 1000000 is a Controller.fetch error code
+    std::cerr << "Error while collecting status report (" << code << ").\n";
+    return 1;
+  }
+
+  switch (options.output) {
+    case ReportFormat::XML:
+      report.formatXml(std::cout);
+      break;
+    case ReportFormat::TEXT:
+      report.formatText(std::cout);
+      break;
+  }
+  return 0;
+}
+
+/** \brief single-section status command
+ */
+static int
+reportStatusSingleSection(ExecuteContext& ctx, bool StatusReportOptions::*wantSection)
+{
+  StatusReportOptions options;
+  options.*wantSection = true;
+  return reportStatus(ctx, options);
+}
+
+/** \brief the 'status report' command
+ */
+static int
+reportStatusComprehensive(ExecuteContext& ctx)
+{
+  StatusReportOptions options;
+  options.output = ctx.args.get<ReportFormat>("format", ReportFormat::TEXT);
+  options.wantForwarderGeneral = options.wantChannels = options.wantFaces =
+    options.wantFib = options.wantRib = options.wantStrategyChoice = true;
+  return reportStatus(ctx, options);
+}
+
+void
+registerStatusCommands(CommandParser& parser)
+{
+  CommandDefinition defStatusReport("status", "report");
+  defStatusReport
+    .setTitle("print NFD status report")
+    .addArg("format", ArgValueType::REPORT_FORMAT, Required::NO, Positional::YES);
+  parser.addCommand(defStatusReport, &reportStatusComprehensive);
+
+  CommandDefinition defStatusShow("status", "show");
+  defStatusShow
+    .setTitle("print general status");
+  parser.addCommand(defStatusShow, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantForwarderGeneral));
+  parser.addAlias("status", "show", "list");
+
+  CommandDefinition defFaceList("face", "list");
+  defFaceList
+    .setTitle("print face list");
+  parser.addCommand(defFaceList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantFaces));
+
+  CommandDefinition defChannelList("channel", "list");
+  defChannelList
+    .setTitle("print channel list");
+  parser.addCommand(defChannelList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantChannels));
+
+  CommandDefinition defStrategyList("strategy", "list");
+  defStrategyList
+    .setTitle("print strategy choices");
+  parser.addCommand(defStrategyList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantStrategyChoice));
+
+  CommandDefinition defFibList("fib", "list");
+  defFibList
+    .setTitle("print FIB entries");
+  parser.addCommand(defFibList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantFib));
+
+  CommandDefinition defRouteList("route", "list");
+  defRouteList
+    .setTitle("print RIB entries");
+  parser.addCommand(defRouteList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantRib));
+}
+
+} // namespace nfdc
+} // namespace tools
+} // namespace nfd
diff --git a/tools/nfdc/status-main.hpp b/tools/nfdc/status.hpp
similarity index 63%
copy from tools/nfdc/status-main.hpp
copy to tools/nfdc/status.hpp
index 8294a2c..57d0eb8 100644
--- a/tools/nfdc/status-main.hpp
+++ b/tools/nfdc/status.hpp
@@ -23,20 +23,48 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef NFD_TOOLS_NFDC_STATUS_MAIN_HPP
-#define NFD_TOOLS_NFDC_STATUS_MAIN_HPP
+#ifndef NFD_TOOLS_NFDC_STATUS_HPP
+#define NFD_TOOLS_NFDC_STATUS_HPP
 
-#include "execute-command.hpp"
+#include "status-report.hpp"
+#include "command-parser.hpp"
 
 namespace nfd {
 namespace tools {
 namespace nfdc {
 
+struct StatusReportOptions
+{
+  ReportFormat output = ReportFormat::TEXT;
+  bool wantForwarderGeneral = false;
+  bool wantChannels = false;
+  bool wantFaces = false;
+  bool wantFib = false;
+  bool wantRib = false;
+  bool wantStrategyChoice = false;
+};
+
+/** \brief collect a status report and write to stdout
+ */
 int
-statusMain(const std::vector<std::string>& args, Face& face, KeyChain& keyChain);
+reportStatus(ExecuteContext& ctx, const StatusReportOptions& options);
+
+/** \brief registers status commands
+ *
+ *  Providing the following commands:
+ *  \li status report
+ *  \li status show
+ *  \li face list
+ *  \li channel list
+ *  \li strategy list
+ *  \li fib list
+ *  \li route list
+ */
+void
+registerStatusCommands(CommandParser& parser);
 
 } // namespace nfdc
 } // namespace tools
 } // namespace nfd
 
-#endif // NFD_TOOLS_NFDC_STATUS_MAIN_HPP
+#endif // NFD_TOOLS_NFDC_STATUS_HPP